PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendors/phpmailer/class.pop3.php

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