PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/user/plugins/email/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php

https://gitlab.com/3dplex/3d-plex-main-site
PHP | 483 lines | 270 code | 49 blank | 164 comment | 14 complexity | 453e589eda8679a09e07d745e2ebeb53 MD5 | raw file
  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. *
  12. <<<<<<< HEAD
  13. * @package Swift
  14. * @subpackage Transport
  15. * @author Chris Corbyn
  16. =======
  17. * @author Chris Corbyn
  18. >>>>>>> update grav cms
  19. */
  20. class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTransport implements Swift_Transport_SmtpAgent
  21. {
  22. /**
  23. * ESMTP extension handlers.
  24. *
  25. * @var Swift_Transport_EsmtpHandler[]
  26. */
  27. private $_handlers = array();
  28. /**
  29. * ESMTP capabilities.
  30. *
  31. * @var string[]
  32. */
  33. private $_capabilities = array();
  34. /**
  35. * Connection buffer parameters.
  36. *
  37. * @var array
  38. */
  39. private $_params = array(
  40. 'protocol' => 'tcp',
  41. 'host' => 'localhost',
  42. 'port' => 25,
  43. 'timeout' => 30,
  44. 'blocking' => 1,
  45. 'tls' => false,
  46. <<<<<<< HEAD
  47. 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET
  48. =======
  49. 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET,
  50. 'stream_context_options' => array(),
  51. >>>>>>> update grav cms
  52. );
  53. /**
  54. * Creates a new EsmtpTransport using the given I/O buffer.
  55. *
  56. * @param Swift_Transport_IoBuffer $buf
  57. * @param Swift_Transport_EsmtpHandler[] $extensionHandlers
  58. * @param Swift_Events_EventDispatcher $dispatcher
  59. */
  60. public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher)
  61. {
  62. parent::__construct($buf, $dispatcher);
  63. $this->setExtensionHandlers($extensionHandlers);
  64. }
  65. /**
  66. * Set the host to connect to.
  67. *
  68. * @param string $host
  69. *
  70. * @return Swift_Transport_EsmtpTransport
  71. */
  72. public function setHost($host)
  73. {
  74. $this->_params['host'] = $host;
  75. return $this;
  76. }
  77. /**
  78. * Get the host to connect to.
  79. *
  80. * @return string
  81. */
  82. public function getHost()
  83. {
  84. return $this->_params['host'];
  85. }
  86. /**
  87. * Set the port to connect to.
  88. *
  89. <<<<<<< HEAD
  90. * @param integer $port
  91. =======
  92. * @param int $port
  93. >>>>>>> update grav cms
  94. *
  95. * @return Swift_Transport_EsmtpTransport
  96. */
  97. public function setPort($port)
  98. {
  99. $this->_params['port'] = (int) $port;
  100. return $this;
  101. }
  102. /**
  103. * Get the port to connect to.
  104. *
  105. * @return int
  106. */
  107. public function getPort()
  108. {
  109. return $this->_params['port'];
  110. }
  111. /**
  112. * Set the connection timeout.
  113. *
  114. <<<<<<< HEAD
  115. * @param integer $timeout seconds
  116. =======
  117. * @param int $timeout seconds
  118. >>>>>>> update grav cms
  119. *
  120. * @return Swift_Transport_EsmtpTransport
  121. */
  122. public function setTimeout($timeout)
  123. {
  124. $this->_params['timeout'] = (int) $timeout;
  125. $this->_buffer->setParam('timeout', (int) $timeout);
  126. return $this;
  127. }
  128. /**
  129. * Get the connection timeout.
  130. *
  131. * @return int
  132. */
  133. public function getTimeout()
  134. {
  135. return $this->_params['timeout'];
  136. }
  137. /**
  138. <<<<<<< HEAD
  139. * Set the encryption type (tls or ssl)
  140. =======
  141. * Set the encryption type (tls or ssl).
  142. >>>>>>> update grav cms
  143. *
  144. * @param string $encryption
  145. *
  146. * @return Swift_Transport_EsmtpTransport
  147. */
  148. public function setEncryption($encryption)
  149. {
  150. <<<<<<< HEAD
  151. =======
  152. $encryption = strtolower($encryption);
  153. >>>>>>> update grav cms
  154. if ('tls' == $encryption) {
  155. $this->_params['protocol'] = 'tcp';
  156. $this->_params['tls'] = true;
  157. } else {
  158. $this->_params['protocol'] = $encryption;
  159. $this->_params['tls'] = false;
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Get the encryption type.
  165. *
  166. * @return string
  167. */
  168. public function getEncryption()
  169. {
  170. return $this->_params['tls'] ? 'tls' : $this->_params['protocol'];
  171. }
  172. /**
  173. <<<<<<< HEAD
  174. =======
  175. * Sets the stream context options.
  176. *
  177. * @param array $options
  178. *
  179. * @return Swift_Transport_EsmtpTransport
  180. */
  181. public function setStreamOptions($options)
  182. {
  183. $this->_params['stream_context_options'] = $options;
  184. return $this;
  185. }
  186. /**
  187. * Returns the stream context options.
  188. *
  189. * @return array
  190. */
  191. public function getStreamOptions()
  192. {
  193. return $this->_params['stream_context_options'];
  194. }
  195. /**
  196. >>>>>>> update grav cms
  197. * Sets the source IP.
  198. *
  199. * @param string $source
  200. *
  201. * @return Swift_Transport_EsmtpTransport
  202. */
  203. public function setSourceIp($source)
  204. {
  205. <<<<<<< HEAD
  206. $this->_params['sourceIp']=$source;
  207. =======
  208. $this->_params['sourceIp'] = $source;
  209. >>>>>>> update grav cms
  210. return $this;
  211. }
  212. /**
  213. * Returns the IP used to connect to the destination.
  214. *
  215. * @return string
  216. */
  217. public function getSourceIp()
  218. {
  219. <<<<<<< HEAD
  220. return $this->_params['sourceIp'];
  221. =======
  222. return isset($this->_params['sourceIp']) ? $this->_params['sourceIp'] : null;
  223. >>>>>>> update grav cms
  224. }
  225. /**
  226. * Set ESMTP extension handlers.
  227. *
  228. * @param Swift_Transport_EsmtpHandler[] $handlers
  229. *
  230. * @return Swift_Transport_EsmtpTransport
  231. */
  232. public function setExtensionHandlers(array $handlers)
  233. {
  234. $assoc = array();
  235. foreach ($handlers as $handler) {
  236. $assoc[$handler->getHandledKeyword()] = $handler;
  237. }
  238. <<<<<<< HEAD
  239. uasort($assoc, array($this, '_sortHandlers'));
  240. =======
  241. @uasort($assoc, array($this, '_sortHandlers'));
  242. >>>>>>> update grav cms
  243. $this->_handlers = $assoc;
  244. $this->_setHandlerParams();
  245. return $this;
  246. }
  247. /**
  248. * Get ESMTP extension handlers.
  249. *
  250. * @return Swift_Transport_EsmtpHandler[]
  251. */
  252. public function getExtensionHandlers()
  253. {
  254. return array_values($this->_handlers);
  255. }
  256. /**
  257. * Run a command against the buffer, expecting the given response codes.
  258. *
  259. * If no response codes are given, the response will not be validated.
  260. * If codes are given, an exception will be thrown on an invalid response.
  261. *
  262. * @param string $command
  263. * @param int[] $codes
  264. * @param string[] $failures An array of failures by-reference
  265. *
  266. * @return string
  267. */
  268. public function executeCommand($command, $codes = array(), &$failures = null)
  269. {
  270. $failures = (array) $failures;
  271. $stopSignal = false;
  272. $response = null;
  273. foreach ($this->_getActiveHandlers() as $handler) {
  274. $response = $handler->onCommand(
  275. $this, $command, $codes, $failures, $stopSignal
  276. );
  277. if ($stopSignal) {
  278. return $response;
  279. }
  280. }
  281. return parent::executeCommand($command, $codes, $failures);
  282. }
  283. // -- Mixin invocation code
  284. /** Mixin handling method for ESMTP handlers */
  285. public function __call($method, $args)
  286. {
  287. foreach ($this->_handlers as $handler) {
  288. if (in_array(strtolower($method),
  289. array_map('strtolower', (array) $handler->exposeMixinMethods())
  290. <<<<<<< HEAD
  291. ))
  292. {
  293. =======
  294. )) {
  295. >>>>>>> update grav cms
  296. $return = call_user_func_array(array($handler, $method), $args);
  297. // Allow fluid method calls
  298. if (is_null($return) && substr($method, 0, 3) == 'set') {
  299. return $this;
  300. } else {
  301. return $return;
  302. }
  303. }
  304. }
  305. <<<<<<< HEAD
  306. trigger_error('Call to undefined method ' . $method, E_USER_ERROR);
  307. }
  308. // -- Protected methods
  309. =======
  310. trigger_error('Call to undefined method '.$method, E_USER_ERROR);
  311. }
  312. >>>>>>> update grav cms
  313. /** Get the params to initialize the buffer */
  314. protected function _getBufferParams()
  315. {
  316. return $this->_params;
  317. }
  318. /** Overridden to perform EHLO instead */
  319. protected function _doHeloCommand()
  320. {
  321. try {
  322. $response = $this->executeCommand(
  323. sprintf("EHLO %s\r\n", $this->_domain), array(250)
  324. );
  325. } catch (Swift_TransportException $e) {
  326. return parent::_doHeloCommand();
  327. }
  328. if ($this->_params['tls']) {
  329. try {
  330. $this->executeCommand("STARTTLS\r\n", array(220));
  331. if (!$this->_buffer->startTLS()) {
  332. throw new Swift_TransportException('Unable to connect with TLS encryption');
  333. }
  334. try {
  335. $response = $this->executeCommand(
  336. sprintf("EHLO %s\r\n", $this->_domain), array(250)
  337. );
  338. } catch (Swift_TransportException $e) {
  339. return parent::_doHeloCommand();
  340. }
  341. } catch (Swift_TransportException $e) {
  342. $this->_throwException($e);
  343. }
  344. }
  345. $this->_capabilities = $this->_getCapabilities($response);
  346. $this->_setHandlerParams();
  347. foreach ($this->_getActiveHandlers() as $handler) {
  348. $handler->afterEhlo($this);
  349. }
  350. }
  351. /** Overridden to add Extension support */
  352. protected function _doMailFromCommand($address)
  353. {
  354. $handlers = $this->_getActiveHandlers();
  355. $params = array();
  356. foreach ($handlers as $handler) {
  357. $params = array_merge($params, (array) $handler->getMailParams());
  358. }
  359. <<<<<<< HEAD
  360. $paramStr = !empty($params) ? ' ' . implode(' ', $params) : '';
  361. $this->executeCommand(
  362. sprintf("MAIL FROM: <%s>%s\r\n", $address, $paramStr), array(250)
  363. =======
  364. $paramStr = !empty($params) ? ' '.implode(' ', $params) : '';
  365. $this->executeCommand(
  366. sprintf("MAIL FROM:<%s>%s\r\n", $address, $paramStr), array(250)
  367. >>>>>>> update grav cms
  368. );
  369. }
  370. /** Overridden to add Extension support */
  371. protected function _doRcptToCommand($address)
  372. {
  373. $handlers = $this->_getActiveHandlers();
  374. $params = array();
  375. foreach ($handlers as $handler) {
  376. $params = array_merge($params, (array) $handler->getRcptParams());
  377. }
  378. <<<<<<< HEAD
  379. $paramStr = !empty($params) ? ' ' . implode(' ', $params) : '';
  380. $this->executeCommand(
  381. sprintf("RCPT TO: <%s>%s\r\n", $address, $paramStr), array(250, 251, 252)
  382. );
  383. }
  384. // -- Private methods
  385. =======
  386. $paramStr = !empty($params) ? ' '.implode(' ', $params) : '';
  387. $this->executeCommand(
  388. sprintf("RCPT TO:<%s>%s\r\n", $address, $paramStr), array(250, 251, 252)
  389. );
  390. }
  391. >>>>>>> update grav cms
  392. /** Determine ESMTP capabilities by function group */
  393. private function _getCapabilities($ehloResponse)
  394. {
  395. $capabilities = array();
  396. $ehloResponse = trim($ehloResponse);
  397. $lines = explode("\r\n", $ehloResponse);
  398. array_shift($lines);
  399. foreach ($lines as $line) {
  400. if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches)) {
  401. $keyword = strtoupper($matches[1]);
  402. $paramStr = strtoupper(ltrim($matches[2], ' ='));
  403. $params = !empty($paramStr) ? explode(' ', $paramStr) : array();
  404. $capabilities[$keyword] = $params;
  405. }
  406. }
  407. return $capabilities;
  408. }
  409. /** Set parameters which are used by each extension handler */
  410. private function _setHandlerParams()
  411. {
  412. foreach ($this->_handlers as $keyword => $handler) {
  413. if (array_key_exists($keyword, $this->_capabilities)) {
  414. $handler->setKeywordParams($this->_capabilities[$keyword]);
  415. }
  416. }
  417. }
  418. /** Get ESMTP handlers which are currently ok to use */
  419. private function _getActiveHandlers()
  420. {
  421. $handlers = array();
  422. foreach ($this->_handlers as $keyword => $handler) {
  423. if (array_key_exists($keyword, $this->_capabilities)) {
  424. $handlers[] = $handler;
  425. }
  426. }
  427. return $handlers;
  428. }
  429. /** Custom sort for extension handler ordering */
  430. private function _sortHandlers($a, $b)
  431. {
  432. return $a->getPriorityOver($b->getHandledKeyword());
  433. }
  434. }