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

/library/swift/lib/Swift/Authenticator/POP3SMTP_.php

https://github.com/fb83/Project-Pier
PHP | 167 lines | 56 code | 12 blank | 99 comment | 11 complexity | 2a9cdcdecb0b1b2d6b74c4c868b30e6a MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * This is the POP3 Before SMTP Authentication for Swift Mailer, a PHP Mailer class.
  4. *
  5. * @package Swift
  6. * @version >= 2.0.0
  7. * @author Chris Corbyn
  8. * @date 30th July 2006
  9. * @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
  10. *
  11. * @copyright Copyright &copy; 2006 Chris Corbyn - All Rights Reserved.
  12. * @filesource
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to
  26. *
  27. * The Free Software Foundation, Inc.,
  28. * 51 Franklin Street,
  29. * Fifth Floor,
  30. * Boston,
  31. * MA 02110-1301 USA
  32. *
  33. * "Chris Corbyn" <chris@w3style.co.uk>
  34. *
  35. */
  36. /**
  37. * SMTP CRAM-MD5 Authenticator Class.
  38. * Runs the commands needed in order to use LOGIN SMTP authentication
  39. * @package Swift
  40. */
  41. class Swift_Authenticator_POP3SMTP implements Swift_IAuthenticator
  42. {
  43. /**
  44. * The string the SMTP server returns to identify
  45. * that it supports this authentication mechanism
  46. * @var string serverString
  47. */
  48. public $serverString = '*POP-SMTP';
  49. /**
  50. * SwiftInstance parent object
  51. * @var object SwiftInstance (reference)
  52. */
  53. protected $baseObject;
  54. /**
  55. * The port we need to connect to
  56. * @var int $port
  57. */
  58. protected $port;
  59. /**
  60. * The server we need to connect to
  61. * @var string server
  62. */
  63. protected $server;
  64. /**
  65. * The connection to POP3
  66. * @var resource connect handle
  67. */
  68. protected $socket;
  69. public function __construct($server, $port=110)
  70. {
  71. $this->port = $port;
  72. $this->server = $server;
  73. }
  74. /**
  75. * Loads an instance of Swift to the Plugin
  76. *
  77. * @param object SwiftInstance
  78. * @return void
  79. */
  80. public function loadBaseObject(&$object)
  81. {
  82. $this->baseObject =& $object;
  83. }
  84. /**
  85. * Executes the logic in the authentication mechanism
  86. *
  87. * @param string username
  88. * @param string password
  89. * @return bool successful
  90. */
  91. public function run($username, $password)
  92. {
  93. return $this->popB4SMTP($username, $password);
  94. }
  95. /**
  96. * Connect to the POP3 server and return true on success
  97. * @return bool success
  98. */
  99. protected function connect()
  100. {
  101. $this->socket = @fsockopen($this->server, $this->port, $errno, $errstr, 15);
  102. if (!$this->socket) return false;
  103. if (!$this->isOK($this->response())) return false;
  104. return true;
  105. }
  106. /**
  107. * Check for an +OK string
  108. * @return bool +OK
  109. */
  110. protected function isOK($string)
  111. {
  112. if (substr($string, 0, 3) == '+OK') return true;
  113. else return false;
  114. }
  115. /**
  116. * Send a command to the server
  117. */
  118. protected function command($comm)
  119. {
  120. @fwrite($this->socket, $comm);
  121. }
  122. /**
  123. * Read the server response
  124. * @return string response
  125. */
  126. protected function response()
  127. {
  128. return fgets($this->socket);
  129. }
  130. /**
  131. * Executes the logic in the authentication mechanism
  132. *
  133. * @param string username
  134. * @param string password
  135. * @return bool successful
  136. */
  137. protected function popB4SMTP($username, $password)
  138. {
  139. //Kill any open session so we can authenticate to POP3 first
  140. $this->baseObject->close();
  141. if (!$this->connect()) return false;
  142. $this->command("USER $username\r\n");
  143. if (!$this->isOK($this->response())) return false;
  144. $this->command("PASS $password\r\n");
  145. if (!$this->isOK($this->response())) return false;
  146. //START patch from Jakob Truelsen - antialize (SF.net)
  147. $this->command("QUIT\r\n");
  148. if (!$this->isOK($this->response())) return false;
  149. //END patch
  150. //Reconnect to the SMTP server
  151. if ($this->baseObject->connect()) return true;
  152. else return false;
  153. }
  154. }
  155. ?>