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

/system/Zend/Mail/Protocol/Abstract.php

https://gitlab.com/Ltaimao/wecenter
PHP | 451 lines | 146 code | 80 blank | 225 comment | 19 complexity | ba160f72f410191abaad21e34d774ff7 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-2015 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-2015 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. * @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($transport, $host, $port) // Modify by WeCenter
  214. {
  215. $errorNum = 0;
  216. $errorStr = '';
  217. $remote = $transport . '://' . $host;
  218. // open connection
  219. $this->_socket = (function_exists('stream_socket_client')) ?
  220. @stream_socket_client($remote . ':' . $port, $errorNum, $errorStr, self::TIMEOUT_CONNECTION)
  221. : @fsockopen($remote, $port, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
  222. if ($this->_socket === false) {
  223. if ($errorNum == 0) {
  224. $errorStr = 'Could not open socket';
  225. }
  226. /**
  227. * @see Zend_Mail_Protocol_Exception
  228. */
  229. //require_once 'Zend/Mail/Protocol/Exception.php';
  230. throw new Zend_Mail_Protocol_Exception($errorStr);
  231. }
  232. if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
  233. /**
  234. * @see Zend_Mail_Protocol_Exception
  235. */
  236. //require_once 'Zend/Mail/Protocol/Exception.php';
  237. throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Disconnect from remote host and free resource
  243. *
  244. * @return void
  245. */
  246. protected function _disconnect()
  247. {
  248. if (is_resource($this->_socket)) {
  249. fclose($this->_socket);
  250. }
  251. }
  252. /**
  253. * Send the given request followed by a LINEEND to the server.
  254. *
  255. * @param string $request
  256. * @throws Zend_Mail_Protocol_Exception
  257. * @return integer|boolean Number of bytes written to remote host
  258. */
  259. protected function _send($request)
  260. {
  261. if (!is_resource($this->_socket)) {
  262. /**
  263. * @see Zend_Mail_Protocol_Exception
  264. */
  265. //require_once 'Zend/Mail/Protocol/Exception.php';
  266. throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  267. }
  268. $this->_request = $request;
  269. $result = fwrite($this->_socket, $request . self::EOL);
  270. // Save request to internal log
  271. $this->_addLog($request . self::EOL);
  272. if ($result === false) {
  273. /**
  274. * @see Zend_Mail_Protocol_Exception
  275. */
  276. //require_once 'Zend/Mail/Protocol/Exception.php';
  277. throw new Zend_Mail_Protocol_Exception('Could not send request to ' . $this->_host);
  278. }
  279. return $result;
  280. }
  281. /**
  282. * Get a line from the stream.
  283. *
  284. * @var integer $timeout Per-request timeout value if applicable
  285. * @throws Zend_Mail_Protocol_Exception
  286. * @return string
  287. */
  288. protected function _receive($timeout = null)
  289. {
  290. if (!is_resource($this->_socket)) {
  291. /**
  292. * @see Zend_Mail_Protocol_Exception
  293. */
  294. //require_once 'Zend/Mail/Protocol/Exception.php';
  295. throw new Zend_Mail_Protocol_Exception('No connection has been established to ' . $this->_host);
  296. }
  297. // Adapters may wish to supply per-commend timeouts according to appropriate RFC
  298. if ($timeout !== null) {
  299. $this->_setStreamTimeout($timeout);
  300. }
  301. // Retrieve response
  302. $reponse = fgets($this->_socket, 1024);
  303. // Save request to internal log
  304. $this->_addLog($reponse);
  305. // Check meta data to ensure connection is still valid
  306. $info = stream_get_meta_data($this->_socket);
  307. if (!empty($info['timed_out'])) {
  308. /**
  309. * @see Zend_Mail_Protocol_Exception
  310. */
  311. //require_once 'Zend/Mail/Protocol/Exception.php';
  312. throw new Zend_Mail_Protocol_Exception($this->_host . ' has timed out');
  313. }
  314. if ($reponse === false) {
  315. /**
  316. * @see Zend_Mail_Protocol_Exception
  317. */
  318. //require_once 'Zend/Mail/Protocol/Exception.php';
  319. throw new Zend_Mail_Protocol_Exception('Could not read from ' . $this->_host);
  320. }
  321. return $reponse;
  322. }
  323. /**
  324. * Parse server response for successful codes
  325. *
  326. * Read the response from the stream and check for expected return code.
  327. * Throws a Zend_Mail_Protocol_Exception if an unexpected code is returned.
  328. *
  329. * @param string|array $code One or more codes that indicate a successful response
  330. * @throws Zend_Mail_Protocol_Exception
  331. * @return string Last line of response string
  332. */
  333. protected function _expect($code, $timeout = null)
  334. {
  335. $this->_response = array();
  336. $cmd = '';
  337. $more = '';
  338. $msg = '';
  339. $errMsg = '';
  340. if (!is_array($code)) {
  341. $code = array($code);
  342. }
  343. do {
  344. $this->_response[] = $result = $this->_receive($timeout);
  345. list($cmd, $more, $msg) = preg_split('/([\s-]+)/', $result, 2, PREG_SPLIT_DELIM_CAPTURE);
  346. if ($errMsg !== '') {
  347. $errMsg .= ' ' . $msg;
  348. } elseif ($cmd === null || !in_array($cmd, $code)) {
  349. $errMsg = $msg;
  350. }
  351. } while (strpos($more, '-') === 0); // The '-' message prefix indicates an information string instead of a response string.
  352. if ($errMsg !== '') {
  353. /**
  354. * @see Zend_Mail_Protocol_Exception
  355. */
  356. //require_once 'Zend/Mail/Protocol/Exception.php';
  357. throw new Zend_Mail_Protocol_Exception($errMsg, $cmd);
  358. }
  359. return $msg;
  360. }
  361. /**
  362. * Set stream timeout
  363. *
  364. * @param integer $timeout
  365. * @return boolean
  366. */
  367. protected function _setStreamTimeout($timeout)
  368. {
  369. return stream_set_timeout($this->_socket, $timeout);
  370. }
  371. }