PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/application/vendor/Swift/classes/Swift/Transport/EsmtpTransport.php

https://bitbucket.org/SinSiXX/tickets
PHP | 353 lines | 215 code | 34 blank | 104 comment | 8 complexity | a98841bf2137c8ce7a922b6a6e2b7012 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Sends Messages over SMTP with ESMTP support.
  11. * @package Swift
  12. * @subpackage Transport
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Transport_EsmtpTransport
  16. extends Swift_Transport_AbstractSmtpTransport
  17. implements Swift_Transport_SmtpAgent
  18. {
  19. /**
  20. * ESMTP extension handlers.
  21. * @var Swift_Transport_EsmtpHandler[]
  22. * @access private
  23. */
  24. private $_handlers = array();
  25. /**
  26. * ESMTP capabilities.
  27. * @var string[]
  28. * @access private
  29. */
  30. private $_capabilities = array();
  31. /**
  32. * Connection buffer parameters.
  33. * @var array
  34. * @access protected
  35. */
  36. private $_params = array(
  37. 'protocol' => 'tcp',
  38. 'host' => 'localhost',
  39. 'port' => 25,
  40. 'timeout' => 30,
  41. 'blocking' => 1,
  42. 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET
  43. );
  44. /**
  45. * Creates a new EsmtpTransport using the given I/O buffer.
  46. * @param Swift_Transport_IoBuffer $buf
  47. * @param Swift_Transport_EsmtpHandler[] $extensionHandlers
  48. * @param Swift_Events_EventDispatcher $dispatcher
  49. */
  50. public function __construct(Swift_Transport_IoBuffer $buf,
  51. array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher)
  52. {
  53. parent::__construct($buf, $dispatcher);
  54. $this->setExtensionHandlers($extensionHandlers);
  55. }
  56. /**
  57. * Set the host to connect to.
  58. * @param string $host
  59. */
  60. public function setHost($host)
  61. {
  62. $this->_params['host'] = $host;
  63. return $this;
  64. }
  65. /**
  66. * Get the host to connect to.
  67. * @return string
  68. */
  69. public function getHost()
  70. {
  71. return $this->_params['host'];
  72. }
  73. /**
  74. * Set the port to connect to.
  75. * @param int $port
  76. */
  77. public function setPort($port)
  78. {
  79. $this->_params['port'] = (int) $port;
  80. return $this;
  81. }
  82. /**
  83. * Get the port to connect to.
  84. * @return int
  85. */
  86. public function getPort()
  87. {
  88. return $this->_params['port'];
  89. }
  90. /**
  91. * Set the connection timeout.
  92. * @param int $timeout seconds
  93. */
  94. public function setTimeout($timeout)
  95. {
  96. $this->_params['timeout'] = (int) $timeout;
  97. $this->_buffer->setParam('timeout', (int) $timeout);
  98. return $this;
  99. }
  100. /**
  101. * Get the connection timeout.
  102. * @return int
  103. */
  104. public function getTimeout()
  105. {
  106. return $this->_params['timeout'];
  107. }
  108. /**
  109. * Set the encryption type (tls or ssl)
  110. * @param string $encryption
  111. */
  112. public function setEncryption($enc)
  113. {
  114. $this->_params['protocol'] = $enc;
  115. return $this;
  116. }
  117. /**
  118. * Get the encryption type.
  119. * @return string
  120. */
  121. public function getEncryption()
  122. {
  123. return $this->_params['protocol'];
  124. }
  125. /**
  126. * Sets the sourceIp
  127. * @param string $source
  128. */
  129. public function setSourceIp($source)
  130. {
  131. $this->_params['sourceIp']=$source;
  132. return $this;
  133. }
  134. /**
  135. * Returns the ip used to connect to the destination
  136. * @return string
  137. */
  138. public function getSourceIp()
  139. {
  140. return $this->_params['sourceIp'];
  141. }
  142. /**
  143. * Set ESMTP extension handlers.
  144. * @param Swift_Transport_EsmtpHandler[] $handlers
  145. */
  146. public function setExtensionHandlers(array $handlers)
  147. {
  148. $assoc = array();
  149. foreach ($handlers as $handler)
  150. {
  151. $assoc[$handler->getHandledKeyword()] = $handler;
  152. }
  153. uasort($assoc, array($this, '_sortHandlers'));
  154. $this->_handlers = $assoc;
  155. $this->_setHandlerParams();
  156. return $this;
  157. }
  158. /**
  159. * Get ESMTP extension handlers.
  160. * @return Swift_Transport_EsmtpHandler[]
  161. */
  162. public function getExtensionHandlers()
  163. {
  164. return array_values($this->_handlers);
  165. }
  166. /**
  167. * Run a command against the buffer, expecting the given response codes.
  168. * If no response codes are given, the response will not be validated.
  169. * If codes are given, an exception will be thrown on an invalid response.
  170. * @param string $command
  171. * @param int[] $codes
  172. * @param string[] &$failures
  173. * @return string
  174. */
  175. public function executeCommand($command, $codes = array(), &$failures = null)
  176. {
  177. $failures = (array) $failures;
  178. $stopSignal = false;
  179. $response = null;
  180. foreach ($this->_getActiveHandlers() as $handler)
  181. {
  182. $response = $handler->onCommand(
  183. $this, $command, $codes, $failures, $stopSignal
  184. );
  185. if ($stopSignal)
  186. {
  187. return $response;
  188. }
  189. }
  190. return parent::executeCommand($command, $codes, $failures);
  191. }
  192. // -- Mixin invocation code
  193. /** Mixin handling method for ESMTP handlers */
  194. public function __call($method, $args)
  195. {
  196. foreach ($this->_handlers as $handler)
  197. {
  198. if (in_array(strtolower($method),
  199. array_map('strtolower', (array) $handler->exposeMixinMethods())
  200. ))
  201. {
  202. $return = call_user_func_array(array($handler, $method), $args);
  203. //Allow fluid method calls
  204. if (is_null($return) && substr($method, 0, 3) == 'set')
  205. {
  206. return $this;
  207. }
  208. else
  209. {
  210. return $return;
  211. }
  212. }
  213. }
  214. trigger_error('Call to undefined method ' . $method, E_USER_ERROR);
  215. }
  216. // -- Protected methods
  217. /** Get the params to initialize the buffer */
  218. protected function _getBufferParams()
  219. {
  220. return $this->_params;
  221. }
  222. /** Overridden to perform EHLO instead */
  223. protected function _doHeloCommand()
  224. {
  225. try
  226. {
  227. $response = $this->executeCommand(
  228. sprintf("EHLO %s\r\n", $this->_domain), array(250)
  229. );
  230. }
  231. catch (Swift_TransportException $e)
  232. {
  233. return parent::_doHeloCommand();
  234. }
  235. $this->_capabilities = $this->_getCapabilities($response);
  236. $this->_setHandlerParams();
  237. foreach ($this->_getActiveHandlers() as $handler)
  238. {
  239. $handler->afterEhlo($this);
  240. }
  241. }
  242. /** Overridden to add Extension support */
  243. protected function _doMailFromCommand($address)
  244. {
  245. $handlers = $this->_getActiveHandlers();
  246. $params = array();
  247. foreach ($handlers as $handler)
  248. {
  249. $params = array_merge($params, (array) $handler->getMailParams());
  250. }
  251. $paramStr = !empty($params) ? ' ' . implode(' ', $params) : '';
  252. $this->executeCommand(
  253. sprintf("MAIL FROM: <%s>%s\r\n", $address, $paramStr), array(250)
  254. );
  255. }
  256. /** Overridden to add Extension support */
  257. protected function _doRcptToCommand($address)
  258. {
  259. $handlers = $this->_getActiveHandlers();
  260. $params = array();
  261. foreach ($handlers as $handler)
  262. {
  263. $params = array_merge($params, (array) $handler->getRcptParams());
  264. }
  265. $paramStr = !empty($params) ? ' ' . implode(' ', $params) : '';
  266. $this->executeCommand(
  267. sprintf("RCPT TO: <%s>%s\r\n", $address, $paramStr), array(250, 251, 252)
  268. );
  269. }
  270. // -- Private methods
  271. /** Determine ESMTP capabilities by function group */
  272. private function _getCapabilities($ehloResponse)
  273. {
  274. $capabilities = array();
  275. $ehloResponse = trim($ehloResponse);
  276. $lines = explode("\r\n", $ehloResponse);
  277. array_shift($lines);
  278. foreach ($lines as $line)
  279. {
  280. if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches))
  281. {
  282. $keyword = strtoupper($matches[1]);
  283. $paramStr = strtoupper(ltrim($matches[2], ' ='));
  284. $params = !empty($paramStr) ? explode(' ', $paramStr) : array();
  285. $capabilities[$keyword] = $params;
  286. }
  287. }
  288. return $capabilities;
  289. }
  290. /** Set parameters which are used by each extension handler */
  291. private function _setHandlerParams()
  292. {
  293. foreach ($this->_handlers as $keyword => $handler)
  294. {
  295. if (array_key_exists($keyword, $this->_capabilities))
  296. {
  297. $handler->setKeywordParams($this->_capabilities[$keyword]);
  298. }
  299. }
  300. }
  301. /** Get ESMTP handlers which are currently ok to use */
  302. private function _getActiveHandlers()
  303. {
  304. $handlers = array();
  305. foreach ($this->_handlers as $keyword => $handler)
  306. {
  307. if (array_key_exists($keyword, $this->_capabilities))
  308. {
  309. $handlers[] = $handler;
  310. }
  311. }
  312. return $handlers;
  313. }
  314. /** Custom sort for extension handler ordering */
  315. private function _sortHandlers($a, $b)
  316. {
  317. return $a->getPriorityOver($b->getHandledKeyword());
  318. }
  319. }