Codice PHP:
<?php
//core dello script "Php Code Sweeper" - Lista funzioni
$sign=array(' ','+','-','*','/','{','}','(',')',':',';','?','>','<','=','!','==','!=','.=','<>','>=','<=','||','&&','.',',','[',']');
$tagst=array("<?","<%","//","#","/*","'","\"","<<<EOT","<<<EOD");
$tagfin=array("?>","%>","\n","\n","*/","'","\"","EOT;","EOD;");
function FindSlash(&$pstr,$pstart,&$sls)
{
$sst=substr($pstr,$pstart-1,1);
if($pstart-1>=0&&$sst==='\\')
{
$sls++;
FindSlash($pstr,$pstart-1,$sls);
}
return;}
function ParseStrEnd(&$pstr,$delim,$p0,&$p01)
{
$p01=false;
$ifslash=false;
$plook=$p0+1;
$go_on=false;
do
{
$p01=strpos($pstr,$delim,$plook);
if($p01!==false)
{
$sls=0;
FindSlash($pstr,$p01,$sls);
$ifslash=$sls>0&&$sls%2>0;
$plook=$p01+1;
$go_on=$ifslash&&$plook<strlen($pstr);
}
else
{
$go_on=false;
}
}
while($go_on);
return $p01===false?'':substr($pstr,$p0,$p01-$p0+1);
}
function GetTagPosEx(&$str,$pstart,&$tags,&$ind,&$spos)
{
$len=strlen($str);
$mn=$len;
$ind=0;
foreach($tags as $k=>$v)
{
$pos=strpos($str,$v,$pstart);
$pos=$pos===false?$len:$pos;
if($pos<$mn){$mn=$pos;$ind=$k;
}
}
$spos=$mn==$len?false:$mn;
if($spos!==false&&($tags[$ind]=='"'||$tags[$ind]=="'"))
{FindSlash($str,$spos,$sls);
$rem=$sls%2;
if($rem)
{
GetTagPosEx($str,$spos+1,$tags,$ind,$spos);
}
}
}
function GetTagPos(&$str,$pstart,&$tags,&$ind)
{
$len=strlen($str);
$mn=$len;
$ind=0;
foreach($tags as $k=>$v)
{
$pos=strpos($str,$v,$pstart);
$pos=$pos===false?$len:$pos;
if($pos<$mn)
{
$mn=$pos;
$ind=$k;
}
}
return $mn==$len?false:$mn;
}
$symt=array("<?","?>","<%","%>","//","#","\n","/*","*/","'","\"","<<<EOT","EOT;","<<<EOD","EOD;");
$states=array(array(0=>32,512=>544,256=>288),array(32=>0,544=>512,288=>256,40=>0,552=>512,296=>256),array(0=>16,512=>528,256=>272),array(16=>0,528=>512,272=>256,24=>0,536=>512,280=>256),array(32=>40,544=>552,288=>296,16=>24,528=>536,272=>280),array(32=>40,544=>552,288=>296,16=>24,528=>536,272=>280),array(40=>32,552=>544,296=>288,24=>16,536=>528,280=>272),array(32=>36,544=>548,288=>292,16=>20,528=>532,272=>276),array(36=>32,548=>544,292=>288,20=>16,532=>528,276=>272),array(0=>512,512=>0,32=>160,160=>32,544=>672,672=>544,288=>416,416=>288,16=>144,144=>16,528=>656,656=>528,272=>400,400=>272),array(0=>256,256=>0,32=>96,96=>32,544=>608,608=>544,288=>352,352=>288,16=>80,80=>16,528=>592,592=>528,272=>336,336=>272),array(32=>34,544=>546,288=>290,16=>18,528=>530,272=>274),array(34=>32,546=>544,290=>288,18=>16,530=>528,274=>272),array(32=>33,544=>545,288=>289,16=>17,528=>529,272=>273),array(33=>32,545=>544,289=>288,17=>16,529=>528,273=>272));
function FindPhpTags(&$str,&$pss)
{
global $states,$symt;
$pst=0;
$state=0;
$ltag=-1;
do
{
GetTagPosEx($str,$pst,$symt,$ind,$spos);
if($spos===false)return;
if(in_array($state,array_keys($states[$ind])))
{
$state=$states[$ind][$state];
$lind=$ltag<0&&($ind==0||$ind==2)?0:-1;
$lind=$ltag==$ind?1:$lind;
if($lind>=0)
{
$pss=empty($pss)?array():$pss;
$ic=$lind==0?count($pss):count($pss)-1;
$pss[$ic][$lind]=$lind==0?$spos:$spos-$pss[$ic][0]+strlen($symt[$ind]);
$ltag=$ind%2?-1:$ind+1;}}$pst=$spos+strlen($symt[$ind]);
}
while($pst<strlen($str)-1);
}
function PackScript(&$outstr,&$pstart,&$errcode,&$remtagst,&$remtagfin,&$phptags,&$hides)
{
$p0=GetTagPos($outstr,$pstart,$remtagst,$typ);
if($p0===false)
{
$errcode=true;
return;
}
$lind=strlen($remtagfin[$typ]);
$lst=strlen($remtagst[$typ]);
$p01=strpos($outstr,$remtagfin[$typ],$p0+$lst);
if($p01===false&&$typ>1)
{
$errcode=$typ+2;
return;
}
$ins='';switch($typ)
{
case 0:case 1:$p01=GetTagPos($outstr,$p0+$lst,$phptags,$t1);
if($p01===false)
{
$errcode=$t1+2;
return;
}
$lind=$t1==$typ?$lind:0;
break;
case 2:break;
case 3:case 4:$stri=' ';
$stri=ParseStrEnd($outstr,$remtagfin[$typ],$p0,$p01);
if($stri=='')
{
$errcode=$typ+2;
return;
}
$ind=count($hides);
$hides[$ind]=$stri;
$ins='@s'.$ind.'@';
break;
case 5:case 6:$ind=count($hides);
$hides[$ind]=substr($outstr,$p0,$p01-$p0+$lind)."\n";
$ins='@s'.$ind.'@';
break;
default:$errcode=100;return;
}
$tail=$p01+$lind<strlen($outstr)?substr($outstr,$p01+$lind):'';
$outstr=substr($outstr,0,$p0).$ins;
$pstart=strlen($outstr);$outstr.=$tail;
if($pstart>=strlen($outstr))
{
$errcode=true;
return;
}
PackScript($outstr,$pstart,$errcode,$remtagst,$remtagfin,$phptags,$hides);
}
function replace(&$str,$params)
{
foreach($params as $k=>$v)
{
while(strpos($str,$k)!==FALSE)
{
$str=str_replace($k,$v,$str);
}
}
}
function DelSlashSym(&$output)
{
$spsymb=array("\n","\r","\t","\f");
foreach($spsymb as $v)
{
$output=str_replace($v,' ',$output);
}
}
function delspace(&$input)
{
global $sign;
$replacements=array();
for($i=0;$i<count($sign);$i++)
{
$replacements[$sign[$i].' ']=$sign[$i];
$replacements[' '.$sign[$i]]=$sign[$i];
}
DelSlashSym($input);replace($input,$replacements);
}
function RestoreStrings(&$output,&$strs)
{
if(empty($strs))return;
foreach($strs as $k=>$v)
{
$output=str_replace('@s'.$k.'@',$v,$output);
}
}
function PackPhp(&$outstr)
{
global $tagst,$tagfin;
$remtagst=array_slice($tagst,2);
$remtagfin=array_slice($tagfin,2);
$phptags=array_slice($tagfin,0,3);
$pstart=0;PackScript($outstr,$pstart,$errcode,$remtagst,$remtagfin,$phptags,$hides);
if($errcode!==true)return false;
delspace($outstr);
RestoreStrings($outstr,$hides);
eregi('(<\?[Pp][Hh][Pp])',$outstr,$rz);
if(!empty($rz[0]))
{
$outstr=preg_replace('(<\?[Pp][Hh][Pp])','<?php ',$outstr,1);
}
return true;
}
function PackFile(&$f,&$output)
{
$output=implode('',$f);FindPhpTags($output,$poss);
if(empty($poss))return;
$rez='';
$prevpos=0;
foreach($poss as $v)
{
if($v[0]-$prevpos>0)
{
$rez.=substr($output,$prevpos,$v[0]-$prevpos);
}
$prevpos=$v[0]+$v[1];
$php_seg=substr($output,$v[0],$v[1]);
if(!PackPhp($php_seg))return false;
$rez.=$php_seg;
}
if($prevpos<strlen($output))
{
$rez.=substr($output,$prevpos);
}
$output=$rez;
return true;
}
//inizio ciclo ricerca file php nella cartella data
if(isset($opt_file))
{
$path_parts = pathinfo("$input_file");
$dirname = $path_parts["dirname"];
echo "<b>Directory di lavoro: ".$dirname." Inizio ottimizzazione file...</b>";
chdir($dirname);
if($handle=opendir($dirname))
{
$k=0;
while(false!==($file=readdir($handle)))
{
$path_parts = pathinfo($file);
$estensione = $path_parts["extension"];
if($file!="."&&$file!=".."&&$estensione == "php"&&$file != "optimizer.php")
{
$php_found_files = $php_found_files + 1;
$input_file = $file;
$output_file = $file;
$backup_file = $file.".old";
//main dell'ottimizzazione dei file
@set_time_limit(600);
@set_magic_quotes_runtime(0);
copy($input_file,$backup_file);
echo "<br>Ottimizzazione sul file ".$input_file." completato.<br>";
echo "<b> Grandezza Iniziale: ".filesize($input_file)." bytes</b><br>";
$f=file($input_file);
$output='';
PackFile($f,$output);
$FILE_OUT=fopen($output_file,'w');
$e=fputs($FILE_OUT,$output)?FALSE:TRUE;
fclose($FILE_OUT);
clearstatcache();
echo "<b> Grandezza finale: ".filesize($output_file)." bytes<br></b>";
@set_magic_quotes_runtime(get_magic_quotes_gpc());
@set_time_limit(30);
}
}
closedir($handle);
if ($php_found_files == 0)
{
echo "<br><br><b>Non è stato trovato nessun file php da ottimizzare...<br>";
echo "Seleziona una cartella diversa da quella che hai appena usato!</b><br>";
}
}
}
?>
<FORM METHOD="post" NAME="form1" action="" ONSUBMIT="return valida();">
Seleziona un file della cartella da ottimizzare...<br>
<input type="file" name="input_file"><br>
<input type="submit" name="opt_file" value="Ottimizza">
</FORM>
<script language="JavaScript">
<!--
function valida()
{
if (document.forms.form1.input_file.value == '')
{
alert("Seleziona la cartella di origine");
return false;
}
}
//-->
</script>