-
+ 745206F90604F46CC369EB2FD4DA8C31803B3C7A0FA13F75C24111F7F3ADDA434DCB05FE7C9176E539498789D4358967B650C7F44D1C03BC781422D765DC59BB
mp-wp/wp-includes/class-pop3.php
(0 . 0)(1 . 651)
78557 <?php
78558 /**
78559 * mail_fetch/setup.php
78560 *
78561 * @package SquirrelMail
78562 *
78563 * @copyright (c) 1999-2006 The SquirrelMail Project Team
78564 *
78565 * @copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
78566 * Modified by Philippe Mingo 2001 mingo@rotedic.com
78567 * An RFC 1939 compliant wrapper class for the POP3 protocol.
78568 *
78569 * Licensed under the GNU GPL. For full terms see the file COPYING.
78570 *
78571 * pop3 class
78572 *
78573 * $Id: class-pop3.php 9503 2008-11-03 23:25:11Z ryan $
78574 */
78575
78576 class POP3 {
78577 var $ERROR = ''; // Error string.
78578
78579 var $TIMEOUT = 60; // Default timeout before giving up on a
78580 // network operation.
78581
78582 var $COUNT = -1; // Mailbox msg count
78583
78584 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
78585 // Per RFC 1939 the returned line a POP3
78586 // server can send is 512 bytes.
78587
78588 var $FP = ''; // The connection to the server's
78589 // file descriptor
78590
78591 var $MAILSERVER = ''; // Set this to hard code the server name
78592
78593 var $DEBUG = FALSE; // set to true to echo pop3
78594 // commands and responses to error_log
78595 // this WILL log passwords!
78596
78597 var $BANNER = ''; // Holds the banner returned by the
78598 // pop server - used for apop()
78599
78600 var $ALLOWAPOP = FALSE; // Allow or disallow apop()
78601 // This must be set to true
78602 // manually
78603
78604 function POP3 ( $server = '', $timeout = '' ) {
78605 settype($this->BUFFER,"integer");
78606 if( !empty($server) ) {
78607 // Do not allow programs to alter MAILSERVER
78608 // if it is already specified. They can get around
78609 // this if they -really- want to, so don't count on it.
78610 if(empty($this->MAILSERVER))
78611 $this->MAILSERVER = $server;
78612 }
78613 if(!empty($timeout)) {
78614 settype($timeout,"integer");
78615 $this->TIMEOUT = $timeout;
78616 if (!ini_get('safe_mode'))
78617 set_time_limit($timeout);
78618 }
78619 return true;
78620 }
78621
78622 function update_timer () {
78623 if (!ini_get('safe_mode'))
78624 set_time_limit($this->TIMEOUT);
78625 return true;
78626 }
78627
78628 function connect ($server, $port = 110) {
78629 // Opens a socket to the specified server. Unless overridden,
78630 // port defaults to 110. Returns true on success, false on fail
78631
78632 // If MAILSERVER is set, override $server with it's value
78633
78634 if (!isset($port) || !$port) {$port = 110;}
78635 if(!empty($this->MAILSERVER))
78636 $server = $this->MAILSERVER;
78637
78638 if(empty($server)){
78639 $this->ERROR = "POP3 connect: " . _("No server specified");
78640 unset($this->FP);
78641 return false;
78642 }
78643
78644 $fp = @fsockopen("$server", $port, $errno, $errstr);
78645
78646 if(!$fp) {
78647 $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
78648 unset($this->FP);
78649 return false;
78650 }
78651
78652 socket_set_blocking($fp,-1);
78653 $this->update_timer();
78654 $reply = fgets($fp,$this->BUFFER);
78655 $reply = $this->strip_clf($reply);
78656 if($this->DEBUG)
78657 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
78658 if(!$this->is_ok($reply)) {
78659 $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
78660 unset($this->FP);
78661 return false;
78662 }
78663 $this->FP = $fp;
78664 $this->BANNER = $this->parse_banner($reply);
78665 return true;
78666 }
78667
78668 function user ($user = "") {
78669 // Sends the USER command, returns true or false
78670
78671 if( empty($user) ) {
78672 $this->ERROR = "POP3 user: " . _("no login ID submitted");
78673 return false;
78674 } elseif(!isset($this->FP)) {
78675 $this->ERROR = "POP3 user: " . _("connection not established");
78676 return false;
78677 } else {
78678 $reply = $this->send_cmd("USER $user");
78679 if(!$this->is_ok($reply)) {
78680 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
78681 return false;
78682 } else
78683 return true;
78684 }
78685 }
78686
78687 function pass ($pass = "") {
78688 // Sends the PASS command, returns # of msgs in mailbox,
78689 // returns false (undef) on Auth failure
78690
78691 if(empty($pass)) {
78692 $this->ERROR = "POP3 pass: " . _("No password submitted");
78693 return false;
78694 } elseif(!isset($this->FP)) {
78695 $this->ERROR = "POP3 pass: " . _("connection not established");
78696 return false;
78697 } else {
78698 $reply = $this->send_cmd("PASS $pass");
78699 if(!$this->is_ok($reply)) {
78700 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
78701 $this->quit();
78702 return false;
78703 } else {
78704 // Auth successful.
78705 $count = $this->last("count");
78706 $this->COUNT = $count;
78707 return $count;
78708 }
78709 }
78710 }
78711
78712 function apop ($login,$pass) {
78713 // Attempts an APOP login. If this fails, it'll
78714 // try a standard login. YOUR SERVER MUST SUPPORT
78715 // THE USE OF THE APOP COMMAND!
78716 // (apop is optional per rfc1939)
78717
78718 if(!isset($this->FP)) {
78719 $this->ERROR = "POP3 apop: " . _("No connection to server");
78720 return false;
78721 } elseif(!$this->ALLOWAPOP) {
78722 $retVal = $this->login($login,$pass);
78723 return $retVal;
78724 } elseif(empty($login)) {
78725 $this->ERROR = "POP3 apop: " . _("No login ID submitted");
78726 return false;
78727 } elseif(empty($pass)) {
78728 $this->ERROR = "POP3 apop: " . _("No password submitted");
78729 return false;
78730 } else {
78731 $banner = $this->BANNER;
78732 if( (!$banner) or (empty($banner)) ) {
78733 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
78734 $retVal = $this->login($login,$pass);
78735 return $retVal;
78736 } else {
78737 $AuthString = $banner;
78738 $AuthString .= $pass;
78739 $APOPString = md5($AuthString);
78740 $cmd = "APOP $login $APOPString";
78741 $reply = $this->send_cmd($cmd);
78742 if(!$this->is_ok($reply)) {
78743 $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
78744 $retVal = $this->login($login,$pass);
78745 return $retVal;
78746 } else {
78747 // Auth successful.
78748 $count = $this->last("count");
78749 $this->COUNT = $count;
78750 return $count;
78751 }
78752 }
78753 }
78754 }
78755
78756 function login ($login = "", $pass = "") {
78757 // Sends both user and pass. Returns # of msgs in mailbox or
78758 // false on failure (or -1, if the error occurs while getting
78759 // the number of messages.)
78760
78761 if( !isset($this->FP) ) {
78762 $this->ERROR = "POP3 login: " . _("No connection to server");
78763 return false;
78764 } else {
78765 $fp = $this->FP;
78766 if( !$this->user( $login ) ) {
78767 // Preserve the error generated by user()
78768 return false;
78769 } else {
78770 $count = $this->pass($pass);
78771 if( (!$count) || ($count == -1) ) {
78772 // Preserve the error generated by last() and pass()
78773 return false;
78774 } else
78775 return $count;
78776 }
78777 }
78778 }
78779
78780 function top ($msgNum, $numLines = "0") {
78781 // Gets the header and first $numLines of the msg body
78782 // returns data in an array with each returned line being
78783 // an array element. If $numLines is empty, returns
78784 // only the header information, and none of the body.
78785
78786 if(!isset($this->FP)) {
78787 $this->ERROR = "POP3 top: " . _("No connection to server");
78788 return false;
78789 }
78790 $this->update_timer();
78791
78792 $fp = $this->FP;
78793 $buffer = $this->BUFFER;
78794 $cmd = "TOP $msgNum $numLines";
78795 fwrite($fp, "TOP $msgNum $numLines\r\n");
78796 $reply = fgets($fp, $buffer);
78797 $reply = $this->strip_clf($reply);
78798 if($this->DEBUG) {
78799 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
78800 }
78801 if(!$this->is_ok($reply))
78802 {
78803 $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
78804 return false;
78805 }
78806
78807 $count = 0;
78808 $MsgArray = array();
78809
78810 $line = fgets($fp,$buffer);
78811 while ( !ereg("^\.\r\n",$line))
78812 {
78813 $MsgArray[$count] = $line;
78814 $count++;
78815 $line = fgets($fp,$buffer);
78816 if(empty($line)) { break; }
78817 }
78818
78819 return $MsgArray;
78820 }
78821
78822 function pop_list ($msgNum = "") {
78823 // If called with an argument, returns that msgs' size in octets
78824 // No argument returns an associative array of undeleted
78825 // msg numbers and their sizes in octets
78826
78827 if(!isset($this->FP))
78828 {
78829 $this->ERROR = "POP3 pop_list: " . _("No connection to server");
78830 return false;
78831 }
78832 $fp = $this->FP;
78833 $Total = $this->COUNT;
78834 if( (!$Total) or ($Total == -1) )
78835 {
78836 return false;
78837 }
78838 if($Total == 0)
78839 {
78840 return array("0","0");
78841 // return -1; // mailbox empty
78842 }
78843
78844 $this->update_timer();
78845
78846 if(!empty($msgNum))
78847 {
78848 $cmd = "LIST $msgNum";
78849 fwrite($fp,"$cmd\r\n");
78850 $reply = fgets($fp,$this->BUFFER);
78851 $reply = $this->strip_clf($reply);
78852 if($this->DEBUG) {
78853 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
78854 }
78855 if(!$this->is_ok($reply))
78856 {
78857 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
78858 return false;
78859 }
78860 list($junk,$num,$size) = preg_split('/\s+/',$reply);
78861 return $size;
78862 }
78863 $cmd = "LIST";
78864 $reply = $this->send_cmd($cmd);
78865 if(!$this->is_ok($reply))
78866 {
78867 $reply = $this->strip_clf($reply);
78868 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
78869 return false;
78870 }
78871 $MsgArray = array();
78872 $MsgArray[0] = $Total;
78873 for($msgC=1;$msgC <= $Total; $msgC++)
78874 {
78875 if($msgC > $Total) { break; }
78876 $line = fgets($fp,$this->BUFFER);
78877 $line = $this->strip_clf($line);
78878 if(ereg("^\.",$line))
78879 {
78880 $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
78881 return false;
78882 }
78883 list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
78884 settype($thisMsg,"integer");
78885 if($thisMsg != $msgC)
78886 {
78887 $MsgArray[$msgC] = "deleted";
78888 }
78889 else
78890 {
78891 $MsgArray[$msgC] = $msgSize;
78892 }
78893 }
78894 return $MsgArray;
78895 }
78896
78897 function get ($msgNum) {
78898 // Retrieve the specified msg number. Returns an array
78899 // where each line of the msg is an array element.
78900
78901 if(!isset($this->FP))
78902 {
78903 $this->ERROR = "POP3 get: " . _("No connection to server");
78904 return false;
78905 }
78906
78907 $this->update_timer();
78908
78909 $fp = $this->FP;
78910 $buffer = $this->BUFFER;
78911 $cmd = "RETR $msgNum";
78912 $reply = $this->send_cmd($cmd);
78913
78914 if(!$this->is_ok($reply))
78915 {
78916 $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
78917 return false;
78918 }
78919
78920 $count = 0;
78921 $MsgArray = array();
78922
78923 $line = fgets($fp,$buffer);
78924 while ( !ereg("^\.\r\n",$line))
78925 {
78926 if ( $line{0} == '.' ) { $line = substr($line,1); }
78927 $MsgArray[$count] = $line;
78928 $count++;
78929 $line = fgets($fp,$buffer);
78930 if(empty($line)) { break; }
78931 }
78932 return $MsgArray;
78933 }
78934
78935 function last ( $type = "count" ) {
78936 // Returns the highest msg number in the mailbox.
78937 // returns -1 on error, 0+ on success, if type != count
78938 // results in a popstat() call (2 element array returned)
78939
78940 $last = -1;
78941 if(!isset($this->FP))
78942 {
78943 $this->ERROR = "POP3 last: " . _("No connection to server");
78944 return $last;
78945 }
78946
78947 $reply = $this->send_cmd("STAT");
78948 if(!$this->is_ok($reply))
78949 {
78950 $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
78951 return $last;
78952 }
78953
78954 $Vars = preg_split('/\s+/',$reply);
78955 $count = $Vars[1];
78956 $size = $Vars[2];
78957 settype($count,"integer");
78958 settype($size,"integer");
78959 if($type != "count")
78960 {
78961 return array($count,$size);
78962 }
78963 return $count;
78964 }
78965
78966 function reset () {
78967 // Resets the status of the remote server. This includes
78968 // resetting the status of ALL msgs to not be deleted.
78969 // This method automatically closes the connection to the server.
78970
78971 if(!isset($this->FP))
78972 {
78973 $this->ERROR = "POP3 reset: " . _("No connection to server");
78974 return false;
78975 }
78976 $reply = $this->send_cmd("RSET");
78977 if(!$this->is_ok($reply))
78978 {
78979 // The POP3 RSET command -never- gives a -ERR
78980 // response - if it ever does, something truely
78981 // wild is going on.
78982
78983 $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
78984 @error_log("POP3 reset: ERROR [$reply]",0);
78985 }
78986 $this->quit();
78987 return true;
78988 }
78989
78990 function send_cmd ( $cmd = "" )
78991 {
78992 // Sends a user defined command string to the
78993 // POP server and returns the results. Useful for
78994 // non-compliant or custom POP servers.
78995 // Do NOT includ the \r\n as part of your command
78996 // string - it will be appended automatically.
78997
78998 // The return value is a standard fgets() call, which
78999 // will read up to $this->BUFFER bytes of data, until it
79000 // encounters a new line, or EOF, whichever happens first.
79001
79002 // This method works best if $cmd responds with only
79003 // one line of data.
79004
79005 if(!isset($this->FP))
79006 {
79007 $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
79008 return false;
79009 }
79010
79011 if(empty($cmd))
79012 {
79013 $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
79014 return "";
79015 }
79016
79017 $fp = $this->FP;
79018 $buffer = $this->BUFFER;
79019 $this->update_timer();
79020 fwrite($fp,"$cmd\r\n");
79021 $reply = fgets($fp,$buffer);
79022 $reply = $this->strip_clf($reply);
79023 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
79024 return $reply;
79025 }
79026
79027 function quit() {
79028 // Closes the connection to the POP3 server, deleting
79029 // any msgs marked as deleted.
79030
79031 if(!isset($this->FP))
79032 {
79033 $this->ERROR = "POP3 quit: " . _("connection does not exist");
79034 return false;
79035 }
79036 $fp = $this->FP;
79037 $cmd = "QUIT";
79038 fwrite($fp,"$cmd\r\n");
79039 $reply = fgets($fp,$this->BUFFER);
79040 $reply = $this->strip_clf($reply);
79041 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
79042 fclose($fp);
79043 unset($this->FP);
79044 return true;
79045 }
79046
79047 function popstat () {
79048 // Returns an array of 2 elements. The number of undeleted
79049 // msgs in the mailbox, and the size of the mbox in octets.
79050
79051 $PopArray = $this->last("array");
79052
79053 if($PopArray == -1) { return false; }
79054
79055 if( (!$PopArray) or (empty($PopArray)) )
79056 {
79057 return false;
79058 }
79059 return $PopArray;
79060 }
79061
79062 function uidl ($msgNum = "")
79063 {
79064 // Returns the UIDL of the msg specified. If called with
79065 // no arguments, returns an associative array where each
79066 // undeleted msg num is a key, and the msg's uidl is the element
79067 // Array element 0 will contain the total number of msgs
79068
79069 if(!isset($this->FP)) {
79070 $this->ERROR = "POP3 uidl: " . _("No connection to server");
79071 return false;
79072 }
79073
79074 $fp = $this->FP;
79075 $buffer = $this->BUFFER;
79076
79077 if(!empty($msgNum)) {
79078 $cmd = "UIDL $msgNum";
79079 $reply = $this->send_cmd($cmd);
79080 if(!$this->is_ok($reply))
79081 {
79082 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
79083 return false;
79084 }
79085 list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
79086 return $myUidl;
79087 } else {
79088 $this->update_timer();
79089
79090 $UIDLArray = array();
79091 $Total = $this->COUNT;
79092 $UIDLArray[0] = $Total;
79093
79094 if ($Total < 1)
79095 {
79096 return $UIDLArray;
79097 }
79098 $cmd = "UIDL";
79099 fwrite($fp, "UIDL\r\n");
79100 $reply = fgets($fp, $buffer);
79101 $reply = $this->strip_clf($reply);
79102 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
79103 if(!$this->is_ok($reply))
79104 {
79105 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
79106 return false;
79107 }
79108
79109 $line = "";
79110 $count = 1;
79111 $line = fgets($fp,$buffer);
79112 while ( !ereg("^\.\r\n",$line)) {
79113 if(ereg("^\.\r\n",$line)) {
79114 break;
79115 }
79116 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
79117 $msgUidl = $this->strip_clf($msgUidl);
79118 if($count == $msg) {
79119 $UIDLArray[$msg] = $msgUidl;
79120 }
79121 else
79122 {
79123 $UIDLArray[$count] = 'deleted';
79124 }
79125 $count++;
79126 $line = fgets($fp,$buffer);
79127 }
79128 }
79129 return $UIDLArray;
79130 }
79131
79132 function delete ($msgNum = "") {
79133 // Flags a specified msg as deleted. The msg will not
79134 // be deleted until a quit() method is called.
79135
79136 if(!isset($this->FP))
79137 {
79138 $this->ERROR = "POP3 delete: " . _("No connection to server");
79139 return false;
79140 }
79141 if(empty($msgNum))
79142 {
79143 $this->ERROR = "POP3 delete: " . _("No msg number submitted");
79144 return false;
79145 }
79146 $reply = $this->send_cmd("DELE $msgNum");
79147 if(!$this->is_ok($reply))
79148 {
79149 $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
79150 return false;
79151 }
79152 return true;
79153 }
79154
79155 // *********************************************************
79156
79157 // The following methods are internal to the class.
79158
79159 function is_ok ($cmd = "") {
79160 // Return true or false on +OK or -ERR
79161
79162 if( empty($cmd) )
79163 return false;
79164 else
79165 return( ereg ("^\+OK", $cmd ) );
79166 }
79167
79168 function strip_clf ($text = "") {
79169 // Strips \r\n from server responses
79170
79171 if(empty($text))
79172 return $text;
79173 else {
79174 $stripped = str_replace("\r",'',$text);
79175 $stripped = str_replace("\n",'',$stripped);
79176 return $stripped;
79177 }
79178 }
79179
79180 function parse_banner ( $server_text ) {
79181 $outside = true;
79182 $banner = "";
79183 $length = strlen($server_text);
79184 for($count =0; $count < $length; $count++)
79185 {
79186 $digit = substr($server_text,$count,1);
79187 if(!empty($digit)) {
79188 if( (!$outside) && ($digit != '<') && ($digit != '>') )
79189 {
79190 $banner .= $digit;
79191 }
79192 if ($digit == '<')
79193 {
79194 $outside = false;
79195 }
79196 if($digit == '>')
79197 {
79198 $outside = true;
79199 }
79200 }
79201 }
79202 $banner = $this->strip_clf($banner); // Just in case
79203 return "<$banner>";
79204 }
79205
79206 } // End class
79207 ?>