| Conditions | 97 |
| Paths | 0 |
| Total Lines | 325 |
| Code Lines | 175 |
| Lines | 46 |
| Ratio | 14.15 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 210 | function Register2() |
||
| 211 | { |
||
| 212 | global $txt, $modSettings, $context, $sourcedir; |
||
| 213 | global $smcFunc, $maintenance; |
||
| 214 | |||
| 215 | checkSession(); |
||
| 216 | validateToken('register'); |
||
| 217 | |||
| 218 | // Check to ensure we're forcing SSL for authentication |
||
| 219 | View Code Duplication | if (!empty($modSettings['force_ssl']) && empty($maintenance) && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) |
|
| 220 | fatal_lang_error('register_ssl_required'); |
||
| 221 | |||
| 222 | // Start collecting together any errors. |
||
| 223 | $reg_errors = array(); |
||
| 224 | |||
| 225 | // You can't register if it's disabled. |
||
| 226 | View Code Duplication | if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 3) |
|
| 227 | fatal_lang_error('registration_disabled', false); |
||
| 228 | |||
| 229 | // Well, if you don't agree, you can't register. |
||
| 230 | if (!empty($modSettings['requireAgreement']) && empty($_SESSION['registration_agreed'])) |
||
| 231 | redirectexit(); |
||
| 232 | |||
| 233 | // Make sure they came from *somewhere*, have a session. |
||
| 234 | if (!isset($_SESSION['old_url'])) |
||
| 235 | redirectexit('action=signup'); |
||
| 236 | |||
| 237 | // If we don't require an agreement, we need a extra check for coppa. |
||
| 238 | if (empty($modSettings['requireAgreement']) && !empty($modSettings['coppaAge'])) |
||
| 239 | $_SESSION['skip_coppa'] = !empty($_POST['accept_agreement']); |
||
| 240 | // Are they under age, and under age users are banned? |
||
| 241 | View Code Duplication | if (!empty($modSettings['coppaAge']) && empty($modSettings['coppaType']) && empty($_SESSION['skip_coppa'])) |
|
| 242 | { |
||
| 243 | loadLanguage('Errors'); |
||
| 244 | fatal_lang_error('under_age_registration_prohibited', false, array($modSettings['coppaAge'])); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Check the time gate for miscreants. First make sure they came from somewhere that actually set it up. |
||
| 248 | if (empty($_SESSION['register']['timenow']) || empty($_SESSION['register']['limit'])) |
||
| 249 | redirectexit('action=signup'); |
||
| 250 | // Failing that, check the time on it. |
||
| 251 | if (time() - $_SESSION['register']['timenow'] < $_SESSION['register']['limit']) |
||
| 252 | { |
||
| 253 | loadLanguage('Errors'); |
||
| 254 | $reg_errors[] = $txt['error_too_quickly']; |
||
| 255 | } |
||
| 256 | |||
| 257 | // Check whether the visual verification code was entered correctly. |
||
| 258 | if (!empty($modSettings['reg_verification'])) |
||
| 259 | { |
||
| 260 | require_once($sourcedir . '/Subs-Editor.php'); |
||
| 261 | $verificationOptions = array( |
||
| 262 | 'id' => 'register', |
||
| 263 | ); |
||
| 264 | $context['visual_verification'] = create_control_verification($verificationOptions, true); |
||
| 265 | |||
| 266 | if (is_array($context['visual_verification'])) |
||
| 267 | { |
||
| 268 | loadLanguage('Errors'); |
||
| 269 | foreach ($context['visual_verification'] as $error) |
||
| 270 | $reg_errors[] = $txt['error_' . $error]; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | View Code Duplication | foreach ($_POST as $key => $value) |
|
| 275 | { |
||
| 276 | if (!is_array($_POST[$key])) |
||
| 277 | $_POST[$key] = htmltrim__recursive(str_replace(array("\n", "\r"), '', $_POST[$key])); |
||
| 278 | } |
||
| 279 | |||
| 280 | // Collect all extra registration fields someone might have filled in. |
||
| 281 | $possible_strings = array( |
||
| 282 | 'birthdate', |
||
| 283 | 'time_format', |
||
| 284 | 'buddy_list', |
||
| 285 | 'pm_ignore_list', |
||
| 286 | 'smiley_set', |
||
| 287 | 'personal_text', 'avatar', |
||
| 288 | 'lngfile', |
||
| 289 | 'secret_question', 'secret_answer', |
||
| 290 | ); |
||
| 291 | $possible_ints = array( |
||
| 292 | 'id_theme', |
||
| 293 | ); |
||
| 294 | $possible_floats = array( |
||
| 295 | 'time_offset', |
||
| 296 | ); |
||
| 297 | $possible_bools = array( |
||
| 298 | 'show_online', |
||
| 299 | ); |
||
| 300 | |||
| 301 | // We may want to add certain things to these if selected in the admin panel. |
||
| 302 | if (!empty($modSettings['registration_fields'])) |
||
| 303 | { |
||
| 304 | $reg_fields = explode(',', $modSettings['registration_fields']); |
||
| 305 | |||
| 306 | // Website is a little different |
||
| 307 | if (in_array('website', $reg_fields)) |
||
| 308 | $possible_strings += array('website_url', 'website_title'); |
||
| 309 | } |
||
| 310 | |||
| 311 | if (isset($_POST['secret_answer']) && $_POST['secret_answer'] != '') |
||
| 312 | $_POST['secret_answer'] = md5($_POST['secret_answer']); |
||
| 313 | |||
| 314 | // Needed for isReservedName() and registerMember(). |
||
| 315 | require_once($sourcedir . '/Subs-Members.php'); |
||
| 316 | |||
| 317 | // Maybe you want set the displayed name during registration |
||
| 318 | if (isset($_POST['real_name'])) |
||
| 319 | { |
||
| 320 | // Are you already allowed to edit the displayed name? |
||
| 321 | View Code Duplication | if (allowedTo('profile_displayed_name') || allowedTo('moderate_forum')) |
|
| 322 | $canEditDisplayName = true; |
||
| 323 | |||
| 324 | // If you are a guest, will you be allowed to once you register? |
||
| 325 | else |
||
| 326 | { |
||
| 327 | $request = $smcFunc['db_query']('', ' |
||
| 328 | SELECT add_deny |
||
| 329 | FROM {db_prefix}permissions |
||
| 330 | WHERE id_group = {int:id_group} AND permission = {string:permission}', |
||
| 331 | array( |
||
| 332 | 'id_group' => 0, |
||
| 333 | 'permission' => 'profile_displayed_name_own', |
||
| 334 | ) |
||
| 335 | ); |
||
| 336 | list($canEditDisplayName) = $smcFunc['db_fetch_row']($request); |
||
| 337 | $smcFunc['db_free_result']($request); |
||
| 338 | } |
||
| 339 | |||
| 340 | if ($canEditDisplayName) |
||
| 341 | { |
||
| 342 | // Sanitize it |
||
| 343 | $_POST['real_name'] = trim(preg_replace('~[\t\n\r \x0B\0' . ($context['utf8'] ? '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}' : '\x00-\x08\x0B\x0C\x0E-\x19\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $_POST['real_name'])); |
||
| 344 | |||
| 345 | // Only set it if we are sure it is good |
||
| 346 | if (trim($_POST['real_name']) != '' && !isReservedName($_POST['real_name']) && $smcFunc['strlen']($_POST['real_name']) < 60) |
||
| 347 | $possible_strings[] = 'real_name'; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | // Handle a string as a birthdate... |
||
| 352 | if (isset($_POST['birthdate']) && $_POST['birthdate'] != '') |
||
| 353 | $_POST['birthdate'] = strftime('%Y-%m-%d', strtotime($_POST['birthdate'])); |
||
| 354 | // Or birthdate parts... |
||
| 355 | elseif (!empty($_POST['bday1']) && !empty($_POST['bday2'])) |
||
| 356 | $_POST['birthdate'] = sprintf('%04d-%02d-%02d', empty($_POST['bday3']) ? 0 : (int) $_POST['bday3'], (int) $_POST['bday1'], (int) $_POST['bday2']); |
||
| 357 | |||
| 358 | // Validate the passed language file. |
||
| 359 | if (isset($_POST['lngfile']) && !empty($modSettings['userLanguage'])) |
||
| 360 | { |
||
| 361 | // Do we have any languages? |
||
| 362 | if (empty($context['languages'])) |
||
| 363 | getLanguages(); |
||
| 364 | |||
| 365 | // Did we find it? |
||
| 366 | if (isset($context['languages'][$_POST['lngfile']])) |
||
| 367 | $_SESSION['language'] = $_POST['lngfile']; |
||
| 368 | else |
||
| 369 | unset($_POST['lngfile']); |
||
| 370 | } |
||
| 371 | else |
||
| 372 | unset($_POST['lngfile']); |
||
| 373 | |||
| 374 | // Set the options needed for registration. |
||
| 375 | $regOptions = array( |
||
| 376 | 'interface' => 'guest', |
||
| 377 | 'username' => !empty($_POST['user']) ? $_POST['user'] : '', |
||
| 378 | 'email' => !empty($_POST['email']) ? $_POST['email'] : '', |
||
| 379 | 'password' => !empty($_POST['passwrd1']) ? $_POST['passwrd1'] : '', |
||
| 380 | 'password_check' => !empty($_POST['passwrd2']) ? $_POST['passwrd2'] : '', |
||
| 381 | 'check_reserved_name' => true, |
||
| 382 | 'check_password_strength' => true, |
||
| 383 | 'check_email_ban' => true, |
||
| 384 | 'send_welcome_email' => !empty($modSettings['send_welcomeEmail']), |
||
| 385 | 'require' => !empty($modSettings['coppaAge']) && empty($_SESSION['skip_coppa']) ? 'coppa' : (empty($modSettings['registration_method']) ? 'nothing' : ($modSettings['registration_method'] == 1 ? 'activation' : 'approval')), |
||
| 386 | 'extra_register_vars' => array(), |
||
| 387 | 'theme_vars' => array(), |
||
| 388 | 'timezone' => !empty($modSettings['default_timezone']) ? $modSettings['default_timezone'] : '', |
||
| 389 | ); |
||
| 390 | |||
| 391 | // Include the additional options that might have been filled in. |
||
| 392 | foreach ($possible_strings as $var) |
||
| 393 | if (isset($_POST[$var])) |
||
| 394 | $regOptions['extra_register_vars'][$var] = $smcFunc['htmlspecialchars']($_POST[$var], ENT_QUOTES); |
||
| 395 | View Code Duplication | foreach ($possible_ints as $var) |
|
| 396 | if (isset($_POST[$var])) |
||
| 397 | $regOptions['extra_register_vars'][$var] = (int) $_POST[$var]; |
||
| 398 | View Code Duplication | foreach ($possible_floats as $var) |
|
| 399 | if (isset($_POST[$var])) |
||
| 400 | $regOptions['extra_register_vars'][$var] = (float) $_POST[$var]; |
||
| 401 | foreach ($possible_bools as $var) |
||
| 402 | if (isset($_POST[$var])) |
||
| 403 | $regOptions['extra_register_vars'][$var] = empty($_POST[$var]) ? 0 : 1; |
||
| 404 | |||
| 405 | // Registration options are always default options... |
||
| 406 | View Code Duplication | if (isset($_POST['default_options'])) |
|
| 407 | $_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options']; |
||
| 408 | $regOptions['theme_vars'] = isset($_POST['options']) && is_array($_POST['options']) ? $_POST['options'] : array(); |
||
| 409 | |||
| 410 | // Make sure they are clean, dammit! |
||
| 411 | $regOptions['theme_vars'] = htmlspecialchars__recursive($regOptions['theme_vars']); |
||
| 412 | |||
| 413 | // Check whether we have fields that simply MUST be displayed? |
||
| 414 | $request = $smcFunc['db_query']('', ' |
||
| 415 | SELECT col_name, field_name, field_type, field_length, mask, show_reg |
||
| 416 | FROM {db_prefix}custom_fields |
||
| 417 | WHERE active = {int:is_active} |
||
| 418 | ORDER BY field_order', |
||
| 419 | array( |
||
| 420 | 'is_active' => 1, |
||
| 421 | ) |
||
| 422 | ); |
||
| 423 | $custom_field_errors = array(); |
||
| 424 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 425 | { |
||
| 426 | // Don't allow overriding of the theme variables. |
||
| 427 | if (isset($regOptions['theme_vars'][$row['col_name']])) |
||
| 428 | unset($regOptions['theme_vars'][$row['col_name']]); |
||
| 429 | |||
| 430 | // Not actually showing it then? |
||
| 431 | if (!$row['show_reg']) |
||
| 432 | continue; |
||
| 433 | |||
| 434 | // Prepare the value! |
||
| 435 | $value = isset($_POST['customfield'][$row['col_name']]) ? trim($_POST['customfield'][$row['col_name']]) : ''; |
||
| 436 | |||
| 437 | // We only care for text fields as the others are valid to be empty. |
||
| 438 | if (!in_array($row['field_type'], array('check', 'select', 'radio'))) |
||
| 439 | { |
||
| 440 | // Is it too long? |
||
| 441 | if ($row['field_length'] && $row['field_length'] < $smcFunc['strlen']($value)) |
||
| 442 | $custom_field_errors[] = array('custom_field_too_long', array($row['field_name'], $row['field_length'])); |
||
| 443 | |||
| 444 | // Any masks to apply? |
||
| 445 | if ($row['field_type'] == 'text' && !empty($row['mask']) && $row['mask'] != 'none') |
||
| 446 | { |
||
| 447 | if ($row['mask'] == 'email' && (!filter_var($value, FILTER_VALIDATE_EMAIL) || strlen($value) > 255)) |
||
| 448 | $custom_field_errors[] = array('custom_field_invalid_email', array($row['field_name'])); |
||
| 449 | elseif ($row['mask'] == 'number' && preg_match('~[^\d]~', $value)) |
||
| 450 | $custom_field_errors[] = array('custom_field_not_number', array($row['field_name'])); |
||
| 451 | elseif (substr($row['mask'], 0, 5) == 'regex' && trim($value) != '' && preg_match(substr($row['mask'], 5), $value) === 0) |
||
| 452 | $custom_field_errors[] = array('custom_field_inproper_format', array($row['field_name'])); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | // Is this required but not there? |
||
| 457 | if (trim($value) == '' && $row['show_reg'] > 1) |
||
| 458 | $custom_field_errors[] = array('custom_field_empty', array($row['field_name'])); |
||
| 459 | } |
||
| 460 | $smcFunc['db_free_result']($request); |
||
| 461 | |||
| 462 | // Process any errors. |
||
| 463 | View Code Duplication | if (!empty($custom_field_errors)) |
|
| 464 | { |
||
| 465 | loadLanguage('Errors'); |
||
| 466 | foreach ($custom_field_errors as $error) |
||
| 467 | $reg_errors[] = vsprintf($txt['error_' . $error[0]], $error[1]); |
||
| 468 | } |
||
| 469 | |||
| 470 | // Lets check for other errors before trying to register the member. |
||
| 471 | if (!empty($reg_errors)) |
||
| 472 | { |
||
| 473 | $_REQUEST['step'] = 2; |
||
| 474 | $_SESSION['register']['limit'] = 5; // If they've filled in some details, they won't need the full 10 seconds of the limit. |
||
| 475 | return Register($reg_errors); |
||
| 476 | } |
||
| 477 | |||
| 478 | $memberID = registerMember($regOptions, true); |
||
| 479 | |||
| 480 | // What there actually an error of some kind dear boy? |
||
| 481 | if (is_array($memberID)) |
||
| 482 | { |
||
| 483 | $reg_errors = array_merge($reg_errors, $memberID); |
||
| 484 | $_REQUEST['step'] = 2; |
||
| 485 | return Register($reg_errors); |
||
| 486 | } |
||
| 487 | |||
| 488 | // Do our spam protection now. |
||
| 489 | spamProtection('register'); |
||
| 490 | |||
| 491 | // Do they want to recieve announcements? |
||
| 492 | require_once($sourcedir . '/Subs-Notify.php'); |
||
| 493 | $prefs = getNotifyPrefs($memberID, 'announcements', true); |
||
| 494 | $var = !empty($_POST['notify_announcements']); |
||
| 495 | $pref = !empty($prefs[$memberID]['announcements']); |
||
| 496 | |||
| 497 | // Don't update if the default is the same. |
||
| 498 | if ($var != $pref) |
||
| 499 | { |
||
| 500 | setNotifyPrefs($memberID, array('announcements' => (int) !empty($_POST['notify_announcements']))); |
||
| 501 | } |
||
| 502 | |||
| 503 | // We'll do custom fields after as then we get to use the helper function! |
||
| 504 | if (!empty($_POST['customfield'])) |
||
| 505 | { |
||
| 506 | require_once($sourcedir . '/Profile.php'); |
||
| 507 | require_once($sourcedir . '/Profile-Modify.php'); |
||
| 508 | makeCustomFieldChanges($memberID, 'register'); |
||
| 509 | } |
||
| 510 | |||
| 511 | // If COPPA has been selected then things get complicated, setup the template. |
||
| 512 | if (!empty($modSettings['coppaAge']) && empty($_SESSION['skip_coppa'])) |
||
| 513 | redirectexit('action=coppa;member=' . $memberID); |
||
| 514 | // Basic template variable setup. |
||
| 515 | elseif (!empty($modSettings['registration_method'])) |
||
| 516 | { |
||
| 517 | loadTemplate('Register'); |
||
| 518 | |||
| 519 | $context += array( |
||
| 520 | 'page_title' => $txt['register'], |
||
| 521 | 'title' => $txt['registration_successful'], |
||
| 522 | 'sub_template' => 'after', |
||
| 523 | 'description' => $modSettings['registration_method'] == 2 ? $txt['approval_after_registration'] : $txt['activate_after_registration'] |
||
| 524 | ); |
||
| 525 | } |
||
| 526 | else |
||
| 527 | { |
||
| 528 | call_integration_hook('integrate_activate', array($regOptions['username'])); |
||
| 529 | |||
| 530 | setLoginCookie(60 * $modSettings['cookieTime'], $memberID, hash_salt($regOptions['register_vars']['passwd'], $regOptions['register_vars']['password_salt'])); |
||
| 531 | |||
| 532 | redirectexit('action=login2;sa=check;member=' . $memberID, $context['server']['needs_login_fix']); |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 891 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.