/[inncomm]/inc/class.POP3.php3
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /inc/class.POP3.php3

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Sun Mar 28 11:01:44 1999 UTC (25 years, 1 month ago) by dpavlin
Branch: DbP, MAIN
CVS Tags: r0, HEAD
Changes since 1.1: +0 -0 lines
first, semi-working version

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

  ViewVC Help
Powered by ViewVC 1.1.26