1   #!/usr/local/bin/php
  2   <?php
  3     $functions = array();
  4    
  5     $php = file($argv[1]);
  6     $n = 1;
  7     for($x=0;$x<count($php);$x++)
  8     {
  9           $line = $php[$x];
  10           if(substr($line,0,10)=="function _")
  11           {
  12                   $dec_parts = explode(" ",$line,2);
  13                   $pp = explode("(",$dec_parts[1]);
  14                   $name = $pp[0];
  15                   $attribs="(".$pp[1];
  16                           
  17                   $start = $x;
  18                   for($f=$x;$f<count($php);$f++)
  19                   {
  20                           if(substr($php[$f],0,1)=="}")
  21                           {
  22                             $end = $f;    
  23                             break;
  24                           }
  25                   }
  26                   if($start && $end && strlen($name))
  27                   {
  28                           $newname = "_".md5($n);
  29                           $n++;
  30                           $functions[$name] = array("start"=>$start,"end"=>$end,"attribs"=>$attribs,"newname"=>$newname);
  31                   }
  32           }
  33     }
  34     //echo "<PRE>"; print_r($functions); echo "</PRE>";
  35  
  36     function GetVarName($s)
  37     {
  38             $alphabet = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  39            
  40             $name = "";
  41             $var_end = 0;
  42             $char = substr($s,$var_end,1);                
  43             if(substr($s,0,1)=="$")
  44             {
  45                   $var_end++;
  46                   $char = substr($s,$var_end,1);                  
  47             }                      
  48             while(is_numeric(strpos($alphabet,$char)) && strlen($char))
  49             {
  50                   $name .= $char;
  51                   $var_end++;
  52                   $char = substr($s,$var_end,1);                  
  53             }
  54           return $name;
  55     }
  56    
  57     function obscure_func($NewName,$Attribs,$code)
  58     {     
  59           global $functions;
  60                   
  61           $globals = array();
  62           
  63           $globals[] = '$this';
  64           $globals[] = '$_GET';
  65           $globals[] = '$_FILES';
  66           $globals[] = '$_POST';
  67           $globals[] = '$_COOKIE';
  68           $globals[] = '$_SERVER';
  69           
  70           $variables = array();
  71  
  72                   $new_code = array();
  73           for($x=0;$x<=count($code);$x++)
  74           {
  75                   $line = $code[$x];
  76                   $line = ltrim($line);
  77                   $line = str_replace("\t","",$line);
  78                   $g = strpos($line,"global");
  79                   if(is_numeric($g))
  80                   {
  81                           $vars = substr($line,$g+7);
  82                           $vars = substr($vars,0,-2);
  83                           $v = explode(",",$vars);
  84                           for($z=0;$z<count($v);$z++)
  85                             $globals[] = ltrim($v[$z]);
  86                   }               
  87                   $new_code[$x] = $line;
  88           }
  89           $code = $new_code;
  90           for($x=0;$x<count($code);$x++)
  91           {               
  92                   foreach($functions as $name=>$attr)
  93                   {
  94                           $line = $code[$x];
  95                           $code[$x] = str_replace($name,$attr["newname"],$line);
  96                   }
  97           }
  98  
  99           $VarCount =0;
  100           if(strlen($Attribs)>3)
  101           {
  102             $a = explode(",",substr($Attribs,1,-2));
  103             if (is_array($a)) {
  104                     foreach($a as $attr)
  105                   {
  106                                   $variables[$attr] = '$_'.md5($VarCount++);
  107                     }
  108                   }
  109           }
  110           
  111           for($x=0;$x<count($code);$x++)
  112           {               
  113                   $line = $code[$x];
  114                   if(!strlen($line))
  115                     continue;
  116                   $p = strpos($line,"$");                 
  117                   while($p>0)
  118                   {
  119                           if(substr($line,$p,2)!="$$")
  120                           {
  121                             $name=GetVarName(substr($line,$p));
  122                             if(strlen($name))
  123                             {
  124                                   $name = "$".trim($name);
  125                                   if(!in_array($name,$globals) && !array_key_exists($name,$variables))
  126                                   $variables[$name] = '$_'.md5($VarCount++);
  127                             }
  128                           }
  129                           $p = strpos($line,"$",$p+1);            
  130                   }               
  131           } 
  132           
  133           for($x=0;$x<count($code);$x++)
  134           { 
  135                   foreach($variables as $v=>$varname)
  136                   {                       
  137                           $p = strpos($code[$x],$v);
  138                           while(is_numeric($p))
  139                           {
  140                             $t = GetVarName(substr($code[$x],$p));
  141                             if('$'.$t == $v)
  142                             {     
  143                               $code[$x] = substr_replace($code[$x],$varname,$p,strlen($t)+1);
  144                             }
  145                             $p = strpos($code[$x],$v,$p+1);
  146                           }
  147                   }       
  148           }
  149           
  150           $o = "function $NewName"."(";
  151           if (is_array($a)) {
  152                   foreach($a as $attr)
  153                   {
  154                       $av[] = $variables[$attr];
  155                   }
  156             }
  157           if(count($av)>0)
  158             $o .= implode(",",$av);
  159           $o .= ")";
  160           $o .= implode(" ",$code);
  161           //$o = str_replace("\n","",$o);
  162           return $o;
  163     }
  164    
  165     $out = "";
  166     $outline = 0;
  167     foreach($functions as $name =>$pos)
  168     {
  169            $dest = $pos["start"];
  170            $newname = $pos["newname"];
  171            if(!$outline)
  172              $outline = $dest;
  173           
  174  
  175            unset($code);
  176            for($x=$dest+1;$x<=$pos["end"];$x++)
  177            {
  178                   $code[] = $php[$x];
  179            }
  180            $newcode = obscure_func($newname,$pos["attribs"],$code);
  181            $out .= $newcode;
  182     }
  183     foreach($functions as $name=>$pos)
  184     {
  185           for($x=$pos["start"];$x<=$pos["end"];$x++)
  186           {
  187                   $php[$x] = "";                  
  188           }
  189     }
  190    
  191     $code =array();
  192     for($x=0;$x<count($php);$x++) {
  193           $line = $php[$x];
  194             foreach($functions as $name=>$attr)
  195             {
  196                   $line = str_replace($name,$attr["newname"],$line);
  197             }
  198             $code[$x] = $line;
  199           }
  200           $php = $code;
  201    
  202     $line=1;
  203    
  204     $tmp_file = fopen($argv[1].'_', 'w');
  205    
  206     for($x=0;$x<count($php);$x++)
  207     {     
  208           if($x==$outline) {
  209             //echo $out;
  210             fwrite($tmp_file, $out);
  211           }
  212           if(strlen($php[$x])) {
  213                   //echo ltrim($php[$x]);
  214                   fwrite($tmp_file, ltrim($php[$x]));
  215           }
  216           $line++;
  217     }
  218     fclose($tmp_file);
  219     rename($argv[1].'_', $argv[1]);
  220           
  221   ?>