3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Protocol
;
15 * Provides low-level methods for concrete adapters to communicate with a remote mail server and track requests and responses.
17 * @todo Implement proxy settings
19 abstract class AbstractProtocol
22 * Mail default EOL string
28 * Default timeout in seconds for initiating session
30 const TIMEOUT_CONNECTION
= 30;
33 * Maximum of the transaction log
36 protected $maximumLog = 64;
40 * Hostname or IP address of remote server
47 * Port number of connection
54 * Instance of Zend\Validator\ValidatorChain to check hostnames
55 * @var \Zend\Validator\ValidatorChain
61 * Socket connection resource
68 * Last request sent to server
75 * Array of server responses to last request
82 * Log of mail requests and server responses for a session
85 private $log = array();
91 * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1)
92 * @param int $port OPTIONAL Port number (default: null)
93 * @throws Exception\RuntimeException
95 public function __construct($host = '127.0.0.1', $port = null)
97 $this->validHost
= new Validator\
ValidatorChain();
98 $this->validHost
->attach(new Validator\
Hostname(Validator\Hostname
::ALLOW_ALL
));
100 if (!$this->validHost
->isValid($host)) {
101 throw new Exception\
RuntimeException(implode(', ', $this->validHost
->getMessages()));
110 * Class destructor to cleanup open resources
113 public function __destruct()
115 $this->_disconnect();
119 * Set the maximum log size
121 * @param int $maximumLog Maximum log size
123 public function setMaximumLog($maximumLog)
125 $this->maximumLog
= (int) $maximumLog;
130 * Get the maximum log size
132 * @return int the maximum log size
134 public function getMaximumLog()
136 return $this->maximumLog
;
141 * Create a connection to the remote host
143 * Concrete adapters for this class will implement their own unique connect scripts, using the _connect() method to create the socket resource.
145 abstract public function connect();
149 * Retrieve the last client request
153 public function getRequest()
155 return $this->request
;
160 * Retrieve the last server response
164 public function getResponse()
166 return $this->response
;
171 * Retrieve the transaction log
175 public function getLog()
177 return implode('', $this->log
);
182 * Reset the transaction log
185 public function resetLog()
187 $this->log
= array();
191 * Add the transaction log
193 * @param string $value new transaction
195 protected function _addLog($value)
197 if ($this->maximumLog
>= 0 && count($this->log
) >= $this->maximumLog
) {
198 array_shift($this->log
);
201 $this->log
[] = $value;
205 * Connect to the server using the supplied transport and target
207 * An example $remote string may be 'tcp://mail.example.com:25' or 'ssh://hostname.com:2222'
209 * @param string $remote Remote
210 * @throws Exception\RuntimeException
213 protected function _connect($remote)
219 $this->socket
= @stream_socket_client
($remote, $errorNum, $errorStr, self
::TIMEOUT_CONNECTION
);
221 if ($this->socket
=== false) {
222 if ($errorNum == 0) {
223 $errorStr = 'Could not open socket';
225 throw new Exception\
RuntimeException($errorStr);
228 if (($result = stream_set_timeout($this->socket
, self
::TIMEOUT_CONNECTION
)) === false) {
229 throw new Exception\
RuntimeException('Could not set stream timeout');
237 * Disconnect from remote host and free resource
240 protected function _disconnect()
242 if (is_resource($this->socket
)) {
243 fclose($this->socket
);
249 * Send the given request followed by a LINEEND to the server.
251 * @param string $request
252 * @throws Exception\RuntimeException
253 * @return int|bool Number of bytes written to remote host
255 protected function _send($request)
257 if (!is_resource($this->socket
)) {
258 throw new Exception\
RuntimeException('No connection has been established to ' . $this->host
);
261 $this->request
= $request;
263 $result = fwrite($this->socket
, $request . self
::EOL
);
265 // Save request to internal log
266 $this->_addLog($request . self
::EOL
);
268 if ($result === false) {
269 throw new Exception\
RuntimeException('Could not send request to ' . $this->host
);
277 * Get a line from the stream.
279 * @param int $timeout Per-request timeout value if applicable
280 * @throws Exception\RuntimeException
283 protected function _receive($timeout = null)
285 if (!is_resource($this->socket
)) {
286 throw new Exception\
RuntimeException('No connection has been established to ' . $this->host
);
289 // Adapters may wish to supply per-commend timeouts according to appropriate RFC
290 if ($timeout !== null) {
291 stream_set_timeout($this->socket
, $timeout);
295 $response = fgets($this->socket
, 1024);
297 // Save request to internal log
298 $this->_addLog($response);
300 // Check meta data to ensure connection is still valid
301 $info = stream_get_meta_data($this->socket
);
303 if (!empty($info['timed_out'])) {
304 throw new Exception\
RuntimeException($this->host
. ' has timed out');
307 if ($response === false) {
308 throw new Exception\
RuntimeException('Could not read from ' . $this->host
);
316 * Parse server response for successful codes
318 * Read the response from the stream and check for expected return code.
319 * Throws a Zend\Mail\Protocol\Exception\ExceptionInterface if an unexpected code is returned.
321 * @param string|array $code One or more codes that indicate a successful response
322 * @param int $timeout Per-request timeout value if applicable
323 * @throws Exception\RuntimeException
324 * @return string Last line of response string
326 protected function _expect($code, $timeout = null)
328 $this->response
= array();
334 if (!is_array($code)) {
335 $code = array($code);
339 $this->response
[] = $result = $this->_receive($timeout);
340 list($cmd, $more, $msg) = preg_split('/([\s-]+)/', $result, 2, PREG_SPLIT_DELIM_CAPTURE
);
342 if ($errMsg !== '') {
343 $errMsg .= ' ' . $msg;
344 } elseif ($cmd === null ||
!in_array($cmd, $code)) {
348 } while (strpos($more, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
350 if ($errMsg !== '') {
351 throw new Exception\
RuntimeException($errMsg);