"Fossies" - the Fresh Open Source Software Archive

Member "icingaweb2-2.11.4/library/vendor/Zend/Mail/Protocol/Abstract.php" (26 Jan 2023, 10545 Bytes) of package /linux/www/icingaweb2-2.11.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 /**
    4  * Zend Framework
    5  *
    6  * LICENSE
    7  *
    8  * This source file is subject to the new BSD license that is bundled
    9  * with this package in the file LICENSE.txt.
   10  * It is also available through the world-wide-web at this URL:
   11  * http://framework.zend.com/license/new-bsd
   12  * If you did not receive a copy of the license and are unable to
   13  * obtain it through the world-wide-web, please send an email
   14  * to license@zend.com so we can send you a copy immediately.
   15  *
   16  * @category   Zend
   17  * @package    Zend_Mail
   18  * @subpackage Protocol
   19  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   21  * @version    $Id$
   22  */
   23 
   24 
   25 /**
   26  * @see Zend_Validate
   27  */
   28 
   29 
   30 /**
   31  * @see Zend_Validate_Hostname
   32  */
   33 
   34 
   35 /**
   36  * Zend_Mail_Protocol_Abstract
   37  *
   38  * Provides low-level methods for concrete adapters to communicate with a remote mail server and track requests and responses.
   39  *
   40  * @category   Zend
   41  * @package    Zend_Mail
   42  * @subpackage Protocol
   43  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   44  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   45  * @version    $Id$
   46  * @todo Implement proxy settings
   47  */
   48 abstract class Zend_Mail_Protocol_Abstract
   49 {
   50     /**
   51      * Mail default EOL string
   52      */
   53     const EOL = "\r\n";
   54 
   55 
   56     /**
   57      * Default timeout in seconds for initiating session
   58      */
   59     const TIMEOUT_CONNECTION = 30;
   60 
   61     /**
   62      * Maximum of the transaction log
   63      * @var integer
   64      */
   65     protected $_maximumLog = 64;
   66 
   67 
   68     /**
   69      * Hostname or IP address of remote server
   70      * @var string
   71      */
   72     protected $_host;
   73 
   74 
   75     /**
   76      * Port number of connection
   77      * @var integer
   78      */
   79     protected $_port;
   80 
   81 
   82     /**
   83      * Instance of Zend_Validate to check hostnames
   84      * @var Zend_Validate
   85      */
   86     protected $_validHost;
   87 
   88 
   89     /**
   90      * Socket connection resource
   91      * @var resource
   92      */
   93     protected $_socket;
   94 
   95 
   96     /**
   97      * Last request sent to server
   98      * @var string
   99      */
  100     protected $_request;
  101 
  102 
  103     /**
  104      * Array of server responses to last request
  105      * @var array
  106      */
  107     protected $_response;
  108 
  109 
  110     /**
  111      * String template for parsing server responses using sscanf (default: 3 digit code and response string)
  112      * @var resource
  113      * @deprecated Since 1.10.3
  114      */
  115     protected $_template = '%d%s';
  116 
  117 
  118     /**
  119      * Log of mail requests and server responses for a session
  120      * @var array
  121      */
  122     private $_log = array();
  123 
  124 
  125     /**
  126      * Constructor.
  127      *
  128      * @param  string  $host OPTIONAL Hostname of remote connection (default: 127.0.0.1)
  129      * @param  integer $port OPTIONAL Port number (default: null)
  130      * @throws Zend_Mail_Protocol_Exception
  131      * @return void
  132      */
  133     public function __construct($host = '127.0.0.1', $port = null)
  134     {
  135         $this->_validHost = new Zend_Validate();
  136         $this->_validHost->addValidator(new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL));
  137 
  138         if (!$this->_validHost->isValid($host)) {
  139             /**
  140              * @see Zend_Mail_Protocol_Exception
  141              */
  142             throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
  143         }
  144 
  145         $this->_host = $host;
  146         $this->_port = $port;
  147     }
  148 
  149 
  150     /**
  151      * Class destructor to cleanup open resources
  152      *
  153      * @return void
  154      */
  155     public function __destruct()
  156     {
  157         $this->_disconnect();
  158     }
  159 
  160     /**
  161      * Set the maximum log size
  162      *
  163      * @param integer $maximumLog Maximum log size
  164      * @return void
  165      */
  166     public function setMaximumLog($maximumLog)
  167     {
  168         $this->_maximumLog = (int) $maximumLog;
  169     }
  170 
  171 
  172     /**
  173      * Get the maximum log size
  174      *
  175      * @return int the maximum log size
  176      */
  177     public function getMaximumLog()
  178     {
  179         return $this->_maximumLog;
  180     }
  181 
  182 
  183     /**
  184      * Create a connection to the remote host
  185      *
  186      * Concrete adapters for this class will implement their own unique connect scripts, using the _connect() method to create the socket resource.
  187      */
  188     abstract public function connect();
  189 
  190 
  191     /**
  192      * Retrieve the last client request
  193      *
  194      * @return string
  195      */
  196     public function getRequest()
  197     {
  198         return $this->_request;
  199     }
  200 
  201 
  202     /**
  203      * Retrieve the last server response
  204      *
  205      * @return array
  206      */
  207     public function getResponse()
  208     {
  209         return $this->_response;
  210     }
  211 
  212 
  213     /**
  214      * Retrieve the transaction log
  215      *
  216      * @return string
  217      */
  218     public function getLog()
  219     {
  220         return implode('', $this->_log);
  221     }
  222 
  223 
  224     /**
  225      * Reset the transaction log
  226      *
  227      * @return void
  228      */
  229     public function resetLog()
  230     {
  231         $this->_log = array();
  232     }
  233 
  234     /**
  235      * Add the transaction log
  236      *
  237      * @param  string new transaction
  238      * @return void
  239      */
  240     protected function _addLog($value)
  241     {
  242         if ($this->_maximumLog >= 0 && count($this->_log) >= $this->_maximumLog) {
  243             array_shift($this->_log);
  244         }
  245 
  246         $this->_log[] = $value;
  247     }
  248 
  249     /**
  250      * Connect to the server using the supplied transport and target
  251      *
  252      * An example $remote string may be 'tcp://mail.example.com:25' or 'ssh://hostname.com:2222'
  253      *
  254      * @param  string $remote Remote
  255      * @throws Zend_Mail_Protocol_Exception
  256      * @return boolean
  257      */
  258     protected function _connect($remote)
  259     {
  260         $errorNum = 0;
  261         $errorStr = '';
  262 
  263         // open connection
  264         $this->_socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
  265 
  266         if ($this->_socket === false) {
  267             if ($errorNum == 0) {
  268                 $errorStr = 'Could not open socket';
  269             }
  270             /**
  271              * @see Zend_Mail_Protocol_Exception
  272              */
  273             throw new Zend_Mail_Protocol_Exception($errorStr);
  274         }
  275 
  276         if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
  277             /**
  278              * @see Zend_Mail_Protocol_Exception
  279              */
  280             throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
  281         }
  282 
  283         return $result;
  284     }
  285 
  286 
  287     /**
  288      * Disconnect from remote host and free resource
  289      *
  290      * @return void
  291      */
  292     protected function _disconnect()
  293     {
  294         if (is_resource($this->_socket)) {
  295             fclose($this->_socket);
  296         }
  297     }
  298 
  299 
  300     /**
  301      * Send the given request followed by a LINEEND to the server.
  302      *
  303      * @param  string $request
  304      * @throws Zend_Mail_Protocol_Exception
  305      * @return integer|boolean Number of bytes written to remote host
  306      */
  307     protected function _send($request)
  308     {
  309         if (!is_resource($this->_socket)) {
  310             /**
  311              * @see Zend_Mail_Protocol_Exception
  312              */
  313             throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  314         }
  315 
  316         $this->_request = $request;
  317 
  318         $result = fwrite($this->_socket, $request . self::EOL);
  319 
  320         // Save request to internal log
  321         $this->_addLog($request . self::EOL);
  322 
  323         if ($result === false) {
  324             /**
  325              * @see Zend_Mail_Protocol_Exception
  326              */
  327             throw new Zend_Mail_Protocol_Exception('Could not send request to ' . $this->_host);
  328         }
  329 
  330         return $result;
  331     }
  332 
  333 
  334     /**
  335      * Get a line from the stream.
  336      *
  337      * @var    integer $timeout Per-request timeout value if applicable
  338      * @throws Zend_Mail_Protocol_Exception
  339      * @return string
  340      */
  341     protected function _receive($timeout = null)
  342     {
  343         if (!is_resource($this->_socket)) {
  344             /**
  345              * @see Zend_Mail_Protocol_Exception
  346              */
  347             throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  348         }
  349 
  350         // Adapters may wish to supply per-commend timeouts according to appropriate RFC
  351         if ($timeout !== null) {
  352             $this->_setStreamTimeout($timeout);
  353         }
  354 
  355         // Retrieve response
  356         $reponse = fgets($this->_socket, 1024);
  357 
  358         // Save request to internal log
  359         $this->_addLog($reponse);
  360 
  361         // Check meta data to ensure connection is still valid
  362         $info = stream_get_meta_data($this->_socket);
  363 
  364         if (!empty($info['timed_out'])) {
  365             /**
  366              * @see Zend_Mail_Protocol_Exception
  367              */
  368             throw new Zend_Mail_Protocol_Exception($this->_host . ' has timed out');
  369         }
  370 
  371         if ($reponse === false) {
  372             /**
  373              * @see Zend_Mail_Protocol_Exception
  374              */
  375             throw new Zend_Mail_Protocol_Exception('Could not read from ' . $this->_host);
  376         }
  377 
  378         return $reponse;
  379     }
  380 
  381 
  382     /**
  383      * Parse server response for successful codes
  384      *
  385      * Read the response from the stream and check for expected return code.
  386      * Throws a Zend_Mail_Protocol_Exception if an unexpected code is returned.
  387      *
  388      * @param  string|array $code One or more codes that indicate a successful response
  389      * @throws Zend_Mail_Protocol_Exception
  390      * @return string Last line of response string
  391      */
  392     protected function _expect($code, $timeout = null)
  393     {
  394         $this->_response = array();
  395         $cmd  = '';
  396         $more = '';
  397         $msg  = '';
  398         $errMsg = '';
  399 
  400         if (!is_array($code)) {
  401             $code = array($code);
  402         }
  403 
  404         do {
  405             $this->_response[] = $result = $this->_receive($timeout);
  406             list($cmd, $more, $msg) = preg_split('/([\s-]+)/', $result, 2, PREG_SPLIT_DELIM_CAPTURE);
  407 
  408             if ($errMsg !== '') {
  409                 $errMsg .= ' ' . $msg;
  410             } elseif ($cmd === null || !in_array($cmd, $code)) {
  411                 $errMsg =  $msg;
  412             }
  413 
  414         } while (strpos($more, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
  415 
  416         if ($errMsg !== '') {
  417             /**
  418              * @see Zend_Mail_Protocol_Exception
  419              */
  420             throw new Zend_Mail_Protocol_Exception($errMsg, $cmd);
  421         }
  422 
  423         return $msg;
  424     }
  425 
  426     /**
  427      * Set stream timeout
  428      *
  429      * @param integer $timeout
  430      * @return boolean
  431      */
  432     protected function _setStreamTimeout($timeout)
  433     {
  434        return stream_set_timeout($this->_socket, $timeout);
  435     }
  436 }