1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Admin tool "Opcache management" - CLI Script to reset opcache
|
|
|
19 |
*
|
|
|
20 |
* @package tool_opcache
|
|
|
21 |
* @copyright 2019 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
define('CLI_SCRIPT', true);
|
|
|
26 |
|
|
|
27 |
require(__DIR__.'/../../../../config.php');
|
|
|
28 |
require_once($CFG->libdir.'/clilib.php');
|
|
|
29 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
// Get cli options.
|
|
|
35 |
list($options, $unrecognized) = cli_get_params(['help' => false,
|
|
|
36 |
'url' => null,
|
|
|
37 |
'reset' => false, ],
|
|
|
38 |
['h' => 'help',
|
|
|
39 |
'u' => 'url',
|
|
|
40 |
'r' => 'reset', ]);
|
|
|
41 |
if ($unrecognized) {
|
|
|
42 |
$unrecognized = implode("\n ", $unrecognized);
|
|
|
43 |
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
// CLI help.
|
|
|
48 |
if ($options['help'] || !($options['url']) || !($options['reset'])) {
|
|
|
49 |
$help = "CLI tool to reset PHP opcache
|
|
|
50 |
|
|
|
51 |
Options:
|
|
|
52 |
-u, --url Full URL to reset_opcache_web.php
|
|
|
53 |
-r, --reset Reset PHP opcache on this server
|
|
|
54 |
-h, --help Print out this help
|
|
|
55 |
|
|
|
56 |
Example:
|
|
|
57 |
\$ sudo -u www-data /usr/bin/php admin/tool/cli/reset_opcache.php ";
|
|
|
58 |
cli_writeln($help);
|
|
|
59 |
exit(0);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
// Clean CLI parameters.
|
|
|
64 |
$options['url'] = clean_param($options['url'], PARAM_URL);
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
// Reset opcache on webserver.
|
|
|
68 |
$curl = new curl(['ignoresecurity' => true]); // The ignoresecurity option means that $CFG->curlsecurityblockedhosts is
|
|
|
69 |
// ignored by purpose. Otherwise, $CFG->curlsecurityblockedhosts might prevent
|
|
|
70 |
// that the web part of this CLI tool is fetched.
|
|
|
71 |
$curloptions = [
|
|
|
72 |
'FRESH_CONNECT' => true,
|
|
|
73 |
'RETURNTRANSFER' => true,
|
|
|
74 |
'FORBID_REUSE' => true,
|
|
|
75 |
'HEADER' => false,
|
|
|
76 |
'CONNECTTIMEOUT' => 5,
|
|
|
77 |
];
|
|
|
78 |
$params = [];
|
|
|
79 |
if (isset($CFG->tool_opcache_reset_secretkey)) {
|
|
|
80 |
$params['secret'] = $CFG->tool_opcache_reset_secretkey;
|
|
|
81 |
}
|
|
|
82 |
$curlret = $curl->get($options['url'], $params, $curloptions);
|
|
|
83 |
|
|
|
84 |
// Echo return string and exit with return status.
|
|
|
85 |
if ($curlret == '0') {
|
|
|
86 |
cli_writeln('PHP opcache has been reset successfully on this server.');
|
|
|
87 |
exit(0);
|
|
|
88 |
} else if ($curlret == '1') {
|
|
|
89 |
cli_error('PHP opcache could not be reset as requested.
|
|
|
90 |
$CFG->tool_opcache_reset_allowed is not set in config.php.
|
|
|
91 |
See README.md for details.');
|
|
|
92 |
} else if ($curlret == '2') {
|
|
|
93 |
cli_error('PHP opcache could not be reset as requested.
|
|
|
94 |
The submitted secret key does not match $CFG->tool_opcache_reset_allowed from config.php.
|
|
|
95 |
See README.md for details.');
|
|
|
96 |
} else {
|
|
|
97 |
cli_error('PHP opcache could not be reset as requested.
|
|
|
98 |
The reason for this may simply be that PHP opcache is disabled on this server.');
|
|
|
99 |
}
|