Total Complexity | 67 |
Total Lines | 524 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Controller { |
||
48 | /** |
||
49 | * @param Request $Request |
||
50 | * @param Response $Response |
||
51 | * |
||
52 | * @throws ExitException |
||
53 | */ |
||
54 | public static function index ($Request, $Response) { |
||
55 | $route = $Request->route; |
||
56 | /** |
||
57 | * This should be present in any case, if not - exit from here |
||
58 | */ |
||
59 | if (!isset($route[0])) { |
||
60 | self::redirect(); |
||
61 | return; |
||
62 | } |
||
63 | $Config = Config::instance(); |
||
64 | $Page = Page::instance(); |
||
65 | $User = User::instance(); |
||
66 | $Social_integration = Social_integration::instance(); |
||
67 | $L = new Prefix('hybridauth_'); |
||
68 | /** |
||
69 | * Confirmation of accounts merging |
||
70 | */ |
||
71 | if ($route[0] == 'merge_confirmation') { |
||
72 | self::merge_confirmation($route, $Page, $L); |
||
73 | return; |
||
74 | } |
||
75 | $provider = $route[0]; |
||
76 | /** |
||
77 | * Authenticated users are not allowed to sign in, also provider should exist and be enabled |
||
78 | */ |
||
79 | if ( |
||
80 | $User->user() || |
||
81 | !@$Config->module('HybridAuth')->providers[$provider]['enabled'] |
||
|
|||
82 | ) { |
||
83 | self::redirect(); |
||
84 | return; |
||
85 | } |
||
86 | $referer = $Request->header('referer'); |
||
87 | /** |
||
88 | * If referer is internal website address, but not HybridAuth module - save referer to cookie |
||
89 | */ |
||
90 | if ( |
||
91 | strpos($referer, $Config->base_url()) === 0 && |
||
92 | strpos($referer, $Config->base_url().'/HybridAuth') === false |
||
93 | ) { |
||
94 | $Response->cookie('HybridAuth_referer', $referer, 0, true); |
||
95 | } |
||
96 | require_once __DIR__.'/Hybrid/Auth.php'; |
||
97 | require_once __DIR__.'/Hybrid/Endpoint.php'; |
||
98 | /** |
||
99 | * Handle authentication endpoint |
||
100 | */ |
||
101 | if (isset($route[1]) && $route[1] == 'endpoint') { |
||
102 | /** |
||
103 | * `$rc[2]` should be present and contain special hash for security reasons (this is called as callback from provider) |
||
104 | */ |
||
105 | if ( |
||
106 | !isset($route[2]) || |
||
107 | strpos($route[2], md5($provider.Session::instance()->get_id())) !== 0 |
||
108 | ) { |
||
109 | self::redirect(); |
||
110 | return; |
||
111 | } |
||
112 | Hybrid_Endpoint::process($_REQUEST); |
||
113 | return; |
||
114 | } |
||
115 | /** |
||
116 | * If user did not specified email |
||
117 | */ |
||
118 | if (!isset($_POST['email'])) { |
||
119 | self::email_not_specified($Config, $provider, $Social_integration, $User, $Page, $L); |
||
120 | return; |
||
121 | } |
||
122 | /** |
||
123 | * If user specified email |
||
124 | */ |
||
125 | self::email_was_specified($provider, $Social_integration, $User, $Page, $L, $Config, $Response); |
||
126 | } |
||
127 | /** |
||
128 | * Redirect to referer or home page |
||
129 | * |
||
130 | * @param bool $with_delay If `true` - redirect will be made in 5 seconds after page load |
||
131 | * |
||
132 | * @throws ExitException |
||
133 | */ |
||
134 | protected static function redirect ($with_delay = false) { |
||
135 | $Response = Response::instance(); |
||
136 | $redirect_to = Request::instance()->cookie('HybridAuth_referer') ?: Config::instance()->base_url(); |
||
137 | $Response->cookie('HybridAuth_referer', ''); |
||
138 | if ($with_delay) { |
||
139 | $Response->header('refresh', "5; url=$redirect_to"); |
||
140 | } else { |
||
141 | $Response->redirect($redirect_to, 301); |
||
142 | Page::instance()->interface = false; |
||
143 | throw new ExitException; |
||
144 | } |
||
145 | } |
||
146 | /** |
||
147 | * @param string[] $route |
||
148 | * @param Page $Page |
||
149 | * @param Prefix $L |
||
150 | * |
||
151 | * @throws ExitException |
||
152 | */ |
||
153 | protected static function merge_confirmation ($route, $Page, $L) { |
||
154 | if (!isset($route[1])) { |
||
155 | self::redirect(); |
||
156 | } |
||
157 | /** |
||
158 | * Check confirmation code |
||
159 | */ |
||
160 | $data = self::get_data_by_confirmation_code($route[1]); |
||
161 | if (!$data) { |
||
162 | $Page->warning($L->merge_confirm_code_invalid); |
||
163 | return; |
||
164 | } |
||
165 | /** |
||
166 | * If confirmation key is valid - merge social profile with main account |
||
167 | */ |
||
168 | self::add_integration_create_session( |
||
169 | $data['id'], |
||
170 | $data['provider'], |
||
171 | $data['identifier'], |
||
172 | $data['profile'] |
||
173 | ); |
||
174 | self::save_hybridauth_session(); |
||
175 | $Page->success( |
||
176 | $L->merging_confirmed_successfully($L->{$data['provider']}) |
||
177 | ); |
||
178 | } |
||
179 | /** |
||
180 | * @param int $id |
||
181 | * @param string $provider |
||
182 | * @param string $identifier |
||
183 | * @param string $profile |
||
184 | */ |
||
185 | protected static function add_integration_create_session ($id, $provider, $identifier, $profile) { |
||
186 | Social_integration::instance()->add($id, $provider, $identifier, $profile); |
||
187 | $User = User::instance(); |
||
188 | /** |
||
189 | * If user was not activated before - activate him |
||
190 | */ |
||
191 | if ($User->get('status', $id) == User::STATUS_NOT_ACTIVATED) { |
||
192 | $User->set('status', User::STATUS_ACTIVE, $id); |
||
193 | } |
||
194 | self::add_session_and_update_data($id, $provider, true); |
||
195 | } |
||
196 | /** |
||
197 | * Save HybridAuth session in user's data in order to restore it next time when calling `get_hybridauth_instance()` |
||
198 | */ |
||
199 | protected static function save_hybridauth_session () { |
||
200 | $User = User::instance(); |
||
201 | $User->set_data( |
||
202 | 'HybridAuth_session', |
||
203 | array_merge( |
||
204 | $User->get_data('HybridAuth_session') ?: [], |
||
205 | unserialize(get_hybridauth_instance()->getSessionData()) |
||
206 | ) |
||
207 | ); |
||
208 | } |
||
209 | /** |
||
210 | * @param string $code |
||
211 | * |
||
212 | * @return false|array |
||
213 | */ |
||
214 | protected static function get_data_by_confirmation_code ($code) { |
||
215 | return Key::instance()->get( |
||
216 | Config::instance()->module('HybridAuth')->db('integration'), |
||
217 | $code, |
||
218 | true |
||
219 | ); |
||
220 | } |
||
221 | /** |
||
222 | * @param Config $Config |
||
223 | * @param string $provider |
||
224 | * @param Social_integration $Social_integration |
||
225 | * @param User $User |
||
226 | * @param Page $Page |
||
227 | * @param Prefix $L |
||
228 | * |
||
229 | * @throws ExitException |
||
230 | */ |
||
231 | protected static function email_not_specified ($Config, $provider, $Social_integration, $User, $Page, $L) { |
||
232 | $profile = self::authenticate_hybridauth($provider); |
||
233 | /** |
||
234 | * Check whether this account was already registered in system. If registered - make login |
||
235 | */ |
||
236 | $user = $Social_integration->find_integration($provider, $profile->identifier); |
||
237 | if ( |
||
238 | $user && |
||
239 | $User->get('status', $user) == User::STATUS_ACTIVE |
||
240 | ) { |
||
241 | self::add_session_and_update_data($user, $provider); |
||
242 | return; |
||
243 | } |
||
244 | if (!Config::instance()->core['allow_user_registration']) { |
||
245 | Page::instance() |
||
246 | ->title($L->registration_prohibited) |
||
247 | ->warning($L->registration_prohibited); |
||
248 | return; |
||
249 | } |
||
250 | $email = strtolower($profile->emailVerified ?: $profile->email); |
||
251 | /** |
||
252 | * If integrated service does not returns email - ask user for email |
||
253 | */ |
||
254 | if (!$email) { |
||
255 | self::email_form($Page, $L); |
||
256 | return; |
||
257 | } |
||
258 | /** |
||
259 | * Search for user with such email |
||
260 | */ |
||
261 | $user = $User->get_id(hash('sha224', $email)); |
||
262 | /** |
||
263 | * If email is already registered - merge social profile with main account |
||
264 | */ |
||
265 | if ($user) { |
||
266 | self::add_integration_create_session($user, $provider, $profile->identifier, $profile->profileURL); |
||
267 | return; |
||
268 | } |
||
269 | /** |
||
270 | * If user doesn't exists - try to register user |
||
271 | */ |
||
272 | $result = self::try_to_register($provider, $email, false); |
||
273 | if (!$result) { |
||
274 | return; |
||
275 | } |
||
276 | $Social_integration->add($result['id'], $provider, $profile->identifier, $profile->profileURL); |
||
277 | /** |
||
278 | * Registration is successful, confirmation is not needed |
||
279 | */ |
||
280 | self::finish_registration_send_email($Config, $result['id'], $result['password'], $provider); |
||
281 | } |
||
282 | /** |
||
283 | * Returns profile |
||
284 | * |
||
285 | * @param string $provider |
||
286 | * |
||
287 | * @return \Hybrid_User_Profile |
||
288 | * |
||
289 | * @throws ExitException |
||
290 | */ |
||
291 | protected static function authenticate_hybridauth ($provider) { |
||
292 | try { |
||
293 | return get_hybridauth_instance($provider)::authenticate($provider)->getUserProfile(); |
||
294 | } catch (ExitException $e) { |
||
295 | throw $e; |
||
296 | } catch (Exception $e) { |
||
297 | trigger_error($e->getMessage()); |
||
298 | self::redirect(true); |
||
299 | throw new ExitException; |
||
300 | } |
||
301 | } |
||
302 | /** |
||
303 | * @throws ExitException |
||
304 | * |
||
305 | * @param string $provider |
||
306 | * @param string $email |
||
307 | * @param bool $email_from_user |
||
308 | * |
||
309 | * @return array|false|string |
||
310 | */ |
||
311 | protected static function try_to_register ($provider, $email, $email_from_user) { |
||
312 | $profile = self::authenticate_hybridauth($provider); |
||
313 | if (!Event::instance()->fire( |
||
314 | 'HybridAuth/registration/before', |
||
315 | [ |
||
316 | 'provider' => $provider, |
||
317 | 'email' => $email, |
||
318 | 'identifier' => $profile->identifier, |
||
319 | 'profile' => $profile->profileURL |
||
320 | ] |
||
321 | ) |
||
322 | ) { |
||
323 | return false; |
||
324 | } |
||
325 | $L = new Prefix('hybridauth_'); |
||
326 | $Page = Page::instance(); |
||
327 | $User = User::instance(); |
||
328 | $result = $email_from_user ? $User->registration($email) : $User->registration($email, false, false); |
||
329 | if (!$result && $email_from_user) { |
||
330 | $Page |
||
331 | ->title($L->please_type_correct_email) |
||
332 | ->warning($L->please_type_correct_email); |
||
333 | self::email_form($Page, $L); |
||
334 | return false; |
||
335 | } |
||
336 | if (!$result || $result == 'error') { |
||
337 | $Page |
||
338 | ->title($L->registration_server_error) |
||
339 | ->warning($L->registration_server_error); |
||
340 | self::redirect(true); |
||
341 | return false; |
||
342 | } |
||
343 | return $result; |
||
344 | } |
||
345 | /** |
||
346 | * @param Page $Page |
||
347 | * @param Prefix $L |
||
348 | */ |
||
349 | protected static function email_form ($Page, $L) { |
||
350 | $Page->content( |
||
351 | h::{'cs-form form'}( |
||
352 | h::{'p.cs-text-center'}( |
||
353 | $L->please_type_your_email.':'. |
||
354 | h::{'input[name=email]'}( |
||
355 | isset($_POST['email']) ? $_POST['email'] : '' |
||
356 | ) |
||
357 | ). |
||
358 | h::{'cs-button button[type=submit]'}( |
||
359 | $L->submit |
||
360 | ) |
||
361 | ) |
||
362 | ); |
||
363 | } |
||
364 | /** |
||
365 | * @param string $provider |
||
366 | * @param Social_integration $Social_integration |
||
367 | * @param User $User |
||
368 | * @param Page $Page |
||
369 | * @param Prefix $L |
||
370 | * @param Config $Config |
||
371 | * @param Response $Response |
||
372 | * |
||
373 | * @throws ExitException |
||
374 | */ |
||
375 | protected static function email_was_specified ($provider, $Social_integration, $User, $Page, $L, $Config, $Response) { |
||
376 | $profile = self::authenticate_hybridauth($provider); |
||
377 | /** |
||
378 | * Try to register user |
||
379 | */ |
||
380 | $result = self::try_to_register($provider, $_POST['email'], true); |
||
381 | if (!$result) { |
||
382 | return; |
||
383 | } |
||
384 | $core_url = $Config->core_url(); |
||
385 | /** |
||
386 | * If email is already registered |
||
387 | */ |
||
388 | if ($result == 'exists') { |
||
389 | /** |
||
390 | * Send merging confirmation email |
||
391 | */ |
||
392 | $id = $User->get_id(hash('sha224', strtolower($_POST['email']))); |
||
393 | $HybridAuth_data['id'] = $id; |
||
394 | $confirmation_code = self::set_data_generate_confirmation_code($HybridAuth_data); |
||
395 | $title = $L->merge_confirmation_mail_title($Config->core['site_name']); |
||
396 | $body = $L->merge_confirmation_mail_body( |
||
397 | $User->username($id) ?: strstr($_POST['email'], '@', true), |
||
398 | $Config->core['site_name'], |
||
399 | $L->$provider, |
||
400 | "$core_url/HybridAuth/merge_confirmation/$confirmation_code", |
||
401 | $L->time($Config->core['registration_confirmation_time'], 'd') |
||
402 | ); |
||
403 | if (self::send_registration_mail($_POST['email'], $title, $body)) { |
||
404 | $Response->cookie('HybridAuth_referer', ''); |
||
405 | $Page->content( |
||
406 | h::p( |
||
407 | $L->merge_confirmation($L->$provider) |
||
408 | ) |
||
409 | ); |
||
410 | } |
||
411 | return; |
||
412 | } |
||
413 | /** |
||
414 | * Registration is successful and confirmation is not required |
||
415 | */ |
||
416 | if ($result['reg_key'] === true) { |
||
417 | $Social_integration->add($result['id'], $provider, $profile->identifier, $profile->profileURL); |
||
418 | self::finish_registration_send_email($Config, $result['id'], $result['password'], $provider); |
||
419 | return; |
||
420 | } |
||
421 | /** |
||
422 | * Registration is successful, but confirmation is needed |
||
423 | */ |
||
424 | $title = $L->registration_need_confirmation_mail($Config->core['site_name']); |
||
425 | $body = $L->registration_need_confirmation_mail_body( |
||
426 | self::get_adapter($provider)->getUserProfile()->displayName ?: strstr($result['email'], '@', true), |
||
427 | $Config->core['site_name'], |
||
428 | "$core_url/profile/registration_confirmation/$result[reg_key]", |
||
429 | $L->time($Config->core['registration_confirmation_time'], 'd') |
||
430 | ); |
||
431 | if (self::send_registration_mail($_POST['email'], $title, $body)) { |
||
432 | self::update_data($provider); |
||
433 | $Response->cookie('HybridAuth_referer', ''); |
||
434 | $Page->content($L->registration_confirmation); |
||
435 | } |
||
436 | } |
||
437 | /** |
||
438 | * @param string $email |
||
439 | * @param string $title |
||
440 | * @param string $body |
||
441 | * |
||
442 | * @return bool |
||
443 | * |
||
444 | * @throws ExitException |
||
445 | */ |
||
446 | protected static function send_registration_mail ($email, $title, $body) { |
||
447 | $result = Mail::instance()->send_to($email, $title, $body); |
||
448 | /** |
||
449 | * If mail sending failed - cancel registration, show error message and redirect to referrer page |
||
450 | */ |
||
451 | if (!$result) { |
||
452 | User::instance()->registration_cancel(); |
||
453 | $L = new Prefix('hybridauth_'); |
||
454 | Page::instance() |
||
455 | ->title($L->registration_mail_sending_error_title) |
||
456 | ->warning($L->registration_mail_sending_error); |
||
457 | self::redirect(true); |
||
458 | } |
||
459 | return $result; |
||
460 | } |
||
461 | /** |
||
462 | * @param array $data |
||
463 | * |
||
464 | * @return false|string |
||
465 | * |
||
466 | * @throws ExitException |
||
467 | */ |
||
468 | protected static function set_data_generate_confirmation_code ($data) { |
||
469 | $code = Key::instance()->add( |
||
470 | Config::instance()->module('HybridAuth')->db('integration'), |
||
471 | false, |
||
472 | $data, |
||
473 | time() + Config::instance()->core['registration_confirmation_time'] * 86400 |
||
474 | ); |
||
475 | if (!$code) { |
||
476 | throw new ExitException(500); |
||
477 | } |
||
478 | return $code; |
||
479 | } |
||
480 | /** |
||
481 | * @param int $user_id |
||
482 | * @param string $provider |
||
483 | * @param bool $redirect_with_delay |
||
484 | * |
||
485 | * @throws ExitException |
||
486 | */ |
||
487 | protected static function add_session_and_update_data ($user_id, $provider, $redirect_with_delay = false) { |
||
488 | $adapter = self::get_adapter($provider); |
||
489 | Event::instance()->fire( |
||
490 | 'HybridAuth/add_session/before', |
||
491 | [ |
||
492 | 'adapter' => $adapter, |
||
493 | 'provider' => $provider |
||
494 | ] |
||
495 | ); |
||
496 | Session::instance()->add($user_id); |
||
497 | self::save_hybridauth_session(); |
||
498 | Event::instance()->fire( |
||
499 | 'HybridAuth/add_session/after', |
||
500 | [ |
||
501 | 'adapter' => $adapter, |
||
502 | 'provider' => $provider |
||
503 | ] |
||
504 | ); |
||
505 | self::update_data($provider); |
||
506 | self::redirect($redirect_with_delay); |
||
507 | } |
||
508 | /** |
||
509 | * @param string $provider |
||
510 | */ |
||
511 | protected static function update_data ($provider) { |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | } |
||
530 | /** |
||
531 | * @param Config $Config |
||
532 | * @param int $user_id |
||
533 | * @param string $password |
||
534 | * @param string $provider |
||
535 | */ |
||
536 | protected static function finish_registration_send_email ($Config, $user_id, $password, $provider) { |
||
554 | } |
||
555 | } |
||
556 | /** |
||
557 | * @param string $provider |
||
558 | * |
||
559 | * @return \Hybrid_Provider_Adapter |
||
560 | * |
||
561 | * @throws ExitException |
||
562 | */ |
||
563 | protected static function get_adapter ($provider) { |
||
571 | } |
||
572 | } |
||
573 | } |
||
574 |