[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/phpmailer/ -> moodle_phpmailer.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Customised version of phpmailer for Moodle
  19   *
  20   * @package    core
  21   * @author     Dan Poltawski <[email protected]>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  // PLEASE NOTE: we use the phpmailer class _unmodified_
  28  // through the joys of OO. Distros are free to use their stock
  29  // version of this file.
  30  // NOTE: do not rely on phpmailer autoloader for performance reasons.
  31  require_once($CFG->libdir.'/phpmailer/class.phpmailer.php');
  32  require_once($CFG->libdir.'/phpmailer/class.smtp.php');
  33  
  34  /**
  35   * Moodle Customised version of the PHPMailer class
  36   *
  37   * This class extends the stock PHPMailer class
  38   * in order to make sensible configuration choices,
  39   * and behave in a way which is friendly to moodle.
  40   *
  41   * @copyright 2009 Dan Poltawski <[email protected]>
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   * @since     Moodle 2.0
  44   */
  45  class moodle_phpmailer extends PHPMailer {
  46  
  47      /**
  48       * Constructor - creates an instance of the PHPMailer class
  49       * with Moodle defaults.
  50       */
  51      public function __construct(){
  52          global $CFG;
  53          $this->Version   = 'Moodle '.$CFG->version;         // mailer version
  54          $this->CharSet   = 'UTF-8';
  55  
  56          // Some MTAs may do double conversion of LF if CRLF used, CRLF is required line ending in RFC 822bis.
  57          if (isset($CFG->mailnewline) and $CFG->mailnewline == 'CRLF') {
  58              $this->LE = "\r\n";
  59          } else {
  60              $this->LE = "\n";
  61          }
  62      }
  63  
  64      /**
  65       * Extended AddCustomHeader function in order to stop duplicate 
  66       * message-ids
  67       * http://tracker.moodle.org/browse/MDL-3681
  68       */
  69      public function addCustomHeader($custom_header, $value = null) {
  70          if ($value === null and preg_match('/message-id:(.*)/i', $custom_header, $matches)) {
  71              $this->MessageID = $matches[1];
  72              return true;
  73          } else if ($value !== null and strcasecmp($custom_header, 'message-id') === 0) {
  74              $this->MessageID = $value;
  75              return true;
  76          } else {
  77              return parent::addCustomHeader($custom_header, $value);
  78          }
  79      }
  80  
  81      /**
  82       * Use internal moodles own core_text to encode mimeheaders.
  83       * Fall back to phpmailers inbuilt functions if not 
  84       */
  85      public function encodeHeader($str, $position = 'text') {
  86          $encoded = core_text::encode_mimeheader($str, $this->CharSet);
  87          if ($encoded !== false) {
  88              $encoded = str_replace("\n", $this->LE, $encoded);
  89              if ($position === 'phrase') {
  90                  return ("\"$encoded\"");
  91              }
  92              return $encoded;
  93          }
  94  
  95          return parent::encodeHeader($str, $position);
  96      }
  97  
  98      /**
  99       * Replaced function to fix tz bug:
 100       * http://tracker.moodle.org/browse/MDL-12596
 101       */
 102      public static function rfcDate() {
 103          $tz = date('Z');
 104          $tzs = ($tz < 0) ? '-' : '+';
 105          $tz = abs($tz);
 106          $tz = (($tz - ($tz%3600) )/3600)*100 + ($tz%3600)/60; // fixed tz bug
 107          $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
 108  
 109          return $result;
 110      }
 111  
 112      /**
 113       * This is a temporary replacement of the parent::EncodeQP() that does not
 114       * call quoted_printable_encode() even if it is available. See MDL-23240 for details
 115       *
 116       * @see parent::EncodeQP() for full documentation
 117       */
 118      public function encodeQP($string, $line_max = 76) {
 119          //if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
 120          //    return quoted_printable_encode($string);
 121          //}
 122          $filters = stream_get_filters();
 123          if (!in_array('convert.*', $filters)) { //Got convert stream filter?
 124              return parent::encodeQP($string, $line_max); //Fall back to old implementation
 125          }
 126          $fp = fopen('php://temp/', 'r+');
 127          $string = preg_replace('/\r\n?/', $this->LE, $string); //Normalise line breaks
 128          $params = array('line-length' => $line_max, 'line-break-chars' => $this->LE);
 129          $s = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $params);
 130          fputs($fp, $string);
 131          rewind($fp);
 132          $out = stream_get_contents($fp);
 133          stream_filter_remove($s);
 134          $out = preg_replace('/^\./m', '=2E', $out); //Encode . if it is first char on a line, workaround for bug in Exchange
 135          fclose($fp);
 136          return $this->fixEOL($out);
 137      }
 138  
 139      /**
 140       * Sends this mail.
 141       *
 142       * This function has been overridden to facilitate unit testing.
 143       *
 144       * @return bool
 145       */
 146      public function postSend() {
 147          // Now ask phpunit if it wants to catch this message.
 148          if (PHPUNIT_TEST) {
 149              if (!phpunit_util::is_redirecting_phpmailer()) {
 150                  debugging('Unit tests must not send real emails! Use $this->redirectEmails()');
 151                  return true;
 152              }
 153              $mail = new stdClass();
 154              $mail->header = $this->MIMEHeader;
 155              $mail->body = $this->MIMEBody;
 156              $mail->subject = $this->Subject;
 157              $mail->from = $this->From;
 158              $mail->to = $this->to[0][0];
 159              phpunit_util::phpmailer_sent($mail);
 160              return true;
 161          } else {
 162              return parent::postSend();
 163          }
 164      }
 165  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1