/** * 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'); } } }