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