PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/class-pop3.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 652 lines | 483 code | 79 blank | 90 comment | 99 complexity | f56e9bb9daca2b93a455f48aa3753acc MD5 | raw file
  1. <?php
  2. /**
  3. * mail_fetch/setup.php
  4. *
  5. * @package SquirrelMail
  6. *
  7. * @copyright (c) 1999-2006 The SquirrelMail Project Team
  8. *
  9. * @copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
  10. * Modified by Philippe Mingo 2001 mingo@rotedic.com
  11. * An RFC 1939 compliant wrapper class for the POP3 protocol.
  12. *
  13. * Licensed under the GNU GPL. For full terms see the file COPYING.
  14. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
  15. *
  16. * pop3 class
  17. *
  18. * $Id: class-pop3.php 17435 2011-02-09 17:35:36Z ryan $
  19. */
  20. class POP3 {
  21. var $ERROR = ''; // Error string.
  22. var $TIMEOUT = 60; // Default timeout before giving up on a
  23. // network operation.
  24. var $COUNT = -1; // Mailbox msg count
  25. var $BUFFER = 512; // Socket buffer for socket fgets() calls.
  26. // Per RFC 1939 the returned line a POP3
  27. // server can send is 512 bytes.
  28. var $FP = ''; // The connection to the server's
  29. // file descriptor
  30. var $MAILSERVER = ''; // Set this to hard code the server name
  31. var $DEBUG = FALSE; // set to true to echo pop3
  32. // commands and responses to error_log
  33. // this WILL log passwords!
  34. var $BANNER = ''; // Holds the banner returned by the
  35. // pop server - used for apop()
  36. var $ALLOWAPOP = FALSE; // Allow or disallow apop()
  37. // This must be set to true
  38. // manually
  39. function POP3 ( $server = '', $timeout = '' ) {
  40. settype($this->BUFFER,"integer");
  41. if( !empty($server) ) {
  42. // Do not allow programs to alter MAILSERVER
  43. // if it is already specified. They can get around
  44. // this if they -really- want to, so don't count on it.
  45. if(empty($this->MAILSERVER))
  46. $this->MAILSERVER = $server;
  47. }
  48. if(!empty($timeout)) {
  49. settype($timeout,"integer");
  50. $this->TIMEOUT = $timeout;
  51. if (!ini_get('safe_mode'))
  52. set_time_limit($timeout);
  53. }
  54. return true;
  55. }
  56. function update_timer () {
  57. if (!ini_get('safe_mode'))
  58. set_time_limit($this->TIMEOUT);
  59. return true;
  60. }
  61. function connect ($server, $port = 110) {
  62. // Opens a socket to the specified server. Unless overridden,
  63. // port defaults to 110. Returns true on success, false on fail
  64. // If MAILSERVER is set, override $server with it's value
  65. if (!isset($port) || !$port) {$port = 110;}
  66. if(!empty($this->MAILSERVER))
  67. $server = $this->MAILSERVER;
  68. if(empty($server)){
  69. $this->ERROR = "POP3 connect: " . _("No server specified");
  70. unset($this->FP);
  71. return false;
  72. }
  73. $fp = @fsockopen("$server", $port, $errno, $errstr);
  74. if(!$fp) {
  75. $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
  76. unset($this->FP);
  77. return false;
  78. }
  79. socket_set_blocking($fp,-1);
  80. $this->update_timer();
  81. $reply = fgets($fp,$this->BUFFER);
  82. $reply = $this->strip_clf($reply);
  83. if($this->DEBUG)
  84. error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
  85. if(!$this->is_ok($reply)) {
  86. $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
  87. unset($this->FP);
  88. return false;
  89. }
  90. $this->FP = $fp;
  91. $this->BANNER = $this->parse_banner($reply);
  92. return true;
  93. }
  94. function user ($user = "") {
  95. // Sends the USER command, returns true or false
  96. if( empty($user) ) {
  97. $this->ERROR = "POP3 user: " . _("no login ID submitted");
  98. return false;
  99. } elseif(!isset($this->FP)) {
  100. $this->ERROR = "POP3 user: " . _("connection not established");
  101. return false;
  102. } else {
  103. $reply = $this->send_cmd("USER $user");
  104. if(!$this->is_ok($reply)) {
  105. $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
  106. return false;
  107. } else
  108. return true;
  109. }
  110. }
  111. function pass ($pass = "") {
  112. // Sends the PASS command, returns # of msgs in mailbox,
  113. // returns false (undef) on Auth failure
  114. if(empty($pass)) {
  115. $this->ERROR = "POP3 pass: " . _("No password submitted");
  116. return false;
  117. } elseif(!isset($this->FP)) {
  118. $this->ERROR = "POP3 pass: " . _("connection not established");
  119. return false;
  120. } else {
  121. $reply = $this->send_cmd("PASS $pass");
  122. if(!$this->is_ok($reply)) {
  123. $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
  124. $this->quit();
  125. return false;
  126. } else {
  127. // Auth successful.
  128. $count = $this->last("count");
  129. $this->COUNT = $count;
  130. return $count;
  131. }
  132. }
  133. }
  134. function apop ($login,$pass) {
  135. // Attempts an APOP login. If this fails, it'll
  136. // try a standard login. YOUR SERVER MUST SUPPORT
  137. // THE USE OF THE APOP COMMAND!
  138. // (apop is optional per rfc1939)
  139. if(!isset($this->FP)) {
  140. $this->ERROR = "POP3 apop: " . _("No connection to server");
  141. return false;
  142. } elseif(!$this->ALLOWAPOP) {
  143. $retVal = $this->login($login,$pass);
  144. return $retVal;
  145. } elseif(empty($login)) {
  146. $this->ERROR = "POP3 apop: " . _("No login ID submitted");
  147. return false;
  148. } elseif(empty($pass)) {
  149. $this->ERROR = "POP3 apop: " . _("No password submitted");
  150. return false;
  151. } else {
  152. $banner = $this->BANNER;
  153. if( (!$banner) or (empty($banner)) ) {
  154. $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
  155. $retVal = $this->login($login,$pass);
  156. return $retVal;
  157. } else {
  158. $AuthString = $banner;
  159. $AuthString .= $pass;
  160. $APOPString = md5($AuthString);
  161. $cmd = "APOP $login $APOPString";
  162. $reply = $this->send_cmd($cmd);
  163. if(!$this->is_ok($reply)) {
  164. $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
  165. $retVal = $this->login($login,$pass);
  166. return $retVal;
  167. } else {
  168. // Auth successful.
  169. $count = $this->last("count");
  170. $this->COUNT = $count;
  171. return $count;
  172. }
  173. }
  174. }
  175. }
  176. function login ($login = "", $pass = "") {
  177. // Sends both user and pass. Returns # of msgs in mailbox or
  178. // false on failure (or -1, if the error occurs while getting
  179. // the number of messages.)
  180. if( !isset($this->FP) ) {
  181. $this->ERROR = "POP3 login: " . _("No connection to server");
  182. return false;
  183. } else {
  184. $fp = $this->FP;
  185. if( !$this->user( $login ) ) {
  186. // Preserve the error generated by user()
  187. return false;
  188. } else {
  189. $count = $this->pass($pass);
  190. if( (!$count) || ($count == -1) ) {
  191. // Preserve the error generated by last() and pass()
  192. return false;
  193. } else
  194. return $count;
  195. }
  196. }
  197. }
  198. function top ($msgNum, $numLines = "0") {
  199. // Gets the header and first $numLines of the msg body
  200. // returns data in an array with each returned line being
  201. // an array element. If $numLines is empty, returns
  202. // only the header information, and none of the body.
  203. if(!isset($this->FP)) {
  204. $this->ERROR = "POP3 top: " . _("No connection to server");
  205. return false;
  206. }
  207. $this->update_timer();
  208. $fp = $this->FP;
  209. $buffer = $this->BUFFER;
  210. $cmd = "TOP $msgNum $numLines";
  211. fwrite($fp, "TOP $msgNum $numLines\r\n");
  212. $reply = fgets($fp, $buffer);
  213. $reply = $this->strip_clf($reply);
  214. if($this->DEBUG) {
  215. @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
  216. }
  217. if(!$this->is_ok($reply))
  218. {
  219. $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
  220. return false;
  221. }
  222. $count = 0;
  223. $MsgArray = array();
  224. $line = fgets($fp,$buffer);
  225. while ( !ereg("^\.\r\n",$line))
  226. {
  227. $MsgArray[$count] = $line;
  228. $count++;
  229. $line = fgets($fp,$buffer);
  230. if(empty($line)) { break; }
  231. }
  232. return $MsgArray;
  233. }
  234. function pop_list ($msgNum = "") {
  235. // If called with an argument, returns that msgs' size in octets
  236. // No argument returns an associative array of undeleted
  237. // msg numbers and their sizes in octets
  238. if(!isset($this->FP))
  239. {
  240. $this->ERROR = "POP3 pop_list: " . _("No connection to server");
  241. return false;
  242. }
  243. $fp = $this->FP;
  244. $Total = $this->COUNT;
  245. if( (!$Total) or ($Total == -1) )
  246. {
  247. return false;
  248. }
  249. if($Total == 0)
  250. {
  251. return array("0","0");
  252. // return -1; // mailbox empty
  253. }
  254. $this->update_timer();
  255. if(!empty($msgNum))
  256. {
  257. $cmd = "LIST $msgNum";
  258. fwrite($fp,"$cmd\r\n");
  259. $reply = fgets($fp,$this->BUFFER);
  260. $reply = $this->strip_clf($reply);
  261. if($this->DEBUG) {
  262. @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
  263. }
  264. if(!$this->is_ok($reply))
  265. {
  266. $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
  267. return false;
  268. }
  269. list($junk,$num,$size) = preg_split('/\s+/',$reply);
  270. return $size;
  271. }
  272. $cmd = "LIST";
  273. $reply = $this->send_cmd($cmd);
  274. if(!$this->is_ok($reply))
  275. {
  276. $reply = $this->strip_clf($reply);
  277. $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
  278. return false;
  279. }
  280. $MsgArray = array();
  281. $MsgArray[0] = $Total;
  282. for($msgC=1;$msgC <= $Total; $msgC++)
  283. {
  284. if($msgC > $Total) { break; }
  285. $line = fgets($fp,$this->BUFFER);
  286. $line = $this->strip_clf($line);
  287. if(ereg("^\.",$line))
  288. {
  289. $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
  290. return false;
  291. }
  292. list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
  293. settype($thisMsg,"integer");
  294. if($thisMsg != $msgC)
  295. {
  296. $MsgArray[$msgC] = "deleted";
  297. }
  298. else
  299. {
  300. $MsgArray[$msgC] = $msgSize;
  301. }
  302. }
  303. return $MsgArray;
  304. }
  305. function get ($msgNum) {
  306. // Retrieve the specified msg number. Returns an array
  307. // where each line of the msg is an array element.
  308. if(!isset($this->FP))
  309. {
  310. $this->ERROR = "POP3 get: " . _("No connection to server");
  311. return false;
  312. }
  313. $this->update_timer();
  314. $fp = $this->FP;
  315. $buffer = $this->BUFFER;
  316. $cmd = "RETR $msgNum";
  317. $reply = $this->send_cmd($cmd);
  318. if(!$this->is_ok($reply))
  319. {
  320. $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
  321. return false;
  322. }
  323. $count = 0;
  324. $MsgArray = array();
  325. $line = fgets($fp,$buffer);
  326. while ( !ereg("^\.\r\n",$line))
  327. {
  328. if ( $line[0] == '.' ) { $line = substr($line,1); }
  329. $MsgArray[$count] = $line;
  330. $count++;
  331. $line = fgets($fp,$buffer);
  332. if(empty($line)) { break; }
  333. }
  334. return $MsgArray;
  335. }
  336. function last ( $type = "count" ) {
  337. // Returns the highest msg number in the mailbox.
  338. // returns -1 on error, 0+ on success, if type != count
  339. // results in a popstat() call (2 element array returned)
  340. $last = -1;
  341. if(!isset($this->FP))
  342. {
  343. $this->ERROR = "POP3 last: " . _("No connection to server");
  344. return $last;
  345. }
  346. $reply = $this->send_cmd("STAT");
  347. if(!$this->is_ok($reply))
  348. {
  349. $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
  350. return $last;
  351. }
  352. $Vars = preg_split('/\s+/',$reply);
  353. $count = $Vars[1];
  354. $size = $Vars[2];
  355. settype($count,"integer");
  356. settype($size,"integer");
  357. if($type != "count")
  358. {
  359. return array($count,$size);
  360. }
  361. return $count;
  362. }
  363. function reset () {
  364. // Resets the status of the remote server. This includes
  365. // resetting the status of ALL msgs to not be deleted.
  366. // This method automatically closes the connection to the server.
  367. if(!isset($this->FP))
  368. {
  369. $this->ERROR = "POP3 reset: " . _("No connection to server");
  370. return false;
  371. }
  372. $reply = $this->send_cmd("RSET");
  373. if(!$this->is_ok($reply))
  374. {
  375. // The POP3 RSET command -never- gives a -ERR
  376. // response - if it ever does, something truely
  377. // wild is going on.
  378. $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
  379. @error_log("POP3 reset: ERROR [$reply]",0);
  380. }
  381. $this->quit();
  382. return true;
  383. }
  384. function send_cmd ( $cmd = "" )
  385. {
  386. // Sends a user defined command string to the
  387. // POP server and returns the results. Useful for
  388. // non-compliant or custom POP servers.
  389. // Do NOT includ the \r\n as part of your command
  390. // string - it will be appended automatically.
  391. // The return value is a standard fgets() call, which
  392. // will read up to $this->BUFFER bytes of data, until it
  393. // encounters a new line, or EOF, whichever happens first.
  394. // This method works best if $cmd responds with only
  395. // one line of data.
  396. if(!isset($this->FP))
  397. {
  398. $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
  399. return false;
  400. }
  401. if(empty($cmd))
  402. {
  403. $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
  404. return "";
  405. }
  406. $fp = $this->FP;
  407. $buffer = $this->BUFFER;
  408. $this->update_timer();
  409. fwrite($fp,"$cmd\r\n");
  410. $reply = fgets($fp,$buffer);
  411. $reply = $this->strip_clf($reply);
  412. if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  413. return $reply;
  414. }
  415. function quit() {
  416. // Closes the connection to the POP3 server, deleting
  417. // any msgs marked as deleted.
  418. if(!isset($this->FP))
  419. {
  420. $this->ERROR = "POP3 quit: " . _("connection does not exist");
  421. return false;
  422. }
  423. $fp = $this->FP;
  424. $cmd = "QUIT";
  425. fwrite($fp,"$cmd\r\n");
  426. $reply = fgets($fp,$this->BUFFER);
  427. $reply = $this->strip_clf($reply);
  428. if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  429. fclose($fp);
  430. unset($this->FP);
  431. return true;
  432. }
  433. function popstat () {
  434. // Returns an array of 2 elements. The number of undeleted
  435. // msgs in the mailbox, and the size of the mbox in octets.
  436. $PopArray = $this->last("array");
  437. if($PopArray == -1) { return false; }
  438. if( (!$PopArray) or (empty($PopArray)) )
  439. {
  440. return false;
  441. }
  442. return $PopArray;
  443. }
  444. function uidl ($msgNum = "")
  445. {
  446. // Returns the UIDL of the msg specified. If called with
  447. // no arguments, returns an associative array where each
  448. // undeleted msg num is a key, and the msg's uidl is the element
  449. // Array element 0 will contain the total number of msgs
  450. if(!isset($this->FP)) {
  451. $this->ERROR = "POP3 uidl: " . _("No connection to server");
  452. return false;
  453. }
  454. $fp = $this->FP;
  455. $buffer = $this->BUFFER;
  456. if(!empty($msgNum)) {
  457. $cmd = "UIDL $msgNum";
  458. $reply = $this->send_cmd($cmd);
  459. if(!$this->is_ok($reply))
  460. {
  461. $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
  462. return false;
  463. }
  464. list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
  465. return $myUidl;
  466. } else {
  467. $this->update_timer();
  468. $UIDLArray = array();
  469. $Total = $this->COUNT;
  470. $UIDLArray[0] = $Total;
  471. if ($Total < 1)
  472. {
  473. return $UIDLArray;
  474. }
  475. $cmd = "UIDL";
  476. fwrite($fp, "UIDL\r\n");
  477. $reply = fgets($fp, $buffer);
  478. $reply = $this->strip_clf($reply);
  479. if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  480. if(!$this->is_ok($reply))
  481. {
  482. $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
  483. return false;
  484. }
  485. $line = "";
  486. $count = 1;
  487. $line = fgets($fp,$buffer);
  488. while ( !ereg("^\.\r\n",$line)) {
  489. if(ereg("^\.\r\n",$line)) {
  490. break;
  491. }
  492. list ($msg,$msgUidl) = preg_split('/\s+/',$line);
  493. $msgUidl = $this->strip_clf($msgUidl);
  494. if($count == $msg) {
  495. $UIDLArray[$msg] = $msgUidl;
  496. }
  497. else
  498. {
  499. $UIDLArray[$count] = 'deleted';
  500. }
  501. $count++;
  502. $line = fgets($fp,$buffer);
  503. }
  504. }
  505. return $UIDLArray;
  506. }
  507. function delete ($msgNum = "") {
  508. // Flags a specified msg as deleted. The msg will not
  509. // be deleted until a quit() method is called.
  510. if(!isset($this->FP))
  511. {
  512. $this->ERROR = "POP3 delete: " . _("No connection to server");
  513. return false;
  514. }
  515. if(empty($msgNum))
  516. {
  517. $this->ERROR = "POP3 delete: " . _("No msg number submitted");
  518. return false;
  519. }
  520. $reply = $this->send_cmd("DELE $msgNum");
  521. if(!$this->is_ok($reply))
  522. {
  523. $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
  524. return false;
  525. }
  526. return true;
  527. }
  528. // *********************************************************
  529. // The following methods are internal to the class.
  530. function is_ok ($cmd = "") {
  531. // Return true or false on +OK or -ERR
  532. if( empty($cmd) )
  533. return false;
  534. else
  535. return( ereg ("^\+OK", $cmd ) );
  536. }
  537. function strip_clf ($text = "") {
  538. // Strips \r\n from server responses
  539. if(empty($text))
  540. return $text;
  541. else {
  542. $stripped = str_replace("\r",'',$text);
  543. $stripped = str_replace("\n",'',$stripped);
  544. return $stripped;
  545. }
  546. }
  547. function parse_banner ( $server_text ) {
  548. $outside = true;
  549. $banner = "";
  550. $length = strlen($server_text);
  551. for($count =0; $count < $length; $count++)
  552. {
  553. $digit = substr($server_text,$count,1);
  554. if(!empty($digit)) {
  555. if( (!$outside) && ($digit != '<') && ($digit != '>') )
  556. {
  557. $banner .= $digit;
  558. }
  559. if ($digit == '<')
  560. {
  561. $outside = false;
  562. }
  563. if($digit == '>')
  564. {
  565. $outside = true;
  566. }
  567. }
  568. }
  569. $banner = $this->strip_clf($banner); // Just in case
  570. return "<$banner>";
  571. }
  572. } // End class
  573. ?>