class.POP3.php 20 KB

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