fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Mail / Protocol / AbstractProtocol.php
blobaf472078b4b24f1b8054fd5dc3e19468afacb549
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Mail\Protocol;
12 use Zend\Validator;
14 /**
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
21 /**
22 * Mail default EOL string
24 const EOL = "\r\n";
26 /**
27 * Default timeout in seconds for initiating session
29 const TIMEOUT_CONNECTION = 30;
31 /**
32 * Maximum of the transaction log
33 * @var int
35 protected $maximumLog = 64;
37 /**
38 * Hostname or IP address of remote server
39 * @var string
41 protected $host;
43 /**
44 * Port number of connection
45 * @var int
47 protected $port;
49 /**
50 * Instance of Zend\Validator\ValidatorChain to check hostnames
51 * @var \Zend\Validator\ValidatorChain
53 protected $validHost;
55 /**
56 * Socket connection resource
57 * @var resource
59 protected $socket;
61 /**
62 * Last request sent to server
63 * @var string
65 protected $request;
67 /**
68 * Array of server responses to last request
69 * @var array
71 protected $response;
73 /**
74 * Log of mail requests and server responses for a session
75 * @var array
77 private $log = array();
79 /**
80 * Constructor.
82 * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1)
83 * @param int $port OPTIONAL Port number (default: null)
84 * @throws Exception\RuntimeException
86 public function __construct($host = '127.0.0.1', $port = null)
88 $this->validHost = new Validator\ValidatorChain();
89 $this->validHost->attach(new Validator\Hostname(Validator\Hostname::ALLOW_ALL));
91 if (!$this->validHost->isValid($host)) {
92 throw new Exception\RuntimeException(implode(', ', $this->validHost->getMessages()));
95 $this->host = $host;
96 $this->port = $port;
99 /**
100 * Class destructor to cleanup open resources
103 public function __destruct()
105 $this->_disconnect();
109 * Set the maximum log size
111 * @param int $maximumLog Maximum log size
113 public function setMaximumLog($maximumLog)
115 $this->maximumLog = (int) $maximumLog;
119 * Get the maximum log size
121 * @return int the maximum log size
123 public function getMaximumLog()
125 return $this->maximumLog;
129 * Create a connection to the remote host
131 * Concrete adapters for this class will implement their own unique connect scripts, using the _connect() method to create the socket resource.
133 abstract public function connect();
136 * Retrieve the last client request
138 * @return string
140 public function getRequest()
142 return $this->request;
146 * Retrieve the last server response
148 * @return array
150 public function getResponse()
152 return $this->response;
156 * Retrieve the transaction log
158 * @return string
160 public function getLog()
162 return implode('', $this->log);
166 * Reset the transaction log
169 public function resetLog()
171 $this->log = array();
175 * Add the transaction log
177 * @param string $value new transaction
179 protected function _addLog($value)
181 if ($this->maximumLog >= 0 && count($this->log) >= $this->maximumLog) {
182 array_shift($this->log);
185 $this->log[] = $value;
189 * Connect to the server using the supplied transport and target
191 * An example $remote string may be 'tcp://mail.example.com:25' or 'ssh://hostname.com:2222'
193 * @param string $remote Remote
194 * @throws Exception\RuntimeException
195 * @return bool
197 protected function _connect($remote)
199 $errorNum = 0;
200 $errorStr = '';
202 // open connection
203 $this->socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
205 if ($this->socket === false) {
206 if ($errorNum == 0) {
207 $errorStr = 'Could not open socket';
209 throw new Exception\RuntimeException($errorStr);
212 if (($result = stream_set_timeout($this->socket, self::TIMEOUT_CONNECTION)) === false) {
213 throw new Exception\RuntimeException('Could not set stream timeout');
216 return $result;
220 * Disconnect from remote host and free resource
223 protected function _disconnect()
225 if (is_resource($this->socket)) {
226 fclose($this->socket);
231 * Send the given request followed by a LINEEND to the server.
233 * @param string $request
234 * @throws Exception\RuntimeException
235 * @return int|bool Number of bytes written to remote host
237 protected function _send($request)
239 if (!is_resource($this->socket)) {
240 throw new Exception\RuntimeException('No connection has been established to ' . $this->host);
243 $this->request = $request;
245 $result = fwrite($this->socket, $request . self::EOL);
247 // Save request to internal log
248 $this->_addLog($request . self::EOL);
250 if ($result === false) {
251 throw new Exception\RuntimeException('Could not send request to ' . $this->host);
254 return $result;
258 * Get a line from the stream.
260 * @param int $timeout Per-request timeout value if applicable
261 * @throws Exception\RuntimeException
262 * @return string
264 protected function _receive($timeout = null)
266 if (!is_resource($this->socket)) {
267 throw new Exception\RuntimeException('No connection has been established to ' . $this->host);
270 // Adapters may wish to supply per-commend timeouts according to appropriate RFC
271 if ($timeout !== null) {
272 stream_set_timeout($this->socket, $timeout);
275 // Retrieve response
276 $response = fgets($this->socket, 1024);
278 // Save request to internal log
279 $this->_addLog($response);
281 // Check meta data to ensure connection is still valid
282 $info = stream_get_meta_data($this->socket);
284 if (!empty($info['timed_out'])) {
285 throw new Exception\RuntimeException($this->host . ' has timed out');
288 if ($response === false) {
289 throw new Exception\RuntimeException('Could not read from ' . $this->host);
292 return $response;
296 * Parse server response for successful codes
298 * Read the response from the stream and check for expected return code.
299 * Throws a Zend\Mail\Protocol\Exception\ExceptionInterface if an unexpected code is returned.
301 * @param string|array $code One or more codes that indicate a successful response
302 * @param int $timeout Per-request timeout value if applicable
303 * @throws Exception\RuntimeException
304 * @return string Last line of response string
306 protected function _expect($code, $timeout = null)
308 $this->response = array();
309 $errMsg = '';
311 if (!is_array($code)) {
312 $code = array($code);
315 do {
316 $this->response[] = $result = $this->_receive($timeout);
317 list($cmd, $more, $msg) = preg_split('/([\s-]+)/', $result, 2, PREG_SPLIT_DELIM_CAPTURE);
319 if ($errMsg !== '') {
320 $errMsg .= ' ' . $msg;
321 } elseif ($cmd === null || !in_array($cmd, $code)) {
322 $errMsg = $msg;
324 } while (strpos($more, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
326 if ($errMsg !== '') {
327 throw new Exception\RuntimeException($errMsg);
330 return $msg;