class.POP3.php 21 KB

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