PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Bkt/Vendor/Phpmailer/class.pop3.php

https://bitbucket.org/xtalandier/bktframework
PHP | 412 lines | 157 code | 65 blank | 190 comment | 30 complexity | 43c2f8877787445113deb84a898e368f MD5 | raw file
Possible License(s): LGPL-2.1
  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. * POP Before SMTP Authentication Class
  37. * Version 5.2.2
  38. *
  39. * Orig Author: Richard Davey (rich@corephp.co.uk)
  40. * Modifications: Andy Prevost
  41. * Modifications: Jim Jagielski
  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 Jim Jagielski
  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 (orig)
  55. * @author Andy Prevost
  56. * @author Jim Jagielski
  57. */
  58. class POP3 {
  59. /**
  60. * Default POP3 port
  61. * @var int
  62. */
  63. public $POP3_PORT = 110;
  64. /**
  65. * Default Timeout
  66. * @var int
  67. */
  68. public $POP3_TIMEOUT = 30;
  69. /**
  70. * POP3 Carriage Return + Line Feed
  71. * @var string
  72. */
  73. public $CRLF = "\r\n";
  74. /**
  75. * Displaying Debug warnings? (0 = now, 1+ = yes)
  76. * @var int
  77. */
  78. public $do_debug = 2;
  79. /**
  80. * POP3 Mail Server
  81. * @var string
  82. */
  83. public $host;
  84. /**
  85. * POP3 Port
  86. * @var int
  87. */
  88. public $port;
  89. /**
  90. * POP3 Timeout Value
  91. * @var int
  92. */
  93. public $tval;
  94. /**
  95. * POP3 Username
  96. * @var string
  97. */
  98. public $username;
  99. /**
  100. * POP3 Password
  101. * @var string
  102. */
  103. public $password;
  104. /**
  105. * Sets the POP3 PHPMailer Version number
  106. * @var string
  107. */
  108. public $Version = '5.2.2-rc1';
  109. /////////////////////////////////////////////////
  110. // PROPERTIES, PRIVATE AND PROTECTED
  111. /////////////////////////////////////////////////
  112. private $pop_conn;
  113. private $connected;
  114. private $error; // Error log array
  115. /**
  116. * Constructor, sets the initial values
  117. * @access public
  118. * @return POP3
  119. */
  120. public function __construct() {
  121. $this->pop_conn = 0;
  122. $this->connected = false;
  123. $this->error = null;
  124. }
  125. /**
  126. * Combination of public events - connect, login, disconnect
  127. * @access public
  128. * @param string $host
  129. * @param integer $port
  130. * @param integer $tval
  131. * @param string $username
  132. * @param string $password
  133. */
  134. public function Authorise ($host, $port = false, $tval = false, $username, $password, $debug_level = 0) {
  135. $this->host = $host;
  136. // If no port value is passed, retrieve it
  137. if ($port == false) {
  138. $this->port = $this->POP3_PORT;
  139. } else {
  140. $this->port = $port;
  141. }
  142. // If no port value is passed, retrieve it
  143. if ($tval == false) {
  144. $this->tval = $this->POP3_TIMEOUT;
  145. } else {
  146. $this->tval = $tval;
  147. }
  148. $this->do_debug = $debug_level;
  149. $this->username = $username;
  150. $this->password = $password;
  151. // Refresh the error log
  152. $this->error = null;
  153. // Connect
  154. $result = $this->Connect($this->host, $this->port, $this->tval);
  155. if ($result) {
  156. $login_result = $this->Login($this->username, $this->password);
  157. if ($login_result) {
  158. $this->Disconnect();
  159. return true;
  160. }
  161. }
  162. // We need to disconnect regardless if the login succeeded
  163. $this->Disconnect();
  164. return false;
  165. }
  166. /**
  167. * Connect to the POP3 server
  168. * @access public
  169. * @param string $host
  170. * @param integer $port
  171. * @param integer $tval
  172. * @return boolean
  173. */
  174. public function Connect ($host, $port = false, $tval = 30) {
  175. // Are we already connected?
  176. if ($this->connected) {
  177. return true;
  178. }
  179. /*
  180. On Windows this will raise a PHP Warning error if the hostname doesn't exist.
  181. Rather than supress it with @fsockopen, let's capture it cleanly instead
  182. */
  183. set_error_handler(array(&$this, 'catchWarning'));
  184. // Connect to the POP3 server
  185. $this->pop_conn = fsockopen($host, // POP3 Host
  186. $port, // Port #
  187. $errno, // Error Number
  188. $errstr, // Error Message
  189. $tval); // Timeout (seconds)
  190. // Restore the error handler
  191. restore_error_handler();
  192. // Does the Error Log now contain anything?
  193. if ($this->error && $this->do_debug >= 1) {
  194. $this->displayErrors();
  195. }
  196. // Did we connect?
  197. if ($this->pop_conn == false) {
  198. // It would appear not...
  199. $this->error = array(
  200. 'error' => "Failed to connect to server $host on port $port",
  201. 'errno' => $errno,
  202. 'errstr' => $errstr
  203. );
  204. if ($this->do_debug >= 1) {
  205. $this->displayErrors();
  206. }
  207. return false;
  208. }
  209. // Increase the stream time-out
  210. // Check for PHP 4.3.0 or later
  211. if (version_compare(phpversion(), '5.0.0', 'ge')) {
  212. stream_set_timeout($this->pop_conn, $tval, 0);
  213. } else {
  214. // Does not work on Windows
  215. if (substr(PHP_OS, 0, 3) !== 'WIN') {
  216. socket_set_timeout($this->pop_conn, $tval, 0);
  217. }
  218. }
  219. // Get the POP3 server response
  220. $pop3_response = $this->getResponse();
  221. // Check for the +OK
  222. if ($this->checkResponse($pop3_response)) {
  223. // The connection is established and the POP3 server is talking
  224. $this->connected = true;
  225. return true;
  226. }
  227. }
  228. /**
  229. * Login to the POP3 server (does not support APOP yet)
  230. * @access public
  231. * @param string $username
  232. * @param string $password
  233. * @return boolean
  234. */
  235. public function Login ($username = '', $password = '') {
  236. if ($this->connected == false) {
  237. $this->error = 'Not connected to POP3 server';
  238. if ($this->do_debug >= 1) {
  239. $this->displayErrors();
  240. }
  241. }
  242. if (empty($username)) {
  243. $username = $this->username;
  244. }
  245. if (empty($password)) {
  246. $password = $this->password;
  247. }
  248. $pop_username = "USER $username" . $this->CRLF;
  249. $pop_password = "PASS $password" . $this->CRLF;
  250. // Send the Username
  251. $this->sendString($pop_username);
  252. $pop3_response = $this->getResponse();
  253. if ($this->checkResponse($pop3_response)) {
  254. // Send the Password
  255. $this->sendString($pop_password);
  256. $pop3_response = $this->getResponse();
  257. if ($this->checkResponse($pop3_response)) {
  258. return true;
  259. } else {
  260. return false;
  261. }
  262. } else {
  263. return false;
  264. }
  265. }
  266. /**
  267. * Disconnect from the POP3 server
  268. * @access public
  269. */
  270. public function Disconnect () {
  271. $this->sendString('QUIT');
  272. fclose($this->pop_conn);
  273. }
  274. /////////////////////////////////////////////////
  275. // Private Methods
  276. /////////////////////////////////////////////////
  277. /**
  278. * Get the socket response back.
  279. * $size is the maximum number of bytes to retrieve
  280. * @access private
  281. * @param integer $size
  282. * @return string
  283. */
  284. private function getResponse ($size = 128) {
  285. $pop3_response = fgets($this->pop_conn, $size);
  286. return $pop3_response;
  287. }
  288. /**
  289. * Send a string down the open socket connection to the POP3 server
  290. * @access private
  291. * @param string $string
  292. * @return integer
  293. */
  294. private function sendString ($string) {
  295. $bytes_sent = fwrite($this->pop_conn, $string, strlen($string));
  296. return $bytes_sent;
  297. }
  298. /**
  299. * Checks the POP3 server response for +OK or -ERR
  300. * @access private
  301. * @param string $string
  302. * @return boolean
  303. */
  304. private function checkResponse ($string) {
  305. if (substr($string, 0, 3) !== '+OK') {
  306. $this->error = array(
  307. 'error' => "Server reported an error: $string",
  308. 'errno' => 0,
  309. 'errstr' => ''
  310. );
  311. if ($this->do_debug >= 1) {
  312. $this->displayErrors();
  313. }
  314. return false;
  315. } else {
  316. return true;
  317. }
  318. }
  319. /**
  320. * If debug is enabled, display the error message array
  321. * @access private
  322. */
  323. private function displayErrors () {
  324. echo '<pre>';
  325. foreach ($this->error as $single_error) {
  326. print_r($single_error);
  327. }
  328. echo '</pre>';
  329. }
  330. /**
  331. * Takes over from PHP for the socket warning handler
  332. * @access private
  333. * @param integer $errno
  334. * @param string $errstr
  335. * @param string $errfile
  336. * @param integer $errline
  337. */
  338. private function catchWarning ($errno, $errstr, $errfile, $errline) {
  339. $this->error[] = array(
  340. 'error' => "Connecting to the POP3 server raised a PHP warning: ",
  341. 'errno' => $errno,
  342. 'errstr' => $errstr
  343. );
  344. }
  345. // End of class
  346. }
  347. ?>