-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_flag.php
91 lines (72 loc) · 2.19 KB
/
debug_flag.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
<?php
require __DIR__."/src/build/helpers.php";
function usage() { echo "Usage: php {$argv[0]} [on|off] [code]\n"; }
if (!isset($argv[1])) {
usage();
exit(1);
}
const TARGET_DIR = [
__DIR__."/src/classes",
__DIR__."/daemon"
];
$argv[1] = strtolower(trim($argv[1]));
$code = isset($argv[2]) ? preg_quote(trim($argv[2])) : null;
if ($argv[1] === "off") {
if (is_string($code)) {
$rxp = "/\/\*\s*debug\s*:\s*({$code})\s*\*\/(.*?)\/\*\s*end_debug\s*\*\//Ssi";
} else {
$rxp = "/\/\*\s*debug\s*:\s*([\w\d]+)\s*\*\/(.*?)\/\*\s*end_debug\s*\*\//Ssi";
}
$callback = function (string $dir, string $file) use ($rxp) {
$ext = explode(".", $file);
if (end($ext) !== "php") {
return;
}
$targetFile = "{$dir}/{$file}";
$content = file_get_contents($targetFile);
$r1 = $r2 = [];
if (preg_match_all($rxp, $content, $m)) {
echo "Turning off ".count($m[0])." debug code in {$targetFile}...";
foreach ($m[0] as $k => $v) {
$comp = base64_encode(gzencode($m[2][$k], 9));
$r1[] = $v;
$r2[] = "/*__debug_code({$m[1][$k]}):b64:gz:{$comp}*/";
}
file_put_contents($targetFile, str_replace($r1, $r2, $content));
echo "OK!\n";
}
};
} else
if ($argv[1] === "on") {
if (is_string($code)) {
$rxp = "/\/\*__debug_code\(({$code})\):b64:gz:(.*?)\*\//Ssi";
} else {
$rxp = "/\/\*__debug_code\(([\w\d]+)\):b64:gz:(.*?)\*\//Ssi";
}
$callback = function (string $dir, string $file) use ($rxp) {
$ext = explode(".", $file);
if (end($ext) !== "php") {
return;
}
$targetFile = "{$dir}/{$file}";
$content = file_get_contents($targetFile);
$r1 = $r2 = [];
if (preg_match_all($rxp, $content, $m)) {
echo "Turning on ".count($m[0])." debug code in {$targetFile}...";
foreach ($m[0] as $k => $v) {
$code = gzdecode(base64_decode($m[2][$k]));
$r1[] = $v;
$r2[] = "/* debug:{$m[1][$k]} */{$code}/* end_debug */";
}
file_put_contents($targetFile, str_replace($r1, $r2, $content));
echo "OK!\n";
}
};
} else {
usage();
exit(1);
}
foreach (TARGET_DIR as $dir) {
recursiveCallbackScanDir($dir, $callback);
}
echo "Finished!\n";