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