/[docman]/auth_pop3.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /auth_pop3.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by dpavlin, Fri Jan 26 09:32:48 2001 UTC revision 1.2 by dpavlin, Fri Jan 26 12:39:57 2001 UTC
# Line 12  Line 12 
12          That should actually be also a vaild e-mail address          That should actually be also a vaild e-mail address
13    
14    
15          this module is uses class.POP3.php3 which is included          this module is based on class.POP3.php3 by cdi@thewebmasters.net
         after this function  
16  */  */
17    
18  function auth_pop3($user) {  function auth_pop3($user) {
19          $email = explode("@",$user[3]);          $email = explode("@",$user[3]);
20          $pop3 = new POP3();          $pop3 = new POP3();
21          $pop3->connect($email[1]);          $pop3->connect($email[1]);
22          $Count = $pop3->login($email[0],$GLOBALS[PHP_AUTH_PW]);          if ($pop3->checklogin($email[0],$GLOBALS[PHP_AUTH_PW])) {
23                  if ( $Count && $Count != -1 ) {                  $pop3->quit();
24                          $pop3->quit();                  return true;
25                          return 1;          }
                 }  
26          $pop3->quit();          $pop3->quit();
27          return 0;          return false;
28  }  }
29    
30  //--------------------------------------------------------------------------  //--------------------------------------------------------------------------
31    
32  /*  /*
33            This is just a part of class.POP3.php3 which is needed for
34            auth_pop3.php module. Please look at original location for
35            whole class!
36    
37          class.POP3.php3 v1.0    99/03/24 CDI cdi@thewebmasters.net          class.POP3.php3 v1.0    99/03/24 CDI cdi@thewebmasters.net
38          Copyright (c) 1999 - CDI (cdi@thewebmasters.net) All Rights Reserved          Copyright (c) 1999 - CDI (cdi@thewebmasters.net) All Rights Reserved
39    
# Line 40  function auth_pop3($user) { Line 42  function auth_pop3($user) {
42    
43  class POP3  class POP3
44  {  {
45          var $ERROR              = "";           //      Error string.          var $ERROR      = "";   // Error string.
46    
47          var $TIMEOUT    = 60;           //      Default timeout before giving up on a          var $TIMEOUT    = 60;   // Default timeout before giving up on a
48                                                                  //      network operation.                                  // network operation.
49    
50          var $COUNT              = -1;           //      Mailbox msg count          var $COUNT      = -1;   // Mailbox msg count
51    
52          var $BUFFER             = 512;          //      Socket buffer for socket fgets() calls.          var $BUFFER     = 512;  // Socket buffer for socket fgets() calls.
53                                                                  //      Per RFC 1939 the returned line a POP3                                  // Per RFC 1939 the returned line a POP3
54                                                                  //      server can send is 512 bytes.                                  // server can send is 512 bytes.
55    
56          var $FP                 = "";           //      The connection to the server's          var $FP         = "";   // The connection to the server's
57                                                                  //      file descriptor                                  // file descriptor
58    
59          var $MAILSERVER = "";           // Set this to hard code the server name          var $MAILSERVER = "";   // Set this to hard code the server name
60    
61          var $DEBUG              = false;        // set to true to echo pop3          var $DEBUG      = false;// set to true to echo pop3
62                                                                  // commands and responses to error_log                                  // commands and responses to error_log
63                                                                  // this WILL log passwords!                                  // this WILL log passwords!
64    
65          var $BANNER             = "";           //      Holds the banner returned by the          var $BANNER     = "";   // Holds the banner returned by the
66                                                                  //      pop server - used for apop()                                  // pop server - used for apop()
67    
68          var $RFC1939    = true;         //      Set by noop(). See rfc1939.txt            var $RFC1939    = true; // Set by noop(). See rfc1939.txt      
69                                                                  //                                  //
70    
71          var $ALLOWAPOP  = false;        //      Allow or disallow apop()          var $ALLOWAPOP  = false;// Allow or disallow apop()
72                                                                  //      This must be set to true                                  // This must be set to true
73                                                                  //      manually.                                  // manually.
74    
75          function POP3 ( $server = "", $timeout = "" )          function POP3 ( $server = "", $timeout = "" )
76          {          {
# Line 100  class POP3 Line 102  class POP3
102    
103          function connect ($server, $port = 110)          function connect ($server, $port = 110)
104          {          {
105                  //      Opens a socket to the specified server. Unless overridden,                  // Opens a socket to the specified server. Unless overridden,
106                  //      port defaults to 110. Returns true on success, false on fail                  // port defaults to 110. Returns true on success, false on fail
107    
108                  // If MAILSERVER is set, override $server with it's value                  // If MAILSERVER is set, override $server with it's value
109    
# Line 149  class POP3 Line 151  class POP3
151                  return true;                  return true;
152          }          }
153    
154          function noop ()  //-----------------------------
         {  
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 noop: No connection to server";  
                         return false;  
                 }  
                 $cmd = "NOOP";  
                 $reply = $this->send_cmd($cmd);  
                 if(!$this->is_ok($reply))  
                 {  
                         return false;  
                 }  
                 return true;  
         }  
   
         function user ($user = "")  
         {  
                 // Sends the USER command, returns true or false  
155    
156                  if(empty($user))          function checklogin ($user, $pass) {
                 {  
                         $this->ERROR = "POP3 user: no user id submitted";  
                         return false;  
                 }  
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 user: connection not established";  
                         return false;  
                 }  
157                  $reply = $this->send_cmd("USER $user");                  $reply = $this->send_cmd("USER $user");
158                  if(!$this->is_ok($reply))                  if(!$this->is_ok($reply))
159                  {                  {
160                          $this->ERROR = "POP3 user: Error [$reply]";                          $this->ERROR = "POP3 user: Error [$reply]";
161                          return false;                          return false;
162                  }                  }
                 return true;  
         }  
163    
         function pass ($pass = "")  
         {  
                 // Sends the PASS command, returns # of msgs in mailbox,  
                 // returns false (undef) on Auth failure  
   
                 if(empty($pass))  
                 {  
                         $this->ERROR = "POP3 pass: no password submitted";  
                         return false;  
                 }  
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 pass: connection not established";  
                         return false;  
                 }  
164                  $reply = $this->send_cmd("PASS $pass");                  $reply = $this->send_cmd("PASS $pass");
165                  if(!$this->is_ok($reply))                  if(!$this->is_ok($reply))
166                  {                  {
# Line 211  class POP3 Line 169  class POP3
169                          return false;                          return false;
170                  }                  }
171                  //      Auth successful.                  //      Auth successful.
172                  $count = $this->last("count");                  return true;
                 $this->COUNT = $count;  
                 $this->RFC1939 = $this->noop();  
                 if(!$this->RFC1939)  
                 {  
                         $this->ERROR = "POP3 pass: NOOP failed. Server not RFC 1939 compliant";  
                         $this->quit();  
                         return false;  
                 }  
                 return $count;  
         }  
   
         function apop ($login,$pass)  
         {  
                 //      Attempts an APOP login. If this fails, it'll  
                 //      try a standard login. YOUR SERVER MUST SUPPORT  
                 //      THE USE OF THE APOP COMMAND!  
                 //      (apop is optional per rfc1939)  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 apop: No connection to server";  
                         return false;  
                 }  
   
                 if(!$this->ALLOWAPOP)  
                 {  
                         $retVal = $this->login($login,$pass);  
                         return $retVal;  
                 }  
   
                 if(empty($login))  
                 {  
                         $this->ERROR = "POP3 apop: No login ID submitted";  
                         return false;  
                 }  
                 if(empty($pass))  
                 {  
                         $this->ERROR = "POP3 apop: No password submitted";  
                         return false;  
                 }  
                 $banner = $this->BANNER;  
                 if( (!$banner) or (empty($banner)) )  
                 {  
                         $this->ERROR = "POP3 apop: No server banner - aborted";  
                         $retVal = $this->login($login,$pass);  
                         return $retVal;  
                 }  
                 $AuthString = $banner;  
                 $AuthString .= $pass;  
                 $APOPString = md5($AuthString);  
                 $cmd = "APOP $login $APOPString";  
                 $reply = $this->send_cmd($cmd);  
                 if(!$this->is_ok($reply))  
                 {  
                         $this->ERROR = "POP3 apop: apop authentication failed - abort";  
                         $retVal = $this->login($login,$pass);  
                         return $retVal;  
                 }  
                 //      Auth successful.  
                 $count = $this->last("count");  
                 $this->COUNT = $count;  
                 $this->RFC1939 = $this->noop();  
                 if(!$this->RFC1939)  
                 {  
                         $this->ERROR = "POP3 apop: NOOP failed. Server not RFC 1939 compliant";  
                         $this->quit();  
                         return false;  
                 }  
                 return $count;  
         }  
   
         function login ($login = "", $pass = "")  
         {  
                 // Sends both user and pass. Returns # of msgs in mailbox or  
                 // false on failure (or -1, if the error occurs while getting  
                 // the number of messages.)  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 login: No connection to server";  
                         return false;  
                 }  
                 $fp = $this->FP;  
                 if(!$this->user($login))  
                 {  
                         //      Preserve the error generated by user()  
                         return false;  
                 }  
                 $count = $this->pass($pass);  
                 if( (!$count) or ($count == -1) )  
                 {  
                         //      Preserve the error generated by last() and pass()  
                         return false;  
                 }  
                 return $count;  
         }  
   
         function top ($msgNum, $numLines = "0")  
         {  
                 //      Gets the header and first $numLines of the msg body  
                 //      returns data in an array with each returned line being  
                 //      an array element. If $numLines is empty, returns  
                 //      only the header information, and none of the body.  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 top: No connection to server";  
                         return false;  
                 }  
                 $this->update_timer();  
   
                 $fp = $this->FP;  
                 $buffer = $this->BUFFER;  
                 $cmd = "TOP $msgNum $numLines";  
                 fwrite($fp, "TOP $msgNum $numLines\r\n");  
                 $reply = fgets($fp, $buffer);  
                 $reply = $this->strip_clf($reply);  
                 if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }  
                 if(!$this->is_ok($reply))  
                 {  
                         $this->ERROR = "POP3 top: Error [$reply]";  
                         return false;  
                 }  
   
                 $count = 0;  
                 $MsgArray = array();  
   
                 $line = fgets($fp,$buffer);  
                 while ( !ereg("^\.\r\n",$line))  
                 {  
                         $MsgArray[$count] = $line;  
                         $count++;  
                         $line = fgets($fp,$buffer);  
                         if(empty($line))        { break; }  
                 }  
   
                 return $MsgArray;  
173          }          }
174    
175          function pop_list ($msgNum = "")  //-------------------------------------------
         {  
                 //      If called with an argument, returns that msgs' size in octets  
                 //      No argument returns an associative array of undeleted  
                 //      msg numbers and their sizes in octets  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 pop_list: No connection to server";  
                         return false;  
                 }  
                 $fp = $this->FP;  
                 $Total = $this->COUNT;  
                 if( (!$Total) or ($Total == -1) )  
                 {  
                         return false;  
                 }  
                 if($Total == 0)  
                 {  
                         return array("0","0");  
                         // return -1;   // mailbox empty  
                 }  
   
                 $this->update_timer();  
   
                 if(!empty($msgNum))  
                 {  
                         $cmd = "LIST $msgNum";  
                         fwrite($fp,"$cmd\r\n");  
                         $reply = fgets($fp,$this->BUFFER);  
                         $reply = $this->strip_clf($reply);  
                         if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }  
                         if(!$this->is_ok($reply))  
                         {  
                                 $this->ERROR = "POP3 pop_list: Error [$reply]";  
                                 return false;  
                         }  
                         list($junk,$num,$size) = explode(" ",$reply);  
                         return $size;  
                 }  
                 $cmd = "LIST";  
                 $reply = $this->send_cmd($cmd);  
                 if(!$this->is_ok($reply))  
                 {  
                         $reply = $this->strip_clf($reply);  
                         $this->ERROR = "POP3 pop_list: Error [$reply]";  
                         return false;  
                 }  
                 $MsgArray = array();  
                 $MsgArray[0] = $Total;  
                 for($msgC=1;$msgC <= $Total; $msgC++)  
                 {  
                         if($msgC > $Total) { break; }  
                         $line = fgets($fp,$this->BUFFER);  
                         $line = $this->strip_clf($line);  
                         if(ereg("^\.",$line))  
                         {  
                                 $this->ERROR = "POP3 pop_list: Premature end of list";  
                                 return false;  
                         }  
                         list($thisMsg,$msgSize) = explode(" ",$line);  
                         settype($thisMsg,"integer");  
                         if($thisMsg != $msgC)  
                         {  
                                 $MsgArray[$msgC] = "deleted";  
                         }  
                         else  
                         {  
                                 $MsgArray[$msgC] = $msgSize;  
                         }  
                 }  
                 return $MsgArray;  
         }  
176    
177          function get ($msgNum)          function noop ()
178          {          {
                 //      Retrieve the specified msg number. Returns an array  
                 //      where each line of the msg is an array element.  
   
179                  if(!isset($this->FP))                  if(!isset($this->FP))
180                  {                  {
181                          $this->ERROR = "POP3 get: No connection to server";                          $this->ERROR = "POP3 noop: No connection to server";
182                          return false;                          return false;
183                  }                  }
184                    $cmd = "NOOP";
                 $this->update_timer();  
   
                 $fp = $this->FP;  
                 $buffer = $this->BUFFER;  
                 $cmd = "RETR $msgNum";  
185                  $reply = $this->send_cmd($cmd);                  $reply = $this->send_cmd($cmd);
   
186                  if(!$this->is_ok($reply))                  if(!$this->is_ok($reply))
187                  {                  {
                         $this->ERROR = "POP3 get: Error [$reply]";  
                         return false;  
                 }  
   
                 $count = 0;  
                 $MsgArray = array();  
   
                 $line = fgets($fp,$buffer);  
                 while ( !ereg("^\.\r\n",$line))  
                 {  
                         $MsgArray[$count] = $line;  
                         $count++;  
                         $line = fgets($fp,$buffer);  
                         if(empty($line))        { break; }  
                 }  
                 return $MsgArray;  
         }  
   
         function last ( $type = "count" )  
         {  
                 //      Returns the highest msg number in the mailbox.  
                 //      returns -1 on error, 0+ on success, if type != count  
                 //      results in a popstat() call (2 element array returned)  
   
                 $last = -1;  
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 last: No connection to server";  
                         return $last;  
                 }  
   
                 $reply = $this->send_cmd("STAT");  
                 if(!$this->is_ok($reply))  
                 {  
                         $this->ERROR = "POP3 last: error [$reply]";  
                         return $last;  
                 }  
   
                 $Vars = explode(" ",$reply);  
                 $count = $Vars[1];  
                 $size = $Vars[2];  
                 settype($count,"integer");  
                 settype($size,"integer");  
                 if($type != "count")  
                 {  
                         return array($count,$size);  
                 }  
                 return $count;  
         }  
   
         function reset ()  
         {  
                 //      Resets the status of the remote server. This includes  
                 //      resetting the status of ALL msgs to not be deleted.  
                 //      This method automatically closes the connection to the server.  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 reset: No connection to server";  
188                          return false;                          return false;
189                  }                  }
                 $reply = $this->send_cmd("RSET");  
                 if(!$this->is_ok($reply))  
                 {  
                         //      The POP3 RSET command -never- gives a -ERR  
                         //      response - if it ever does, something truely  
                         //      wild is going on.  
   
                         $this->ERROR = "POP3 reset: Error [$reply]";  
                         @error_log("POP3 reset: ERROR [$reply]",0);  
                 }  
                 $this->quit();  
190                  return true;                  return true;
191          }          }
192    
# Line 578  class POP3 Line 248  class POP3
248                  return true;                  return true;
249          }          }
250    
         function popstat ()  
         {  
                 //      Returns an array of 2 elements. The number of undeleted  
                 //      msgs in the mailbox, and the size of the mbox in octets.  
   
                 $PopArray = $this->last("array");  
   
                 if($PopArray == -1) { return false; }  
   
                 if( (!$PopArray) or (empty($PopArray)) )  
                 {  
                         return false;  
                 }  
                 return $PopArray;  
         }  
   
         function uidl ($msgNum = "")  
         {  
                 //      Returns the UIDL of the msg specified. If called with  
                 //      no arguments, returns an associative array where each  
                 //      undeleted msg num is a key, and the msg's uidl is the element  
                 //      Array element 0 will contain the total number of msgs  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 uidl: No connection to server";  
                         return false;  
                 }  
   
                 $fp = $this->FP;  
                 $buffer = $this->BUFFER;  
   
                 if(!empty($msgNum))  
                 {  
                         $cmd = "UIDL $msgNum";  
                         $reply = $this->send_cmd($cmd);  
                         if(!$this->is_ok($reply))  
                         {  
                                 $this->ERROR = "POP3 uidl: Error [$reply]";  
                                 return false;  
                         }  
                         list ($ok,$num,$myUidl) = explode(" ",$reply);  
                         return $myUidl;  
                 }  
                 else  
                 {  
                         $this->update_timer();  
   
                         $UIDLArray = array();  
                         $Total = $this->COUNT;  
                         $UIDLArray[0] = $Total;  
   
                         if ($Total < 1)  
                         {  
                                 return $UIDLArray;  
                         }  
                         $cmd = "UIDL";  
                         fwrite($fp, "UIDL\r\n");  
                         $reply = fgets($fp, $buffer);  
                         $reply = $this->strip_clf($reply);  
                         if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }  
                         if(!$this->is_ok($reply))  
                         {  
                                 $this->ERROR = "POP3 uidl: Error [$reply]";  
                                 return false;  
                         }  
   
                         $line = "";  
                         $count = 1;  
                         $line = fgets($fp,$buffer);  
                         while ( !ereg("^\.\r\n",$line))  
                         {  
                                 if(ereg("^\.\r\n",$line))  
                                 {  
                                         break;  
                                 }  
                                 list ($msg,$msgUidl) = explode(" ",$line);  
                                 $msgUidl = $this->strip_clf($msgUidl);  
                                 if($count == $msg)  
                                 {  
                                         $UIDLArray[$msg] = $msgUidl;  
                                 }  
                                 else  
                                 {  
                                         $UIDLArray[$count] = "deleted";  
                                 }  
                                 $count++;  
                                 $line = fgets($fp,$buffer);  
                         }  
                 }  
                 return $UIDLArray;  
         }  
   
         function delete ($msgNum = "")  
         {  
                 //      Flags a specified msg as deleted. The msg will not  
                 //      be deleted until a quit() method is called.  
   
                 if(!isset($this->FP))  
                 {  
                         $this->ERROR = "POP3 delete: No connection to server";  
                         return false;  
                 }  
                 if(empty($msgNum))  
                 {  
                         $this->ERROR = "POP3 delete: No msg number submitted";  
                         return false;  
                 }  
                 $reply = $this->send_cmd("DELE $msgNum");  
                 if(!$this->is_ok($reply))  
                 {  
                         $this->ERROR = "POP3 delete: Command failed [$reply]";  
                         return false;  
                 }  
                 return true;  
         }  
   
251          //      *********************************************************          //      *********************************************************
252    
253          //      The following methods are internal to the class.          //      The following methods are internal to the class.
# Line 703  class POP3 Line 256  class POP3
256          {          {
257                  //      Return true or false on +OK or -ERR                  //      Return true or false on +OK or -ERR
258    
259                  if(empty($cmd))                                 { return false; }                  if(empty($cmd)) { return false; }
260                  if ( ereg ("^\+OK", $cmd ) )    { return true; }                  if ( ereg ("^\+OK", $cmd ) )    { return true; }
261                  return false;                  return false;
262          }          }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.26