Ejemplo n.º 1
0
 function update_user($username, $changes)
 {
     if (!is_array($changes) or empty($changes)) {
         throw new Exception('-');
     }
     $assignments = array();
     $arguments = array();
     $err = array();
     if (isset($changes['full_name'])) {
         if (!Validator::validate_full_name($changes['full_name'])) {
             $err['full_name'] = $this->msg->_('/signup/errors/full-name.two-words', [255]);
         } else {
             $assignments[] = 'full_name = ?';
             $arguments[] = $changes['full_name'];
         }
     }
     if (isset($changes['birth_date'])) {
         if (!Validator::validate_birth_date($changes['birth_date'])) {
             $err['birth_date'] = $this->msg->_('/signup/errors/b-date.invalid');
         } else {
             $assignments[] = 'birth_date = ?';
             $arguments[] = $changes['birth_date'];
         }
     }
     if (isset($changes['gender'])) {
         if (!Validator::validate_gender($changes['gender'])) {
             $err['gender'] = $this->msg->_('/signup/errors/gender.invalid');
         } else {
             $assignments[] = 'gender = ?';
             $arguments[] = $changes['gender'];
         }
     }
     if (isset($changes['status'])) {
         if (!Validator::validate_status($changes['status'])) {
             $err['status'] = $this->msg->_('/update-user/errors/status.invalid');
         } else {
             $assignments[] = 'status = ?';
             $arguments[] = $changes['status'];
             if ($changes['status'] === 'active') {
                 $user = $this->get_users(array('fields' => 'email', 'username' => $username))['items'][0];
                 MailSender::tell_approved($this->msg, $user['email']);
             }
         }
     }
     if (isset($changes['password'])) {
         if (!Validator::validate_password($changes['password'])) {
             $err['password'] = $this->msg->_('/signup/errors/password.invalid');
         } else {
             $assignments[] = 'password = ?';
             $arguments[] = password_hash($changes['password'], PASSWORD_BCRYPT);
         }
     }
     if (!empty($err)) {
         throw new Exception(my_json_encode($err));
     }
     $sql = 'UPDATE `user` SET ';
     $sql .= implode(', ', $assignments);
     $sql .= ' WHERE username = ?;';
     $arguments[] = $username;
     $s = $this->conn->prepare($sql);
     if (!$s) {
         throw new DatabaseException($this->conn->errorInfo()[2]);
     }
     if (!$s->execute($arguments)) {
         throw new DatabaseException($s->errorInfo()[2]);
     }
     return $s->rowCount();
 }