PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 356 lines | 140 code | 72 blank | 144 comment | 19 complexity | fc95fc551db26b973ff56d0fbb24c364 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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
  8. */
  9. namespace Zend\Mail\Protocol;
  10. use Zend\Validator;
  11. /**
  12. * Provides low-level methods for concrete adapters to communicate with a remote mail server and track requests and responses.
  13. *
  14. * @todo Implement proxy settings
  15. */
  16. abstract class AbstractProtocol
  17. {
  18. /**
  19. * Mail default EOL string
  20. */
  21. const EOL = "\r\n";
  22. /**
  23. * Default timeout in seconds for initiating session
  24. */
  25. const TIMEOUT_CONNECTION = 30;
  26. /**
  27. * Maximum of the transaction log
  28. * @var int
  29. */
  30. protected $maximumLog = 64;
  31. /**
  32. * Hostname or IP address of remote server
  33. * @var string
  34. */
  35. protected $host;
  36. /**
  37. * Port number of connection
  38. * @var int
  39. */
  40. protected $port;
  41. /**
  42. * Instance of Zend\Validator\ValidatorChain to check hostnames
  43. * @var \Zend\Validator\ValidatorChain
  44. */
  45. protected $validHost;
  46. /**
  47. * Socket connection resource
  48. * @var resource
  49. */
  50. protected $socket;
  51. /**
  52. * Last request sent to server
  53. * @var string
  54. */
  55. protected $request;
  56. /**
  57. * Array of server responses to last request
  58. * @var array
  59. */
  60. protected $response;
  61. /**
  62. * Log of mail requests and server responses for a session
  63. * @var array
  64. */
  65. private $log = array();
  66. /**
  67. * Constructor.
  68. *
  69. * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1)
  70. * @param int $port OPTIONAL Port number (default: null)
  71. * @throws Exception\RuntimeException
  72. */
  73. public function __construct($host = '127.0.0.1', $port = null)
  74. {
  75. $this->validHost = new Validator\ValidatorChain();
  76. $this->validHost->attach(new Validator\Hostname(Validator\Hostname::ALLOW_ALL));
  77. if (!$this->validHost->isValid($host)) {
  78. throw new Exception\RuntimeException(implode(', ', $this->validHost->getMessages()));
  79. }
  80. $this->host = $host;
  81. $this->port = $port;
  82. }
  83. /**
  84. * Class destructor to cleanup open resources
  85. *
  86. */
  87. public function __destruct()
  88. {
  89. $this->_disconnect();
  90. }
  91. /**
  92. * Set the maximum log size
  93. *
  94. * @param int $maximumLog Maximum log size
  95. */
  96. public function setMaximumLog($maximumLog)
  97. {
  98. $this->maximumLog = (int) $maximumLog;
  99. }
  100. /**
  101. * Get the maximum log size
  102. *
  103. * @return int the maximum log size
  104. */
  105. public function getMaximumLog()
  106. {
  107. return $this->maximumLog;
  108. }
  109. /**
  110. * Create a connection to the remote host
  111. *
  112. * Concrete adapters for this class will implement their own unique connect scripts, using the _connect() method to create the socket resource.
  113. */
  114. abstract public function connect();
  115. /**
  116. * Retrieve the last client request
  117. *
  118. * @return string
  119. */
  120. public function getRequest()
  121. {
  122. return $this->request;
  123. }
  124. /**
  125. * Retrieve the last server response
  126. *
  127. * @return array
  128. */
  129. public function getResponse()
  130. {
  131. return $this->response;
  132. }
  133. /**
  134. * Retrieve the transaction log
  135. *
  136. * @return string
  137. */
  138. public function getLog()
  139. {
  140. return implode('', $this->log);
  141. }
  142. /**
  143. * Reset the transaction log
  144. *
  145. */
  146. public function resetLog()
  147. {
  148. $this->log = array();
  149. }
  150. /**
  151. * Add the transaction log
  152. *
  153. * @param string $value new transaction
  154. */
  155. protected function _addLog($value)
  156. {
  157. if ($this->maximumLog >= 0 && count($this->log) >= $this->maximumLog) {
  158. array_shift($this->log);
  159. }
  160. $this->log[] = $value;
  161. }
  162. /**
  163. * Connect to the server using the supplied transport and target
  164. *
  165. * An example $remote string may be 'tcp://mail.example.com:25' or 'ssh://hostname.com:2222'
  166. *
  167. * @param string $remote Remote
  168. * @throws Exception\RuntimeException
  169. * @return bool
  170. */
  171. protected function _connect($remote)
  172. {
  173. $errorNum = 0;
  174. $errorStr = '';
  175. // open connection
  176. $this->socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
  177. if ($this->socket === false) {
  178. if ($errorNum == 0) {
  179. $errorStr = 'Could not open socket';
  180. }
  181. throw new Exception\RuntimeException($errorStr);
  182. }
  183. if (($result = stream_set_timeout($this->socket, self::TIMEOUT_CONNECTION)) === false) {
  184. throw new Exception\RuntimeException('Could not set stream timeout');
  185. }
  186. return $result;
  187. }
  188. /**
  189. * Disconnect from remote host and free resource
  190. *
  191. */
  192. protected function _disconnect()
  193. {
  194. if (is_resource($this->socket)) {
  195. fclose($this->socket);
  196. }
  197. }
  198. /**
  199. * Send the given request followed by a LINEEND to the server.
  200. *
  201. * @param string $request
  202. * @throws Exception\RuntimeException
  203. * @return int|bool Number of bytes written to remote host
  204. */
  205. protected function _send($request)
  206. {
  207. if (!is_resource($this->socket)) {
  208. throw new Exception\RuntimeException('No connection has been established to ' . $this->host);
  209. }
  210. $this->request = $request;
  211. $result = fwrite($this->socket, $request . self::EOL);
  212. // Save request to internal log
  213. $this->_addLog($request . self::EOL);
  214. if ($result === false) {
  215. throw new Exception\RuntimeException('Could not send request to ' . $this->host);
  216. }
  217. return $result;
  218. }
  219. /**
  220. * Get a line from the stream.
  221. *
  222. * @param int $timeout Per-request timeout value if applicable
  223. * @throws Exception\RuntimeException
  224. * @return string
  225. */
  226. protected function _receive($timeout = null)
  227. {
  228. if (!is_resource($this->socket)) {
  229. throw new Exception\RuntimeException('No connection has been established to ' . $this->host);
  230. }
  231. // Adapters may wish to supply per-commend timeouts according to appropriate RFC
  232. if ($timeout !== null) {
  233. stream_set_timeout($this->socket, $timeout);
  234. }
  235. // Retrieve response
  236. $response = fgets($this->socket, 1024);
  237. // Save request to internal log
  238. $this->_addLog($response);
  239. // Check meta data to ensure connection is still valid
  240. $info = stream_get_meta_data($this->socket);
  241. if (!empty($info['timed_out'])) {
  242. throw new Exception\RuntimeException($this->host . ' has timed out');
  243. }
  244. if ($response === false) {
  245. throw new Exception\RuntimeException('Could not read from ' . $this->host);
  246. }
  247. return $response;
  248. }
  249. /**
  250. * Parse server response for successful codes
  251. *
  252. * Read the response from the stream and check for expected return code.
  253. * Throws a Zend_Mail_Protocol_Exception if an unexpected code is returned.
  254. *
  255. * @param string|array $code One or more codes that indicate a successful response
  256. * @param int $timeout Per-request timeout value if applicable
  257. * @throws Exception\RuntimeException
  258. * @return string Last line of response string
  259. */
  260. protected function _expect($code, $timeout = null)
  261. {
  262. $this->response = array();
  263. $cmd = '';
  264. $more = '';
  265. $msg = '';
  266. $errMsg = '';
  267. if (!is_array($code)) {
  268. $code = array($code);
  269. }
  270. do {
  271. $this->response[] = $result = $this->_receive($timeout);
  272. list($cmd, $more, $msg) = preg_split('/([\s-]+)/', $result, 2, PREG_SPLIT_DELIM_CAPTURE);
  273. if ($errMsg !== '') {
  274. $errMsg .= ' ' . $msg;
  275. } elseif ($cmd === null || !in_array($cmd, $code)) {
  276. $errMsg = $msg;
  277. }
  278. } while (strpos($more, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
  279. if ($errMsg !== '') {
  280. throw new Exception\RuntimeException($errMsg);
  281. }
  282. return $msg;
  283. }
  284. }