home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / pop3serveracc.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  13.2 KB  |  440 lines

  1. POP3 server access 
  2.  
  3. Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box. 
  4.  
  5.  
  6.  
  7. <?
  8.  
  9. /*
  10.  * Copyright (C) 1998, Manuel Lemos (mlemos@acm.org)
  11.  * 
  12.  * Permission to use and modify this software and its
  13.  * documentation for any purpose other than its incorporation
  14.  * into a commercial product is hereby granted without fee,
  15.  * as long as the author is notified that this piece of software
  16.  * is being used in other applications.
  17.  * Permission to copy and distribute this software and its
  18.  * documentation only for non-commercial use is also granted
  19.  * without fee, provided, however, that the above copyright
  20.  * notice appear in all copies, that both that copyright notice
  21.  * and this permission notice appear in supporting documentation.
  22.  * The author makes no representations about the suitability
  23.  * of this software for any purpose.  It is provided ``as is'',
  24.  * without express or implied warranty.
  25.  */
  26.  
  27. /* put this class definition in a file named pop3.php */
  28.  
  29. class pop3_class
  30. {
  31.  var $hostname="";
  32.  var $port=110;
  33.  
  34.  /* Private variables - DO NOT ACCESS */
  35.  
  36.  var $connection=0;
  37.  var $state="DISCONNECTED";
  38.  var $greeting="";
  39.  var $must_update=0;
  40.  
  41.  
  42.  /* Private methods - DO NOT CALL */
  43.  
  44.  Function GetLine()
  45.  {
  46.   for($line="";;)
  47.   {
  48.    if(feof($this->connection))
  49.     return(0);
  50.    $line.=fgets($this->connection,100);
  51.    $length=strlen($line);
  52.    if($length>=2
  53.    && substr($line,$length-2,2)=="\r\n")
  54.     return(substr($line,0,$length-2));
  55.   }
  56.  }
  57.  
  58.  Function PutLine($line)
  59.  {
  60.   return(fputs($this->connection,"$line\r\n"));
  61.  }
  62.  
  63.  Function OpenConnection()
  64.  {
  65.   if($this->hostname=="")
  66.    return("2 it was not specified a valid hostname");
  67.   switch(($this->connection=fsockopen($this->hostname,$this->port)))
  68.   {
  69.    case -3:
  70.     return("-3 socket could not be created");
  71.    case -4:
  72.     return("-4 dns lookup on hostname \"$hostname\" failed");
  73.    case -5:
  74.     return("-5 connection refused or timed out");
  75.    case -6:
  76.     return("-6 fdopen() call failed");
  77.    case -7:
  78.     return("-7 setvbuf() call failed");
  79.    default:
  80.     return("");
  81.   }
  82.  }
  83.  
  84.  Function CloseConnection()
  85.  {
  86.   if($this->connection!=0)
  87.   {
  88.    fclose($this->connection);
  89.    $this->connection=0;
  90.   }
  91.  }
  92.  
  93.  /* Public methods */
  94.  
  95.  /* Open method - set the object variable $hostname to the POP3 server address. */
  96.  
  97.  Function Open()
  98.  {
  99.   if($this->state!="DISCONNECTED")
  100.    return("1 a connection is already opened");
  101.   if(($error=$this->OpenConnection())!="")
  102.    return($error);
  103.   $this->greeting=$this->GetLine();
  104.   if(GetType($this->greeting)!="string"
  105.   || strtok($this->greeting," ")!="+OK")
  106.   {
  107.    $this->CloseConnection();
  108.    return("3 POP3 server greeting was not found");
  109.   }
  110.   $this->greeting=strtok("\r\n");
  111.   $this->must_update=0;
  112.   $this->state="AUTHORIZATION";
  113.   return("");
  114.  }
  115.  
  116.  /* Close method - this method must be called at least if there are any
  117.      messages to be deleted */
  118.  
  119.  Function Close()
  120.  {
  121.   if($this->state=="DISCONNECTED")
  122.    return("no connection was opened");
  123.   if($this->must_update)
  124.   {
  125.    if($this->PutLine("QUIT")==0)
  126.     return("Could not send the QUIT command");
  127.    $response=$this->GetLine();
  128.    if(GetType($response)!="string")
  129.     return("Could not get quit command response");
  130.    if(strtok($response," ")!="+OK")
  131.     return("Could not quit the connection: ".strtok("\r\n"));
  132.   }
  133.   $this->CloseConnection();
  134.   $this->state="DISCONNECTED";
  135.   return("");
  136.  }
  137.  
  138.  /* Login method - pass the user name and password of POP account.  Set
  139.      $apop to 1 or 0 wether you want to login using APOP method or not.  */
  140.  
  141.  Function Login($user,$password,$apop)
  142.  {
  143.   if($this->state!="AUTHORIZATION")
  144.    return("connection is not in AUTHORIZATION state");
  145.   if($apop)
  146.   {
  147.    if($this->PutLine("APOP $user ".md5($this->greeting.$password))==0)
  148.     return("Could not send the APOP command");
  149.    $response=$this->GetLine();
  150.    if(GetType($response)!="string")
  151.     return("Could not get APOP login command response");
  152.    if(strtok($response," ")!="+OK")
  153.     return("APOP login failed: ".strtok("\r\n"));
  154.   }
  155.   else
  156.   {
  157.    if($this->PutLine("USER $user")==0)
  158.     return("Could not send the USER command");
  159.    $response=$this->GetLine();
  160.    if(GetType($response)!="string")
  161.     return("Could not get user login entry response");
  162.    if(strtok($response," ")!="+OK")
  163.     return("User error: ".strtok("\r\n"));
  164.    if($this->PutLine("PASS $password")==0)
  165.     return("Could not send the PASS command");
  166.    $response=$this->GetLine();
  167.    if(GetType($response)!="string")
  168.     return("Could not get login password entry response");
  169.    if(strtok($response," ")!="+OK")
  170.     return("Password error: ".strtok("\r\n"));
  171.   }
  172.   $this->state="TRANSACTION";
  173.   return("");
  174.  }
  175.  
  176.  /* Statistics method - pass references to variables to hold the number of
  177.      messages in the mail box and the size that they take in bytes.  */
  178.  
  179.  Function Statistics($messages,$size)
  180.  {
  181.   if($this->state!="TRANSACTION")
  182.    return("connection is not in TRANSACTION state");
  183.   if($this->PutLine("STAT")==0)
  184.    return("Could not send the STAT command");
  185.   $response=$this->GetLine();
  186.   if(GetType($response)!="string")
  187.    return("Could not get the statistics command response");
  188.   if(strtok($response," ")!="+OK")
  189.    return("Could not get the statistics: ".strtok("\r\n"));
  190.   $messages=strtok(" ");
  191.   $size=strtok(" ");
  192.   return("");
  193.  }
  194.  
  195.  /* ListMessages method - the $message argument indicates the number of a
  196.      message to be listed.  If you specify an empty string it will list all
  197.      messages in the mail box.  The $unique_id flag indicates if you want
  198.      to list the each message unique identifier, otherwise it will
  199.      return the size of each message listed.  If you list all messages the
  200.      result will be returned in an array. */
  201.  
  202.  Function ListMessages($message,$unique_id)
  203.  {
  204.   if($this->state!="TRANSACTION")
  205.    return("connection is not in TRANSACTION state");
  206.   if($unique_id)
  207.    $list_command="UIDL";
  208.   else
  209.    $list_command="LIST";
  210.   if($this->PutLine("$list_command $message")==0)
  211.    return("Could not send the $list_command command");
  212.   $response=$this->GetLine();
  213.   if(GetType($response)!="string")
  214.    return("Could not get message list command response");
  215.   if(strtok($response," ")!="+OK")
  216.    return("Could not get the message listing: ".strtok("\r\n"));
  217.   if($message=="")
  218.   {
  219.    for($messages=array();;)
  220.    {
  221.     $response=$this->GetLine();
  222.     if(GetType($response)!="string")
  223.      return("Could not get message list response");
  224.     if($response==".")
  225.      break;
  226.     $message=intval(strtok($response," "));
  227.     if($unique_id)
  228.      $messages[$message]=strtok(" ");
  229.     else
  230.      $messages[$message]=intval(strtok(" "));
  231.    }
  232.    return($messages);
  233.   }
  234.   else
  235.   {
  236.    $message=intval(strtok(" "));
  237.    return(intval(strtok(" ")));
  238.   }
  239.  }
  240.  
  241.  /* RetrieveMessage method - the $message argument indicates the number of
  242.      a message to be listed.  Pass a reference variables that will hold the
  243.      arrays of the $header and $body lines.  The $lines argument tells how
  244.      many lines of the message are to be retrieved.  Pass a negative number
  245.      if you want to retrieve the whole message. */
  246.  
  247.  Function RetrieveMessage($message,$headers,$body,$lines)
  248.  {
  249.   if($this->state!="TRANSACTION")
  250.    return("connection is not in TRANSACTION state");
  251.   if($lines<0)
  252.   {
  253.    $command="RETR";
  254.    $arguments="$message";
  255.   }
  256.   else
  257.   {
  258.    $command="TOP";
  259.    $arguments="$message $lines";
  260.   }
  261.   if($this->PutLine("$command $arguments")==0)
  262.    return("Could not send the $command command");
  263.   $response=$this->GetLine();
  264.   if(GetType($response)!="string")
  265.    return("Could not get message retrieval command response");
  266.   if(strtok($response," ")!="+OK")
  267.    return("Could not retrieve the message: ".strtok("\r\n"));
  268.   for($headers=$body=array(),$line=0;;$line++)
  269.   {
  270.    $response=$this->GetLine();
  271.    if(GetType($response)!="string")
  272.     return("Could not retrieve the message");
  273.    switch($response)
  274.    {
  275.     case ".":
  276.      return("");
  277.     case "":
  278.      break 2;
  279.     default:
  280.      if(substr($response,0,1)==".")
  281.       $response=substr($response,1,strlen($response)-1);
  282.      break;
  283.    }
  284.    $headers[$line]=$response;
  285.   }
  286.   for($line=0;;$line++)
  287.   {
  288.    $response=$this->GetLine();
  289.    if(GetType($response)!="string")
  290.     return("Could not retrieve the message");
  291.    switch($response)
  292.    {
  293.     case ".":
  294.      return("");
  295.     default:
  296.      if(substr($response,0,1)==".")
  297.       $response=substr($response,1,strlen($response)-1);
  298.      break;
  299.    }
  300.    $body[$line]=$response;
  301.   }
  302.   return("");
  303.  }
  304.  
  305.  /* DeleteMessage method - the $message argument indicates the number of
  306.      a message to be marked as deleted.  Messages will only be effectively
  307.      deleted upon a successful call to the Close method. */
  308.  
  309.  Function DeleteMessage($message)
  310.  {
  311.   if($this->state!="TRANSACTION")
  312.    return("connection is not in TRANSACTION state");
  313.   if($this->PutLine("DELE $message")==0)
  314.    return("Could not send the DELE command");
  315.   $response=$this->GetLine();
  316.   if(GetType($response)!="string")
  317.    return("Could not get message delete command response");
  318.   if(strtok($response," ")!="+OK")
  319.    return("Could not delete the message: ".strtok("\r\n"));
  320.   $this->must_update=1;
  321.   return("");
  322.  }
  323.  
  324.  /* ResetDeletedMessages method - Reset the list of marked to be deleted
  325.      messages.  No messages will be marked to be deleted upon a successful
  326.      call to this method.  */
  327.  
  328.  Function ResetDeletedMessages()
  329.  {
  330.   if($this->state!="TRANSACTION")
  331.    return("connection is not in TRANSACTION state");
  332.   if($this->PutLine("RSET")==0)
  333.    return("Could not send the RSET command");
  334.   $response=$this->GetLine();
  335.   if(GetType($response)!="string")
  336.    return("Could not get reset deleted messages command response");
  337.   if(strtok($response," ")!="+OK")
  338.    return("Could not reset deleted messages: ".strtok("\r\n"));
  339.   $this->must_update=0;
  340.   return("");
  341.  }
  342.  
  343.  /* IssueNOOP method - Just pings the server to prevent it auto-close the
  344.      connection after an idle timeout (tipically 10 minutes).  Not very
  345.      useful for most likely uses of this class.  It's just here for
  346.      protocol support completeness.  */
  347.  
  348.  Function IssueNOOP()
  349.  {
  350.   if($this->state!="TRANSACTION")
  351.    return("connection is not in TRANSACTION state");
  352.   if($this->PutLine("NOOP")==0)
  353.    return("Could not send the NOOP command");
  354.   $response=$this->GetLine();
  355.   if(GetType($response)!="string")
  356.    return("Could not NOOP command response");
  357.   if(strtok($response," ")!="+OK")
  358.    return("Could not issue the NOOP command: ".strtok("\r\n"));
  359.   return("");
  360.  }
  361. };
  362.  
  363. /* ---- pop3.php3 class file ends here. ---- */
  364.  
  365. ?>
  366. <? ---- example file ---- ?><HTML>
  367. <HEAD>
  368. <TITLE>POP3 PHP class test</TITLE>
  369. </HEAD>
  370. <BODY>
  371. <?
  372.  include("pop3.php3");
  373.  
  374.  $user="mlemos";
  375.  $password="password";
  376.  $apop=0;
  377.  $pop3_connection=new pop3_class;
  378.  $pop3_connection->hostname="localhost";
  379.  if(($error=$pop3_connection->Open())=="")
  380.  {
  381.   echo "<PRE>Connected to the POP3 server "$pop3_connection->hostname".</PRE>\n";
  382.   if(($error=$pop3_connection->Login($user,$password,$apop))=="")
  383.   {
  384.    echo "<PRE>User "$user" logged in.</PRE>\n";
  385.    if(($error=$pop3_connection->Statistics(&$messages,&$size))=="")
  386.    {
  387.     echo "<PRE>There are $messages messages in the mail box with a total of $size bytes.</PRE>\n";
  388.     $result=$pop3_connection->ListMessages("",0);
  389.     if(GetType($result)=="array")
  390.     {
  391.      for(Reset($result),$message=0;$message<count($result);Next($result),$message++)
  392.       echo "<PRE>Message ",Key($result)," - ",$result[Key($result)]," bytes.</PRE>\n";
  393.      $result=$pop3_connection->ListMessages("",1);
  394.      if(GetType($result)=="array")
  395.      {
  396.       for(Reset($result),$message=0;$message<count($result);Next($result),$message++)
  397.        echo "<PRE>Message ",Key($result),", Unique ID - \"",$result[Key($result)],"\"</PRE>\n";
  398.       if($messages>0)
  399.       {
  400.        if(($error=$pop3_connection->RetrieveMessage(1,&$headers,&$body,2))=="")
  401.        {
  402.         echo "<PRE>Message 1:\n---Message headers starts below---</PRE>\n";
  403.         for($line=0;$line<count($headers);$line++)
  404.          echo "<PRE>",HtmlSpecialChars($headers[$line]),"</PRE>\n";
  405.         echo "<PRE>---Message headers ends above---\n---Message body starts below---</PRE>\n";
  406.         for($line=0;$line<count($body);$line++)
  407.          echo "<PRE>",HtmlSpecialChars($body[$line]),"</PRE>\n";
  408.         echo "<PRE>---Message body ends above---</PRE>\n";
  409.         if(($error=$pop3_connection->DeleteMessage(1))=="")
  410.         {
  411.          echo "<PRE>Marked message 1 for deletion.</PRE>\n";
  412.          if(($error=$pop3_connection->ResetDeletedMessages())=="")
  413.          {
  414.           echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
  415.          }
  416.         }
  417.        }
  418.       }
  419.       if($error==""
  420.       && ($error=$pop3_connection->Close())=="")
  421.        echo "<PRE>Disconnected from the POP3 server "$pop3_connection->hostname".</PRE>\n";
  422.       
  423.      }
  424.      else
  425.       $error=$result;
  426.     }
  427.     else
  428.      $error=$result;
  429.    }
  430.   }
  431.  }
  432.  if($error!="")
  433.   echo "<H2>Error: ",HtmlSpecialChars($error),"</H2>";
  434. ?>
  435.  
  436. </BODY>
  437. </HTML>
  438.  
  439.  
  440.