3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Moodle deployment utility
21 * This script looks after deploying available updates to the local Moodle site.
24 * $ sudo -u apache php mdeploy.php --upgrade \
25 * --package=https://moodle.org/plugins/download.php/...zip \
26 * --dataroot=/home/mudrd8mz/moodledata/moodle24
29 * @copyright 2012 David Mudrak <david@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 if (defined('MOODLE_INTERNAL')) {
34 die('This is a standalone utility that should not be included by any other Moodle code.');
38 // Exceptions //////////////////////////////////////////////////////////////////
40 class invalid_coding_exception
extends Exception
{}
41 class missing_option_exception
extends Exception
{}
42 class invalid_option_exception
extends Exception
{}
43 class unauthorized_access_exception
extends Exception
{}
44 class download_file_exception
extends Exception
{}
45 class backup_folder_exception
extends Exception
{}
46 class zip_exception
extends Exception
{}
47 class filesystem_exception
extends Exception
{}
48 class checksum_exception
extends Exception
{}
51 // Various support classes /////////////////////////////////////////////////////
54 * Base class implementing the singleton pattern using late static binding feature.
56 * @copyright 2012 David Mudrak <david@moodle.com>
57 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59 abstract class singleton_pattern
{
61 /** @var array singleton_pattern instances */
62 protected static $singletoninstances = array();
65 * Factory method returning the singleton instance.
67 * Subclasses may want to override the {@link self::initialize()} method that is
68 * called right after their instantiation.
70 * @return mixed the singleton instance
72 final public static function instance() {
73 $class = get_called_class();
74 if (!isset(static::$singletoninstances[$class])) {
75 static::$singletoninstances[$class] = new static();
76 static::$singletoninstances[$class]->initialize();
78 return static::$singletoninstances[$class];
82 * Optional post-instantiation code.
84 protected function initialize() {
85 // Do nothing in this base class.
89 * Direct instantiation not allowed, use the factory method {@link instance()}
91 final protected function __construct() {
95 * Sorry, this is singleton.
97 final protected function __clone() {
102 // User input handling /////////////////////////////////////////////////////////
105 * Provides access to the script options.
107 * Implements the delegate pattern by dispatching the calls to appropriate
108 * helper class (CLI or HTTP).
110 * @copyright 2012 David Mudrak <david@moodle.com>
111 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
113 class input_manager
extends singleton_pattern
{
115 const TYPE_FILE
= 'file'; // File name
116 const TYPE_FLAG
= 'flag'; // No value, just a flag (switch)
117 const TYPE_INT
= 'int'; // Integer
118 const TYPE_PATH
= 'path'; // Full path to a file or a directory
119 const TYPE_RAW
= 'raw'; // Raw value, keep as is
120 const TYPE_URL
= 'url'; // URL to a file
121 const TYPE_PLUGIN
= 'plugin'; // Plugin name
122 const TYPE_MD5
= 'md5'; // MD5 hash
124 /** @var input_cli_provider|input_http_provider the provider of the input */
125 protected $inputprovider = null;
128 * Returns the value of an option passed to the script.
130 * If the caller passes just the $name, the requested argument is considered
131 * required. The caller may specify the second argument which then
132 * makes the argument optional with the given default value.
134 * If the type of the $name option is TYPE_FLAG (switch), this method returns
135 * true if the flag has been passed or false if it was not. Specifying the
136 * default value makes no sense in this case and leads to invalid coding exception.
138 * The array options are not supported.
140 * @example $filename = $input->get_option('f');
141 * @example $filename = $input->get_option('filename');
142 * @example if ($input->get_option('verbose')) { ... }
143 * @param string $name
146 public function get_option($name, $default = 'provide_default_value_explicitly') {
148 $this->validate_option_name($name);
150 $info = $this->get_option_info($name);
152 if ($info->type
=== input_manager
::TYPE_FLAG
) {
153 return $this->inputprovider
->has_option($name);
156 if (func_num_args() == 1) {
157 return $this->get_required_option($name);
159 return $this->get_optional_option($name, $default);
164 * Returns the meta-information about the given option.
166 * @param string|null $name short or long option name, defaults to returning the list of all
167 * @return array|object|false array with all, object with the specific option meta-information or false of no such an option
169 public function get_option_info($name=null) {
171 $supportedoptions = array(
172 array('', 'passfile', input_manager
::TYPE_FILE
, 'File name of the passphrase file (HTTP access only)'),
173 array('', 'password', input_manager
::TYPE_RAW
, 'Session passphrase (HTTP access only)'),
174 array('', 'proxy', input_manager
::TYPE_RAW
, 'HTTP proxy host and port (e.g. \'our.proxy.edu:8888\')'),
175 array('', 'proxytype', input_manager
::TYPE_RAW
, 'Proxy type (HTTP or SOCKS5)'),
176 array('', 'proxyuserpwd', input_manager
::TYPE_RAW
, 'Proxy username and password (e.g. \'username:password\')'),
177 array('', 'returnurl', input_manager
::TYPE_URL
, 'Return URL (HTTP access only)'),
178 array('d', 'dataroot', input_manager
::TYPE_PATH
, 'Full path to the dataroot (moodledata) directory'),
179 array('h', 'help', input_manager
::TYPE_FLAG
, 'Prints usage information'),
180 array('i', 'install', input_manager
::TYPE_FLAG
, 'Installation mode'),
181 array('m', 'md5', input_manager
::TYPE_MD5
, 'Expected MD5 hash of the ZIP package to deploy'),
182 array('n', 'name', input_manager
::TYPE_PLUGIN
, 'Plugin name (the name of its folder)'),
183 array('p', 'package', input_manager
::TYPE_URL
, 'URL to the ZIP package to deploy'),
184 array('r', 'typeroot', input_manager
::TYPE_PATH
, 'Full path of the container for this plugin type'),
185 array('u', 'upgrade', input_manager
::TYPE_FLAG
, 'Upgrade mode'),
188 if (is_null($name)) {
190 foreach ($supportedoptions as $optioninfo) {
191 $info = new stdClass();
192 $info->shortname
= $optioninfo[0];
193 $info->longname
= $optioninfo[1];
194 $info->type
= $optioninfo[2];
195 $info->desc
= $optioninfo[3];
203 foreach ($supportedoptions as $optioninfo) {
204 if (strlen($name) == 1) {
205 // Search by the short option name
206 if ($optioninfo[0] === $name) {
207 $found = $optioninfo;
211 // Search by the long option name
212 if ($optioninfo[1] === $name) {
213 $found = $optioninfo;
223 $info = new stdClass();
224 $info->shortname
= $found[0];
225 $info->longname
= $found[1];
226 $info->type
= $found[2];
227 $info->desc
= $found[3];
233 * Casts the value to the given type.
235 * @param mixed $raw the raw value
236 * @param string $type the expected value type, e.g. {@link input_manager::TYPE_INT}
239 public function cast_value($raw, $type) {
241 if (is_array($raw)) {
242 throw new invalid_coding_exception('Unsupported array option.');
243 } else if (is_object($raw)) {
244 throw new invalid_coding_exception('Unsupported object option.');
249 case input_manager
::TYPE_FILE
:
250 $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\':\\\\/]~u', '', $raw);
251 $raw = preg_replace('~\.\.+~', '', $raw);
257 case input_manager
::TYPE_FLAG
:
260 case input_manager
::TYPE_INT
:
263 case input_manager
::TYPE_PATH
:
264 if (strpos($raw, '~') !== false) {
265 throw new invalid_option_exception('Using the tilde (~) character in paths is not supported');
267 $raw = str_replace('\\', '/', $raw);
268 $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\':]~u', '', $raw);
269 $raw = preg_replace('~\.\.+~', '', $raw);
270 $raw = preg_replace('~//+~', '/', $raw);
271 $raw = preg_replace('~/(\./)+~', '/', $raw);
274 case input_manager
::TYPE_RAW
:
277 case input_manager
::TYPE_URL
:
278 $regex = '^(https?|ftp)\:\/\/'; // protocol
279 $regex .= '([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?'; // optional user and password
280 $regex .= '[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*'; // hostname or IP (one word like http://localhost/ allowed)
281 $regex .= '(\:[0-9]{2,5})?'; // port (optional)
282 $regex .= '(\/([a-z0-9+\$_-]\.?)+)*\/?'; // path to the file
283 $regex .= '(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?'; // HTTP params
285 if (preg_match('#'.$regex.'#i', $raw)) {
288 throw new invalid_option_exception('Not a valid URL');
291 case input_manager
::TYPE_PLUGIN
:
292 if (!preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/', $raw)) {
293 throw new invalid_option_exception('Invalid plugin name');
295 if (strpos($raw, '__') !== false) {
296 throw new invalid_option_exception('Invalid plugin name');
300 case input_manager
::TYPE_MD5
:
301 if (!preg_match('/^[a-f0-9]{32}$/', $raw)) {
302 throw new invalid_option_exception('Invalid MD5 hash format');
307 throw new invalid_coding_exception('Unknown option type.');
313 * Picks the appropriate helper class to delegate calls to.
315 protected function initialize() {
316 if (PHP_SAPI
=== 'cli') {
317 $this->inputprovider
= input_cli_provider
::instance();
319 $this->inputprovider
= input_http_provider
::instance();
323 // End of external API
326 * Validates the parameter name.
328 * @param string $name
329 * @throws invalid_coding_exception
331 protected function validate_option_name($name) {
334 throw new invalid_coding_exception('Invalid empty option name.');
337 $meta = $this->get_option_info($name);
339 throw new invalid_coding_exception('Invalid option name: '.$name);
344 * Returns cleaned option value or throws exception.
346 * @param string $name the name of the parameter
347 * @param string $type the parameter type, e.g. {@link input_manager::TYPE_INT}
350 protected function get_required_option($name) {
351 if ($this->inputprovider
->has_option($name)) {
352 return $this->inputprovider
->get_option($name);
354 throw new missing_option_exception('Missing required option: '.$name);
359 * Returns cleaned option value or the default value
361 * @param string $name the name of the parameter
362 * @param string $type the parameter type, e.g. {@link input_manager::TYPE_INT}
363 * @param mixed $default the default value.
366 protected function get_optional_option($name, $default) {
367 if ($this->inputprovider
->has_option($name)) {
368 return $this->inputprovider
->get_option($name);
377 * Base class for input providers.
379 * @copyright 2012 David Mudrak <david@moodle.com>
380 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
382 abstract class input_provider
extends singleton_pattern
{
384 /** @var array list of all passed valid options */
385 protected $options = array();
388 * Returns the casted value of the option.
390 * @param string $name option name
391 * @throws invalid_coding_exception if the option has not been passed
392 * @return mixed casted value of the option
394 public function get_option($name) {
396 if (!$this->has_option($name)) {
397 throw new invalid_coding_exception('Option not passed: '.$name);
400 return $this->options
[$name];
404 * Was the given option passed?
406 * @param string $name optionname
409 public function has_option($name) {
410 return array_key_exists($name, $this->options
);
414 * Initializes the input provider.
416 protected function initialize() {
417 $this->populate_options();
420 // End of external API
423 * Parses and validates all supported options passed to the script.
425 protected function populate_options() {
427 $input = input_manager
::instance();
428 $raw = $this->parse_raw_options();
431 foreach ($raw as $k => $v) {
432 if (is_array($v) or is_object($v)) {
436 $info = $input->get_option_info($k);
441 $casted = $input->cast_value($v, $info->type
);
443 if (!empty($info->shortname
)) {
444 $cooked[$info->shortname
] = $casted;
447 if (!empty($info->longname
)) {
448 $cooked[$info->longname
] = $casted;
452 // Store the options.
453 $this->options
= $cooked;
459 * Provides access to the script options passed via CLI.
461 * @copyright 2012 David Mudrak <david@moodle.com>
462 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
464 class input_cli_provider
extends input_provider
{
467 * Parses raw options passed to the script.
469 * @return array as returned by getopt()
471 protected function parse_raw_options() {
473 $input = input_manager
::instance();
475 // Signatures of some in-built PHP functions are just crazy, aren't they.
479 foreach ($input->get_option_info() as $option) {
480 if ($option->type
=== input_manager
::TYPE_FLAG
) {
481 // No value expected for this option.
482 $short .= $option->shortname
;
483 $long[] = $option->longname
;
485 // A value expected for the option, all considered as optional.
486 $short .= empty($option->shortname
) ?
'' : $option->shortname
.'::';
487 $long[] = empty($option->longname
) ?
'' : $option->longname
.'::';
491 return getopt($short, $long);
497 * Provides access to the script options passed via HTTP request.
499 * @copyright 2012 David Mudrak <david@moodle.com>
500 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
502 class input_http_provider
extends input_provider
{
505 * Parses raw options passed to the script.
507 * @return array of raw values passed via HTTP request
509 protected function parse_raw_options() {
515 // Output handling /////////////////////////////////////////////////////////////
518 * Provides output operations.
520 * @copyright 2012 David Mudrak <david@moodle.com>
521 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
523 class output_manager
extends singleton_pattern
{
525 /** @var output_cli_provider|output_http_provider the provider of the output functionality */
526 protected $outputprovider = null;
529 * Magic method triggered when invoking an inaccessible method.
531 * @param string $name method name
532 * @param array $arguments method arguments
534 public function __call($name, array $arguments = array()) {
535 call_user_func_array(array($this->outputprovider
, $name), $arguments);
539 * Picks the appropriate helper class to delegate calls to.
541 protected function initialize() {
542 if (PHP_SAPI
=== 'cli') {
543 $this->outputprovider
= output_cli_provider
::instance();
545 $this->outputprovider
= output_http_provider
::instance();
552 * Base class for all output providers.
554 * @copyright 2012 David Mudrak <david@moodle.com>
555 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
557 abstract class output_provider
extends singleton_pattern
{
561 * Provides output to the command line.
563 * @copyright 2012 David Mudrak <david@moodle.com>
564 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
566 class output_cli_provider
extends output_provider
{
569 * Prints help information in CLI mode.
571 public function help() {
573 $this->outln('mdeploy.php - Moodle (http://moodle.org) deployment utility');
575 $this->outln('Usage: $ sudo -u apache php mdeploy.php [options]');
577 $input = input_manager
::instance();
578 foreach($input->get_option_info() as $info) {
580 if (!empty($info->shortname
)) {
581 $option[] = '-'.$info->shortname
;
583 if (!empty($info->longname
)) {
584 $option[] = '--'.$info->longname
;
586 $this->outln(sprintf('%-20s %s', implode(', ', $option), $info->desc
));
590 // End of external API
593 * Writes a text to the STDOUT followed by a new line character.
595 * @param string $text text to print
597 protected function outln($text='') {
598 fputs(STDOUT
, $text.PHP_EOL
);
604 * Provides HTML output as a part of HTTP response.
606 * @copyright 2012 David Mudrak <david@moodle.com>
607 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
609 class output_http_provider
extends output_provider
{
612 * Prints help on the script usage.
614 public function help() {
615 // No help available via HTTP
619 * Display the information about uncaught exception
621 * @param Exception $e uncaught exception
623 public function exception(Exception
$e) {
625 $docslink = 'http://docs.moodle.org/en/admin/mdeploy/'.get_class($e);
626 $this->start_output();
627 echo('<h1>Oops! It did it again</h1>');
628 echo('<p><strong>Moodle deployment utility had a trouble with your request.
629 See <a href="'.$docslink.'">the docs page</a> and the debugging information for more details.</strong></p>');
631 echo exception_handlers
::format_exception_info($e);
636 // End of external API
639 * Produce the HTML page header
641 protected function start_output() {
642 echo '<!doctype html>
645 <meta charset="utf-8">
646 <style type="text/css">
647 body {background-color:#666;font-family:"DejaVu Sans","Liberation Sans",Freesans,sans-serif;}
648 h1 {text-align:center;}
649 pre {white-space: pre-wrap;}
650 #page {background-color:#eee;width:1024px;margin:5em auto;border:3px solid #333;border-radius: 15px;padding:1em;}
658 * Produce the HTML page footer
660 protected function end_output() {
661 echo '</div></body></html>';
665 // The main class providing all the functionality //////////////////////////////
668 * The actual worker class implementing the main functionality of the script.
670 * @copyright 2012 David Mudrak <david@moodle.com>
671 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
673 class worker
extends singleton_pattern
{
675 const EXIT_OK
= 0; // Success exit code.
676 const EXIT_HELP
= 1; // Explicit help required.
677 const EXIT_UNKNOWN_ACTION
= 127; // Neither -i nor -u provided.
679 /** @var input_manager */
680 protected $input = null;
682 /** @var output_manager */
683 protected $output = null;
685 /** @var int the most recent cURL error number, zero for no error */
686 private $curlerrno = null;
688 /** @var string the most recent cURL error message, empty string for no error */
689 private $curlerror = null;
691 /** @var array|false the most recent cURL request info, if it was successful */
692 private $curlinfo = null;
694 /** @var string the full path to the log file */
695 private $logfile = null;
698 * Main - the one that actually does something
700 public function execute() {
702 $this->log('=== MDEPLOY EXECUTION START ===');
704 // Authorize access. None in CLI. Passphrase in HTTP.
707 // Asking for help in the CLI mode.
708 if ($this->input
->get_option('help')) {
709 $this->output
->help();
710 $this->done(self
::EXIT_HELP
);
713 if ($this->input
->get_option('upgrade')) {
714 $this->log('Plugin upgrade requested');
716 // Fetch the ZIP file into a temporary location.
717 $source = $this->input
->get_option('package');
718 $target = $this->target_location($source);
719 $this->log('Downloading package '.$source);
721 if ($this->download_file($source, $target)) {
722 $this->log('Package downloaded into '.$target);
724 $this->log('cURL error ' . $this->curlerrno
. ' ' . $this->curlerror
);
725 $this->log('Unable to download the file');
726 throw new download_file_exception('Unable to download the package');
729 // Compare MD5 checksum of the ZIP file
730 $md5remote = $this->input
->get_option('md5');
731 $md5local = md5_file($target);
733 if ($md5local !== $md5remote) {
734 $this->log('MD5 checksum failed. Expected: '.$md5remote.' Got: '.$md5local);
735 throw new checksum_exception('MD5 checksum failed');
737 $this->log('MD5 checksum ok');
739 // Backup the current version of the plugin
740 $plugintyperoot = $this->input
->get_option('typeroot');
741 $pluginname = $this->input
->get_option('name');
742 $sourcelocation = $plugintyperoot.'/'.$pluginname;
743 $backuplocation = $this->backup_location($sourcelocation);
745 $this->log('Current plugin code location: '.$sourcelocation);
746 $this->log('Moving the current code into archive: '.$backuplocation);
748 // We don't want to touch files unless we are pretty sure it would be all ok.
749 if (!$this->move_directory_source_precheck($sourcelocation)) {
750 throw new backup_folder_exception('Unable to backup the current version of the plugin (source precheck failed)');
752 if (!$this->move_directory_target_precheck($backuplocation)) {
753 throw new backup_folder_exception('Unable to backup the current version of the plugin (backup precheck failed)');
756 // Looking good, let's try it.
757 if (!$this->move_directory($sourcelocation, $backuplocation)) {
758 throw new backup_folder_exception('Unable to backup the current version of the plugin (moving failed)');
761 // Unzip the plugin package file into the target location.
762 $this->unzip_plugin($target, $plugintyperoot, $sourcelocation, $backuplocation);
763 $this->log('Package successfully extracted');
765 // Redirect to the given URL (in HTTP) or exit (in CLI).
768 } else if ($this->input
->get_option('install')) {
769 // Installing a new plugin not implemented yet.
772 // Print help in CLI by default.
773 $this->output
->help();
774 $this->done(self
::EXIT_UNKNOWN_ACTION
);
778 * Attempts to log a thrown exception
780 * @param Exception $e uncaught exception
782 public function log_exception(Exception
$e) {
783 $this->log($e->__toString());
787 * Initialize the worker class.
789 protected function initialize() {
790 $this->input
= input_manager
::instance();
791 $this->output
= output_manager
::instance();
794 // End of external API
797 * Finish this script execution.
799 * @param int $exitcode
801 protected function done($exitcode = self
::EXIT_OK
) {
803 if (PHP_SAPI
=== 'cli') {
807 $returnurl = $this->input
->get_option('returnurl');
808 $this->redirect($returnurl);
814 * Authorize access to the script.
816 * In CLI mode, the access is automatically authorized. In HTTP mode, the
817 * passphrase submitted via the request params must match the contents of the
818 * file, the name of which is passed in another parameter.
820 * @throws unauthorized_access_exception
822 protected function authorize() {
824 if (PHP_SAPI
=== 'cli') {
825 $this->log('Successfully authorized using the CLI SAPI');
829 $dataroot = $this->input
->get_option('dataroot');
830 $passfile = $this->input
->get_option('passfile');
831 $password = $this->input
->get_option('password');
833 $passpath = $dataroot.'/mdeploy/auth/'.$passfile;
835 if (!is_readable($passpath)) {
836 throw new unauthorized_access_exception('Unable to read the passphrase file.');
839 $stored = file($passpath, FILE_IGNORE_NEW_LINES
);
841 // "This message will self-destruct in five seconds." -- Mission Commander Swanbeck, Mission: Impossible II
844 if (is_readable($passpath)) {
845 throw new unauthorized_access_exception('Unable to remove the passphrase file.');
848 if (count($stored) < 2) {
849 throw new unauthorized_access_exception('Invalid format of the passphrase file.');
852 if (time() - (int)$stored[1] > 30 * 60) {
853 throw new unauthorized_access_exception('Passphrase timeout.');
856 if (strlen($stored[0]) < 24) {
857 throw new unauthorized_access_exception('Session passphrase not long enough.');
860 if ($password !== $stored[0]) {
861 throw new unauthorized_access_exception('Session passphrase does not match the stored one.');
864 $this->log('Successfully authorized using the passphrase file');
868 * Returns the full path to the log file.
872 protected function log_location() {
874 if (!is_null($this->logfile
)) {
875 return $this->logfile
;
878 $dataroot = $this->input
->get_option('dataroot', '');
880 if (empty($dataroot)) {
881 $this->logfile
= false;
882 return $this->logfile
;
885 $myroot = $dataroot.'/mdeploy';
887 if (!is_dir($myroot)) {
888 mkdir($myroot, 02777, true);
891 $this->logfile
= $myroot.'/mdeploy.log';
892 return $this->logfile
;
896 * Choose the target location for the given ZIP's URL.
898 * @param string $source URL
901 protected function target_location($source) {
903 $dataroot = $this->input
->get_option('dataroot');
904 $pool = $dataroot.'/mdeploy/var';
906 if (!is_dir($pool)) {
907 mkdir($pool, 02777, true);
910 $target = $pool.'/'.md5($source);
913 while (file_exists($target.'.'.$suffix.'.zip')) {
917 return $target.'.'.$suffix.'.zip';
921 * Choose the location of the current plugin folder backup
923 * @param string $path full path to the current folder
926 protected function backup_location($path) {
928 $dataroot = $this->input
->get_option('dataroot');
929 $pool = $dataroot.'/mdeploy/archive';
931 if (!is_dir($pool)) {
932 mkdir($pool, 02777, true);
935 $target = $pool.'/'.basename($path).'_'.time();
938 while (file_exists($target.'.'.$suffix)) {
942 return $target.'.'.$suffix;
946 * Downloads the given file into the given destination.
948 * This is basically a simplified version of {@link download_file_content()} from
949 * Moodle itself, tuned for fetching files from moodle.org servers.
951 * @param string $source file url starting with http(s)://
952 * @param string $target store the downloaded content to this file (full path)
953 * @return bool true on success, false otherwise
954 * @throws download_file_exception
956 protected function download_file($source, $target) {
958 $newlines = array("\r", "\n");
959 $source = str_replace($newlines, '', $source);
960 if (!preg_match('|^https?://|i', $source)) {
961 throw new download_file_exception('Unsupported transport protocol.');
963 if (!$ch = curl_init($source)) {
964 $this->log('Unable to init cURL.');
968 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, true); // verify the peer's certificate
969 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, 2); // check the existence of a common name and also verify that it matches the hostname provided
970 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true); // return the transfer as a string
971 curl_setopt($ch, CURLOPT_HEADER
, false); // don't include the header in the output
972 curl_setopt($ch, CURLOPT_TIMEOUT
, 3600);
973 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 20); // nah, moodle.org is never unavailable! :-p
974 curl_setopt($ch, CURLOPT_URL
, $source);
976 $dataroot = $this->input
->get_option('dataroot');
977 $cacertfile = $dataroot.'/moodleorgca.crt';
978 if (is_readable($cacertfile)) {
979 // Do not use CA certs provided by the operating system. Instead,
980 // use this CA cert to verify the ZIP provider.
981 $this->log('Using custom CA certificate '.$cacertfile);
982 curl_setopt($ch, CURLOPT_CAINFO
, $cacertfile);
985 $proxy = $this->input
->get_option('proxy', false);
986 if (!empty($proxy)) {
987 curl_setopt($ch, CURLOPT_PROXY
, $proxy);
989 $proxytype = $this->input
->get_option('proxytype', false);
990 if (strtoupper($proxytype) === 'SOCKS5') {
991 $this->log('Using SOCKS5 proxy');
992 curl_setopt($ch, CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
993 } else if (!empty($proxytype)) {
994 $this->log('Using HTTP proxy');
995 curl_setopt($ch, CURLOPT_PROXYTYPE
, CURLPROXY_HTTP
);
996 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL
, false);
999 $proxyuserpwd = $this->input
->get_option('proxyuserpwd', false);
1000 if (!empty($proxyuserpwd)) {
1001 curl_setopt($ch, CURLOPT_PROXYUSERPWD
, $proxyuserpwd);
1002 curl_setopt($ch, CURLOPT_PROXYAUTH
, CURLAUTH_BASIC | CURLAUTH_NTLM
);
1006 $targetfile = fopen($target, 'w');
1009 throw new download_file_exception('Unable to create local file '.$target);
1012 curl_setopt($ch, CURLOPT_FILE
, $targetfile);
1014 $result = curl_exec($ch);
1016 // try to detect encoding problems
1017 if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
1018 curl_setopt($ch, CURLOPT_ENCODING
, 'none');
1019 $result = curl_exec($ch);
1022 fclose($targetfile);
1024 $this->curlerrno
= curl_errno($ch);
1025 $this->curlerror
= curl_error($ch);
1026 $this->curlinfo
= curl_getinfo($ch);
1028 if (!$result or $this->curlerrno
) {
1031 } else if (is_array($this->curlinfo
) and (empty($this->curlinfo
['http_code']) or $this->curlinfo
['http_code'] != 200)) {
1041 * @param string $message
1043 protected function log($message) {
1045 $logpath = $this->log_location();
1047 if (empty($logpath)) {
1048 // no logging available
1052 $f = fopen($logpath, 'ab');
1055 throw new filesystem_exception('Unable to open the log file for appending');
1058 $message = $this->format_log_message($message);
1060 fwrite($f, $message);
1066 * Prepares the log message for writing into the file
1068 * @param string $msg
1071 protected function format_log_message($msg) {
1074 $timestamp = date("Y-m-d H:i:s");
1076 return $timestamp . ' '. $msg . PHP_EOL
;
1080 * Checks to see if the given source could be safely moved into a new location
1082 * @param string $source full path to the existing directory
1085 protected function move_directory_source_precheck($source) {
1087 if (!is_writable($source)) {
1091 if (is_dir($source)) {
1092 $handle = opendir($source);
1099 while ($filename = readdir($handle)) {
1100 $sourcepath = $source.'/'.$filename;
1102 if ($filename === '.' or $filename === '..') {
1106 if (is_dir($sourcepath)) {
1107 $result = $result && $this->move_directory_source_precheck($sourcepath);
1110 $result = $result && is_writable($sourcepath);
1120 * Checks to see if a source foldr could be safely moved into the given new location
1122 * @param string $destination full path to the new expected location of a folder
1125 protected function move_directory_target_precheck($target) {
1127 if (file_exists($target)) {
1131 $result = mkdir($target, 02777) && rmdir($target);
1137 * Moves the given source into a new location recursively
1139 * @param string $source full path to the existing directory
1140 * @param string $destination full path to the new location of the folder
1143 protected function move_directory($source, $target) {
1145 if (file_exists($target)) {
1146 throw new filesystem_exception('Unable to move the directory - target location already exists');
1149 if (is_dir($source)) {
1150 $handle = opendir($source);
1152 throw new filesystem_exception('Source location is not a directory');
1155 mkdir($target, 02777);
1157 while ($filename = readdir($handle)) {
1158 $sourcepath = $source.'/'.$filename;
1159 $targetpath = $target.'/'.$filename;
1161 if ($filename === '.' or $filename === '..') {
1165 if (is_dir($sourcepath)) {
1166 $this->move_directory($sourcepath, $targetpath);
1169 rename($sourcepath, $targetpath);
1174 return rmdir($source);
1178 * Deletes the given directory recursively
1180 * @param string $path full path to the directory
1182 protected function remove_directory($path) {
1184 if (!file_exists($path)) {
1188 if (is_dir($path)) {
1189 $handle = opendir($path);
1191 throw new filesystem_exception('Given path is not a directory');
1194 while ($filename = readdir($handle)) {
1195 $filepath = $path.'/'.$filename;
1197 if ($filename === '.' or $filename === '..') {
1201 if (is_dir($filepath)) {
1202 $this->remove_directory($filepath);
1210 return rmdir($path);
1214 * Unzip the file obtained from the Plugins directory to this site
1216 * @param string $ziplocation full path to the ZIP file
1217 * @param string $plugintyperoot full path to the plugin's type location
1218 * @param string $expectedlocation expected full path to the plugin after it is extracted
1219 * @param string $backuplocation location of the previous version of the plugin
1221 protected function unzip_plugin($ziplocation, $plugintyperoot, $expectedlocation, $backuplocation) {
1223 $zip = new ZipArchive();
1224 $result = $zip->open($ziplocation);
1226 if ($result !== true) {
1227 $this->move_directory($backuplocation, $expectedlocation);
1228 throw new zip_exception('Unable to open the zip package');
1231 // Make sure that the ZIP has expected structure
1232 $pluginname = basename($expectedlocation);
1233 for ($i = 0; $i < $zip->numFiles
; $i++
) {
1234 $stat = $zip->statIndex($i);
1235 $filename = $stat['name'];
1236 $filename = explode('/', $filename);
1237 if ($filename[0] !== $pluginname) {
1239 throw new zip_exception('Invalid structure of the zip package');
1243 if (!$zip->extractTo($plugintyperoot)) {
1245 $this->remove_directory($expectedlocation); // just in case something was created
1246 $this->move_directory($backuplocation, $expectedlocation);
1247 throw new zip_exception('Unable to extract the zip package');
1251 unlink($ziplocation);
1255 * Redirect the browser
1257 * @todo check if there has been some output yet
1258 * @param string $url
1260 protected function redirect($url) {
1261 header('Location: '.$url);
1267 * Provides exception handlers for this script
1269 class exception_handlers
{
1272 * Sets the exception handler
1275 * @param string $handler name
1277 public static function set_handler($handler) {
1279 if (PHP_SAPI
=== 'cli') {
1280 // No custom handler available for CLI mode.
1281 set_exception_handler(null);
1285 set_exception_handler('exception_handlers::'.$handler.'_exception_handler');
1289 * Returns the text describing the thrown exception
1291 * By default, PHP displays full path to scripts when the exception is thrown. In order to prevent
1292 * sensitive information leak (and yes, the path to scripts at a web server _is_ sensitive information)
1293 * the path to scripts is removed from the message.
1295 * @param Exception $e thrown exception
1298 public static function format_exception_info(Exception
$e) {
1300 $mydir = dirname(__FILE__
).'/';
1301 $text = $e->__toString();
1302 $text = str_replace($mydir, '', $text);
1307 * Very basic exception handler
1309 * @param Exception $e uncaught exception
1311 public static function bootstrap_exception_handler(Exception
$e) {
1312 echo('<h1>Oops! It did it again</h1>');
1313 echo('<p><strong>Moodle deployment utility had a trouble with your request. See the debugging information for more details.</strong></p>');
1315 echo self
::format_exception_info($e);
1320 * Default exception handler
1322 * When this handler is used, input_manager and output_manager singleton instances already
1323 * exist in the memory and can be used.
1325 * @param Exception $e uncaught exception
1327 public static function default_exception_handler(Exception
$e) {
1329 $worker = worker
::instance();
1330 $worker->log_exception($e);
1332 $output = output_manager
::instance();
1333 $output->exception($e);
1337 ////////////////////////////////////////////////////////////////////////////////
1339 // Check if the script is actually executed or if it was just included by someone
1340 // else - typically by the PHPUnit. This is a PHP alternative to the Python's
1341 // if __name__ == '__main__'
1342 if (!debug_backtrace()) {
1343 // We are executed by the SAPI.
1344 exception_handlers
::set_handler('bootstrap');
1345 // Initialize the worker class to actually make the job.
1346 $worker = worker
::instance();
1347 exception_handlers
::set_handler('default');
1349 // Lights, Camera, Action!
1353 // We are included - probably by some unit testing framework. Do nothing.