PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/application/vendor/phpmailer/class.pop3.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 410 lines | 157 code | 65 blank | 188 comment | 30 complexity | 078ffe074e174265a1d4a9aea7b2168e MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php
  2. /*~ class.pop3.php
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 5.2.1 |
  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. * @version $Id: class.pop3.php 450 2010-06-23 16:46:33Z coolbru $
  35. */
  36. /**
  37. * POP Before SMTP Authentication Class
  38. * Version 5.2.1
  39. *
  40. * Author: Richard Davey (rich@corephp.co.uk)
  41. * Modifications: Andy Prevost
  42. * License: LGPL, see PHPMailer License
  43. *
  44. * Specifically for PHPMailer to allow POP before SMTP authentication.
  45. * Does not yet work with APOP - if you have an APOP account, contact Richard Davey
  46. * and we can test changes to this script.
  47. *
  48. * This class is based on the structure of the SMTP class originally authored by Chris Ryan
  49. *
  50. * This class is rfc 1939 compliant and implements all the commands
  51. * required for POP3 connection, authentication and disconnection.
  52. *
  53. * @package PHPMailer
  54. * @author Richard Davey
  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.1';
  107. /////////////////////////////////////////////////
  108. // PROPERTIES, PRIVATE AND PROTECTED
  109. /////////////////////////////////////////////////
  110. private $pop_conn;
  111. private $connected;
  112. private $error; // Error log array
  113. /**
  114. * Constructor, sets the initial values
  115. * @access public
  116. * @return POP3
  117. */
  118. public function __construct() {
  119. $this->pop_conn = 0;
  120. $this->connected = false;
  121. $this->error = null;
  122. }
  123. /**
  124. * Combination of public events - connect, login, disconnect
  125. * @access public
  126. * @param string $host
  127. * @param integer $port
  128. * @param integer $tval
  129. * @param string $username
  130. * @param string $password
  131. */
  132. public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
  133. $this->host = $host;
  134. // If no port value is passed, retrieve it
  135. if ($port == false) {
  136. $this->port = $this->POP3_PORT;
  137. } else {
  138. $this->port = $port;
  139. }
  140. // If no port value is passed, retrieve it
  141. if ($tval == false) {
  142. $this->tval = $this->POP3_TIMEOUT;
  143. } else {
  144. $this->tval = $tval;
  145. }
  146. $this->do_debug = $debug_level;
  147. $this->username = $username;
  148. $this->password = $password;
  149. // Refresh the error log
  150. $this->error = null;
  151. // Connect
  152. $result = $this->Connect($this->host, $this->port, $this->tval);
  153. if ($result) {
  154. $login_result = $this->Login($this->username, $this->password);
  155. if ($login_result) {
  156. $this->Disconnect();
  157. return true;
  158. }
  159. }
  160. // We need to disconnect regardless if the login succeeded
  161. $this->Disconnect();
  162. return false;
  163. }
  164. /**
  165. * Connect to the POP3 server
  166. * @access public
  167. * @param string $host
  168. * @param integer $port
  169. * @param integer $tval
  170. * @return boolean
  171. */
  172. public function Connect ($host, $port = false, $tval = 30) {
  173. // Are we already connected?
  174. if ($this->connected) {
  175. return true;
  176. }
  177. /*
  178. On Windows this will raise a PHP Warning error if the hostname doesn't exist.
  179. Rather than supress it with @fsockopen, let's capture it cleanly instead
  180. */
  181. set_error_handler(array(&$this, 'catchWarning'));
  182. // Connect to the POP3 server
  183. $this->pop_conn = fsockopen($host, // POP3 Host
  184. $port, // Port #
  185. $errno, // Error Number
  186. $errstr, // Error Message
  187. $tval); // Timeout (seconds)
  188. // Restore the error handler
  189. restore_error_handler();
  190. // Does the Error Log now contain anything?
  191. if ($this->error && $this->do_debug >= 1) {
  192. $this->displayErrors();
  193. }
  194. // Did we connect?
  195. if ($this->pop_conn == false) {
  196. // It would appear not...
  197. $this->error = array(
  198. 'error' => "Failed to connect to server $host on port $port",
  199. 'errno' => $errno,
  200. 'errstr' => $errstr
  201. );
  202. if ($this->do_debug >= 1) {
  203. $this->displayErrors();
  204. }
  205. return false;
  206. }
  207. // Increase the stream time-out
  208. // Check for PHP 4.3.0 or later
  209. if (version_compare(phpversion(), '5.0.0', 'ge')) {
  210. stream_set_timeout($this->pop_conn, $tval, 0);
  211. } else {
  212. // Does not work on Windows
  213. if (substr(PHP_OS, 0, 3) !== 'WIN') {
  214. socket_set_timeout($this->pop_conn, $tval, 0);
  215. }
  216. }
  217. // Get the POP3 server response
  218. $pop3_response = $this->getResponse();
  219. // Check for the +OK
  220. if ($this->checkResponse($pop3_response)) {
  221. // The connection is established and the POP3 server is talking
  222. $this->connected = true;
  223. return true;
  224. }
  225. }
  226. /**
  227. * Login to the POP3 server (does not support APOP yet)
  228. * @access public
  229. * @param string $username
  230. * @param string $password
  231. * @return boolean
  232. */
  233. public function Login ($username = '', $password = '') {
  234. if ($this->connected == false) {
  235. $this->error = 'Not connected to POP3 server';
  236. if ($this->do_debug >= 1) {
  237. $this->displayErrors();
  238. }
  239. }
  240. if (empty($username)) {
  241. $username = $this->username;
  242. }
  243. if (empty($password)) {
  244. $password = $this->password;
  245. }
  246. $pop_username = "USER $username" . $this->CRLF;
  247. $pop_password = "PASS $password" . $this->CRLF;
  248. // Send the Username
  249. $this->sendString($pop_username);
  250. $pop3_response = $this->getResponse();
  251. if ($this->checkResponse($pop3_response)) {
  252. // Send the Password
  253. $this->sendString($pop_password);
  254. $pop3_response = $this->getResponse();
  255. if ($this->checkResponse($pop3_response)) {
  256. return true;
  257. } else {
  258. return false;
  259. }
  260. } else {
  261. return false;
  262. }
  263. }
  264. /**
  265. * Disconnect from the POP3 server
  266. * @access public
  267. */
  268. public function Disconnect () {
  269. $this->sendString('QUIT');
  270. fclose($this->pop_conn);
  271. }
  272. /////////////////////////////////////////////////
  273. // Private Methods
  274. /////////////////////////////////////////////////
  275. /**
  276. * Get the socket response back.
  277. * $size is the maximum number of bytes to retrieve
  278. * @access private
  279. * @param integer $size
  280. * @return string
  281. */
  282. private function getResponse ($size = 128) {
  283. $pop3_response = fgets($this->pop_conn, $size);
  284. return $pop3_response;
  285. }
  286. /**
  287. * Send a string down the open socket connection to the POP3 server
  288. * @access private
  289. * @param string $string
  290. * @return integer
  291. */
  292. private function sendString ($string) {
  293. $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
  294. return $bytes_sent;
  295. }
  296. /**
  297. * Checks the POP3 server response for +OK or -ERR
  298. * @access private
  299. * @param string $string
  300. * @return boolean
  301. */
  302. private function checkResponse ($string) {
  303. if (substr($string, 0, 3) !== '+OK') {
  304. $this->error = array(
  305. 'error' => "Server reported an error: $string",
  306. 'errno' => 0,
  307. 'errstr' => ''
  308. );
  309. if ($this->do_debug >= 1) {
  310. $this->displayErrors();
  311. }
  312. return false;
  313. } else {
  314. return true;
  315. }
  316. }
  317. /**
  318. * If debug is enabled, display the error message array
  319. * @access private
  320. */
  321. private function displayErrors () {
  322. echo '<pre>';
  323. foreach ($this->error as $single_error) {
  324. print_r($single_error);
  325. }
  326. echo '</pre>';
  327. }
  328. /**
  329. * Takes over from PHP for the socket warning handler
  330. * @access private
  331. * @param integer $errno
  332. * @param string $errstr
  333. * @param string $errfile
  334. * @param integer $errline
  335. */
  336. private function catchWarning ($errno, $errstr, $errfile, $errline) {
  337. $this->error[] = array(
  338. 'error' => "Connecting to the POP3 server raised a PHP warning: ",
  339. 'errno' => $errno,
  340. 'errstr' => $errstr
  341. );
  342. }
  343. // End of class
  344. }
  345. ?>