Moodle release 2.7.4
[moodle.git] / mdeploy.php
blobad42d99ed19b2911c7e16276422aaf081e37625c
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Moodle deployment utility
21 * This script looks after deploying new add-ons and available updates for them
22 * to the local Moodle site. It can operate via both HTTP and CLI mode.
23 * Moodle itself calls this utility via the HTTP mode when the admin is about to
24 * install or update an add-on. You can use the CLI mode in your custom deployment
25 * shell scripts.
27 * CLI usage example:
29 * $ sudo -u apache php mdeploy.php --install \
30 * --package=https://moodle.org/plugins/download.php/...zip \
31 * --typeroot=/var/www/moodle/htdocs/blocks
32 * --name=loancalc
33 * --md5=...
34 * --dataroot=/var/www/moodle/data
36 * $ sudo -u apache php mdeploy.php --upgrade \
37 * --package=https://moodle.org/plugins/download.php/...zip \
38 * --typeroot=/var/www/moodle/htdocs/blocks
39 * --name=loancalc
40 * --md5=...
41 * --dataroot=/var/www/moodle/data
43 * When called via HTTP, additional parameters returnurl, passfile and password must be
44 * provided. Optional proxy configuration can be passed using parameters proxy, proxytype
45 * and proxyuserpwd.
47 * Changes
49 * 1.1 - Added support to install a new plugin from the Moodle Plugins directory.
50 * 1.0 - Initial version used in Moodle 2.4 to deploy available updates.
52 * @package core
53 * @subpackage mdeploy
54 * @version 1.1
55 * @copyright 2012 David Mudrak <david@moodle.com>
56 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59 if (defined('MOODLE_INTERNAL')) {
60 die('This is a standalone utility that should not be included by any other Moodle code.');
64 // Exceptions //////////////////////////////////////////////////////////////////
66 class invalid_coding_exception extends Exception {}
67 class missing_option_exception extends Exception {}
68 class invalid_option_exception extends Exception {}
69 class unauthorized_access_exception extends Exception {}
70 class download_file_exception extends Exception {}
71 class backup_folder_exception extends Exception {}
72 class zip_exception extends Exception {}
73 class filesystem_exception extends Exception {}
74 class checksum_exception extends Exception {}
77 // Various support classes /////////////////////////////////////////////////////
79 /**
80 * Base class implementing the singleton pattern using late static binding feature.
82 * @copyright 2012 David Mudrak <david@moodle.com>
83 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
85 abstract class singleton_pattern {
87 /** @var array singleton_pattern instances */
88 protected static $singletoninstances = array();
90 /**
91 * Factory method returning the singleton instance.
93 * Subclasses may want to override the {@link self::initialize()} method that is
94 * called right after their instantiation.
96 * @return mixed the singleton instance
98 final public static function instance() {
99 $class = get_called_class();
100 if (!isset(static::$singletoninstances[$class])) {
101 static::$singletoninstances[$class] = new static();
102 static::$singletoninstances[$class]->initialize();
104 return static::$singletoninstances[$class];
108 * Optional post-instantiation code.
110 protected function initialize() {
111 // Do nothing in this base class.
115 * Direct instantiation not allowed, use the factory method {@link instance()}
117 final protected function __construct() {
121 * Sorry, this is singleton.
123 final protected function __clone() {
128 // User input handling /////////////////////////////////////////////////////////
131 * Provides access to the script options.
133 * Implements the delegate pattern by dispatching the calls to appropriate
134 * helper class (CLI or HTTP).
136 * @copyright 2012 David Mudrak <david@moodle.com>
137 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
139 class input_manager extends singleton_pattern {
141 const TYPE_FILE = 'file'; // File name
142 const TYPE_FLAG = 'flag'; // No value, just a flag (switch)
143 const TYPE_INT = 'int'; // Integer
144 const TYPE_PATH = 'path'; // Full path to a file or a directory
145 const TYPE_RAW = 'raw'; // Raw value, keep as is
146 const TYPE_URL = 'url'; // URL to a file
147 const TYPE_PLUGIN = 'plugin'; // Plugin name
148 const TYPE_MD5 = 'md5'; // MD5 hash
150 /** @var input_cli_provider|input_http_provider the provider of the input */
151 protected $inputprovider = null;
154 * Returns the value of an option passed to the script.
156 * If the caller passes just the $name, the requested argument is considered
157 * required. The caller may specify the second argument which then
158 * makes the argument optional with the given default value.
160 * If the type of the $name option is TYPE_FLAG (switch), this method returns
161 * true if the flag has been passed or false if it was not. Specifying the
162 * default value makes no sense in this case and leads to invalid coding exception.
164 * The array options are not supported.
166 * @example $filename = $input->get_option('f');
167 * @example $filename = $input->get_option('filename');
168 * @example if ($input->get_option('verbose')) { ... }
169 * @param string $name
170 * @return mixed
172 public function get_option($name, $default = 'provide_default_value_explicitly') {
174 $this->validate_option_name($name);
176 $info = $this->get_option_info($name);
178 if ($info->type === input_manager::TYPE_FLAG) {
179 return $this->inputprovider->has_option($name);
182 if (func_num_args() == 1) {
183 return $this->get_required_option($name);
184 } else {
185 return $this->get_optional_option($name, $default);
190 * Returns the meta-information about the given option.
192 * @param string|null $name short or long option name, defaults to returning the list of all
193 * @return array|object|false array with all, object with the specific option meta-information or false of no such an option
195 public function get_option_info($name=null) {
197 $supportedoptions = array(
198 array('', 'passfile', input_manager::TYPE_FILE, 'File name of the passphrase file (HTTP access only)'),
199 array('', 'password', input_manager::TYPE_RAW, 'Session passphrase (HTTP access only)'),
200 array('', 'proxy', input_manager::TYPE_RAW, 'HTTP proxy host and port (e.g. \'our.proxy.edu:8888\')'),
201 array('', 'proxytype', input_manager::TYPE_RAW, 'Proxy type (HTTP or SOCKS5)'),
202 array('', 'proxyuserpwd', input_manager::TYPE_RAW, 'Proxy username and password (e.g. \'username:password\')'),
203 array('', 'returnurl', input_manager::TYPE_URL, 'Return URL (HTTP access only)'),
204 array('d', 'dataroot', input_manager::TYPE_PATH, 'Full path to the dataroot (moodledata) directory'),
205 array('h', 'help', input_manager::TYPE_FLAG, 'Prints usage information'),
206 array('i', 'install', input_manager::TYPE_FLAG, 'Installation mode'),
207 array('m', 'md5', input_manager::TYPE_MD5, 'Expected MD5 hash of the ZIP package to deploy'),
208 array('n', 'name', input_manager::TYPE_PLUGIN, 'Plugin name (the name of its folder)'),
209 array('p', 'package', input_manager::TYPE_URL, 'URL to the ZIP package to deploy'),
210 array('r', 'typeroot', input_manager::TYPE_PATH, 'Full path of the container for this plugin type'),
211 array('u', 'upgrade', input_manager::TYPE_FLAG, 'Upgrade mode'),
214 if (is_null($name)) {
215 $all = array();
216 foreach ($supportedoptions as $optioninfo) {
217 $info = new stdClass();
218 $info->shortname = $optioninfo[0];
219 $info->longname = $optioninfo[1];
220 $info->type = $optioninfo[2];
221 $info->desc = $optioninfo[3];
222 $all[] = $info;
224 return $all;
227 $found = false;
229 foreach ($supportedoptions as $optioninfo) {
230 if (strlen($name) == 1) {
231 // Search by the short option name
232 if ($optioninfo[0] === $name) {
233 $found = $optioninfo;
234 break;
236 } else {
237 // Search by the long option name
238 if ($optioninfo[1] === $name) {
239 $found = $optioninfo;
240 break;
245 if (!$found) {
246 return false;
249 $info = new stdClass();
250 $info->shortname = $found[0];
251 $info->longname = $found[1];
252 $info->type = $found[2];
253 $info->desc = $found[3];
255 return $info;
259 * Casts the value to the given type.
261 * @param mixed $raw the raw value
262 * @param string $type the expected value type, e.g. {@link input_manager::TYPE_INT}
263 * @return mixed
265 public function cast_value($raw, $type) {
267 if (is_array($raw)) {
268 throw new invalid_coding_exception('Unsupported array option.');
269 } else if (is_object($raw)) {
270 throw new invalid_coding_exception('Unsupported object option.');
273 switch ($type) {
275 case input_manager::TYPE_FILE:
276 $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\':\\\\/]~u', '', $raw);
277 $raw = preg_replace('~\.\.+~', '', $raw);
278 if ($raw === '.') {
279 $raw = '';
281 return $raw;
283 case input_manager::TYPE_FLAG:
284 return true;
286 case input_manager::TYPE_INT:
287 return (int)$raw;
289 case input_manager::TYPE_PATH:
290 if (strpos($raw, '~') !== false) {
291 throw new invalid_option_exception('Using the tilde (~) character in paths is not supported');
293 $colonpos = strpos($raw, ':');
294 if ($colonpos !== false) {
295 if ($colonpos !== 1 or strrpos($raw, ':') !== 1) {
296 throw new invalid_option_exception('Using the colon (:) character in paths is supported for Windows drive labels only.');
298 if (preg_match('/^[a-zA-Z]:/', $raw) !== 1) {
299 throw new invalid_option_exception('Using the colon (:) character in paths is supported for Windows drive labels only.');
302 $raw = str_replace('\\', '/', $raw);
303 $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\']~u', '', $raw);
304 $raw = preg_replace('~\.\.+~', '', $raw);
305 $raw = preg_replace('~//+~', '/', $raw);
306 $raw = preg_replace('~/(\./)+~', '/', $raw);
307 return $raw;
309 case input_manager::TYPE_RAW:
310 return $raw;
312 case input_manager::TYPE_URL:
313 $regex = '^(https?|ftp)\:\/\/'; // protocol
314 $regex .= '([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?'; // optional user and password
315 $regex .= '[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*'; // hostname or IP (one word like http://localhost/ allowed)
316 $regex .= '(\:[0-9]{2,5})?'; // port (optional)
317 $regex .= '(\/([a-z0-9+\$_-]\.?)+)*\/?'; // path to the file
318 $regex .= '(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?'; // HTTP params
320 if (preg_match('#'.$regex.'#i', $raw)) {
321 return $raw;
322 } else {
323 throw new invalid_option_exception('Not a valid URL');
326 case input_manager::TYPE_PLUGIN:
327 if (!preg_match('/^[a-z][a-z0-9_]*[a-z0-9]$/', $raw)) {
328 throw new invalid_option_exception('Invalid plugin name');
330 if (strpos($raw, '__') !== false) {
331 throw new invalid_option_exception('Invalid plugin name');
333 return $raw;
335 case input_manager::TYPE_MD5:
336 if (!preg_match('/^[a-f0-9]{32}$/', $raw)) {
337 throw new invalid_option_exception('Invalid MD5 hash format');
339 return $raw;
341 default:
342 throw new invalid_coding_exception('Unknown option type.');
348 * Picks the appropriate helper class to delegate calls to.
350 protected function initialize() {
351 if (PHP_SAPI === 'cli') {
352 $this->inputprovider = input_cli_provider::instance();
353 } else {
354 $this->inputprovider = input_http_provider::instance();
358 // End of external API
361 * Validates the parameter name.
363 * @param string $name
364 * @throws invalid_coding_exception
366 protected function validate_option_name($name) {
368 if (empty($name)) {
369 throw new invalid_coding_exception('Invalid empty option name.');
372 $meta = $this->get_option_info($name);
373 if (empty($meta)) {
374 throw new invalid_coding_exception('Invalid option name: '.$name);
379 * Returns cleaned option value or throws exception.
381 * @param string $name the name of the parameter
382 * @param string $type the parameter type, e.g. {@link input_manager::TYPE_INT}
383 * @return mixed
385 protected function get_required_option($name) {
386 if ($this->inputprovider->has_option($name)) {
387 return $this->inputprovider->get_option($name);
388 } else {
389 throw new missing_option_exception('Missing required option: '.$name);
394 * Returns cleaned option value or the default value
396 * @param string $name the name of the parameter
397 * @param string $type the parameter type, e.g. {@link input_manager::TYPE_INT}
398 * @param mixed $default the default value.
399 * @return mixed
401 protected function get_optional_option($name, $default) {
402 if ($this->inputprovider->has_option($name)) {
403 return $this->inputprovider->get_option($name);
404 } else {
405 return $default;
412 * Base class for input providers.
414 * @copyright 2012 David Mudrak <david@moodle.com>
415 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
417 abstract class input_provider extends singleton_pattern {
419 /** @var array list of all passed valid options */
420 protected $options = array();
423 * Returns the casted value of the option.
425 * @param string $name option name
426 * @throws invalid_coding_exception if the option has not been passed
427 * @return mixed casted value of the option
429 public function get_option($name) {
431 if (!$this->has_option($name)) {
432 throw new invalid_coding_exception('Option not passed: '.$name);
435 return $this->options[$name];
439 * Was the given option passed?
441 * @param string $name optionname
442 * @return bool
444 public function has_option($name) {
445 return array_key_exists($name, $this->options);
449 * Initializes the input provider.
451 protected function initialize() {
452 $this->populate_options();
455 // End of external API
458 * Parses and validates all supported options passed to the script.
460 protected function populate_options() {
462 $input = input_manager::instance();
463 $raw = $this->parse_raw_options();
464 $cooked = array();
466 foreach ($raw as $k => $v) {
467 if (is_array($v) or is_object($v)) {
468 // Not supported.
471 $info = $input->get_option_info($k);
472 if (!$info) {
473 continue;
476 $casted = $input->cast_value($v, $info->type);
478 if (!empty($info->shortname)) {
479 $cooked[$info->shortname] = $casted;
482 if (!empty($info->longname)) {
483 $cooked[$info->longname] = $casted;
487 // Store the options.
488 $this->options = $cooked;
494 * Provides access to the script options passed via CLI.
496 * @copyright 2012 David Mudrak <david@moodle.com>
497 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
499 class input_cli_provider extends input_provider {
502 * Parses raw options passed to the script.
504 * @return array as returned by getopt()
506 protected function parse_raw_options() {
508 $input = input_manager::instance();
510 // Signatures of some in-built PHP functions are just crazy, aren't they.
511 $short = '';
512 $long = array();
514 foreach ($input->get_option_info() as $option) {
515 if ($option->type === input_manager::TYPE_FLAG) {
516 // No value expected for this option.
517 $short .= $option->shortname;
518 $long[] = $option->longname;
519 } else {
520 // A value expected for the option, all considered as optional.
521 $short .= empty($option->shortname) ? '' : $option->shortname.'::';
522 $long[] = empty($option->longname) ? '' : $option->longname.'::';
526 return getopt($short, $long);
532 * Provides access to the script options passed via HTTP request.
534 * @copyright 2012 David Mudrak <david@moodle.com>
535 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
537 class input_http_provider extends input_provider {
540 * Parses raw options passed to the script.
542 * @return array of raw values passed via HTTP request
544 protected function parse_raw_options() {
545 return $_POST;
550 // Output handling /////////////////////////////////////////////////////////////
553 * Provides output operations.
555 * @copyright 2012 David Mudrak <david@moodle.com>
556 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
558 class output_manager extends singleton_pattern {
560 /** @var output_cli_provider|output_http_provider the provider of the output functionality */
561 protected $outputprovider = null;
564 * Magic method triggered when invoking an inaccessible method.
566 * @param string $name method name
567 * @param array $arguments method arguments
569 public function __call($name, array $arguments = array()) {
570 call_user_func_array(array($this->outputprovider, $name), $arguments);
574 * Picks the appropriate helper class to delegate calls to.
576 protected function initialize() {
577 if (PHP_SAPI === 'cli') {
578 $this->outputprovider = output_cli_provider::instance();
579 } else {
580 $this->outputprovider = output_http_provider::instance();
587 * Base class for all output providers.
589 * @copyright 2012 David Mudrak <david@moodle.com>
590 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
592 abstract class output_provider extends singleton_pattern {
596 * Provides output to the command line.
598 * @copyright 2012 David Mudrak <david@moodle.com>
599 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
601 class output_cli_provider extends output_provider {
604 * Prints help information in CLI mode.
606 public function help() {
608 $this->outln('mdeploy.php - Moodle (http://moodle.org) deployment utility');
609 $this->outln();
610 $this->outln('Usage: $ sudo -u apache php mdeploy.php [options]');
611 $this->outln();
612 $input = input_manager::instance();
613 foreach($input->get_option_info() as $info) {
614 $option = array();
615 if (!empty($info->shortname)) {
616 $option[] = '-'.$info->shortname;
618 if (!empty($info->longname)) {
619 $option[] = '--'.$info->longname;
621 $this->outln(sprintf('%-20s %s', implode(', ', $option), $info->desc));
625 // End of external API
628 * Writes a text to the STDOUT followed by a new line character.
630 * @param string $text text to print
632 protected function outln($text='') {
633 fputs(STDOUT, $text.PHP_EOL);
639 * Provides HTML output as a part of HTTP response.
641 * @copyright 2012 David Mudrak <david@moodle.com>
642 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
644 class output_http_provider extends output_provider {
647 * Prints help on the script usage.
649 public function help() {
650 // No help available via HTTP
654 * Display the information about uncaught exception
656 * @param Exception $e uncaught exception
658 public function exception(Exception $e) {
660 $docslink = 'http://docs.moodle.org/en/admin/mdeploy/'.get_class($e);
661 $this->start_output();
662 echo('<h1>Oops! It did it again</h1>');
663 echo('<p><strong>Moodle deployment utility had a trouble with your request.
664 See <a href="'.$docslink.'">the docs page</a> and the debugging information for more details.</strong></p>');
665 echo('<pre>');
666 echo exception_handlers::format_exception_info($e);
667 echo('</pre>');
668 $this->end_output();
671 // End of external API
674 * Produce the HTML page header
676 protected function start_output() {
677 echo '<!doctype html>
678 <html lang="en">
679 <head>
680 <meta charset="utf-8">
681 <style type="text/css">
682 body {background-color:#666;font-family:"DejaVu Sans","Liberation Sans",Freesans,sans-serif;}
683 h1 {text-align:center;}
684 pre {white-space: pre-wrap;}
685 #page {background-color:#eee;width:1024px;margin:5em auto;border:3px solid #333;border-radius: 15px;padding:1em;}
686 </style>
687 </head>
688 <body>
689 <div id="page">';
693 * Produce the HTML page footer
695 protected function end_output() {
696 echo '</div></body></html>';
700 // The main class providing all the functionality //////////////////////////////
703 * The actual worker class implementing the main functionality of the script.
705 * @copyright 2012 David Mudrak <david@moodle.com>
706 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
708 class worker extends singleton_pattern {
710 const EXIT_OK = 0; // Success exit code.
711 const EXIT_HELP = 1; // Explicit help required.
712 const EXIT_UNKNOWN_ACTION = 127; // Neither -i nor -u provided.
714 /** @var input_manager */
715 protected $input = null;
717 /** @var output_manager */
718 protected $output = null;
720 /** @var int the most recent cURL error number, zero for no error */
721 private $curlerrno = null;
723 /** @var string the most recent cURL error message, empty string for no error */
724 private $curlerror = null;
726 /** @var array|false the most recent cURL request info, if it was successful */
727 private $curlinfo = null;
729 /** @var string the full path to the log file */
730 private $logfile = null;
733 * Main - the one that actually does something
735 public function execute() {
737 $this->log('=== MDEPLOY EXECUTION START ===');
739 // Authorize access. None in CLI. Passphrase in HTTP.
740 $this->authorize();
742 // Asking for help in the CLI mode.
743 if ($this->input->get_option('help')) {
744 $this->output->help();
745 $this->done(self::EXIT_HELP);
748 if ($this->input->get_option('upgrade')) {
749 $this->log('Plugin upgrade requested');
751 // Fetch the ZIP file into a temporary location.
752 $source = $this->input->get_option('package');
753 $target = $this->target_location($source);
754 $this->log('Downloading package '.$source);
756 if ($this->download_file($source, $target)) {
757 $this->log('Package downloaded into '.$target);
758 } else {
759 $this->log('cURL error ' . $this->curlerrno . ' ' . $this->curlerror);
760 $this->log('Unable to download the file from ' . $source . ' into ' . $target);
761 throw new download_file_exception('Unable to download the package');
764 // Compare MD5 checksum of the ZIP file
765 $md5remote = $this->input->get_option('md5');
766 $md5local = md5_file($target);
768 if ($md5local !== $md5remote) {
769 $this->log('MD5 checksum failed. Expected: '.$md5remote.' Got: '.$md5local);
770 throw new checksum_exception('MD5 checksum failed');
772 $this->log('MD5 checksum ok');
774 // Backup the current version of the plugin
775 $plugintyperoot = $this->input->get_option('typeroot');
776 $pluginname = $this->input->get_option('name');
777 $sourcelocation = $plugintyperoot.'/'.$pluginname;
778 $backuplocation = $this->backup_location($sourcelocation);
780 $this->log('Current plugin code location: '.$sourcelocation);
781 $this->log('Moving the current code into archive: '.$backuplocation);
783 if (file_exists($sourcelocation)) {
784 // We don't want to touch files unless we are pretty sure it would be all ok.
785 if (!$this->move_directory_source_precheck($sourcelocation)) {
786 throw new backup_folder_exception('Unable to backup the current version of the plugin (source precheck failed)');
788 if (!$this->move_directory_target_precheck($backuplocation)) {
789 throw new backup_folder_exception('Unable to backup the current version of the plugin (backup precheck failed)');
792 // Looking good, let's try it.
793 if (!$this->move_directory($sourcelocation, $backuplocation, true)) {
794 throw new backup_folder_exception('Unable to backup the current version of the plugin (moving failed)');
797 } else {
798 // Upgrading missing plugin - this happens often during upgrades.
799 if (!$this->create_directory_precheck($sourcelocation)) {
800 throw new filesystem_exception('Unable to prepare the plugin location (cannot create new directory)');
804 // Unzip the plugin package file into the target location.
805 $this->unzip_plugin($target, $plugintyperoot, $sourcelocation, $backuplocation);
806 $this->log('Package successfully extracted');
808 // Redirect to the given URL (in HTTP) or exit (in CLI).
809 $this->done();
811 } else if ($this->input->get_option('install')) {
812 $this->log('Plugin installation requested');
814 $plugintyperoot = $this->input->get_option('typeroot');
815 $pluginname = $this->input->get_option('name');
816 $source = $this->input->get_option('package');
817 $md5remote = $this->input->get_option('md5');
819 // Check if the plugin location if available for us.
820 $pluginlocation = $plugintyperoot.'/'.$pluginname;
822 $this->log('New plugin code location: '.$pluginlocation);
824 if (file_exists($pluginlocation)) {
825 throw new filesystem_exception('Unable to prepare the plugin location (directory already exists)');
828 if (!$this->create_directory_precheck($pluginlocation)) {
829 throw new filesystem_exception('Unable to prepare the plugin location (cannot create new directory)');
832 // Fetch the ZIP file into a temporary location.
833 $target = $this->target_location($source);
834 $this->log('Downloading package '.$source);
836 if ($this->download_file($source, $target)) {
837 $this->log('Package downloaded into '.$target);
838 } else {
839 $this->log('cURL error ' . $this->curlerrno . ' ' . $this->curlerror);
840 $this->log('Unable to download the file');
841 throw new download_file_exception('Unable to download the package');
844 // Compare MD5 checksum of the ZIP file
845 $md5local = md5_file($target);
847 if ($md5local !== $md5remote) {
848 $this->log('MD5 checksum failed. Expected: '.$md5remote.' Got: '.$md5local);
849 throw new checksum_exception('MD5 checksum failed');
851 $this->log('MD5 checksum ok');
853 // Unzip the plugin package file into the plugin location.
854 $this->unzip_plugin($target, $plugintyperoot, $pluginlocation, false);
855 $this->log('Package successfully extracted');
857 // Redirect to the given URL (in HTTP) or exit (in CLI).
858 $this->done();
861 // Print help in CLI by default.
862 $this->output->help();
863 $this->done(self::EXIT_UNKNOWN_ACTION);
867 * Attempts to log a thrown exception
869 * @param Exception $e uncaught exception
871 public function log_exception(Exception $e) {
872 $this->log($e->__toString());
876 * Initialize the worker class.
878 protected function initialize() {
879 $this->input = input_manager::instance();
880 $this->output = output_manager::instance();
883 // End of external API
886 * Finish this script execution.
888 * @param int $exitcode
890 protected function done($exitcode = self::EXIT_OK) {
892 if (PHP_SAPI === 'cli') {
893 exit($exitcode);
895 } else {
896 $returnurl = $this->input->get_option('returnurl');
897 $this->redirect($returnurl);
898 exit($exitcode);
903 * Authorize access to the script.
905 * In CLI mode, the access is automatically authorized. In HTTP mode, the
906 * passphrase submitted via the request params must match the contents of the
907 * file, the name of which is passed in another parameter.
909 * @throws unauthorized_access_exception
911 protected function authorize() {
913 if (PHP_SAPI === 'cli') {
914 $this->log('Successfully authorized using the CLI SAPI');
915 return;
918 $dataroot = $this->input->get_option('dataroot');
919 $passfile = $this->input->get_option('passfile');
920 $password = $this->input->get_option('password');
922 $passpath = $dataroot.'/mdeploy/auth/'.$passfile;
924 if (!is_readable($passpath)) {
925 throw new unauthorized_access_exception('Unable to read the passphrase file.');
928 $stored = file($passpath, FILE_IGNORE_NEW_LINES);
930 // "This message will self-destruct in five seconds." -- Mission Commander Swanbeck, Mission: Impossible II
931 unlink($passpath);
933 if (is_readable($passpath)) {
934 throw new unauthorized_access_exception('Unable to remove the passphrase file.');
937 if (count($stored) < 2) {
938 throw new unauthorized_access_exception('Invalid format of the passphrase file.');
941 if (time() - (int)$stored[1] > 30 * 60) {
942 throw new unauthorized_access_exception('Passphrase timeout.');
945 if (strlen($stored[0]) < 24) {
946 throw new unauthorized_access_exception('Session passphrase not long enough.');
949 if ($password !== $stored[0]) {
950 throw new unauthorized_access_exception('Session passphrase does not match the stored one.');
953 $this->log('Successfully authorized using the passphrase file');
957 * Returns the full path to the log file.
959 * @return string
961 protected function log_location() {
963 if (!is_null($this->logfile)) {
964 return $this->logfile;
967 $dataroot = $this->input->get_option('dataroot', '');
969 if (empty($dataroot)) {
970 $this->logfile = false;
971 return $this->logfile;
974 $myroot = $dataroot.'/mdeploy';
976 if (!is_dir($myroot)) {
977 mkdir($myroot, 02777, true);
980 $this->logfile = $myroot.'/mdeploy.log';
981 return $this->logfile;
985 * Choose the target location for the given ZIP's URL.
987 * @param string $source URL
988 * @return string
990 protected function target_location($source) {
992 $dataroot = $this->input->get_option('dataroot');
993 $pool = $dataroot.'/mdeploy/var';
995 if (!is_dir($pool)) {
996 mkdir($pool, 02777, true);
999 $target = $pool.'/'.md5($source);
1001 $suffix = 0;
1002 while (file_exists($target.'.'.$suffix.'.zip')) {
1003 $suffix++;
1006 return $target.'.'.$suffix.'.zip';
1010 * Choose the location of the current plugin folder backup
1012 * @param string $path full path to the current folder
1013 * @return string
1015 protected function backup_location($path) {
1017 $dataroot = $this->input->get_option('dataroot');
1018 $pool = $dataroot.'/mdeploy/archive';
1020 if (!is_dir($pool)) {
1021 mkdir($pool, 02777, true);
1024 $target = $pool.'/'.basename($path).'_'.time();
1026 $suffix = 0;
1027 while (file_exists($target.'.'.$suffix)) {
1028 $suffix++;
1031 return $target.'.'.$suffix;
1035 * Downloads the given file into the given destination.
1037 * This is basically a simplified version of {@link download_file_content()} from
1038 * Moodle itself, tuned for fetching files from moodle.org servers.
1040 * @param string $source file url starting with http(s)://
1041 * @param string $target store the downloaded content to this file (full path)
1042 * @return bool true on success, false otherwise
1043 * @throws download_file_exception
1045 protected function download_file($source, $target) {
1047 $newlines = array("\r", "\n");
1048 $source = str_replace($newlines, '', $source);
1049 if (!preg_match('|^https?://|i', $source)) {
1050 throw new download_file_exception('Unsupported transport protocol.');
1052 if (!$ch = curl_init($source)) {
1053 $this->log('Unable to init cURL.');
1054 return false;
1057 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // verify the peer's certificate
1058 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // check the existence of a common name and also verify that it matches the hostname provided
1059 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the transfer as a string
1060 curl_setopt($ch, CURLOPT_HEADER, false); // don't include the header in the output
1061 curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
1062 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); // nah, moodle.org is never unavailable! :-p
1063 curl_setopt($ch, CURLOPT_URL, $source);
1064 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Allow redirection, we trust in ssl.
1065 curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
1067 if ($cacertfile = $this->get_cacert()) {
1068 // Do not use CA certs provided by the operating system. Instead,
1069 // use this CA cert to verify the ZIP provider.
1070 $this->log('Using custom CA certificate '.$cacertfile);
1071 curl_setopt($ch, CURLOPT_CAINFO, $cacertfile);
1072 } else {
1073 $this->log('Using operating system CA certificates.');
1076 $proxy = $this->input->get_option('proxy', false);
1077 if (!empty($proxy)) {
1078 curl_setopt($ch, CURLOPT_PROXY, $proxy);
1080 $proxytype = $this->input->get_option('proxytype', false);
1081 if (strtoupper($proxytype) === 'SOCKS5') {
1082 $this->log('Using SOCKS5 proxy');
1083 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1084 } else if (!empty($proxytype)) {
1085 $this->log('Using HTTP proxy');
1086 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
1087 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
1090 $proxyuserpwd = $this->input->get_option('proxyuserpwd', false);
1091 if (!empty($proxyuserpwd)) {
1092 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyuserpwd);
1093 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
1097 $targetfile = fopen($target, 'w');
1099 if (!$targetfile) {
1100 throw new download_file_exception('Unable to create local file '.$target);
1103 curl_setopt($ch, CURLOPT_FILE, $targetfile);
1105 $result = curl_exec($ch);
1107 // try to detect encoding problems
1108 if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
1109 curl_setopt($ch, CURLOPT_ENCODING, 'none');
1110 $result = curl_exec($ch);
1113 fclose($targetfile);
1115 $this->curlerrno = curl_errno($ch);
1116 $this->curlerror = curl_error($ch);
1117 $this->curlinfo = curl_getinfo($ch);
1119 if (!$result or $this->curlerrno) {
1120 $this->log('Curl Error.');
1121 return false;
1123 } else if (is_array($this->curlinfo) and (empty($this->curlinfo['http_code']) or ($this->curlinfo['http_code'] != 200))) {
1124 $this->log('Curl remote error.');
1125 $this->log(print_r($this->curlinfo,true));
1126 return false;
1129 return true;
1133 * Get the location of ca certificates.
1134 * @return string absolute file path or empty if default used
1136 protected function get_cacert() {
1137 $dataroot = $this->input->get_option('dataroot');
1139 // Bundle in dataroot always wins.
1140 if (is_readable($dataroot.'/moodleorgca.crt')) {
1141 return realpath($dataroot.'/moodleorgca.crt');
1144 // Next comes the default from php.ini
1145 $cacert = ini_get('curl.cainfo');
1146 if (!empty($cacert) and is_readable($cacert)) {
1147 return realpath($cacert);
1150 // Windows PHP does not have any certs, we need to use something.
1151 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
1152 if (is_readable(__DIR__.'/lib/cacert.pem')) {
1153 return realpath(__DIR__.'/lib/cacert.pem');
1157 // Use default, this should work fine on all properly configured *nix systems.
1158 return null;
1162 * Log a message
1164 * @param string $message
1166 protected function log($message) {
1168 $logpath = $this->log_location();
1170 if (empty($logpath)) {
1171 // no logging available
1172 return;
1175 $f = fopen($logpath, 'ab');
1177 if ($f === false) {
1178 throw new filesystem_exception('Unable to open the log file for appending');
1181 $message = $this->format_log_message($message);
1183 fwrite($f, $message);
1185 fclose($f);
1189 * Prepares the log message for writing into the file
1191 * @param string $msg
1192 * @return string
1194 protected function format_log_message($msg) {
1196 $msg = trim($msg);
1197 $timestamp = date("Y-m-d H:i:s");
1199 return $timestamp . ' '. $msg . PHP_EOL;
1203 * Checks to see if the given source could be safely moved into a new location
1205 * @param string $source full path to the existing directory
1206 * @return bool
1208 protected function move_directory_source_precheck($source) {
1210 if (!is_writable($source)) {
1211 return false;
1214 if (is_dir($source)) {
1215 $handle = opendir($source);
1216 } else {
1217 return false;
1220 $result = true;
1222 while ($filename = readdir($handle)) {
1223 $sourcepath = $source.'/'.$filename;
1225 if ($filename === '.' or $filename === '..') {
1226 continue;
1229 if (is_dir($sourcepath)) {
1230 $result = $result && $this->move_directory_source_precheck($sourcepath);
1232 } else {
1233 $result = $result && is_writable($sourcepath);
1237 closedir($handle);
1239 return $result;
1243 * Checks to see if a source folder could be safely moved into the given new location
1245 * @param string $destination full path to the new expected location of a folder
1246 * @return bool
1248 protected function move_directory_target_precheck($target) {
1250 // Check if the target folder does not exist yet, can be created
1251 // and removed again.
1252 $result = $this->create_directory_precheck($target);
1254 // At the moment, it seems to be enough to check. We may want to add
1255 // more steps in the future.
1257 return $result;
1261 * Make sure the given directory can be created (and removed)
1263 * @param string $path full path to the folder
1264 * @return bool
1266 protected function create_directory_precheck($path) {
1268 if (file_exists($path)) {
1269 return false;
1272 $result = mkdir($path, 02777) && rmdir($path);
1274 return $result;
1278 * Moves the given source into a new location recursively
1280 * The target location can not exist.
1282 * @param string $source full path to the existing directory
1283 * @param string $destination full path to the new location of the folder
1284 * @param bool $keepsourceroot should the root of the $source be kept or removed at the end
1285 * @return bool
1287 protected function move_directory($source, $target, $keepsourceroot = false) {
1289 if (file_exists($target)) {
1290 throw new filesystem_exception('Unable to move the directory - target location already exists');
1293 return $this->move_directory_into($source, $target, $keepsourceroot);
1297 * Moves the given source into a new location recursively
1299 * If the target already exists, files are moved into it. The target is created otherwise.
1301 * @param string $source full path to the existing directory
1302 * @param string $destination full path to the new location of the folder
1303 * @param bool $keepsourceroot should the root of the $source be kept or removed at the end
1304 * @return bool
1306 protected function move_directory_into($source, $target, $keepsourceroot = false) {
1308 if (is_dir($source)) {
1309 $handle = opendir($source);
1310 } else {
1311 throw new filesystem_exception('Source location is not a directory');
1314 if (is_dir($target)) {
1315 $result = true;
1316 } else {
1317 $result = mkdir($target, 02777);
1320 while ($filename = readdir($handle)) {
1321 $sourcepath = $source.'/'.$filename;
1322 $targetpath = $target.'/'.$filename;
1324 if ($filename === '.' or $filename === '..') {
1325 continue;
1328 if (is_dir($sourcepath)) {
1329 $result = $result && $this->move_directory($sourcepath, $targetpath, false);
1331 } else {
1332 $result = $result && rename($sourcepath, $targetpath);
1336 closedir($handle);
1338 if (!$keepsourceroot) {
1339 $result = $result && rmdir($source);
1342 clearstatcache();
1344 return $result;
1348 * Deletes the given directory recursively
1350 * @param string $path full path to the directory
1351 * @param bool $keeppathroot should the root of the $path be kept (i.e. remove the content only) or removed too
1352 * @return bool
1354 protected function remove_directory($path, $keeppathroot = false) {
1356 $result = true;
1358 if (!file_exists($path)) {
1359 return $result;
1362 if (is_dir($path)) {
1363 $handle = opendir($path);
1364 } else {
1365 throw new filesystem_exception('Given path is not a directory');
1368 while ($filename = readdir($handle)) {
1369 $filepath = $path.'/'.$filename;
1371 if ($filename === '.' or $filename === '..') {
1372 continue;
1375 if (is_dir($filepath)) {
1376 $result = $result && $this->remove_directory($filepath, false);
1378 } else {
1379 $result = $result && unlink($filepath);
1383 closedir($handle);
1385 if (!$keeppathroot) {
1386 $result = $result && rmdir($path);
1389 clearstatcache();
1391 return $result;
1395 * Unzip the file obtained from the Plugins directory to this site
1397 * @param string $ziplocation full path to the ZIP file
1398 * @param string $plugintyperoot full path to the plugin's type location
1399 * @param string $expectedlocation expected full path to the plugin after it is extracted
1400 * @param string|bool $backuplocation location of the previous version of the plugin or false for no backup
1402 protected function unzip_plugin($ziplocation, $plugintyperoot, $expectedlocation, $backuplocation) {
1404 $zip = new ZipArchive();
1405 $result = $zip->open($ziplocation);
1407 if ($result !== true) {
1408 if ($backuplocation !== false) {
1409 $this->move_directory($backuplocation, $expectedlocation);
1411 throw new zip_exception('Unable to open the zip package');
1414 // Make sure that the ZIP has expected structure
1415 $pluginname = basename($expectedlocation);
1416 for ($i = 0; $i < $zip->numFiles; $i++) {
1417 $stat = $zip->statIndex($i);
1418 $filename = $stat['name'];
1419 $filename = explode('/', $filename);
1420 if ($filename[0] !== $pluginname) {
1421 $zip->close();
1422 throw new zip_exception('Invalid structure of the zip package');
1426 if (!$zip->extractTo($plugintyperoot)) {
1427 $zip->close();
1428 $this->remove_directory($expectedlocation, true); // just in case something was created
1429 if ($backuplocation !== false) {
1430 $this->move_directory_into($backuplocation, $expectedlocation);
1432 throw new zip_exception('Unable to extract the zip package');
1435 $zip->close();
1436 unlink($ziplocation);
1440 * Redirect the browser
1442 * @todo check if there has been some output yet
1443 * @param string $url
1445 protected function redirect($url) {
1446 header('Location: '.$url);
1452 * Provides exception handlers for this script
1454 class exception_handlers {
1457 * Sets the exception handler
1460 * @param string $handler name
1462 public static function set_handler($handler) {
1464 if (PHP_SAPI === 'cli') {
1465 // No custom handler available for CLI mode.
1466 set_exception_handler(null);
1467 return;
1470 set_exception_handler('exception_handlers::'.$handler.'_exception_handler');
1474 * Returns the text describing the thrown exception
1476 * By default, PHP displays full path to scripts when the exception is thrown. In order to prevent
1477 * sensitive information leak (and yes, the path to scripts at a web server _is_ sensitive information)
1478 * the path to scripts is removed from the message.
1480 * @param Exception $e thrown exception
1481 * @return string
1483 public static function format_exception_info(Exception $e) {
1485 $mydir = dirname(__FILE__).'/';
1486 $text = $e->__toString();
1487 $text = str_replace($mydir, '', $text);
1488 return $text;
1492 * Very basic exception handler
1494 * @param Exception $e uncaught exception
1496 public static function bootstrap_exception_handler(Exception $e) {
1497 echo('<h1>Oops! It did it again</h1>');
1498 echo('<p><strong>Moodle deployment utility had a trouble with your request. See the debugging information for more details.</strong></p>');
1499 echo('<pre>');
1500 echo self::format_exception_info($e);
1501 echo('</pre>');
1505 * Default exception handler
1507 * When this handler is used, input_manager and output_manager singleton instances already
1508 * exist in the memory and can be used.
1510 * @param Exception $e uncaught exception
1512 public static function default_exception_handler(Exception $e) {
1514 $worker = worker::instance();
1515 $worker->log_exception($e);
1517 $output = output_manager::instance();
1518 $output->exception($e);
1522 ////////////////////////////////////////////////////////////////////////////////
1524 // Check if the script is actually executed or if it was just included by someone
1525 // else - typically by the PHPUnit. This is a PHP alternative to the Python's
1526 // if __name__ == '__main__'
1527 if (!debug_backtrace()) {
1528 // We are executed by the SAPI.
1529 exception_handlers::set_handler('bootstrap');
1530 // Initialize the worker class to actually make the job.
1531 $worker = worker::instance();
1532 exception_handlers::set_handler('default');
1534 // Lights, Camera, Action!
1535 $worker->execute();
1537 } else {
1538 // We are included - probably by some unit testing framework. Do nothing.