| 1 |
<?php |
| 2 |
/** vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ |
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | PHP Version 5 and 7 | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | Copyright (c) 1997-2021 Jon Parise and Chuck Hagenbuch | |
| 7 |
// | All rights reserved. | |
| 8 |
// | | |
| 9 |
// | Redistribution and use in source and binary forms, with or without | |
| 10 |
// | modification, are permitted provided that the following conditions | |
| 11 |
// | are met: | |
| 12 |
// | | |
| 13 |
// | 1. Redistributions of source code must retain the above copyright | |
| 14 |
// | notice, this list of conditions and the following disclaimer. | |
| 15 |
// | | |
| 16 |
// | 2. Redistributions in binary form must reproduce the above copyright | |
| 17 |
// | notice, this list of conditions and the following disclaimer in | |
| 18 |
// | the documentation and/or other materials provided with the | |
| 19 |
// | distribution. | |
| 20 |
// | | |
| 21 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 22 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 23 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 24 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 25 |
// | COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 26 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 27 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 28 |
// | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 29 |
// | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 30 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| 31 |
// | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 32 |
// | POSSIBILITY OF SUCH DAMAGE. | |
| 33 |
// +----------------------------------------------------------------------+ |
| 34 |
// | Authors: Chuck Hagenbuch <[email protected]> | |
| 35 |
// | Jon Parise <[email protected]> | |
| 36 |
// | Damian Alejandro Fernandez Sosa <[email protected]> | |
| 37 |
// +----------------------------------------------------------------------+ |
| 38 |
|
| 39 |
require_once 'PEAR.php'; |
| 40 |
require_once 'Net/Socket.php'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Provides an implementation of the SMTP protocol using PEAR's |
| 44 |
* Net_Socket class. |
| 45 |
* |
| 46 |
* @package Net_SMTP |
| 47 |
* @author Chuck Hagenbuch <[email protected]> |
| 48 |
* @author Jon Parise <[email protected]> |
| 49 |
* @author Damian Alejandro Fernandez Sosa <[email protected]> |
| 50 |
* @license http://opensource.org/licenses/bsd-license.php BSD-2-Clause |
| 51 |
* |
| 52 |
* @example basic.php A basic implementation of the Net_SMTP package. |
| 53 |
*/ |
| 54 |
class Net_SMTP |
| 55 |
{ |
| 56 |
/** |
| 57 |
* The server to connect to. |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
public $host = 'localhost'; |
| 61 |
|
| 62 |
/** |
| 63 |
* The port to connect to. |
| 64 |
* @var int |
| 65 |
*/ |
| 66 |
public $port = 25; |
| 67 |
|
| 68 |
/** |
| 69 |
* The value to give when sending EHLO or HELO. |
| 70 |
* @var string |
| 71 |
*/ |
| 72 |
public $localhost = 'localhost'; |
| 73 |
|
| 74 |
/** |
| 75 |
* List of supported authentication methods, in preferential order. |
| 76 |
* @var array |
| 77 |
*/ |
| 78 |
public $auth_methods = array(); |
| 79 |
|
| 80 |
/** |
| 81 |
* Use SMTP command pipelining (specified in RFC 2920) if the SMTP |
| 82 |
* server supports it. |
| 83 |
* |
| 84 |
* When pipeling is enabled, rcptTo(), mailFrom(), sendFrom(), |
| 85 |
* somlFrom() and samlFrom() do not wait for a response from the |
| 86 |
* SMTP server but return immediately. |
| 87 |
* |
| 88 |
* @var bool |
| 89 |
*/ |
| 90 |
public $pipelining = false; |
| 91 |
|
| 92 |
/** |
| 93 |
* Number of pipelined commands. |
| 94 |
* @var int |
| 95 |
*/ |
| 96 |
protected $pipelined_commands = 0; |
| 97 |
|
| 98 |
/** |
| 99 |
* Should debugging output be enabled? |
| 100 |
* @var boolean |
| 101 |
*/ |
| 102 |
protected $debug = false; |
| 103 |
|
| 104 |
/** |
| 105 |
* Debug output handler. |
| 106 |
* @var callback |
| 107 |
*/ |
| 108 |
protected $debug_handler = null; |
| 109 |
|
| 110 |
/** |
| 111 |
* The socket resource being used to connect to the SMTP server. |
| 112 |
* @var resource |
| 113 |
*/ |
| 114 |
protected $socket = null; |
| 115 |
|
| 116 |
/** |
| 117 |
* Array of socket options that will be passed to Net_Socket::connect(). |
| 118 |
* @see stream_context_create() |
| 119 |
* @var array |
| 120 |
*/ |
| 121 |
protected $socket_options = null; |
| 122 |
|
| 123 |
/** |
| 124 |
* The socket I/O timeout value in seconds. |
| 125 |
* @var int |
| 126 |
*/ |
| 127 |
protected $timeout = 0; |
| 128 |
|
| 129 |
/** |
| 130 |
* The most recent server response code. |
| 131 |
* @var int |
| 132 |
*/ |
| 133 |
protected $code = -1; |
| 134 |
|
| 135 |
/** |
| 136 |
* The most recent server response arguments. |
| 137 |
* @var array |
| 138 |
*/ |
| 139 |
protected $arguments = array(); |
| 140 |
|
| 141 |
/** |
| 142 |
* Stores the SMTP server's greeting string. |
| 143 |
* @var string |
| 144 |
*/ |
| 145 |
protected $greeting = null; |
| 146 |
|
| 147 |
/** |
| 148 |
* Stores detected features of the SMTP server. |
| 149 |
* @var array |
| 150 |
*/ |
| 151 |
protected $esmtp = array(); |
| 152 |
|
| 153 |
/** |
| 154 |
* GSSAPI principal. |
| 155 |
* @var string |
| 156 |
*/ |
| 157 |
protected $gssapi_principal = null; |
| 158 |
|
| 159 |
/** |
| 160 |
* GSSAPI principal. |
| 161 |
* @var string |
| 162 |
*/ |
| 163 |
protected $gssapi_cname = null; |
| 164 |
|
| 165 |
/** |
| 166 |
* SCRAM SHA-Hash algorithm. |
| 167 |
* |
| 168 |
* @var string |
| 169 |
*/ |
| 170 |
protected $scram_sha_hash_algorithm = null; |
| 171 |
|
| 172 |
/** |
| 173 |
* Instantiates a new Net_SMTP object, overriding any defaults |
| 174 |
* with parameters that are passed in. |
| 175 |
* |
| 176 |
* If you have SSL support in PHP, you can connect to a server |
| 177 |
* over SSL using an 'ssl://' prefix: |
| 178 |
* |
| 179 |
* // 465 is a common smtps port. |
| 180 |
* $smtp = new Net_SMTP('ssl://mail.host.com', 465); |
| 181 |
* $smtp->connect(); |
| 182 |
* |
| 183 |
* @param string $host The server to connect to. |
| 184 |
* @param integer $port The port to connect to. |
| 185 |
* @param string $localhost The value to give when sending EHLO or HELO. |
| 186 |
* @param boolean $pipelining Use SMTP command pipelining |
| 187 |
* @param integer $timeout Socket I/O timeout in seconds. |
| 188 |
* @param array $socket_options Socket stream_context_create() options. |
| 189 |
* @param string $gssapi_principal GSSAPI service principal name |
| 190 |
* @param string $gssapi_cname GSSAPI credentials cache |
| 191 |
* |
| 192 |
* @since 1.0 |
| 193 |
*/ |
| 194 |
public function __construct($host = null, $port = null, $localhost = null, |
| 195 |
$pipelining = false, $timeout = 0, $socket_options = null, |
| 196 |
$gssapi_principal = null, $gssapi_cname = null |
| 197 |
) { |
| 198 |
if (isset($host)) { |
| 199 |
$this->host = $host; |
| 200 |
} |
| 201 |
if (isset($port)) { |
| 202 |
$this->port = $port; |
| 203 |
} |
| 204 |
if (isset($localhost)) { |
| 205 |
$this->localhost = $localhost; |
| 206 |
} |
| 207 |
|
| 208 |
$this->pipelining = $pipelining; |
| 209 |
$this->socket = new Net_Socket(); |
| 210 |
$this->socket_options = $socket_options; |
| 211 |
$this->timeout = $timeout; |
| 212 |
$this->gssapi_principal = $gssapi_principal; |
| 213 |
$this->gssapi_cname = $gssapi_cname; |
| 214 |
|
| 215 |
/* If PHP krb5 extension is loaded, we enable GSSAPI method. */ |
| 216 |
if (isset($gssapi_principal) && extension_loaded('krb5')) { |
| 217 |
$this->setAuthMethod('GSSAPI', array($this, 'authGSSAPI')); |
| 218 |
} |
| 219 |
|
| 220 |
/* Include the Auth_SASL package. If the package is available, we |
| 221 |
* enable the authentication methods that depend upon it. */ |
| 222 |
if (@include_once 'Auth/SASL.php') { |
| 223 |
$this->setAuthMethod('CRAM-MD5', array($this, 'authCramMD5')); |
| 224 |
$this->setAuthMethod('DIGEST-MD5', array($this, 'authDigestMD5')); |
| 225 |
$this->setAuthMethod('SCRAM-SHA-1', array($this, 'authScramSHA1')); |
| 226 |
$this->setAuthMethod('SCRAM-SHA-224', array($this, 'authScramSHA224')); |
| 227 |
$this->setAuthMethod('SCRAM-SHA-256', array($this, 'authScramSHA256')); |
| 228 |
$this->setAuthMethod('SCRAM-SHA-384', array($this, 'authScramSHA384')); |
| 229 |
$this->setAuthMethod('SCRAM-SHA-512', array($this, 'authScramSHA512')); |
| 230 |
} |
| 231 |
|
| 232 |
/* These standard authentication methods are always available. */ |
| 233 |
$this->setAuthMethod('LOGIN', array($this, 'authLogin'), false); |
| 234 |
$this->setAuthMethod('PLAIN', array($this, 'authPlain'), false); |
| 235 |
$this->setAuthMethod('XOAUTH2', array($this, 'authXOAuth2'), false); |
| 236 |
$this->setAuthMethod('OAUTHBEARER', array($this, 'authOAuthBearer'), false); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Set the socket I/O timeout value in seconds plus microseconds. |
| 241 |
* |
| 242 |
* @param integer $seconds Timeout value in seconds. |
| 243 |
* @param integer $microseconds Additional value in microseconds. |
| 244 |
* |
| 245 |
* @since 1.5.0 |
| 246 |
*/ |
| 247 |
public function setTimeout($seconds, $microseconds = 0) |
| 248 |
{ |
| 249 |
return $this->socket->setTimeout($seconds, $microseconds); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Set the value of the debugging flag. |
| 254 |
* |
| 255 |
* @param boolean $debug New value for the debugging flag. |
| 256 |
* @param callback $handler Debug handler callback |
| 257 |
* |
| 258 |
* @since 1.1.0 |
| 259 |
*/ |
| 260 |
public function setDebug($debug, $handler = null) |
| 261 |
{ |
| 262 |
$this->debug = $debug; |
| 263 |
$this->debug_handler = $handler; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Write the given debug text to the current debug output handler. |
| 268 |
* |
| 269 |
* @param string $message Debug mesage text. |
| 270 |
* |
| 271 |
* @since 1.3.3 |
| 272 |
*/ |
| 273 |
protected function debug($message) |
| 274 |
{ |
| 275 |
if ($this->debug) { |
| 276 |
if ($this->debug_handler) { |
| 277 |
call_user_func_array( |
| 278 |
$this->debug_handler, array(&$this, $message) |
| 279 |
); |
| 280 |
} else { |
| 281 |
echo "DEBUG: $message\n"; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Send the given string of data to the server. |
| 288 |
* |
| 289 |
* @param string $data The string of data to send. |
| 290 |
* |
| 291 |
* @return mixed The number of bytes that were actually written, |
| 292 |
* or a PEAR_Error object on failure. |
| 293 |
* |
| 294 |
* @since 1.1.0 |
| 295 |
*/ |
| 296 |
protected function send($data) |
| 297 |
{ |
| 298 |
$this->debug("Send: $data"); |
| 299 |
|
| 300 |
$result = $this->socket->write($data); |
| 301 |
if (!$result || PEAR::isError($result)) { |
| 302 |
$msg = $result ? $result->getMessage() : "unknown error"; |
| 303 |
return PEAR::raiseError("Failed to write to socket: $msg"); |
| 304 |
} |
| 305 |
|
| 306 |
return $result; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Send a command to the server with an optional string of |
| 311 |
* arguments. A carriage return / linefeed (CRLF) sequence will |
| 312 |
* be appended to each command string before it is sent to the |
| 313 |
* SMTP server - an error will be thrown if the command string |
| 314 |
* already contains any newline characters. Use send() for |
| 315 |
* commands that must contain newlines. |
| 316 |
* |
| 317 |
* @param string $command The SMTP command to send to the server. |
| 318 |
* @param string $args A string of optional arguments to append |
| 319 |
* to the command. |
| 320 |
* |
| 321 |
* @return mixed The result of the send() call. |
| 322 |
* |
| 323 |
* @since 1.1.0 |
| 324 |
*/ |
| 325 |
protected function put($command, $args = '') |
| 326 |
{ |
| 327 |
if (!empty($args)) { |
| 328 |
$command .= ' ' . $args; |
| 329 |
} |
| 330 |
|
| 331 |
if (strcspn($command, "\r\n") !== strlen($command)) { |
| 332 |
return PEAR::raiseError('Commands cannot contain newlines'); |
| 333 |
} |
| 334 |
|
| 335 |
return $this->send($command . "\r\n"); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Read a reply from the SMTP server. The reply consists of a response |
| 340 |
* code and a response message. |
| 341 |
* |
| 342 |
* @param mixed $valid The set of valid response codes. These |
| 343 |
* may be specified as an array of integer |
| 344 |
* values or as a single integer value. |
| 345 |
* @param bool $later Do not parse the response now, but wait |
| 346 |
* until the last command in the pipelined |
| 347 |
* command group |
| 348 |
* |
| 349 |
* @return mixed True if the server returned a valid response code or |
| 350 |
* a PEAR_Error object is an error condition is reached. |
| 351 |
* |
| 352 |
* @since 1.1.0 |
| 353 |
* |
| 354 |
* @see getResponse |
| 355 |
*/ |
| 356 |
protected function parseResponse($valid, $later = false) |
| 357 |
{ |
| 358 |
$this->code = -1; |
| 359 |
$this->arguments = array(); |
| 360 |
|
| 361 |
if ($later) { |
| 362 |
$this->pipelined_commands++; |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
for ($i = 0; $i <= $this->pipelined_commands; $i++) { |
| 367 |
while ($line = $this->socket->readLine()) { |
| 368 |
$this->debug("Recv: $line"); |
| 369 |
|
| 370 |
/* If we receive an empty line, the connection was closed. */ |
| 371 |
if (empty($line)) { |
| 372 |
$this->disconnect(); |
| 373 |
return PEAR::raiseError('Connection was closed'); |
| 374 |
} |
| 375 |
|
| 376 |
/* Read the code and store the rest in the arguments array. */ |
| 377 |
$code = substr($line, 0, 3); |
| 378 |
$this->arguments[] = trim(substr($line, 4)); |
| 379 |
|
| 380 |
/* Check the syntax of the response code. */ |
| 381 |
if (is_numeric($code)) { |
| 382 |
$this->code = (int)$code; |
| 383 |
} else { |
| 384 |
$this->code = -1; |
| 385 |
break; |
| 386 |
} |
| 387 |
|
| 388 |
/* If this is not a multiline response, we're done. */ |
| 389 |
if (substr($line, 3, 1) != '-') { |
| 390 |
break; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
$this->pipelined_commands = 0; |
| 396 |
|
| 397 |
/* Compare the server's response code with the valid code/codes. */ |
| 398 |
if (is_int($valid) && ($this->code === $valid)) { |
| 399 |
return true; |
| 400 |
} elseif (is_array($valid) && in_array($this->code, $valid, true)) { |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
return PEAR::raiseError('Invalid response code received from server', $this->code); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Issue an SMTP command and verify its response. |
| 409 |
* |
| 410 |
* @param string $command The SMTP command string or data. |
| 411 |
* @param mixed $valid The set of valid response codes. These |
| 412 |
* may be specified as an array of integer |
| 413 |
* values or as a single integer value. |
| 414 |
* |
| 415 |
* @return mixed True on success or a PEAR_Error object on failure. |
| 416 |
* |
| 417 |
* @since 1.6.0 |
| 418 |
*/ |
| 419 |
public function command($command, $valid) |
| 420 |
{ |
| 421 |
if (PEAR::isError($error = $this->put($command))) { |
| 422 |
return $error; |
| 423 |
} |
| 424 |
if (PEAR::isError($error = $this->parseResponse($valid))) { |
| 425 |
return $error; |
| 426 |
} |
| 427 |
|
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Return a 2-tuple containing the last response from the SMTP server. |
| 433 |
* |
| 434 |
* @return array A two-element array: the first element contains the |
| 435 |
* response code as an integer and the second element |
| 436 |
* contains the response's arguments as a string. |
| 437 |
* |
| 438 |
* @since 1.1.0 |
| 439 |
*/ |
| 440 |
public function getResponse() |
| 441 |
{ |
| 442 |
return array($this->code, implode("\n", $this->arguments)); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Return the SMTP server's greeting string. |
| 447 |
* |
| 448 |
* @return string A string containing the greeting string, or null if |
| 449 |
* a greeting has not been received. |
| 450 |
* |
| 451 |
* @since 1.3.3 |
| 452 |
*/ |
| 453 |
public function getGreeting() |
| 454 |
{ |
| 455 |
return $this->greeting; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Attempt to connect to the SMTP server. |
| 460 |
* |
| 461 |
* @param int $timeout The timeout value (in seconds) for the |
| 462 |
* socket connection attempt. |
| 463 |
* @param bool $persistent Should a persistent socket connection |
| 464 |
* be used? |
| 465 |
* |
| 466 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 467 |
* kind of failure, or true on success. |
| 468 |
* @since 1.0 |
| 469 |
*/ |
| 470 |
public function connect($timeout = null, $persistent = false) |
| 471 |
{ |
| 472 |
$this->greeting = null; |
| 473 |
|
| 474 |
$result = $this->socket->connect( |
| 475 |
$this->host, $this->port, $persistent, $timeout, $this->socket_options |
| 476 |
); |
| 477 |
|
| 478 |
if (PEAR::isError($result)) { |
| 479 |
return PEAR::raiseError( |
| 480 |
'Failed to connect socket: ' . $result->getMessage() |
| 481 |
); |
| 482 |
} |
| 483 |
|
| 484 |
/* |
| 485 |
* Now that we're connected, reset the socket's timeout value for |
| 486 |
* future I/O operations. This allows us to have different socket |
| 487 |
* timeout values for the initial connection (our $timeout parameter) |
| 488 |
* and all other socket operations. |
| 489 |
*/ |
| 490 |
if ($this->timeout > 0) { |
| 491 |
if (PEAR::isError($error = $this->setTimeout($this->timeout))) { |
| 492 |
return $error; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
if (PEAR::isError($error = $this->parseResponse(220))) { |
| 497 |
return $error; |
| 498 |
} |
| 499 |
|
| 500 |
/* Extract and store a copy of the server's greeting string. */ |
| 501 |
list(, $this->greeting) = $this->getResponse(); |
| 502 |
|
| 503 |
if (PEAR::isError($error = $this->negotiate())) { |
| 504 |
return $error; |
| 505 |
} |
| 506 |
|
| 507 |
return true; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Attempt to disconnect from the SMTP server. |
| 512 |
* |
| 513 |
* @param bool $force Forces a disconnection of the socket even if |
| 514 |
* the QUIT command fails |
| 515 |
* |
| 516 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 517 |
* kind of failure, or true on success. |
| 518 |
* @since 1.0 |
| 519 |
*/ |
| 520 |
public function disconnect($force = false) |
| 521 |
{ |
| 522 |
/* parseResponse is only needed if put QUIT is successful */ |
| 523 |
if (!PEAR::isError($error = $this->put('QUIT'))) { |
| 524 |
$error = $this->parseResponse(221); |
| 525 |
} |
| 526 |
|
| 527 |
/* disconnecting socket if there is no error on the QUIT |
| 528 |
* command or force disconnecting is requested */ |
| 529 |
if (!PEAR::isError($error) || $force) { |
| 530 |
if (PEAR::isError($error_socket = $this->socket->disconnect())) { |
| 531 |
return PEAR::raiseError( |
| 532 |
'Failed to disconnect socket: ' . $error_socket->getMessage() |
| 533 |
); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
if (PEAR::isError($error)) { |
| 538 |
return $error; |
| 539 |
} |
| 540 |
|
| 541 |
return true; |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Attempt to send the EHLO command and obtain a list of ESMTP |
| 546 |
* extensions available, and failing that just send HELO. |
| 547 |
* |
| 548 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 549 |
* kind of failure, or true on success. |
| 550 |
* |
| 551 |
* @since 1.1.0 |
| 552 |
*/ |
| 553 |
protected function negotiate() |
| 554 |
{ |
| 555 |
if (PEAR::isError($error = $this->put('EHLO', $this->localhost))) { |
| 556 |
return $error; |
| 557 |
} |
| 558 |
|
| 559 |
if (PEAR::isError($this->parseResponse(250))) { |
| 560 |
/* If the EHLO failed, try the simpler HELO command. */ |
| 561 |
if (PEAR::isError($error = $this->put('HELO', $this->localhost))) { |
| 562 |
return $error; |
| 563 |
} |
| 564 |
if (PEAR::isError($this->parseResponse(250))) { |
| 565 |
return PEAR::raiseError('HELO was not accepted', $this->code); |
| 566 |
} |
| 567 |
|
| 568 |
return true; |
| 569 |
} |
| 570 |
|
| 571 |
foreach ($this->arguments as $argument) { |
| 572 |
$verb = strtok($argument, ' '); |
| 573 |
$len = strlen($verb); |
| 574 |
$arguments = substr($argument, $len + 1, strlen($argument) - $len - 1); |
| 575 |
$this->esmtp[$verb] = $arguments; |
| 576 |
} |
| 577 |
|
| 578 |
if (!isset($this->esmtp['PIPELINING'])) { |
| 579 |
$this->pipelining = false; |
| 580 |
} |
| 581 |
|
| 582 |
return true; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Returns the name of the best authentication method that the server |
| 587 |
* has advertised. |
| 588 |
* |
| 589 |
* @return mixed Returns a string containing the name of the best |
| 590 |
* supported authentication method or a PEAR_Error object |
| 591 |
* if a failure condition is encountered. |
| 592 |
* @since 1.1.0 |
| 593 |
*/ |
| 594 |
protected function getBestAuthMethod() |
| 595 |
{ |
| 596 |
$available_methods = explode(' ', $this->esmtp['AUTH']); |
| 597 |
|
| 598 |
foreach ($this->auth_methods as $method => $callback) { |
| 599 |
if (in_array($method, $available_methods)) { |
| 600 |
return $method; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
return PEAR::raiseError('No supported authentication methods'); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Establish STARTTLS Connection. |
| 609 |
* |
| 610 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 611 |
* kind of failure, true on success, or false if SSL/TLS |
| 612 |
* isn't available. |
| 613 |
* @since 1.10.0 |
| 614 |
*/ |
| 615 |
public function starttls() |
| 616 |
{ |
| 617 |
/* We can only attempt a TLS connection if one has been requested, |
| 618 |
* we're running PHP 5.1.0 or later, have access to the OpenSSL |
| 619 |
* extension, are connected to an SMTP server which supports the |
| 620 |
* STARTTLS extension, and aren't already connected over a secure |
| 621 |
* (SSL) socket connection. */ |
| 622 |
if (version_compare(PHP_VERSION, '5.1.0', '>=') |
| 623 |
&& extension_loaded('openssl') && isset($this->esmtp['STARTTLS']) |
| 624 |
&& strncasecmp($this->host, 'ssl://', 6) !== 0 |
| 625 |
) { |
| 626 |
/* Start the TLS connection attempt. */ |
| 627 |
if (PEAR::isError($result = $this->put('STARTTLS'))) { |
| 628 |
return $result; |
| 629 |
} |
| 630 |
if (PEAR::isError($result = $this->parseResponse(220))) { |
| 631 |
return $result; |
| 632 |
} |
| 633 |
if (isset($this->socket_options['ssl']['crypto_method'])) { |
| 634 |
$crypto_method = $this->socket_options['ssl']['crypto_method']; |
| 635 |
} else { |
| 636 |
/* STREAM_CRYPTO_METHOD_TLS_ANY_CLIENT constant does not exist |
| 637 |
* and STREAM_CRYPTO_METHOD_SSLv23_CLIENT constant is |
| 638 |
* inconsistent across PHP versions. */ |
| 639 |
$crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT; |
| 640 |
|
| 641 |
if (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) { |
| 642 |
$crypto_method |= @STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT; |
| 643 |
} |
| 644 |
|
| 645 |
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) { |
| 646 |
$crypto_method |= @STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT; |
| 647 |
} |
| 648 |
|
| 649 |
if (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT')) { |
| 650 |
$crypto_method |= @STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
for ($attempts = 1; $attempts < 15; $attempts++) { |
| 655 |
if(PEAR::isError( |
| 656 |
$result = $this->socket->enableCrypto( |
| 657 |
true, $crypto_method) |
| 658 |
) |
| 659 |
) { |
| 660 |
return $result; |
| 661 |
} |
| 662 |
if ($this->socket->isBlocking() !== true) { |
| 663 |
usleep($attempts); |
| 664 |
} |
| 665 |
if ($result !== 0) { |
| 666 |
break; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if ($result !== true) { |
| 671 |
$last_error = error_get_last(); |
| 672 |
$crypto_types_arr = $this->getDefinedConstantsKeyFilter( |
| 673 |
'STREAM_CRYPTO_METHOD_' |
| 674 |
); |
| 675 |
$error_types_arr = $this->getDefinedConstantsKeyFilter( |
| 676 |
'E_' |
| 677 |
); |
| 678 |
|
| 679 |
$resultErrorString = "STARTTLS failed "; |
| 680 |
//{enableCrypto: false; |
| 681 |
$resultErrorString .= "{enableCrypto: %s; "; |
| 682 |
//crypto_method: STREAM_CRYPTO_METHOD_TLS_CLIENT (3); |
| 683 |
$resultErrorString .= "crypto_method: %s (%s); "; |
| 684 |
//attempts: 1; |
| 685 |
$resultErrorString .= "attempts: %d; "; |
| 686 |
//E_ERROR (1): ErrorMessage} |
| 687 |
$resultErrorString .= "%s (%s): %s}"; |
| 688 |
|
| 689 |
return PEAR::raiseError( |
| 690 |
sprintf( |
| 691 |
$resultErrorString, |
| 692 |
var_export($result, true), |
| 693 |
array_search($crypto_method, $crypto_types_arr), |
| 694 |
var_export($crypto_method, true), |
| 695 |
$attempts, |
| 696 |
array_search($last_error['type'], $error_types_arr), |
| 697 |
$last_error['type'], |
| 698 |
$last_error['message'] |
| 699 |
) |
| 700 |
); |
| 701 |
} |
| 702 |
|
| 703 |
/* Send EHLO again to recieve the AUTH string from the |
| 704 |
* SMTP server. */ |
| 705 |
$this->negotiate(); |
| 706 |
} else { |
| 707 |
return false; |
| 708 |
} |
| 709 |
|
| 710 |
return true; |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Attempt to do SMTP authentication. |
| 715 |
* |
| 716 |
* @param string $uid The userid to authenticate as. |
| 717 |
* @param string $pwd The password to authenticate with. |
| 718 |
* @param string $method The requested authentication method. If none is |
| 719 |
* specified, the best supported method will be used. |
| 720 |
* If you use the special method `OAUTH`, library |
| 721 |
* will choose between OAUTHBEARER or XOAUTH2 |
| 722 |
* according the server's capabilities. |
| 723 |
* @param bool $tls Flag indicating whether or not TLS should be attempted. |
| 724 |
* @param string $authz An optional authorization identifier. If specified, this |
| 725 |
* identifier will be used as the authorization proxy. |
| 726 |
* |
| 727 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 728 |
* kind of failure, or true on success. |
| 729 |
* @since 1.0 |
| 730 |
*/ |
| 731 |
public function auth($uid, $pwd , $method = '', $tls = true, $authz = '') |
| 732 |
{ |
| 733 |
/* We can only attempt a TLS connection if one has been requested, |
| 734 |
* we're running PHP 5.1.0 or later, have access to the OpenSSL |
| 735 |
* extension, are connected to an SMTP server which supports the |
| 736 |
* STARTTLS extension, and aren't already connected over a secure |
| 737 |
* (SSL) socket connection. */ |
| 738 |
if ($tls) { |
| 739 |
/* Start the TLS connection attempt. */ |
| 740 |
if (PEAR::isError($starttls = $this->starttls())) { |
| 741 |
return $starttls; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if (empty($this->esmtp['AUTH'])) { |
| 746 |
return PEAR::raiseError('SMTP server does not support authentication'); |
| 747 |
} |
| 748 |
|
| 749 |
/* If no method has been specified, get the name of the best |
| 750 |
* supported method advertised by the SMTP server. */ |
| 751 |
if (empty($method)) { |
| 752 |
if (PEAR::isError($method = $this->getBestAuthMethod())) { |
| 753 |
/* Return the PEAR_Error object from _getBestAuthMethod(). */ |
| 754 |
return $method; |
| 755 |
} |
| 756 |
} elseif ($method === 'OAUTH') { |
| 757 |
// special case of OAUTH, use the supported method |
| 758 |
$found = false; |
| 759 |
$available_methods = explode(' ', $this->esmtp['AUTH']); |
| 760 |
foreach (['OAUTHBEARER', 'XOAUTH2'] as $method) { |
| 761 |
if (in_array($method, $available_methods)) { |
| 762 |
$found = true; |
| 763 |
break; |
| 764 |
} |
| 765 |
} |
| 766 |
if (!$found) { |
| 767 |
return PEAR::raiseError("neither OAUTHBEARER nor XOAUTH2 is a supported authentication method"); |
| 768 |
} |
| 769 |
} else { |
| 770 |
$method = strtoupper($method); |
| 771 |
if (!array_key_exists($method, $this->auth_methods)) { |
| 772 |
return PEAR::raiseError("$method is not a supported authentication method"); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
if (!isset($this->auth_methods[$method])) { |
| 777 |
return PEAR::raiseError("$method is not a supported authentication method"); |
| 778 |
} |
| 779 |
|
| 780 |
if (!is_callable($this->auth_methods[$method], false)) { |
| 781 |
return PEAR::raiseError("$method authentication method cannot be called"); |
| 782 |
} |
| 783 |
|
| 784 |
if (is_array($this->auth_methods[$method])) { |
| 785 |
list($object, $method) = $this->auth_methods[$method]; |
| 786 |
$result = $object->{$method}($uid, $pwd, $authz, $this); |
| 787 |
} else { |
| 788 |
$func = $this->auth_methods[$method]; |
| 789 |
$result = $func($uid, $pwd, $authz, $this); |
| 790 |
} |
| 791 |
|
| 792 |
/* If an error was encountered, return the PEAR_Error object. */ |
| 793 |
if (PEAR::isError($result)) { |
| 794 |
return $result; |
| 795 |
} |
| 796 |
|
| 797 |
return true; |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
* Add a new authentication method. |
| 802 |
* |
| 803 |
* @param string $name The authentication method name (e.g. 'PLAIN') |
| 804 |
* @param mixed $callback The authentication callback (given as the name of a |
| 805 |
* function or as an (object, method name) array). |
| 806 |
* @param bool $prepend Should the new method be prepended to the list of |
| 807 |
* available methods? This is the default behavior, |
| 808 |
* giving the new method the highest priority. |
| 809 |
* |
| 810 |
* @return mixed True on success or a PEAR_Error object on failure. |
| 811 |
* |
| 812 |
* @since 1.6.0 |
| 813 |
*/ |
| 814 |
public function setAuthMethod($name, $callback, $prepend = true) |
| 815 |
{ |
| 816 |
if (!is_string($name)) { |
| 817 |
return PEAR::raiseError('Method name is not a string'); |
| 818 |
} |
| 819 |
|
| 820 |
if (!is_string($callback) && !is_array($callback)) { |
| 821 |
return PEAR::raiseError('Method callback must be string or array'); |
| 822 |
} |
| 823 |
|
| 824 |
if (is_array($callback)) { |
| 825 |
if (!is_object($callback[0]) || !is_string($callback[1])) { |
| 826 |
return PEAR::raiseError('Bad mMethod callback array'); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
if ($prepend) { |
| 831 |
$this->auth_methods = array_merge( |
| 832 |
array($name => $callback), $this->auth_methods |
| 833 |
); |
| 834 |
} else { |
| 835 |
$this->auth_methods[$name] = $callback; |
| 836 |
} |
| 837 |
|
| 838 |
return true; |
| 839 |
} |
| 840 |
|
| 841 |
/** |
| 842 |
* Authenticates the user using the DIGEST-MD5 method. |
| 843 |
* |
| 844 |
* @param string $uid The userid to authenticate as. |
| 845 |
* @param string $pwd The password to authenticate with. |
| 846 |
* @param string $authz The optional authorization proxy identifier. |
| 847 |
* |
| 848 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 849 |
* kind of failure, or true on success. |
| 850 |
* @since 1.1.0 |
| 851 |
* @deprecated 1.11.0 |
| 852 |
*/ |
| 853 |
protected function authDigestMD5($uid, $pwd, $authz = '') |
| 854 |
{ |
| 855 |
/* TODO trigger deprecation error in 2.0.0 and remove authDigestMD5() in 3.0.0 |
| 856 |
trigger_error(__CLASS__ . ' (' . $this->host . '): Authentication method DIGEST-MD5' . |
| 857 |
' is no longer secure and should be avoided.', E_USER_DEPRECATED); |
| 858 |
*/ |
| 859 |
|
| 860 |
if (PEAR::isError($error = $this->put('AUTH', 'DIGEST-MD5'))) { |
| 861 |
return $error; |
| 862 |
} |
| 863 |
/* 334: Continue authentication request */ |
| 864 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 865 |
/* 503: Error: already authenticated */ |
| 866 |
if ($this->code === 503) { |
| 867 |
return true; |
| 868 |
} |
| 869 |
return $error; |
| 870 |
} |
| 871 |
|
| 872 |
$digest = Auth_SASL::factory('digest-md5'); |
| 873 |
$challenge = base64_decode($this->arguments[0]); |
| 874 |
$auth_str = base64_encode( |
| 875 |
$digest->getResponse($uid, $pwd, $challenge, $this->host, "smtp", $authz) |
| 876 |
); |
| 877 |
|
| 878 |
if (PEAR::isError($error = $this->put($auth_str))) { |
| 879 |
return $error; |
| 880 |
} |
| 881 |
/* 334: Continue authentication request */ |
| 882 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 883 |
return $error; |
| 884 |
} |
| 885 |
|
| 886 |
/* We don't use the protocol's third step because SMTP doesn't |
| 887 |
* allow subsequent authentication, so we just silently ignore |
| 888 |
* it. */ |
| 889 |
if (PEAR::isError($error = $this->put(''))) { |
| 890 |
return $error; |
| 891 |
} |
| 892 |
/* 235: Authentication successful */ |
| 893 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 894 |
return $error; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
/** |
| 899 |
* Authenticates the user using the CRAM-MD5 method. |
| 900 |
* |
| 901 |
* @param string $uid The userid to authenticate as. |
| 902 |
* @param string $pwd The password to authenticate with. |
| 903 |
* @param string $authz The optional authorization proxy identifier. |
| 904 |
* |
| 905 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 906 |
* kind of failure, or true on success. |
| 907 |
* @since 1.1.0 |
| 908 |
* @deprecated 1.11.0 |
| 909 |
*/ |
| 910 |
protected function authCRAMMD5($uid, $pwd, $authz = '') |
| 911 |
{ |
| 912 |
/* TODO trigger deprecation error in 2.0.0 and remove authCRAMMD5() in 3.0.0 |
| 913 |
trigger_error(__CLASS__ . ' (' . $this->host . '): Authentication method CRAM-MD5' . |
| 914 |
' is no longer secure and should be avoided.', E_USER_DEPRECATED); |
| 915 |
*/ |
| 916 |
|
| 917 |
if (PEAR::isError($error = $this->put('AUTH', 'CRAM-MD5'))) { |
| 918 |
return $error; |
| 919 |
} |
| 920 |
/* 334: Continue authentication request */ |
| 921 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 922 |
/* 503: Error: already authenticated */ |
| 923 |
if ($this->code === 503) { |
| 924 |
return true; |
| 925 |
} |
| 926 |
return $error; |
| 927 |
} |
| 928 |
|
| 929 |
$challenge = base64_decode($this->arguments[0]); |
| 930 |
$cram = Auth_SASL::factory('cram-md5'); |
| 931 |
$auth_str = base64_encode($cram->getResponse($uid, $pwd, $challenge)); |
| 932 |
|
| 933 |
if (PEAR::isError($error = $this->put($auth_str))) { |
| 934 |
return $error; |
| 935 |
} |
| 936 |
|
| 937 |
/* 235: Authentication successful */ |
| 938 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 939 |
return $error; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
/** |
| 944 |
* Authenticates the user using the LOGIN method. |
| 945 |
* |
| 946 |
* @param string $uid The userid to authenticate as. |
| 947 |
* @param string $pwd The password to authenticate with. |
| 948 |
* @param string $authz The optional authorization proxy identifier. |
| 949 |
* |
| 950 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 951 |
* kind of failure, or true on success. |
| 952 |
* @since 1.1.0 |
| 953 |
* @deprecated 1.11.0 |
| 954 |
*/ |
| 955 |
protected function authLogin($uid, $pwd, $authz = '') |
| 956 |
{ |
| 957 |
/* TODO trigger deprecation error in 2.0.0 and remove authLogin() in 3.0.0 |
| 958 |
trigger_error(__CLASS__ . ' (' . $this->host . '): Authentication method LOGIN' . |
| 959 |
' is no longer secure and should be avoided.', E_USER_DEPRECATED); |
| 960 |
*/ |
| 961 |
|
| 962 |
if (PEAR::isError($error = $this->put('AUTH', 'LOGIN'))) { |
| 963 |
return $error; |
| 964 |
} |
| 965 |
/* 334: Continue authentication request */ |
| 966 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 967 |
/* 503: Error: already authenticated */ |
| 968 |
if ($this->code === 503) { |
| 969 |
return true; |
| 970 |
} |
| 971 |
return $error; |
| 972 |
} |
| 973 |
|
| 974 |
if (PEAR::isError($error = $this->put(base64_encode($uid)))) { |
| 975 |
return $error; |
| 976 |
} |
| 977 |
/* 334: Continue authentication request */ |
| 978 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 979 |
return $error; |
| 980 |
} |
| 981 |
|
| 982 |
if (PEAR::isError($error = $this->put(base64_encode($pwd)))) { |
| 983 |
return $error; |
| 984 |
} |
| 985 |
|
| 986 |
/* 235: Authentication successful */ |
| 987 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 988 |
return $error; |
| 989 |
} |
| 990 |
|
| 991 |
return true; |
| 992 |
} |
| 993 |
|
| 994 |
/** |
| 995 |
* Authenticates the user using the PLAIN method. |
| 996 |
* |
| 997 |
* @param string $uid The userid to authenticate as. |
| 998 |
* @param string $pwd The password to authenticate with. |
| 999 |
* @param string $authz The optional authorization proxy identifier. |
| 1000 |
* |
| 1001 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1002 |
* kind of failure, or true on success. |
| 1003 |
* @since 1.1.0 |
| 1004 |
*/ |
| 1005 |
protected function authPlain($uid, $pwd, $authz = '') |
| 1006 |
{ |
| 1007 |
if (PEAR::isError($error = $this->put('AUTH', 'PLAIN'))) { |
| 1008 |
return $error; |
| 1009 |
} |
| 1010 |
/* 334: Continue authentication request */ |
| 1011 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1012 |
/* 503: Error: already authenticated */ |
| 1013 |
if ($this->code === 503) { |
| 1014 |
return true; |
| 1015 |
} |
| 1016 |
return $error; |
| 1017 |
} |
| 1018 |
|
| 1019 |
$auth_str = base64_encode($authz . chr(0) . $uid . chr(0) . $pwd); |
| 1020 |
|
| 1021 |
if (PEAR::isError($error = $this->put($auth_str))) { |
| 1022 |
return $error; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* 235: Authentication successful */ |
| 1026 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 1027 |
return $error; |
| 1028 |
} |
| 1029 |
|
| 1030 |
return true; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/** |
| 1034 |
* Authenticates the user using the GSSAPI method. |
| 1035 |
* |
| 1036 |
* PHP krb5 extension is required, |
| 1037 |
* service principal and credentials cache must be set. |
| 1038 |
* |
| 1039 |
* @param string $uid The userid to authenticate as. |
| 1040 |
* @param string $pwd The password to authenticate with. |
| 1041 |
* @param string $authz The optional authorization proxy identifier. |
| 1042 |
* |
| 1043 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1044 |
* kind of failure, or true on success. |
| 1045 |
*/ |
| 1046 |
protected function authGSSAPI($uid, $pwd, $authz = '') |
| 1047 |
{ |
| 1048 |
if (PEAR::isError($error = $this->put('AUTH', 'GSSAPI'))) { |
| 1049 |
return $error; |
| 1050 |
} |
| 1051 |
/* 334: Continue authentication request */ |
| 1052 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1053 |
/* 503: Error: already authenticated */ |
| 1054 |
if ($this->code === 503) { |
| 1055 |
return true; |
| 1056 |
} |
| 1057 |
return $error; |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (!$this->gssapi_principal) { |
| 1061 |
return PEAR::raiseError('No Kerberos service principal set', 2); |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (!empty($this->gssapi_cname)) { |
| 1065 |
putenv('KRB5CCNAME=' . $this->gssapi_cname); |
| 1066 |
} |
| 1067 |
|
| 1068 |
try { |
| 1069 |
$ccache = new KRB5CCache(); |
| 1070 |
if (!empty($this->gssapi_cname)) { |
| 1071 |
$ccache->open($this->gssapi_cname); |
| 1072 |
} |
| 1073 |
|
| 1074 |
$gssapicontext = new GSSAPIContext(); |
| 1075 |
$gssapicontext->acquireCredentials($ccache); |
| 1076 |
|
| 1077 |
$token = ''; |
| 1078 |
$success = $gssapicontext->initSecContext($this->gssapi_principal, null, null, null, $token); |
| 1079 |
$token = base64_encode($token); |
| 1080 |
} |
| 1081 |
catch (Exception $e) { |
| 1082 |
return PEAR::raiseError('GSSAPI authentication failed: ' . $e->getMessage()); |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (PEAR::isError($error = $this->put($token))) { |
| 1086 |
return $error; |
| 1087 |
} |
| 1088 |
|
| 1089 |
/* 334: Continue authentication request */ |
| 1090 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1091 |
return $error; |
| 1092 |
} |
| 1093 |
|
| 1094 |
$response = $this->arguments[0]; |
| 1095 |
|
| 1096 |
try { |
| 1097 |
$challenge = base64_decode($response); |
| 1098 |
$gssapicontext->unwrap($challenge, $challenge); |
| 1099 |
$gssapicontext->wrap($challenge, $challenge, true); |
| 1100 |
} |
| 1101 |
catch (Exception $e) { |
| 1102 |
return PEAR::raiseError('GSSAPI authentication failed: ' . $e->getMessage()); |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (PEAR::isError($error = $this->put(base64_encode($challenge)))) { |
| 1106 |
return $error; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* 235: Authentication successful */ |
| 1110 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 1111 |
return $error; |
| 1112 |
} |
| 1113 |
|
| 1114 |
return true; |
| 1115 |
} |
| 1116 |
|
| 1117 |
/** |
| 1118 |
* Authenticates the user using the XOAUTH2 method. |
| 1119 |
* |
| 1120 |
* @param string $uid The userid to authenticate as. |
| 1121 |
* @param string $token The access token prefixed by it's type |
| 1122 |
* example: "Bearer $access_token". |
| 1123 |
* @param string $authz The optional authorization proxy identifier. |
| 1124 |
* @param object $conn The current object |
| 1125 |
* |
| 1126 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1127 |
* kind of failure, or true on success. |
| 1128 |
* @since 1.9.0 |
| 1129 |
*/ |
| 1130 |
//FIXME: to switch into protected method on next major release |
| 1131 |
public function authXOAuth2($uid, $token, $authz, $conn) |
| 1132 |
{ |
| 1133 |
$auth = base64_encode("user=$uid\1auth=$token\1\1"); |
| 1134 |
return $this->authenticateOAuth('XOAUTH2', $auth, $authz, $conn); |
| 1135 |
} |
| 1136 |
|
| 1137 |
/** |
| 1138 |
* Authenticates the user using the OAUTHBEARER method. |
| 1139 |
* |
| 1140 |
* @param string $uid The userid to authenticate as. |
| 1141 |
* @param string $token The access token prefixed by it's type |
| 1142 |
* example: "Bearer $access_token". |
| 1143 |
* @param string $authz The optional authorization proxy identifier. |
| 1144 |
* @param object $conn The current object |
| 1145 |
* |
| 1146 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1147 |
* kind of failure, or true on success. |
| 1148 |
* @since 1.9.3 |
| 1149 |
* @see https://www.rfc-editor.org/rfc/rfc7628.html |
| 1150 |
*/ |
| 1151 |
protected function authOAuthBearer($uid, $token, $authz, $conn) |
| 1152 |
{ |
| 1153 |
$auth = base64_encode("n,a=$uid\1auth=$token\1\1"); |
| 1154 |
return $this->authenticateOAuth('OAUTHBEARER', $auth, $authz, $conn); |
| 1155 |
} |
| 1156 |
|
| 1157 |
/** |
| 1158 |
* Authenticates the user using the OAUTHBEARER or XOAUTH2 method. |
| 1159 |
* |
| 1160 |
* @param string $method The method (OAUTHBEARER or XOAUTH2) |
| 1161 |
* @param string $auth The authentication string (base64 coded) |
| 1162 |
* @param string $authz The optional authorization proxy identifier. |
| 1163 |
* @param object $conn The current object |
| 1164 |
* |
| 1165 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1166 |
* kind of failure, or true on success. |
| 1167 |
*/ |
| 1168 |
protected function authenticateOAuth( $method, $auth, $authz, $conn) |
| 1169 |
{ |
| 1170 |
// Maximum length of the base64-encoded token to be sent in the initial response is 504 - strlen($method) bytes, |
| 1171 |
// according to RFC 4954 (https://datatracker.ietf.org/doc/html/rfc4954); for longer tokens an empty initial |
| 1172 |
// response MUST be sent and the token must be sent separately |
| 1173 |
// (504 bytes = /SMTP command length limit/ - 6 bytes /"AUTH "/ -strlen($method) - 1 byte /" "/ - 2 bytes /CRLF/) |
| 1174 |
if (strlen($auth) <= (504-strlen($method))) { |
| 1175 |
if (PEAR::isError($error = $this->put('AUTH', $method . ' ' . $auth))) { |
| 1176 |
return $error; |
| 1177 |
} |
| 1178 |
} else { |
| 1179 |
if (PEAR::isError($error = $this->put('AUTH', $method))) { |
| 1180 |
return $error; |
| 1181 |
} |
| 1182 |
|
| 1183 |
// server is expected to respond with 334 |
| 1184 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1185 |
return $error; |
| 1186 |
} |
| 1187 |
|
| 1188 |
// then follows the token |
| 1189 |
if (PEAR::isError($error = $this->put($auth))) { |
| 1190 |
return $error; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
/* 235: Authentication successful or 334: Continue authentication */ |
| 1195 |
if (PEAR::isError($error = $this->parseResponse([235, 334]))) { |
| 1196 |
return $error; |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* 334: Continue authentication request */ |
| 1200 |
if ($this->code === 334) { |
| 1201 |
/* Send an empty line as response to 334 */ |
| 1202 |
if (PEAR::isError($error = $this->put(''))) { |
| 1203 |
return $error; |
| 1204 |
} |
| 1205 |
|
| 1206 |
/* Expect 235: Authentication successful */ |
| 1207 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 1208 |
return $error; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
return true; |
| 1213 |
} |
| 1214 |
|
| 1215 |
/** |
| 1216 |
* Authenticates the user using the SCRAM-SHA-1 method. |
| 1217 |
* |
| 1218 |
* @param string $uid The userid to authenticate as. |
| 1219 |
* @param string $pwd The password to authenticate with. |
| 1220 |
* @param string $authz The optional authorization proxy identifier. |
| 1221 |
* |
| 1222 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1223 |
* kind of failure, or true on success. |
| 1224 |
* @since 1.11.0 |
| 1225 |
*/ |
| 1226 |
protected function authScramSHA1($uid, $pwd, $authz = '') |
| 1227 |
{ |
| 1228 |
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-1'; |
| 1229 |
return $this->authScramSHA($uid, $pwd, $authz); |
| 1230 |
} |
| 1231 |
|
| 1232 |
/** |
| 1233 |
* Authenticates the user using the SCRAM-SHA-224 method. |
| 1234 |
* |
| 1235 |
* @param string $uid The userid to authenticate as. |
| 1236 |
* @param string $pwd The password to authenticate with. |
| 1237 |
* @param string $authz The optional authorization proxy identifier. |
| 1238 |
* |
| 1239 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1240 |
* kind of failure, or true on success. |
| 1241 |
* @since 1.11.0 |
| 1242 |
*/ |
| 1243 |
protected function authScramSHA224($uid, $pwd, $authz = '') |
| 1244 |
{ |
| 1245 |
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-224'; |
| 1246 |
return $this->authScramSHA($uid, $pwd, $authz); |
| 1247 |
} |
| 1248 |
|
| 1249 |
/** |
| 1250 |
* Authenticates the user using the SCRAM-SHA-256 method. |
| 1251 |
* |
| 1252 |
* @param string $uid The userid to authenticate as. |
| 1253 |
* @param string $pwd The password to authenticate with. |
| 1254 |
* @param string $authz The optional authorization proxy identifier. |
| 1255 |
* |
| 1256 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1257 |
* kind of failure, or true on success. |
| 1258 |
* @since 1.11.0 |
| 1259 |
*/ |
| 1260 |
protected function authScramSHA256($uid, $pwd, $authz = '') |
| 1261 |
{ |
| 1262 |
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-256'; |
| 1263 |
return $this->authScramSHA($uid, $pwd, $authz); |
| 1264 |
} |
| 1265 |
|
| 1266 |
/** |
| 1267 |
* Authenticates the user using the SCRAM-SHA-384 method. |
| 1268 |
* |
| 1269 |
* @param string $uid The userid to authenticate as. |
| 1270 |
* @param string $pwd The password to authenticate with. |
| 1271 |
* @param string $authz The optional authorization proxy identifier. |
| 1272 |
* |
| 1273 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1274 |
* kind of failure, or true on success. |
| 1275 |
* @since 1.11.0 |
| 1276 |
*/ |
| 1277 |
protected function authScramSHA384($uid, $pwd, $authz = '') |
| 1278 |
{ |
| 1279 |
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-384'; |
| 1280 |
return $this->authScramSHA($uid, $pwd, $authz); |
| 1281 |
} |
| 1282 |
|
| 1283 |
/** |
| 1284 |
* Authenticates the user using the SCRAM-SHA-512 method. |
| 1285 |
* |
| 1286 |
* @param string $uid The userid to authenticate as. |
| 1287 |
* @param string $pwd The password to authenticate with. |
| 1288 |
* @param string $authz The optional authorization proxy identifier. |
| 1289 |
* |
| 1290 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1291 |
* kind of failure, or true on success. |
| 1292 |
* @since 1.11.0 |
| 1293 |
*/ |
| 1294 |
protected function authScramSHA512($uid, $pwd, $authz = '') |
| 1295 |
{ |
| 1296 |
$this->scram_sha_hash_algorithm = 'SCRAM-SHA-512'; |
| 1297 |
return $this->authScramSHA($uid, $pwd, $authz); |
| 1298 |
} |
| 1299 |
|
| 1300 |
/** |
| 1301 |
* Authenticates the user using the SCRAM-SHA method. |
| 1302 |
* |
| 1303 |
* @param string $uid The userid to authenticate as. |
| 1304 |
* @param string $pwd The password to authenticate with. |
| 1305 |
* @param string $authz The optional authorization proxy identifier. |
| 1306 |
* |
| 1307 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1308 |
* kind of failure, or true on success. |
| 1309 |
* @since 1.11.0 |
| 1310 |
*/ |
| 1311 |
protected function authScramSHA($uid, $pwd, $authz = '') |
| 1312 |
{ |
| 1313 |
if (PEAR::isError($error = $this->put('AUTH', $this->scram_sha_hash_algorithm))) { |
| 1314 |
return $error; |
| 1315 |
} |
| 1316 |
/* 334: Continue authentication request */ |
| 1317 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1318 |
/* 503: Error: already authenticated */ |
| 1319 |
if ($this->code === 503) { |
| 1320 |
return true; |
| 1321 |
} |
| 1322 |
return $error; |
| 1323 |
} |
| 1324 |
|
| 1325 |
$cram = Auth_SASL::factory($this->scram_sha_hash_algorithm); |
| 1326 |
$auth_str = base64_encode($cram->getResponse($uid, $pwd)); |
| 1327 |
|
| 1328 |
/* Step 1: Send first authentication request */ |
| 1329 |
if (PEAR::isError($error = $this->put($auth_str))) { |
| 1330 |
return $error; |
| 1331 |
} |
| 1332 |
|
| 1333 |
/* 334: Continue authentication request with password salt */ |
| 1334 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1335 |
return $error; |
| 1336 |
} |
| 1337 |
|
| 1338 |
$challenge = base64_decode($this->arguments[0]); |
| 1339 |
$auth_str = base64_encode($cram->getResponse($uid, $pwd, $challenge)); |
| 1340 |
|
| 1341 |
/* Step 2: Send salted authentication request */ |
| 1342 |
if (PEAR::isError($error = $this->put($auth_str))) { |
| 1343 |
return $error; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* 334: Continue authentication request with password salt */ |
| 1347 |
if (PEAR::isError($error = $this->parseResponse(334))) { |
| 1348 |
return $error; |
| 1349 |
} |
| 1350 |
|
| 1351 |
/* Verify server signature */ |
| 1352 |
$verification = $cram->processOutcome(base64_decode($this->arguments[0])); |
| 1353 |
if ($verification == false) { |
| 1354 |
return PEAR::raiseError("SCRAM Server verification on step 3 not successful"); |
| 1355 |
} |
| 1356 |
|
| 1357 |
/* Step 3: Send a request to acknowledge verification */ |
| 1358 |
if (PEAR::isError($error = $this->put("NOOP"))) { |
| 1359 |
return $error; |
| 1360 |
} |
| 1361 |
|
| 1362 |
/* 235: Authentication successful */ |
| 1363 |
if (PEAR::isError($error = $this->parseResponse(235))) { |
| 1364 |
return $error; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
/** |
| 1369 |
* Send the HELO command. |
| 1370 |
* |
| 1371 |
* @param string $domain The domain name to say we are. |
| 1372 |
* |
| 1373 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1374 |
* kind of failure, or true on success. |
| 1375 |
* @since 1.0 |
| 1376 |
*/ |
| 1377 |
public function helo($domain) |
| 1378 |
{ |
| 1379 |
if (PEAR::isError($error = $this->put('HELO', $domain))) { |
| 1380 |
return $error; |
| 1381 |
} |
| 1382 |
if (PEAR::isError($error = $this->parseResponse(250))) { |
| 1383 |
return $error; |
| 1384 |
} |
| 1385 |
|
| 1386 |
return true; |
| 1387 |
} |
| 1388 |
|
| 1389 |
/** |
| 1390 |
* Return the list of SMTP service extensions advertised by the server. |
| 1391 |
* |
| 1392 |
* @return array The list of SMTP service extensions. |
| 1393 |
* @since 1.3 |
| 1394 |
*/ |
| 1395 |
public function getServiceExtensions() |
| 1396 |
{ |
| 1397 |
return $this->esmtp; |
| 1398 |
} |
| 1399 |
|
| 1400 |
/** |
| 1401 |
* Send the MAIL FROM: command. |
| 1402 |
* |
| 1403 |
* @param string $sender The sender (reverse path) to set. |
| 1404 |
* @param string $params String containing additional MAIL parameters, |
| 1405 |
* such as the NOTIFY flags defined by RFC 1891 |
| 1406 |
* or the VERP protocol. |
| 1407 |
* |
| 1408 |
* If $params is an array, only the 'verp' option |
| 1409 |
* is supported. If 'verp' is true, the XVERP |
| 1410 |
* parameter is appended to the MAIL command. |
| 1411 |
* If the 'verp' value is a string, the full |
| 1412 |
* XVERP=value parameter is appended. |
| 1413 |
* |
| 1414 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1415 |
* kind of failure, or true on success. |
| 1416 |
* @since 1.0 |
| 1417 |
*/ |
| 1418 |
public function mailFrom($sender, $params = null) |
| 1419 |
{ |
| 1420 |
$args = "FROM:<$sender>"; |
| 1421 |
|
| 1422 |
/* Support the deprecated array form of $params. */ |
| 1423 |
if (is_array($params) && isset($params['verp'])) { |
| 1424 |
if ($params['verp'] === true) { |
| 1425 |
$args .= ' XVERP'; |
| 1426 |
} elseif (trim($params['verp'])) { |
| 1427 |
$args .= ' XVERP=' . $params['verp']; |
| 1428 |
} |
| 1429 |
} elseif (is_string($params) && !empty($params)) { |
| 1430 |
$args .= ' ' . $params; |
| 1431 |
} |
| 1432 |
|
| 1433 |
if (PEAR::isError($error = $this->put('MAIL', $args))) { |
| 1434 |
return $error; |
| 1435 |
} |
| 1436 |
if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) { |
| 1437 |
return $error; |
| 1438 |
} |
| 1439 |
|
| 1440 |
return true; |
| 1441 |
} |
| 1442 |
|
| 1443 |
/** |
| 1444 |
* Send the RCPT TO: command. |
| 1445 |
* |
| 1446 |
* @param string $recipient The recipient (forward path) to add. |
| 1447 |
* @param string $params String containing additional RCPT parameters, |
| 1448 |
* such as the NOTIFY flags defined by RFC 1891. |
| 1449 |
* |
| 1450 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1451 |
* kind of failure, or true on success. |
| 1452 |
* |
| 1453 |
* @since 1.0 |
| 1454 |
*/ |
| 1455 |
public function rcptTo($recipient, $params = null) |
| 1456 |
{ |
| 1457 |
$args = "TO:<$recipient>"; |
| 1458 |
if (is_string($params)) { |
| 1459 |
$args .= ' ' . $params; |
| 1460 |
} |
| 1461 |
|
| 1462 |
if (PEAR::isError($error = $this->put('RCPT', $args))) { |
| 1463 |
return $error; |
| 1464 |
} |
| 1465 |
if (PEAR::isError($error = $this->parseResponse(array(250, 251), $this->pipelining))) { |
| 1466 |
return $error; |
| 1467 |
} |
| 1468 |
|
| 1469 |
return true; |
| 1470 |
} |
| 1471 |
|
| 1472 |
/** |
| 1473 |
* Quote the data so that it meets SMTP standards. |
| 1474 |
* |
| 1475 |
* This is provided as a separate public function to facilitate |
| 1476 |
* easier overloading for the cases where it is desirable to |
| 1477 |
* customize the quoting behavior. |
| 1478 |
* |
| 1479 |
* @param string &$data The message text to quote. The string must be passed |
| 1480 |
* by reference, and the text will be modified in place. |
| 1481 |
* |
| 1482 |
* @since 1.2 |
| 1483 |
*/ |
| 1484 |
public function quotedata(&$data) |
| 1485 |
{ |
| 1486 |
/* Because a single leading period (.) signifies an end to the |
| 1487 |
* data, legitimate leading periods need to be "doubled" ('..'). */ |
| 1488 |
$data = preg_replace('/^\./m', '..', $data); |
| 1489 |
|
| 1490 |
/* Change Unix (\n) and Mac (\r) linefeeds into CRLF's (\r\n). */ |
| 1491 |
$data = preg_replace('/(?:\r\n|\n|\r(?!\n))/', "\r\n", $data); |
| 1492 |
} |
| 1493 |
|
| 1494 |
/** |
| 1495 |
* Send the DATA command. |
| 1496 |
* |
| 1497 |
* @param mixed $data The message data, either as a string or an open |
| 1498 |
* file resource. |
| 1499 |
* @param string $headers The message headers. If $headers is provided, |
| 1500 |
* $data is assumed to contain only body data. |
| 1501 |
* |
| 1502 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1503 |
* kind of failure, or true on success. |
| 1504 |
* @since 1.0 |
| 1505 |
*/ |
| 1506 |
public function data($data, $headers = null) |
| 1507 |
{ |
| 1508 |
/* Verify that $data is a supported type. */ |
| 1509 |
if (!is_string($data) && !is_resource($data)) { |
| 1510 |
return PEAR::raiseError('Expected a string or file resource'); |
| 1511 |
} |
| 1512 |
|
| 1513 |
/* Start by considering the size of the optional headers string. We |
| 1514 |
* also account for the addition 4 character "\r\n\r\n" separator |
| 1515 |
* sequence. */ |
| 1516 |
$size = $headers_size = (is_null($headers)) ? 0 : strlen($headers) + 4; |
| 1517 |
|
| 1518 |
if (is_resource($data)) { |
| 1519 |
$stat = fstat($data); |
| 1520 |
if ($stat === false) { |
| 1521 |
return PEAR::raiseError('Failed to get file size'); |
| 1522 |
} |
| 1523 |
$size += $stat['size']; |
| 1524 |
} else { |
| 1525 |
$size += strlen($data); |
| 1526 |
} |
| 1527 |
|
| 1528 |
/* RFC 1870, section 3, subsection 3 states "a value of zero indicates |
| 1529 |
* that no fixed maximum message size is in force". Furthermore, it |
| 1530 |
* says that if "the parameter is omitted no information is conveyed |
| 1531 |
* about the server's fixed maximum message size". */ |
| 1532 |
$limit = (isset($this->esmtp['SIZE'])) ? $this->esmtp['SIZE'] : 0; |
| 1533 |
if ($limit > 0 && $size >= $limit) { |
| 1534 |
return PEAR::raiseError('Message size exceeds server limit'); |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* Initiate the DATA command. */ |
| 1538 |
if (PEAR::isError($error = $this->put('DATA'))) { |
| 1539 |
return $error; |
| 1540 |
} |
| 1541 |
if (PEAR::isError($error = $this->parseResponse(354))) { |
| 1542 |
return $error; |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* If we have a separate headers string, send it first. */ |
| 1546 |
if (!is_null($headers)) { |
| 1547 |
$this->quotedata($headers); |
| 1548 |
if (PEAR::isError($result = $this->send($headers . "\r\n\r\n"))) { |
| 1549 |
return $result; |
| 1550 |
} |
| 1551 |
|
| 1552 |
/* Subtract the headers size now that they've been sent. */ |
| 1553 |
$size -= $headers_size; |
| 1554 |
} |
| 1555 |
|
| 1556 |
/* Now we can send the message body data. */ |
| 1557 |
if (is_resource($data)) { |
| 1558 |
/* Stream the contents of the file resource out over our socket |
| 1559 |
* connection, line by line. Each line must be run through the |
| 1560 |
* quoting routine. */ |
| 1561 |
while (strlen($line = fread($data, 8192)) > 0) { |
| 1562 |
/* If the last character is an newline, we need to grab the |
| 1563 |
* next character to check to see if it is a period. */ |
| 1564 |
while (!feof($data)) { |
| 1565 |
$char = fread($data, 1); |
| 1566 |
$line .= $char; |
| 1567 |
if ($char != "\n") { |
| 1568 |
break; |
| 1569 |
} |
| 1570 |
} |
| 1571 |
$this->quotedata($line); |
| 1572 |
if (PEAR::isError($result = $this->send($line))) { |
| 1573 |
return $result; |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
$last = $line; |
| 1578 |
} else { |
| 1579 |
/* |
| 1580 |
* Break up the data by sending one chunk (up to 512k) at a time. |
| 1581 |
* This approach reduces our peak memory usage. |
| 1582 |
*/ |
| 1583 |
for ($offset = 0; $offset < $size;) { |
| 1584 |
$end = $offset + 512000; |
| 1585 |
|
| 1586 |
/* |
| 1587 |
* Ensure we don't read beyond our data size or span multiple |
| 1588 |
* lines. quotedata() can't properly handle character data |
| 1589 |
* that's split across two line break boundaries. |
| 1590 |
*/ |
| 1591 |
if ($end >= $size) { |
| 1592 |
$end = $size; |
| 1593 |
} else { |
| 1594 |
for (; $end < $size; $end++) { |
| 1595 |
if ($data[$end] != "\n") { |
| 1596 |
break; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
/* Extract our chunk and run it through the quoting routine. */ |
| 1602 |
$chunk = substr($data, $offset, $end - $offset); |
| 1603 |
$this->quotedata($chunk); |
| 1604 |
|
| 1605 |
/* If we run into a problem along the way, abort. */ |
| 1606 |
if (PEAR::isError($result = $this->send($chunk))) { |
| 1607 |
return $result; |
| 1608 |
} |
| 1609 |
|
| 1610 |
/* Advance the offset to the end of this chunk. */ |
| 1611 |
$offset = $end; |
| 1612 |
} |
| 1613 |
|
| 1614 |
$last = $chunk; |
| 1615 |
} |
| 1616 |
|
| 1617 |
/* Don't add another CRLF sequence if it's already in the data */ |
| 1618 |
$terminator = (substr($last, -2) == "\r\n" ? '' : "\r\n") . ".\r\n"; |
| 1619 |
|
| 1620 |
/* Finally, send the DATA terminator sequence. */ |
| 1621 |
if (PEAR::isError($result = $this->send($terminator))) { |
| 1622 |
return $result; |
| 1623 |
} |
| 1624 |
|
| 1625 |
/* Verify that the data was successfully received by the server. */ |
| 1626 |
if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) { |
| 1627 |
return $error; |
| 1628 |
} |
| 1629 |
|
| 1630 |
return true; |
| 1631 |
} |
| 1632 |
|
| 1633 |
/** |
| 1634 |
* Send the SEND FROM: command. |
| 1635 |
* |
| 1636 |
* @param string $path The reverse path to send. |
| 1637 |
* |
| 1638 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1639 |
* kind of failure, or true on success. |
| 1640 |
* @since 1.2.6 |
| 1641 |
*/ |
| 1642 |
public function sendFrom($path) |
| 1643 |
{ |
| 1644 |
if (PEAR::isError($error = $this->put('SEND', "FROM:<$path>"))) { |
| 1645 |
return $error; |
| 1646 |
} |
| 1647 |
if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) { |
| 1648 |
return $error; |
| 1649 |
} |
| 1650 |
|
| 1651 |
return true; |
| 1652 |
} |
| 1653 |
|
| 1654 |
/** |
| 1655 |
* Send the SOML FROM: command. |
| 1656 |
* |
| 1657 |
* @param string $path The reverse path to send. |
| 1658 |
* |
| 1659 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1660 |
* kind of failure, or true on success. |
| 1661 |
* @since 1.2.6 |
| 1662 |
*/ |
| 1663 |
public function somlFrom($path) |
| 1664 |
{ |
| 1665 |
if (PEAR::isError($error = $this->put('SOML', "FROM:<$path>"))) { |
| 1666 |
return $error; |
| 1667 |
} |
| 1668 |
if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) { |
| 1669 |
return $error; |
| 1670 |
} |
| 1671 |
|
| 1672 |
return true; |
| 1673 |
} |
| 1674 |
|
| 1675 |
/** |
| 1676 |
* Send the SAML FROM: command. |
| 1677 |
* |
| 1678 |
* @param string $path The reverse path to send. |
| 1679 |
* |
| 1680 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1681 |
* kind of failure, or true on success. |
| 1682 |
* @since 1.2.6 |
| 1683 |
*/ |
| 1684 |
public function samlFrom($path) |
| 1685 |
{ |
| 1686 |
if (PEAR::isError($error = $this->put('SAML', "FROM:<$path>"))) { |
| 1687 |
return $error; |
| 1688 |
} |
| 1689 |
if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) { |
| 1690 |
return $error; |
| 1691 |
} |
| 1692 |
|
| 1693 |
return true; |
| 1694 |
} |
| 1695 |
|
| 1696 |
/** |
| 1697 |
* Send the RSET command. |
| 1698 |
* |
| 1699 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1700 |
* kind of failure, or true on success. |
| 1701 |
* @since 1.0 |
| 1702 |
*/ |
| 1703 |
public function rset() |
| 1704 |
{ |
| 1705 |
if (PEAR::isError($error = $this->put('RSET'))) { |
| 1706 |
return $error; |
| 1707 |
} |
| 1708 |
if (PEAR::isError($error = $this->parseResponse(250, $this->pipelining))) { |
| 1709 |
return $error; |
| 1710 |
} |
| 1711 |
|
| 1712 |
return true; |
| 1713 |
} |
| 1714 |
|
| 1715 |
/** |
| 1716 |
* Send the VRFY command. |
| 1717 |
* |
| 1718 |
* @param string $string The string to verify |
| 1719 |
* |
| 1720 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1721 |
* kind of failure, or true on success. |
| 1722 |
* @since 1.0 |
| 1723 |
*/ |
| 1724 |
public function vrfy($string) |
| 1725 |
{ |
| 1726 |
/* Note: 251 is also a valid response code */ |
| 1727 |
if (PEAR::isError($error = $this->put('VRFY', $string))) { |
| 1728 |
return $error; |
| 1729 |
} |
| 1730 |
if (PEAR::isError($error = $this->parseResponse(array(250, 252)))) { |
| 1731 |
return $error; |
| 1732 |
} |
| 1733 |
|
| 1734 |
return true; |
| 1735 |
} |
| 1736 |
|
| 1737 |
/** |
| 1738 |
* Send the NOOP command. |
| 1739 |
* |
| 1740 |
* @return mixed Returns a PEAR_Error with an error message on any |
| 1741 |
* kind of failure, or true on success. |
| 1742 |
* @since 1.0 |
| 1743 |
*/ |
| 1744 |
public function noop() |
| 1745 |
{ |
| 1746 |
if (PEAR::isError($error = $this->put('NOOP'))) { |
| 1747 |
return $error; |
| 1748 |
} |
| 1749 |
if (PEAR::isError($error = $this->parseResponse(250))) { |
| 1750 |
return $error; |
| 1751 |
} |
| 1752 |
|
| 1753 |
return true; |
| 1754 |
} |
| 1755 |
|
| 1756 |
/** |
| 1757 |
* Backwards-compatibility method. identifySender()'s functionality is |
| 1758 |
* now handled internally. |
| 1759 |
* |
| 1760 |
* @return boolean This method always return true. |
| 1761 |
* |
| 1762 |
* @since 1.0 |
| 1763 |
*/ |
| 1764 |
public function identifySender() |
| 1765 |
{ |
| 1766 |
return true; |
| 1767 |
} |
| 1768 |
|
| 1769 |
/** |
| 1770 |
* Backwards-compatibility method. |
| 1771 |
* array_filter alternative in PHP5.4 for using |
| 1772 |
* key filter because array_filter mode parameter |
| 1773 |
* is only available since PHP5.6. |
| 1774 |
* |
| 1775 |
* @param string $filter The string to filter |
| 1776 |
* @return array Filtered constants array. |
| 1777 |
*/ |
| 1778 |
private function getDefinedConstantsKeyFilter($filter) { |
| 1779 |
$constants_filtered = array(); |
| 1780 |
$filter_length = strlen($filter); |
| 1781 |
$constants = get_defined_constants(); |
| 1782 |
foreach ($constants as $key=>$value){ |
| 1783 |
if (substr($key, 0, $filter_length) == $filter) { |
| 1784 |
$constants_filtered[$key] = $value; |
| 1785 |
} |
| 1786 |
} |
| 1787 |
return $constants_filtered; |
| 1788 |
} |
| 1789 |
} |