diff --git a/_cleanup.php b/_cleanup.php index 3c30efdd0..d7649bdc6 100644 --- a/_cleanup.php +++ b/_cleanup.php @@ -16,6 +16,12 @@ if (php_sapi_name() !== 'cli') { exit; } +// Get all constants +require_once BB_PATH . '/library/defines.php'; + +// Include CLI functions +require INC_DIR . '/functions_cli.php'; + $items = [ '.github', '.cliffignore', @@ -41,44 +47,3 @@ foreach ($items as $item) { removeDir($path); } } - -/** - * Remove target file - * - * @param string $file Path to file - */ -function removeFile(string $file): void -{ - if (unlink($file)) { - echo "- File removed: $file" . PHP_EOL; - } else { - echo "- File cannot be removed: $file" . PHP_EOL; - exit; - } -} - -/** - * Remove folder (recursively) - * - * @param string $dir Path to folder - */ -function removeDir(string $dir): void -{ - $it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS); - $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); - - foreach ($files as $file) { - if ($file->isDir()) { - removeDir($file->getPathname()); - } else { - removeFile($file->getPathname()); - } - } - - if (rmdir($dir)) { - echo "- Folder removed: $dir" . PHP_EOL; - } else { - echo "- Folder cannot be removed: $dir" . PHP_EOL; - exit; - } -} diff --git a/install.php b/install.php index ff88365d3..ba0a6ae25 100644 --- a/install.php +++ b/install.php @@ -14,6 +14,12 @@ if (php_sapi_name() !== 'cli') { die('Please run php ' . basename(__FILE__) . ' in CLI mode'); } +// Get all constants +require_once BB_PATH . '/library/defines.php'; + +// Include CLI functions +require INC_DIR . '/functions_cli.php'; + /** * System requirements */ @@ -34,125 +40,6 @@ define('CHECK_REQUIREMENTS', [ ], ]); -/** - * Colored console output - * - * @param string $str - * @param string $type - * @return void - */ -function out(string $str, string $type = ''): void -{ - echo match ($type) { - 'error' => "\033[31m$str \033[0m\n", - 'success' => "\033[32m$str \033[0m\n", - 'warning' => "\033[33m$str \033[0m\n", - 'info' => "\033[36m$str \033[0m\n", - 'debug' => "\033[90m$str \033[0m\n", - default => "$str\n", - }; -} - -/** - * Run process with realtime output - * - * @param string $cmd - * @param string|null $input - * @return void - */ -function runProcess(string $cmd, string $input = null): void -{ - $descriptorSpec = [ - 0 => ['pipe', 'r'], - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'], - ]; - - $process = proc_open($cmd, $descriptorSpec, $pipes); - - if (!is_resource($process)) { - out('- Could not start subprocess', 'error'); - return; - } - - // Write input if provided - if ($input !== null) { - fwrite($pipes[0], $input); - fclose($pipes[0]); - } - - // Read and print output in real-time - while (!feof($pipes[1])) { - echo stream_get_contents($pipes[1], 1); - flush(); // Flush output buffer for immediate display - } - - // Read and print error output - while (!feof($pipes[2])) { - echo stream_get_contents($pipes[2], 1); - flush(); - } - - fclose($pipes[1]); - fclose($pipes[2]); - - proc_close($process); -} - -/** - * Remove directory recursively - * - * @param string $dir - * @return void - */ -function rmdir_rec(string $dir): void -{ - $it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS); - $files = new RecursiveIteratorIterator($it, - RecursiveIteratorIterator::CHILD_FIRST); - foreach ($files as $file) { - if ($file->isDir()) { - rmdir($file->getPathname()); - } else { - unlink($file->getPathname()); - } - } - rmdir($dir); -} - -/** - * Setting permissions recursively - * - * @param string $dir - * @param int $dirPermissions - * @param int $filePermissions - * @return void - */ -function chmod_r(string $dir, int $dirPermissions, int $filePermissions): void -{ - $dp = opendir($dir); - while ($file = readdir($dp)) { - if (($file == '.') || ($file == '..')) { - continue; - } - - $fullPath = realpath($dir . '/' . $file); - if (is_dir($fullPath)) { - out("- Directory: $fullPath"); - chmod($fullPath, $dirPermissions); - chmod_r($fullPath, $dirPermissions, $filePermissions); - } elseif (is_file($fullPath)) { - // out("- File: $fullPath"); - chmod($fullPath, $filePermissions); - } else { - out("- Cannot find target path: $fullPath", 'error'); - return; - } - } - - closedir($dp); -} - // Welcoming message out("--- TorrentPier Installer ---\n", 'info'); diff --git a/library/includes/functions_cli.php b/library/includes/functions_cli.php new file mode 100644 index 000000000..37864e20a --- /dev/null +++ b/library/includes/functions_cli.php @@ -0,0 +1,172 @@ +isDir()) { + removeDir($file->getPathname()); + } else { + removeFile($file->getPathname()); + } + } + + if (rmdir($dir)) { + echo "- Folder removed: $dir" . PHP_EOL; + } else { + echo "- Folder cannot be removed: $dir" . PHP_EOL; + exit; + } +} + +/** + * Colored console output + * + * @param string $str + * @param string $type + * @return void + */ +function out(string $str, string $type = ''): void +{ + echo match ($type) { + 'error' => "\033[31m$str \033[0m\n", + 'success' => "\033[32m$str \033[0m\n", + 'warning' => "\033[33m$str \033[0m\n", + 'info' => "\033[36m$str \033[0m\n", + 'debug' => "\033[90m$str \033[0m\n", + default => "$str\n", + }; +} + +/** + * Run process with realtime output + * + * @param string $cmd + * @param string|null $input + * @return void + */ +function runProcess(string $cmd, string $input = null): void +{ + $descriptorSpec = [ + 0 => ['pipe', 'r'], + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'], + ]; + + $process = proc_open($cmd, $descriptorSpec, $pipes); + + if (!is_resource($process)) { + out('- Could not start subprocess', 'error'); + return; + } + + // Write input if provided + if ($input !== null) { + fwrite($pipes[0], $input); + fclose($pipes[0]); + } + + // Read and print output in real-time + while (!feof($pipes[1])) { + echo stream_get_contents($pipes[1], 1); + flush(); // Flush output buffer for immediate display + } + + // Read and print error output + while (!feof($pipes[2])) { + echo stream_get_contents($pipes[2], 1); + flush(); + } + + fclose($pipes[1]); + fclose($pipes[2]); + + proc_close($process); +} + +/** + * Remove directory recursively + * + * @param string $dir + * @return void + */ +function rmdir_rec(string $dir): void +{ + $it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator($it, + RecursiveIteratorIterator::CHILD_FIRST); + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getPathname()); + } else { + unlink($file->getPathname()); + } + } + rmdir($dir); +} + +/** + * Setting permissions recursively + * + * @param string $dir + * @param int $dirPermissions + * @param int $filePermissions + * @return void + */ +function chmod_r(string $dir, int $dirPermissions, int $filePermissions): void +{ + $dp = opendir($dir); + while ($file = readdir($dp)) { + if (($file == '.') || ($file == '..')) { + continue; + } + + $fullPath = realpath($dir . '/' . $file); + if (is_dir($fullPath)) { + out("- Directory: $fullPath"); + chmod($fullPath, $dirPermissions); + chmod_r($fullPath, $dirPermissions, $filePermissions); + } elseif (is_file($fullPath)) { + // out("- File: $fullPath"); + chmod($fullPath, $filePermissions); + } else { + out("- Cannot find target path: $fullPath", 'error'); + return; + } + } + + closedir($dp); +}