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