Просмотр файла magmail/newget.php

Размер файла: 4.75Kb
<?


require("functionChat.ssp");
$maxlen = 5000;
$LocalHost = (strpos(" ".$GLOBALS['DOCUMENT_ROOT'],'d:/web')>0);


$charset = "";

function data_decode($charset, $data)
// to win 1251
{
  if (strpos($charset,"1251") !== false) {return $data;}
  if (strpos($charset,"koi8") !== false)
    {return convert_cyr_string($data,"k","w");}
  if (strpos($charset,"8859") !== false)
    {return convert_cyr_string($data,"i","w");}
  if (strpos($charset,"866") !== false)
    {return convert_cyr_string($data,"a","w");}
  if (strpos($charset,"utf") !== false)
    {return iconv("UTF-8","windows-1251",$data);}
  // if unknown
  return convert_cyr_string($data,"k","w");
}


function SetCharset(&$structure, $data)
{
global $charset;
  $charset = "";
  if (isset($structure))
  {
    $para_count=count($structure->parameters);
    for ($i=0;$i<$para_count;$i++)
    {
      $att=$structure->parameters[$i]->attribute;
      $charset=strtolower($structure->parameters[$i]->value);
      if ($att == "charset") return data_decode($charset, $data);
    }
  }
  return convert_cyr_string($data,"k","w");
}

function get_subtype(&$structure)
{
if($structure->subtype)
 {
 return strtolower($structure->subtype);
 }
return "plain";
}


function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false)
{
if(!$structure)
 {
 $structure = imap_fetchstructure($stream, $msg_number);
 }
if($structure)
 {
 if(strtolower($mime_type) == get_subtype($structure))
  {
  if(!$part_number)
   {
   $part_number = "1";
   }
  $text = imap_fetchbody($stream, $msg_number, $part_number);
  if($structure->encoding == 3)
   {
   return SetCharset($structure,imap_base64($text));
   }
  else if($structure->encoding == 4)
   {
   return SetCharset($structure,quoted_printable_decode($text));
   }
  else
   {
   return SetCharset($structure,$text);
   }
  }
 if (isset($structure->parts) && (count($structure->parts) > 1)) /* multipart */
  {
  while(list($index, $sub_structure) = each($structure->parts))
   {
   $prefix = "";
   if($part_number)
    {
    $prefix = $part_number . '.';
    }
   $data = get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
   if($data)
    {
    return $data;
    }
   }
  }
 }
return false;
}


function mime_decode($s)
{
global $subcharset;
  $subcharset = "";
  $elements = imap_mime_header_decode($s);
  $decoded = "";
  for($i = 0;$i < count($elements);$i++) {
    $subcharset = strtolower($elements[$i]->charset);
    $text =$elements[$i]->text;
    $text = data_decode($subcharset, $text);
    $decoded = $decoded . $text;
  }
  return $decoded;
}
$f = fopen("getmail_log.txt","a+");
$start = date("d M H:i:s");
//echo "start=$start";
fwrite($f,"start=$start\n");


if ($LocalHost) $Host="{mail.valuehost.ru:143}INBOX";
else $Host="{mail.valuehost.ru:143/imap/notls}";
$Login="[email protected]";
$Password="";

$mbox = imap_open ($Host, $Login, $Password) or die ("can't connect: " . imap_last_error());

imap_expunge ($mbox);
$num = imap_num_msg($mbox);



for( $i=1; $i<=$num; $i++ ) {

  $head = imap_header($mbox,$i);
  $date = $head->date;
  $from = mime_decode($head->fromaddress);
  $subject = mime_decode($head->subject);

  $frombox = $head->from[0]->mailbox;
  $fromhost = $head->from[0]->host;
  $tobox = $head->to[0]->mailbox;

  if (strtolower($tobox) != 'testbest') continue;

  $tohost = $head->to[0]->host;

  $text = get_part($mbox, $i, "plain");
  if (!$text) $text = "Error: plain text not found";

  openDB();
  $mymess = "<$subject> $text";
  $mymess = ReplaceChar($mymess);
  if (strlen($mymess) > $maxlen)
  {
    $mymess = substr(ReplaceChar($mymess),0,5000)." [lim=5kb]";
  }
  $now = time();

  $sqlSel="select * from users where nick='$tobox'";
  $result=mysql_query($sqlSel) or die(mysql_error());
  $CountUsers=mysql_num_rows($result);
  $res = "Not found";
  If ($CountUsers == 1)
  {
    $fromad = "$frombox@$fromhost";
    if (strtolower($tobox) != 'testbest')
    {
      $sqlnext ="insert into letters (messtime,fromid,toid,fromnick,tonick,messtype,messtext) values ('$now',0,0,'$fromad','$tobox','$UnReadType','$mymess')";
      mysql_query($sqlnext) or die(mysql_error());
      $res = "OK";
    }
  }
//  echo "<br>time=".date("d M H:i:s")." from=$frombox@$fromhost to=$tobox@$tohost charset=$charset text20=[".substr($text,0,20)."] res=$res\n";
  fwrite($f, "time=".date("d M H:i:s")." from=$frombox@$fromhost to=$tobox@$tohost subject=$subject charset=$charset text20=[".substr($text,0,20)."] res=$res \n");
}
$stop = date("d M H:i:s");
//echo "  stop=$stop";
fwrite($f,"stop=$stop\n");
mysql_close();
fclose($f);
imap_expunge ($mbox);
imap_close ($mbox);
?>