PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/phpmailer/pop3.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 418 lines | 155 code | 64 blank | 199 comment | 28 complexity | eb605e83af9ece1ddb96aa1b4cd59a7f MD5 | raw file
  1. <?php
  2. /*~ class.pop3.php
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 5.2.2 |
  6. | Site: https://code.google.com/a/apache-extras.org/p/phpmailer/ |
  7. | ------------------------------------------------------------------------- |
  8. | Admin: Jim Jagielski (project admininistrator) |
  9. | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  10. | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net |
  11. | : Jim Jagielski (jimjag) jimjag@gmail.com |
  12. | Founder: Brent R. Matzelle (original founder) |
  13. | Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved. |
  14. | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
  15. | Copyright (c) 2001-2003, Brent R. Matzelle |
  16. | ------------------------------------------------------------------------- |
  17. | License: Distributed under the Lesser General Public License (LGPL) |
  18. | http://www.gnu.org/copyleft/lesser.html |
  19. | This program is distributed in the hope that it will be useful - WITHOUT |
  20. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  21. | FITNESS FOR A PARTICULAR PURPOSE. |
  22. '---------------------------------------------------------------------------'
  23. */
  24. /**
  25. * PHPMailer - PHP POP Before SMTP Authentication Class
  26. * NOTE: Designed for use with PHP version 5 and up
  27. * @package PHPMailer
  28. * @author Andy Prevost
  29. * @author Marcus Bointon
  30. * @author Jim Jagielski
  31. * @copyright 2010 - 2012 Jim Jagielski
  32. * @copyright 2004 - 2009 Andy Prevost
  33. * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
  34. */
  35. /**
  36. * PHP POP-Before-SMTP Authentication Class
  37. *
  38. * Version 5.2.2
  39. *
  40. * @license: LGPL, see PHPMailer License
  41. *
  42. * Specifically for PHPMailer to allow POP before SMTP authentication.
  43. * Does not yet work with APOP - if you have an APOP account, contact Jim Jagielski
  44. * and we can test changes to this script.
  45. *
  46. * This class is based on the structure of the SMTP class originally authored by Chris Ryan
  47. *
  48. * This class is rfc 1939 compliant and implements all the commands
  49. * required for POP3 connection, authentication and disconnection.
  50. *
  51. * @package PHPMailer
  52. * @author Richard Davey (orig) <rich@corephp.co.uk>
  53. * @author Andy Prevost
  54. * @author Jim Jagielski
  55. */
  56. class POP3 {
  57. /**
  58. * Default POP3 port
  59. * @var int
  60. */
  61. public $POP3_PORT = 110;
  62. /**
  63. * Default Timeout
  64. * @var int
  65. */
  66. public $POP3_TIMEOUT = 30;
  67. /**
  68. * POP3 Carriage Return + Line Feed
  69. * @var string
  70. */
  71. public $CRLF = "\r\n";
  72. /**
  73. * Displaying Debug warnings? (0 = now, 1+ = yes)
  74. * @var int
  75. */
  76. public $do_debug = 2;
  77. /**
  78. * POP3 Mail Server
  79. * @var string
  80. */
  81. public $host;
  82. /**
  83. * POP3 Port
  84. * @var int
  85. */
  86. public $port;
  87. /**
  88. * POP3 Timeout Value
  89. * @var int
  90. */
  91. public $tval;
  92. /**
  93. * POP3 Username
  94. * @var string
  95. */
  96. public $username;
  97. /**
  98. * POP3 Password
  99. * @var string
  100. */
  101. public $password;
  102. /**
  103. * Sets the POP3 PHPMailer Version number
  104. * @var string
  105. */
  106. public $Version = '5.2.2';
  107. /////////////////////////////////////////////////
  108. // PROPERTIES, PRIVATE AND PROTECTED
  109. /////////////////////////////////////////////////
  110. /**
  111. * @var resource Resource handle for the POP connection socket
  112. */
  113. private $pop_conn;
  114. /**
  115. * @var boolean Are we connected?
  116. */
  117. private $connected;
  118. /**
  119. * @var array Error container
  120. */
  121. private $error; // Error log array
  122. /**
  123. * Constructor, sets the initial values
  124. * @access public
  125. * @return POP3
  126. */
  127. public function __construct() {
  128. $this->pop_conn = 0;
  129. $this->connected = false;
  130. $this->error = null;
  131. }
  132. /**
  133. * Combination of public events - connect, login, disconnect
  134. * @access public
  135. * @param string $host
  136. * @param bool|int $port
  137. * @param bool|int $tval
  138. * @param string $username
  139. * @param string $password
  140. * @param int $debug_level
  141. * @return bool
  142. */
  143. public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
  144. $this->host = $host;
  145. // If no port value is passed, retrieve it
  146. if ($port == false) {
  147. $this->port = $this->POP3_PORT;
  148. } else {
  149. $this->port = $port;
  150. }
  151. // If no port value is passed, retrieve it
  152. if ($tval == false) {
  153. $this->tval = $this->POP3_TIMEOUT;
  154. } else {
  155. $this->tval = $tval;
  156. }
  157. $this->do_debug = $debug_level;
  158. $this->username = $username;
  159. $this->password = $password;
  160. // Refresh the error log
  161. $this->error = null;
  162. // Connect
  163. $result = $this->Connect($this->host, $this->port, $this->tval);
  164. if ($result) {
  165. $login_result = $this->Login($this->username, $this->password);
  166. if ($login_result) {
  167. $this->Disconnect();
  168. return true;
  169. }
  170. }
  171. // We need to disconnect regardless if the login succeeded
  172. $this->Disconnect();
  173. return false;
  174. }
  175. /**
  176. * Connect to the POP3 server
  177. * @access public
  178. * @param string $host
  179. * @param bool|int $port
  180. * @param integer $tval
  181. * @return boolean
  182. */
  183. public function Connect ($host, $port = false, $tval = 30) {
  184. // Are we already connected?
  185. if ($this->connected) {
  186. return true;
  187. }
  188. /*
  189. On Windows this will raise a PHP Warning error if the hostname doesn't exist.
  190. Rather than supress it with @fsockopen, let's capture it cleanly instead
  191. */
  192. set_error_handler(array(&$this, 'catchWarning'));
  193. // Connect to the POP3 server
  194. $this->pop_conn = fsockopen($host, // POP3 Host
  195. $port, // Port #
  196. $errno, // Error Number
  197. $errstr, // Error Message
  198. $tval); // Timeout (seconds)
  199. // Restore the error handler
  200. restore_error_handler();
  201. // Does the Error Log now contain anything?
  202. if ($this->error && $this->do_debug >= 1) {
  203. $this->displayErrors();
  204. }
  205. // Did we connect?
  206. if ($this->pop_conn == false) {
  207. // It would appear not...
  208. $this->error = array(
  209. 'error' => "Failed to connect to server $host on port $port",
  210. 'errno' => $errno,
  211. 'errstr' => $errstr
  212. );
  213. if ($this->do_debug >= 1) {
  214. $this->displayErrors();
  215. }
  216. return false;
  217. }
  218. // Increase the stream time-out
  219. // Check for PHP 4.3.0 or later
  220. if (version_compare(phpversion(), '5.0.0', 'ge')) {
  221. stream_set_timeout($this->pop_conn, $tval, 0);
  222. } else {
  223. // Does not work on Windows
  224. if (substr(PHP_OS, 0, 3) !== 'WIN') {
  225. socket_set_timeout($this->pop_conn, $tval, 0);
  226. }
  227. }
  228. // Get the POP3 server response
  229. $pop3_response = $this->getResponse();
  230. // Check for the +OK
  231. if ($this->checkResponse($pop3_response)) {
  232. // The connection is established and the POP3 server is talking
  233. $this->connected = true;
  234. return true;
  235. }
  236. return false;
  237. }
  238. /**
  239. * Login to the POP3 server (does not support APOP yet)
  240. * @access public
  241. * @param string $username
  242. * @param string $password
  243. * @return boolean
  244. */
  245. public function Login ($username = '', $password = '') {
  246. if ($this->connected == false) {
  247. $this->error = 'Not connected to POP3 server';
  248. if ($this->do_debug >= 1) {
  249. $this->displayErrors();
  250. }
  251. }
  252. if (empty($username)) {
  253. $username = $this->username;
  254. }
  255. if (empty($password)) {
  256. $password = $this->password;
  257. }
  258. $pop_username = "USER $username" . $this->CRLF;
  259. $pop_password = "PASS $password" . $this->CRLF;
  260. // Send the Username
  261. $this->sendString($pop_username);
  262. $pop3_response = $this->getResponse();
  263. if ($this->checkResponse($pop3_response)) {
  264. // Send the Password
  265. $this->sendString($pop_password);
  266. $pop3_response = $this->getResponse();
  267. if ($this->checkResponse($pop3_response)) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * Disconnect from the POP3 server
  275. * @access public
  276. */
  277. public function Disconnect () {
  278. $this->sendString('QUIT');
  279. fclose($this->pop_conn);
  280. }
  281. /////////////////////////////////////////////////
  282. // Private Methods
  283. /////////////////////////////////////////////////
  284. /**
  285. * Get the socket response back.
  286. * $size is the maximum number of bytes to retrieve
  287. * @access private
  288. * @param integer $size
  289. * @return string
  290. */
  291. private function getResponse ($size = 128) {
  292. $pop3_response = fgets($this->pop_conn, $size);
  293. return $pop3_response;
  294. }
  295. /**
  296. * Send a string down the open socket connection to the POP3 server
  297. * @access private
  298. * @param string $string
  299. * @return integer
  300. */
  301. private function sendString ($string) {
  302. $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
  303. return $bytes_sent;
  304. }
  305. /**
  306. * Checks the POP3 server response for +OK or -ERR
  307. * @access private
  308. * @param string $string
  309. * @return boolean
  310. */
  311. private function checkResponse ($string) {
  312. if (substr($string, 0, 3) !== '+OK') {
  313. $this->error = array(
  314. 'error' => "Server reported an error: $string",
  315. 'errno' => 0,
  316. 'errstr' => ''
  317. );
  318. if ($this->do_debug >= 1) {
  319. $this->displayErrors();
  320. }
  321. return false;
  322. } else {
  323. return true;
  324. }
  325. }
  326. /**
  327. * If debug is enabled, display the error message array
  328. * @access private
  329. */
  330. private function displayErrors () {
  331. echo '<pre>';
  332. foreach ($this->error as $single_error) {
  333. print_r($single_error);
  334. }
  335. echo '</pre>';
  336. }
  337. /**
  338. * Takes over from PHP for the socket warning handler
  339. * @access private
  340. * @param integer $errno
  341. * @param string $errstr
  342. * @param string $errfile
  343. * @param integer $errline
  344. */
  345. private function catchWarning ($errno, $errstr, $errfile, $errline) {
  346. $this->error[] = array(
  347. 'error' => "Connecting to the POP3 server raised a PHP warning: ",
  348. 'errno' => $errno,
  349. 'errstr' => $errstr
  350. );
  351. }
  352. // End of class
  353. }
  354. ?>