Example #1
0
 /**
  * Validate data provided during user update
  * @param $data
  * @return array
  */
 private function _validateUserUpdate($data)
 {
     $id = $data['fieldId'];
     $user = $data['userData'];
     $errors = array();
     $validator = new ASValidator();
     $userInfo = $this->getInfo();
     if ($userInfo == null) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('user_dont_exist'));
         return $errors;
     }
     //check if email is not empty
     if ($validator->isEmpty($user['email'])) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('email_required'));
     }
     //check if username is not empty
     if ($validator->isEmpty($user['username'])) {
         $errors[] = array("id" => $id['username'], "msg" => ASLang::get('username_required'));
     }
     //check if password and confirm password are the same
     if (!$user['password'] == hash('sha512', '') && $user['password'] != $user['confirm_password']) {
         $errors[] = array("id" => $id['confirm_password'], "msg" => ASLang::get('passwords_dont_match'));
     }
     //check if email format is correct
     if (!$validator->emailValid($user['email'])) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('email_wrong_format'));
     }
     //check if email is available
     if ($user['email'] != $userInfo['email'] && $validator->emailExist($user['email'])) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('email_taken'));
     }
     //check if username is available
     if ($user['username'] != $userInfo['username'] && $validator->usernameExist($user['username'])) {
         $errors[] = array("id" => $id['username'], "msg" => ASLang::get('username_taken'));
     }
     return $errors;
 }
     // hey, this user is registered here, just associate social account with his email
     $user = $register->getByEmail($userProfile->email);
     $register->addSocialAccount($user['user_id'], $provider, $userProfile->identifier);
     $login->byId($user['user_id']);
     redirect(get_redirect_page());
 } else {
     // this is first time that user is registring on this webiste, create his account
     $user = new ASUser(null);
     // generate unique username
     // for example, if two users with same display name (that is usually first and last name)
     // are registred, they will have the same username, so we have to add some random number here
     $username = str_replace(' ', '', $userProfile->displayName);
     $tmpUsername = $username;
     $i = 0;
     $max = 50;
     while ($validator->usernameExist($tmpUsername)) {
         //try maximum 50 times
         // Note: Chances for going over 2-3 times are really really low but just in case,
         // if somehow it always generate username that is already in use, prevent database from crashing
         // and generate some random unique username (it can be changed by administrator later)
         if ($i > $max) {
             break;
         }
         $tmpUsername = $username . rand(1, 10000);
         $i++;
     }
     // there are more than 50 trials, generate random username
     if ($i > $max) {
         $tmpUsername = uniqid('user', true);
     }
     $username = $tmpUsername;
 /**
  * Validate user provided fields.
  * @param $data User provided fieds and id's of those fields that will be used for displaying error messages on client side.
  * @param bool $botProtection Should bot protection be validated or not
  * @return array Array with errors if there are some, empty array otherwise.
  */
 public function validateUser($data, $botProtection = true)
 {
     $id = $data['fieldId'];
     $user = $data['userData'];
     $errors = array();
     $validator = new ASValidator();
     //check if email is not empty
     if ($validator->isEmpty($user['email'])) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('email_required'));
     }
     //check if username is not empty
     if ($validator->isEmpty($user['username'])) {
         $errors[] = array("id" => $id['username'], "msg" => ASLang::get('username_required'));
     }
     //check if password is not empty
     if ($validator->isEmpty($user['password'])) {
         $errors[] = array("id" => $id['password'], "msg" => ASLang::get('password_required'));
     }
     //check if password and confirm password are the same
     if ($user['password'] != $user['confirm_password']) {
         $errors[] = array("id" => $id['confirm_password'], "msg" => ASLang::get('passwords_dont_match'));
     }
     //check if email format is correct
     if (!$validator->emailValid($user['email'])) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('email_wrong_format'));
     }
     //check if email is available
     if ($validator->emailExist($user['email'])) {
         $errors[] = array("id" => $id['email'], "msg" => ASLang::get('email_taken'));
     }
     //check if username is available
     if ($validator->usernameExist($user['username'])) {
         $errors[] = array("id" => $id['username'], "msg" => ASLang::get('username_taken'));
     }
     if ($botProtection) {
         //bot protection
         $sum = ASSession::get("bot_first_number") + ASSession::get("bot_second_number");
         if ($sum != intval($user['bot_sum'])) {
             $errors[] = array("id" => $id['bot_sum'], "msg" => ASLang::get('wrong_sum'));
         }
     }
     return $errors;
 }