class.pop3.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:16k
源码类别:

WEB邮件程序

开发平台:

PHP

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