MDL-36963 Add unit tests for directory operations in mdeploy.php
[moodle.git] / mdeploy.php
blobc16bc0fe986c8940e08a0d9b1da5fcad93f94f44
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 available updates to the local Moodle site.
23 * CLI usage example:
24 * $ sudo -u apache php mdeploy.php --upgrade \
25 * --package=https://moodle.org/plugins/download.php/...zip \
26 * --dataroot=/home/mudrd8mz/moodledata/moodle24
28 * @package core
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 /////////////////////////////////////////////////////
53 /**
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();
64 /**
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];
81 /**
82 * Optional post-instantiation code.
84 protected function initialize() {
85 // Do nothing in this base class.
88 /**
89 * Direct instantiation not allowed, use the factory method {@link instance()}
91 final protected function __construct() {
94 /**
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
144 * @return mixed
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);
158 } else {
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)) {
189 $all = array();
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];
196 $all[] = $info;
198 return $all;
201 $found = false;
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;
208 break;
210 } else {
211 // Search by the long option name
212 if ($optioninfo[1] === $name) {
213 $found = $optioninfo;
214 break;
219 if (!$found) {
220 return false;
223 $info = new stdClass();
224 $info->shortname = $found[0];
225 $info->longname = $found[1];
226 $info->type = $found[2];
227 $info->desc = $found[3];
229 return $info;
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}
237 * @return mixed
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.');
247 switch ($type) {
249 case input_manager::TYPE_FILE:
250 $raw = preg_replace('~[[:cntrl:]]|[&<>"`\|\':\\\\/]~u', '', $raw);
251 $raw = preg_replace('~\.\.+~', '', $raw);
252 if ($raw === '.') {
253 $raw = '';
255 return $raw;
257 case input_manager::TYPE_FLAG:
258 return true;
260 case input_manager::TYPE_INT:
261 return (int)$raw;
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);
272 return $raw;
274 case input_manager::TYPE_RAW:
275 return $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)) {
286 return $raw;
287 } else {
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');
298 return $raw;
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');
304 return $raw;
306 default:
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();
318 } else {
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) {
333 if (empty($name)) {
334 throw new invalid_coding_exception('Invalid empty option name.');
337 $meta = $this->get_option_info($name);
338 if (empty($meta)) {
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}
348 * @return mixed
350 protected function get_required_option($name) {
351 if ($this->inputprovider->has_option($name)) {
352 return $this->inputprovider->get_option($name);
353 } else {
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.
364 * @return mixed
366 protected function get_optional_option($name, $default) {
367 if ($this->inputprovider->has_option($name)) {
368 return $this->inputprovider->get_option($name);
369 } else {
370 return $default;
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
407 * @return bool
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();
429 $cooked = array();
431 foreach ($raw as $k => $v) {
432 if (is_array($v) or is_object($v)) {
433 // Not supported.
436 $info = $input->get_option_info($k);
437 if (!$info) {
438 continue;
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.
476 $short = '';
477 $long = array();
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;
484 } else {
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() {
510 return $_POST;
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();
544 } else {
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');
574 $this->outln();
575 $this->outln('Usage: $ sudo -u apache php mdeploy.php [options]');
576 $this->outln();
577 $input = input_manager::instance();
578 foreach($input->get_option_info() as $info) {
579 $option = array();
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>');
630 echo('<pre>');
631 echo exception_handlers::format_exception_info($e);
632 echo('</pre>');
633 $this->end_output();
636 // End of external API
639 * Produce the HTML page header
641 protected function start_output() {
642 echo '<!doctype html>
643 <html lang="en">
644 <head>
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;}
651 </style>
652 </head>
653 <body>
654 <div id="page">';
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.
705 $this->authorize();
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);
723 } else {
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).
766 $this->done();
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') {
804 exit($exitcode);
806 } else {
807 $returnurl = $this->input->get_option('returnurl');
808 $this->redirect($returnurl);
809 exit($exitcode);
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');
826 return;
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
842 unlink($passpath);
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.
870 * @return string
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
899 * @return string
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);
912 $suffix = 0;
913 while (file_exists($target.'.'.$suffix.'.zip')) {
914 $suffix++;
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
924 * @return string
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();
937 $suffix = 0;
938 while (file_exists($target.'.'.$suffix)) {
939 $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.');
965 return false;
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');
1008 if (!$targetfile) {
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) {
1029 return false;
1031 } else if (is_array($this->curlinfo) and (empty($this->curlinfo['http_code']) or $this->curlinfo['http_code'] != 200)) {
1032 return false;
1035 return true;
1039 * Log a message
1041 * @param string $message
1043 protected function log($message) {
1045 $logpath = $this->log_location();
1047 if (empty($logpath)) {
1048 // no logging available
1049 return;
1052 $f = fopen($logpath, 'ab');
1054 if ($f === false) {
1055 throw new filesystem_exception('Unable to open the log file for appending');
1058 $message = $this->format_log_message($message);
1060 fwrite($f, $message);
1062 fclose($f);
1066 * Prepares the log message for writing into the file
1068 * @param string $msg
1069 * @return string
1071 protected function format_log_message($msg) {
1073 $msg = trim($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
1083 * @return bool
1085 protected function move_directory_source_precheck($source) {
1087 if (!is_writable($source)) {
1088 return false;
1091 if (is_dir($source)) {
1092 $handle = opendir($source);
1093 } else {
1094 return false;
1097 $result = true;
1099 while ($filename = readdir($handle)) {
1100 $sourcepath = $source.'/'.$filename;
1102 if ($filename === '.' or $filename === '..') {
1103 continue;
1106 if (is_dir($sourcepath)) {
1107 $result = $result && $this->move_directory_source_precheck($sourcepath);
1109 } else {
1110 $result = $result && is_writable($sourcepath);
1114 closedir($handle);
1116 return $result;
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
1123 * @return bool
1125 protected function move_directory_target_precheck($target) {
1127 if (file_exists($target)) {
1128 return false;
1131 $result = mkdir($target, 02777) && rmdir($target);
1133 return $result;
1137 * Moves the given source into a new location recursively
1139 * The target location can not exist.
1141 * @param string $source full path to the existing directory
1142 * @param string $destination full path to the new location of the folder
1143 * @param bool $keepsourceroot should the root of the $source be kept or removed at the end
1144 * @return bool
1146 protected function move_directory($source, $target, $keepsourceroot = false) {
1148 if (file_exists($target)) {
1149 throw new filesystem_exception('Unable to move the directory - target location already exists');
1152 return $this->move_directory_into($source, $target, $keepsourceroot);
1156 * Moves the given source into a new location recursively
1158 * If the target already exists, files are moved into it. The target is created otherwise.
1160 * @param string $source full path to the existing directory
1161 * @param string $destination full path to the new location of the folder
1162 * @param bool $keepsourceroot should the root of the $source be kept or removed at the end
1163 * @return bool
1165 protected function move_directory_into($source, $target, $keepsourceroot = false) {
1167 if (is_dir($source)) {
1168 $handle = opendir($source);
1169 } else {
1170 throw new filesystem_exception('Source location is not a directory');
1173 if (is_dir($target)) {
1174 $result = true;
1175 } else {
1176 $result = mkdir($target, 02777);
1179 while ($filename = readdir($handle)) {
1180 $sourcepath = $source.'/'.$filename;
1181 $targetpath = $target.'/'.$filename;
1183 if ($filename === '.' or $filename === '..') {
1184 continue;
1187 if (is_dir($sourcepath)) {
1188 $result = $result && $this->move_directory($sourcepath, $targetpath, false);
1190 } else {
1191 $result = $result && rename($sourcepath, $targetpath);
1195 closedir($handle);
1197 if (!$keepsourceroot) {
1198 $result = $result && rmdir($source);
1201 clearstatcache();
1203 return $result;
1207 * Deletes the given directory recursively
1209 * @param string $path full path to the directory
1210 * @param bool $keeppathroot should the root of the $path be kept (i.e. remove the content only) or removed too
1211 * @return bool
1213 protected function remove_directory($path, $keeppathroot = false) {
1215 $result = true;
1217 if (!file_exists($path)) {
1218 return $result;
1221 if (is_dir($path)) {
1222 $handle = opendir($path);
1223 } else {
1224 throw new filesystem_exception('Given path is not a directory');
1227 while ($filename = readdir($handle)) {
1228 $filepath = $path.'/'.$filename;
1230 if ($filename === '.' or $filename === '..') {
1231 continue;
1234 if (is_dir($filepath)) {
1235 $result = $result && $this->remove_directory($filepath, false);
1237 } else {
1238 $result = $result && unlink($filepath);
1242 closedir($handle);
1244 if (!$keeppathroot) {
1245 $result = $result && rmdir($path);
1248 clearstatcache();
1250 return $result;
1254 * Unzip the file obtained from the Plugins directory to this site
1256 * @param string $ziplocation full path to the ZIP file
1257 * @param string $plugintyperoot full path to the plugin's type location
1258 * @param string $expectedlocation expected full path to the plugin after it is extracted
1259 * @param string $backuplocation location of the previous version of the plugin
1261 protected function unzip_plugin($ziplocation, $plugintyperoot, $expectedlocation, $backuplocation) {
1263 $zip = new ZipArchive();
1264 $result = $zip->open($ziplocation);
1266 if ($result !== true) {
1267 $this->move_directory($backuplocation, $expectedlocation);
1268 throw new zip_exception('Unable to open the zip package');
1271 // Make sure that the ZIP has expected structure
1272 $pluginname = basename($expectedlocation);
1273 for ($i = 0; $i < $zip->numFiles; $i++) {
1274 $stat = $zip->statIndex($i);
1275 $filename = $stat['name'];
1276 $filename = explode('/', $filename);
1277 if ($filename[0] !== $pluginname) {
1278 $zip->close();
1279 throw new zip_exception('Invalid structure of the zip package');
1283 if (!$zip->extractTo($plugintyperoot)) {
1284 $zip->close();
1285 $this->remove_directory($expectedlocation); // just in case something was created
1286 $this->move_directory($backuplocation, $expectedlocation);
1287 throw new zip_exception('Unable to extract the zip package');
1290 $zip->close();
1291 unlink($ziplocation);
1295 * Redirect the browser
1297 * @todo check if there has been some output yet
1298 * @param string $url
1300 protected function redirect($url) {
1301 header('Location: '.$url);
1307 * Provides exception handlers for this script
1309 class exception_handlers {
1312 * Sets the exception handler
1315 * @param string $handler name
1317 public static function set_handler($handler) {
1319 if (PHP_SAPI === 'cli') {
1320 // No custom handler available for CLI mode.
1321 set_exception_handler(null);
1322 return;
1325 set_exception_handler('exception_handlers::'.$handler.'_exception_handler');
1329 * Returns the text describing the thrown exception
1331 * By default, PHP displays full path to scripts when the exception is thrown. In order to prevent
1332 * sensitive information leak (and yes, the path to scripts at a web server _is_ sensitive information)
1333 * the path to scripts is removed from the message.
1335 * @param Exception $e thrown exception
1336 * @return string
1338 public static function format_exception_info(Exception $e) {
1340 $mydir = dirname(__FILE__).'/';
1341 $text = $e->__toString();
1342 $text = str_replace($mydir, '', $text);
1343 return $text;
1347 * Very basic exception handler
1349 * @param Exception $e uncaught exception
1351 public static function bootstrap_exception_handler(Exception $e) {
1352 echo('<h1>Oops! It did it again</h1>');
1353 echo('<p><strong>Moodle deployment utility had a trouble with your request. See the debugging information for more details.</strong></p>');
1354 echo('<pre>');
1355 echo self::format_exception_info($e);
1356 echo('</pre>');
1360 * Default exception handler
1362 * When this handler is used, input_manager and output_manager singleton instances already
1363 * exist in the memory and can be used.
1365 * @param Exception $e uncaught exception
1367 public static function default_exception_handler(Exception $e) {
1369 $worker = worker::instance();
1370 $worker->log_exception($e);
1372 $output = output_manager::instance();
1373 $output->exception($e);
1377 ////////////////////////////////////////////////////////////////////////////////
1379 // Check if the script is actually executed or if it was just included by someone
1380 // else - typically by the PHPUnit. This is a PHP alternative to the Python's
1381 // if __name__ == '__main__'
1382 if (!debug_backtrace()) {
1383 // We are executed by the SAPI.
1384 exception_handlers::set_handler('bootstrap');
1385 // Initialize the worker class to actually make the job.
1386 $worker = worker::instance();
1387 exception_handlers::set_handler('default');
1389 // Lights, Camera, Action!
1390 $worker->execute();
1392 } else {
1393 // We are included - probably by some unit testing framework. Do nothing.