Make WordPress Core


Ignore:
Timestamp:
02/28/2013 06:33:13 PM (13 years ago)
Author:
nacin
Message:

Update to PHPMailer 5.2.4. props bpetty. fixes #21074.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-smtp.php

    r19849 r23522  
    33.---------------------------------------------------------------------------.
    44|  Software: PHPMailer - PHP email class                                    |
    5 |   Version: 5.2.1                                                          |
     5|   Version: 5.2.4                                                          |
    66|      Site: https://code.google.com/a/apache-extras.org/p/phpmailer/       |
    77| ------------------------------------------------------------------------- |
     
    3333 * @copyright 2010 - 2012 Jim Jagielski
    3434 * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
    35  * @version $Id: class.smtp.php 450 2010-06-23 16:46:33Z coolbru $
    3635 */
    3736
    3837/**
    39  * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
    40  * commands except TURN which will always return a not implemented
    41  * error. SMTP also provides some utility methods for sending mail
    42  * to an SMTP server.
    43  * original author: Chris Ryan
     38 * PHP RFC821 SMTP client
     39 *
     40 * Implements all the RFC 821 SMTP commands except TURN which will always return a not implemented error.
     41 * SMTP also provides some utility methods for sending mail to an SMTP server.
     42 * @author Chris Ryan
     43 * @package PHPMailer
    4444 */
    4545
     
    5252
    5353  /**
    54    *  SMTP reply line ending
     54   *  SMTP reply line ending (don't change)
    5555   *  @var string
    5656   */
     
    6464
    6565  /**
     66   * Sets the function/method to use for debugging output.
     67   * Right now we only honor "echo" or "error_log"
     68   * @var string
     69   */
     70  public $Debugoutput     = "echo";
     71
     72  /**
    6673   *  Sets VERP use on/off (default is off)
    6774   *  @var bool
     
    7077
    7178  /**
     79   * Sets the SMTP timeout value for reads, in seconds
     80   * @var int
     81   */
     82  public $Timeout         = 15;
     83
     84  /**
     85   * Sets the SMTP timelimit value for reads, in seconds
     86   * @var int
     87   */
     88  public $Timelimit       = 30;
     89
     90  /**
    7291   * Sets the SMTP PHPMailer Version number
    7392   * @var string
    7493   */
    75   public $Version         = '5.2.1';
     94  public $Version         = '5.2.4';
    7695
    7796  /////////////////////////////////////////////////
     
    7998  /////////////////////////////////////////////////
    8099
    81   private $smtp_conn; // the socket to the server
    82   private $error;     // error if any on the last call
    83   private $helo_rply; // the reply the server sent to us for HELO
     100  /**
     101   * @var resource The socket to the server
     102   */
     103  private $smtp_conn;
     104  /**
     105   * @var string Error message, if any, for the last call
     106   */
     107  private $error;
     108  /**
     109   * @var string The reply the server sent to us for HELO
     110   */
     111  private $helo_rply;
     112
     113  /**
     114   * Outputs debugging info via user-defined method
     115   * @param string $str
     116   */
     117  private function edebug($str) {
     118    if ($this->Debugoutput == "error_log") {
     119        error_log($str);
     120    } else {
     121        echo $str;
     122    }
     123  }
    84124
    85125  /**
    86126   * Initialize the class so that the data is in a known state.
    87127   * @access public
    88    * @return void
     128   * @return SMTP
    89129   */
    90130  public function __construct() {
     
    111151   * SMTP CODE FAILURE: 421
    112152   * @access public
     153   * @param string $host
     154   * @param int $port
     155   * @param int $tval
    113156   * @return bool
    114157   */
     
    140183                           "errstr" => $errstr);
    141184      if($this->do_debug >= 1) {
    142         echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
     185        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />');
    143186      }
    144187      return false;
     
    147190    // SMTP server can take longer to respond, give longer timeout for first read
    148191    // Windows does not have support for this timeout function
    149     if(substr(PHP_OS, 0, 3) != "WIN")
    150      socket_set_timeout($this->smtp_conn, $tval, 0);
     192    if(substr(PHP_OS, 0, 3) != "WIN") {
     193     $max = ini_get('max_execution_time');
     194     if ($max != 0 && $tval > $max) { // don't bother if unlimited
     195      @set_time_limit($tval);
     196     }
     197     stream_set_timeout($this->smtp_conn, $tval, 0);
     198    }
    151199
    152200    // get any announcement
     
    154202
    155203    if($this->do_debug >= 2) {
    156       echo "SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />';
     204      $this->edebug("SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />');
    157205    }
    158206
     
    183231
    184232    if($this->do_debug >= 2) {
    185       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     233      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    186234    }
    187235
     
    192240               "smtp_msg"  => substr($rply,4));
    193241      if($this->do_debug >= 1) {
    194         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     242        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    195243      }
    196244      return false;
     
    209257   * Hello() method.  Returns true if successfully authenticated.
    210258   * @access public
    211    * @return bool
    212    */
    213   public function Authenticate($username, $password) {
    214     // Start authentication
    215     fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
    216 
    217     $rply = $this->get_lines();
    218     $code = substr($rply,0,3);
    219 
    220     if($code != 334) {
    221       $this->error =
    222         array("error" => "AUTH not accepted from server",
    223               "smtp_code" => $code,
    224               "smtp_msg" => substr($rply,4));
    225       if($this->do_debug >= 1) {
    226         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
    227       }
    228       return false;
    229     }
    230 
    231     // Send encoded username
    232     fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
    233 
    234     $rply = $this->get_lines();
    235     $code = substr($rply,0,3);
    236 
    237     if($code != 334) {
    238       $this->error =
    239         array("error" => "Username not accepted from server",
    240               "smtp_code" => $code,
    241               "smtp_msg" => substr($rply,4));
    242       if($this->do_debug >= 1) {
    243         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
    244       }
    245       return false;
    246     }
    247 
    248     // Send encoded password
    249     fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
    250 
    251     $rply = $this->get_lines();
    252     $code = substr($rply,0,3);
    253 
    254     if($code != 235) {
    255       $this->error =
    256         array("error" => "Password not accepted from server",
    257               "smtp_code" => $code,
    258               "smtp_msg" => substr($rply,4));
    259       if($this->do_debug >= 1) {
    260         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
    261       }
    262       return false;
    263     }
    264 
     259   * @param string $username
     260   * @param string $password
     261   * @param string $authtype
     262   * @param string $realm
     263   * @param string $workstation
     264   * @return bool
     265   */
     266  public function Authenticate($username, $password, $authtype='LOGIN', $realm='', $workstation='') {
     267    if (empty($authtype)) {
     268      $authtype = 'LOGIN';
     269    }
     270
     271    switch ($authtype) {
     272      case 'PLAIN':
     273        // Start authentication
     274        fputs($this->smtp_conn,"AUTH PLAIN" . $this->CRLF);
     275   
     276        $rply = $this->get_lines();
     277        $code = substr($rply,0,3);
     278   
     279        if($code != 334) {
     280          $this->error =
     281            array("error" => "AUTH not accepted from server",
     282                  "smtp_code" => $code,
     283                  "smtp_msg" => substr($rply,4));
     284          if($this->do_debug >= 1) {
     285            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     286          }
     287          return false;
     288        }
     289        // Send encoded username and password
     290        fputs($this->smtp_conn, base64_encode("\0".$username."\0".$password) . $this->CRLF);
     291
     292        $rply = $this->get_lines();
     293        $code = substr($rply,0,3);
     294   
     295        if($code != 235) {
     296          $this->error =
     297            array("error" => "Authentication not accepted from server",
     298                  "smtp_code" => $code,
     299                  "smtp_msg" => substr($rply,4));
     300          if($this->do_debug >= 1) {
     301            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     302          }
     303          return false;
     304        }
     305        break;
     306      case 'LOGIN':
     307        // Start authentication
     308        fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
     309   
     310        $rply = $this->get_lines();
     311        $code = substr($rply,0,3);
     312   
     313        if($code != 334) {
     314          $this->error =
     315            array("error" => "AUTH not accepted from server",
     316                  "smtp_code" => $code,
     317                  "smtp_msg" => substr($rply,4));
     318          if($this->do_debug >= 1) {
     319            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     320          }
     321          return false;
     322        }
     323   
     324        // Send encoded username
     325        fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
     326   
     327        $rply = $this->get_lines();
     328        $code = substr($rply,0,3);
     329   
     330        if($code != 334) {
     331          $this->error =
     332            array("error" => "Username not accepted from server",
     333                  "smtp_code" => $code,
     334                  "smtp_msg" => substr($rply,4));
     335          if($this->do_debug >= 1) {
     336            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     337          }
     338          return false;
     339        }
     340   
     341        // Send encoded password
     342        fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
     343   
     344        $rply = $this->get_lines();
     345        $code = substr($rply,0,3);
     346   
     347        if($code != 235) {
     348          $this->error =
     349            array("error" => "Password not accepted from server",
     350                  "smtp_code" => $code,
     351                  "smtp_msg" => substr($rply,4));
     352          if($this->do_debug >= 1) {
     353            $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
     354          }
     355          return false;
     356        }
     357        break;
     358      case 'NTLM':
     359        /*
     360         * ntlm_sasl_client.php
     361         ** Bundled with Permission
     362         **
     363         ** How to telnet in windows: http://technet.microsoft.com/en-us/library/aa995718%28EXCHG.65%29.aspx
     364         ** PROTOCOL Documentation http://curl.haxx.se/rfc/ntlm.html#ntlmSmtpAuthentication
     365         */
     366        require_once('ntlm_sasl_client.php');
     367        $temp = new stdClass();
     368        $ntlm_client = new ntlm_sasl_client_class;
     369        if(! $ntlm_client->Initialize($temp)){//let's test if every function its available
     370            $this->error = array("error" => $temp->error);
     371            if($this->do_debug >= 1) {
     372                $this->edebug("You need to enable some modules in your php.ini file: " . $this->error["error"] . $this->CRLF);
     373            }
     374            return false;
     375        }
     376        $msg1 = $ntlm_client->TypeMsg1($realm, $workstation);//msg1
     377       
     378        fputs($this->smtp_conn,"AUTH NTLM " . base64_encode($msg1) . $this->CRLF);
     379
     380        $rply = $this->get_lines();
     381        $code = substr($rply,0,3);
     382       
     383
     384        if($code != 334) {
     385            $this->error =
     386                array("error" => "AUTH not accepted from server",
     387                      "smtp_code" => $code,
     388                      "smtp_msg" => substr($rply,4));
     389            if($this->do_debug >= 1) {
     390                $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF);
     391            }
     392            return false;
     393        }
     394       
     395        $challange = substr($rply,3);//though 0 based, there is a white space after the 3 digit number....//msg2
     396        $challange = base64_decode($challange);
     397        $ntlm_res = $ntlm_client->NTLMResponse(substr($challange,24,8),$password);
     398        $msg3 = $ntlm_client->TypeMsg3($ntlm_res,$username,$realm,$workstation);//msg3
     399        // Send encoded username
     400        fputs($this->smtp_conn, base64_encode($msg3) . $this->CRLF);
     401
     402        $rply = $this->get_lines();
     403        $code = substr($rply,0,3);
     404
     405        if($code != 235) {
     406            $this->error =
     407                array("error" => "Could not authenticate",
     408                      "smtp_code" => $code,
     409                      "smtp_msg" => substr($rply,4));
     410            if($this->do_debug >= 1) {
     411                $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF);
     412            }
     413            return false;
     414        }
     415        break;
     416    }
    265417    return true;
    266418  }
     
    277429        // the socket is valid but we are not connected
    278430        if($this->do_debug >= 1) {
    279             echo "SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected";
     431            $this->edebug("SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected");
    280432        }
    281433        $this->Close();
     
    325477   * SMTP CODE ERROR  : 500,501,503,421
    326478   * @access public
     479   * @param string $msg_data
    327480   * @return bool
    328481   */
     
    342495
    343496    if($this->do_debug >= 2) {
    344       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     497      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    345498    }
    346499
     
    351504              "smtp_msg" => substr($rply,4));
    352505      if($this->do_debug >= 1) {
    353         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     506        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    354507      }
    355508      return false;
     
    436589
    437590    if($this->do_debug >= 2) {
    438       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     591      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    439592    }
    440593
     
    445598              "smtp_msg" => substr($rply,4));
    446599      if($this->do_debug >= 1) {
    447         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     600        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    448601      }
    449602      return false;
     
    462615   * SMTP CODE ERROR  : 500, 501, 504, 421
    463616   * @access public
     617   * @param string $host
    464618   * @return bool
    465619   */
     
    492646   * Sends a HELO/EHLO command.
    493647   * @access private
     648   * @param string $hello
     649   * @param string $host
    494650   * @return bool
    495651   */
     
    501657
    502658    if($this->do_debug >= 2) {
    503       echo "SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />';
     659      $this->edebug("SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />');
    504660    }
    505661
     
    510666              "smtp_msg" => substr($rply,4));
    511667      if($this->do_debug >= 1) {
    512         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     668        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    513669      }
    514670      return false;
     
    532688   * SMTP CODE SUCCESS: 500,501,421
    533689   * @access public
     690   * @param string $from
    534691   * @return bool
    535692   */
     
    543700    }
    544701
    545     $useVerp = ($this->do_verp ? "XVERP" : "");
     702    $useVerp = ($this->do_verp ? " XVERP" : "");
    546703    fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
    547704
     
    550707
    551708    if($this->do_debug >= 2) {
    552       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     709      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    553710    }
    554711
     
    559716              "smtp_msg" => substr($rply,4));
    560717      if($this->do_debug >= 1) {
    561         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     718        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    562719      }
    563720      return false;
     
    575732   * SMTP CODE ERROR  : 500
    576733   * @access public
     734   * @param bool $close_on_error
    577735   * @return bool
    578736   */
     
    593751
    594752    if($this->do_debug >= 2) {
    595       echo "SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />';
     753      $this->edebug("SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />');
    596754    }
    597755
     
    607765      $rval = false;
    608766      if($this->do_debug >= 1) {
    609         echo "SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />';
     767        $this->edebug("SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />');
    610768      }
    611769    }
     
    628786   * SMTP CODE ERROR  : 500,501,503,421
    629787   * @access public
     788   * @param string $to
    630789   * @return bool
    631790   */
     
    645804
    646805    if($this->do_debug >= 2) {
    647       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     806      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    648807    }
    649808
     
    654813              "smtp_msg" => substr($rply,4));
    655814      if($this->do_debug >= 1) {
    656         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     815        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    657816      }
    658817      return false;
     
    688847
    689848    if($this->do_debug >= 2) {
    690       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     849      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    691850    }
    692851
     
    697856              "smtp_msg" => substr($rply,4));
    698857      if($this->do_debug >= 1) {
    699         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     858        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    700859      }
    701860      return false;
     
    719878   * SMTP CODE SUCCESS: 500,501,502,421
    720879   * @access public
     880   * @param string $from
    721881   * @return bool
    722882   */
     
    736896
    737897    if($this->do_debug >= 2) {
    738       echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
     898      $this->edebug("SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />');
    739899    }
    740900
     
    745905              "smtp_msg" => substr($rply,4));
    746906      if($this->do_debug >= 1) {
    747         echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
     907        $this->edebug("SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />');
    748908      }
    749909      return false;
     
    769929                                    "is not implemented");
    770930    if($this->do_debug >= 1) {
    771       echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />';
     931      $this->edebug("SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />');
    772932    }
    773933    return false;
     
    798958  private function get_lines() {
    799959    $data = "";
    800     while(!feof($this->smtp_conn)) {
     960    $endtime = 0;
     961    /* If for some reason the fp is bad, don't inf loop */
     962    if (!is_resource($this->smtp_conn)) {
     963      return $data;
     964    }
     965    stream_set_timeout($this->smtp_conn, $this->Timeout);
     966    if ($this->Timelimit > 0) {
     967      $endtime = time() + $this->Timelimit;
     968    }
     969    while(is_resource($this->smtp_conn) && !feof($this->smtp_conn)) {
    801970      $str = @fgets($this->smtp_conn,515);
    802971      if($this->do_debug >= 4) {
    803         echo "SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />';
    804         echo "SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />';
     972        $this->edebug("SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />');
     973        $this->edebug("SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />');
    805974      }
    806975      $data .= $str;
    807976      if($this->do_debug >= 4) {
    808         echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />';
     977        $this->edebug("SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />');
    809978      }
    810979      // if 4th character is a space, we are done reading, break the loop
    811980      if(substr($str,3,1) == " ") { break; }
     981      // Timed-out? Log and break
     982      $info = stream_get_meta_data($this->smtp_conn);
     983      if ($info['timed_out']) {
     984        if($this->do_debug >= 4) {
     985          $this->edebug("SMTP -> get_lines(): timed-out (" . $this->Timeout . " seconds) <br />");
     986        }
     987        break;
     988      }
     989      // Now check if reads took too long
     990      if ($endtime) {
     991        if (time() > $endtime) {
     992          if($this->do_debug >= 4) {
     993            $this->edebug("SMTP -> get_lines(): timelimit reached (" . $this->Timelimit . " seconds) <br />");
     994          }
     995          break;
     996        }
     997      }
    812998    }
    813999    return $data;
     
    8151001
    8161002}
    817 
    8181003?>
Note: See TracChangeset for help on using the changeset viewer.