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