Index: trunk/obscure.php =================================================================== diff -u -r13 -r1253 --- trunk/obscure.php (.../obscure.php) (revision 13) +++ trunk/obscure.php (.../obscure.php) (revision 1253) @@ -1,18 +1,48 @@ #!/usr/local/bin/php + <?php - $functions = array(); + + if (isset($argv[2]) && $argv[2] != '') { + define('SEED', $argv[2]); + } + else { + 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 ( preg_match("/^[ ]*\/\/.*?$/", trim($line)) ) { + //echo "SKIPPING ".$php[$x]; + $php[$x] = ""; + continue; //cut comments + } + + if ( preg_match("/^\/\*.*\*\/$/", trim($line)) ) { + //echo "SKIPPING ".$php[$x]; + $php[$x] = ""; + continue; //cut comments + } + if(substr($line,0,10)=="function _") { $dec_parts = explode(" ",$line,2); $pp = explode("(",$dec_parts[1]); $name = $pp[0]; $attribs="(".$pp[1]; + + //echo "found func $name attribs: $attribs\n"; $start = $x; for($f=$x;$f<count($php);$f++) @@ -25,12 +55,13 @@ } if($start && $end && strlen($name)) { - $newname = "_".md5($n); + $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) @@ -78,8 +109,8 @@ $g = strpos($line,"global"); if(is_numeric($g)) { - $vars = substr($line,$g+7); - $vars = substr($vars,0,-2); + $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]); @@ -99,11 +130,22 @@ $VarCount =0; if(strlen($Attribs)>3) { - $a = explode(",",substr($Attribs,1,-2)); - if (is_array($a)) { + $Attribs = trim($Attribs); + $Attribs = str_replace("\t","",$Attribs); + //echo "getting attribs from $Attribs\n"; + $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] = '$_'.md5($VarCount++); + list($attr,$default) = explode('=', $attr); + //echo "attr: $attr / def = $default\n"; + if ($default != '') { + $defaults[$attr] = $default; + //echo "stored defaults for $attr\n"; + } + $variables[$attr] = '$_'.gen_name($VarCount++); } } } @@ -123,7 +165,7 @@ { $name = "$".trim($name); if(!in_array($name,$globals) && !array_key_exists($name,$variables)) - $variables[$name] = '$_'.md5($VarCount++); + $variables[$name] = '$_'.gen_name($VarCount++); } } $p = strpos($line,"$",$p+1); @@ -132,8 +174,10 @@ 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)) { @@ -151,21 +195,30 @@ if (is_array($a)) { foreach($a as $attr) { - $av[] = $variables[$attr]; + list($attr,$default) = explode('=', $attr); + $av[] = ($variables[$attr].(isset($defaults[$attr]) ? '='.$defaults[$attr] : '')); } } if(count($av)>0) $o .= implode(",",$av); $o .= ")"; + //echo "reversed: $o\n"; $o .= implode(" ",$code); //$o = str_replace("\n","",$o); return $o; } $out = ""; $outline = 0; - foreach($functions as $name =>$pos) - { + + $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) @@ -206,16 +259,16 @@ for($x=0;$x<count($php);$x++) { if($x==$outline) { - //echo $out; + //echo "$line: ".$out; fwrite($tmp_file, $out); } if(strlen($php[$x])) { - //echo ltrim($php[$x]); + //echo "$line: ".ltrim($php[$x]); fwrite($tmp_file, ltrim($php[$x])); } $line++; } fclose($tmp_file); rename($argv[1].'_', $argv[1]); -?> \ No newline at end of file +?>