PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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