Archive for October, 2008

Getting the variables out of a PHP document

Sunday, October 26th, 2008

Sometimes you need to find the variables from a php file. I needed it to reverse engineer a script. I was checking which variables and objects are passed to the script. So I wrote my own parser for it. It’ll find all the variables from the document. It’ll only work for php.

Here is the code.

<?php
$con = file_get_contents("php://stdin");
$reg = '[a-zA-Z_][a-zA-Z_0-9]*';
$reg_v = "\\\${$reg}";
$reg_s = "$reg_v(?!\-\>)";
preg_match_all("/$reg_s/i",$con,$m);
$reg_s = "$reg_v\->$reg";
preg_match_all("/$reg_s/i",$con,$m1);
//$reg_a = '\[([\'"])([^\'"]+)\1\]';
//preg_match_all("/$reg_v$reg_a/i",$con,$m2);
//preg_match_all("/$reg_s$reg_a/i",$con,$m3);
foreach(array_unique(array_merge($m[0],$m1[0],$m2[0],$m3[0])) as $v):
echo $v.PHP_EOL;
endforeach;
?>

Save this as a file like vars.php. Then issue the following code for your subject file (subject.php).


# php /path/to/vars.php < /path/to/subject.php
$desc
$key
$value
$col
$__tags
$hex
$rand_size
$tags_n
$tpl->showPlayer
$tpl->playerHeight
$tpl->playerWidth
$tpl->viedoId

That was the output for me.

Hope it helps.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to StumbleUpon