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