Phpauth\Auth::addRequest PHP Method

addRequest() protected method

Creates an activation entry and sends email to user
protected addRequest ( integer $uid, string $email, string $type, boolean &$sendmail ) : boolean
$uid integer
$email string
$type string
$sendmail boolean = NULL
return boolean
    protected function addRequest($uid, $email, $type, &$sendmail)
    {
        $return['error'] = true;
        if ($type != "activation" && $type != "reset") {
            $return['message'] = $this->lang["system_error"] . " #08";
            return $return;
        }
        // if not set manually, check config data
        if ($sendmail === NULL) {
            $sendmail = true;
            if ($type == "reset" && $this->config->emailmessage_suppress_reset === true) {
                $sendmail = false;
                $return['error'] = false;
                return $return;
            }
            if ($type == "activation" && $this->config->emailmessage_suppress_activation === true) {
                $sendmail = false;
                $return['error'] = false;
                return $return;
            }
        }
        $query = $this->dbh->prepare("SELECT id, expire FROM {$this->config->table_requests} WHERE uid = ? AND type = ?");
        $query->execute(array($uid, $type));
        if ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
            $expiredate = strtotime($row['expire']);
            $currentdate = strtotime(date("Y-m-d H:i:s"));
            if ($currentdate < $expiredate) {
                $return['message'] = $this->lang["reset_exists"];
                return $return;
            }
            $this->deleteRequest($row['id']);
        }
        if ($type == "activation" && $this->getBaseUser($uid)['isactive'] == 1) {
            $return['message'] = $this->lang["already_activated"];
            return $return;
        }
        $key = $this->getRandomKey(20);
        $expire = date("Y-m-d H:i:s", strtotime($this->config->request_key_expiration));
        $query = $this->dbh->prepare("INSERT INTO {$this->config->table_requests} (uid, rkey, expire, type) VALUES (?, ?, ?, ?)");
        if (!$query->execute(array($uid, $key, $expire, $type))) {
            $return['message'] = $this->lang["system_error"] . " #09";
            return $return;
        }
        $request_id = $this->dbh->lastInsertId();
        if ($sendmail === true) {
            // Check configuration for SMTP parameters
            $mail = new PHPMailer();
            $mail->CharSet = $this->config->mail_charset;
            if ($this->config->smtp) {
                $mail->isSMTP();
                $mail->Host = $this->config->smtp_host;
                $mail->SMTPAuth = $this->config->smtp_auth;
                if (!is_null($this->config->smtp_auth)) {
                    $mail->Username = $this->config->smtp_username;
                    $mail->Password = $this->config->smtp_password;
                }
                $mail->Port = $this->config->smtp_port;
                if (!is_null($this->config->smtp_security)) {
                    $mail->SMTPSecure = $this->config->smtp_security;
                }
            }
            $mail->From = $this->config->site_email;
            $mail->FromName = $this->config->site_name;
            $mail->addAddress($email);
            $mail->isHTML(true);
            if ($type == "activation") {
                $mail->Subject = sprintf($this->lang['email_activation_subject'], $this->config->site_name);
                $mail->Body = sprintf($this->lang['email_activation_body'], $this->config->site_url, $this->config->site_activation_page, $key);
                $mail->AltBody = sprintf($this->lang['email_activation_altbody'], $this->config->site_url, $this->config->site_activation_page, $key);
            } else {
                $mail->Subject = sprintf($this->lang['email_reset_subject'], $this->config->site_name);
                $mail->Body = sprintf($this->lang['email_reset_body'], $this->config->site_url, $this->config->site_password_reset_page, $key);
                $mail->AltBody = sprintf($this->lang['email_reset_altbody'], $this->config->site_url, $this->config->site_password_reset_page, $key);
            }
            if (!$mail->send()) {
                $this->deleteRequest($request_id);
                $return['message'] = $this->lang["system_error"] . " #10";
                return $return;
            }
        }
        $return['error'] = false;
        return $return;
    }