За последние 24 часа нас посетили 18369 программистов и 1613 роботов. Сейчас ищут 1849 программистов ...

Небольшой pop3 класс

Тема в разделе "Решения, алгоритмы", создана пользователем EvelRus, 1 мар 2008.

  1. EvelRus

    EvelRus Активный пользователь

    С нами с:
    16 ноя 2006
    Сообщения:
    2.168
    Симпатии:
    0
    Адрес:
    Москва
    Класс не мой, но хороший )))

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

    RomanBush Активный пользователь

    С нами с:
    5 дек 2007
    Сообщения:
    798
    Симпатии:
    0
    Адрес:
    200 км от Москвы
    Бессмысленный класс. Вот если бы он разбирал multipart - messages, выдирал картинки и вставлял их в нужные места html-части сообщения - вот тогда да. Было бы интересно. А просто принять почту - это не фокус - это на полчаса работы.
     
  3. EvelRus

    EvelRus Активный пользователь

    С нами с:
    16 ноя 2006
    Сообщения:
    2.168
    Симпатии:
    0
    Адрес:
    Москва
    имхо все с чего-то начинается :)))
    Можешь предложить свой вариант :))))