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