Ejemplo n.º 1
0
 public function resend_confirmation_email($email)
 {
     $sql = 'SELECT user_id, status
         FROM `user`
         WHERE email = :email;';
     $s = $this->conn->prepare($sql);
     if (!$s) {
         throw new DatabaseException($this->conn->errorInfo()[2]);
     }
     $s->bindValue(':email', $email);
     if (!$s->execute()) {
         throw new DatabaseException($s->errorInfo()[2]);
     }
     $user_data = $s->fetch(PDO::FETCH_ASSOC);
     if (empty($user_data)) {
         $key = '/signup/confirmation-ui/new-code/unregistered';
         throw new Exception($this->msg->_($key));
     }
     if ($user_data['status'] !== 'pending-activation') {
         $key = '/signup/confirmation-ui/new-code/already-active';
         throw new Exception($this->msg->_($key));
     }
     // delete old confirmation codes from this user
     $sql = 'DELETE
         FROM `confirmation_code`
         WHERE user_id = :user_id';
     $s = $this->conn->prepare($sql);
     if (!$s) {
         throw new DatabaseException($this->conn->errorInfo()[2]);
     }
     $s->bindValue(':user_id', $user_data['user_id']);
     if (!$s->execute()) {
         throw new DatabaseException($s->errorInfo()[2]);
     }
     // generate new code and send it
     $confirmation_code = random_hex_string(32);
     $sql = 'INSERT INTO `confirmation_code` (code, user_id, expires_at)
         VALUES (:code, :user_id, :expires_at)';
     $s = $this->conn->prepare($sql);
     if (!$s) {
         throw new DatabaseException($this->conn->errorInfo()[2]);
     }
     $s->bindValue(':user_id', $user_data['user_id']);
     $s->bindValue(':code', $confirmation_code);
     $s->bindValue(':expires_at', date('Y-m-d H:i:s', strtotime('+24 hours')));
     if (!$s->execute()) {
         throw new DatabaseException($s->errorInfo()[2]);
     }
     MailSender::send_confirmation_mail($this->msg, $email, $confirmation_code);
 }