e69294d85112a5786f959bfe8670421ec16a619f
[squirrelmail.git] / plugins / mail_fetch / class.POP3.php
1 <?
2
3 /**
4 ** mail_fetch/setup.php
5 **
6 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
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 ** pop3 class
14 **
15 ** $Id$
16 **/
17
18 class POP3 {
19 var $ERROR = ''; // Error string.
20
21 var $TIMEOUT = 60; // Default timeout before giving up on a
22 // network operation.
23
24 var $COUNT = -1; // Mailbox msg count
25
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.
29
30 var $FP = ''; // The connection to the server's
31 // file descriptor
32
33 var $MAILSERVER = ''; // Set this to hard code the server name
34
35 var $DEBUG = FALSE; // set to true to echo pop3
36 // commands and responses to error_log
37 // this WILL log passwords!
38
39 var $BANNER = ''; // Holds the banner returned by the
40 // pop server - used for apop()
41
42 var $RFC1939 = TRUE; // Set by noop(). See rfc1939.txt
43 //
44
45 var $ALLOWAPOP = FASLE; // Allow or disallow apop()
46 // This must be set to true
47 // manually
48
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;
57 }
58 if(!empty($timeout)) {
59 settype($timeout,"integer");
60 $this->TIMEOUT = $timeout;
61 set_time_limit($timeout);
62 }
63 return true;
64 }
65
66 function update_timer () {
67 set_time_limit($this->TIMEOUT);
68 return true;
69 }
70
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(!empty($this->MAILSERVER))
78 $server = $this->MAILSERVER;
79
80 if(empty($server)){
81 $this->ERROR = _("POP3 connect:") . ' ' . _("No server specified");
82 unset($this->FP);
83 return false;
84 }
85
86 $fp = fsockopen("$server", $port, $errno, $errstr);
87
88 if(!$fp) {
89 $this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$errno] [$errstr]";
90 unset($this->FP);
91 return false;
92 }
93
94 socket_set_blocking($fp,-1);
95 $this->update_timer();
96 $reply = fgets($fp,$this->BUFFER);
97 $reply = $this->strip_clf($reply);
98 if($this->DEBUG)
99 error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
100 if(!$this->is_ok($reply)) {
101 $this->ERROR = _("POP3 connect:") . ' ' . _("Error ") . "[$reply]";
102 unset($this->FP);
103 return false;
104 }
105 $this->FP = $fp;
106 $this->BANNER = $this->parse_banner($reply);
107 $this->RFC1939 = $this->noop();
108 if($this->RFC1939) {
109 $this->ERROR = _("POP3: premature NOOP OK, NOT an RFC 1939 Compliant server");
110 $this->quit();
111 return false;
112 } else
113 return true;
114 }
115
116 function noop () {
117
118 if(!isset($this->FP)) {
119 $this->ERROR = _("POP3 noop:") . ' ' . _("No connection to server");
120 return false;
121 } else {
122 $cmd = "NOOP";
123 $reply = $this->send_cmd( $cmd );
124 return( $this->is_ok( $reply ) );
125 }
126 }
127
128 function user ($user = "") {
129 // Sends the USER command, returns true or false
130
131 if( empty($user) ) {
132 $this->ERROR = _("POP3 user:") . ' ' . _("no login ID submitted");
133 return false;
134 } elseif(!isset($this->FP)) {
135 $this->ERROR = _("POP3 user:") . ' ' . _("connection not established");
136 return false;
137 } else {
138 $reply = $this->send_cmd("USER $user");
139 if(!$this->is_ok($reply)) {
140 $this->ERROR = _("POP3 user:") . ' ' . _("Error ") . "[$reply]";
141 return false;
142 } else
143 return true;
144 }
145 }
146
147 function pass ($pass = "") {
148 // Sends the PASS command, returns # of msgs in mailbox,
149 // returns false (undef) on Auth failure
150
151 if(empty($pass)) {
152 $this->ERROR = _("POP3 pass:") . ' ' . _("No password submitted");
153 return false;
154 } elseif(!isset($this->FP)) {
155 $this->ERROR = _("POP3 pass:") . ' ' . _("connection not established");
156 return false;
157 } else {
158 $reply = $this->send_cmd("PASS $pass");
159 if(!$this->is_ok($reply)) {
160 $this->ERROR = _("POP3 pass:") . ' ' . _("authentication failed ") . "[$reply]";
161 $this->quit();
162 return false;
163 } else {
164 // Auth successful.
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");
170 $this->quit();
171 return false;
172 } else
173 return $count;
174 }
175 }
176 }
177
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)
183
184 if(!isset($this->FP)) {
185 $this->ERROR = _("POP3 apop:") . ' ' . _("No connection to server");
186 return false;
187 } elseif(!$this->ALLOWAPOP) {
188 $retVal = $this->login($login,$pass);
189 return $retVal;
190 } elseif(empty($login)) {
191 $this->ERROR = _("POP3 apop:") . ' ' . _("No login ID submitted");
192 return false;
193 } elseif(empty($pass)) {
194 $this->ERROR = _("POP3 apop:") . ' ' . _("No password submitted");
195 return false;
196 } else {
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);
201 return $retVal;
202 } else {
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);
211 return $retVal;
212 } else {
213 // Auth successful.
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");
219 $this->quit();
220 return false;
221 } else
222 return $count;
223 }
224 }
225 }
226 }
227
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.)
232
233 if( !isset($this->FP) ) {
234 $this->ERROR = _("POP3 login:") . ' ' . _("No connection to server");
235 return false;
236 } else {
237 $fp = $this->FP;
238 if( !$this->user( $login ) ) {
239 // Preserve the error generated by user()
240 return false;
241 } else {
242 $count = $this->pass($pass);
243 if( (!$count) || ($count == -1) ) {
244 // Preserve the error generated by last() and pass()
245 return false;
246 } else
247 return $count;
248 }
249 }
250 }
251
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.
257
258 if(!isset($this->FP)) {
259 $this->ERROR = _("POP3 top:") . ' ' . _("No connection to server");
260 return false;
261 }
262 $this->update_timer();
263
264 $fp = $this->FP;
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);
270 if($this->DEBUG) {
271 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
272 }
273 if(!$this->is_ok($reply))
274 {
275 $this->ERROR = _("POP3 top:") . ' ' . _("Error ") . "[$reply]";
276 return false;
277 }
278
279 $count = 0;
280 $MsgArray = array();
281
282 $line = fgets($fp,$buffer);
283 while ( !ereg("^\.\r\n",$line))
284 {
285 $MsgArray[$count] = $line;
286 $count++;
287 $line = fgets($fp,$buffer);
288 if(empty($line)) { break; }
289 }
290
291 return $MsgArray;
292 }
293
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
298
299 if(!isset($this->FP))
300 {
301 $this->ERROR = _("POP3 pop_list:") . ' ' . _("No connection to server");
302 return false;
303 }
304 $fp = $this->FP;
305 $Total = $this->COUNT;
306 if( (!$Total) or ($Total == -1) )
307 {
308 return false;
309 }
310 if($Total == 0)
311 {
312 return array("0","0");
313 // return -1; // mailbox empty
314 }
315
316 $this->update_timer();
317
318 if(!empty($msgNum))
319 {
320 $cmd = "LIST $msgNum";
321 fwrite($fp,"$cmd\r\n");
322 $reply = fgets($fp,$this->BUFFER);
323 $reply = $this->strip_clf($reply);
324 if($this->DEBUG) {
325 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
326 }
327 if(!$this->is_ok($reply))
328 {
329 $this->ERROR = _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
330 return false;
331 }
332 list($junk,$num,$size) = explode(" ",$reply);
333 return $size;
334 }
335 $cmd = "LIST";
336 $reply = $this->send_cmd($cmd);
337 if(!$this->is_ok($reply))
338 {
339 $reply = $this->strip_clf($reply);
340 $this->ERROR = _("POP3 pop_list:") . ' ' . _("Error ") . "[$reply]";
341 return false;
342 }
343 $MsgArray = array();
344 $MsgArray[0] = $Total;
345 for($msgC=1;$msgC <= $Total; $msgC++)
346 {
347 if($msgC > $Total) { break; }
348 $line = fgets($fp,$this->BUFFER);
349 $line = $this->strip_clf($line);
350 if(ereg("^\.",$line))
351 {
352 $this->ERROR = _("POP3 pop_list:") . ' ' . _("Premature end of list");
353 return false;
354 }
355 list($thisMsg,$msgSize) = explode(" ",$line);
356 settype($thisMsg,"integer");
357 if($thisMsg != $msgC)
358 {
359 $MsgArray[$msgC] = "deleted";
360 }
361 else
362 {
363 $MsgArray[$msgC] = $msgSize;
364 }
365 }
366 return $MsgArray;
367 }
368
369 function get ($msgNum) {
370 // Retrieve the specified msg number. Returns an array
371 // where each line of the msg is an array element.
372
373 if(!isset($this->FP))
374 {
375 $this->ERROR = _("POP3 get:") . ' ' . _("No connection to server");
376 return false;
377 }
378
379 $this->update_timer();
380
381 $fp = $this->FP;
382 $buffer = $this->BUFFER;
383 $cmd = "RETR $msgNum";
384 $reply = $this->send_cmd($cmd);
385
386 if(!$this->is_ok($reply))
387 {
388 $this->ERROR = _("POP3 get:") . ' ' . _("Error ") . "[$reply]";
389 return false;
390 }
391
392 $count = 0;
393 $MsgArray = array();
394
395 $line = fgets($fp,$buffer);
396 while ( !ereg("^\.\r\n",$line))
397 {
398 $MsgArray[$count] = $line;
399 $count++;
400 $line = fgets($fp,$buffer);
401 if(empty($line)) { break; }
402 }
403 return $MsgArray;
404 }
405
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)
410
411 $last = -1;
412 if(!isset($this->FP))
413 {
414 $this->ERROR = _("POP3 last:") . ' ' . _("No connection to server");
415 return $last;
416 }
417
418 $reply = $this->send_cmd("STAT");
419 if(!$this->is_ok($reply))
420 {
421 $this->ERROR = _("POP3 last:") . ' ' . _("Error ") . "[$reply]";
422 return $last;
423 }
424
425 $Vars = explode(" ",$reply);
426 $count = $Vars[1];
427 $size = $Vars[2];
428 settype($count,"integer");
429 settype($size,"integer");
430 if($type != "count")
431 {
432 return array($count,$size);
433 }
434 return $count;
435 }
436
437 function reset () {
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.
441
442 if(!isset($this->FP))
443 {
444 $this->ERROR = _("POP3 reset:") . ' ' . _("No connection to server");
445 return false;
446 }
447 $reply = $this->send_cmd("RSET");
448 if(!$this->is_ok($reply))
449 {
450 // The POP3 RSET command -never- gives a -ERR
451 // response - if it ever does, something truely
452 // wild is going on.
453
454 $this->ERROR = _("POP3 reset:") . ' ' . _("Error ") . "[$reply]";
455 @error_log("POP3 reset: ERROR [$reply]",0);
456 }
457 $this->quit();
458 return true;
459 }
460
461 function send_cmd ( $cmd = "" )
462 {
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.
468
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.
472
473 // This method works best if $cmd responds with only
474 // one line of data.
475
476 if(!isset($this->FP))
477 {
478 $this->ERROR = _("POP3 send_cmd:") . ' ' . _("No connection to server");
479 return false;
480 }
481
482 if(empty($cmd))
483 {
484 $this->ERROR = _("POP3 send_cmd:") . ' ' . _("Empty command string");
485 return "";
486 }
487
488 $fp = $this->FP;
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); }
495 return $reply;
496 }
497
498 function quit() {
499 // Closes the connection to the POP3 server, deleting
500 // any msgs marked as deleted.
501
502 if(!isset($this->FP))
503 {
504 $this->ERROR = _("POP3 quit:") . ' ' . _("connection does not exist");
505 return false;
506 }
507 $fp = $this->FP;
508 $cmd = "QUIT";
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); }
513 fclose($fp);
514 unset($this->FP);
515 return true;
516 }
517
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.
521
522 $PopArray = $this->last("array");
523
524 if($PopArray == -1) { return false; }
525
526 if( (!$PopArray) or (empty($PopArray)) )
527 {
528 return false;
529 }
530 return $PopArray;
531 }
532
533 function uidl ($msgNum = "")
534 {
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
539
540 if(!isset($this->FP)) {
541 $this->ERROR = _("POP3 uidl:") . ' ' . _("No connection to server");
542 return false;
543 }
544
545 $fp = $this->FP;
546 $buffer = $this->BUFFER;
547
548 if(!empty($msgNum)) {
549 $cmd = "UIDL $msgNum";
550 $reply = $this->send_cmd($cmd);
551 if(!$this->is_ok($reply))
552 {
553 $this->ERROR = _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
554 return false;
555 }
556 list ($ok,$num,$myUidl) = explode(" ",$reply);
557 return $myUidl;
558 } else {
559 $this->update_timer();
560
561 $UIDLArray = array();
562 $Total = $this->COUNT;
563 $UIDLArray[0] = $Total;
564
565 if ($Total < 1)
566 {
567 return $UIDLArray;
568 }
569 $cmd = "UIDL";
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))
575 {
576 $this->ERROR = _("POP3 uidl:") . ' ' . _("Error ") . "[$reply]";
577 return false;
578 }
579
580 $line = "";
581 $count = 1;
582 $line = fgets($fp,$buffer);
583 while ( !ereg("^\.\r\n",$line)) {
584 if(ereg("^\.\r\n",$line)) {
585 break;
586 }
587 list ($msg,$msgUidl) = explode(" ",$line);
588 $msgUidl = $this->strip_clf($msgUidl);
589 if($count == $msg) {
590 $UIDLArray[$msg] = $msgUidl;
591 }
592 else
593 {
594 $UIDLArray[$count] = 'deleted';
595 }
596 $count++;
597 $line = fgets($fp,$buffer);
598 }
599 }
600 return $UIDLArray;
601 }
602
603 function delete ($msgNum = "") {
604 // Flags a specified msg as deleted. The msg will not
605 // be deleted until a quit() method is called.
606
607 if(!isset($this->FP))
608 {
609 $this->ERROR = _("POP3 delete:") . ' ' . _("No connection to server");
610 return false;
611 }
612 if(empty($msgNum))
613 {
614 $this->ERROR = _("POP3 delete:") . ' ' . _("No msg number submitted");
615 return false;
616 }
617 $reply = $this->send_cmd("DELE $msgNum");
618 if(!$this->is_ok($reply))
619 {
620 $this->ERROR = _("POP3 delete:") . ' ' . _("Command failed ") . "[$reply]";
621 return false;
622 }
623 return true;
624 }
625
626 // *********************************************************
627
628 // The following methods are internal to the class.
629
630 function is_ok ($cmd = "") {
631 // Return true or false on +OK or -ERR
632
633 if( empty($cmd) )
634 return false;
635 else
636 return( ereg ("^\+OK", $cmd ) );
637 }
638
639 function strip_clf ($text = "") {
640 // Strips \r\n from server responses
641
642 if(empty($text))
643 return $text;
644 else {
645 $stripped = str_replace("\r",'',$text);
646 $stripped = str_replace("\n",'',$stripped);
647 return $stripped;
648 }
649 }
650
651 function parse_banner ( $server_text ) {
652 $outside = true;
653 $banner = "";
654 $length = strlen($server_text);
655 for($count =0; $count < $length; $count++)
656 {
657 $digit = substr($server_text,$count,1);
658 if(!empty($digit)) {
659 if( (!$outside) && ($digit != '<') && ($digit != '>') )
660 {
661 $banner .= $digit;
662 }
663 if ($digit == '<')
664 {
665 $outside = false;
666 }
667 if($digit == '>')
668 {
669 $outside = true;
670 }
671 }
672 }
673 $banner = $this->strip_clf($banner); // Just in case
674 return "<$banner>";
675 }
676
677 } // End class
678
679 ?>