~canonical-sysadmins/wordpress/4.7.4

« back to all changes in this revision

Viewing changes to wp-includes/class-smtp.php

  • Committer: Jamon Camisso
  • Date: 2017-01-12 15:31:31 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: jamon.camisso@canonical.com-20170112153131-yfjnoh8x21u7flat
Merge WP4.7.1 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
     * The PHPMailer SMTP version number.
31
31
     * @var string
32
32
     */
33
 
    const VERSION = '5.2.14';
 
33
    const VERSION = '5.2.22';
34
34
 
35
35
    /**
36
36
     * SMTP line break constant.
81
81
     * @deprecated Use the `VERSION` constant instead
82
82
     * @see SMTP::VERSION
83
83
     */
84
 
    public $Version = '5.2.14';
 
84
    public $Version = '5.2.22';
85
85
 
86
86
    /**
87
87
     * SMTP server port number.
150
150
     */
151
151
    public $Timelimit = 300;
152
152
 
 
153
        /**
 
154
         * @var array patterns to extract smtp transaction id from smtp reply
 
155
         * Only first capture group will be use, use non-capturing group to deal with it
 
156
         * Extend this class to override this property to fulfil your needs.
 
157
         */
 
158
        protected $smtp_transaction_id_patterns = array(
 
159
                'exim' => '/[0-9]{3} OK id=(.*)/',
 
160
                'sendmail' => '/[0-9]{3} 2.0.0 (.*) Message/',
 
161
                'postfix' => '/[0-9]{3} 2.0.0 Ok: queued as (.*)/'
 
162
        );
 
163
 
153
164
    /**
154
165
     * The socket for the server connection.
155
166
     * @var resource
206
217
        }
207
218
        //Avoid clash with built-in function names
208
219
        if (!in_array($this->Debugoutput, array('error_log', 'html', 'echo')) and is_callable($this->Debugoutput)) {
209
 
            call_user_func($this->Debugoutput, $str, $this->do_debug);
 
220
            call_user_func($this->Debugoutput, $str, $level);
210
221
            return;
211
222
        }
212
223
        switch ($this->Debugoutput) {
272
283
        $errstr = '';
273
284
        if ($streamok) {
274
285
            $socket_context = stream_context_create($options);
275
 
            //Suppress errors; connection failures are handled at a higher level
276
 
            $this->smtp_conn = @stream_socket_client(
 
286
            set_error_handler(array($this, 'errorHandler'));
 
287
            $this->smtp_conn = stream_socket_client(
277
288
                $host . ":" . $port,
278
289
                $errno,
279
290
                $errstr,
281
292
                STREAM_CLIENT_CONNECT,
282
293
                $socket_context
283
294
            );
 
295
            restore_error_handler();
284
296
        } else {
285
297
            //Fall back to fsockopen which should work in more places, but is missing some features
286
298
            $this->edebug(
287
299
                "Connection: stream_socket_client not available, falling back to fsockopen",
288
300
                self::DEBUG_CONNECTION
289
301
            );
 
302
            set_error_handler(array($this, 'errorHandler'));
290
303
            $this->smtp_conn = fsockopen(
291
304
                $host,
292
305
                $port,
294
307
                $errstr,
295
308
                $timeout
296
309
            );
 
310
            restore_error_handler();
297
311
        }
298
312
        // Verify we connected properly
299
313
        if (!is_resource($this->smtp_conn)) {
336
350
        if (!$this->sendCommand('STARTTLS', 'STARTTLS', 220)) {
337
351
            return false;
338
352
        }
 
353
 
 
354
        //Allow the best TLS version(s) we can
 
355
        $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
 
356
 
 
357
        //PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
 
358
        //so add them back in manually if we can
 
359
        if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
 
360
            $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
 
361
            $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
 
362
        }
 
363
 
339
364
        // Begin encrypted connection
340
365
        if (!stream_socket_enable_crypto(
341
366
            $this->smtp_conn,
342
367
            true,
343
 
            STREAM_CRYPTO_METHOD_TLS_CLIENT
 
368
            $crypto_method
344
369
        )) {
345
370
            return false;
346
371
        }
353
378
     * @see hello()
354
379
     * @param string $username The user name
355
380
     * @param string $password The password
356
 
     * @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH2)
 
381
     * @param string $authtype The auth type (PLAIN, LOGIN, CRAM-MD5)
357
382
     * @param string $realm The auth realm for NTLM
358
383
     * @param string $workstation The auth workstation for NTLM
359
384
     * @param null|OAuth $OAuth An optional OAuth instance (@see PHPMailerOAuth)
389
414
            );
390
415
 
391
416
            if (empty($authtype)) {
392
 
                foreach (array('LOGIN', 'CRAM-MD5', 'PLAIN') as $method) {
 
417
                foreach (array('CRAM-MD5', 'LOGIN', 'PLAIN') as $method) {
393
418
                    if (in_array($method, $this->server_caps['AUTH'])) {
394
419
                        $authtype = $method;
395
420
                        break;
673
698
    protected function parseHelloFields($type)
674
699
    {
675
700
        $this->server_caps = array();
676
 
        $lines = explode("\n", $this->last_reply);
 
701
        $lines = explode("\n", $this->helo_rply);
677
702
 
678
703
        foreach ($lines as $n => $s) {
679
704
            //First 4 chars contain response code followed by - or space
1115
1140
    {
1116
1141
        return $this->Timeout;
1117
1142
    }
 
1143
 
 
1144
    /**
 
1145
     * Reports an error number and string.
 
1146
     * @param integer $errno The error number returned by PHP.
 
1147
     * @param string $errmsg The error message returned by PHP.
 
1148
     */
 
1149
    protected function errorHandler($errno, $errmsg)
 
1150
    {
 
1151
        $notice = 'Connection: Failed to connect to server.';
 
1152
        $this->setError(
 
1153
            $notice,
 
1154
            $errno,
 
1155
            $errmsg
 
1156
        );
 
1157
        $this->edebug(
 
1158
            $notice . ' Error number ' . $errno . '. "Error notice: ' . $errmsg,
 
1159
            self::DEBUG_CONNECTION
 
1160
        );
 
1161
    }
 
1162
 
 
1163
        /**
 
1164
         * Will return the ID of the last smtp transaction based on a list of patterns provided
 
1165
         * in SMTP::$smtp_transaction_id_patterns.
 
1166
         * If no reply has been received yet, it will return null.
 
1167
         * If no pattern has been matched, it will return false.
 
1168
         * @return bool|null|string
 
1169
         */
 
1170
        public function getLastTransactionID()
 
1171
        {
 
1172
                $reply = $this->getLastReply();
 
1173
 
 
1174
                if (empty($reply)) {
 
1175
                        return null;
 
1176
                }
 
1177
 
 
1178
                foreach($this->smtp_transaction_id_patterns as $smtp_transaction_id_pattern) {
 
1179
                        if(preg_match($smtp_transaction_id_pattern, $reply, $matches)) {
 
1180
                                return $matches[1];
 
1181
                        }
 
1182
                }
 
1183
 
 
1184
                return false;
 
1185
    }
1118
1186
}