コード例 #1
0
 public function testFallbackMbstringFunctions()
 {
     if (!extension_loaded('mbstring')) {
         $this->markTestSkipped("The mb_string functions must be installed to test the fallback functions");
     }
     $sampleUTF = "Östergötland_coat_of_arms.png";
     //mb_substr
     $substr_params = array(array(0, 0), array(5, -4), array(33), array(100, -5), array(-8, 10), array(1, 1), array(2, -1));
     foreach ($substr_params as $param_set) {
         $old_param_set = $param_set;
         array_unshift($param_set, $sampleUTF);
         $this->assertEquals(call_user_func_array('mb_substr', $param_set), call_user_func_array('Fallback::mb_substr', $param_set), 'Fallback mb_substr with params ' . implode(', ', $old_param_set));
     }
     //mb_strlen
     $this->assertEquals(mb_strlen($sampleUTF), Fallback::mb_strlen($sampleUTF), 'Fallback mb_strlen');
     //mb_str(r?)pos
     $strpos_params = array();
     foreach ($strpos_params as $param_set) {
         $old_param_set = $param_set;
         array_unshift($param_set, $sampleUTF);
         $this->assertEquals(call_user_func_array('mb_strpos', $param_set), call_user_func_array('Fallback::mb_strpos', $param_set), 'Fallback mb_strpos with params ' . implode(', ', $old_param_set));
         $this->assertEquals(call_user_func_array('mb_strrpos', $param_set), call_user_func_array('Fallback::mb_strrpos', $param_set), 'Fallback mb_strrpos with params ' . implode(', ', $old_param_set));
     }
 }
コード例 #2
0
ファイル: GlobalFunctions.php プロジェクト: D66Ha/mediawiki
 /**
  * @codeCoverageIgnore
  * @see Fallback::mb_strrpos
  * @return int
  */
 function mb_strrpos($haystack, $needle, $offset = 0, $encoding = '')
 {
     return Fallback::mb_strrpos($haystack, $needle, $offset, $encoding);
 }
コード例 #3
0
ファイル: UserMailer.php プロジェクト: Tjorriemorrie/app
 /**
  * This function will perform a direct (authenticated) login to
  * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
  * array of parameters. It requires PEAR:Mail to do that.
  * Otherwise it just uses the standard PHP 'mail' function.
  *
  * @param MailAddress $to : recipient's email (or an array of them)
  * @param MailAddress $from : sender's email
  * @param String $subject : email's subject.
  * @param String $body : email's text.
  * $body can be array with text and html version of email message, and also can contain attachements
  * $body = array('text' => 'Email text', 'html' => '<b>Email text</b>')
  * @param MailAddress $replyTo : optional reply-to email (default: null).
  * @param String $contentType : optional custom Content-Type
  * @param String $category : optional category for statistic
  * @param int $priority : optional priority for email
  * @param Array $attachments : optional list of files to send as attachments
  *
  * @return Status object
  * @throws MWException
  */
 public static function send(MailAddress $to, MailAddress $from, $subject, $body, MailAddress $replyTo = null, $contentType = null, $category = 'UserMailer', $priority = 0, $attachments = [])
 {
     if (!is_array($to)) {
         $to = [$to];
     }
     # Make sure we have at least one address
     $has_address = false;
     foreach ($to as $u) {
         if ($u->address) {
             $has_address = true;
             break;
         }
     }
     if (!$has_address) {
         return Status::newFatal('user-mail-no-addy');
     }
     wfRunHooks('UserMailerSend', [&$to]);
     # Forge email headers
     # -------------------
     #
     # WARNING
     #
     # DO NOT add To: or Subject: headers at this step. They need to be
     # handled differently depending upon the mailer we are going to use.
     #
     # To:
     #  PHP mail() first argument is the mail receiver. The argument is
     #  used as a recipient destination and as a To header.
     #
     #  PEAR mailer has a recipient argument which is only used to
     #  send the mail. If no To header is given, PEAR will set it to
     #  to 'undisclosed-recipients:'.
     #
     #  NOTE: To: is for presentation, the actual recipient is specified
     #  by the mailer using the Rcpt-To: header.
     #
     # Subject:
     #  PHP mail() second argument to pass the subject, passing a Subject
     #  as an additional header will result in a duplicate header.
     #
     #  PEAR mailer should be passed a Subject header.
     #
     # -- hashar 20120218
     $headers['From'] = $from->toString();
     $headers['Return-Path'] = $from->address;
     if ($replyTo && $replyTo instanceof MailAddress) {
         $headers['Reply-To'] = $replyTo->toString();
     }
     $headers['Date'] = date('r');
     $headers['MIME-Version'] = '1.0';
     if (empty($attachments)) {
         $headers['Content-Type'] = is_null($contentType) ? 'text/plain; charset=UTF-8' : $contentType;
         $headers['Content-Transfer-Encoding'] = '8bit';
     }
     $headers['Message-ID'] = self::makeMsgId();
     $headers['X-Mailer'] = 'MediaWiki mailer';
     $headers['X-Msg-Category'] = $category;
     if ($priority) {
         $headers['X-Priority'] = $priority;
     }
     $ret = wfRunHooks('AlternateUserMailer', [$headers, $to, $from, $subject, $body, $priority, $attachments]);
     if ($ret === false) {
         return Status::newGood();
     } elseif ($ret !== true) {
         return Status::newFatal('php-mail-error', $ret);
     }
     # MoLi: body can be an array with text and html message
     # MW core uses only text version of email message, so $body as array should be used only with AlternateUserMailer hook
     if (is_array($body) && isset($body['text'])) {
         $body = $body['text'];
     }
     if (is_array(F::app()->wg->SMTP)) {
         #
         # PEAR MAILER
         #
         if (function_exists('stream_resolve_include_path')) {
             $found = stream_resolve_include_path('Mail.php');
         } else {
             $found = Fallback::stream_resolve_include_path('Mail.php');
         }
         if (!$found) {
             throw new MWException('PEAR mail package is not installed');
         }
         require_once 'Mail.php';
         wfSuppressWarnings();
         // Create the mail object using the Mail::factory method
         $mail_object =& Mail::factory('smtp', F::app()->wg->SMTP);
         if (PEAR::isError($mail_object)) {
             wfDebug("PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n");
             wfRestoreWarnings();
             return Status::newFatal('pear-mail-error', $mail_object->getMessage());
         }
         wfDebug("Sending mail via PEAR::Mail\n");
         $headers['Subject'] = self::quotedPrintable($subject);
         # When sending only to one recipient, shows it its email using To:
         if (count($to) == 1) {
             $headers['To'] = $to[0]->toString();
         }
         # Split jobs since SMTP servers tends to limit the maximum
         # number of possible recipients.
         $chunks = array_chunk($to, F::app()->wg->EnotifMaxRecips);
         foreach ($chunks as $chunk) {
             if (!wfRunHooks('ComposeMail', [$chunk, &$body, &$headers])) {
                 continue;
             }
             $status = self::sendWithPear($mail_object, $chunk, $headers, $body);
             # FIXME : some chunks might be sent while others are not!
             if (!$status->isOK()) {
                 wfRestoreWarnings();
                 return $status;
             }
         }
         wfRestoreWarnings();
         return Status::newGood();
     } else {
         #
         # PHP mail()
         #
         # Line endings need to be different on Unix and Windows due to
         # the bug described at http://trac.wordpress.org/ticket/2603
         if (wfIsWindows()) {
             $body = str_replace("\n", "\r\n", $body);
             $endl = "\r\n";
         } else {
             $endl = "\n";
         }
         if (count($to) > 1) {
             $headers['To'] = 'undisclosed-recipients:;';
         }
         $headers = self::arrayToHeaderString($headers, $endl);
         wfDebug("Sending mail via internal mail() function\n");
         self::$mErrorString = '';
         $html_errors = ini_get('html_errors');
         ini_set('html_errors', '0');
         $safeMode = wfIniGetBool('safe_mode');
         foreach ($to as $recip) {
             if (!wfRunHooks('ComposeMail', array($recip, &$body, &$headers))) {
                 continue;
             }
             if ($safeMode) {
                 $sent = mail($recip, self::quotedPrintable($subject), $body, $headers);
             } else {
                 $sent = mail($recip, self::quotedPrintable($subject), $body, $headers, F::app()->wg->AdditionalMailParams);
             }
         }
         ini_set('html_errors', $html_errors);
         if (self::$mErrorString) {
             wfDebug("Error sending mail: " . self::$mErrorString . "\n");
             return Status::newFatal('php-mail-error', self::$mErrorString);
         } elseif (!$sent) {
             // mail function only tells if there's an error
             wfDebug("Unknown error sending mail\n");
             return Status::newFatal('php-mail-error-unknown');
         } else {
             return Status::newGood();
         }
     }
 }
コード例 #4
0
ファイル: UserMailer.php プロジェクト: eFFemeer/seizamcore
 /**
  * This function will perform a direct (authenticated) login to
  * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
  * array of parameters. It requires PEAR:Mail to do that.
  * Otherwise it just uses the standard PHP 'mail' function.
  *
  * @param $to MailAddress: recipient's email (or an array of them)
  * @param $from MailAddress: sender's email
  * @param $subject String: email's subject.
  * @param $body String: email's text.
  * @param $replyto MailAddress: optional reply-to email (default: null).
  * @param $contentType String: optional custom Content-Type (default: text/plain; charset=UTF-8)
  * @return Status object
  */
 public static function send($to, $from, $subject, $body, $replyto = null, $contentType = 'text/plain; charset=UTF-8')
 {
     global $wgSMTP, $wgEnotifMaxRecips, $wgAdditionalMailParams;
     if (!is_array($to)) {
         $to = array($to);
     }
     wfDebug(__METHOD__ . ': sending mail to ' . implode(', ', $to) . "\n");
     if (is_array($wgSMTP)) {
         if (function_exists('stream_resolve_include_path')) {
             $found = stream_resolve_include_path('Mail.php');
         } else {
             $found = Fallback::stream_resolve_include_path('Mail.php');
         }
         if (!$found) {
             throw new MWException('PEAR mail package is not installed');
         }
         require_once 'Mail.php';
         $msgid = str_replace(" ", "_", microtime());
         if (function_exists('posix_getpid')) {
             $msgid .= '.' . posix_getpid();
         }
         if (is_array($to)) {
             $dest = array();
             foreach ($to as $u) {
                 $dest[] = $u->address;
             }
         } else {
             $dest = $to->address;
         }
         $headers['From'] = $from->toString();
         $headers['Return-Path'] = $from->address;
         if (count($to) > 1) {
             $headers['To'] = 'undisclosed-recipients:;';
         } else {
             $headers['To'] = $to[0]->toString();
         }
         if ($replyto) {
             $headers['Reply-To'] = $replyto->toString();
         }
         $headers['Subject'] = self::quotedPrintable($subject);
         $headers['Date'] = date('r');
         $headers['MIME-Version'] = '1.0';
         $headers['Content-type'] = is_null($contentType) ? 'text/plain; charset=UTF-8' : $contentType;
         $headers['Content-transfer-encoding'] = '8bit';
         // @todo FIXME
         $headers['Message-ID'] = "<{$msgid}@" . $wgSMTP['IDHost'] . '>';
         $headers['X-Mailer'] = 'MediaWiki mailer';
         wfSuppressWarnings();
         // Create the mail object using the Mail::factory method
         $mail_object =& Mail::factory('smtp', $wgSMTP);
         if (PEAR::isError($mail_object)) {
             wfDebug("PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n");
             wfRestoreWarnings();
             return Status::newFatal('pear-mail-error', $mail_object->getMessage());
         }
         wfDebug("Sending mail via PEAR::Mail to {$dest}\n");
         $chunks = array_chunk((array) $dest, $wgEnotifMaxRecips);
         foreach ($chunks as $chunk) {
             $status = self::sendWithPear($mail_object, $chunk, $headers, $body);
             if (!$status->isOK()) {
                 wfRestoreWarnings();
                 return $status;
             }
         }
         wfRestoreWarnings();
         return Status::newGood();
     } else {
         # Line endings need to be different on Unix and Windows due to
         # the bug described at http://trac.wordpress.org/ticket/2603
         if (wfIsWindows()) {
             $body = str_replace("\n", "\r\n", $body);
             $endl = "\r\n";
         } else {
             $endl = "\n";
         }
         $headers = array("MIME-Version: 1.0", "Content-type: {$contentType}", "Content-Transfer-Encoding: 8bit", "X-Mailer: MediaWiki mailer", "From: " . $from->toString());
         if ($replyto) {
             $headers[] = "Reply-To: " . $replyto->toString();
         }
         $headers = implode($endl, $headers);
         wfDebug("Sending mail via internal mail() function\n");
         self::$mErrorString = '';
         $html_errors = ini_get('html_errors');
         ini_set('html_errors', '0');
         set_error_handler('UserMailer::errorHandler');
         if (!is_array($to)) {
             $to = array($to);
         }
         foreach ($to as $recip) {
             $sent = mail($recip->toString(), self::quotedPrintable($subject), $body, $headers, $wgAdditionalMailParams);
         }
         restore_error_handler();
         ini_set('html_errors', $html_errors);
         if (self::$mErrorString) {
             wfDebug("Error sending mail: " . self::$mErrorString . "\n");
             return Status::newFatal('php-mail-error', self::$mErrorString);
         } elseif (!$sent) {
             // mail function only tells if there's an error
             wfDebug("Error sending mail\n");
             return Status::newFatal('php-mail-error-unknown');
         } else {
             return Status::newGood();
         }
     }
 }