These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | (defined('BASEPATH')) OR exit('No direct script access allowed'); |
||
4 | |||
5 | /** |
||
6 | * Image CMS |
||
7 | * Класс авторизации через посторонние сервисы |
||
8 | * @author a.gula <[email protected]> |
||
9 | * @property socauth_model $socauth_model |
||
10 | */ |
||
11 | |||
12 | |||
13 | use OAuth\Common\Consumer\Credentials; |
||
14 | use OAuth\Common\Http\Uri; |
||
15 | use OAuth\Common\Storage\Session; |
||
16 | use OAuth\OAuth2\Service\Facebook; |
||
17 | use OAuth\OAuth2\Service\Vkontakte; |
||
18 | |||
19 | /** |
||
20 | * @property Socauth_model socauth_model |
||
21 | */ |
||
22 | class Socauth extends MY_Controller |
||
23 | { |
||
24 | |||
25 | public $settings; |
||
26 | |||
27 | public $serviceFactory; |
||
28 | |||
29 | public $uriFactory; |
||
30 | |||
31 | public $currentUri; |
||
32 | |||
33 | public function __construct() { |
||
34 | |||
35 | parent::__construct(); |
||
36 | $lang = new MY_Lang(); |
||
37 | $lang->load('socauth'); |
||
38 | $this->load->module('core'); |
||
39 | $this->load->model('socauth_model'); |
||
40 | |||
41 | $this->settings = $this->socauth_model->getSettings(); |
||
42 | $this->serviceFactory = new \OAuth\ServiceFactory(); |
||
43 | $this->uriFactory = new Uri\UriFactory(); |
||
44 | $this->currentUri = $this->uriFactory->createFromSuperGlobalArray($_SERVER); |
||
45 | $this->currentUri->setQuery(''); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @param type $soc type of social service |
||
51 | */ |
||
52 | public function unlink($soc) { |
||
53 | |||
54 | if ($this->dx_auth->is_logged_in()) { |
||
55 | if ($this->socauth_model->delUserSocial($soc)) { |
||
56 | echo json_encode(['answer' => 'sucesfull']); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Just alias (not action, because starts from "_", and now accessable in system) |
||
63 | */ |
||
64 | public function _socAuth($social, $id, $username, $email, $address, $key, $phone, $redirect = true) { |
||
65 | |||
66 | return $this->socAuth($social, $id, $username, $email, $address, $key, $phone, $redirect); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * |
||
71 | * @param type $social social service ID |
||
72 | * @param type $id social service ID |
||
73 | * @param type $username name in social service |
||
74 | * @param type $email email in social service |
||
75 | * @param type $address address in social service |
||
76 | * @param type $key |
||
77 | * @param type $phone phone in social service |
||
78 | */ |
||
79 | private function socAuth($social, $id, $username, $email, $address, $key, $phone, $redirect = true) { |
||
80 | |||
81 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
||
82 | redirect('/socauth/error'); |
||
83 | } |
||
84 | |||
85 | $user = $this->socauth_model->getUserSocInfoBySocId($id); |
||
86 | |||
87 | if (count($user) == 0) { |
||
88 | |||
89 | $emailCheck = $this->socauth_model->getUserByEmail($email); |
||
90 | |||
91 | if (count($emailCheck) > 0 ) { |
||
92 | |||
93 | redirect('/socauth/error'); |
||
94 | } |
||
95 | |||
96 | $pass = random_string('alnum', 20); |
||
97 | |||
98 | $register = $this->dx_auth->register($username, $pass, $email, $address, $key, $phone, TRUE); |
||
99 | if (!$register) { |
||
100 | redirect('/socauth/error'); |
||
101 | } |
||
102 | |||
103 | $userId = $this->socauth_model->getUserByEmail($email); |
||
104 | |||
105 | $this->socauth_model->setUserSoc($id, $social, $userId['id']); |
||
106 | } else { |
||
107 | $data = new stdClass; |
||
108 | $userData = $this->db |
||
109 | ->join('mod_social', 'users.id=mod_social.userId') |
||
110 | ->where('socialId', $id) |
||
111 | ->get('users', 1) |
||
112 | ->row(); |
||
113 | |||
114 | if (count($userData) == 0) { |
||
115 | redirect('/socauth/error'); |
||
116 | } |
||
117 | |||
118 | $data->role_id = $userData->role_id; |
||
119 | $data->id = $userData->userId; |
||
120 | $data->username = $userData->username; |
||
121 | |||
122 | $this->dx_auth->_set_session($data); |
||
123 | $this->dx_auth->_set_last_ip_and_last_login($userData->id); |
||
124 | $this->dx_auth->_clear_login_attempts(); |
||
125 | $this->dx_auth_event->user_logged_in($userData->id); |
||
126 | } |
||
127 | if ($redirect) { |
||
128 | |||
129 | $url = $this->input->cookie('url'); |
||
130 | $this->jsOpenPopap($url); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function index() { |
||
135 | |||
136 | if (!$this->dx_auth->is_logged_in()) { |
||
137 | redirect('/auth/login'); |
||
138 | } else { |
||
139 | redirect($this->input->cookie('url')); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * rendering login buttons |
||
145 | */ |
||
146 | public function renderLogin() { |
||
147 | |||
148 | if (!$this->dx_auth->is_logged_in()) { |
||
149 | $this->writeCookies(); |
||
150 | \CMSFactory\assetManager::create() |
||
151 | ->setData($this->settings) |
||
152 | ->render('loginButtons', TRUE); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Write cookies for auth |
||
158 | */ |
||
159 | View Code Duplication | private function writeCookies() { |
|
160 | |||
161 | $this->load->helper('cookie'); |
||
162 | if (!strstr($this->uri->uri_string(), 'socauth/vk')) { |
||
163 | $cookie = [ |
||
164 | 'name' => 'url', |
||
165 | 'value' => $this->input->server('HTTP_REFERER'), |
||
166 | 'expire' => '15000000', |
||
167 | 'prefix' => '', |
||
168 | ]; |
||
169 | $this->input->set_cookie($cookie); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * rendering link buttons |
||
175 | */ |
||
176 | public function renderLink() { |
||
177 | |||
178 | if ($this->dx_auth->is_logged_in()) { |
||
179 | $this->writeCookies(); |
||
180 | |||
181 | $socials = $this->db |
||
182 | ->where('userId', $this->dx_auth->get_user_id()) |
||
183 | ->get('mod_social'); |
||
184 | |||
185 | if (!$socials) { |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | $socials = $socials->result_array(); |
||
190 | |||
191 | foreach ($socials as $soc) { |
||
192 | if (!$soc['isMain']) { |
||
193 | $social[$soc['social']] = 'linked'; |
||
194 | } else { |
||
195 | $social[$soc['social']] = 'main'; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | \CMSFactory\assetManager::create() |
||
200 | ->setData($this->settings) |
||
201 | ->setData($social) |
||
202 | ->registerScript('socauth') |
||
203 | ->render('linkButtons', TRUE); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * get data from yandex |
||
209 | */ |
||
210 | public function ya() { |
||
211 | |||
212 | if ($this->input->get()) { |
||
213 | |||
214 | $params = [ |
||
215 | 'grant_type' => 'authorization_code', |
||
216 | 'code' => $this->input->get('code'), |
||
217 | 'client_id' => $this->settings['yandexClientID'], |
||
218 | 'client_secret' => $this->settings['yandexClientSecret'], |
||
219 | ]; |
||
220 | $url = 'https://oauth.yandex.ru/token'; |
||
221 | $curl = curl_init(); |
||
222 | curl_setopt($curl, CURLOPT_URL, $url); |
||
223 | curl_setopt($curl, CURLOPT_POST, 1); |
||
224 | curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params))); |
||
225 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
226 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||
227 | $result = curl_exec($curl); |
||
228 | curl_close($curl); |
||
229 | $tokenInfo = json_decode($result, true); |
||
230 | |||
231 | if (isset($tokenInfo['access_token'])) { |
||
232 | $params = [ |
||
233 | 'format' => 'json', |
||
234 | 'oauth_token' => $tokenInfo['access_token'], |
||
235 | ]; |
||
236 | |||
237 | $userInfo = json_decode(file_get_contents('https://login.yandex.ru/info?' . urldecode(http_build_query($params))), true); |
||
238 | |||
239 | } |
||
240 | if (!$this->dx_auth->is_logged_in()) { |
||
241 | $this->socAuth('ya', $userInfo['id'], $userInfo['real_name'], $userInfo['default_email'], '', '', ''); |
||
242 | } else { |
||
243 | $this->link('ya', $userInfo['id']); |
||
244 | } |
||
245 | } else { |
||
246 | $this->core->error_404(); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * |
||
252 | * @param string $soc type of social service |
||
253 | * @param string $socId social service ID |
||
254 | * @param bool $redirect |
||
255 | */ |
||
256 | public function link($soc, $socId, $redirect = true) { |
||
257 | |||
258 | $this->socauth_model->setLink($soc, $socId); |
||
259 | |||
260 | if ($this->settings['URLredirect']) { |
||
261 | redirect(site_url() . $this->settings['URLredirect']); |
||
262 | } |
||
263 | |||
264 | if ($redirect) { |
||
265 | |||
266 | $url = $this->input->cookie('url'); |
||
267 | $this->jsOpenPopap($url); |
||
268 | |||
269 | } else { |
||
270 | |||
271 | redirect($this->input->cookie('url')); |
||
272 | } |
||
273 | |||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param string $url |
||
278 | */ |
||
279 | private function jsOpenPopap($url) { |
||
280 | |||
281 | echo "<script type='text/javascript'>"; |
||
282 | echo "(function(){ |
||
283 | if(window.opener !== null){ |
||
284 | window.opener.location.assign(\" $url \"); |
||
285 | window.close(); |
||
286 | }else{ |
||
287 | window.location.assign(\" $url \"); |
||
288 | } |
||
289 | })()"; |
||
290 | echo '</script>'; |
||
291 | |||
292 | } |
||
293 | |||
294 | /** |
||
295 | * get data from facebook |
||
296 | */ |
||
297 | public function facebook() { |
||
298 | |||
299 | if ($this->input->get()) { |
||
300 | $storage = new Session(); |
||
301 | |||
302 | $credentials = new Credentials( |
||
303 | $this->settings['facebookClientID'], |
||
304 | $this->settings['facebookClientSecret'], |
||
305 | $this->currentUri->getAbsoluteUri() |
||
306 | ); |
||
307 | |||
308 | /** @var $facebookService Facebook */ |
||
309 | $facebookService = $this->serviceFactory->createService('facebook', $credentials, $storage); |
||
310 | |||
311 | View Code Duplication | if (!empty($this->input->get('code'))) { |
|
312 | |||
313 | $state = $this->input->get('state') ?: null; |
||
314 | |||
315 | $token = $facebookService->requestAccessToken($this->input->get('code'), $state); |
||
0 ignored issues
–
show
|
|||
316 | |||
317 | $result = json_decode($facebookService->request('/me?fields=email,name,location'), true); |
||
318 | |||
319 | } |
||
320 | |||
321 | View Code Duplication | if (!$this->dx_auth->is_logged_in()) { |
|
322 | $this->socAuth('fb', $result['id'], $result['name'], $result['email'], $result['location']['name'], '', ''); |
||
323 | } else { |
||
324 | $this->link('fb', $result['id']); |
||
325 | } |
||
326 | } else { |
||
327 | $this->core->error_404(); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * get data from Vkontakte |
||
333 | */ |
||
334 | public function vk() { |
||
335 | |||
336 | $this->core->set_meta_tags('SocAuts'); |
||
337 | if ($this->input->get()) { |
||
338 | |||
339 | $storage = new Session(); |
||
340 | |||
341 | $credentials = new Credentials( |
||
342 | $this->settings['vkClientID'], |
||
343 | $this->settings['vkClientSecret'], |
||
344 | $this->currentUri->getAbsoluteUri() |
||
345 | ); |
||
346 | |||
347 | /** @var $vkService Vkontakte */ |
||
348 | $vkService = $this->serviceFactory->createService('vkontakte', $credentials, $storage); |
||
349 | |||
350 | if (!empty($this->input->get('code'))) { |
||
351 | $token = $vkService->requestAccessToken($this->input->get('code')); |
||
352 | $result = json_decode($vkService->request('/users.get?v=5.80&fields=city,country'), true); |
||
353 | } |
||
354 | |||
355 | $address = $result['response'][0]['city']['title'] . ' ' . $result['response'][0]['country']['title']; |
||
356 | |||
357 | View Code Duplication | if (!$this->dx_auth->is_logged_in()) { |
|
358 | $this->socAuth('vk', $token->getExtraParams()['user_id'], $result['response'][0]['first_name'], $token->getExtraParams()['email'], $address, '', ''); |
||
359 | } else { |
||
360 | $this->link('vk', $token->getExtraParams()['user_id']); |
||
361 | } |
||
362 | } else { |
||
363 | $this->core->error_404(); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | public function error() { |
||
368 | |||
369 | $this->core->set_meta_tags('SocAuts'); |
||
370 | |||
371 | if (!$this->dx_auth->is_logged_in()) { |
||
372 | redirect('/auth/login'); |
||
373 | } else { |
||
374 | redirect($this->input->cookie('url')); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * get data from google |
||
380 | */ |
||
381 | public function google() { |
||
382 | |||
383 | if ($this->input->get()) { |
||
384 | $storage = new Session(); |
||
385 | $credentials = new Credentials( |
||
386 | $this->settings['googleClientID'], |
||
387 | $this->settings['googleClientSecret'], |
||
388 | $this->currentUri->getAbsoluteUri() |
||
389 | ); |
||
390 | /** @var $googleService Google */ |
||
391 | $googleService = $this->serviceFactory->createService('google', $credentials, $storage, ['userinfo_email', 'userinfo_profile']); |
||
392 | View Code Duplication | if (!empty($this->input->get('code'))) { |
|
393 | $state = $this->input->get('state') ?: null; |
||
394 | $googleService->requestAccessToken($this->input->get('code'), $state); |
||
395 | |||
396 | $result = json_decode($googleService->request('userinfo'), true); |
||
397 | |||
398 | } |
||
399 | |||
400 | if (!$this->dx_auth->is_logged_in()) { |
||
401 | $this->socAuth('google', $result['id'], $result['name'], $result['email'], '', '', ''); |
||
402 | } else { |
||
403 | $this->link('google', $result['id']); |
||
404 | } |
||
405 | } else { |
||
406 | $this->core->error_404(); |
||
407 | } |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * install method |
||
412 | */ |
||
413 | public function _install() { |
||
414 | |||
415 | $this->load->dbforge(); |
||
416 | ($this->dx_auth->is_admin()) OR exit; |
||
417 | $fields = [ |
||
418 | 'id' => [ |
||
419 | 'type' => 'INT', |
||
420 | 'auto_increment' => TRUE, |
||
421 | ], |
||
422 | 'socialId' => [ |
||
423 | 'type' => 'VARCHAR', |
||
424 | 'constraint' => '30', |
||
425 | 'null' => TRUE, |
||
426 | ], |
||
427 | 'userId' => [ |
||
428 | 'type' => 'VARCHAR', |
||
429 | 'constraint' => '25', |
||
430 | 'null' => TRUE, |
||
431 | ], |
||
432 | 'social' => [ |
||
433 | 'type' => 'VARCHAR', |
||
434 | 'constraint' => '20', |
||
435 | 'null' => TRUE, |
||
436 | ], |
||
437 | 'isMain' => [ |
||
438 | 'type' => 'INT', |
||
439 | 'constraint' => '1', |
||
440 | 'null' => TRUE, |
||
441 | ], |
||
442 | ]; |
||
443 | |||
444 | $this->dbforge->add_field($fields); |
||
445 | $this->dbforge->add_key('id', TRUE); |
||
446 | $this->dbforge->create_table('mod_social'); |
||
447 | |||
448 | $this->db->where('name', 'socauth'); |
||
449 | $this->db->update( |
||
450 | 'components', |
||
451 | [ |
||
452 | 'enabled' => 1, |
||
453 | 'autoload' => 0, |
||
454 | ] |
||
455 | ); |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * deinstall method |
||
460 | */ |
||
461 | public function _deinstall() { |
||
462 | |||
463 | $this->load->dbforge(); |
||
464 | ($this->dx_auth->is_admin()) OR exit; |
||
465 | $this->dbforge->drop_table('mod_social'); |
||
466 | } |
||
467 | |||
468 | } |
||
469 | |||
470 | /* End of file socauth.php */ |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.