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