#!/usr/local/bin/php

<?php

	define('SEED', rand(0, 100000));

	function gen_name($old)
	{
		return md5($old.SEED);
		//return '_n_'.$old;
	}

	$functions = array();
  
  $php = file($argv[1]);
  $n = 1;
  for($x=0;$x<count($php);$x++)
  {
  	$line = $php[$x];
  	if(substr($line,0,10)=="function _")
  	{
  		$dec_parts = explode(" ",$line,2);
  		$pp = explode("(",$dec_parts[1]);
  		$name = $pp[0];
  		$attribs="(".$pp[1];
  	  		
  		$start = $x;
  		for($f=$x;$f<count($php);$f++)
  		{
  			if(substr($php[$f],0,1)=="}")
  			{
  			  $end = $f;	
  			  break;
  			}
  		}
  		if($start && $end && strlen($name))
  		{
  			$newname = "_".gen_name($n);
  			$n++;
  			$functions[$name] = array("start"=>$start,"end"=>$end,"attribs"=>$attribs,"newname"=>$newname);
  		}
  	}
  }
//  print_r($functions);
  //echo "<PRE>"; print_r($functions); echo "</PRE>";

  function GetVarName($s)
  {
  	  $alphabet = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  	  
	  $name = "";
  	  $var_end = 0;
  	  $char = substr($s,$var_end,1);   		
  	  if(substr($s,0,1)=="$")
  	  {
  	  	$var_end++;
		$char = substr($s,$var_end,1);   		
  	  }  	  	 	  
  	  while(is_numeric(strpos($alphabet,$char)) && strlen($char))
  	  {
  		$name .= $char;
  		$var_end++;
		$char = substr($s,$var_end,1); 			
  	  }
  	return $name;
  }
  
  function obscure_func($NewName,$Attribs,$code)
  { 	
  	global $functions;
  	  	
  	$globals = array();
  	
  	$globals[] = '$this';
  	$globals[] = '$_GET';
  	$globals[] = '$_FILES';
  	$globals[] = '$_POST';
  	$globals[] = '$_COOKIE';
  	$globals[] = '$_SERVER';
  	
  	$variables = array();

		$new_code = array();
  	for($x=0;$x<=count($code);$x++)
  	{
  		$line = $code[$x];
  		$line = ltrim($line);
  		$line = str_replace("\t","",$line);
  		$g = strpos($line,"global");
  		if(is_numeric($g))
  		{
  			$vars = trim(substr($line,$g+7));
  			$vars = substr($vars,0,-1);
  			$v = explode(",",$vars);
  			for($z=0;$z<count($v);$z++)
  			  $globals[] = ltrim($v[$z]);
  		}  		
  		$new_code[$x] = $line;
  	}
  	$code = $new_code;
  	for($x=0;$x<count($code);$x++)
  	{  		
  		foreach($functions as $name=>$attr)
  		{
  			$line = $code[$x];
  			$code[$x] = str_replace($name,$attr["newname"],$line);
  		}
  	}

  	$VarCount =0;
  	if(strlen($Attribs)>3)
  	{
  		$Attribs = trim($Attribs);
  		$Attribs = str_replace("\t","",$Attribs);
  	  $a = explode(",",substr($Attribs,1,-1));
//  	  echo "got attribs for func [$Attribs]:\n";
//  	  var_dump($a);
  	  if (is_array($a) && $a[0] != '') {
	  	  foreach($a as $attr)
	    	{
		  		$variables[$attr] = '$_'.gen_name($VarCount++);
	  	  }
	  	}
  	}
  	
  	for($x=0;$x<count($code);$x++)
  	{  		
  		$line = $code[$x];
  		if(!strlen($line))
  		  continue;
  		$p = strpos($line,"$");  		
  		while($p>0)
  		{
  			if(substr($line,$p,2)!="$$")
  			{
  			  $name=GetVarName(substr($line,$p));
  			  if(strlen($name))
  		 	  {
  		 	  	$name = "$".trim($name);
  		 	  	if(!in_array($name,$globals) && !array_key_exists($name,$variables))
  			    	$variables[$name] = '$_'.gen_name($VarCount++);
  			  }
  			}
  			$p = strpos($line,"$",$p+1);  		
  		}  		
  	}  
  	
  	for($x=0;$x<count($code);$x++)
  	{  
//  		print_r($variables);
  		foreach($variables as $v=>$varname)
  		{  			
  			//echo "strpos ".$code[$x].', '.$v."\n";
  			$p = strpos($code[$x],$v);
  			while(is_numeric($p))
  			{
  			  $t = GetVarName(substr($code[$x],$p));
  			  if('$'.$t == $v)
  			  {	
			    $code[$x] = substr_replace($code[$x],$varname,$p,strlen($t)+1);
  			  }
  			  $p = strpos($code[$x],$v,$p+1);
  			}
  		}  	
  	}
  	
  	$o = "function $NewName"."(";
  	if (is_array($a)) {
	  	foreach($a as $attr)
	  	{
	  	    $av[] = $variables[$attr];
	  	}
	  }
  	if(count($av)>0)
  	  $o .= implode(",",$av);
  	$o .= ")";
  	$o .= implode(" ",$code);
  	//$o = str_replace("\n","",$o);
  	return $o;
  }
  
  $out = "";
  $outline = 0;
  
  $shuffled = array_rand($functions, count($functions));
//  print_r($shuffled);

  foreach ($shuffled as $name) {
  	$pos = $functions[$name];
   
  //foreach($functions as $name =>$pos)
  //{
  	 $dest = $pos["start"];
  	 $newname = $pos["newname"];
  	 if(!$outline)
  	   $outline = $dest;
  	 

  	 unset($code);
  	 for($x=$dest+1;$x<=$pos["end"];$x++)
  	 {
  	 	$code[] = $php[$x];
  	 }
  	 $newcode = obscure_func($newname,$pos["attribs"],$code);
  	 $out .= $newcode;
  }
  foreach($functions as $name=>$pos)
  {
  	for($x=$pos["start"];$x<=$pos["end"];$x++)
  	{
  		$php[$x] = "";  		
  	}
  }
  
  $code =array();
  for($x=0;$x<count($php);$x++) {
  	$line = $php[$x];
	  foreach($functions as $name=>$attr)
	  {
	  	$line = str_replace($name,$attr["newname"],$line);
	  }
	  $code[$x] = $line;
	}
	$php = $code;
  
  $line=1;
  
  $tmp_file = fopen($argv[1].'_', 'w');
  
  for($x=0;$x<count($php);$x++)
  {  	
  	if($x==$outline) {
  	  //echo $out;
  	  fwrite($tmp_file, $out);
  	}
  	if(strlen($php[$x])) {
  		//echo ltrim($php[$x]);
  		fwrite($tmp_file, ltrim($php[$x]));
  	}
  	$line++;
  }
  fclose($tmp_file);
  rename($argv[1].'_', $argv[1]);
  	
?>