PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/mailers/class.pop3.php

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