rhymix/common/scripts/common.php
Kijin Sung 42d09bde65 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.
2023-08-16 23:12:25 +09:00

46 lines
1.3 KiB
PHP

<?php
/**
* This file must be included at the top of all shell scripts (cron jobs).
*
* HERE BE DRAGONS.
*
* Failure to perform the checks listed in this file at the top of a cron job,
* or any attempt to work around the limitations deliberately placed in this
* file, may result in errors or degradation of service.
*
* Please be warned that errors may not show up immediately, especially if you
* screw up the permissions inside deeply nested directory trees. You may find
* it difficult and/or costly to undo the damages when errors begin to show up
* several months later.
*/
// Abort if not CLI.
if (PHP_SAPI !== 'cli')
{
echo "This script must be executed on the command line interface.\n";
exit(1);
}
// Load Rhymix.
chdir(dirname(dirname(__DIR__)));
require_once dirname(__DIR__) . '/autoload.php';
Context::init();
// Abort if the UID does not match.
$uid = Rhymix\Framework\Storage::getServerUID();
if ($uid === 0)
{
echo "This script must NOT be executed by the root user.\n";
exit(2);
}
if (file_exists(\RX_BASEDIR . 'files/config/config.php'))
{
$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);
}
}