/** *Processes request after request string is parsed and controller name and action is determined */ public function processRequest() { if (!Helpers::is_controller_exists($this->getControllerName())) { $this->go404(); return; } $class = new \ReflectionClass(Helpers::get_controller_class_name($this->getControllerName())); $methods = $class->getMethods(); $actionMethod = null; foreach ($methods as $method) { if (strtolower($method->getName()) === strtolower('action' . $this->getActionName())) { $actionMethod = $method; break; } } if ($actionMethod == null) { $this->go404(); return; } $controller_class_name = Helpers::get_controller_class_name($this->getControllerName()); $params = $actionMethod->getParameters(); $form_param_found = false; foreach ($params as $param) { if ($param->isOptional()) { $defVal = $param->getDefaultValue(); if (preg_match('/^%form:(\\w+)$/i', $defVal, $matches)) { if (array_key_exists($matches[1], $_POST)) { $form_param_found = true; break; } else { $this->go404(); return; } } } } if (!$form_param_found) { $method_parameters = array_slice($this->url_array, 2); if (count($method_parameters) != count($params)) { $this->go404(); return; } else { $matched_params = array(); for ($i = 0; $i < count($params); $i++) { if ($params[$i]->isOptional()) { $defVal = $param->getDefaultValue(); if (preg_match('/^%d$/i', $defVal)) { if (!preg_match('/^\\d+$/', $method_parameters[$i])) { $this->go404(); return; } else { $matched_params[$i] = $method_parameters[$i]; } } } else { $matched_params[$i] = $method_parameters[$i]; } } $actionMethod->invokeArgs(new $controller_class_name(), $method_parameters); return; } } else { if (isset($_FILES)) { $form = new Form($_POST, new Files($_FILES)); } else { $form = new Form($_POST); } $actionMethod->invoke(new $controller_class_name(), $form); return; } }
/** * Implicitly renders view if it appear as variable on the template, applying language settings to it * @return string - Result rendered view * @throws PropertyAlreadyExistsException - Throws if 'lang' property as already set */ public final function __toString() { $this->add('lang', Helpers::get_current_lang_template_translation($this->templateName)); return $this->viewRederer->renderTemplate($this->templateName, $this->getProperties(), true); }
/** * Responsible for user input validation and puts user data to DB if all is OK * @param string $form Registration form that user sends by pressing submit button * @throws \userregister\app\exceptions\GenericException * @throws \userregister\app\exceptions\PropertyAlreadyExistsException */ public function actionConfirm($form = '%form:doRegister') { $has_errors = false; $validation_summary = new ValidationSummary('validation_summary'); try { if ($form->accountName == "") { $validation_summary->add('account_name_required', true); $has_errors = true; } else { if (mb_strlen($form->accountName) > Constants::SIMPLE_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if (DAL::checkAccountExists($form->accountName)) { $validation_summary->add('account_name_exists', true); $has_errors = true; } if (!preg_match(Constants::ACCOUNT_NAME_REGEX, $form->accountName)) { $validation_summary->add('account_name_invalid', true); $has_errors = true; } } if ($form->accountPass == "") { $validation_summary->add('account_pass_cannot_empty', true); $has_errors = true; } else { if (mb_strlen($form->accountPass) > Constants::PASS_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if ($form->accountPass != $form->accountPassRepeat) { $validation_summary->add('account_pass_not_match', true); $has_errors = true; } } if (!preg_match(Constants::SIMPLE_STRING_REGEX, $form->userName)) { $validation_summary->add('user_name', true); $has_errors = true; } if (mb_strlen($form->userName) > Constants::SIMPLE_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if (!preg_match(Constants::SIMPLE_STRING_REGEX, $form->userSurname)) { $validation_summary->add('user_surname', true); $has_errors = true; } if (mb_strlen($form->userSurname) > Constants::SIMPLE_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if (!preg_match(Constants::SIMPLE_STRING_REGEX, $form->userPatronymic)) { $validation_summary->add('user_patronymic', true); $has_errors = true; } if (mb_strlen($form->userPatronymic) > Constants::SIMPLE_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if (!isset($form->userGender)) { throw new FormMalformedException('form_malformed'); } if ($form->userEmail == '') { $validation_summary->add('user_email_required', true); $has_errors = true; } else { if (mb_strlen($form->userEmail) > Constants::EMAIL_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if (!preg_match(Constants::EMAIL_REGEX, $form->userEmail)) { $validation_summary->add('user_email', true); $has_errors = true; } } if (count($form->files) > 0) { if ($form->MAX_FILE_SIZE != Constants::MAX_FILE_SIZE) { throw new FormMalformedException('Form is malformed'); } $image_errors = false; switch ($form->files->userPhoto['error']) { case UPLOAD_ERR_FORM_SIZE: $validation_summary->add('user_file_size_not_allowed', true); $has_errors = true; $image_errors = true; break; case UPLOAD_ERR_INI_SIZE: $validation_summary->add('user_file_size_not_allowed', true); $has_errors = true; $image_errors = true; break; case UPLOAD_ERR_OK: if ($form->files->userPhoto['size'] > Constants::MAX_FILE_SIZE) { $validation_summary->add('user_file_size_not_allowed', true); $has_errors = true; $image_errors = true; } if (!preg_match(Constants::FILE_REGEX, $form->files->userPhoto['name'])) { $validation_summary->add('user_file_ext', true); $has_errors = true; $image_errors = true; } break; } if ($form->files->userPhoto['error'] == UPLOAD_ERR_OK && !$image_errors) { $fileName = Helpers::generateGUID(); $ext = pathinfo($form->files->userPhoto['name'], PATHINFO_EXTENSION); $newFileName = $fileName . '.' . $ext; move_uploaded_file($form->files->userPhoto['tmp_name'], USER_FILES_DIR . DIR_SEP . $newFileName); $form->uploadedFile = $newFileName; } } if (!preg_match(Constants::PHONE_REGEX, $form->userPhone)) { $validation_summary->add('user_phone', true); $has_errors = true; } if (mb_strlen($form->userPhone) > Constants::SIMPLE_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } if (!preg_match(Constants::SIMPLE_STRING_REGEX, $form->userCity)) { $validation_summary->add('user_city', true); $has_errors = true; } if (mb_strlen($form->userPhone) > Constants::SIMPLE_FIELD_MAX_LENGTH) { throw new FormMalformedException('form_malformed'); } } catch (\Exception $ex) { $validation_summary->add('form_is_malformed', true); $has_errors = true; } if ($has_errors) { $_SESSION['validation_errors'] = serialize($validation_summary); $_SESSION['prev_form'] = serialize($form); $this->addStyle('validation_summary'); header('Location: /register'); } else { try { $user_id = DAL::addUser($form); header("Location: /register/RegisterSuccess/{$user_id}"); $_SESSION[Constants::AUTH_USER_ID] = $user_id; } catch (\Exception $ex) { header('Location: /home/404'); } } }
<?php use userregister\app\core\Router; use userregister\app\core\helpers\Helpers; error_reporting(E_ALL); header('Content-Type: text/html; charset=utf-8'); session_start(); if (!isset($_COOKIE['lang']) && isset($_SESSION['lang'])) { setcookie('lang', 'ru', strtotime("+1 week")); $_SESSION['lang'] = 'ru'; } require_once dirname(__FILE__) . '/app/core/helpers/definitions.php'; require_once SITE_PHYSICAL_ROOT_PATH . '/app/core/helpers/Helpers_class.php'; Helpers::init(); $router = new Router(); $router->processRequest();