e69294d85112a5786f959bfe8670421ec16a619f
4 ** mail_fetch/setup.php
6 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
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.
19 var $ERROR = ''; // Error string.
21 var $TIMEOUT = 60; // Default timeout before giving up on a
24 var $COUNT = -1; // Mailbox msg count
26 var $BUFFER = 512; // Socket buffer for socket fgets() calls.
27 // Per RFC 1939 the returned line a POP3
28 // server can send is 512 bytes.
30 var $FP = ''; // The connection to the server's
33 var $MAILSERVER = ''; // Set this to hard code the server name
35 var $DEBUG = FALSE; // set to true to echo pop3
36 // commands and responses to error_log
37 // this WILL log passwords!
39 var $BANNER = ''; // Holds the banner returned by the
40 // pop server - used for apop()
42 var $RFC1939 = TRUE; // Set by noop(). See rfc1939.txt
45 var $ALLOWAPOP = FASLE
; // Allow or disallow apop()
46 // This must be set to true
49 function POP3 ( $server = '', $timeout = '' ) {
50 settype($this->BUFFER
,"integer");
51 if( !empty($server) ) {
52 // Do not allow programs to alter MAILSERVER
53 // if it is already specified. They can get around
54 // this if they -really- want to, so don't count on it.
55 if(empty($this->MAILSERVER
))
56 $this->MAILSERVER
= $server;
58 if(!empty($timeout)) {
59 settype($timeout,"integer");
60 $this->TIMEOUT
= $timeout;
61 set_time_limit($timeout);
66 function update_timer () {
67 set_time_limit($this->TIMEOUT
);
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
75 // If MAILSERVER is set, override $server with it's value
77 if(!empty($this->MAILSERVER
))
78 $server = $this->MAILSERVER
;
81 $this->ERROR
= _("POP3 connect:") . ' ' . _("No server specified");
86 $fp = fsockopen("$server", $port, $errno, $errstr);
89 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
94 socket_set_blocking($fp,-1);
95 $this->update_timer();
96 $reply = fgets($fp,$this->BUFFER
);
97 $reply = $this->strip_clf($reply);
99 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
100 if(!$this->is_ok($reply)) {
101 $this->ERROR
= _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
106 $this->BANNER
= $this->parse_banner($reply);
107 $this->RFC1939
= $this->noop();
109 $this->ERROR
= _("POP3: premature NOOP OK, NOT an RFC 1939 Compliant server");
118 if(!isset($this->FP
)) {
119 $this->ERROR
= _("POP3 noop:") . ' ' . _("No connection to server");
123 $reply = $this->send_cmd( $cmd );
124 return( $this->is_ok( $reply ) );
128 function user ($user = "") {
129 // Sends the USER command, returns true or false
132 $this->ERROR
= _("POP3 user:") . ' ' . _("no login ID submitted");
134 } elseif(!isset($this->FP
)) {
135 $this->ERROR
= _("POP3 user:") . ' ' . _("connection not established");
138 $reply = $this->send_cmd("USER $user");
139 if(!$this->is_ok($reply)) {
140 $this->ERROR
= _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
147 function pass ($pass = "") {
148 // Sends the PASS command, returns # of msgs in mailbox,
149 // returns false (undef) on Auth failure
152 $this->ERROR
= _("POP3 pass:") . ' ' . _("No password submitted");
154 } elseif(!isset($this->FP
)) {
155 $this->ERROR
= _("POP3 pass:") . ' ' . _("connection not established");
158 $reply = $this->send_cmd("PASS $pass");
159 if(!$this->is_ok($reply)) {
160 $this->ERROR
= _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
165 $count = $this->last("count");
166 $this->COUNT
= $count;
167 $this->RFC1939
= $this->noop();
168 if(!$this->RFC1939
) {
169 $this->ERROR
= _("POP3 pass:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
178 function apop ($login,$pass) {
179 // Attempts an APOP login. If this fails, it'll
180 // try a standard login. YOUR SERVER MUST SUPPORT
181 // THE USE OF THE APOP COMMAND!
182 // (apop is optional per rfc1939)
184 if(!isset($this->FP
)) {
185 $this->ERROR
= _("POP3 apop:") . ' ' . _("No connection to server");
187 } elseif(!$this->ALLOWAPOP
) {
188 $retVal = $this->login($login,$pass);
190 } elseif(empty($login)) {
191 $this->ERROR
= _("POP3 apop:") . ' ' . _("No login ID submitted");
193 } elseif(empty($pass)) {
194 $this->ERROR
= _("POP3 apop:") . ' ' . _("No password submitted");
197 $banner = $this->BANNER
;
198 if( (!$banner) or (empty($banner)) ) {
199 $this->ERROR
= _("POP3 apop:") . ' ' . _("No server banner") . ' - ' . _("abort");
200 $retVal = $this->login($login,$pass);
203 $AuthString = $banner;
204 $AuthString .= $pass;
205 $APOPString = md5($AuthString);
206 $cmd = "APOP $login $APOPString";
207 $reply = $this->send_cmd($cmd);
208 if(!$this->is_ok($reply)) {
209 $this->ERROR
= _("POP3 apop:") . ' ' . _("apop authentication failed") . ' - ' . _("abort");
210 $retVal = $this->login($login,$pass);
214 $count = $this->last("count");
215 $this->COUNT
= $count;
216 $this->RFC1939
= $this->noop();
217 if(!$this->RFC1939
) {
218 $this->ERROR
= _("POP3 apop:") . ' ' . _("NOOP failed. Server not RFC 1939 compliant");
228 function login ($login = "", $pass = "") {
229 // Sends both user and pass. Returns # of msgs in mailbox or
230 // false on failure (or -1, if the error occurs while getting
231 // the number of messages.)
233 if( !isset($this->FP
) ) {
234 $this->ERROR
= _("POP3 login:") . ' ' . _("No connection to server");
238 if( !$this->user( $login ) ) {
239 // Preserve the error generated by user()
242 $count = $this->pass($pass);
243 if( (!$count) ||
($count == -1) ) {
244 // Preserve the error generated by last() and pass()
252 function top ($msgNum, $numLines = "0") {
253 // Gets the header and first $numLines of the msg body
254 // returns data in an array with each returned line being
255 // an array element. If $numLines is empty, returns
256 // only the header information, and none of the body.
258 if(!isset($this->FP
)) {
259 $this->ERROR
= _("POP3 top:") . ' ' . _("No connection to server");
262 $this->update_timer();
265 $buffer = $this->BUFFER
;
266 $cmd = "TOP $msgNum $numLines";
267 fwrite($fp, "TOP $msgNum $numLines\r\n");
268 $reply = fgets($fp, $buffer);
269 $reply = $this->strip_clf($reply);
271 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
273 if(!$this->is_ok($reply))
275 $this->ERROR
= _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
282 $line = fgets($fp,$buffer);
283 while ( !ereg("^\.\r\n",$line))
285 $MsgArray[$count] = $line;
287 $line = fgets($fp,$buffer);
288 if(empty($line)) { break; }
294 function pop_list ($msgNum = "") {
295 // If called with an argument, returns that msgs' size in octets
296 // No argument returns an associative array of undeleted
297 // msg numbers and their sizes in octets
299 if(!isset($this->FP
))
301 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("No connection to server");
305 $Total = $this->COUNT
;
306 if( (!$Total) or ($Total == -1) )
312 return array("0","0");
313 // return -1; // mailbox empty
316 $this->update_timer();
320 $cmd = "LIST $msgNum";
321 fwrite($fp,"$cmd\r\n");
322 $reply = fgets($fp,$this->BUFFER
);
323 $reply = $this->strip_clf($reply);
325 @error_log
("POP3 SEND [$cmd] GOT [$reply]",0);
327 if(!$this->is_ok($reply))
329 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
332 list($junk,$num,$size) = explode(" ",$reply);
336 $reply = $this->send_cmd($cmd);
337 if(!$this->is_ok($reply))
339 $reply = $this->strip_clf($reply);
340 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
344 $MsgArray[0] = $Total;
345 for($msgC=1;$msgC <= $Total; $msgC++
)
347 if($msgC > $Total) { break; }
348 $line = fgets($fp,$this->BUFFER
);
349 $line = $this->strip_clf($line);
350 if(ereg("^\.",$line))
352 $this->ERROR
= _("POP3 pop_list:") . ' ' . _("Premature end of list");
355 list($thisMsg,$msgSize) = explode(" ",$line);
356 settype($thisMsg,"integer");
357 if($thisMsg != $msgC)
359 $MsgArray[$msgC] = "deleted";
363 $MsgArray[$msgC] = $msgSize;
369 function get ($msgNum) {
370 // Retrieve the specified msg number. Returns an array
371 // where each line of the msg is an array element.
373 if(!isset($this->FP
))
375 $this->ERROR
= _("POP3 get:") . ' ' . _("No connection to server");
379 $this->update_timer();
382 $buffer = $this->BUFFER
;
383 $cmd = "RETR $msgNum";
384 $reply = $this->send_cmd($cmd);
386 if(!$this->is_ok($reply))
388 $this->ERROR
= _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
395 $line = fgets($fp,$buffer);
396 while ( !ereg("^\.\r\n",$line))
398 $MsgArray[$count] = $line;
400 $line = fgets($fp,$buffer);
401 if(empty($line)) { break; }
406 function last ( $type = "count" ) {
407 // Returns the highest msg number in the mailbox.
408 // returns -1 on error, 0+ on success, if type != count
409 // results in a popstat() call (2 element array returned)
412 if(!isset($this->FP
))
414 $this->ERROR
= _("POP3 last:") . ' ' . _("No connection to server");
418 $reply = $this->send_cmd("STAT");
419 if(!$this->is_ok($reply))
421 $this->ERROR
= _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
425 $Vars = explode(" ",$reply);
428 settype($count,"integer");
429 settype($size,"integer");
432 return array($count,$size);
438 // Resets the status of the remote server. This includes
439 // resetting the status of ALL msgs to not be deleted.
440 // This method automatically closes the connection to the server.
442 if(!isset($this->FP
))
444 $this->ERROR
= _("POP3 reset:") . ' ' . _("No connection to server");
447 $reply = $this->send_cmd("RSET");
448 if(!$this->is_ok($reply))
450 // The POP3 RSET command -never- gives a -ERR
451 // response - if it ever does, something truely
454 $this->ERROR
= _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
455 @error_log
("POP3 reset: ERROR [$reply]",0);
461 function send_cmd ( $cmd = "" )
463 // Sends a user defined command string to the
464 // POP server and returns the results. Useful for
465 // non-compliant or custom POP servers.
466 // Do NOT includ the \r\n as part of your command
467 // string - it will be appended automatically.
469 // The return value is a standard fgets() call, which
470 // will read up to $this->BUFFER bytes of data, until it
471 // encounters a new line, or EOF, whichever happens first.
473 // This method works best if $cmd responds with only
476 if(!isset($this->FP
))
478 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("No connection to server");
484 $this->ERROR
= _("POP3 send_cmd:") . ' ' . _("Empty command string");
489 $buffer = $this->BUFFER
;
490 $this->update_timer();
491 fwrite($fp,"$cmd\r\n");
492 $reply = fgets($fp,$buffer);
493 $reply = $this->strip_clf($reply);
494 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
499 // Closes the connection to the POP3 server, deleting
500 // any msgs marked as deleted.
502 if(!isset($this->FP
))
504 $this->ERROR
= _("POP3 quit:") . ' ' . _("connection does not exist");
509 fwrite($fp,"$cmd\r\n");
510 $reply = fgets($fp,$this->BUFFER
);
511 $reply = $this->strip_clf($reply);
512 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
518 function popstat () {
519 // Returns an array of 2 elements. The number of undeleted
520 // msgs in the mailbox, and the size of the mbox in octets.
522 $PopArray = $this->last("array");
524 if($PopArray == -1) { return false; }
526 if( (!$PopArray) or (empty($PopArray)) )
533 function uidl ($msgNum = "")
535 // Returns the UIDL of the msg specified. If called with
536 // no arguments, returns an associative array where each
537 // undeleted msg num is a key, and the msg's uidl is the element
538 // Array element 0 will contain the total number of msgs
540 if(!isset($this->FP
)) {
541 $this->ERROR
= _("POP3 uidl:") . ' ' . _("No connection to server");
546 $buffer = $this->BUFFER
;
548 if(!empty($msgNum)) {
549 $cmd = "UIDL $msgNum";
550 $reply = $this->send_cmd($cmd);
551 if(!$this->is_ok($reply))
553 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
556 list ($ok,$num,$myUidl) = explode(" ",$reply);
559 $this->update_timer();
561 $UIDLArray = array();
562 $Total = $this->COUNT
;
563 $UIDLArray[0] = $Total;
570 fwrite($fp, "UIDL\r\n");
571 $reply = fgets($fp, $buffer);
572 $reply = $this->strip_clf($reply);
573 if($this->DEBUG
) { @error_log
("POP3 SEND [$cmd] GOT [$reply]",0); }
574 if(!$this->is_ok($reply))
576 $this->ERROR
= _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
582 $line = fgets($fp,$buffer);
583 while ( !ereg("^\.\r\n",$line)) {
584 if(ereg("^\.\r\n",$line)) {
587 list ($msg,$msgUidl) = explode(" ",$line);
588 $msgUidl = $this->strip_clf($msgUidl);
590 $UIDLArray[$msg] = $msgUidl;
594 $UIDLArray[$count] = 'deleted';
597 $line = fgets($fp,$buffer);
603 function delete ($msgNum = "") {
604 // Flags a specified msg as deleted. The msg will not
605 // be deleted until a quit() method is called.
607 if(!isset($this->FP
))
609 $this->ERROR
= _("POP3 delete:") . ' ' . _("No connection to server");
614 $this->ERROR
= _("POP3 delete:") . ' ' . _("No msg number submitted");
617 $reply = $this->send_cmd("DELE $msgNum");
618 if(!$this->is_ok($reply))
620 $this->ERROR
= _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
626 // *********************************************************
628 // The following methods are internal to the class.
630 function is_ok ($cmd = "") {
631 // Return true or false on +OK or -ERR
636 return( ereg ("^\+OK", $cmd ) );
639 function strip_clf ($text = "") {
640 // Strips \r\n from server responses
645 $stripped = str_replace("\r",'',$text);
646 $stripped = str_replace("\n",'',$stripped);
651 function parse_banner ( $server_text ) {
654 $length = strlen($server_text);
655 for($count =0; $count < $length; $count++
)
657 $digit = substr($server_text,$count,1);
659 if( (!$outside) && ($digit != '<') && ($digit != '>') )
673 $banner = $this->strip_clf($banner); // Just in case