PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Mail/Protocol/Abstract.php

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 385 lines | 127 code | 72 blank | 186 comment | 16 complexity | 35484918a6abab1050657d9e62ae6ef4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Protocol
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 8064 2008-02-16 10:58:39Z thomas $
  21. */
  22. /**
  23. * @see Zend_Validate
  24. */
  25. require_once 'Zend/Validate.php';
  26. /**
  27. * @see Zend_Validate_Hostname
  28. */
  29. require_once 'Zend/Validate/Hostname.php';
  30. /**
  31. * Zend_Mail_Protocol_Abstract
  32. *
  33. * Provides low-level methods for concrete adapters to communicate with a remote mail server and track requests and responses.
  34. *
  35. * @category Zend
  36. * @package Zend_Mail
  37. * @subpackage Protocol
  38. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @version $Id: Abstract.php 8064 2008-02-16 10:58:39Z thomas $
  41. * @todo Implement proxy settings
  42. */
  43. abstract class Zend_Mail_Protocol_Abstract
  44. {
  45. /**
  46. * Mail default EOL string
  47. */
  48. const EOL = "\r\n";
  49. /**
  50. * Default timeout in seconds for initiating session
  51. */
  52. const TIMEOUT_CONNECTION = 30;
  53. /**
  54. * Hostname or IP address of remote server
  55. * @var string
  56. */
  57. protected $_host;
  58. /**
  59. * Port number of connection
  60. * @var integer
  61. */
  62. protected $_port;
  63. /**
  64. * Instance of Zend_Validate to check hostnames
  65. * @var Zend_Validate
  66. */
  67. protected $_validHost;
  68. /**
  69. * Socket connection resource
  70. * @var resource
  71. */
  72. protected $_socket;
  73. /**
  74. * Last request sent to server
  75. * @var string
  76. */
  77. protected $_request;
  78. /**
  79. * Array of server responses to last request
  80. * @var array
  81. */
  82. protected $_response;
  83. /**
  84. * String template for parsing server responses using sscanf (default: 3 digit code and response string)
  85. * @var resource
  86. */
  87. protected $_template = '%d%s';
  88. /**
  89. * Log of mail requests and server responses for a session
  90. * @var string
  91. */
  92. private $_log;
  93. /**
  94. * Constructor.
  95. *
  96. * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1)
  97. * @param integer $port OPTIONAL Port number (default: null)
  98. * @throws Zend_Mail_Protocol_Exception
  99. * @return void
  100. */
  101. public function __construct($host = '127.0.0.1', $port = null)
  102. {
  103. $this->_validHost = new Zend_Validate();
  104. $this->_validHost->addValidator(new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL));
  105. if (!$this->_validHost->isValid($host)) {
  106. /**
  107. * @see Zend_Mail_Protocol_Exception
  108. */
  109. require_once 'Zend/Mail/Protocol/Exception.php';
  110. throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
  111. }
  112. $this->_host = $host;
  113. $this->_port = $port;
  114. }
  115. /**
  116. * Class destructor to cleanup open resources
  117. *
  118. * @return void
  119. */
  120. public function __destruct()
  121. {
  122. $this->_disconnect();
  123. }
  124. /**
  125. * Create a connection to the remote host
  126. *
  127. * Concrete adapters for this class will implement their own unique connect scripts, using the _connect() method to create the socket resource.
  128. */
  129. abstract public function connect();
  130. /**
  131. * Retrieve the last client request
  132. *
  133. * @return string
  134. */
  135. public function getRequest()
  136. {
  137. return $this->_request;
  138. }
  139. /**
  140. * Retrieve the last server response
  141. *
  142. * @return array
  143. */
  144. public function getResponse()
  145. {
  146. return $this->_response;
  147. }
  148. /**
  149. * Retrieve the transaction log
  150. *
  151. * @return string
  152. */
  153. public function getLog()
  154. {
  155. return $this->_log;
  156. }
  157. /**
  158. * Reset the transaction log
  159. *
  160. * @return void
  161. */
  162. public function resetLog()
  163. {
  164. $this->_log = '';
  165. }
  166. /**
  167. * Connect to the server using the supplied transport and target
  168. *
  169. * An example $remote string may be 'tcp://mail.example.com:25' or 'ssh://hostname.com:2222'
  170. *
  171. * @param string $remote Remote
  172. * @throws Zend_Mail_Protocol_Exception
  173. * @return boolean
  174. */
  175. protected function _connect($remote)
  176. {
  177. $errorNum = 0;
  178. $errorStr = '';
  179. // open connection
  180. $this->_socket = stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
  181. if ($this->_socket === false) {
  182. if ($errorNum == 0) {
  183. $errorStr = 'Could not open socket';
  184. }
  185. /**
  186. * @see Zend_Mail_Protocol_Exception
  187. */
  188. require_once 'Zend/Mail/Protocol/Exception.php';
  189. throw new Zend_Mail_Protocol_Exception($errorStr);
  190. }
  191. if (($result = stream_set_timeout($this->_socket, self::TIMEOUT_CONNECTION)) === false) {
  192. /**
  193. * @see Zend_Mail_Protocol_Exception
  194. */
  195. require_once 'Zend/Mail/Protocol/Exception.php';
  196. throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
  197. }
  198. return $result;
  199. }
  200. /**
  201. * Disconnect from remote host and free resource
  202. *
  203. * @return void
  204. */
  205. protected function _disconnect()
  206. {
  207. if (is_resource($this->_socket)) {
  208. fclose($this->_socket);
  209. }
  210. }
  211. /**
  212. * Send the given request followed by a LINEEND to the server.
  213. *
  214. * @param string $request
  215. * @throws Zend_Mail_Protocol_Exception
  216. * @return integer|boolean Number of bytes written to remote host
  217. */
  218. protected function _send($request)
  219. {
  220. if (!is_resource($this->_socket)) {
  221. /**
  222. * @see Zend_Mail_Protocol_Exception
  223. */
  224. require_once 'Zend/Mail/Protocol/Exception.php';
  225. throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  226. }
  227. $this->_request = $request;
  228. $result = fwrite($this->_socket, $request . self::EOL);
  229. // Save request to internal log
  230. $this->_log .= $request . self::EOL;
  231. if ($result === false) {
  232. /**
  233. * @see Zend_Mail_Protocol_Exception
  234. */
  235. require_once 'Zend/Mail/Protocol/Exception.php';
  236. throw new Zend_Mail_Protocol_Exception('Could not send request to ' . $this->_host);
  237. }
  238. return $result;
  239. }
  240. /**
  241. * Get a line from the stream.
  242. *
  243. * @var integer $timeout Per-request timeout value if applicable
  244. * @throws Zend_Mail_Protocol_Exception
  245. * @return string
  246. */
  247. protected function _receive($timeout = null)
  248. {
  249. if (!is_resource($this->_socket)) {
  250. /**
  251. * @see Zend_Mail_Protocol_Exception
  252. */
  253. require_once 'Zend/Mail/Protocol/Exception.php';
  254. throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  255. }
  256. // Adapters may wish to supply per-commend timeouts according to appropriate RFC
  257. if ($timeout !== null) {
  258. stream_set_timeout($this->_socket, $timeout);
  259. }
  260. // Retrieve response
  261. $reponse = fgets($this->_socket, 1024);
  262. // Save request to internal log
  263. $this->_log .= $reponse;
  264. // Check meta data to ensure connection is still valid
  265. $info = stream_get_meta_data($this->_socket);
  266. if (!empty($info['timed_out'])) {
  267. /**
  268. * @see Zend_Mail_Protocol_Exception
  269. */
  270. require_once 'Zend/Mail/Protocol/Exception.php';
  271. throw new Zend_Mail_Protocol_Exception($this->_host . ' has timed out');
  272. }
  273. if ($reponse === false) {
  274. /**
  275. * @see Zend_Mail_Protocol_Exception
  276. */
  277. require_once 'Zend/Mail/Protocol/Exception.php';
  278. throw new Zend_Mail_Protocol_Exception('Could not read from ' . $this->_host);
  279. }
  280. return $reponse;
  281. }
  282. /**
  283. * Parse server response for successful codes
  284. *
  285. * Read the response from the stream and check for expected return code.
  286. * Throws a Zend_Mail_Protocol_Exception if an unexpected code is returned.
  287. *
  288. * @param string|array $code One or more codes that indicate a successful response
  289. * @throws Zend_Mail_Protocol_Exception
  290. * @return string Last line of response string
  291. */
  292. protected function _expect($code, $timeout = null)
  293. {
  294. $this->_response = array();
  295. $cmd = '';
  296. $msg = '';
  297. if (!is_array($code)) {
  298. $code = array($code);
  299. }
  300. do {
  301. $this->_response[] = $result = $this->_receive($timeout);
  302. sscanf($result, $this->_template, $cmd, $msg);
  303. if ($cmd === null || !in_array($cmd, $code)) {
  304. /**
  305. * @see Zend_Mail_Protocol_Exception
  306. */
  307. require_once 'Zend/Mail/Protocol/Exception.php';
  308. throw new Zend_Mail_Protocol_Exception($result);
  309. }
  310. } while (strpos($msg, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
  311. return $msg;
  312. }
  313. }