Просмотр файла includes/upload_file_from_email.php

Размер файла: 8.62Kb
<?php
#if ($user=='test') {header("Content-Type: text/plain");

$upload_email=@file('/home/kmx/www/sites/upload_email.dat');

# Время поледней проверки ящика
if ($upload_email[0]<(time()-300))
if ($mbox=imap_open("{pop.mail.ru/pop3:110}INBOX","kmxupload","kmx-hosting"))
	{
	$num = imap_num_msg($mbox);
#	print "============== ВСЕГО: $num писем. =================";
	for( $i=1; $i<=$num; $i++ )
	    {
	    $header = imap_fetchheader( $mbox, $i, FT_PREFETCHTEXT);
	    $header2 = imap_fetch_overview( $mbox, $i);
	    $param=get_object_vars($header2[0]);
	    $struct=imap_fetchstructure( $mbox, $i);
	    $struct=get_object_vars($struct);

	    $subj=$param['subject'];
	    $subj=imap_mime_header_decode($subj);
	    $subj=get_object_vars($subj[0]);
	    $subj=$subj['text'];

#	    print  "\n==[$i]=========================\n";
		print_r($struct);
#		print  "\n==[$i]------------------------=\n";
	    $parameter=$struct['parameters'];
	    $parts=$struct['parts'];  //массив частей в виде объектов
	    $count_parts=sizeof($struct['parts']);

	    if ($count_parts<=1)
	        {
	        $count_parts=1;
	        $encoding[0]=$struct['encoding'];
	        $subtype[0]=$struct['subtype'];
	        $type[0]=$struct['type'];
            $i_param_found=false;

	        if ($part['ifparameters'])
	            {
	            $parameter_a=$part['parameters'];
	            for ($i_param=0;(($i_param<sizeof($parameter_a)) && !$i_param_found);$i_param++)
	                {
	                $parameter=get_object_vars($parameter_a[$i_param]);
	                $atrib[$j-1]=$parameter['attribute'];
	                $value[$j-1]=$parameter['value'];
	                if (($atrib[$j-1]=='NAME')||($atrib[$j-1]=='FILENAME')) $i_param_found=true;
	                }
	            }

	        if ($part['ifdparameters'] && !$i_param_found)
	            {
	            $parameter_a=$part['dparameters'];
	            for ($i_param=0;(($i_param<sizeof($parameter_a)) && !$i_param_found);$i_param++)
	                {
	                $parameter=get_object_vars($parameter_a[$i_param]);
	                $atrib[$j-1]=$parameter['attribute'];
	                $value[$j-1]=$parameter['value'];
	                if (($atrib[$j-1]=='NAME')||($atrib[$j-1]=='FILENAME')) $i_param_found=true;
	                }
	            }
	        }

	    if ($count_parts>1)
	        {
	        for ($j=1;$j<=$count_parts;$j++)
	            {
	            $part=get_object_vars($parts[$j-1]);
	            $encoding[$j-1]=$part['encoding'];
	            $subtype[$j-1]=$part['subtype'];
	            $type[$j-1]=$part['type'];
                $i_param_found=false;

	            if ($part['ifparameters'])
	                {
	                $parameter_a=$part['parameters'];
                    for ($i_param=0;(($i_param<sizeof($parameter_a)) && !$i_param_found);$i_param++)
						{
	                    $parameter=get_object_vars($parameter_a[$i_param]);
	                    $atrib[$j-1]=$parameter['attribute'];
	                    $value[$j-1]=$parameter['value'];
                        if (($atrib[$j-1]=='NAME')||($atrib[$j-1]=='FILENAME')) $i_param_found=true;
						}
	                }

	            if ($part['ifdparameters'] && !$i_param_found)
	                {
	                $parameter_a=$part['dparameters'];
                    for ($i_param=0;(($i_param<sizeof($parameter_a)) && !$i_param_found);$i_param++)
						{
	                    $parameter=get_object_vars($parameter_a[$i_param]);
	                    $atrib[$j-1]=$parameter['attribute'];
	                    $value[$j-1]=$parameter['value'];
                        if (($atrib[$j-1]=='NAME')||($atrib[$j-1]=='FILENAME')) $i_param_found=true;
						}
	                }

	            $bp=imap_fetchbody( $mbox, $i, $j, FT_PEEK);
	            $body[$j-1]=$bp;
	            }
	        }

	    unset($files);
	    $text='-'.$subj;

	    for ($j=0;$j<$count_parts;$j++)
	        {
	        if ($type[$j]==6) $type[$j]='VIDEO';
	        elseif ($type[$j]==5) $type[$j]='IMAGE';
	        elseif ($type[$j]==4) $type[$j]='AUDIO';
	        elseif ($type[$j]==0) $type[$j]='TEXT';
	        else $type[$j]='#'.$type[$j];

	        $value[$j]=strl($value[$j]);

#	        print "------------------------------\n";
#	        print "Тип:       ".$type[$j]."\n";
#	        print "Подтип:    ".$subtype[$j]."\n";
#	        print "Кодировка: ".$encoding[$j]."\n";
#	        print "Атрибут:   ".$atrib[$j]."\n";
#	        print "Значение:  ".$value[$j]."\n";
#	        print "Тело:\n".substr($body[$j],0,128)."\n";

	/**/
	        if (($type[$j]==0)&&(($subtype[$j]=='PLAIN')||($subtype[$j]=='HTML')))
	            {
	            $text.=$body[$j];
 				if ($encoding[$j]==3) $text.=base64_decode($body[$j]);
				}
	        else
	            {
#	            print "[ATR:".$atrib[$j]."]";
	            if (($atrib[$j]=='NAME')||($atrib[$j]=='FILENAME'))
	                {
#	                print "[FN:".$value[$j]."]";
	                $fname=explode('.',strtolower($value[$j]));
	                $name=$fname[0];
	                $ext=$fname[1];
	                # определяем правильность имени
	                $chekname=(strlen(trim(strtr(str_replace(' ','*',$name),'-_()0123456789abcdefghjiklmnopqrstuvwxyz','                                        ')))==0);
	                # если правильное имя
	                if ($chekname)
	                # если допустимое расширение
	                if (($ext=='jpg')||
	                    ($ext=='mid')||
	                    ($ext=='3gp')||
	                    ($ext=='mmf')||
	                    ($ext=='gif')||
	                    ($ext=='mp3')||
	                    ($ext=='wav')||
	                    ($ext=='png')||
	                    ($ext=='jar')||
	                    ($ext=='jad')||
	                    ($ext=='thm')||
	                    ($ext=='mpn')||
	                    ($ext=='amr')||
	                    ($ext=='sis')||
	                    ($ext=='scs')||
	                    ($ext=='col')||
	                    ($ext=='wbmp'))
	                    {
	                    if ($encoding[$j]==3)
	                        {
	                        $_file['name']=$name.'.'.$ext;
	                        $_file['body']=base64_decode($body[$j]);
	                        $_file['size']=strlen($_file['body']);
	                        # если размер файла меньше 256kb
	                        if ($_file['size']<=262144) $files[]=$_file;
	                        }
	                    }
	                }
	            }
	    	}

#	    print "\n-----";
#	    print "\n[TEXT:".$text."]";
#	    print "\n[FILES:".sizeof($files)."]";

	    if (strpos($text,'(key:'))
	        {
	        $code1=explode('(key:',$text);
	        $code2=explode(')',$code1[1]);
	        $_key=$code2[0];
#	        print "\n[CODE:$_key]";
	        $q=@mysql(DBName,"SELECT `domain` , `login` FROM `users` WHERE `key` = '".addslashes($_key)."' LIMIT 0, 1");
	        $data=@mysql_fetch_array($q);
	        $_user=$data['login'];
	        $_domain=$data['domain'];
#	        print "\n[USER:$_user - $_domain]";
	        if ($_user!='')
	            {
				$uploadsitespath='/home/kmx/www/sites/'.$_domain.'/'.$_user;
	            $_diskspace=@file($uploadsitespath.'/diskspace.dat');
	            $limit=$_diskspace[2];
	            for ($fi=0;$fi<sizeof($files);$fi++)
	                {
	                if ($files[$fi]['size']<=$limit)
	                    {
#				        print "\n[FN:".$files[$fi]['name']."]";
						if (!is_dir($uploadsitespath.'/_UPLOAD_')) {@mkdir($uploadsitespath.'/_UPLOAD_');@chmod($uploadsitespath.'/_UPLOAD_',0777);}
	                    $fp = @fopen ($uploadsitespath.'/_UPLOAD_/'.$files[$fi]['name'], "w+");
	                    @fwrite ($fp, $files[$fi]['body']);
	                    @fclose ($fp);
	                    $limit=$limit-$files[$fi]['size'];
	                    }
	                }
                $_diskspace[0]=$_diskspace[0]+0;
				$_diskspace[1]=dir_size($uploadsitespath);
				$_diskspace[2]=$_diskspace[0]-$_diskspace[1];

				$fp = fopen ($uploadsitespath.'/diskspace.dat', "w+");
                fwrite ($fp, $_diskspace[0]."\n".$_diskspace[1]."\n".$_diskspace[2]);
				fclose ($fp);
	            }
	        }
		imap_delete( $mbox, $i );   //Удаление письма
	    }
#	print "\n==[END]=======================\n";
	imap_expunge($mbox);
	imap_close($mbox);
	print_r(imap_errors());

    $upload_email[0]=time()."\n";
	$fp = fopen ('/home/kmx/www/sites/upload_email.dat', "w+");
	fwrite ($fp, implode("",$upload_email));
	fclose ($fp);
	}
#exit;}
?>