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