socialAuth.class.php
9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* socialAuth Class
*
* @pattent Abstract Factory
*
* @author Roman
* @version 3.0.20111010
*/
class socialAuth
{
///////////////////////////////////////////////////////////////////////////
private static $settings = array(
// OAuth 2.0 //////////////////////////////////////////////////////////
'vkontakte' => array(
'client_id' => '4393164',
'client_secret' => 'Vb22Sn4L6oqduK737N5T',
'authorize_uri' => 'http://api.vkontakte.ru/oauth/authorize?scope=notify&response_type=code&display=page',
'access_token_uri' => 'https://api.vkontakte.ru/oauth/access_token',
'profile_uri' => 'https://api.vkontakte.ru/method/getProfiles?fields=uid,first_name,last_name,nickname,screen_name,sex,bdate,photo_big',
),
'facebook' => array(
'client_id' => '194750163930252',
'client_secret' => 'b678a74a660f40754e70644339593edb',
'authorize_uri' => 'https://www.facebook.com/dialog/oauth?scope=user_about_me,user_birthday,user_location',
'access_token_uri' => 'https://graph.facebook.com/oauth/access_token',
'profile_uri' => 'https://graph.facebook.com/me',
),
'google' => array(
'client_id' => '1075068890014.apps.googleusercontent.com',
'client_secret' => 'vy1jdGQy_QoA_hJ1itMXjaWm',
'authorize_uri' => 'https://accounts.google.com/o/oauth2/auth?scope=https://www.google.com/m8/feeds/&response_type=code',
'access_token_uri' => 'https://accounts.google.com/o/oauth2/token',
'profile_uri' => 'https://www.google.com/m8/feeds/contacts/default/full',
),
);
private static $redirect_uri = 'http://semena.dev.artwebua.com.ua/customer_login/social/'; // + $mechanism
///////////////////////////////////////////////////////////////////////////
/**
* socialAuth::login()
*
* @author Roman
* @version 1.0.20111012
*
* @param string $mechanism
* @return array
*/
public static function login( $mechanism )
{
///////////////////////////////////////////////////////////////////////
try
{
switch( $mechanism )
{
// OAuth v2.0 /////////////////////////////////////////////////
case 'vkontakte':
$auth = new socialAuthOAuth20Vkontakte( array_merge( self::$settings[$mechanism], array( 'redirect_uri' => self::$redirect_uri.$mechanism ) ) );
break;
case 'facebook':
$auth = new socialAuthOAuth20Facebook( array_merge( self::$settings[$mechanism], array( 'redirect_uri' => self::$redirect_uri.$mechanism ) ) );
break;
case 'google':
$auth = new socialAuthOAuth20Google( array_merge( self::$settings[$mechanism], array( 'redirect_uri' => self::$redirect_uri.$mechanism ) ) );
break;
// OpenID /////////////////////////////////////////////////////
case 'openid':
$auth = new socialAuthOpenIDCustom( isset($_GET['openid_url']) ? trim($_GET['openid_url']) : '' );
break;
case 'openid_finish':
$auth = new socialAuthOpenIDCustomFinish();
break;
case 'yandex':
$auth = new socialAuthOpenIDCustom( 'http://openid.yandex.ru' );
break;
default:
return false;
break;
}
return $auth->init();
}
catch( kException $e )
{
$e->setFlashMessage( 'user_login_social' );
#$e->sendErrorMail();
core::redirect( '@user_login' );
die();
}
///////////////////////////////////////////////////////////////////////
// set flash message
user::setFlash(
core::i18n( 'w23_error_unknown' ),
'user_login_social'
);
core::redirect( '@user_login' );
die();
///////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////
/**
* socialAuth::userLoginOrRegisterIfNotExists()
*
* @author Roman
* @version 1.1.20111007
*
* @param array $data
* @return array
*/
public static function userLoginOrRegisterIfNotExists( $data = array() )
{
// check for exists
$db = db::AutoExecute(
'sp_user_select_check_login',
array(
'login' => 'eid_'.md5(md5($data['login'])),
),
15, 0
);
if( !isset($db['error']) )
{
if( isset($db['data']['0']['unique']) && $db['data']['0']['unique']==0 )
{
// TODO
// show form change user name
}
// register
$db = db::AutoExecute(
'sp_user_insert_social',
array(
'login' => 'eid_'.md5(md5($data['login'])),
'email' => $data['email'],
'name' => $data['name'],
'name_unique' => common::simplifyString( $data['name'] ),
'bithday' => $data['bithday'],
'gender' => $data['gender'],
'region_id' => $data['region_id'],
'about_me' => $data['about_me'],
'interests' => $data['interests'],
'vote' => $data['vote'],
'contact_icq' => $data['contact_icq'],
'options' => $data['options'],
#'avatar' => 'avatar_filename'
'user_ip' => common::getIP(),
)
);
if( !isset($db['error']) )
{
if( !empty($db['data']) && isset($db['data']['1']['id']) )
{
// AVATAR /////////////////////////////////////////////////
$user_id = $db['data']['1']['id'];
$filename = common2::strconvert( $user_id );
$filepath = sfConfig::get('sf_root_dir').'/storage_units/1/avatars/'.fs::getDepthFilePath( $filename, 4, true ).$filename.'/';
fs::createDir( $filepath );
$filename .= '.png';
// generate identicon and save to avatar path
identicon::save( md5(md5(sha1( 'censor_user_'.$user_id ))), 100, $filepath.$filename );
// update user avatar
$db2 = db::AutoExecute(
'sp_user_update_avatar',
array(
'user_id' => $user_id,
'avatar' => $filename,
)
);
// drop and refresh user data
common::dropDependences( 'user', array( 'user_id' => $user_id ) );
// DATA ///////////////////////////////////////////////////
return $db['data']['1'];
}
}
else
{
//user::setFlash( core::i18n( 'w23_error_403', null, 'widgets' ), 'user_login' );
user::setFlash( core::i18n( 'w23_error_500', array( '{{user_email}}' => $data['email'] ), 'widgets', true ), 'user_login' );
}
}
else
{
// login
$db = db::AutoExecute(
'sp_user_select',
array(
'login' => 'eid_'.md5(md5($data['login'])),
'passwd' => '00000000000000000000000000000000',
'user_ip' => common::getIP(),
),
15, 0
);
if( !isset($db['error']) )
{
if( !empty($db['data']) && isset($db['data']['1']['id']) )
{
return $db['data']['1'];
}
}
else
{
user::setFlash( core::i18n( 'w23_error_403', null, 'widgets' ), 'user_login' );
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////
}