Skip to content

Commit

Permalink
MDL-23824 CLI script improvements - just define('CLI_SCRIPT', true) b…
Browse files Browse the repository at this point in the history
…efore require config.php; all incorrect uses of cli and web scripts are detected; refactored cron script - now in two separate sctipts; fix cli inline docs and help - we have to sudo to apache account; standardised cli script locations in auth plugins
  • Loading branch information
skodak committed Aug 17, 2010
1 parent e890316 commit 28bd3d9
Show file tree
Hide file tree
Showing 18 changed files with 912 additions and 725 deletions.
61 changes: 61 additions & 0 deletions admin/cli/cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* CLI cron
*
* This script looks through all the module directories for cron.php files
* and runs them. These files can contain cleanup functions, email functions
* or anything that needs to be run on a regular basis.
*
* @package core
* @subpackage cli
* @copyright 2009 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->libdir.'/clilib.php'); // cli only functions
require_once($CFG->libdir.'/cronlib.php');

// now get cli options
list($options, $unrecognized) = cli_get_params(array('help'=>false),
array('h'=>'help'));

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}

if ($options['help']) {
$help =
"Execute periodic cron actions.
Options:
-h, --help Print out this help
Example:
\$sudo -u www-data /usr/bin/php admin/cli/cron.php
";

echo $help;
die;
}

cron_run();
9 changes: 6 additions & 3 deletions admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

// extra execution prevention - we can not just require config.php here
if (isset($_SERVER['REMOTE_ADDR'])) {
error_log("admin/cli/install.php can not be called from web server!");
exit;
exit(1);
}

$help =
Expand Down Expand Up @@ -68,7 +70,8 @@
required in non-interactive mode.
-h, --help Print out this help
Example: \$sudo -u wwwrun /usr/bin/php admin/cli/install.php --lang=cs
Example:
\$sudo -u www-data /usr/bin/php admin/cli/install.php --lang=cs
"; //TODO: localize, mark as needed in install - to be translated later when everything is finished


Expand Down
9 changes: 6 additions & 3 deletions admin/cli/install_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

// extra execution prevention - we can not just require config.php here
if (isset($_SERVER['REMOTE_ADDR'])) {
error_log("admin/cli/install_database.php can not be called from web server!");
exit;
exit(1);
}

$help =
Expand All @@ -46,7 +48,8 @@
--agree-license Indicates agreement with software license.
-h, --help Print out this help
Example: \$sudo -u wwwrun /usr/bin/php admin/cli/install_database.php --lang=cs --adminpass=soMePass123 --agree-license
Example:
\$sudo -u www-data /usr/bin/php admin/cli/install_database.php --lang=cs --adminpass=soMePass123 --agree-license
";

// Check that PHP is of a sufficient version
Expand Down
13 changes: 5 additions & 8 deletions admin/cli/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

if (isset($_SERVER['REMOTE_ADDR'])) {
error_log("admin/cli/maintenance.php can not be called from web server!");
exit;
}
define('CLI_SCRIPT', true);

require_once dirname(dirname(dirname(__FILE__))).'/config.php';
require(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->libdir.'/clilib.php'); // cli only functions


Expand All @@ -43,8 +40,7 @@
}

if ($options['help']) {

$help =
$help =
"Maintenance mode settings.
Current status displayed if not option specified.
Expand All @@ -53,7 +49,8 @@
--disable Disable maintenance mode
-h, --help Print out this help
Example: \$sudo -u wwwrun /usr/bin/php admin/cli/maintenance.php
Example:
\$sudo -u www-data /usr/bin/php admin/cli/maintenance.php
"; //TODO: localize - to be translated later when everything is finished

echo $help;
Expand Down
15 changes: 6 additions & 9 deletions admin/cli/reset_password.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

if (isset($_SERVER['REMOTE_ADDR'])) {
error_log("admin/cli/reset_password.php can not be called from web server!");
exit;
}
define('CLI_SCRIPT', true);

require_once dirname(dirname(dirname(__FILE__))).'/config.php';
require(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->libdir.'/clilib.php'); // cli only functions


Expand All @@ -43,8 +40,7 @@
}

if ($options['help']) {

$help =
$help =
"Reset local user passwords, useful especially for admin acounts.
There are no security checks here because anybody who is able to
Expand All @@ -53,7 +49,8 @@
Options:
-h, --help Print out this help
Example: \$sudo -u wwwrun /usr/bin/php admin/cli/reset_password.php
Example:
\$sudo -u www-data /usr/bin/php admin/cli/reset_password.php
"; //TODO: localize - to be translated later when everything is finished

echo $help;
Expand All @@ -79,6 +76,6 @@

$DB->set_field('user', 'password', $hashedpassword, array('id'=>$user->id));

echo "Password cahnged\n";
echo "Password changed\n";

exit(0); // 0 means success
13 changes: 5 additions & 8 deletions admin/cli/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

if (isset($_SERVER['REMOTE_ADDR'])) {
error_log("admin/cli/upgrade.php can not be called from web server!");
exit;
}
define('CLI_SCRIPT', true);

require_once dirname(dirname(dirname(__FILE__))).'/config.php';
require(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
require_once($CFG->libdir.'/clilib.php'); // cli only functions
Expand All @@ -54,8 +51,7 @@
}

if ($options['help']) {

$help =
$help =
"Command line Moodle upgrade.
Please note you must execute this script with the same uid as apache!
Expand All @@ -65,7 +61,8 @@
--non-interactive No interactive questions or confirmations
-h, --help Print out this help
Example: \$sudo -u wwwrun /usr/bin/php admin/cli/upgrade.php
Example:
\$sudo -u www-data /usr/bin/php admin/cli/upgrade.php
"; //TODO: localize - to be translated later when everything is finished

echo $help;
Expand Down
Loading

0 comments on commit 28bd3d9

Please sign in to comment.