PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpmailer/class.pop3.php

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