Advertisement
Guest User

Untitled

a guest
May 28th, 2017
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.68 KB | None | 0 0
  1. <?php
  2.  
  3. // Vars
  4.  
  5. $time_difference=absint(0)*3600;
  6.  
  7. $phone_delim='::';
  8.  
  9. $hosturl='';
  10. $hostport='110';
  11. $hostlogin='';
  12. $hostpassword='';
  13.  
  14. $characterset='utf-8';
  15.  
  16.  
  17. $dbhost='localhost';
  18. $dbusername='';
  19. $dbpassword='';
  20. $db='';
  21.  
  22. // Functions
  23.  
  24. function absint( $maybeint ) {
  25.     return abs( intval( $maybeint ) );
  26. }
  27.  
  28. function sanitize_email($email) {
  29.     return preg_replace('/[^a-z0-9+_.@-]/i', '', $email);
  30. }
  31.  
  32. function iso_descrambler($string) {
  33.     if(!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)){
  34.         return $string;
  35.     }else{
  36.         $subject=str_replace('_', ' ', $matches[2]);
  37.         $subject=preg_replace_callback('#\=([0-9a-f]{2})#i', create_function('$match', 'return chr(hexdec(strtolower($match[1])));'), $subject);
  38.         return $subject;
  39.     }
  40. }
  41.  
  42. function xmlrpc_getposttitle($content){
  43.     global $post_default_title;
  44.     if(preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle)){
  45.         $post_title=$matchtitle[0];
  46.         $post_title=preg_replace('/<title>/si', '', $post_title);
  47.         $post_title=preg_replace('/<\/title>/si', '', $post_title);
  48.     }else{
  49.         $post_title = $post_default_title;
  50.     }
  51.    
  52.     return $post_title;
  53. }
  54.  
  55. // Required
  56.  
  57. require_once('class-pop3.php' );
  58.  
  59.  
  60. // POP3
  61.  
  62. $pop3 = new POP3();
  63.  
  64. if(!$pop3->connect($hosturl, $hostport) ||  !$pop3->user($hostlogin) || (!$count = $pop3->pass($hostpassword))) {
  65.    
  66.     $pop3->quit();
  67.    
  68.     if(0 === $count){
  69.         echo "There doesn't seem to be any new mail."; 
  70.         $pop3->ERROR();
  71.     }
  72. }
  73.  
  74. for($i = 1; $i <= $count; $i++){
  75.  
  76.     $message = $pop3->get($i);
  77.  
  78.     foreach ($message as $line) {
  79.  
  80.     $bodysignal=false;
  81.     $boundary='';
  82.     $charset='';
  83.     $content='';
  84.     $content_type='';
  85.     $content_transfer_encoding='';
  86.     $author_found=false;
  87.     $dmonths=array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  88.  
  89.         if (strlen($line)<3){
  90.             $bodysignal=true;
  91.         }
  92.        
  93.         if($bodysignal){
  94.             $content.=$line;
  95.         }else{
  96.            
  97.             if (preg_match('/Content-Type: /i', $line)){
  98.                 $content_type=trim($line);
  99.                 $content_type=substr($content_type, 14, strlen($content_type) - 14);
  100.                 $content_type=explode(';', $content_type);
  101.                
  102.                 if(!empty($content_type[1])){
  103.                     $charset=explode('=', $content_type[1]);
  104.                     $charset=(!empty( $charset[1]))?trim($charset[1]):'';
  105.                 }
  106.                
  107.                 $content_type=$content_type[0];
  108.             }
  109.            
  110.             if(preg_match('/Content-Transfer-Encoding: /i', $line)){
  111.                 $content_transfer_encoding=trim($line);
  112.                 $content_transfer_encoding=substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
  113.                 $content_transfer_encoding=explode(';', $content_transfer_encoding);
  114.                 $content_transfer_encoding=$content_transfer_encoding[0];
  115.             }
  116.            
  117.             if(($content_type=='multipart/alternative')&&(false!==strpos($line, 'boundary="'))&&('' == $boundary)){
  118.                 $boundary=trim($line);
  119.                 $boundary=explode('"', $boundary);
  120.                 $boundary=$boundary[1];
  121.             }
  122.            
  123.             if(preg_match('/Subject: /i', $line)){
  124.                 $subject=trim($line);
  125.                 $subject=substr($subject, 9, strlen($subject) - 9);
  126.                
  127.                 if(function_exists('iconv_mime_decode')){
  128.                     $subject=iconv_mime_decode($subject, 2, $charset);
  129.                 }else{
  130.                     $subject=iso_descrambler($subject);
  131.                 }
  132.                
  133.                 $subject=explode($phone_delim, $subject);
  134.                 $subject=$subject[0];
  135.             }          
  136.  
  137.             if (preg_match('/(From|Reply-To): /', $line)) {
  138.                
  139.                 if(preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches)){
  140.                     $author=$matches[0];
  141.                 }else{
  142.                     $author=trim($line);
  143.                     $author=sanitize_email($author);
  144.                 }
  145.                
  146.                 $post_author=$author;
  147.             }
  148.            
  149.            
  150.             if(preg_match('/Date: /i', $line)){
  151.                 $ddate=trim($line);
  152.                 $ddate=str_replace('Date: ', '', $ddate);
  153.                
  154.                 if(strpos($ddate, ',')){
  155.                     $ddate=trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate)));
  156.                 }
  157.                
  158.                 $date_arr=explode(' ', $ddate);
  159.                 $date_time=explode(':', $date_arr[3]);
  160.  
  161.                 $ddate_H=$date_time[0];
  162.                 $ddate_i=$date_time[1];
  163.                 $ddate_s=$date_time[2];
  164.  
  165.                 $ddate_m=$date_arr[1];
  166.                 $ddate_d=$date_arr[0];
  167.                 $ddate_Y=$date_arr[2];
  168.                 for ($j=0; $j<12; $j++){
  169.                     if($ddate_m==$dmonths[$j]){
  170.                         $ddate_m=$j+1;
  171.                     }
  172.                 }
  173.  
  174.                 $time_zn = intval($date_arr[4]) * 36;
  175.                 $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
  176.                 $ddate_U = $ddate_U - $time_zn;
  177.                 $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
  178.                 $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
  179.             }
  180.         }
  181.     }
  182.    
  183.     $subject = trim($subject);
  184.  
  185.     if ($content_type=='multipart/alternative'){
  186.         $content=explode('--'.$boundary, $content);
  187.         $content=$content[2];
  188.        
  189.         if (preg_match('/Content-Transfer-Encoding: quoted-printable/i', $content, $delim)){
  190.             $content=explode($delim[0], $content);
  191.             $content=$content[1];
  192.         }
  193.        
  194.         $content=strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
  195.     }
  196.    
  197.     $content=trim($content);
  198.  
  199.     if (false!==stripos($content_transfer_encoding, "quoted-printable")){
  200.     $content=quoted_printable_decode($content);
  201.     }
  202.  
  203.     if (function_exists('iconv') && !empty($charset)){
  204.         $content=iconv($charset, $characterset, $content);
  205.     }
  206.  
  207.     $content=explode($phone_delim, $content);
  208.     $content=empty($content[1])?$content[0]:$content[1];
  209.  
  210.     $content=trim($content);
  211.     $post_content=$content;
  212.  
  213.     $post_title=xmlrpc_getposttitle($content);
  214.  
  215.     if ($post_title==''){
  216.     $post_title = $subject;
  217.     }
  218.    
  219.     mysql_connect($dbhost, $dbusername, $dbpassword) or die(mysql_error());
  220.     mysql_select_db($db) or die(mysql_error());
  221.  
  222.     mysql_query("INSERT INTO tablename (author, subject, body, date) VALUES('$post_author', '$post_title', '$post_content', '$post_date')") or die(mysql_error());  
  223.  
  224.     if(!$pop3->delete($i)) {       
  225.         $pop3->reset();
  226.         exit;
  227.     }
  228.  
  229. }
  230.  
  231. $pop3->quit();
  232.  
  233. ?>
  234.  
  235.  
  236. //////////////////////////// class-pop3 ////////////////////////////////
  237.  
  238.  
  239. <?php
  240. /**
  241.  * mail_fetch/setup.php
  242.  *
  243.  * @package SquirrelMail
  244.  *
  245.  * @copyright (c) 1999-2006 The SquirrelMail Project Team
  246.  *
  247.  * @copyright (c) 1999 CDI (cdi@thewebmasters.net) All Rights Reserved
  248.  * Modified by Philippe Mingo 2001 mingo@rotedic.com
  249.  * An RFC 1939 compliant wrapper class for the POP3 protocol.
  250.  *
  251.  * Licensed under the GNU GPL. For full terms see the file COPYING.
  252.  *
  253.  * pop3 class
  254.  *
  255.  * $Id: class-pop3.php 9503 2008-11-03 23:25:11Z ryan $
  256.  */
  257.  
  258. class POP3 {
  259.     var $ERROR      = '';       //  Error string.
  260.  
  261.     var $TIMEOUT    = 60;       //  Default timeout before giving up on a
  262.                                 //  network operation.
  263.  
  264.     var $COUNT      = -1;       //  Mailbox msg count
  265.  
  266.     var $BUFFER     = 512;      //  Socket buffer for socket fgets() calls.
  267.                                 //  Per RFC 1939 the returned line a POP3
  268.                                 //  server can send is 512 bytes.
  269.  
  270.     var $FP         = '';       //  The connection to the server's
  271.                                 //  file descriptor
  272.  
  273.     var $MAILSERVER = '';       // Set this to hard code the server name
  274.  
  275.     var $DEBUG      = FALSE;    // set to true to echo pop3
  276.                                 // commands and responses to error_log
  277.                                 // this WILL log passwords!
  278.  
  279.     var $BANNER     = '';       //  Holds the banner returned by the
  280.                                 //  pop server - used for apop()
  281.  
  282.     var $ALLOWAPOP  = FALSE;    //  Allow or disallow apop()
  283.                                 //  This must be set to true
  284.                                 //  manually
  285.  
  286.     function POP3 ( $server = '', $timeout = '' ) {
  287.         settype($this->BUFFER,"integer");
  288.         if( !empty($server) ) {
  289.             // Do not allow programs to alter MAILSERVER
  290.             // if it is already specified. They can get around
  291.             // this if they -really- want to, so don't count on it.
  292.             if(empty($this->MAILSERVER))
  293.                 $this->MAILSERVER = $server;
  294.         }
  295.         if(!empty($timeout)) {
  296.             settype($timeout,"integer");
  297.             $this->TIMEOUT = $timeout;
  298.             if (!ini_get('safe_mode'))
  299.                 set_time_limit($timeout);
  300.         }
  301.         return true;
  302.     }
  303.  
  304.     function update_timer () {
  305.         if (!ini_get('safe_mode'))
  306.             set_time_limit($this->TIMEOUT);
  307.         return true;
  308.     }
  309.  
  310.     function connect ($server, $port = 110)  {
  311.         //  Opens a socket to the specified server. Unless overridden,
  312.         //  port defaults to 110. Returns true on success, false on fail
  313.  
  314.         // If MAILSERVER is set, override $server with it's value
  315.  
  316.     if (!isset($port) || !$port) {$port = 110;}
  317.         if(!empty($this->MAILSERVER))
  318.             $server = $this->MAILSERVER;
  319.  
  320.         if(empty($server)){
  321.             $this->ERROR = "POP3 connect: " . _("No server specified");
  322.             unset($this->FP);
  323.             return false;
  324.         }
  325.  
  326.         $fp = @fsockopen("$server", $port, $errno, $errstr);
  327.  
  328.         if(!$fp) {
  329.             $this->ERROR = "POP3 connect: " . _("Error ") . "[$errno] [$errstr]";
  330.             unset($this->FP);
  331.             return false;
  332.         }
  333.  
  334.         socket_set_blocking($fp,-1);
  335.         $this->update_timer();
  336.         $reply = fgets($fp,$this->BUFFER);
  337.         $reply = $this->strip_clf($reply);
  338.         if($this->DEBUG)
  339.             error_log("POP3 SEND [connect: $server] GOT [$reply]",0);
  340.         if(!$this->is_ok($reply)) {
  341.             $this->ERROR = "POP3 connect: " . _("Error ") . "[$reply]";
  342.             unset($this->FP);
  343.             return false;
  344.         }
  345.         $this->FP = $fp;
  346.         $this->BANNER = $this->parse_banner($reply);
  347.         return true;
  348.     }
  349.  
  350.     function user ($user = "") {
  351.         // Sends the USER command, returns true or false
  352.  
  353.         if( empty($user) ) {
  354.             $this->ERROR = "POP3 user: " . _("no login ID submitted");
  355.             return false;
  356.         } elseif(!isset($this->FP)) {
  357.             $this->ERROR = "POP3 user: " . _("connection not established");
  358.             return false;
  359.         } else {
  360.             $reply = $this->send_cmd("USER $user");
  361.             if(!$this->is_ok($reply)) {
  362.                 $this->ERROR = "POP3 user: " . _("Error ") . "[$reply]";
  363.                 return false;
  364.             } else
  365.                 return true;
  366.         }
  367.     }
  368.  
  369.     function pass ($pass = "")     {
  370.         // Sends the PASS command, returns # of msgs in mailbox,
  371.         // returns false (undef) on Auth failure
  372.  
  373.         if(empty($pass)) {
  374.             $this->ERROR = "POP3 pass: " . _("No password submitted");
  375.             return false;
  376.         } elseif(!isset($this->FP)) {
  377.             $this->ERROR = "POP3 pass: " . _("connection not established");
  378.             return false;
  379.         } else {
  380.             $reply = $this->send_cmd("PASS $pass");
  381.             if(!$this->is_ok($reply)) {
  382.                 $this->ERROR = "POP3 pass: " . _("Authentication failed") . " [$reply]";
  383.                 $this->quit();
  384.                 return false;
  385.             } else {
  386.                 //  Auth successful.
  387.                 $count = $this->last("count");
  388.                 $this->COUNT = $count;
  389.                 return $count;
  390.             }
  391.         }
  392.     }
  393.  
  394.     function apop ($login,$pass) {
  395.         //  Attempts an APOP login. If this fails, it'll
  396.         //  try a standard login. YOUR SERVER MUST SUPPORT
  397.         //  THE USE OF THE APOP COMMAND!
  398.         //  (apop is optional per rfc1939)
  399.  
  400.         if(!isset($this->FP)) {
  401.             $this->ERROR = "POP3 apop: " . _("No connection to server");
  402.             return false;
  403.         } elseif(!$this->ALLOWAPOP) {
  404.             $retVal = $this->login($login,$pass);
  405.             return $retVal;
  406.         } elseif(empty($login)) {
  407.             $this->ERROR = "POP3 apop: " . _("No login ID submitted");
  408.             return false;
  409.         } elseif(empty($pass)) {
  410.             $this->ERROR = "POP3 apop: " . _("No password submitted");
  411.             return false;
  412.         } else {
  413.             $banner = $this->BANNER;
  414.             if( (!$banner) or (empty($banner)) ) {
  415.                 $this->ERROR = "POP3 apop: " . _("No server banner") . ' - ' . _("abort");
  416.                 $retVal = $this->login($login,$pass);
  417.                 return $retVal;
  418.             } else {
  419.                 $AuthString = $banner;
  420.                 $AuthString .= $pass;
  421.                 $APOPString = md5($AuthString);
  422.                 $cmd = "APOP $login $APOPString";
  423.                 $reply = $this->send_cmd($cmd);
  424.                 if(!$this->is_ok($reply)) {
  425.                     $this->ERROR = "POP3 apop: " . _("apop authentication failed") . ' - ' . _("abort");
  426.                     $retVal = $this->login($login,$pass);
  427.                     return $retVal;
  428.                 } else {
  429.                     //  Auth successful.
  430.                     $count = $this->last("count");
  431.                     $this->COUNT = $count;
  432.                     return $count;
  433.                 }
  434.             }
  435.         }
  436.     }
  437.  
  438.     function login ($login = "", $pass = "") {
  439.         // Sends both user and pass. Returns # of msgs in mailbox or
  440.         // false on failure (or -1, if the error occurs while getting
  441.         // the number of messages.)
  442.  
  443.         if( !isset($this->FP) ) {
  444.             $this->ERROR = "POP3 login: " . _("No connection to server");
  445.             return false;
  446.         } else {
  447.             $fp = $this->FP;
  448.             if( !$this->user( $login ) ) {
  449.                 //  Preserve the error generated by user()
  450.                 return false;
  451.             } else {
  452.                 $count = $this->pass($pass);
  453.                 if( (!$count) || ($count == -1) ) {
  454.                     //  Preserve the error generated by last() and pass()
  455.                     return false;
  456.                 } else
  457.                     return $count;
  458.             }
  459.         }
  460.     }
  461.  
  462.     function top ($msgNum, $numLines = "0") {
  463.         //  Gets the header and first $numLines of the msg body
  464.         //  returns data in an array with each returned line being
  465.         //  an array element. If $numLines is empty, returns
  466.         //  only the header information, and none of the body.
  467.  
  468.         if(!isset($this->FP)) {
  469.             $this->ERROR = "POP3 top: " . _("No connection to server");
  470.             return false;
  471.         }
  472.         $this->update_timer();
  473.  
  474.         $fp = $this->FP;
  475.         $buffer = $this->BUFFER;
  476.         $cmd = "TOP $msgNum $numLines";
  477.         fwrite($fp, "TOP $msgNum $numLines\r\n");
  478.         $reply = fgets($fp, $buffer);
  479.         $reply = $this->strip_clf($reply);
  480.         if($this->DEBUG) {
  481.             @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
  482.         }
  483.         if(!$this->is_ok($reply))
  484.         {
  485.             $this->ERROR = "POP3 top: " . _("Error ") . "[$reply]";
  486.             return false;
  487.         }
  488.  
  489.         $count = 0;
  490.         $MsgArray = array();
  491.  
  492.         $line = fgets($fp,$buffer);
  493.         while ( !ereg("^\.\r\n",$line))
  494.         {
  495.             $MsgArray[$count] = $line;
  496.             $count++;
  497.             $line = fgets($fp,$buffer);
  498.             if(empty($line))    { break; }
  499.         }
  500.  
  501.         return $MsgArray;
  502.     }
  503.  
  504.     function pop_list ($msgNum = "") {
  505.         //  If called with an argument, returns that msgs' size in octets
  506.         //  No argument returns an associative array of undeleted
  507.         //  msg numbers and their sizes in octets
  508.  
  509.         if(!isset($this->FP))
  510.         {
  511.             $this->ERROR = "POP3 pop_list: " . _("No connection to server");
  512.             return false;
  513.         }
  514.         $fp = $this->FP;
  515.         $Total = $this->COUNT;
  516.         if( (!$Total) or ($Total == -1) )
  517.         {
  518.             return false;
  519.         }
  520.         if($Total == 0)
  521.         {
  522.             return array("0","0");
  523.             // return -1;   // mailbox empty
  524.         }
  525.  
  526.         $this->update_timer();
  527.  
  528.         if(!empty($msgNum))
  529.         {
  530.             $cmd = "LIST $msgNum";
  531.             fwrite($fp,"$cmd\r\n");
  532.             $reply = fgets($fp,$this->BUFFER);
  533.             $reply = $this->strip_clf($reply);
  534.             if($this->DEBUG) {
  535.                 @error_log("POP3 SEND [$cmd] GOT [$reply]",0);
  536.             }
  537.             if(!$this->is_ok($reply))
  538.             {
  539.                 $this->ERROR = "POP3 pop_list: " . _("Error ") . "[$reply]";
  540.                 return false;
  541.             }
  542.             list($junk,$num,$size) = preg_split('/\s+/',$reply);
  543.             return $size;
  544.         }
  545.         $cmd = "LIST";
  546.         $reply = $this->send_cmd($cmd);
  547.         if(!$this->is_ok($reply))
  548.         {
  549.             $reply = $this->strip_clf($reply);
  550.             $this->ERROR = "POP3 pop_list: " . _("Error ") .  "[$reply]";
  551.             return false;
  552.         }
  553.         $MsgArray = array();
  554.         $MsgArray[0] = $Total;
  555.         for($msgC=1;$msgC <= $Total; $msgC++)
  556.         {
  557.             if($msgC > $Total) { break; }
  558.             $line = fgets($fp,$this->BUFFER);
  559.             $line = $this->strip_clf($line);
  560.             if(ereg("^\.",$line))
  561.             {
  562.                 $this->ERROR = "POP3 pop_list: " . _("Premature end of list");
  563.                 return false;
  564.             }
  565.             list($thisMsg,$msgSize) = preg_split('/\s+/',$line);
  566.             settype($thisMsg,"integer");
  567.             if($thisMsg != $msgC)
  568.             {
  569.                 $MsgArray[$msgC] = "deleted";
  570.             }
  571.             else
  572.             {
  573.                 $MsgArray[$msgC] = $msgSize;
  574.             }
  575.         }
  576.         return $MsgArray;
  577.     }
  578.  
  579.     function get ($msgNum) {
  580.         //  Retrieve the specified msg number. Returns an array
  581.         //  where each line of the msg is an array element.
  582.  
  583.         if(!isset($this->FP))
  584.         {
  585.             $this->ERROR = "POP3 get: " . _("No connection to server");
  586.             return false;
  587.         }
  588.  
  589.         $this->update_timer();
  590.  
  591.         $fp = $this->FP;
  592.         $buffer = $this->BUFFER;
  593.         $cmd = "RETR $msgNum";
  594.         $reply = $this->send_cmd($cmd);
  595.  
  596.         if(!$this->is_ok($reply))
  597.         {
  598.             $this->ERROR = "POP3 get: " . _("Error ") . "[$reply]";
  599.             return false;
  600.         }
  601.  
  602.         $count = 0;
  603.         $MsgArray = array();
  604.  
  605.         $line = fgets($fp,$buffer);
  606.         while ( !ereg("^\.\r\n",$line))
  607.         {
  608.             if ( $line{0} == '.' ) { $line = substr($line,1); }
  609.             $MsgArray[$count] = $line;
  610.             $count++;
  611.             $line = fgets($fp,$buffer);
  612.             if(empty($line))    { break; }
  613.         }
  614.         return $MsgArray;
  615.     }
  616.  
  617.     function last ( $type = "count" ) {
  618.         //  Returns the highest msg number in the mailbox.
  619.         //  returns -1 on error, 0+ on success, if type != count
  620.         //  results in a popstat() call (2 element array returned)
  621.  
  622.         $last = -1;
  623.         if(!isset($this->FP))
  624.         {
  625.             $this->ERROR = "POP3 last: " . _("No connection to server");
  626.             return $last;
  627.         }
  628.  
  629.         $reply = $this->send_cmd("STAT");
  630.         if(!$this->is_ok($reply))
  631.         {
  632.             $this->ERROR = "POP3 last: " . _("Error ") . "[$reply]";
  633.             return $last;
  634.         }
  635.  
  636.         $Vars = preg_split('/\s+/',$reply);
  637.         $count = $Vars[1];
  638.         $size = $Vars[2];
  639.         settype($count,"integer");
  640.         settype($size,"integer");
  641.         if($type != "count")
  642.         {
  643.             return array($count,$size);
  644.         }
  645.         return $count;
  646.     }
  647.  
  648.     function reset () {
  649.         //  Resets the status of the remote server. This includes
  650.         //  resetting the status of ALL msgs to not be deleted.
  651.         //  This method automatically closes the connection to the server.
  652.  
  653.         if(!isset($this->FP))
  654.         {
  655.             $this->ERROR = "POP3 reset: " . _("No connection to server");
  656.             return false;
  657.         }
  658.         $reply = $this->send_cmd("RSET");
  659.         if(!$this->is_ok($reply))
  660.         {
  661.             //  The POP3 RSET command -never- gives a -ERR
  662.             //  response - if it ever does, something truely
  663.             //  wild is going on.
  664.  
  665.             $this->ERROR = "POP3 reset: " . _("Error ") . "[$reply]";
  666.             @error_log("POP3 reset: ERROR [$reply]",0);
  667.         }
  668.         $this->quit();
  669.         return true;
  670.     }
  671.  
  672.     function send_cmd ( $cmd = "" )
  673.     {
  674.         //  Sends a user defined command string to the
  675.         //  POP server and returns the results. Useful for
  676.         //  non-compliant or custom POP servers.
  677.         //  Do NOT includ the \r\n as part of your command
  678.         //  string - it will be appended automatically.
  679.  
  680.         //  The return value is a standard fgets() call, which
  681.         //  will read up to $this->BUFFER bytes of data, until it
  682.         //  encounters a new line, or EOF, whichever happens first.
  683.  
  684.         //  This method works best if $cmd responds with only
  685.         //  one line of data.
  686.  
  687.         if(!isset($this->FP))
  688.         {
  689.             $this->ERROR = "POP3 send_cmd: " . _("No connection to server");
  690.             return false;
  691.         }
  692.  
  693.         if(empty($cmd))
  694.         {
  695.             $this->ERROR = "POP3 send_cmd: " . _("Empty command string");
  696.             return "";
  697.         }
  698.  
  699.         $fp = $this->FP;
  700.         $buffer = $this->BUFFER;
  701.         $this->update_timer();
  702.         fwrite($fp,"$cmd\r\n");
  703.         $reply = fgets($fp,$buffer);
  704.         $reply = $this->strip_clf($reply);
  705.         if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  706.         return $reply;
  707.     }
  708.  
  709.     function quit() {
  710.         //  Closes the connection to the POP3 server, deleting
  711.         //  any msgs marked as deleted.
  712.  
  713.         if(!isset($this->FP))
  714.         {
  715.             $this->ERROR = "POP3 quit: " . _("connection does not exist");
  716.             return false;
  717.         }
  718.         $fp = $this->FP;
  719.         $cmd = "QUIT";
  720.         fwrite($fp,"$cmd\r\n");
  721.         $reply = fgets($fp,$this->BUFFER);
  722.         $reply = $this->strip_clf($reply);
  723.         if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  724.         fclose($fp);
  725.         unset($this->FP);
  726.         return true;
  727.     }
  728.  
  729.     function popstat () {
  730.         //  Returns an array of 2 elements. The number of undeleted
  731.         //  msgs in the mailbox, and the size of the mbox in octets.
  732.  
  733.         $PopArray = $this->last("array");
  734.  
  735.         if($PopArray == -1) { return false; }
  736.  
  737.         if( (!$PopArray) or (empty($PopArray)) )
  738.         {
  739.             return false;
  740.         }
  741.         return $PopArray;
  742.     }
  743.  
  744.     function uidl ($msgNum = "")
  745.     {
  746.         //  Returns the UIDL of the msg specified. If called with
  747.         //  no arguments, returns an associative array where each
  748.         //  undeleted msg num is a key, and the msg's uidl is the element
  749.         //  Array element 0 will contain the total number of msgs
  750.  
  751.         if(!isset($this->FP)) {
  752.             $this->ERROR = "POP3 uidl: " . _("No connection to server");
  753.             return false;
  754.         }
  755.  
  756.         $fp = $this->FP;
  757.         $buffer = $this->BUFFER;
  758.  
  759.         if(!empty($msgNum)) {
  760.             $cmd = "UIDL $msgNum";
  761.             $reply = $this->send_cmd($cmd);
  762.             if(!$this->is_ok($reply))
  763.             {
  764.                 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
  765.                 return false;
  766.             }
  767.             list ($ok,$num,$myUidl) = preg_split('/\s+/',$reply);
  768.             return $myUidl;
  769.         } else {
  770.             $this->update_timer();
  771.  
  772.             $UIDLArray = array();
  773.             $Total = $this->COUNT;
  774.             $UIDLArray[0] = $Total;
  775.  
  776.             if ($Total < 1)
  777.             {
  778.                 return $UIDLArray;
  779.             }
  780.             $cmd = "UIDL";
  781.             fwrite($fp, "UIDL\r\n");
  782.             $reply = fgets($fp, $buffer);
  783.             $reply = $this->strip_clf($reply);
  784.             if($this->DEBUG) { @error_log("POP3 SEND [$cmd] GOT [$reply]",0); }
  785.             if(!$this->is_ok($reply))
  786.             {
  787.                 $this->ERROR = "POP3 uidl: " . _("Error ") . "[$reply]";
  788.                 return false;
  789.             }
  790.  
  791.             $line = "";
  792.             $count = 1;
  793.             $line = fgets($fp,$buffer);
  794.             while ( !ereg("^\.\r\n",$line)) {
  795.                 if(ereg("^\.\r\n",$line)) {
  796.                     break;
  797.                 }
  798.                 list ($msg,$msgUidl) = preg_split('/\s+/',$line);
  799.                 $msgUidl = $this->strip_clf($msgUidl);
  800.                 if($count == $msg) {
  801.                     $UIDLArray[$msg] = $msgUidl;
  802.                 }
  803.                 else
  804.                 {
  805.                     $UIDLArray[$count] = 'deleted';
  806.                 }
  807.                 $count++;
  808.                 $line = fgets($fp,$buffer);
  809.             }
  810.         }
  811.         return $UIDLArray;
  812.     }
  813.  
  814.     function delete ($msgNum = "") {
  815.         //  Flags a specified msg as deleted. The msg will not
  816.         //  be deleted until a quit() method is called.
  817.  
  818.         if(!isset($this->FP))
  819.         {
  820.             $this->ERROR = "POP3 delete: " . _("No connection to server");
  821.             return false;
  822.         }
  823.         if(empty($msgNum))
  824.         {
  825.             $this->ERROR = "POP3 delete: " . _("No msg number submitted");
  826.             return false;
  827.         }
  828.         $reply = $this->send_cmd("DELE $msgNum");
  829.         if(!$this->is_ok($reply))
  830.         {
  831.             $this->ERROR = "POP3 delete: " . _("Command failed ") . "[$reply]";
  832.             return false;
  833.         }
  834.         return true;
  835.     }
  836.  
  837.     //  *********************************************************
  838.  
  839.     //  The following methods are internal to the class.
  840.  
  841.     function is_ok ($cmd = "") {
  842.         //  Return true or false on +OK or -ERR
  843.  
  844.         if( empty($cmd) )
  845.             return false;
  846.         else
  847.             return( ereg ("^\+OK", $cmd ) );
  848.     }
  849.  
  850.     function strip_clf ($text = "") {
  851.         // Strips \r\n from server responses
  852.  
  853.         if(empty($text))
  854.             return $text;
  855.         else {
  856.             $stripped = str_replace("\r",'',$text);
  857.             $stripped = str_replace("\n",'',$stripped);
  858.             return $stripped;
  859.         }
  860.     }
  861.  
  862.     function parse_banner ( $server_text ) {
  863.         $outside = true;
  864.         $banner = "";
  865.         $length = strlen($server_text);
  866.         for($count =0; $count < $length; $count++)
  867.         {
  868.             $digit = substr($server_text,$count,1);
  869.             if(!empty($digit))             {
  870.                 if( (!$outside) && ($digit != '<') && ($digit != '>') )
  871.                 {
  872.                     $banner .= $digit;
  873.                 }
  874.                 if ($digit == '<')
  875.                 {
  876.                     $outside = false;
  877.                 }
  878.                 if($digit == '>')
  879.                 {
  880.                     $outside = true;
  881.                 }
  882.             }
  883.         }
  884.         $banner = $this->strip_clf($banner);    // Just in case
  885.         return "<$banner>";
  886.     }
  887.  
  888. }   // End class
  889. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement