Make WordPress Core

Ticket #21559: patch.diff

File patch.diff, 29.0 KB (added by bilalcoder, 12 years ago)
  • .php

    old new  
    3232                                //  file descriptor
    3333
    3434    var $MAILSERVER = '';       // Set this to hard code the server name
     35    var $PORT           = 110;          // Server port
    3536
    3637    var $DEBUG      = FALSE;    // set to true to echo pop3
    3738                                // commands and responses to error_log
     
    4344    var $ALLOWAPOP  = FALSE;    //  Allow or disallow apop()
    4445                                //  This must be set to true
    4546                                //  manually
     47                               
     48    var $safemode_enabled = true;
    4649
    47     function POP3 ( $server = '', $timeout = '' ) {
    48         settype($this->BUFFER,"integer");
    49         if( !empty($server) ) {
    50             // Do not allow programs to alter MAILSERVER
    51             // if it is already specified. They can get around
    52             // this if they -really- want to, so don't count on it.
    53             if(empty($this->MAILSERVER))
    54                 $this->MAILSERVER = $server;
    55         }
    56         if(!empty($timeout)) {
    57             settype($timeout,"integer");
    58             $this->TIMEOUT = $timeout;
    59             if (!ini_get('safe_mode'))
    60                 set_time_limit($timeout);
    61         }
    62         return true;
     50    function POP3 ( $server = '', $timeout = 0, $port = 110, $connect = false ) {
     51
     52        $this->safemode_enabled = (bool)ini_get('safe_mode');
     53        $this->BUFFER                   = (int)$this->BUFFER;
     54       
     55        $success = $this->_init( $server, $port, $timeout );
     56       
     57        if ($connect && $success)
     58                $this->connect( $this->MAILSERVER, $this->PORT );
     59    }
     60   
     61    private function _init($server = '', $port = 110, $timeout = 0){
     62
     63        // Do not allow programs to alter MAILSERVER
     64        // if it is already specified. They can get around
     65        // this if they -really- want to, so don't count on it.
     66        if (empty($this->MAILSERVER)) {
     67                if (!empty($server))
     68                        $this->MAILSERVER = $server;
     69        }
     70       
     71        if ($timeout) {
     72                $this->TIMEOUT = (int)$timeout;
     73                if (!$this->safemode_enabled)
     74                        set_time_limit( $this->TIMEOUT );
     75        }
     76       
     77        $port = (int)$port;
     78        if ($port) {
     79                $this->PORT = $port;
     80        }
     81       
     82        return !empty($this->MAILSERVER);
     83    }
     84   
     85    function set_error( $error , $caller = null, $close = false ) {
     86        $this->ERROR = 'POP3 '. ( $caller ? $caller.'()' : '' ).': '. $error;
     87       
     88        if ($close)
     89                unset($this->FP);
     90       
     91        return false;
    6392    }
    6493
    6594    function update_timer () {
    66         if (!ini_get('safe_mode'))
     95        if (!$this->safemode_enabled)
    6796            set_time_limit($this->TIMEOUT);
    6897        return true;
    6998    }
    7099
    71     function connect ($server, $port = 110)  {
    72         //  Opens a socket to the specified server. Unless overridden,
    73         //  port defaults to 110. Returns true on success, false on fail
    74 
    75         // If MAILSERVER is set, override $server with it's value
    76 
    77     if (!isset($port) || !$port) {$port = 110;}
    78         if(!empty($this->MAILSERVER))
    79             $server = $this->MAILSERVER;
    80 
    81         if(empty($server)){
    82             $this->ERROR = "POP3 connect: " . _("No server specified");
    83             unset($this->FP);
    84             return false;
     100    function connect ($server = '', $port = 110)  {
     101       
     102        if ( empty($this->MAILSERVER) || $this->PORT != $port ) {
     103                if (!$this->_init( $server, $port )) {
     104                        return $this->set_error( _("No server specified"), __FUNCTION__, true );
     105                }
    85106        }
     107               
     108        $this->FP = @fsockopen($this->MAILSERVER, $this->PORT, $errno, $errstr);
    86109
    87         $fp = @fsockopen("$server", $port, $errno, $errstr);
    88 
    89         if(!$fp) {
    90             $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
    91             unset($this->FP);
    92             return false;
     110        if(!$this->FP) {
     111            return $this->set_error(  _("Error ") . "[$errno] [$errstr]", __FUNCTION__, true );
    93112        }
     113       
     114                function_exists('stream_set_blocking') ? stream_set_blocking($this->FP, 1) : socket_set_blocking($this->FP, -1);
    94115
    95         socket_set_blocking($fp,-1);
    96116        $this->update_timer();
    97         $reply = fgets($fp,$this->BUFFER);
    98         $reply = $this->strip_clf($reply);
    99         if($this->DEBUG)
    100             error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
     117        $reply = $this->get_reply();
     118       
    101119        if(!$this->is_ok($reply)) {
    102             $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
    103             unset($this->FP);
    104             return false;
     120            return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__, true );
    105121        }
    106         $this->FP = $fp;
     122       
    107123        $this->BANNER = $this->parse_banner($reply);
    108124        return true;
    109125    }
     126   
     127     private function send($string) {
     128        $raw = $string;
     129       
     130        if ($this->FP) {
     131                $string = strpos($string, "\r\n") === FALSE ? $string."\r\n" : $string;
     132               
     133                if ( ($number = @fwrite($this->FP, $string)) ) {
     134                        if ($this->DEBUG) {
     135                                @error_log('POP3 '. __FUNCTION__.'(): ['.$raw.']');
     136                        }
     137                        return $number;
     138                }
     139        }
     140       
     141        return false;
     142    }
     143   
     144    private function get_reply($strip_clf = true) {
     145        $reply = @fgets( $this->FP, $this->BUFFER );
     146               
     147        if ($reply) {
     148                if ($strip_clf) {
     149                        $reply = $this->strip_clf( $reply );
     150                }
     151               
     152                if ($this->DEBUG) {
     153                        @error_log('POP3 GOT: ['. $reply .'] FROM ['.$this->MAILSERVER.']',0);
     154                }
     155        }
     156       
     157        return $reply;
     158    }
    110159
    111160    function user ($user = "") {
    112161        // Sends the USER command, returns true or false
    113162
    114         if( empty($user) ) {
    115             $this->ERROR = "POP3 user: " . _("no login ID submitted");
    116             return false;
    117         } elseif(!isset($this->FP)) {
    118             $this->ERROR = "POP3 user: " . _("connection not established");
    119             return false;
    120         } else {
    121             $reply = $this->send_cmd("USER $user");
    122             if(!$this->is_ok($reply)) {
    123                 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
    124                 return false;
    125             } else
    126                 return true;
    127         }
     163        if( empty($user) )
     164            return $this->set_error( _("no login ID submitted"), __FUNCTION__ );
     165       
     166        if(!isset($this->FP))
     167            return $this->set_error( _("connection not established"), __FUNCTION__ );
     168       
     169        $reply = $this->send_cmd("USER $user");
     170        if(!$this->is_ok($reply))
     171                return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
     172               
     173        return $this;
    128174    }
    129175
    130176    function pass ($pass = "")     {
    131177        // Sends the PASS command, returns # of msgs in mailbox,
    132178        // returns false (undef) on Auth failure
    133179
    134         if(empty($pass)) {
    135             $this->ERROR = "POP3 pass: " . _("No password submitted");
    136             return false;
    137         } elseif(!isset($this->FP)) {
    138             $this->ERROR = "POP3 pass: " . _("connection not established");
     180        if(empty($pass))
     181           return $this->set_error( _("No password submitted"), __FUNCTION__ );
     182       
     183        if(!isset($this->FP))
     184            return $this->set_error( _("connection not established"), __FUNCTION__ );
     185           
     186        $reply = $this->send_cmd("PASS $pass");
     187        if(!$this->is_ok($reply)) {
     188            $this->set_error( _("Authentication failed") . " [$reply]", __FUNCTION__ );
     189            $this->quit();
    139190            return false;
    140         } else {
    141             $reply = $this->send_cmd("PASS $pass");
    142             if(!$this->is_ok($reply)) {
    143                 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
    144                 $this->quit();
    145                 return false;
    146             } else {
    147                 //  Auth successful.
    148                 $count = $this->last("count");
    149                 $this->COUNT = $count;
    150                 return $count;
    151             }
    152191        }
     192       
     193        //  Auth successful.
     194        $this->COUNT = $this->last("count");
     195        return $this->COUNT;
    153196    }
    154197
    155198    function apop ($login,$pass) {
     
    158201        //  THE USE OF THE APOP COMMAND!
    159202        //  (apop is optional per rfc1939)
    160203
    161         if(!isset($this->FP)) {
    162             $this->ERROR = "POP3 apop: " . _("No connection to server");
    163             return false;
    164         } elseif(!$this->ALLOWAPOP) {
     204        if(!isset($this->FP))
     205            return $this->set_error( _("No connection to server"), __FUNCTION__ );
     206           
     207        if(!$this->ALLOWAPOP) {
     208            $retVal = $this->login($login,$pass);
     209            return $retVal;
     210        }
     211       
     212        if (empty($login))
     213            return $this->set_error( _("No login ID submitted"), __FUNCTION__ );
     214           
     215        if(empty($pass))
     216            return $this->set_error( _("No password submitted"), __FUNCTION__ );
     217           
     218       
     219        $banner = $this->BANNER;
     220        if( !$banner ) {
     221            $this->set_error(  _("No server banner") . ' - ' . _("abort"), __FUNCTION__ );
     222            $retVal = $this->login($login,$pass);
     223            return $retVal;
     224        }
     225       
     226        $AuthString = $banner. $pass;
     227        $APOPString = md5($AuthString);
     228       
     229        $reply = $this->send_cmd("APOP $login $APOPString");
     230        if(!$this->is_ok($reply)) {
     231            $this->set_error( _("apop authentication failed") . ' - ' . _("abort"), __FUNCTION__ );
    165232            $retVal = $this->login($login,$pass);
    166233            return $retVal;
    167         } elseif(empty($login)) {
    168             $this->ERROR = "POP3 apop: " . _("No login ID submitted");
    169             return false;
    170         } elseif(empty($pass)) {
    171             $this->ERROR = "POP3 apop: " . _("No password submitted");
    172             return false;
    173         } else {
    174             $banner = $this->BANNER;
    175             if( (!$banner) or (empty($banner)) ) {
    176                 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
    177                 $retVal = $this->login($login,$pass);
    178                 return $retVal;
    179             } else {
    180                 $AuthString = $banner;
    181                 $AuthString .= $pass;
    182                 $APOPString = md5($AuthString);
    183                 $cmd = "APOP $login $APOPString";
    184                 $reply = $this->send_cmd($cmd);
    185                 if(!$this->is_ok($reply)) {
    186                     $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
    187                     $retVal = $this->login($login,$pass);
    188                     return $retVal;
    189                 } else {
    190                     //  Auth successful.
    191                     $count = $this->last("count");
    192                     $this->COUNT = $count;
    193                     return $count;
    194                 }
    195             }
    196234        }
     235       
     236        //  Auth successful.
     237        $this->COUNT = $this->last("count");
     238        return $this->COUNT;
    197239    }
    198240
    199241    function login ($login = "", $pass = "") {
     
    202244        // the number of messages.)
    203245
    204246        if( !isset($this->FP) ) {
    205             $this->ERROR = "POP3 login: " . _("No connection to server");
    206             return false;
     247            return $this->set_error( _("No connection to server"), __FUNCTION__ );
    207248        } else {
    208249            $fp = $this->FP;
    209250            if( !$this->user( $login ) ) {
     
    220261        }
    221262    }
    222263
    223     function top ($msgNum, $numLines = "0") {
     264    function top ($msgNum, $numLines = 0) {
    224265        //  Gets the header and first $numLines of the msg body
    225266        //  returns data in an array with each returned line being
    226267        //  an array element. If $numLines is empty, returns
    227268        //  only the header information, and none of the body.
    228269
    229         if(!isset($this->FP)) {
    230             $this->ERROR = "POP3 top: " . _("No connection to server");
    231             return false;
    232         }
     270        if(!isset($this->FP))
     271            return $this->set_error( _("No connection to server"), __FUNCTION__ );
     272       
    233273        $this->update_timer();
    234274
    235         $fp = $this->FP;
    236         $buffer = $this->BUFFER;
    237         $cmd = "TOP $msgNum $numLines";
    238         fwrite($fp, "TOP $msgNum $numLines\r\n");
    239         $reply = fgets($fp, $buffer);
    240         $reply = $this->strip_clf($reply);
    241         if($this->DEBUG) {
    242             @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
    243         }
    244         if(!$this->is_ok($reply))
    245         {
    246             $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
    247             return false;
     275        $this->send( "TOP $msgNum $numLines" );
     276        $reply = $this->get_reply();
     277        if (!$this->is_ok($reply)) {
     278                return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
    248279        }
    249280
    250         $count = 0;
    251281        $MsgArray = array();
    252 
    253         $line = fgets($fp,$buffer);
    254         while ( !preg_match('/^\.\r\n/',$line))
    255         {
    256             $MsgArray[$count] = $line;
    257             $count++;
    258             $line = fgets($fp,$buffer);
    259             if(empty($line))    { break; }
     282        while ( ( $line = fgets($this->FP, $this->BUFFER) ) && !$this->iseof( $line ) ) {
     283                $MsgArray[] = $line;
    260284        }
    261285
    262286        return $MsgArray;
    263287    }
    264288
    265     function pop_list ($msgNum = "") {
     289    function pop_list ($msgNum = '') {
    266290        //  If called with an argument, returns that msgs' size in octets
    267291        //  No argument returns an associative array of undeleted
    268292        //  msg numbers and their sizes in octets
    269293
    270         if(!isset($this->FP))
    271         {
    272             $this->ERROR = "POP3 pop_list: " . _("No connection to server");
    273             return false;
    274         }
    275         $fp = $this->FP;
    276         $Total = $this->COUNT;
    277         if( (!$Total) or ($Total == -1) )
    278         {
    279             return false;
    280         }
    281         if($Total == 0)
    282         {
    283             return array("0","0");
    284             // return -1;   // mailbox empty
     294        if (!isset($this->FP))
     295            return $this->set_error( _("No connection to server"), __FUNCTION__ );
     296       
     297        if ($this->COUNT === FALSE || $this->COUNT == -1) {
     298                return false;
     299        }
     300       
     301        if ( $this->COUNT == 0 ) {
     302                return array(0,0);
    285303        }
    286304
    287305        $this->update_timer();
    288 
    289         if(!empty($msgNum))
    290         {
    291             $cmd = "LIST $msgNum";
    292             fwrite($fp,"$cmd\r\n");
    293             $reply = fgets($fp,$this->BUFFER);
    294             $reply = $this->strip_clf($reply);
    295             if($this->DEBUG) {
    296                 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
     306               
     307        if (!$msgNum) {
     308                $this->send( "LIST $msgNum" );
     309
     310            $reply = $this->get_reply();
     311            if (!$this->is_ok($reply))
     312                return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
     313               
     314            // Gmail and some email providers return the word 'messages' in the response, example:
     315            // +OK 307 messages (23191343 bytes)
     316            preg_match_all('([0-9]+)', $reply, $m);
     317            $size = FALSE;
     318            if( !empty($m[0]) ) {
     319                $size = array_pop($m[0]);
    297320            }
    298             if(!$this->is_ok($reply))
    299             {
    300                 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
    301                 return false;
    302             }
    303             list($junk,$num,$size) = preg_split('/\s+/',$reply);
    304321            return $size;
    305322        }
    306         $cmd = "LIST";
    307         $reply = $this->send_cmd($cmd);
    308         if(!$this->is_ok($reply))
    309         {
    310             $reply = $this->strip_clf($reply);
    311             $this->ERROR = "POP3 pop_list: " . _("Error ") .  "[$reply]";
    312             return false;
    313         }
    314         $MsgArray = array();
    315         $MsgArray[0] = $Total;
    316         for($msgC=1;$msgC <= $Total; $msgC++)
    317         {
    318             if($msgC > $Total) { break; }
    319             $line = fgets($fp,$this->BUFFER);
    320             $line = $this->strip_clf($line);
    321             if(strpos($line, '.') === 0)
    322             {
    323                 $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
    324                 return false;
    325             }
    326             list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
    327             settype($thisMsg,"integer");
    328             if($thisMsg != $msgC)
    329             {
    330                 $MsgArray[$msgC] = "deleted";
    331             }
    332             else
    333             {
    334                 $MsgArray[$msgC] = $msgSize;
    335             }
    336         }
    337         return $MsgArray;
     323       
     324        $reply = $this->send_cmd('LIST');
     325        if (!$this->is_ok($reply))
     326            return $this->set_error( _("Error ") .  "[$reply]", __FUNCTION__ );
     327           
     328        $msg = array();   
     329        for( $i=1; $i <= $this->COUNT; $i++ ) {
     330                $info = $this->get_reply();
     331                if ( !empty($info) ) {
     332                        $info = explode(' ', $info);
     333                        $msg[ $info[0] ] = $info[1];
     334                }
     335        }
     336       
     337        return $msg;
    338338    }
    339339
    340340    function get ($msgNum) {
    341341        //  Retrieve the specified msg number. Returns an array
    342342        //  where each line of the msg is an array element.
    343343
    344         if(!isset($this->FP))
    345         {
    346             $this->ERROR = "POP3 get: " . _("No connection to server");
    347             return false;
    348         }
     344        if (!isset($this->FP))
     345                return $this->set_error( _("No connection to server"), __FUNCTION__ );
    349346
    350347        $this->update_timer();
     348        $reply = $this->send_cmd('RETR '. $msgNum);
    351349
    352         $fp = $this->FP;
    353         $buffer = $this->BUFFER;
    354         $cmd = "RETR $msgNum";
    355         $reply = $this->send_cmd($cmd);
    356 
    357         if(!$this->is_ok($reply))
    358         {
    359             $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
    360             return false;
    361         }
     350        if(!$this->is_ok($reply))
     351                return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
    362352
    363         $count = 0;
    364353        $MsgArray = array();
    365354
    366         $line = fgets($fp,$buffer);
    367         while ( !preg_match('/^\.\r\n/',$line))
    368         {
    369             if ( $line{0} == '.' ) { $line = substr($line,1); }
    370             $MsgArray[$count] = $line;
    371             $count++;
    372             $line = fgets($fp,$buffer);
    373             if(empty($line))    { break; }
     355        while ( ( $line = fgets($this->FP, $this->BUFFER) ) && !$this->iseof( $line ) ) {
     356                $MsgArray[] = $line;
    374357        }
     358       
    375359        return $MsgArray;
    376360    }
     361   
     362    private function iseof( $line ) {
     363        return $line == ".\r\n" || empty($line);
     364    }
    377365
    378366    function last ( $type = "count" ) {
     367        static $saved_last;
    379368        //  Returns the highest msg number in the mailbox.
    380369        //  returns -1 on error, 0+ on success, if type != count
    381370        //  results in a popstat() call (2 element array returned)
    382371
    383         $last = -1;
    384         if(!isset($this->FP))
    385         {
    386             $this->ERROR = "POP3 last: " . _("No connection to server");
    387             return $last;
     372        if (!isset($this->FP)) {
     373                $this->set_error( _("No connection to server"), __FUNCTION__ );
     374                return -1;
    388375        }
    389376
    390377        $reply = $this->send_cmd("STAT");
    391         if(!$this->is_ok($reply))
    392         {
    393             $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
    394             return $last;
     378        if(!$this->is_ok($reply)) {
     379                $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
     380                return -1;
    395381        }
    396382
    397         $Vars = preg_split('/\s+/',$reply);
    398         $count = $Vars[1];
    399         $size = $Vars[2];
    400         settype($count,"integer");
    401         settype($size,"integer");
    402         if($type != "count")
    403         {
    404             return array($count,$size);
     383        if( !$saved_last ) {
     384                list( $ok, $count, $size) = explode( ' ', $reply);
     385                $saved_last = array( $count, $size );
    405386        }
    406         return $count;
     387       
     388        return $type == 'count' ? $saved_last['count'] : $saved_last;
    407389    }
    408390
    409391    function reset () {
     
    411393        //  resetting the status of ALL msgs to not be deleted.
    412394        //  This method automatically closes the connection to the server.
    413395
    414         if(!isset($this->FP))
    415         {
    416             $this->ERROR = "POP3 reset: " . _("No connection to server");
    417             return false;
    418         }
     396        if (!isset($this->FP))
     397                return $this->set_error( _("No connection to server"), __FUNCTION__ );
     398               
    419399        $reply = $this->send_cmd("RSET");
    420400        if(!$this->is_ok($reply))
    421401        {
     
    430410        return true;
    431411    }
    432412
    433     function send_cmd ( $cmd = "" )
    434     {
     413    function send_cmd ( $cmd ) {
    435414        //  Sends a user defined command string to the
    436415        //  POP server and returns the results. Useful for
    437416        //  non-compliant or custom POP servers.
    438         //  Do NOT includ the \r\n as part of your command
    439         //  string - it will be appended automatically.
    440417
    441418        //  The return value is a standard fgets() call, which
    442419        //  will read up to $this->BUFFER bytes of data, until it
     
    445422        //  This method works best if $cmd responds with only
    446423        //  one line of data.
    447424
    448         if(!isset($this->FP))
    449         {
    450             $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
    451             return false;
    452         }
     425        if (!isset($this->FP))
     426            return $this->set_error( _("No connection to server"), __FUNCTION__ );
    453427
    454         if(empty($cmd))
    455         {
    456             $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
     428        if (empty($cmd)) {
     429            $this->set_error( _("Empty command string") , __FUNCTION__ );
    457430            return "";
    458431        }
    459432
    460         $fp = $this->FP;
    461         $buffer = $this->BUFFER;
    462433        $this->update_timer();
    463         fwrite($fp,"$cmd\r\n");
    464         $reply = fgets($fp,$buffer);
    465         $reply = $this->strip_clf($reply);
    466         if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
     434        $reply = false;
     435        if ( $this->send( $cmd ) )
     436                $reply = $this->get_reply();
     437       
    467438        return $reply;
    468439    }
    469440
     
    471442        //  Closes the connection to the POP3 server, deleting
    472443        //  any msgs marked as deleted.
    473444
    474         if(!isset($this->FP))
    475         {
    476             $this->ERROR = "POP3 quit: " . _("connection does not exist");
    477             return false;
    478         }
    479         $fp = $this->FP;
    480         $cmd = "QUIT";
    481         fwrite($fp,"$cmd\r\n");
    482         $reply = fgets($fp,$this->BUFFER);
    483         $reply = $this->strip_clf($reply);
    484         if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
    485         fclose($fp);
    486         unset($this->FP);
     445        if (!isset($this->FP))
     446            return $this->set_error( _("Connection already closed."), __FUNCTION__ );
     447       
     448        $this->send('QUIT') && $this->get_reply();
     449       
     450        fclose($this->FP);
    487451        return true;
    488452    }
    489453
     
    493457
    494458        $PopArray = $this->last("array");
    495459
    496         if($PopArray == -1) { return false; }
    497 
    498         if( (!$PopArray) or (empty($PopArray)) )
    499         {
    500             return false;
    501         }
     460        if ( empty($PopArray) || $PopArray == -1 )
     461                return false;
     462       
    502463        return $PopArray;
    503464    }
    504465
     
    509470        //  undeleted msg num is a key, and the msg's uidl is the element
    510471        //  Array element 0 will contain the total number of msgs
    511472
    512         if(!isset($this->FP)) {
    513             $this->ERROR = "POP3 uidl: " . _("No connection to server");
    514             return false;
    515         }
     473        if (!isset($this->FP))
     474            return $this->set_error( _("No connection to server"), __FUNCTION__ );
    516475
    517         $fp = $this->FP;
    518         $buffer = $this->BUFFER;
    519 
    520         if(!empty($msgNum)) {
    521             $cmd = "UIDL $msgNum";
    522             $reply = $this->send_cmd($cmd);
     476        if (!empty($msgNum)) {
     477            $reply = $this->send_cmd("UIDL $msgNum");
     478           
    523479            if(!$this->is_ok($reply))
    524             {
    525                 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
    526                 return false;
    527             }
     480                return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
     481           
    528482            list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
    529483            return $myUidl;
    530         } else {
    531             $this->update_timer();
    532 
    533             $UIDLArray = array();
    534             $Total = $this->COUNT;
    535             $UIDLArray[0] = $Total;
    536 
    537             if ($Total < 1)
    538             {
    539                 return $UIDLArray;
    540             }
    541             $cmd = "UIDL";
    542             fwrite($fp, "UIDL\r\n");
    543             $reply = fgets($fp, $buffer);
    544             $reply = $this->strip_clf($reply);
    545             if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
    546             if(!$this->is_ok($reply))
    547             {
    548                 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
    549                 return false;
    550             }
     484        }
     485               
     486        $this->update_timer();
    551487
    552             $line = "";
    553             $count = 1;
    554             $line = fgets($fp,$buffer);
    555             while ( !preg_match('/^\.\r\n/',$line)) {
    556                 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
    557                 $msgUidl = $this->strip_clf($msgUidl);
    558                 if($count == $msg) {
    559                     $UIDLArray[$msg] = $msgUidl;
    560                 }
    561                 else
    562                 {
    563                     $UIDLArray[$count] = 'deleted';
    564                 }
    565                 $count++;
    566                 $line = fgets($fp,$buffer);
    567             }
     488        $this->send('UIDL');
     489        $reply = $this->get_reply();
     490           
     491        if ( !$this->is_ok($reply) )
     492            return $this->set_error( _("Error ") . "[$reply]", __FUNCTION__ );
     493
     494                $response = array();
     495        while ( ( $line = fgets($this->FP, $this->BUFFER) ) && !$this->iseof( $line ) ) {
     496                $exploded = preg_split('#\s+#', $line);
     497                $response[ $exploded[0] ] = $exploded[1];
    568498        }
    569         return $UIDLArray;
     499               
     500        return $response;
    570501    }
    571502
    572     function delete ($msgNum = "") {
     503    function delete ($msgNum) {
    573504        //  Flags a specified msg as deleted. The msg will not
    574505        //  be deleted until a quit() method is called.
    575506
    576         if(!isset($this->FP))
    577         {
    578             $this->ERROR = "POP3 delete: " . _("No connection to server");
    579             return false;
    580         }
    581         if(empty($msgNum))
    582         {
    583             $this->ERROR = "POP3 delete: " . _("No msg number submitted");
    584             return false;
    585         }
     507        if (!isset($this->FP))
     508                return $this->set_error( _("No connection to server"), __FUNCTION__ );
     509               
     510        if (empty($msgNum))
     511            return $this->set_error( _("No msg number submitted"), __FUNCTION__ );
     512
    586513        $reply = $this->send_cmd("DELE $msgNum");
    587         if(!$this->is_ok($reply))
    588         {
    589             $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
    590             return false;
    591         }
     514        if (!$this->is_ok($reply))
     515                return $this->set_error( _("Command failed ") . "[$reply]", __FUNCTION__ );
     516       
    592517        return true;
    593518    }
    594519
     
    596521
    597522    //  The following methods are internal to the class.
    598523
    599     function is_ok ($cmd = "") {
     524    function is_ok ($cmd) {
    600525        //  Return true or false on +OK or -ERR
    601 
    602         if( empty($cmd) )
    603             return false;
    604         else
    605             return( stripos($cmd, '+OK') !== false );
     526        return stristr($cmd, '+OK') !== FALSE;
    606527    }
    607528
    608     function strip_clf ($text = "") {
     529    function strip_clf ($text) {
    609530        // Strips \r\n from server responses
    610531
    611         if(empty($text))
    612             return $text;
    613         else {
    614             $stripped = str_replace(array("\r","\n"),'',$text);
    615             return $stripped;
    616         }
     532        return str_replace(array("\r","\n"), '', $text);
    617533    }
    618534
    619535    function parse_banner ( $server_text ) {
    620         $outside = true;
    621         $banner = "";
    622         $length = strlen($server_text);
    623         for($count =0; $count < $length; $count++)
    624         {
    625             $digit = substr($server_text,$count,1);
    626             if(!empty($digit))             {
    627                 if( (!$outside) && ($digit != '<') && ($digit != '>') )
    628                 {
    629                     $banner .= $digit;
    630                 }
    631                 if ($digit == '<')
    632                 {
    633                     $outside = false;
    634                 }
    635                 if($digit == '>')
    636                 {
    637                     $outside = true;
    638                 }
    639             }
    640         }
    641         $banner = $this->strip_clf($banner);    // Just in case
    642         return "<$banner>";
     536        if (empty($server_text)) {
     537                return $server_text;
     538        }
     539       
     540        preg_match_all('#<([^<>]+)>#', $server_text, $matches);
     541       
     542        $banners = array();
     543        if (isset($matches[1]) && !empty($matches[1])) {
     544                $banners = $matches[1];
     545        }
     546       
     547        return implode('', $banners);
     548    }
     549   
     550    function get_error() {
     551        $error  =  $this->ERROR;
     552        unset($this->ERROR);
     553        return $error;
    643554    }
    644555
    645556}   // End class
    646557
    647 // For php4 compatibility
    648 if (!function_exists("stripos")) {
    649     function stripos($haystack, $needle){
    650         return strpos($haystack, stristr( $haystack, $needle ));
    651     }
    652 }