PageRenderTime 62ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/zend/Zend/Mail/Protocol/Abstract.php

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