-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathphp2ast.php
52 lines (44 loc) · 1.16 KB
/
php2ast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use PhpParser\Error;
use PhpParser\ParserFactory;
function utf8ize($d)
{
if (is_array($d)) {
foreach ($d as $k => $v) {
unset($d[$k]);
$d[utf8ize($k)] = utf8ize($v);
}
} else if (is_object($d)) {
$objVars = get_object_vars($d);
foreach ($objVars as $key => $value) {
$d->$key = utf8ize($value);
}
} else if (is_string($d)) {
return iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($d));
}
return $d;
}
function php2ast()
{
global $argv;
$fname = $argv[1];
if (!is_readable($fname)) {
echo "[-] Error reading file {$fname}.\n";
exit(1);
}
$code = file_get_contents($fname);
if ($code === FALSE) {
echo "[-] Error reading file {$fname}\n";
exit(2);
}
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse($code);
echo json_encode(utf8ize($ast));
} catch (Error $error) {
echo "[-] Error parsing {$fname}: {$error->getMessage()}\n";
exit(3);
}
}
php2ast();