Commit 37ae28f6 by Scott

Merge branch 'pr/628' into dev

parents 5843b2db 28bfdd98
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
Description: Wrapper class for sending email notifications to users
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
use Q2A\Exceptions\FatalErrorException;
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
}
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer/class.phpmailer.php';
require_once QA_INCLUDE_DIR . 'db/selects.php'; //required for qa_db_select_with_pending()
require_once QA_INCLUDE_DIR . 'app/options.php'; //required for qa_opt()
class Q2A_Notifications_Email
{
private $email;
private $handle;
private function __construct($email, $handle)
{
$this->email = $email;
$this->handle = $handle;
}
/**
* Factory method.
* @param type $userid
* @param type $email
* @param type $handle
* @return \self
* @throws FatalErrorException
*/
public static function CreateByUserID($userid, $email = '', $handle = '')
{
if (!(int)$userid) {
throw new FatalErrorException('User ID not specified in Notifications/Email/CreateByUserID.');
}
$needemail = !PHPMailer::validateAddress($email); // take from user if invalid
$needhandle = empty($handle);
if (QA_FINAL_EXTERNAL_USERS) {
if ($needhandle) {
$handles = qa_get_public_from_userids(array($userid));
$handle = isset($handles[$userid]) ? $handles[$userid] : '';
}
if ($needemail) {
$email = qa_get_user_email($userid);
}
} elseif ($needemail || $needhandle) {
$useraccount = qa_db_select_with_pending(
array(
'columns' => array('email', 'handle'),
'source' => '^users WHERE userid = #',
'arguments' => array($userid),
'single' => true,
)
);
if ($needhandle) {
$handle = $useraccount['handle'];
}
if ($needemail) {
$email = $useraccount['email'];
}
}
return new self($email, $handle);
}
/**
* Factory method.
* @param type $email
* @param type $handle
* @return \self
*/
public static function CreateByEmailAddress($email, $handle = '')
{
return new self($email, $handle);
}
public function sendMessage($subject, $body, $subs, $html = false)
{
$fullsubs = $this->buildSubs($subs);
$bodyPrefix = (empty($this->handle) ? '' : qa_lang_sub('emails/to_handle_prefix', $this->handle));
if (PHPMailer::validateAddress($this->email)) {
$mailer = new Q2A_Notifications_Mailer(array(
'fromemail' => qa_opt('from_email'),
'fromname' => qa_opt('site_title'),
'toemail' => $this->email,
'toname' => $this->handle,
'subject' => strtr($subject, $fullsubs),
'body' => $bodyPrefix . strtr($body, $fullsubs),
'html' => $html,
));
$send_status = $mailer->send();
if (!$send_status) {
error_log('PHP Question2Answer email send error: ' . $mailer->ErrorInfo);
}
return $send_status;
}
return false;
}
protected function buildSubs($subs)
{
$addsubs = array(
'^site_title' => qa_opt('site_title'),
'^site_url' => qa_opt('site_url'),
'^handle' => $this->handle,
'^email' => $this->email,
'^open' => "\n",
'^close' => "\n"
);
return array_merge($subs, $addsubs);
}
}
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
Description: Wrapper class for sending email notifications to users
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
}
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer/PHPMailerAutoload.php';
class Q2A_Notifications_Mailer extends PHPMailer
{
public function __construct($params = array())
{
parent::__construct();
$this->CharSet = 'utf-8';
$this->From = $params['fromemail'];
$this->Sender = $params['fromemail'];
$this->FromName = $params['fromname'];
$this->addAddress($params['toemail'], $params['toname']);
if (!empty($params['replytoemail'])) {
$this->addReplyTo($params['replytoemail'], $params['replytoname']);
}
$this->Subject = $params['subject'];
$this->Body = $params['body'];
if ($params['html']) {
$this->isHTML(true);
}
if (qa_opt('smtp_active')) {
$this->isSMTP();
$this->Host = qa_opt('smtp_address');
$this->Port = qa_opt('smtp_port');
if (qa_opt('smtp_secure')) {
$this->SMTPSecure = qa_opt('smtp_secure');
} else {
$this->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
),
);
}
if (qa_opt('smtp_authenticate')) {
$this->SMTPAuth = true;
$this->Username = qa_opt('smtp_username');
$this->Password = qa_opt('smtp_password');
}
}
}
}
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
Description: Email notifications
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
}
class Q2A_Notifications_Status
{
protected static $notifications_suspended = 0;
/**
* Suspend the sending of all notifications if $suspend is true, otherwise
* reinstate it. A counter is kept to allow multiple calls.
* @param bool $suspend
*/
public static function suspend($suspend = true)
{
self::$notifications_suspended += ($suspend ? 1 : -1);
}
public static function IsSuspended()
{
return self::$notifications_suspended > 0;
}
}
......@@ -24,19 +24,15 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
exit;
}
require_once QA_INCLUDE_DIR . 'app/options.php';
/**
* @deprecated: Use Q2A_Notifications_Status::suspend() instead.
* Suspend the sending of all email notifications via qa_send_notification(...) if $suspend is true, otherwise
* reinstate it. A counter is kept to allow multiple calls.
* @param bool $suspend
*/
function qa_suspend_notifications($suspend = true)
{
global $qa_notifications_suspended;
$qa_notifications_suspended += ($suspend ? 1 : -1);
Q2A_Notifications_Status::suspend($suspend);
}
......@@ -57,70 +53,19 @@ function qa_send_notification($userid, $email, $handle, $subject, $body, $subs,
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
global $qa_notifications_suspended;
if ($qa_notifications_suspended > 0)
return false;
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'util/string.php';
if (isset($userid)) {
$needemail = !qa_email_validate(@$email); // take from user if invalid, e.g. @ used in practice
$needhandle = empty($handle);
if ($needemail || $needhandle) {
if (QA_FINAL_EXTERNAL_USERS) {
if ($needhandle) {
$handles = qa_get_public_from_userids(array($userid));
$handle = @$handles[$userid];
}
if ($needemail)
$email = qa_get_user_email($userid);
} else {
$useraccount = qa_db_select_with_pending(
array(
'columns' => array('email', 'handle'),
'source' => '^users WHERE userid = #',
'arguments' => array($userid),
'single' => true,
)
);
if ($needhandle)
$handle = @$useraccount['handle'];
if ($needemail)
$email = @$useraccount['email'];
}
}
if (Q2A_Notifications_Status::isSuspended()) {
return;
}
if (isset($email) && qa_email_validate($email)) {
$subs['^site_title'] = qa_opt('site_title');
$subs['^site_url'] = qa_opt('site_url');
$subs['^handle'] = $handle;
$subs['^email'] = $email;
$subs['^open'] = "\n";
$subs['^close'] = "\n";
return qa_send_email(array(
'fromemail' => qa_opt('from_email'),
'fromname' => qa_opt('site_title'),
'toemail' => $email,
'toname' => $handle,
'subject' => strtr($subject, $subs),
'body' => (empty($handle) ? '' : qa_lang_sub('emails/to_handle_prefix', $handle)) . strtr($body, $subs),
'html' => $html,
));
if ($userid) {
$sender = Q2A_Notifications_Email::CreateByUserID($userid, $email, $handle);
} else {
$sender = Q2A_Notifications_Email::CreateByEmailAddress($email, $handle);
}
return false;
return $sender->sendMessage($subject, $body, $subs, $html);
}
/**
* Send the email based on the $params array - the following keys are required (some can be empty): fromemail,
* fromname, toemail, toname, subject, body, html
......@@ -131,49 +76,7 @@ function qa_send_email($params)
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
// @error_log(print_r($params, true));
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer/PHPMailerAutoload.php';
$mailer = new PHPMailer();
$mailer->CharSet = 'utf-8';
$mailer->From = $params['fromemail'];
$mailer->Sender = $params['fromemail'];
$mailer->FromName = $params['fromname'];
$mailer->addAddress($params['toemail'], $params['toname']);
if (!empty($params['replytoemail'])) {
$mailer->addReplyTo($params['replytoemail'], $params['replytoname']);
}
$mailer->Subject = $params['subject'];
$mailer->Body = $params['body'];
if ($params['html'])
$mailer->isHTML(true);
if (qa_opt('smtp_active')) {
$mailer->isSMTP();
$mailer->Host = qa_opt('smtp_address');
$mailer->Port = qa_opt('smtp_port');
if (qa_opt('smtp_secure')) {
$mailer->SMTPSecure = qa_opt('smtp_secure');
} else {
$mailer->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
),
);
}
if (qa_opt('smtp_authenticate')) {
$mailer->SMTPAuth = true;
$mailer->Username = qa_opt('smtp_username');
$mailer->Password = qa_opt('smtp_password');
}
}
$mailer = new Q2A_Notifications_Mailer($params);
$send_status = $mailer->send();
if (!$send_status) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment