-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathreplace_concat_includes.php
97 lines (76 loc) · 1.91 KB
/
replace_concat_includes.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env php
<?php
/*
*
* Replaces define ("MYCONSTANT", "mydir/"); include (MYCONSTANT."myfile.php"); with include ("mydir/"."myfile.php");
* in all php files in downloaded_php/downloads
*
* TODO: This isn't working properly. Probably need to escape more characters passed to r-option. This is probably best done in
* the replace_include_constants plugin, however.
*/
include ("test/framework/lib/header.php");
function eh ($errno, $errstr)
{
if ($errno == E_NOTICE)
return;
print "$errno, $errstr";
}
set_error_handler ("eh", E_ALL);
$directory_name = "downloaded_php/downloads/";
$dirs = get_directories ($directory_name);
foreach ($dirs as $dir)
{
$defines = "";
$i = 0;
$files = get_all_files ($dir);
foreach ($files as $file)
{
if ($file != NULL)
{
$defines = $defines.get_defines ($file);
echo "Analysing $file\n";
}
}
foreach ($files as $file)
{
echo "Applying changes to $file\n";
apply_changes ($file, $defines);
}
}
function get_directories ($dirname)
{
$result = array ();
foreach (new DirectoryIterator($dirname) as $fileInfo)
{
if ($fileInfo->isDot())
continue;
$result[] = $fileInfo->getPathname ();
}
return $result;
}
function get_all_files ($dir)
{
$results = split ("\n", _exec ("find $dir/ -type f -name \"*.php\""));
array_pop ($results);
return $results;
}
function _exec ($command)
{
// 20 seconds, max
list ($output) = complete_exec ($command, NULL, 20);
return $output;
}
function get_defines ($file)
{
$command = "src/phc $file --run plugins/tools/get_defines.la 2>/dev/null";
return _exec ($command);
}
function apply_changes ($file, $defines)
{
$command = "src/phc $file --run plugins/tools/replace_include_constants.la --r-option='$defines' --dump=decomment 2>/dev/null";
$newcode = _exec ($command);
$fh = fopen ($file, "w");
fwrite ($fh, $newcode);
fclose ($fh);
}
?>