|
<?php |
|
|
|
if (!file_exists(__DIR__ . '/traces/out/')) { |
|
mkdir(__DIR__ . '/traces/out/'); |
|
} |
|
|
|
$dir = __DIR__ . '/' . ($argv[1] ?? './traces'); |
|
if (is_dir($dir)) { |
|
$files = scandir($dir); |
|
foreach ($files as $file) { |
|
if (str_contains($file, ".xt")) { |
|
scan($dir . '/' . $file); |
|
} |
|
} |
|
} |
|
|
|
function scan(string $filename) |
|
{ |
|
$loader = load($filename); |
|
|
|
if (count($loader) === 0) { |
|
return; |
|
} |
|
|
|
if (!file_exists(__DIR__ . '/traces/out/files.txt')) { |
|
touch(__DIR__ . '/traces/out/files.txt'); |
|
} |
|
if (!file_exists(__DIR__ . '/traces/out/includes.txt')) { |
|
touch(__DIR__ . '/traces/out/includes.txt'); |
|
} |
|
if (!file_exists(__DIR__ . '/traces/out/functions.txt')) { |
|
touch(__DIR__ . '/traces/out/functions.txt'); |
|
} |
|
|
|
$files_raw = explode("\n", file_get_contents(__DIR__ . '/traces/out/files.txt')); |
|
$files = array_column($loader, 'file'); |
|
$files = array_unique($files + $files_raw); |
|
|
|
$includes_raw = explode("\n", file_get_contents(__DIR__ . '/traces/out/includes.txt')); |
|
$includes = array_filter(array_column($loader, 'include_file')); |
|
$includes = array_unique($includes + $includes_raw); |
|
|
|
$functions_raw = explode("\n", file_get_contents(__DIR__ . '/traces/out/functions.txt')); |
|
$functions = array_filter(array_column($loader, 'fn')); |
|
$functions = array_unique($functions + $functions_raw); |
|
|
|
file_put_contents(__DIR__ . '/traces/out/files.txt', implode("\n", $files)); |
|
file_put_contents(__DIR__ . '/traces/out/includes.txt', implode("\n", $includes)); |
|
file_put_contents(__DIR__ . '/traces/out/functions.txt', implode("\n", $functions)); |
|
|
|
|
|
$origCsv = csv_to_array(__DIR__ . '/traces/out/output.csv'); |
|
$newCsvData = (is_array($origCsv) ? $origCsv : []) + (array) $loader; |
|
$newCsvData = array_unique(array_values($newCsvData), SORT_REGULAR); |
|
|
|
$fp = fopen(__DIR__ . '/traces/out/output.csv', 'w', FILE_APPEND); |
|
fputcsv($fp, array_keys($loader[0])); |
|
foreach ($newCsvData as $fields) { |
|
fputcsv($fp, $fields); |
|
} |
|
|
|
fclose($fp); |
|
|
|
unlink($filename); |
|
} |
|
|
|
|
|
function csv_to_array($filename = '', $delimiter = ',') |
|
{ |
|
if (!file_exists($filename) || !is_readable($filename)) |
|
return false; |
|
|
|
$header = null; |
|
$data = array(); |
|
if (($handle = fopen($filename, 'r')) !== false) { |
|
while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) { |
|
if (!$header) |
|
$header = $row; |
|
else |
|
$data[] = array_combine($header, $row); |
|
} |
|
fclose($handle); |
|
} |
|
return $data; |
|
} |
|
|
|
function load(string $filename) |
|
{ |
|
$file = new \SplFileObject($filename); |
|
$frames = []; |
|
|
|
while (!$file->eof()) { |
|
$line = $file->fgetcsv("\t"); |
|
|
|
if (!$line) { |
|
break; |
|
} |
|
|
|
if (count($line) >= 11) { |
|
if ($line[5]) { |
|
$b = get_defined_functions(); |
|
if (in_array($line[5], $b['internal'])) { |
|
continue; |
|
} |
|
} |
|
$frames[] = [ |
|
"fn" => $line[5], |
|
"include_file" => $line[7], |
|
"file" => $line[8], |
|
"line" => $line[9], |
|
]; |
|
} |
|
} |
|
|
|
return $frames; |
|
} |