class-pop3.php 20 KB

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