Created
January 18, 2024 08:27
-
-
Save divinity76/b58afe97492c6a290e0d88fed0e92316 to your computer and use it in GitHub Desktop.
php advanced code inserter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
$dir = realpath(__DIR__ . '/..'); | |
$files = shell_exec("find " . escapeshellarg($dir) . " -print0"); | |
$files = explode("\x00", rtrim($files, "\x00")); | |
$files = array_filter($files, function ($filepath) { | |
if(!preg_match("/page/", $filepath)) { | |
return false; | |
} | |
if(!preg_match("/\.php$/", $filepath)) { | |
return false; | |
} | |
return true; | |
}); | |
var_dump($files); | |
$insertCode = ' | |
get_headers((defined("DOCKERDOMAIN") ? DOCKERDOMAIN : "http://localhost") . "/sa/logger?" . http_build_query(array("category" => "pages_file_in_use", "message" => __FILE__ . " - {$_SERVER[\'HTTP_HOST\']}{$_SERVER[\'REQUEST_URI\']} ",)), false, stream_context_create(array("ssl" => array("verify_peer" => false, "verify_peer_name" => false, "allow_self_signed" => true, "security_level" => 0)))); | |
'; | |
foreach ($files as $file) { | |
$raw = file_get_contents($file); | |
preg_match("/namespace\s+[\s\S]*?(\;)/", $raw, $matches, PREG_OFFSET_CAPTURE); | |
$pos = $matches[1][1] ?? null; | |
if (empty($pos)) { | |
preg_match("/declare\s*\(strict_types[\s\S]*?(\;)/", $raw, $matches, PREG_OFFSET_CAPTURE); | |
$pos = $matches[1][1] ?? null; | |
} | |
if (empty($pos)) { | |
$code = "<?php\n" . $insertCode . "\n?>" . $raw; | |
} else { | |
$pos = filter_var($pos, FILTER_VALIDATE_INT); | |
if ($pos === false) { | |
throw new \LogicException("must be"); | |
} | |
$code = substr($raw, 0, $pos) . ";" . $insertCode . substr($raw, $pos + strlen(";")); | |
} | |
file_put_contents($file, $code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment