PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/PHPmailer/class.pop3.php

https://gitlab.com/vince.omega/ajax-form
PHP | 397 lines | 172 code | 31 blank | 194 comment | 21 complexity | e843907d7843688a242f41fdedfcab27 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPMailer POP-Before-SMTP Authentication Class.
  4. * PHP Version 5
  5. * @package PHPMailer
  6. * @link https://github.com/PHPMailer/PHPMailer/
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  10. * @author Brent R. Matzelle (original founder)
  11. * @copyright 2012 - 2014 Marcus Bointon
  12. * @copyright 2010 - 2012 Jim Jagielski
  13. * @copyright 2004 - 2009 Andy Prevost
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. /**
  20. * PHPMailer POP-Before-SMTP Authentication Class.
  21. * Specifically for PHPMailer to use for RFC1939 POP-before-SMTP authentication.
  22. * Does not support APOP.
  23. * @package PHPMailer
  24. * @author Richard Davey (original author) <rich@corephp.co.uk>
  25. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  26. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  27. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  28. */
  29. class POP3
  30. {
  31. /**
  32. * The POP3 PHPMailer Version number.
  33. * @type string
  34. * @access public
  35. */
  36. public $Version = '5.2.8';
  37. /**
  38. * Default POP3 port number.
  39. * @type integer
  40. * @access public
  41. */
  42. public $POP3_PORT = 110;
  43. /**
  44. * Default timeout in seconds.
  45. * @type integer
  46. * @access public
  47. */
  48. public $POP3_TIMEOUT = 30;
  49. /**
  50. * POP3 Carriage Return + Line Feed.
  51. * @type string
  52. * @access public
  53. * @deprecated Use the constant instead
  54. */
  55. public $CRLF = "\r\n";
  56. /**
  57. * Debug display level.
  58. * Options: 0 = no, 1+ = yes
  59. * @type integer
  60. * @access public
  61. */
  62. public $do_debug = 0;
  63. /**
  64. * POP3 mail server hostname.
  65. * @type string
  66. * @access public
  67. */
  68. public $host;
  69. /**
  70. * POP3 port number.
  71. * @type integer
  72. * @access public
  73. */
  74. public $port;
  75. /**
  76. * POP3 Timeout Value in seconds.
  77. * @type integer
  78. * @access public
  79. */
  80. public $tval;
  81. /**
  82. * POP3 username
  83. * @type string
  84. * @access public
  85. */
  86. public $username;
  87. /**
  88. * POP3 password.
  89. * @type string
  90. * @access public
  91. */
  92. public $password;
  93. /**
  94. * Resource handle for the POP3 connection socket.
  95. * @type resource
  96. * @access private
  97. */
  98. private $pop_conn;
  99. /**
  100. * Are we connected?
  101. * @type boolean
  102. * @access private
  103. */
  104. private $connected = false;
  105. /**
  106. * Error container.
  107. * @type array
  108. * @access private
  109. */
  110. private $errors = array();
  111. /**
  112. * Line break constant
  113. */
  114. const CRLF = "\r\n";
  115. /**
  116. * Simple static wrapper for all-in-one POP before SMTP
  117. * @param $host
  118. * @param boolean $port
  119. * @param boolean $tval
  120. * @param string $username
  121. * @param string $password
  122. * @param integer $debug_level
  123. * @return boolean
  124. */
  125. public static function popBeforeSmtp(
  126. $host,
  127. $port = false,
  128. $tval = false,
  129. $username = '',
  130. $password = '',
  131. $debug_level = 0
  132. ) {
  133. $pop = new POP3;
  134. return $pop->authorise($host, $port, $tval, $username, $password, $debug_level);
  135. }
  136. /**
  137. * Authenticate with a POP3 server.
  138. * A connect, login, disconnect sequence
  139. * appropriate for POP-before SMTP authorisation.
  140. * @access public
  141. * @param string $host The hostname to connect to
  142. * @param integer|boolean $port The port number to connect to
  143. * @param integer|boolean $timeout The timeout value
  144. * @param string $username
  145. * @param string $password
  146. * @param integer $debug_level
  147. * @return boolean
  148. */
  149. public function authorise($host, $port = false, $timeout = false, $username = '', $password = '', $debug_level = 0)
  150. {
  151. $this->host = $host;
  152. // If no port value provided, use default
  153. if ($port === false) {
  154. $this->port = $this->POP3_PORT;
  155. } else {
  156. $this->port = (integer)$port;
  157. }
  158. // If no timeout value provided, use default
  159. if ($timeout === false) {
  160. $this->tval = $this->POP3_TIMEOUT;
  161. } else {
  162. $this->tval = (integer)$timeout;
  163. }
  164. $this->do_debug = $debug_level;
  165. $this->username = $username;
  166. $this->password = $password;
  167. // Reset the error log
  168. $this->errors = array();
  169. // connect
  170. $result = $this->connect($this->host, $this->port, $this->tval);
  171. if ($result) {
  172. $login_result = $this->login($this->username, $this->password);
  173. if ($login_result) {
  174. $this->disconnect();
  175. return true;
  176. }
  177. }
  178. // We need to disconnect regardless of whether the login succeeded
  179. $this->disconnect();
  180. return false;
  181. }
  182. /**
  183. * Connect to a POP3 server.
  184. * @access public
  185. * @param string $host
  186. * @param integer|boolean $port
  187. * @param integer $tval
  188. * @return boolean
  189. */
  190. public function connect($host, $port = false, $tval = 30)
  191. {
  192. // Are we already connected?
  193. if ($this->connected) {
  194. return true;
  195. }
  196. //On Windows this will raise a PHP Warning error if the hostname doesn't exist.
  197. //Rather than suppress it with @fsockopen, capture it cleanly instead
  198. set_error_handler(array($this, 'catchWarning'));
  199. if ($port === false) {
  200. $port = $this->POP3_PORT;
  201. }
  202. // connect to the POP3 server
  203. $this->pop_conn = fsockopen(
  204. $host, // POP3 Host
  205. $port, // Port #
  206. $errno, // Error Number
  207. $errstr, // Error Message
  208. $tval
  209. ); // Timeout (seconds)
  210. // Restore the error handler
  211. restore_error_handler();
  212. // Did we connect?
  213. if ($this->pop_conn === false) {
  214. // It would appear not...
  215. $this->setError(array(
  216. 'error' => "Failed to connect to server $host on port $port",
  217. 'errno' => $errno,
  218. 'errstr' => $errstr
  219. ));
  220. return false;
  221. }
  222. // Increase the stream time-out
  223. stream_set_timeout($this->pop_conn, $tval, 0);
  224. // Get the POP3 server response
  225. $pop3_response = $this->getResponse();
  226. // Check for the +OK
  227. if ($this->checkResponse($pop3_response)) {
  228. // The connection is established and the POP3 server is talking
  229. $this->connected = true;
  230. return true;
  231. }
  232. return false;
  233. }
  234. /**
  235. * Log in to the POP3 server.
  236. * Does not support APOP (RFC 2828, 4949).
  237. * @access public
  238. * @param string $username
  239. * @param string $password
  240. * @return boolean
  241. */
  242. public function login($username = '', $password = '')
  243. {
  244. if (!$this->connected) {
  245. $this->setError('Not connected to POP3 server');
  246. }
  247. if (empty($username)) {
  248. $username = $this->username;
  249. }
  250. if (empty($password)) {
  251. $password = $this->password;
  252. }
  253. // Send the Username
  254. $this->sendString("USER $username" . self::CRLF);
  255. $pop3_response = $this->getResponse();
  256. if ($this->checkResponse($pop3_response)) {
  257. // Send the Password
  258. $this->sendString("PASS $password" . self::CRLF);
  259. $pop3_response = $this->getResponse();
  260. if ($this->checkResponse($pop3_response)) {
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. /**
  267. * Disconnect from the POP3 server.
  268. * @access public
  269. */
  270. public function disconnect()
  271. {
  272. $this->sendString('QUIT');
  273. //The QUIT command may cause the daemon to exit, which will kill our connection
  274. //So ignore errors here
  275. try {
  276. @fclose($this->pop_conn);
  277. } catch (Exception $e) {
  278. //Do nothing
  279. };
  280. }
  281. /**
  282. * Get a response from the POP3 server.
  283. * $size is the maximum number of bytes to retrieve
  284. * @param integer $size
  285. * @return string
  286. * @access private
  287. */
  288. private function getResponse($size = 128)
  289. {
  290. $response = fgets($this->pop_conn, $size);
  291. if ($this->do_debug >= 1) {
  292. echo "Server -> Client: $response";
  293. }
  294. return $response;
  295. }
  296. /**
  297. * Send raw data to the POP3 server.
  298. * @param string $string
  299. * @return integer
  300. * @access private
  301. */
  302. private function sendString($string)
  303. {
  304. if ($this->pop_conn) {
  305. if ($this->do_debug >= 2) { //Show client messages when debug >= 2
  306. echo "Client -> Server: $string";
  307. }
  308. return fwrite($this->pop_conn, $string, strlen($string));
  309. }
  310. return 0;
  311. }
  312. /**
  313. * Checks the POP3 server response.
  314. * Looks for for +OK or -ERR.
  315. * @param string $string
  316. * @return boolean
  317. * @access private
  318. */
  319. private function checkResponse($string)
  320. {
  321. if (substr($string, 0, 3) !== '+OK') {
  322. $this->setError(array(
  323. 'error' => "Server reported an error: $string",
  324. 'errno' => 0,
  325. 'errstr' => ''
  326. ));
  327. return false;
  328. } else {
  329. return true;
  330. }
  331. }
  332. /**
  333. * Add an error to the internal error store.
  334. * Also display debug output if it's enabled.
  335. * @param $error
  336. */
  337. private function setError($error)
  338. {
  339. $this->errors[] = $error;
  340. if ($this->do_debug >= 1) {
  341. echo '<pre>';
  342. foreach ($this->errors as $error) {
  343. print_r($error);
  344. }
  345. echo '</pre>';
  346. }
  347. }
  348. /**
  349. * POP3 connection error handler.
  350. * @param integer $errno
  351. * @param string $errstr
  352. * @param string $errfile
  353. * @param integer $errline
  354. * @access private
  355. */
  356. private function catchWarning($errno, $errstr, $errfile, $errline)
  357. {
  358. $this->setError(array(
  359. 'error' => "Connecting to the POP3 server raised a PHP warning: ",
  360. 'errno' => $errno,
  361. 'errstr' => $errstr,
  362. 'errfile' => $errfile,
  363. 'errline' => $errline
  364. ));
  365. }
  366. }