From 42d09bde65a4e298699d4d04e9eb0a96e04efc23 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Wed, 16 Aug 2023 23:07:03 +0900 Subject: [PATCH] Provide a single entry point for all command-line scripts Core example: php index.php common.clean_empty_dirs -> executes common/scripts/clean_empty_dirs.php Third-party example: php index.php module_name.script_name -> executes modules/module_name/scripts/script_name.php This should be the preferred method of executing PHP scripts included with any third-party module, because it ensures that the working environment is correct. For example, it will set the current working directory to Rhymix root, check that the UID matches the usual web user, include all core libraries and initialize the Context properly. Over time, we will expand the command-line functionality similar to the 'artisan' command in Laravel. --- classes/module/ModuleHandler.class.php | 32 ++++++++++++++++++++++++++ common/scripts/common.php | 13 +++++++---- index.php | 14 ++++++++--- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/classes/module/ModuleHandler.class.php b/classes/module/ModuleHandler.class.php index ec48e4c5b..14975b103 100644 --- a/classes/module/ModuleHandler.class.php +++ b/classes/module/ModuleHandler.class.php @@ -1226,6 +1226,38 @@ class ModuleHandler extends Handler $oDisplayHandler->printContent($oModule); } + /** + * Process command line arguments. + * + * @param array $args + * @return void + */ + public static function procCommandLineArguments(array $args) + { + $command = count($args) > 1 ? $args[1] : ''; + $args = count($args) > 2 ? array_slice($args, 2) : []; + if (!$command) + { + echo 'Command not supplied.' . PHP_EOL; + exit(1); + } + if (!preg_match('/^([a-z0-9_-]+)\.([a-z0-9_-]+)$/', $command, $matches)) + { + echo 'Invalid command: ' . $command . PHP_EOL; + exit(1); + } + + $path = sprintf('%s%s/scripts/%s.php', \RX_BASEDIR , $matches[1] === 'common' ? 'common' : ('modules/' . $matches[1]), $matches[2]); + if (!file_exists($path) || !is_readable($path)) + { + echo 'Command not found: ' . $command . PHP_EOL; + exit(1); + } + + require_once \RX_BASEDIR . 'common/scripts/common.php'; + require_once $path; + } + /** * returns module's path * @param string $module module name diff --git a/common/scripts/common.php b/common/scripts/common.php index 53fd332dd..17abecdb3 100644 --- a/common/scripts/common.php +++ b/common/scripts/common.php @@ -34,10 +34,13 @@ if ($uid === 0) echo "This script must NOT be executed by the root user.\n"; exit(2); } -$web_server_uid = fileowner(RX_BASEDIR . 'files/config/config.php'); -if ($uid !== $web_server_uid) +if (file_exists(\RX_BASEDIR . 'files/config/config.php')) { - $web_server_uid = posix_getpwuid($web_server_uid); - echo "This script must be executed by the same user as the usual web server process ({$web_server_uid['name']}).\n"; - exit(3); + $web_server_uid = fileowner(\RX_BASEDIR . 'files/config/config.php'); + if ($uid !== $web_server_uid) + { + $web_server_uid = posix_getpwuid($web_server_uid); + echo "This script must be executed by the same user as the usual web server process ({$web_server_uid['name']}).\n"; + exit(3); + } } diff --git a/index.php b/index.php index 06c06039f..701e4ed92 100644 --- a/index.php +++ b/index.php @@ -47,6 +47,14 @@ Context::init(); /** * Initialize and execute the requested module. */ -$oModuleHandler = new ModuleHandler(); -$oModuleHandler->init() && $oModuleHandler->displayContent($oModuleHandler->procModule()); -Context::close(); +if (PHP_SAPI !== 'cli') +{ + $oModuleHandler = new ModuleHandler(); + $oModuleHandler->init() && $oModuleHandler->displayContent($oModuleHandler->procModule()); + Context::close(); +} +else +{ + Rhymix\Framework\Debug::disable(); + ModuleHandler::procCommandLineArguments($argv); +}