class.POP3.php 20 KB

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