class.POP3.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. /**
  3. * mail_fetch/setup.php
  4. *
  5. * Copyright (c) 1999-2003 The SquirrelMail Project Team
  6. *
  7. * Copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
  8. * Modified by Philippe Mingo 2001 mingo@rotedic.com
  9. * An RFC 1939 compliant wrapper class for the POP3 protocol.
  10. *
  11. * Licensed under the GNU GPL. For full terms see the file COPYING.
  12. *
  13. * pop3 class
  14. *
  15. * $Id$
  16. */
  17. class POP3 {
  18. var $ERROR = ''; // Error string.
  19. var $TIMEOUT = 60; // Default timeout before giving up on a
  20. // network operation.
  21. var $COUNT = -1; // Mailbox msg count
  22. var $BUFFER = 512; // Socket buffer for socket fgets() calls.
  23. // Per RFC 1939 the returned line a POP3
  24. // server can send is 512 bytes.
  25. var $FP = ''; // The connection to the server's
  26. // file descriptor
  27. var $MAILSERVER = ''; // Set this to hard code the server name
  28. var $DEBUG = FALSE; // set to true to echo pop3
  29. // commands and responses to error_log
  30. // this WILL log passwords!
  31. var $BANNER = ''; // Holds the banner returned by the
  32. // pop server - used for apop()
  33. var $ALLOWAPOP = FALSE; // Allow or disallow apop()
  34. // This must be set to true
  35. // manually
  36. function POP3 ( $server = '', $timeout = '' ) {
  37. settype($this->BUFFER,"integer");
  38. if( !empty($server) ) {
  39. // Do not allow programs to alter MAILSERVER
  40. // if it is already specified. They can get around
  41. // this if they -really- want to, so don't count on it.
  42. if(empty($this->MAILSERVER))
  43. $this->MAILSERVER = $server;
  44. }
  45. if(!empty($timeout)) {
  46. settype($timeout,"integer");
  47. $this->TIMEOUT = $timeout;
  48. set_time_limit($timeout);
  49. }
  50. return true;
  51. }
  52. function update_timer () {
  53. set_time_limit($this->TIMEOUT);
  54. return true;
  55. }
  56. function connect ($server, $port = 110) {
  57. // Opens a socket to the specified server. Unless overridden,
  58. // port defaults to 110. Returns true on success, false on fail
  59. // If MAILSERVER is set, override $server with it's value
  60. if (!isset($port) || !$port) {$port = 110;}
  61. if(!empty($this->MAILSERVER))
  62. $server = $this->MAILSERVER;
  63. if(empty($server)){
  64. $this->ERROR = _("POP3 connect:") . ' ' . _("No server specified");
  65. unset($this->FP);
  66. return false;
  67. }
  68. $fp = fsockopen("$server", $port, $errno, $errstr);
  69. if(!$fp) {
  70. $this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
  71. unset($this->FP);
  72. return false;
  73. }
  74. socket_set_blocking($fp,-1);
  75. $this->update_timer();
  76. $reply = fgets($fp,$this->BUFFER);
  77. $reply = $this->strip_clf($reply);
  78. if($this->DEBUG)
  79. error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
  80. if(!$this->is_ok($reply)) {
  81. $this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
  82. unset($this->FP);
  83. return false;
  84. }
  85. $this->FP = $fp;
  86. $this->BANNER = $this->parse_banner($reply);
  87. return true;
  88. }
  89. function noop () {
  90. if(!isset($this->FP)) {
  91. $this->ERROR = _("POP3 noop:") . ' ' . _("No connection to server");
  92. return false;
  93. } else {
  94. $cmd = "NOOP";
  95. $reply = $this->send_cmd( $cmd );
  96. return( $this->is_ok( $reply ) );
  97. }
  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 ( !ereg("^\.\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) = explode(" ",$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(ereg("^\.",$line))
  293. {
  294. $this->ERROR = _("POP3 pop_list:") . ' ' . _("Premature end of list");
  295. return false;
  296. }
  297. list($thisMsg,$msgSize) = explode(" ",$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 ( !ereg("^\.\r\n",$line))
  332. {
  333. $MsgArray[$count] = $line;
  334. $count++;
  335. $line = fgets($fp,$buffer);
  336. if(empty($line)) { break; }
  337. }
  338. return $MsgArray;
  339. }
  340. function last ( $type = "count" ) {
  341. // Returns the highest msg number in the mailbox.
  342. // returns -1 on error, 0+ on success, if type != count
  343. // results in a popstat() call (2 element array returned)
  344. $last = -1;
  345. if(!isset($this->FP))
  346. {
  347. $this->ERROR = _("POP3 last:") . ' ' . _("No connection to server");
  348. return $last;
  349. }
  350. $reply = $this->send_cmd("STAT");
  351. if(!$this->is_ok($reply))
  352. {
  353. $this->ERROR = _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
  354. return $last;
  355. }
  356. $Vars = explode(" ",$reply);
  357. $count = $Vars[1];
  358. $size = $Vars[2];
  359. settype($count,"integer");
  360. settype($size,"integer");
  361. if($type != "count")
  362. {
  363. return array($count,$size);
  364. }
  365. return $count;
  366. }
  367. function reset () {
  368. // Resets the status of the remote server. This includes
  369. // resetting the status of ALL msgs to not be deleted.
  370. // This method automatically closes the connection to the server.
  371. if(!isset($this->FP))
  372. {
  373. $this->ERROR = _("POP3 reset:") . ' ' . _("No connection to server");
  374. return false;
  375. }
  376. $reply = $this->send_cmd("RSET");
  377. if(!$this->is_ok($reply))
  378. {
  379. // The POP3 RSET command -never- gives a -ERR
  380. // response - if it ever does, something truely
  381. // wild is going on.
  382. $this->ERROR = _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
  383. @error_log("POP3 reset: ERROR [$reply]",0);
  384. }
  385. $this->quit();
  386. return true;
  387. }
  388. function send_cmd ( $cmd = "" )
  389. {
  390. // Sends a user defined command string to the
  391. // POP server and returns the results. Useful for
  392. // non-compliant or custom POP servers.
  393. // Do NOT includ the \r\n as part of your command
  394. // string - it will be appended automatically.
  395. // The return value is a standard fgets() call, which
  396. // will read up to $this->BUFFER bytes of data, until it
  397. // encounters a new line, or EOF, whichever happens first.
  398. // This method works best if $cmd responds with only
  399. // one line of data.
  400. if(!isset($this->FP))
  401. {
  402. $this->ERROR = _("POP3 send_cmd:") . ' ' . _("No connection to server");
  403. return false;
  404. }
  405. if(empty($cmd))
  406. {
  407. $this->ERROR = _("POP3 send_cmd:") . ' ' . _("Empty command string");
  408. return "";
  409. }
  410. $fp = $this->FP;
  411. $buffer = $this->BUFFER;
  412. $this->update_timer();
  413. fwrite($fp,"$cmd\r\n");
  414. $reply = fgets($fp,$buffer);
  415. $reply = $this->strip_clf($reply);
  416. if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  417. return $reply;
  418. }
  419. function quit() {
  420. // Closes the connection to the POP3 server, deleting
  421. // any msgs marked as deleted.
  422. if(!isset($this->FP))
  423. {
  424. $this->ERROR = _("POP3 quit:") . ' ' . _("connection does not exist");
  425. return false;
  426. }
  427. $fp = $this->FP;
  428. $cmd = "QUIT";
  429. fwrite($fp,"$cmd\r\n");
  430. $reply = fgets($fp,$this->BUFFER);
  431. $reply = $this->strip_clf($reply);
  432. if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  433. fclose($fp);
  434. unset($this->FP);
  435. return true;
  436. }
  437. function popstat () {
  438. // Returns an array of 2 elements. The number of undeleted
  439. // msgs in the mailbox, and the size of the mbox in octets.
  440. $PopArray = $this->last("array");
  441. if($PopArray == -1) { return false; }
  442. if( (!$PopArray) or (empty($PopArray)) )
  443. {
  444. return false;
  445. }
  446. return $PopArray;
  447. }
  448. function uidl ($msgNum = "")
  449. {
  450. // Returns the UIDL of the msg specified. If called with
  451. // no arguments, returns an associative array where each
  452. // undeleted msg num is a key, and the msg's uidl is the element
  453. // Array element 0 will contain the total number of msgs
  454. if(!isset($this->FP)) {
  455. $this->ERROR = _("POP3 uidl:") . ' ' . _("No connection to server");
  456. return false;
  457. }
  458. $fp = $this->FP;
  459. $buffer = $this->BUFFER;
  460. if(!empty($msgNum)) {
  461. $cmd = "UIDL $msgNum";
  462. $reply = $this->send_cmd($cmd);
  463. if(!$this->is_ok($reply))
  464. {
  465. $this->ERROR = _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
  466. return false;
  467. }
  468. list ($ok,$num,$myUidl) = explode(" ",$reply);
  469. return $myUidl;
  470. } else {
  471. $this->update_timer();
  472. $UIDLArray = array();
  473. $Total = $this->COUNT;
  474. $UIDLArray[0] = $Total;
  475. if ($Total < 1)
  476. {
  477. return $UIDLArray;
  478. }
  479. $cmd = "UIDL";
  480. fwrite($fp, "UIDL\r\n");
  481. $reply = fgets($fp, $buffer);
  482. $reply = $this->strip_clf($reply);
  483. if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  484. if(!$this->is_ok($reply))
  485. {
  486. $this->ERROR = _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
  487. return false;
  488. }
  489. $line = "";
  490. $count = 1;
  491. $line = fgets($fp,$buffer);
  492. while ( !ereg("^\.\r\n",$line)) {
  493. if(ereg("^\.\r\n",$line)) {
  494. break;
  495. }
  496. list ($msg,$msgUidl) = explode(" ",$line);
  497. $msgUidl = $this->strip_clf($msgUidl);
  498. if($count == $msg) {
  499. $UIDLArray[$msg] = $msgUidl;
  500. }
  501. else
  502. {
  503. $UIDLArray[$count] = 'deleted';
  504. }
  505. $count++;
  506. $line = fgets($fp,$buffer);
  507. }
  508. }
  509. return $UIDLArray;
  510. }
  511. function delete ($msgNum = "") {
  512. // Flags a specified msg as deleted. The msg will not
  513. // be deleted until a quit() method is called.
  514. if(!isset($this->FP))
  515. {
  516. $this->ERROR = _("POP3 delete:") . ' ' . _("No connection to server");
  517. return false;
  518. }
  519. if(empty($msgNum))
  520. {
  521. $this->ERROR = _("POP3 delete:") . ' ' . _("No msg number submitted");
  522. return false;
  523. }
  524. $reply = $this->send_cmd("DELE $msgNum");
  525. if(!$this->is_ok($reply))
  526. {
  527. $this->ERROR = _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
  528. return false;
  529. }
  530. return true;
  531. }
  532. // *********************************************************
  533. // The following methods are internal to the class.
  534. function is_ok ($cmd = "") {
  535. // Return true or false on +OK or -ERR
  536. if( empty($cmd) )
  537. return false;
  538. else
  539. return( ereg ("^\+OK", $cmd ) );
  540. }
  541. function strip_clf ($text = "") {
  542. // Strips \r\n from server responses
  543. if(empty($text))
  544. return $text;
  545. else {
  546. $stripped = str_replace("\r",'',$text);
  547. $stripped = str_replace("\n",'',$stripped);
  548. return $stripped;
  549. }
  550. }
  551. function parse_banner ( $server_text ) {
  552. $outside = true;
  553. $banner = "";
  554. $length = strlen($server_text);
  555. for($count =0; $count < $length; $count++)
  556. {
  557. $digit = substr($server_text,$count,1);
  558. if(!empty($digit)) {
  559. if( (!$outside) && ($digit != '<') && ($digit != '>') )
  560. {
  561. $banner .= $digit;
  562. }
  563. if ($digit == '<')
  564. {
  565. $outside = false;
  566. }
  567. if($digit == '>')
  568. {
  569. $outside = true;
  570. }
  571. }
  572. }
  573. $banner = $this->strip_clf($banner); // Just in case
  574. return "<$banner>";
  575. }
  576. } // End class
  577. ?>