From 8843e0bb3cb62df8fff8d800940ecdd25be80801 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Tue, 3 Nov 2009 21:02:36 +0000 Subject: [PATCH] lib/phpmailer MDL-20701 - Convert to moodle_phpmailer class Stage 2 - We now extend phpmailer and do our customisations in our subclass rather than directly in the original file. Previously we modified SetLanguage(), but I have not carried that change over because I could not see code paths where this gets executed.(See bug) I tested the header/content of the old class vs this one and think the changes are all carried over. (If anyone has clever thoughts for how to unit test this mailing out it would be useful, especially for upgrading phpmailer). --- lib/moodlelib.php | 4 +- lib/phpmailer/README_MOODLE.txt | 19 +++-- lib/phpmailer/moodle_phpmailer.php | 107 +++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 lib/phpmailer/moodle_phpmailer.php diff --git a/lib/moodlelib.php b/lib/moodlelib.php index ebd0b34922..ae2c584155 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4416,8 +4416,8 @@ function &get_mailer($action='get') { get_mailer('flush'); } - include_once($CFG->libdir.'/phpmailer/class.phpmailer.php'); - $mailer = new phpmailer(); + include_once($CFG->libdir.'/phpmailer/moodle_phpmailer.php'); + $mailer = new moodle_phpmailer(); $counter = 1; diff --git a/lib/phpmailer/README_MOODLE.txt b/lib/phpmailer/README_MOODLE.txt index 590e73906a..57d158c95d 100644 --- a/lib/phpmailer/README_MOODLE.txt +++ b/lib/phpmailer/README_MOODLE.txt @@ -1,12 +1,11 @@ -Description of PHPMailer 1.73 library import into Moodle +Description of PHPMailer 5.1 library import into Moodle -Changes: +We now use a vanilla version of phpmailer and do our customisations in a +subclass. -class.phpmailer.php - * Duplicate Message-IDs in Forum mail (MDL-3681) - * Support for gb18030 (MDL-5229) - * Correct timezone in date (MDL-12596) - * Backported fixes for CVE-2007-3215 (MDL-18348) - * Custom EncodeHeader() to allow multibyte subjects (textlib). Seems that current phpmailer version (2.3) has it properly implemented (though dependent of mbstring). - * Custom constructor PHPMailer() - * Custom SetLanguage() function +When doing the import we remove directories/files: +aboutus.html +class.pop3.php +docs/ +examples/ +test/ diff --git a/lib/phpmailer/moodle_phpmailer.php b/lib/phpmailer/moodle_phpmailer.php new file mode 100644 index 0000000000..fca7fe612e --- /dev/null +++ b/lib/phpmailer/moodle_phpmailer.php @@ -0,0 +1,107 @@ +. + * + * @package moodle + * @subpackage lib + * @author Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * Customised version of phpmailer for Moodle + */ + +// PLEASE NOTE: we use the phpmailer class _unmodified_ +// through the joys of OO. Distros are free to use their stock +// version of this file. +require_once($CFG->libdir.'/phpmailer/class.phpmailer.php'); + +/** + * Moodle Customised version of the PHPMailer class + * + * This class extends the stock PHPMailer class + * in order to make sensible configuration choices, + * and behave in a way which is friendly to moodle. + * + * @copyright 2009 Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class moodle_phpmailer extends PHPMailer { + + /** + * Constructor - creates an instance of the PHPMailer class + * with Moodle defaults. + */ + public function __construct(){ + global $CFG; + $this->Version = 'Moodle '.$CFG->version; // mailer version + $this->PluginDir = $CFG->libdir.'/phpmailer/'; // plugin directory (eg smtp plugin) + $this->CharSet = 'UTF-8'; + } + + /** + * Extended AddCustomHeader function in order to stop duplicate + * message-ids + * http://tracker.moodle.org/browse/MDL-3681 + */ + public function AddCustomHeader($custom_header) { + if(preg_match('/message-id:(.*)/i', $custom_header, $matches)){ + $this->MessageID = $matches[1]; + return true; + }else{ + return parent::AddCustomHeader($custom_header); + } + } + + /** + * Use internal moodles own textlib to encode mimeheaders. + * Fall back to phpmailers inbuilt functions if not + */ + public function EncodeHeader ($str, $position = 'text') { + $textlib = textlib_get_instance(); + $encoded = $textlib->encode_mimeheader($str, $this->CharSet); + if ($encoded !== false) { + $encoded = str_replace("\n", $this->LE, $encoded); + if ($position == 'phrase') { + return ("\"$encoded\""); + } + return $encoded; + } + + return parent::EncodeHeader($str, $position); + } + + /** + * Replaced function to fix tz bug: + * http://tracker.moodle.org/browse/MDL-12596 + * + * PLEASE NOTE: intentionally not declared this function public in + * order that we keep compatibiltiy with previous versions of phpmailer + * where it was declared private. + */ + static function RFCDate() { + $tz = date('Z'); + $tzs = ($tz < 0) ? '-' : '+'; + $tz = abs($tz); + $tz = (($tz - ($tz%3600) )/3600)*100 + ($tz%3600)/60; // fixed tz bug + $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz); + + return $result; + } +} -- 2.39.5