/**
  * Action method that responsible for user sign in
  * @param string $form - Form that user sends by pressing submit button
  */
 public function actionLogin($form = "%form:btnLogin")
 {
     $user = DAL::getUserByAccountData($form->userName, $form->userPass);
     if ($user === false) {
         $_SESSION[self::USER_NOT_FOUND] = true;
         header('Location: /');
     } else {
         $_SESSION[Constants::AUTH_USER_ID] = $user['Id'];
         header("Location: /profile/view/{$user['Id']}");
     }
 }
 /**
  * Method used by AJAX request from register form to check if user name is free to use
  * @param $name - String that passed from Account name field on the register form
  */
 public function actionCheckName($name)
 {
     header('Content-type: text/html');
     if (!preg_match(Constants::ACCOUNT_NAME_REGEX, $name)) {
         http_response_code(200);
         echo 'invalid';
         return;
     }
     try {
         if (DAL::checkAccountExists($name)) {
             http_response_code(200);
             echo 'true';
         } else {
             http_response_code(200);
             echo 'false';
         }
     } catch (GenericException $ex) {
         http_response_code(404);
     }
 }
 /**
  * Action method responsible for user profile display
  * @param string $profile_id - User id which profile to display
  * @throws \userregister\app\exceptions\GenericException
  * @throws \userregister\app\exceptions\PropertyAlreadyExistsException
  */
 public function actionView($profile_id = "%d")
 {
     if (!isset($_SESSION[Constants::AUTH_USER_ID])) {
         header('Location: /home/accessdenied');
     } elseif ($_SESSION[Constants::AUTH_USER_ID] != $profile_id) {
         header('Location: /home/accessdenied');
     }
     $user = DAL::getUser($profile_id);
     $profileView = new ViewProfile('view_profile');
     $profileView->add('userPhoto', $user['UserPhoto']);
     $profileView->add('userAccountName', $user['AccountName']);
     $profileView->add('userName', $user['UserName']);
     $profileView->add('userSurname', $user['Surname']);
     $profileView->add('userPatronymic', $user['Patronymic']);
     $profileView->add('userGender', $user['Gender']);
     $profileView->add('userEmail', $user['Email']);
     $profileView->add('userPhone', $user['Phone']);
     $profileView->add('userCity', $user['City']);
     $this->setTitle('userProfile');
     $this->renderMainView($profileView);
 }
 /**
  * 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');
         }
     }
 }