PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/tanduy/zf
PHP | 411 lines | 141 code | 75 blank | 195 comment | 18 complexity | 0aa3f226782879f308156e9a8d2494a1 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @version $Id$
  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. * Maximum of the transaction log
  55. */
  56. const MAXIMUM_LOG = 64;
  57. /**
  58. * Hostname or IP address of remote server
  59. * @var string
  60. */
  61. protected $_host;
  62. /**
  63. * Port number of connection
  64. * @var integer
  65. */
  66. protected $_port;
  67. /**
  68. * Instance of Zend_Validate to check hostnames
  69. * @var Zend_Validate
  70. */
  71. protected $_validHost;
  72. /**
  73. * Socket connection resource
  74. * @var resource
  75. */
  76. protected $_socket;
  77. /**
  78. * Last request sent to server
  79. * @var string
  80. */
  81. protected $_request;
  82. /**
  83. * Array of server responses to last request
  84. * @var array
  85. */
  86. protected $_response;
  87. /**
  88. * String template for parsing server responses using sscanf (default: 3 digit code and response string)
  89. * @var resource
  90. */
  91. protected $_template = '%d%s';
  92. /**
  93. * Log of mail requests and server responses for a session
  94. * @var array
  95. */
  96. private $_log = array();
  97. /**
  98. * Constructor.
  99. *
  100. * @param string $host OPTIONAL Hostname of remote connection (default: 127.0.0.1)
  101. * @param integer $port OPTIONAL Port number (default: null)
  102. * @throws Zend_Mail_Protocol_Exception
  103. * @return void
  104. */
  105. public function __construct($host = '127.0.0.1', $port = null)
  106. {
  107. $this->_validHost = new Zend_Validate();
  108. $this->_validHost->addValidator(new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL));
  109. if (!$this->_validHost->isValid($host)) {
  110. /**
  111. * @see Zend_Mail_Protocol_Exception
  112. */
  113. require_once 'Zend/Mail/Protocol/Exception.php';
  114. throw new Zend_Mail_Protocol_Exception(join(', ', $this->_validHost->getMessages()));
  115. }
  116. $this->_host = $host;
  117. $this->_port = $port;
  118. }
  119. /**
  120. * Class destructor to cleanup open resources
  121. *
  122. * @return void
  123. */
  124. public function __destruct()
  125. {
  126. $this->_disconnect();
  127. }
  128. /**
  129. * Create a connection to the remote host
  130. *
  131. * Concrete adapters for this class will implement their own unique connect scripts, using the _connect() method to create the socket resource.
  132. */
  133. abstract public function connect();
  134. /**
  135. * Retrieve the last client request
  136. *
  137. * @return string
  138. */
  139. public function getRequest()
  140. {
  141. return $this->_request;
  142. }
  143. /**
  144. * Retrieve the last server response
  145. *
  146. * @return array
  147. */
  148. public function getResponse()
  149. {
  150. return $this->_response;
  151. }
  152. /**
  153. * Retrieve the transaction log
  154. *
  155. * @return string
  156. */
  157. public function getLog()
  158. {
  159. return implode('', $this->_log);
  160. }
  161. /**
  162. * Reset the transaction log
  163. *
  164. * @return void
  165. */
  166. public function resetLog()
  167. {
  168. $this->_log = array();
  169. }
  170. /**
  171. * Add the transaction log
  172. *
  173. * @param string new transaction
  174. * @return void
  175. */
  176. protected function _addLog($value)
  177. {
  178. if (count($this->_log) >= self::MAXIMUM_LOG) {
  179. array_shift($this->_log);
  180. }
  181. $this->_log[] = $value;
  182. }
  183. /**
  184. * Connect to the server using the supplied transport and target
  185. *
  186. * An example $remote string may be 'tcp://mail.example.com:25' or 'ssh://hostname.com:2222'
  187. *
  188. * @param string $remote Remote
  189. * @throws Zend_Mail_Protocol_Exception
  190. * @return boolean
  191. */
  192. protected function _connect($remote)
  193. {
  194. $errorNum = 0;
  195. $errorStr = '';
  196. // open connection
  197. $this->_socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
  198. if ($this->_socket === false) {
  199. if ($errorNum == 0) {
  200. $errorStr = 'Could not open socket';
  201. }
  202. /**
  203. * @see Zend_Mail_Protocol_Exception
  204. */
  205. require_once 'Zend/Mail/Protocol/Exception.php';
  206. throw new Zend_Mail_Protocol_Exception($errorStr);
  207. }
  208. if (($result = stream_set_timeout($this->_socket, self::TIMEOUT_CONNECTION)) === false) {
  209. /**
  210. * @see Zend_Mail_Protocol_Exception
  211. */
  212. require_once 'Zend/Mail/Protocol/Exception.php';
  213. throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
  214. }
  215. return $result;
  216. }
  217. /**
  218. * Disconnect from remote host and free resource
  219. *
  220. * @return void
  221. */
  222. protected function _disconnect()
  223. {
  224. if (is_resource($this->_socket)) {
  225. fclose($this->_socket);
  226. }
  227. }
  228. /**
  229. * Send the given request followed by a LINEEND to the server.
  230. *
  231. * @param string $request
  232. * @throws Zend_Mail_Protocol_Exception
  233. * @return integer|boolean Number of bytes written to remote host
  234. */
  235. protected function _send($request)
  236. {
  237. if (!is_resource($this->_socket)) {
  238. /**
  239. * @see Zend_Mail_Protocol_Exception
  240. */
  241. require_once 'Zend/Mail/Protocol/Exception.php';
  242. throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  243. }
  244. $this->_request = $request;
  245. $result = fwrite($this->_socket, $request . self::EOL);
  246. // Save request to internal log
  247. $this->_addLog($request . self::EOL);
  248. if ($result === false) {
  249. /**
  250. * @see Zend_Mail_Protocol_Exception
  251. */
  252. require_once 'Zend/Mail/Protocol/Exception.php';
  253. throw new Zend_Mail_Protocol_Exception('Could not send request to ' . $this->_host);
  254. }
  255. return $result;
  256. }
  257. /**
  258. * Get a line from the stream.
  259. *
  260. * @var integer $timeout Per-request timeout value if applicable
  261. * @throws Zend_Mail_Protocol_Exception
  262. * @return string
  263. */
  264. protected function _receive($timeout = null)
  265. {
  266. if (!is_resource($this->_socket)) {
  267. /**
  268. * @see Zend_Mail_Protocol_Exception
  269. */
  270. require_once 'Zend/Mail/Protocol/Exception.php';
  271. throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  272. }
  273. // Adapters may wish to supply per-commend timeouts according to appropriate RFC
  274. if ($timeout !== null) {
  275. stream_set_timeout($this->_socket, $timeout);
  276. }
  277. // Retrieve response
  278. $reponse = fgets($this->_socket, 1024);
  279. // Save request to internal log
  280. $this->_addLog($reponse);
  281. // Check meta data to ensure connection is still valid
  282. $info = stream_get_meta_data($this->_socket);
  283. if (!empty($info['timed_out'])) {
  284. /**
  285. * @see Zend_Mail_Protocol_Exception
  286. */
  287. require_once 'Zend/Mail/Protocol/Exception.php';
  288. throw new Zend_Mail_Protocol_Exception($this->_host . ' has timed out');
  289. }
  290. if ($reponse === false) {
  291. /**
  292. * @see Zend_Mail_Protocol_Exception
  293. */
  294. require_once 'Zend/Mail/Protocol/Exception.php';
  295. throw new Zend_Mail_Protocol_Exception('Could not read from ' . $this->_host);
  296. }
  297. return $reponse;
  298. }
  299. /**
  300. * Parse server response for successful codes
  301. *
  302. * Read the response from the stream and check for expected return code.
  303. * Throws a Zend_Mail_Protocol_Exception if an unexpected code is returned.
  304. *
  305. * @param string|array $code One or more codes that indicate a successful response
  306. * @throws Zend_Mail_Protocol_Exception
  307. * @return string Last line of response string
  308. */
  309. protected function _expect($code, $timeout = null)
  310. {
  311. $this->_response = array();
  312. $cmd = '';
  313. $msg = '';
  314. $errMsg = '';
  315. if (!is_array($code)) {
  316. $code = array($code);
  317. }
  318. do {
  319. $this->_response[] = $result = $this->_receive($timeout);
  320. sscanf($result, $this->_template, $cmd, $msg);
  321. if ($errMsg !== '') {
  322. $errMsg .= $msg;
  323. } elseif ($cmd === null || !in_array($cmd, $code)) {
  324. $errMsg = $msg;
  325. }
  326. } while (strpos($msg, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
  327. if ($errMsg !== '') {
  328. /**
  329. * @see Zend_Mail_Protocol_Exception
  330. */
  331. require_once 'Zend/Mail/Protocol/Exception.php';
  332. throw new Zend_Mail_Protocol_Exception($errMsg);
  333. }
  334. return $msg;
  335. }
  336. }