home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 May / INTERNET103.ISO / pc / software / windows / building / php_nuke / html / modules / webmail / pop3.php < prev    next >
Encoding:
PHP Script  |  2002-09-16  |  11.3 KB  |  261 lines

  1. <?php
  2.  
  3. /*************************************************************************/
  4.  #  Mailbox 0.9.2a   by Sivaprasad R.L (http://netlogger.net)             #
  5.  #  eMailBox 0.9.3   by Don Grabowski  (http://ecomjunk.com)              #
  6.  #          --  A pop3 client addon for phpnuked websites --              #
  7.  #                                                                        #
  8.  # This program is distributed in the hope that it will be useful,        #
  9.  # but WITHOUT ANY WARRANTY; without even the implied warranty of         #
  10.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
  11.  # GNU General Public License for more details.                           #
  12.  #                                                                        #
  13.  # You should have received a copy of the GNU General Public License      #
  14.  # along with this program; if not, write to the Free Software            #
  15.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              #
  16.  #                                                                        #
  17.  #             Copyright (C) by Sivaprasad R.L                            #
  18.  #            Script completed by Ecomjunk.com 2001                       #
  19. /*************************************************************************/
  20.  
  21. class POP3{
  22.         var $hostname;
  23.         var $user;
  24.         var $password;
  25.         var $apop="";
  26.         var $port=110;
  27.         var $DEBUG=0;
  28.         var $exit = true;
  29.         var $has_error = false;
  30.  
  31.         /* Private variables - DO NOT ACCESS */
  32.  
  33.         var $connection=0;
  34.         var $greeting = "";
  35.         var $state="DISCONNECTED";
  36.         var $must_update=0;
  37.  
  38.         function POP3($hostname,$user,$password,$apop="") {
  39.                 $this->hostname = $hostname;
  40.                 $this->user = $user;
  41.                 $this->password = $password;
  42.                 $this->apop = $apop;
  43.         }
  44.  
  45.          function AddError($error) {
  46.                  $this->has_error = true;
  47.                  echo "<center>\n";
  48.              echo "<b>Error:</b> $error\n";
  49.                  echo "</center>\n";
  50.                  $this->CloseConnection();
  51.                  if ($this->exit) exit;
  52.         }
  53.     
  54.         function POP3Command($command, $result="") {
  55.                 if ($this->DEBUG) echo "<b>Sending Command: </b>".$command."<br>";flush();
  56.                 @fputs($this->connection, "$command\r\n");
  57.                 $result = @fgets($this->connection, 100);
  58.  
  59.                 if (eregi("^(\+OK)", $result)) :
  60.                         if ($this->DEBUG) echo "<b>Result OK: </b><br>";flush();
  61.                         return true;
  62.                 else :
  63.                         $this->AddError($result);
  64.                 endif;
  65.         }
  66.          function OpenConnection() {
  67.                  if ($this->DEBUG) echo "<b>Openning Connection to: </b>".$this->hostname."<br>";flush();
  68.                  if($this->hostname=="")
  69.                            $this->AddError("You must specified a valid hostname");
  70.                    $this->connection = fsockopen($this->hostname,$this->port, &$errno, &$errstr);
  71.                 if ($this->DEBUG) echo "<b>Connection opened </b><br>";flush();
  72.                    if (!($this->connection)) :
  73.                            if ($errno == 0)
  74.                                     $this->AddError("Invalid Mail Server Name or Server Connection Error");
  75.                            $this->AddError($errno." ".$errstr);
  76.                    endif;
  77.                 return true;
  78.          }
  79.  
  80.          function CloseConnection() {
  81.                   if($this->connection!=0) :
  82.                            fclose($this->connection);
  83.                            $this->connection=0;
  84.                 endif;
  85.          }
  86.  
  87.          function Open() {
  88.                   if($this->state!="DISCONNECTED")
  89.                            $this->AddError("1 a connection is already opened");
  90.                   $this->OpenConnection();
  91.                         $this->greeting = @fgets($this->connection, 100);
  92.                           if(GetType($this->greeting)!="string" OR strtok($this->greeting," ")!="+OK") :
  93.                                    $this->CloseConnection();
  94.                                    $this->AddError("2 POP3 server greeting was not found");
  95.                           endif;
  96.                   $this->greeting=strtok("\r\n");
  97.                   $this->must_update=0;
  98.                   $this->state="AUTHORIZATION";
  99.                   $this->Login();
  100.                   return true;
  101.          }
  102.  
  103.  /* Close method - this method must be called at least if there are any
  104.      messages to be deleted */
  105.  
  106.          function Close() {
  107.                   if($this->state=="DISCONNECTED")
  108.                            $this->AddError("no connection was opened");
  109.                   if($this->must_update)
  110.                            $this->POP3Command("QUIT");
  111.                   $this->CloseConnection();
  112.                   $this->state="DISCONNECTED";
  113.                   return true;
  114.          }
  115.  
  116.  /* Login method - pass the user name and password of POP account.  Set
  117.      $apop to 1 or 0 wether you want to login using APOP method or not.  */
  118.  
  119.         function Login() {
  120.                   if($this->state!="AUTHORIZATION")
  121.                            $this->AddError("connection is not in AUTHORIZATION state");
  122.                   if($this->apop) :
  123.                            $this->POP3Command("APOP $this->user ".md5($this->greeting.$this->password));
  124.                   else :
  125.                           $this->POP3Command("USER $this->user");
  126.                           $this->POP3Command("PASS $this->password");
  127.                   endif;
  128.                   $this->state="TRANSACTION";
  129.          }
  130.  
  131.         /* Statistics method - pass references to variables to hold the number of
  132.      messages in the mail box and the size that they take in bytes.  */
  133.  
  134.         function Stats($msg=""){
  135.                   if($this->state!="TRANSACTION")
  136.                            $this->AddError("connection is not in TRANSACTION state");
  137.                   if ($msg == "") :
  138.                           $this->POP3Command("STAT", &$result);
  139.                   else :
  140.                           $this->POP3Command("LIST $msg", &$result);
  141.                   endif;
  142.                   $p = explode(" ", $result);
  143.                   $stat["message"] = $p[1];
  144.                   $stat["size"] = $p[2];
  145.                   return $stat;
  146.          }
  147.  
  148.         function GetHeaders($message=1) {
  149.                 $this->POP3Command("TOP $message 0");
  150.                 for ($headers="";;) {
  151.                         $line = fgets($this->connection, 100);
  152.                           if (trim($line) == "." OR feof($this->connection)) {
  153.                                   break;
  154.                         }
  155.                           $headers .= $line;
  156.                   }
  157.                 return $headers;
  158.         }
  159.  
  160.         function GetMessageID($message="") {
  161.                 if ($message) :
  162.                         $this->POP3Command("UIDL $message", &$result);
  163.                         $id = explode (" ", $result);
  164.                         return ereg_replace("[<>]","",$id[2]);
  165.                 else :
  166.                         $this->POP3Command("UIDL") ;
  167.                         while (!feof($this->connection)) :
  168.                                 $line = fgets($this->connection, 100);
  169.                                 if (trim($line) == ".") {
  170.                                           break;
  171.                                 }
  172.                                 $part = explode (" ", $line);
  173.                                 $part[1] = ereg_replace("[<>]","",$part[1]);
  174.                                 $id[$part[0]] = $part[1];
  175.                         endwhile;
  176.                         return $id;
  177.                 endif;
  178.  
  179.         }
  180.  
  181.         function GetMessage($msg=1) {
  182.                 $i = 0;
  183.                 $this->POP3Command("RETR $msg");
  184.                 for ($m="";;) {
  185.                         $line = fgets($this->connection, 100);
  186.                         if (trim($line) == "." OR feof($this->connection)) {
  187.                                   break;
  188.                         }
  189.                         if (chop($line) == ""  AND $i < 1) :
  190.                                 $message["header"] = $m;
  191.                                 $i++;
  192.                         endif;
  193.                         if ($i > 0)
  194.                                 $messagebody .= $line;
  195.                         $m .= $line;
  196.                 }
  197.                 $message["body"] = $messagebody;
  198.                 $message["full"] = $m;
  199.                 return $message;
  200.         }
  201.  
  202.         function ListMessage($msg) {
  203.                 $list = array();
  204.                 $list["has_attachment"] = false;
  205.                 $list["size"] = '';
  206.                 $this->POP3Command("RETR $msg");
  207.                 for ($m="";;) {
  208.                         $line = fgets($this->connection, 100);
  209.                         $list["size"] += strlen($line);
  210.                         if (trim($line) == "." OR feof($this->connection)) {
  211.                                   break;
  212.                         }
  213.                         if (eregi("^Subject: (.*)", $line, $reg))
  214.                                 $list["subject"] = $reg[1];
  215.                         if (eregi("^Date: (.*)", $line, $reg))
  216.                                 $date = $reg[1];
  217.                         if (eregi("^From: (.*)", $line, $reg))
  218.                                 $from = $reg[1];
  219.                         if (eregi("^Content-Disposition: attachment", $line) OR eregi("^Content-Disposition: inline", $line))
  220.                                 $list["has_attachment"] = true;;
  221.                 }
  222.                 eregi("(.+) (.+) (.+) ([0-9]{1,2})([0-9]{1,2}) (.+):(.+):(.+) .+", $date, $dreg);
  223.                 $list["date"] = $dreg[1]." ".$dreg[2]." ".$dreg[3];
  224.                 $from = eregi_replace("<|>|\[|\]|\(|\)|\"|\'|(mailto:)", "", $from);
  225.                  if (eregi("(.*)? (.+@.+\\..+)", $from)) :
  226.                            eregi("(.*)? (.+@.+\\..+)", $from, $reg);
  227.                            $list["sender"]["name"] = $reg[1];
  228.                            $list["sender"]["email"] = $reg[2];
  229.                  else :
  230.                          eregi("(.+@.+\\..+)", $from, $reg);
  231.                          $list["sender"]["name"] = $reg[1];
  232.                          $list["sender"]["email"] = $reg[1];
  233.                 endif;
  234.                 return $list;
  235.         }
  236.  
  237.          function DeleteMessage($message) {
  238.                   if($this->state!="TRANSACTION")
  239.                            $this->AddError("connection is not in TRANSACTION state");
  240.                   $this->POP3Command("DELE $message");
  241.                 $this->must_update=1;
  242.                   return true;
  243.          }
  244.  
  245.          function ResetDeletedMessages() {
  246.                   if($this->state!="TRANSACTION")
  247.                            $this->AddError("connection is not in TRANSACTION state");
  248.                   $this->POP3Command("RSET");
  249.                   $this->must_update=0;
  250.                   return("");
  251.          }
  252.  
  253.          function NOOP() {
  254.                   if($this->state!="TRANSACTION")
  255.                            $this->AddError("connection is not in TRANSACTION state");
  256.                   $this->POP3Command("NOOP");
  257.                   return("");
  258.           }
  259. };
  260.  
  261. ?>