* @license https://mit-license.org/ MIT License * @link https://git.mysnippetsofcode.com/tts/neatoDeploy */ ini_set('display_errors', 1); error_reporting(E_ALL); $cwd = getcwd(); if (! str_contains($cwd, "neato")) { $cwd = "/opt/neatoDeployments"; } $pk = file_get_contents($cwd . "/sumfiles.sig"); if ($pk === false) { echo "No Signatures for sum file checking!"; exit(1); } if (!isset($argv[1])) { echo 'Please give Script to run, example: ./neato_deploy.sh apache' . PHP_EOL; echo "Note: deploy_ is added to the beginning of the filename and .php is added to the end!" . PHP_EOL; echo "Also, the deployment file must be in the deploy_files path." . PHP_EOL; echo "Insecure: you may pass a http web site text file: IE http://mysite.com/apache.txt" . PHP_EOL; exit(1); } define('CONFIG_FILE', basename($argv[1])); require "neato_common.php"; /** * Tell php stan $os_like is from another file * * @phpstan-ignore-next-line Variable $os_like might not be defined */ if ($os_like == 'debian') { putenv("DEBIAN_FRONTEND=noninteractive"); } if (isset($os['id']) && file_exists('OS/neato_' . ucfirst($os['id']) . '.php')) { include 'OS/neato_' . ucfirst($os['id']) . '.php'; } else { $is_Linux = $os['linux'] ?? true; $generic_OS_file = ($is_Linux) ? 'OS/neato_Linux_Generic.php' : 'OS/neato_Windows_Generic.php'; include $generic_OS_file; } /** * Clean up the file name, so user has a better time. * * @param string $file (passed by Reference) deployment file to be prettier * * @return void */ function Clean_File_name(string & $file): void { $file = str_replace("deploy_", "", $file); $file = str_replace(".php", "", $file); } $auto = (in_array('-y', $argv)) ? true : false; $mark = (in_array('-marksafe', $argv)) ? true : false; $skipdeploy = (in_array('-skipdeploy', $argv)) ? true : false; $file = $argv[1]; Clean_File_name($file); if (isStringFound($argv[1], 'http://') || isStringFound($argv[1], 'https://')) { if (isset($argv[2]) && $argv[2] !== "-y" ) { $file = $argv[2]; Clean_File_name($file); } else { $pos = strrpos($argv[1], '/'); $file = substr($argv[1], $pos + 1); $file = str_replace(".txt", "", $file); Clean_File_name($file); } \utils\curl::save($argv[1], "/deploy_files/deploy_{$file}.php"); } /** * Make SHA sum files * * @param mixed $shasum plan hash of file * * @return void */ function Save_sha($shasum): void { $xor = xorEncrypt($shasum, $GLOBALS['pk']); file_put_contents($GLOBALS['cwd'] . '/sums/deploy_' . $GLOBALS['file'].'.sum', $xor); } /** * Check for dangerous stuff and ask if ok to run * bails on dangerous files if not accepted * * @return void */ function Do_Harm_checker(): void { include 'neato_danger_checker.php'; if (isFileDangerious($GLOBALS['cwd'] . '/deploy_files/deploy_' . $GLOBALS['file'].'.php') ) { if (! $GLOBALS['auto']) { $answer = readline("Do you wish to execute this Script, anyways!! ? "); if (strtolower(trim($answer)) === "yes" ) { echo "Okay...!" . PHP_EOL; } else { exit(1); } } } } /* Keep as last lines */ if (file_exists($cwd . '/deploy_files/deploy_' . $file.'.php')) { $check_for_harm = true; $shasum = shaSum($cwd . '/deploy_files/deploy_' . $file.'.php'); if ($shasum === false) { echo "Unable to SHA sum script!"; exit(1); } if (file_exists($cwd . '/sums/deploy_' . $file.'.sum')) { if ($skipdeploy) { unlink($cwd . '/sums/deploy_' . $file.'.sum'); } else { $sum = readMyFile($cwd . '/sums/deploy_' . $file.'.sum'); if ($sum === false) { echo "Bad sum file, Aborting..."; exit(1); } $xor = xorEncrypt($sum, $pk); if ($shasum === $xor) { $check_for_harm = false; } else { echo "Danger: SUM of Script has been modified!"; exit(1); } } } if ($mark) { Save_sha($shasum); } if ($skipdeploy) { echo "Skipping running of Deploy php file...\r\n"; exit(0); } if ($check_for_harm) { Do_Harm_checker(); } include $cwd . '/deploy_files/deploy_' . $file.'.php'; } else { echo 'PHP Script deploy_files/deploy_'. $file . '.php does not exist!!' . PHP_EOL; exit(1); } echo PHP_EOL;