Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 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. 14 // 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/>. 17 18 /** 19 * Various upgrade/install related functions and classes. 20 * 21 * @package core 22 * @subpackage upgrade 23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** UPGRADE_LOG_NORMAL = 0 */ 30 define('UPGRADE_LOG_NORMAL', 0); 31 /** UPGRADE_LOG_NOTICE = 1 */ 32 define('UPGRADE_LOG_NOTICE', 1); 33 /** UPGRADE_LOG_ERROR = 2 */ 34 define('UPGRADE_LOG_ERROR', 2); 35 36 /** 37 * Exception indicating unknown error during upgrade. 38 * 39 * @package core 40 * @subpackage upgrade 41 * @copyright 2009 Petr Skoda {@link http://skodak.org} 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 43 */ 44 class upgrade_exception extends moodle_exception { 45 function __construct($plugin, $version, $debuginfo=NULL) { 46 global $CFG; 47 $a = (object)array('plugin'=>$plugin, 'version'=>$version); 48 parent::__construct('upgradeerror', 'admin', "$CFG->wwwroot/$CFG->admin/index.php", $a, $debuginfo); 49 } 50 } 51 52 /** 53 * Exception indicating downgrade error during upgrade. 54 * 55 * @package core 56 * @subpackage upgrade 57 * @copyright 2009 Petr Skoda {@link http://skodak.org} 58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 59 */ 60 class downgrade_exception extends moodle_exception { 61 function __construct($plugin, $oldversion, $newversion) { 62 global $CFG; 63 $plugin = is_null($plugin) ? 'moodle' : $plugin; 64 $a = (object)array('plugin'=>$plugin, 'oldversion'=>$oldversion, 'newversion'=>$newversion); 65 parent::__construct('cannotdowngrade', 'debug', "$CFG->wwwroot/$CFG->admin/index.php", $a); 66 } 67 } 68 69 /** 70 * @package core 71 * @subpackage upgrade 72 * @copyright 2009 Petr Skoda {@link http://skodak.org} 73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 74 */ 75 class upgrade_requires_exception extends moodle_exception { 76 function __construct($plugin, $pluginversion, $currentmoodle, $requiremoodle) { 77 global $CFG; 78 $a = new stdClass(); 79 $a->pluginname = $plugin; 80 $a->pluginversion = $pluginversion; 81 $a->currentmoodle = $currentmoodle; 82 $a->requiremoodle = $requiremoodle; 83 parent::__construct('pluginrequirementsnotmet', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a); 84 } 85 } 86 87 /** 88 * Exception thrown when attempting to install a plugin that declares incompatibility with moodle version 89 * 90 * @package core 91 * @subpackage upgrade 92 * @copyright 2019 Peter Burnett <peterburnett@catalyst-au.net> 93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 94 */ 95 class plugin_incompatible_exception extends moodle_exception { 96 /** 97 * Constructor function for exception 98 * 99 * @param \core\plugininfo\base $plugin The plugin causing the exception 100 * @param int $pluginversion The version of the plugin causing the exception 101 */ 102 public function __construct($plugin, $pluginversion) { 103 global $CFG; 104 $a = new stdClass(); 105 $a->pluginname = $plugin; 106 $a->pluginversion = $pluginversion; 107 $a->moodleversion = $CFG->branch; 108 109 parent::__construct('pluginunsupported', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a); 110 } 111 } 112 113 /** 114 * @package core 115 * @subpackage upgrade 116 * @copyright 2009 Petr Skoda {@link http://skodak.org} 117 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 118 */ 119 class plugin_defective_exception extends moodle_exception { 120 function __construct($plugin, $details) { 121 global $CFG; 122 parent::__construct('detectedbrokenplugin', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $plugin, $details); 123 } 124 } 125 126 /** 127 * Misplaced plugin exception. 128 * 129 * Note: this should be used only from the upgrade/admin code. 130 * 131 * @package core 132 * @subpackage upgrade 133 * @copyright 2009 Petr Skoda {@link http://skodak.org} 134 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 135 */ 136 class plugin_misplaced_exception extends moodle_exception { 137 /** 138 * Constructor. 139 * @param string $component the component from version.php 140 * @param string $expected expected directory, null means calculate 141 * @param string $current plugin directory path 142 */ 143 public function __construct($component, $expected, $current) { 144 global $CFG; 145 if (empty($expected)) { 146 list($type, $plugin) = core_component::normalize_component($component); 147 $plugintypes = core_component::get_plugin_types(); 148 if (isset($plugintypes[$type])) { 149 $expected = $plugintypes[$type] . '/' . $plugin; 150 } 151 } 152 if (strpos($expected, '$CFG->dirroot') !== 0) { 153 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', $expected); 154 } 155 if (strpos($current, '$CFG->dirroot') !== 0) { 156 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $current); 157 } 158 $a = new stdClass(); 159 $a->component = $component; 160 $a->expected = $expected; 161 $a->current = $current; 162 parent::__construct('detectedmisplacedplugin', 'core_plugin', "$CFG->wwwroot/$CFG->admin/index.php", $a); 163 } 164 } 165 166 /** 167 * Static class monitors performance of upgrade steps. 168 */ 169 class core_upgrade_time { 170 /** @var float Time at start of current upgrade (plugin/system) */ 171 protected static $before; 172 /** @var float Time at end of last savepoint */ 173 protected static $lastsavepoint; 174 /** @var bool Flag to indicate whether we are recording timestamps or not. */ 175 protected static $isrecording = false; 176 177 /** 178 * Records current time at the start of the current upgrade item, e.g. plugin. 179 */ 180 public static function record_start() { 181 self::$before = microtime(true); 182 self::$lastsavepoint = self::$before; 183 self::$isrecording = true; 184 } 185 186 /** 187 * Records current time at the end of a given numbered step. 188 * 189 * @param float $version Version number (may have decimals, or not) 190 */ 191 public static function record_savepoint($version) { 192 global $CFG, $OUTPUT; 193 194 // In developer debug mode we show a notification after each individual save point. 195 if ($CFG->debugdeveloper && self::$isrecording) { 196 $time = microtime(true); 197 198 $notification = new \core\output\notification($version . ': ' . 199 get_string('successduration', '', format_float($time - self::$lastsavepoint, 2)), 200 \core\output\notification::NOTIFY_SUCCESS); 201 $notification->set_show_closebutton(false); 202 echo $OUTPUT->render($notification); 203 self::$lastsavepoint = $time; 204 } 205 } 206 207 /** 208 * Gets the time since the record_start function was called, rounded to 2 digits. 209 * 210 * @return float Elapsed time 211 */ 212 public static function get_elapsed() { 213 return microtime(true) - self::$before; 214 } 215 } 216 217 /** 218 * Sets maximum expected time needed for upgrade task. 219 * Please always make sure that upgrade will not run longer! 220 * 221 * The script may be automatically aborted if upgrade times out. 222 * 223 * @category upgrade 224 * @param int $max_execution_time in seconds (can not be less than 60 s) 225 */ 226 function upgrade_set_timeout($max_execution_time=300) { 227 global $CFG; 228 229 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) { 230 $upgraderunning = get_config(null, 'upgraderunning'); 231 } else { 232 $upgraderunning = $CFG->upgraderunning; 233 } 234 235 if (!$upgraderunning) { 236 if (CLI_SCRIPT) { 237 // never stop CLI upgrades 238 $upgraderunning = 0; 239 } else { 240 // web upgrade not running or aborted 241 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/"); 242 } 243 } 244 245 if ($max_execution_time < 60) { 246 // protection against 0 here 247 $max_execution_time = 60; 248 } 249 250 $expected_end = time() + $max_execution_time; 251 252 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) { 253 // no need to store new end, it is nearly the same ;-) 254 return; 255 } 256 257 if (CLI_SCRIPT) { 258 // there is no point in timing out of CLI scripts, admins can stop them if necessary 259 core_php_time_limit::raise(); 260 } else { 261 core_php_time_limit::raise($max_execution_time); 262 } 263 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time 264 } 265 266 /** 267 * Upgrade savepoint, marks end of each upgrade block. 268 * It stores new main version, resets upgrade timeout 269 * and abort upgrade if user cancels page loading. 270 * 271 * Please do not make large upgrade blocks with lots of operations, 272 * for example when adding tables keep only one table operation per block. 273 * 274 * @category upgrade 275 * @param bool $result false if upgrade step failed, true if completed 276 * @param string or float $version main version 277 * @param bool $allowabort allow user to abort script execution here 278 * @return void 279 */ 280 function upgrade_main_savepoint($result, $version, $allowabort=true) { 281 global $CFG; 282 283 //sanity check to avoid confusion with upgrade_mod_savepoint usage. 284 if (!is_bool($allowabort)) { 285 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?'; 286 throw new coding_exception($errormessage); 287 } 288 289 if (!$result) { 290 throw new upgrade_exception(null, $version); 291 } 292 293 if ($CFG->version >= $version) { 294 // something really wrong is going on in main upgrade script 295 throw new downgrade_exception(null, $CFG->version, $version); 296 } 297 298 set_config('version', $version); 299 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached'); 300 301 // reset upgrade timeout to default 302 upgrade_set_timeout(); 303 304 core_upgrade_time::record_savepoint($version); 305 306 // this is a safe place to stop upgrades if user aborts page loading 307 if ($allowabort and connection_aborted()) { 308 die; 309 } 310 } 311 312 /** 313 * Module upgrade savepoint, marks end of module upgrade blocks 314 * It stores module version, resets upgrade timeout 315 * and abort upgrade if user cancels page loading. 316 * 317 * @category upgrade 318 * @param bool $result false if upgrade step failed, true if completed 319 * @param string or float $version main version 320 * @param string $modname name of module 321 * @param bool $allowabort allow user to abort script execution here 322 * @return void 323 */ 324 function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) { 325 global $DB; 326 327 $component = 'mod_'.$modname; 328 329 if (!$result) { 330 throw new upgrade_exception($component, $version); 331 } 332 333 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version')); 334 335 if (!$module = $DB->get_record('modules', array('name'=>$modname))) { 336 print_error('modulenotexist', 'debug', '', $modname); 337 } 338 339 if ($dbversion >= $version) { 340 // something really wrong is going on in upgrade script 341 throw new downgrade_exception($component, $dbversion, $version); 342 } 343 set_config('version', $version, $component); 344 345 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached'); 346 347 // reset upgrade timeout to default 348 upgrade_set_timeout(); 349 350 core_upgrade_time::record_savepoint($version); 351 352 // this is a safe place to stop upgrades if user aborts page loading 353 if ($allowabort and connection_aborted()) { 354 die; 355 } 356 } 357 358 /** 359 * Blocks upgrade savepoint, marks end of blocks upgrade blocks 360 * It stores block version, resets upgrade timeout 361 * and abort upgrade if user cancels page loading. 362 * 363 * @category upgrade 364 * @param bool $result false if upgrade step failed, true if completed 365 * @param string or float $version main version 366 * @param string $blockname name of block 367 * @param bool $allowabort allow user to abort script execution here 368 * @return void 369 */ 370 function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) { 371 global $DB; 372 373 $component = 'block_'.$blockname; 374 375 if (!$result) { 376 throw new upgrade_exception($component, $version); 377 } 378 379 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version')); 380 381 if (!$block = $DB->get_record('block', array('name'=>$blockname))) { 382 print_error('blocknotexist', 'debug', '', $blockname); 383 } 384 385 if ($dbversion >= $version) { 386 // something really wrong is going on in upgrade script 387 throw new downgrade_exception($component, $dbversion, $version); 388 } 389 set_config('version', $version, $component); 390 391 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached'); 392 393 // reset upgrade timeout to default 394 upgrade_set_timeout(); 395 396 core_upgrade_time::record_savepoint($version); 397 398 // this is a safe place to stop upgrades if user aborts page loading 399 if ($allowabort and connection_aborted()) { 400 die; 401 } 402 } 403 404 /** 405 * Plugins upgrade savepoint, marks end of blocks upgrade blocks 406 * It stores plugin version, resets upgrade timeout 407 * and abort upgrade if user cancels page loading. 408 * 409 * @category upgrade 410 * @param bool $result false if upgrade step failed, true if completed 411 * @param string or float $version main version 412 * @param string $type The type of the plugin. 413 * @param string $plugin The name of the plugin. 414 * @param bool $allowabort allow user to abort script execution here 415 * @return void 416 */ 417 function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) { 418 global $DB; 419 420 $component = $type.'_'.$plugin; 421 422 if (!$result) { 423 throw new upgrade_exception($component, $version); 424 } 425 426 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version')); 427 428 if ($dbversion >= $version) { 429 // Something really wrong is going on in the upgrade script 430 throw new downgrade_exception($component, $dbversion, $version); 431 } 432 set_config('version', $version, $component); 433 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached'); 434 435 // Reset upgrade timeout to default 436 upgrade_set_timeout(); 437 438 core_upgrade_time::record_savepoint($version); 439 440 // This is a safe place to stop upgrades if user aborts page loading 441 if ($allowabort and connection_aborted()) { 442 die; 443 } 444 } 445 446 /** 447 * Detect if there are leftovers in PHP source files. 448 * 449 * During main version upgrades administrators MUST move away 450 * old PHP source files and start from scratch (or better 451 * use git). 452 * 453 * @return bool true means borked upgrade, false means previous PHP files were properly removed 454 */ 455 function upgrade_stale_php_files_present() { 456 global $CFG; 457 458 $someexamplesofremovedfiles = array( 459 // Removed in 3.11. 460 '/customfield/edit.php', 461 '/lib/phpunit/classes/autoloader.php', 462 '/lib/xhprof/README', 463 '/message/defaultoutputs.php', 464 '/user/files_form.php', 465 // Removed in 3.10. 466 '/grade/grading/classes/privacy/gradingform_provider.php', 467 '/lib/coursecatlib.php', 468 '/lib/form/htmleditor.php', 469 '/message/classes/output/messagearea/contact.php', 470 // Removed in 3.9. 471 '/course/classes/output/modchooser_item.php', 472 '/course/yui/build/moodle-course-modchooser/moodle-course-modchooser-min.js', 473 '/course/yui/src/modchooser/js/modchooser.js', 474 '/h5p/classes/autoloader.php', 475 '/lib/adodb/readme.txt', 476 '/lib/maxmind/GeoIp2/Compat/JsonSerializable.php', 477 // Removed in 3.8. 478 '/lib/amd/src/modal_confirm.js', 479 '/lib/fonts/font-awesome-4.7.0/css/font-awesome.css', 480 '/lib/jquery/jquery-3.2.1.min.js', 481 '/lib/recaptchalib.php', 482 '/lib/sessionkeepalive_ajax.php', 483 '/lib/yui/src/checknet/js/checknet.js', 484 '/question/amd/src/qbankmanager.js', 485 // Removed in 3.7. 486 '/lib/form/yui/src/showadvanced/js/showadvanced.js', 487 '/lib/tests/output_external_test.php', 488 '/message/amd/src/message_area.js', 489 '/message/templates/message_area.mustache', 490 '/question/yui/src/qbankmanager/build.json', 491 // Removed in 3.6. 492 '/lib/classes/session/memcache.php', 493 '/lib/eventslib.php', 494 '/lib/form/submitlink.php', 495 '/lib/medialib.php', 496 '/lib/password_compat/lib/password.php', 497 // Removed in 3.5. 498 '/lib/dml/mssql_native_moodle_database.php', 499 '/lib/dml/mssql_native_moodle_recordset.php', 500 '/lib/dml/mssql_native_moodle_temptables.php', 501 // Removed in 3.4. 502 '/auth/README.txt', 503 '/calendar/set.php', 504 '/enrol/users.php', 505 '/enrol/yui/rolemanager/assets/skins/sam/rolemanager.css', 506 // Removed in 3.3. 507 '/badges/backpackconnect.php', 508 '/calendar/yui/src/info/assets/skins/sam/moodle-calendar-info.css', 509 '/competency/classes/external/exporter.php', 510 '/mod/forum/forum.js', 511 '/user/pixgroup.php', 512 // Removed in 3.2. 513 '/calendar/preferences.php', 514 '/lib/alfresco/', 515 '/lib/jquery/jquery-1.12.1.min.js', 516 '/lib/password_compat/tests/', 517 '/lib/phpunit/classes/unittestcase.php', 518 // Removed in 3.1. 519 '/lib/classes/log/sql_internal_reader.php', 520 '/lib/zend/', 521 '/mod/forum/pix/icon.gif', 522 '/tag/templates/tagname.mustache', 523 // Removed in 3.0. 524 '/mod/lti/grade.php', 525 '/tag/coursetagslib.php', 526 // Removed in 2.9. 527 '/lib/timezone.txt', 528 // Removed in 2.8. 529 '/course/delete_category_form.php', 530 // Removed in 2.7. 531 '/admin/tool/qeupgradehelper/version.php', 532 // Removed in 2.6. 533 '/admin/block.php', 534 '/admin/oacleanup.php', 535 // Removed in 2.5. 536 '/backup/lib.php', 537 '/backup/bb/README.txt', 538 '/lib/excel/test.php', 539 // Removed in 2.4. 540 '/admin/tool/unittest/simpletestlib.php', 541 // Removed in 2.3. 542 '/lib/minify/builder/', 543 // Removed in 2.2. 544 '/lib/yui/3.4.1pr1/', 545 // Removed in 2.2. 546 '/search/cron_php5.php', 547 '/course/report/log/indexlive.php', 548 '/admin/report/backups/index.php', 549 '/admin/generator.php', 550 // Removed in 2.1. 551 '/lib/yui/2.8.0r4/', 552 // Removed in 2.0. 553 '/blocks/admin/block_admin.php', 554 '/blocks/admin_tree/block_admin_tree.php', 555 ); 556 557 foreach ($someexamplesofremovedfiles as $file) { 558 if (file_exists($CFG->dirroot.$file)) { 559 return true; 560 } 561 } 562 563 return false; 564 } 565 566 /** 567 * Upgrade plugins 568 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype') 569 * return void 570 */ 571 function upgrade_plugins($type, $startcallback, $endcallback, $verbose) { 572 global $CFG, $DB; 573 574 /// special cases 575 if ($type === 'mod') { 576 return upgrade_plugins_modules($startcallback, $endcallback, $verbose); 577 } else if ($type === 'block') { 578 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose); 579 } 580 581 $plugs = core_component::get_plugin_list($type); 582 583 foreach ($plugs as $plug=>$fullplug) { 584 // Reset time so that it works when installing a large number of plugins 585 core_php_time_limit::raise(600); 586 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name 587 588 // check plugin dir is valid name 589 if (empty($component)) { 590 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.'); 591 } 592 593 if (!is_readable($fullplug.'/version.php')) { 594 continue; 595 } 596 597 $plugin = new stdClass(); 598 $plugin->version = null; 599 $module = $plugin; // Prevent some notices when plugin placed in wrong directory. 600 require ($fullplug.'/version.php'); // defines $plugin with version etc 601 unset($module); 602 603 if (empty($plugin->version)) { 604 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.'); 605 } 606 607 if (empty($plugin->component)) { 608 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.'); 609 } 610 611 if ($plugin->component !== $component) { 612 throw new plugin_misplaced_exception($plugin->component, null, $fullplug); 613 } 614 615 $plugin->name = $plug; 616 $plugin->fullname = $component; 617 618 if (!empty($plugin->requires)) { 619 if ($plugin->requires > $CFG->version) { 620 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires); 621 } else if ($plugin->requires < 2010000000) { 622 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.'); 623 } 624 } 625 626 // Throw exception if plugin is incompatible with moodle version. 627 if (!empty($plugin->incompatible)) { 628 if ($CFG->branch >= $plugin->incompatible) { 629 throw new plugin_incompatible_exception($component, $plugin->version); 630 } 631 } 632 633 // try to recover from interrupted install.php if needed 634 if (file_exists($fullplug.'/db/install.php')) { 635 if (get_config($plugin->fullname, 'installrunning')) { 636 require_once ($fullplug.'/db/install.php'); 637 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery'; 638 if (function_exists($recover_install_function)) { 639 $startcallback($component, true, $verbose); 640 $recover_install_function(); 641 unset_config('installrunning', $plugin->fullname); 642 update_capabilities($component); 643 log_update_descriptions($component); 644 external_update_descriptions($component); 645 \core\task\manager::reset_scheduled_tasks_for_component($component); 646 \core_analytics\manager::update_default_models_for_component($component); 647 message_update_providers($component); 648 \core\message\inbound\manager::update_handlers_for_component($component); 649 if ($type === 'message') { 650 message_update_processors($plug); 651 } 652 upgrade_plugin_mnet_functions($component); 653 core_tag_area::reset_definitions_for_component($component); 654 $endcallback($component, true, $verbose); 655 } 656 } 657 } 658 659 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching! 660 if (empty($installedversion)) { // new installation 661 $startcallback($component, true, $verbose); 662 663 /// Install tables if defined 664 if (file_exists($fullplug.'/db/install.xml')) { 665 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml'); 666 } 667 668 /// store version 669 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false); 670 671 /// execute post install file 672 if (file_exists($fullplug.'/db/install.php')) { 673 require_once ($fullplug.'/db/install.php'); 674 set_config('installrunning', 1, $plugin->fullname); 675 $post_install_function = 'xmldb_'.$plugin->fullname.'_install'; 676 $post_install_function(); 677 unset_config('installrunning', $plugin->fullname); 678 } 679 680 /// Install various components 681 update_capabilities($component); 682 log_update_descriptions($component); 683 external_update_descriptions($component); 684 \core\task\manager::reset_scheduled_tasks_for_component($component); 685 \core_analytics\manager::update_default_models_for_component($component); 686 message_update_providers($component); 687 \core\message\inbound\manager::update_handlers_for_component($component); 688 if ($type === 'message') { 689 message_update_processors($plug); 690 } 691 upgrade_plugin_mnet_functions($component); 692 core_tag_area::reset_definitions_for_component($component); 693 $endcallback($component, true, $verbose); 694 695 } else if ($installedversion < $plugin->version) { // upgrade 696 /// Run the upgrade function for the plugin. 697 $startcallback($component, false, $verbose); 698 699 if (is_readable($fullplug.'/db/upgrade.php')) { 700 require_once ($fullplug.'/db/upgrade.php'); // defines upgrading function 701 702 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade'; 703 $result = $newupgrade_function($installedversion); 704 } else { 705 $result = true; 706 } 707 708 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching! 709 if ($installedversion < $plugin->version) { 710 // store version if not already there 711 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false); 712 } 713 714 /// Upgrade various components 715 update_capabilities($component); 716 log_update_descriptions($component); 717 external_update_descriptions($component); 718 \core\task\manager::reset_scheduled_tasks_for_component($component); 719 \core_analytics\manager::update_default_models_for_component($component); 720 message_update_providers($component); 721 \core\message\inbound\manager::update_handlers_for_component($component); 722 if ($type === 'message') { 723 // Ugly hack! 724 message_update_processors($plug); 725 } 726 upgrade_plugin_mnet_functions($component); 727 core_tag_area::reset_definitions_for_component($component); 728 $endcallback($component, false, $verbose); 729 730 } else if ($installedversion > $plugin->version) { 731 throw new downgrade_exception($component, $installedversion, $plugin->version); 732 } 733 } 734 } 735 736 /** 737 * Find and check all modules and load them up or upgrade them if necessary 738 * 739 * @global object 740 * @global object 741 */ 742 function upgrade_plugins_modules($startcallback, $endcallback, $verbose) { 743 global $CFG, $DB; 744 745 $mods = core_component::get_plugin_list('mod'); 746 747 foreach ($mods as $mod=>$fullmod) { 748 749 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it 750 continue; 751 } 752 753 $component = clean_param('mod_'.$mod, PARAM_COMPONENT); 754 755 // check module dir is valid name 756 if (empty($component)) { 757 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.'); 758 } 759 760 if (!is_readable($fullmod.'/version.php')) { 761 throw new plugin_defective_exception($component, 'Missing version.php'); 762 } 763 764 $module = new stdClass(); 765 $plugin = new stdClass(); 766 $plugin->version = null; 767 require ($fullmod .'/version.php'); // Defines $plugin with version etc. 768 769 // Check if the legacy $module syntax is still used. 770 if (!is_object($module) or (count((array)$module) > 0)) { 771 throw new plugin_defective_exception($component, 'Unsupported $module syntax detected in version.php'); 772 } 773 774 // Prepare the record for the {modules} table. 775 $module = clone($plugin); 776 unset($module->version); 777 unset($module->component); 778 unset($module->dependencies); 779 unset($module->release); 780 781 if (empty($plugin->version)) { 782 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.'); 783 } 784 785 if (empty($plugin->component)) { 786 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.'); 787 } 788 789 if ($plugin->component !== $component) { 790 throw new plugin_misplaced_exception($plugin->component, null, $fullmod); 791 } 792 793 if (!empty($plugin->requires)) { 794 if ($plugin->requires > $CFG->version) { 795 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires); 796 } else if ($plugin->requires < 2010000000) { 797 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.'); 798 } 799 } 800 801 if (empty($module->cron)) { 802 $module->cron = 0; 803 } 804 805 // all modules must have en lang pack 806 if (!is_readable("$fullmod/lang/en/$mod.php")) { 807 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.'); 808 } 809 810 $module->name = $mod; // The name MUST match the directory 811 812 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching! 813 814 if (file_exists($fullmod.'/db/install.php')) { 815 if (get_config($module->name, 'installrunning')) { 816 require_once ($fullmod.'/db/install.php'); 817 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery'; 818 if (function_exists($recover_install_function)) { 819 $startcallback($component, true, $verbose); 820 $recover_install_function(); 821 unset_config('installrunning', $module->name); 822 // Install various components too 823 update_capabilities($component); 824 log_update_descriptions($component); 825 external_update_descriptions($component); 826 \core\task\manager::reset_scheduled_tasks_for_component($component); 827 \core_analytics\manager::update_default_models_for_component($component); 828 message_update_providers($component); 829 \core\message\inbound\manager::update_handlers_for_component($component); 830 upgrade_plugin_mnet_functions($component); 831 core_tag_area::reset_definitions_for_component($component); 832 $endcallback($component, true, $verbose); 833 } 834 } 835 } 836 837 if (empty($installedversion)) { 838 $startcallback($component, true, $verbose); 839 840 /// Execute install.xml (XMLDB) - must be present in all modules 841 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml'); 842 843 /// Add record into modules table - may be needed in install.php already 844 $module->id = $DB->insert_record('modules', $module); 845 upgrade_mod_savepoint(true, $plugin->version, $module->name, false); 846 847 /// Post installation hook - optional 848 if (file_exists("$fullmod/db/install.php")) { 849 require_once("$fullmod/db/install.php"); 850 // Set installation running flag, we need to recover after exception or error 851 set_config('installrunning', 1, $module->name); 852 $post_install_function = 'xmldb_'.$module->name.'_install'; 853 $post_install_function(); 854 unset_config('installrunning', $module->name); 855 } 856 857 /// Install various components 858 update_capabilities($component); 859 log_update_descriptions($component); 860 external_update_descriptions($component); 861 \core\task\manager::reset_scheduled_tasks_for_component($component); 862 \core_analytics\manager::update_default_models_for_component($component); 863 message_update_providers($component); 864 \core\message\inbound\manager::update_handlers_for_component($component); 865 upgrade_plugin_mnet_functions($component); 866 core_tag_area::reset_definitions_for_component($component); 867 868 $endcallback($component, true, $verbose); 869 870 } else if ($installedversion < $plugin->version) { 871 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue 872 $startcallback($component, false, $verbose); 873 874 if (is_readable($fullmod.'/db/upgrade.php')) { 875 require_once ($fullmod.'/db/upgrade.php'); // defines new upgrading function 876 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade'; 877 $result = $newupgrade_function($installedversion, $module); 878 } else { 879 $result = true; 880 } 881 882 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching! 883 $currmodule = $DB->get_record('modules', array('name'=>$module->name)); 884 if ($installedversion < $plugin->version) { 885 // store version if not already there 886 upgrade_mod_savepoint($result, $plugin->version, $mod, false); 887 } 888 889 // update cron flag if needed 890 if ($currmodule->cron != $module->cron) { 891 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name)); 892 } 893 894 // Upgrade various components 895 update_capabilities($component); 896 log_update_descriptions($component); 897 external_update_descriptions($component); 898 \core\task\manager::reset_scheduled_tasks_for_component($component); 899 \core_analytics\manager::update_default_models_for_component($component); 900 message_update_providers($component); 901 \core\message\inbound\manager::update_handlers_for_component($component); 902 upgrade_plugin_mnet_functions($component); 903 core_tag_area::reset_definitions_for_component($component); 904 905 $endcallback($component, false, $verbose); 906 907 } else if ($installedversion > $plugin->version) { 908 throw new downgrade_exception($component, $installedversion, $plugin->version); 909 } 910 } 911 } 912 913 914 /** 915 * This function finds all available blocks and install them 916 * into blocks table or do all the upgrade process if newer. 917 * 918 * @global object 919 * @global object 920 */ 921 function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) { 922 global $CFG, $DB; 923 924 require_once($CFG->dirroot.'/blocks/moodleblock.class.php'); 925 926 $blocktitles = array(); // we do not want duplicate titles 927 928 //Is this a first install 929 $first_install = null; 930 931 $blocks = core_component::get_plugin_list('block'); 932 933 foreach ($blocks as $blockname=>$fullblock) { 934 935 if (is_null($first_install)) { 936 $first_install = ($DB->count_records('block_instances') == 0); 937 } 938 939 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it 940 continue; 941 } 942 943 $component = clean_param('block_'.$blockname, PARAM_COMPONENT); 944 945 // check block dir is valid name 946 if (empty($component)) { 947 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.'); 948 } 949 950 if (!is_readable($fullblock.'/version.php')) { 951 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.'); 952 } 953 $plugin = new stdClass(); 954 $plugin->version = null; 955 $plugin->cron = 0; 956 $module = $plugin; // Prevent some notices when module placed in wrong directory. 957 include ($fullblock.'/version.php'); 958 unset($module); 959 $block = clone($plugin); 960 unset($block->version); 961 unset($block->component); 962 unset($block->dependencies); 963 unset($block->release); 964 965 if (empty($plugin->version)) { 966 throw new plugin_defective_exception($component, 'Missing block version number in version.php.'); 967 } 968 969 if (empty($plugin->component)) { 970 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.'); 971 } 972 973 if ($plugin->component !== $component) { 974 throw new plugin_misplaced_exception($plugin->component, null, $fullblock); 975 } 976 977 if (!empty($plugin->requires)) { 978 if ($plugin->requires > $CFG->version) { 979 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires); 980 } else if ($plugin->requires < 2010000000) { 981 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.'); 982 } 983 } 984 985 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) { 986 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.'); 987 } 988 include_once($fullblock.'/block_'.$blockname.'.php'); 989 990 $classname = 'block_'.$blockname; 991 992 if (!class_exists($classname)) { 993 throw new plugin_defective_exception($component, 'Can not load main class.'); 994 } 995 996 $blockobj = new $classname; // This is what we'll be testing 997 $blocktitle = $blockobj->get_title(); 998 999 // OK, it's as we all hoped. For further tests, the object will do them itself. 1000 if (!$blockobj->_self_test()) { 1001 throw new plugin_defective_exception($component, 'Self test failed.'); 1002 } 1003 1004 $block->name = $blockname; // The name MUST match the directory 1005 1006 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching! 1007 1008 if (file_exists($fullblock.'/db/install.php')) { 1009 if (get_config('block_'.$blockname, 'installrunning')) { 1010 require_once ($fullblock.'/db/install.php'); 1011 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery'; 1012 if (function_exists($recover_install_function)) { 1013 $startcallback($component, true, $verbose); 1014 $recover_install_function(); 1015 unset_config('installrunning', 'block_'.$blockname); 1016 // Install various components 1017 update_capabilities($component); 1018 log_update_descriptions($component); 1019 external_update_descriptions($component); 1020 \core\task\manager::reset_scheduled_tasks_for_component($component); 1021 \core_analytics\manager::update_default_models_for_component($component); 1022 message_update_providers($component); 1023 \core\message\inbound\manager::update_handlers_for_component($component); 1024 upgrade_plugin_mnet_functions($component); 1025 core_tag_area::reset_definitions_for_component($component); 1026 $endcallback($component, true, $verbose); 1027 } 1028 } 1029 } 1030 1031 if (empty($installedversion)) { // block not installed yet, so install it 1032 $conflictblock = array_search($blocktitle, $blocktitles); 1033 if ($conflictblock !== false) { 1034 // Duplicate block titles are not allowed, they confuse people 1035 // AND PHP's associative arrays ;) 1036 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock))); 1037 } 1038 $startcallback($component, true, $verbose); 1039 1040 if (file_exists($fullblock.'/db/install.xml')) { 1041 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml'); 1042 } 1043 $block->id = $DB->insert_record('block', $block); 1044 upgrade_block_savepoint(true, $plugin->version, $block->name, false); 1045 1046 if (file_exists($fullblock.'/db/install.php')) { 1047 require_once ($fullblock.'/db/install.php'); 1048 // Set installation running flag, we need to recover after exception or error 1049 set_config('installrunning', 1, 'block_'.$blockname); 1050 $post_install_function = 'xmldb_block_'.$blockname.'_install'; 1051 $post_install_function(); 1052 unset_config('installrunning', 'block_'.$blockname); 1053 } 1054 1055 $blocktitles[$block->name] = $blocktitle; 1056 1057 // Install various components 1058 update_capabilities($component); 1059 log_update_descriptions($component); 1060 external_update_descriptions($component); 1061 \core\task\manager::reset_scheduled_tasks_for_component($component); 1062 \core_analytics\manager::update_default_models_for_component($component); 1063 message_update_providers($component); 1064 \core\message\inbound\manager::update_handlers_for_component($component); 1065 core_tag_area::reset_definitions_for_component($component); 1066 upgrade_plugin_mnet_functions($component); 1067 1068 $endcallback($component, true, $verbose); 1069 1070 } else if ($installedversion < $plugin->version) { 1071 $startcallback($component, false, $verbose); 1072 1073 if (is_readable($fullblock.'/db/upgrade.php')) { 1074 require_once ($fullblock.'/db/upgrade.php'); // defines new upgrading function 1075 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade'; 1076 $result = $newupgrade_function($installedversion, $block); 1077 } else { 1078 $result = true; 1079 } 1080 1081 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching! 1082 $currblock = $DB->get_record('block', array('name'=>$block->name)); 1083 if ($installedversion < $plugin->version) { 1084 // store version if not already there 1085 upgrade_block_savepoint($result, $plugin->version, $block->name, false); 1086 } 1087 1088 if ($currblock->cron != $block->cron) { 1089 // update cron flag if needed 1090 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id)); 1091 } 1092 1093 // Upgrade various components 1094 update_capabilities($component); 1095 log_update_descriptions($component); 1096 external_update_descriptions($component); 1097 \core\task\manager::reset_scheduled_tasks_for_component($component); 1098 \core_analytics\manager::update_default_models_for_component($component); 1099 message_update_providers($component); 1100 \core\message\inbound\manager::update_handlers_for_component($component); 1101 upgrade_plugin_mnet_functions($component); 1102 core_tag_area::reset_definitions_for_component($component); 1103 1104 $endcallback($component, false, $verbose); 1105 1106 } else if ($installedversion > $plugin->version) { 1107 throw new downgrade_exception($component, $installedversion, $plugin->version); 1108 } 1109 } 1110 1111 1112 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks 1113 if ($first_install) { 1114 //Iterate over each course - there should be only site course here now 1115 if ($courses = $DB->get_records('course')) { 1116 foreach ($courses as $course) { 1117 blocks_add_default_course_blocks($course); 1118 } 1119 } 1120 1121 blocks_add_default_system_blocks(); 1122 } 1123 } 1124 1125 1126 /** 1127 * Log_display description function used during install and upgrade. 1128 * 1129 * @param string $component name of component (moodle, mod_assignment, etc.) 1130 * @return void 1131 */ 1132 function log_update_descriptions($component) { 1133 global $DB; 1134 1135 $defpath = core_component::get_component_directory($component).'/db/log.php'; 1136 1137 if (!file_exists($defpath)) { 1138 $DB->delete_records('log_display', array('component'=>$component)); 1139 return; 1140 } 1141 1142 // load new info 1143 $logs = array(); 1144 include($defpath); 1145 $newlogs = array(); 1146 foreach ($logs as $log) { 1147 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name 1148 } 1149 unset($logs); 1150 $logs = $newlogs; 1151 1152 $fields = array('module', 'action', 'mtable', 'field'); 1153 // update all log fist 1154 $dblogs = $DB->get_records('log_display', array('component'=>$component)); 1155 foreach ($dblogs as $dblog) { 1156 $name = $dblog->module.'-'.$dblog->action; 1157 1158 if (empty($logs[$name])) { 1159 $DB->delete_records('log_display', array('id'=>$dblog->id)); 1160 continue; 1161 } 1162 1163 $log = $logs[$name]; 1164 unset($logs[$name]); 1165 1166 $update = false; 1167 foreach ($fields as $field) { 1168 if ($dblog->$field != $log[$field]) { 1169 $dblog->$field = $log[$field]; 1170 $update = true; 1171 } 1172 } 1173 if ($update) { 1174 $DB->update_record('log_display', $dblog); 1175 } 1176 } 1177 foreach ($logs as $log) { 1178 $dblog = (object)$log; 1179 $dblog->component = $component; 1180 $DB->insert_record('log_display', $dblog); 1181 } 1182 } 1183 1184 /** 1185 * Web service discovery function used during install and upgrade. 1186 * @param string $component name of component (moodle, mod_assignment, etc.) 1187 * @return void 1188 */ 1189 function external_update_descriptions($component) { 1190 global $DB, $CFG; 1191 1192 $defpath = core_component::get_component_directory($component).'/db/services.php'; 1193 1194 if (!file_exists($defpath)) { 1195 require_once($CFG->dirroot.'/lib/externallib.php'); 1196 external_delete_descriptions($component); 1197 return; 1198 } 1199 1200 // load new info 1201 $functions = array(); 1202 $services = array(); 1203 include($defpath); 1204 1205 // update all function fist 1206 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component)); 1207 foreach ($dbfunctions as $dbfunction) { 1208 if (empty($functions[$dbfunction->name])) { 1209 $DB->delete_records('external_functions', array('id'=>$dbfunction->id)); 1210 // do not delete functions from external_services_functions, beacuse 1211 // we want to notify admins when functions used in custom services disappear 1212 1213 //TODO: this looks wrong, we have to delete it eventually (skodak) 1214 continue; 1215 } 1216 1217 $function = $functions[$dbfunction->name]; 1218 unset($functions[$dbfunction->name]); 1219 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath']; 1220 $function['methodname'] = $function['methodname'] ?? 'execute'; 1221 1222 $update = false; 1223 if ($dbfunction->classname != $function['classname']) { 1224 $dbfunction->classname = $function['classname']; 1225 $update = true; 1226 } 1227 if ($dbfunction->methodname != $function['methodname']) { 1228 $dbfunction->methodname = $function['methodname']; 1229 $update = true; 1230 } 1231 if ($dbfunction->classpath != $function['classpath']) { 1232 $dbfunction->classpath = $function['classpath']; 1233 $update = true; 1234 } 1235 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:''; 1236 if ($dbfunction->capabilities != $functioncapabilities) { 1237 $dbfunction->capabilities = $functioncapabilities; 1238 $update = true; 1239 } 1240 1241 if (isset($function['services']) and is_array($function['services'])) { 1242 sort($function['services']); 1243 $functionservices = implode(',', $function['services']); 1244 } else { 1245 // Force null values in the DB. 1246 $functionservices = null; 1247 } 1248 1249 if ($dbfunction->services != $functionservices) { 1250 // Now, we need to check if services were removed, in that case we need to remove the function from them. 1251 $servicesremoved = array_diff(explode(",", $dbfunction->services), explode(",", $functionservices)); 1252 foreach ($servicesremoved as $removedshortname) { 1253 if ($externalserviceid = $DB->get_field('external_services', 'id', array("shortname" => $removedshortname))) { 1254 $DB->delete_records('external_services_functions', array('functionname' => $dbfunction->name, 1255 'externalserviceid' => $externalserviceid)); 1256 } 1257 } 1258 1259 $dbfunction->services = $functionservices; 1260 $update = true; 1261 } 1262 if ($update) { 1263 $DB->update_record('external_functions', $dbfunction); 1264 } 1265 } 1266 foreach ($functions as $fname => $function) { 1267 $dbfunction = new stdClass(); 1268 $dbfunction->name = $fname; 1269 $dbfunction->classname = $function['classname']; 1270 $dbfunction->methodname = $function['methodname'] ?? 'execute'; 1271 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath']; 1272 $dbfunction->component = $component; 1273 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:''; 1274 1275 if (isset($function['services']) and is_array($function['services'])) { 1276 sort($function['services']); 1277 $dbfunction->services = implode(',', $function['services']); 1278 } else { 1279 // Force null values in the DB. 1280 $dbfunction->services = null; 1281 } 1282 1283 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction); 1284 } 1285 unset($functions); 1286 1287 // now deal with services 1288 $dbservices = $DB->get_records('external_services', array('component'=>$component)); 1289 foreach ($dbservices as $dbservice) { 1290 if (empty($services[$dbservice->name])) { 1291 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id)); 1292 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id)); 1293 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id)); 1294 $DB->delete_records('external_services', array('id'=>$dbservice->id)); 1295 continue; 1296 } 1297 $service = $services[$dbservice->name]; 1298 unset($services[$dbservice->name]); 1299 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled']; 1300 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability']; 1301 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers']; 1302 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles']; 1303 $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles']; 1304 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname']; 1305 1306 $update = false; 1307 if ($dbservice->requiredcapability != $service['requiredcapability']) { 1308 $dbservice->requiredcapability = $service['requiredcapability']; 1309 $update = true; 1310 } 1311 if ($dbservice->restrictedusers != $service['restrictedusers']) { 1312 $dbservice->restrictedusers = $service['restrictedusers']; 1313 $update = true; 1314 } 1315 if ($dbservice->downloadfiles != $service['downloadfiles']) { 1316 $dbservice->downloadfiles = $service['downloadfiles']; 1317 $update = true; 1318 } 1319 if ($dbservice->uploadfiles != $service['uploadfiles']) { 1320 $dbservice->uploadfiles = $service['uploadfiles']; 1321 $update = true; 1322 } 1323 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation) 1324 if (isset($service['shortname']) and 1325 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) { 1326 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']); 1327 } 1328 if ($dbservice->shortname != $service['shortname']) { 1329 //check that shortname is unique 1330 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null 1331 $existingservice = $DB->get_record('external_services', 1332 array('shortname' => $service['shortname'])); 1333 if (!empty($existingservice)) { 1334 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']); 1335 } 1336 } 1337 $dbservice->shortname = $service['shortname']; 1338 $update = true; 1339 } 1340 if ($update) { 1341 $DB->update_record('external_services', $dbservice); 1342 } 1343 1344 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id)); 1345 foreach ($functions as $function) { 1346 $key = array_search($function->functionname, $service['functions']); 1347 if ($key === false) { 1348 $DB->delete_records('external_services_functions', array('id'=>$function->id)); 1349 } else { 1350 unset($service['functions'][$key]); 1351 } 1352 } 1353 foreach ($service['functions'] as $fname) { 1354 $newf = new stdClass(); 1355 $newf->externalserviceid = $dbservice->id; 1356 $newf->functionname = $fname; 1357 $DB->insert_record('external_services_functions', $newf); 1358 } 1359 unset($functions); 1360 } 1361 foreach ($services as $name => $service) { 1362 //check that shortname is unique 1363 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null 1364 $existingservice = $DB->get_record('external_services', 1365 array('shortname' => $service['shortname'])); 1366 if (!empty($existingservice)) { 1367 throw new moodle_exception('installserviceshortnameerror', 'webservice'); 1368 } 1369 } 1370 1371 $dbservice = new stdClass(); 1372 $dbservice->name = $name; 1373 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled']; 1374 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability']; 1375 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers']; 1376 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles']; 1377 $dbservice->uploadfiles = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles']; 1378 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname']; 1379 $dbservice->component = $component; 1380 $dbservice->timecreated = time(); 1381 $dbservice->id = $DB->insert_record('external_services', $dbservice); 1382 foreach ($service['functions'] as $fname) { 1383 $newf = new stdClass(); 1384 $newf->externalserviceid = $dbservice->id; 1385 $newf->functionname = $fname; 1386 $DB->insert_record('external_services_functions', $newf); 1387 } 1388 } 1389 } 1390 1391 /** 1392 * Allow plugins and subsystems to add external functions to other plugins or built-in services. 1393 * This function is executed just after all the plugins have been updated. 1394 */ 1395 function external_update_services() { 1396 global $DB; 1397 1398 // Look for external functions that want to be added in existing services. 1399 $functions = $DB->get_records_select('external_functions', 'services IS NOT NULL'); 1400 1401 $servicescache = array(); 1402 foreach ($functions as $function) { 1403 // Prevent edge cases. 1404 if (empty($function->services)) { 1405 continue; 1406 } 1407 $services = explode(',', $function->services); 1408 1409 foreach ($services as $serviceshortname) { 1410 // Get the service id by shortname. 1411 if (!empty($servicescache[$serviceshortname])) { 1412 $serviceid = $servicescache[$serviceshortname]; 1413 } else if ($service = $DB->get_record('external_services', array('shortname' => $serviceshortname))) { 1414 // If the component is empty, it means that is not a built-in service. 1415 // We don't allow functions to inject themselves in services created by an user in Moodle. 1416 if (empty($service->component)) { 1417 continue; 1418 } 1419 $serviceid = $service->id; 1420 $servicescache[$serviceshortname] = $serviceid; 1421 } else { 1422 // Service not found. 1423 continue; 1424 } 1425 // Finally add the function to the service. 1426 $newf = new stdClass(); 1427 $newf->externalserviceid = $serviceid; 1428 $newf->functionname = $function->name; 1429 1430 if (!$DB->record_exists('external_services_functions', (array)$newf)) { 1431 $DB->insert_record('external_services_functions', $newf); 1432 } 1433 } 1434 } 1435 } 1436 1437 /** 1438 * upgrade logging functions 1439 */ 1440 function upgrade_handle_exception($ex, $plugin = null) { 1441 global $CFG; 1442 1443 // rollback everything, we need to log all upgrade problems 1444 abort_all_db_transactions(); 1445 1446 $info = get_exception_info($ex); 1447 1448 // First log upgrade error 1449 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace); 1450 1451 // Always turn on debugging - admins need to know what is going on 1452 set_debugging(DEBUG_DEVELOPER, true); 1453 1454 default_exception_handler($ex, true, $plugin); 1455 } 1456 1457 /** 1458 * Adds log entry into upgrade_log table 1459 * 1460 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR 1461 * @param string $plugin frankenstyle component name 1462 * @param string $info short description text of log entry 1463 * @param string $details long problem description 1464 * @param string $backtrace string used for errors only 1465 * @return void 1466 */ 1467 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) { 1468 global $DB, $USER, $CFG; 1469 1470 if (empty($plugin)) { 1471 $plugin = 'core'; 1472 } 1473 1474 list($plugintype, $pluginname) = core_component::normalize_component($plugin); 1475 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname; 1476 1477 $backtrace = format_backtrace($backtrace, true); 1478 1479 $currentversion = null; 1480 $targetversion = null; 1481 1482 //first try to find out current version number 1483 if ($plugintype === 'core') { 1484 //main 1485 $currentversion = $CFG->version; 1486 1487 $version = null; 1488 include("$CFG->dirroot/version.php"); 1489 $targetversion = $version; 1490 1491 } else { 1492 $pluginversion = get_config($component, 'version'); 1493 if (!empty($pluginversion)) { 1494 $currentversion = $pluginversion; 1495 } 1496 $cd = core_component::get_component_directory($component); 1497 if (file_exists("$cd/version.php")) { 1498 $plugin = new stdClass(); 1499 $plugin->version = null; 1500 $module = $plugin; 1501 include("$cd/version.php"); 1502 $targetversion = $plugin->version; 1503 } 1504 } 1505 1506 $log = new stdClass(); 1507 $log->type = $type; 1508 $log->plugin = $component; 1509 $log->version = $currentversion; 1510 $log->targetversion = $targetversion; 1511 $log->info = $info; 1512 $log->details = $details; 1513 $log->backtrace = $backtrace; 1514 $log->userid = $USER->id; 1515 $log->timemodified = time(); 1516 try { 1517 $DB->insert_record('upgrade_log', $log); 1518 } catch (Exception $ignored) { 1519 // possible during install or 2.0 upgrade 1520 } 1521 } 1522 1523 /** 1524 * Marks start of upgrade, blocks any other access to site. 1525 * The upgrade is finished at the end of script or after timeout. 1526 * 1527 * @global object 1528 * @global object 1529 * @global object 1530 */ 1531 function upgrade_started($preinstall=false) { 1532 global $CFG, $DB, $PAGE, $OUTPUT; 1533 1534 static $started = false; 1535 1536 if ($preinstall) { 1537 ignore_user_abort(true); 1538 upgrade_setup_debug(true); 1539 1540 } else if ($started) { 1541 upgrade_set_timeout(120); 1542 1543 } else { 1544 if (!CLI_SCRIPT and !$PAGE->headerprinted) { 1545 $strupgrade = get_string('upgradingversion', 'admin'); 1546 $PAGE->set_pagelayout('maintenance'); 1547 upgrade_init_javascript(); 1548 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release); 1549 $PAGE->set_heading($strupgrade); 1550 $PAGE->navbar->add($strupgrade); 1551 $PAGE->set_cacheable(false); 1552 echo $OUTPUT->header(); 1553 } 1554 1555 ignore_user_abort(true); 1556 core_shutdown_manager::register_function('upgrade_finished_handler'); 1557 upgrade_setup_debug(true); 1558 set_config('upgraderunning', time()+300); 1559 $started = true; 1560 } 1561 } 1562 1563 /** 1564 * Internal function - executed if upgrade interrupted. 1565 */ 1566 function upgrade_finished_handler() { 1567 upgrade_finished(); 1568 } 1569 1570 /** 1571 * Indicates upgrade is finished. 1572 * 1573 * This function may be called repeatedly. 1574 * 1575 * @global object 1576 * @global object 1577 */ 1578 function upgrade_finished($continueurl=null) { 1579 global $CFG, $DB, $OUTPUT; 1580 1581 if (!empty($CFG->upgraderunning)) { 1582 unset_config('upgraderunning'); 1583 // We have to forcefully purge the caches using the writer here. 1584 // This has to be done after we unset the config var. If someone hits the site while this is set they will 1585 // cause the config values to propogate to the caches. 1586 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between 1587 // then and now that leaving a window for things to fall out of sync. 1588 cache_helper::purge_all(true); 1589 upgrade_setup_debug(false); 1590 ignore_user_abort(false); 1591 if ($continueurl) { 1592 echo $OUTPUT->continue_button($continueurl); 1593 echo $OUTPUT->footer(); 1594 die; 1595 } 1596 } 1597 } 1598 1599 /** 1600 * @global object 1601 * @global object 1602 */ 1603 function upgrade_setup_debug($starting) { 1604 global $CFG, $DB; 1605 1606 static $originaldebug = null; 1607 1608 if ($starting) { 1609 if ($originaldebug === null) { 1610 $originaldebug = $DB->get_debug(); 1611 } 1612 if (!empty($CFG->upgradeshowsql)) { 1613 $DB->set_debug(true); 1614 } 1615 } else { 1616 $DB->set_debug($originaldebug); 1617 } 1618 } 1619 1620 function print_upgrade_separator() { 1621 if (!CLI_SCRIPT) { 1622 echo '<hr />'; 1623 } 1624 } 1625 1626 /** 1627 * Default start upgrade callback 1628 * @param string $plugin 1629 * @param bool $installation true if installation, false means upgrade 1630 */ 1631 function print_upgrade_part_start($plugin, $installation, $verbose) { 1632 global $OUTPUT; 1633 if (empty($plugin) or $plugin == 'moodle') { 1634 upgrade_started($installation); // does not store upgrade running flag yet 1635 if ($verbose) { 1636 echo $OUTPUT->heading(get_string('coresystem')); 1637 } 1638 } else { 1639 upgrade_started(); 1640 if ($verbose) { 1641 echo $OUTPUT->heading($plugin); 1642 } 1643 } 1644 if ($installation) { 1645 if (empty($plugin) or $plugin == 'moodle') { 1646 // no need to log - log table not yet there ;-) 1647 } else { 1648 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation'); 1649 } 1650 } else { 1651 core_upgrade_time::record_start(); 1652 if (empty($plugin) or $plugin == 'moodle') { 1653 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade'); 1654 } else { 1655 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade'); 1656 } 1657 } 1658 } 1659 1660 /** 1661 * Default end upgrade callback 1662 * @param string $plugin 1663 * @param bool $installation true if installation, false means upgrade 1664 */ 1665 function print_upgrade_part_end($plugin, $installation, $verbose) { 1666 global $OUTPUT; 1667 upgrade_started(); 1668 if ($installation) { 1669 if (empty($plugin) or $plugin == 'moodle') { 1670 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed'); 1671 } else { 1672 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed'); 1673 } 1674 } else { 1675 if (empty($plugin) or $plugin == 'moodle') { 1676 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded'); 1677 } else { 1678 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded'); 1679 } 1680 } 1681 if ($verbose) { 1682 if ($installation) { 1683 $message = get_string('success'); 1684 } else { 1685 $duration = core_upgrade_time::get_elapsed(); 1686 $message = get_string('successduration', '', format_float($duration, 2)); 1687 } 1688 $notification = new \core\output\notification($message, \core\output\notification::NOTIFY_SUCCESS); 1689 $notification->set_show_closebutton(false); 1690 echo $OUTPUT->render($notification); 1691 print_upgrade_separator(); 1692 } 1693 } 1694 1695 /** 1696 * Sets up JS code required for all upgrade scripts. 1697 * @global object 1698 */ 1699 function upgrade_init_javascript() { 1700 global $PAGE; 1701 // scroll to the end of each upgrade page so that ppl see either error or continue button, 1702 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-) 1703 $js = "window.scrollTo(0, 5000000);"; 1704 $PAGE->requires->js_init_code($js); 1705 } 1706 1707 /** 1708 * Try to upgrade the given language pack (or current language) 1709 * 1710 * @param string $lang the code of the language to update, defaults to the current language 1711 */ 1712 function upgrade_language_pack($lang = null) { 1713 global $CFG; 1714 1715 if (!empty($CFG->skiplangupgrade)) { 1716 return; 1717 } 1718 1719 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) { 1720 // weird, somebody uninstalled the import utility 1721 return; 1722 } 1723 1724 if (!$lang) { 1725 $lang = current_language(); 1726 } 1727 1728 if (!get_string_manager()->translation_exists($lang)) { 1729 return; 1730 } 1731 1732 get_string_manager()->reset_caches(); 1733 1734 if ($lang === 'en') { 1735 return; // Nothing to do 1736 } 1737 1738 upgrade_started(false); 1739 1740 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php"); 1741 tool_langimport_preupgrade_update($lang); 1742 1743 get_string_manager()->reset_caches(); 1744 1745 print_upgrade_separator(); 1746 } 1747 1748 /** 1749 * Build the current theme so that the user doesn't have to wait for it 1750 * to build on the first page load after the install / upgrade. 1751 */ 1752 function upgrade_themes() { 1753 global $CFG; 1754 1755 require_once("{$CFG->libdir}/outputlib.php"); 1756 1757 // Build the current theme so that the user can immediately 1758 // browse the site without having to wait for the theme to build. 1759 $themeconfig = theme_config::load($CFG->theme); 1760 $direction = right_to_left() ? 'rtl' : 'ltr'; 1761 theme_build_css_for_themes([$themeconfig], [$direction]); 1762 1763 // Only queue the task if there isn't already one queued. 1764 if (empty(\core\task\manager::get_adhoc_tasks('\\core\\task\\build_installed_themes_task'))) { 1765 // Queue a task to build all of the site themes at some point 1766 // later. These can happen offline because it doesn't block the 1767 // user unless they quickly change theme. 1768 $adhoctask = new \core\task\build_installed_themes_task(); 1769 \core\task\manager::queue_adhoc_task($adhoctask); 1770 } 1771 } 1772 1773 /** 1774 * Install core moodle tables and initialize 1775 * @param float $version target version 1776 * @param bool $verbose 1777 * @return void, may throw exception 1778 */ 1779 function install_core($version, $verbose) { 1780 global $CFG, $DB; 1781 1782 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty. 1783 remove_dir($CFG->cachedir.'', true); 1784 make_cache_directory('', true); 1785 1786 remove_dir($CFG->localcachedir.'', true); 1787 make_localcache_directory('', true); 1788 1789 remove_dir($CFG->tempdir.'', true); 1790 make_temp_directory('', true); 1791 1792 remove_dir($CFG->backuptempdir.'', true); 1793 make_backup_temp_directory('', true); 1794 1795 remove_dir($CFG->dataroot.'/muc', true); 1796 make_writable_directory($CFG->dataroot.'/muc', true); 1797 1798 try { 1799 core_php_time_limit::raise(600); 1800 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag 1801 1802 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml"); 1803 upgrade_started(); // we want the flag to be stored in config table ;-) 1804 1805 // set all core default records and default settings 1806 require_once("$CFG->libdir/db/install.php"); 1807 xmldb_main_install(); // installs the capabilities too 1808 1809 // store version 1810 upgrade_main_savepoint(true, $version, false); 1811 1812 // Continue with the installation 1813 log_update_descriptions('moodle'); 1814 external_update_descriptions('moodle'); 1815 \core\task\manager::reset_scheduled_tasks_for_component('moodle'); 1816 \core_analytics\manager::update_default_models_for_component('moodle'); 1817 message_update_providers('moodle'); 1818 \core\message\inbound\manager::update_handlers_for_component('moodle'); 1819 core_tag_area::reset_definitions_for_component('moodle'); 1820 1821 // Write default settings unconditionally 1822 admin_apply_default_settings(NULL, true); 1823 1824 print_upgrade_part_end(null, true, $verbose); 1825 1826 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something 1827 // during installation didn't use APIs. 1828 cache_helper::purge_all(); 1829 } catch (exception $ex) { 1830 upgrade_handle_exception($ex); 1831 } catch (Throwable $ex) { 1832 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5). 1833 upgrade_handle_exception($ex); 1834 } 1835 } 1836 1837 /** 1838 * Upgrade moodle core 1839 * @param float $version target version 1840 * @param bool $verbose 1841 * @return void, may throw exception 1842 */ 1843 function upgrade_core($version, $verbose) { 1844 global $CFG, $SITE, $DB, $COURSE; 1845 1846 raise_memory_limit(MEMORY_EXTRA); 1847 1848 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades 1849 1850 try { 1851 // Reset caches before any output. 1852 cache_helper::purge_all(true); 1853 purge_all_caches(); 1854 1855 // Upgrade current language pack if we can 1856 upgrade_language_pack(); 1857 1858 print_upgrade_part_start('moodle', false, $verbose); 1859 1860 // Pre-upgrade scripts for local hack workarounds. 1861 $preupgradefile = "$CFG->dirroot/local/preupgrade.php"; 1862 if (file_exists($preupgradefile)) { 1863 core_php_time_limit::raise(); 1864 require($preupgradefile); 1865 // Reset upgrade timeout to default. 1866 upgrade_set_timeout(); 1867 } 1868 1869 $result = xmldb_main_upgrade($CFG->version); 1870 if ($version > $CFG->version) { 1871 // store version if not already there 1872 upgrade_main_savepoint($result, $version, false); 1873 } 1874 1875 // In case structure of 'course' table has been changed and we forgot to update $SITE, re-read it from db. 1876 $SITE = $DB->get_record('course', array('id' => $SITE->id)); 1877 $COURSE = clone($SITE); 1878 1879 // perform all other component upgrade routines 1880 update_capabilities('moodle'); 1881 log_update_descriptions('moodle'); 1882 external_update_descriptions('moodle'); 1883 \core\task\manager::reset_scheduled_tasks_for_component('moodle'); 1884 \core_analytics\manager::update_default_models_for_component('moodle'); 1885 message_update_providers('moodle'); 1886 \core\message\inbound\manager::update_handlers_for_component('moodle'); 1887 core_tag_area::reset_definitions_for_component('moodle'); 1888 // Update core definitions. 1889 cache_helper::update_definitions(true); 1890 1891 // Purge caches again, just to be sure we arn't holding onto old stuff now. 1892 cache_helper::purge_all(true); 1893 purge_all_caches(); 1894 1895 // Clean up contexts - more and more stuff depends on existence of paths and contexts 1896 context_helper::cleanup_instances(); 1897 context_helper::create_instances(null, false); 1898 context_helper::build_all_paths(false); 1899 $syscontext = context_system::instance(); 1900 $syscontext->mark_dirty(); 1901 1902 print_upgrade_part_end('moodle', false, $verbose); 1903 } catch (Exception $ex) { 1904 upgrade_handle_exception($ex); 1905 } catch (Throwable $ex) { 1906 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5). 1907 upgrade_handle_exception($ex); 1908 } 1909 } 1910 1911 /** 1912 * Upgrade/install other parts of moodle 1913 * @param bool $verbose 1914 * @return void, may throw exception 1915 */ 1916 function upgrade_noncore($verbose) { 1917 global $CFG; 1918 1919 raise_memory_limit(MEMORY_EXTRA); 1920 1921 // upgrade all plugins types 1922 try { 1923 // Reset caches before any output. 1924 cache_helper::purge_all(true); 1925 purge_all_caches(); 1926 1927 $plugintypes = core_component::get_plugin_types(); 1928 foreach ($plugintypes as $type=>$location) { 1929 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose); 1930 } 1931 // Upgrade services. 1932 // This function gives plugins and subsystems a chance to add functions to existing built-in services. 1933 external_update_services(); 1934 1935 // Update cache definitions. Involves scanning each plugin for any changes. 1936 cache_helper::update_definitions(); 1937 // Mark the site as upgraded. 1938 set_config('allversionshash', core_component::get_all_versions_hash()); 1939 1940 // Purge caches again, just to be sure we arn't holding onto old stuff now. 1941 cache_helper::purge_all(true); 1942 purge_all_caches(); 1943 1944 } catch (Exception $ex) { 1945 upgrade_handle_exception($ex); 1946 } catch (Throwable $ex) { 1947 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5). 1948 upgrade_handle_exception($ex); 1949 } 1950 } 1951 1952 /** 1953 * Checks if the main tables have been installed yet or not. 1954 * 1955 * Note: we can not use caches here because they might be stale, 1956 * use with care! 1957 * 1958 * @return bool 1959 */ 1960 function core_tables_exist() { 1961 global $DB; 1962 1963 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all. 1964 return false; 1965 1966 } else { // Check for missing main tables 1967 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml 1968 foreach ($mtables as $mtable) { 1969 if (!in_array($mtable, $tables)) { 1970 return false; 1971 } 1972 } 1973 return true; 1974 } 1975 } 1976 1977 /** 1978 * upgrades the mnet rpc definitions for the given component. 1979 * this method doesn't return status, an exception will be thrown in the case of an error 1980 * 1981 * @param string $component the plugin to upgrade, eg auth_mnet 1982 */ 1983 function upgrade_plugin_mnet_functions($component) { 1984 global $DB, $CFG; 1985 1986 list($type, $plugin) = core_component::normalize_component($component); 1987 $path = core_component::get_plugin_directory($type, $plugin); 1988 1989 $publishes = array(); 1990 $subscribes = array(); 1991 if (file_exists($path . '/db/mnet.php')) { 1992 require_once($path . '/db/mnet.php'); // $publishes comes from this file 1993 } 1994 if (empty($publishes)) { 1995 $publishes = array(); // still need this to be able to disable stuff later 1996 } 1997 if (empty($subscribes)) { 1998 $subscribes = array(); // still need this to be able to disable stuff later 1999 } 2000 2001 static $servicecache = array(); 2002 2003 // rekey an array based on the rpc method for easy lookups later 2004 $publishmethodservices = array(); 2005 $subscribemethodservices = array(); 2006 foreach($publishes as $servicename => $service) { 2007 if (is_array($service['methods'])) { 2008 foreach($service['methods'] as $methodname) { 2009 $service['servicename'] = $servicename; 2010 $publishmethodservices[$methodname][] = $service; 2011 } 2012 } 2013 } 2014 2015 // Disable functions that don't exist (any more) in the source 2016 // Should these be deleted? What about their permissions records? 2017 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) { 2018 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) { 2019 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id)); 2020 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) { 2021 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id)); 2022 } 2023 } 2024 2025 // reflect all the services we're publishing and save them 2026 static $cachedclasses = array(); // to store reflection information in 2027 foreach ($publishes as $service => $data) { 2028 $f = $data['filename']; 2029 $c = $data['classname']; 2030 foreach ($data['methods'] as $method) { 2031 $dataobject = new stdClass(); 2032 $dataobject->plugintype = $type; 2033 $dataobject->pluginname = $plugin; 2034 $dataobject->enabled = 1; 2035 $dataobject->classname = $c; 2036 $dataobject->filename = $f; 2037 2038 if (is_string($method)) { 2039 $dataobject->functionname = $method; 2040 2041 } else if (is_array($method)) { // wants to override file or class 2042 $dataobject->functionname = $method['method']; 2043 $dataobject->classname = $method['classname']; 2044 $dataobject->filename = $method['filename']; 2045 } 2046 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method; 2047 $dataobject->static = false; 2048 2049 require_once($path . '/' . $dataobject->filename); 2050 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function 2051 if (!empty($dataobject->classname)) { 2052 if (!class_exists($dataobject->classname)) { 2053 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname)); 2054 } 2055 $key = $dataobject->filename . '|' . $dataobject->classname; 2056 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object 2057 try { 2058 $cachedclasses[$key] = new ReflectionClass($dataobject->classname); 2059 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful 2060 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage())); 2061 } 2062 } 2063 $r =& $cachedclasses[$key]; 2064 if (!$r->hasMethod($dataobject->functionname)) { 2065 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname)); 2066 } 2067 $functionreflect = $r->getMethod($dataobject->functionname); 2068 $dataobject->static = (int)$functionreflect->isStatic(); 2069 } else { 2070 if (!function_exists($dataobject->functionname)) { 2071 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename)); 2072 } 2073 try { 2074 $functionreflect = new ReflectionFunction($dataobject->functionname); 2075 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful 2076 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage())); 2077 } 2078 } 2079 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect)); 2080 $dataobject->help = admin_mnet_method_get_help($functionreflect); 2081 2082 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) { 2083 $dataobject->id = $record_exists->id; 2084 $dataobject->enabled = $record_exists->enabled; 2085 $DB->update_record('mnet_rpc', $dataobject); 2086 } else { 2087 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true); 2088 } 2089 2090 // TODO this API versioning must be reworked, here the recently processed method 2091 // sets the service API which may not be correct 2092 foreach ($publishmethodservices[$dataobject->functionname] as $service) { 2093 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) { 2094 $serviceobj->apiversion = $service['apiversion']; 2095 $DB->update_record('mnet_service', $serviceobj); 2096 } else { 2097 $serviceobj = new stdClass(); 2098 $serviceobj->name = $service['servicename']; 2099 $serviceobj->description = empty($service['description']) ? '' : $service['description']; 2100 $serviceobj->apiversion = $service['apiversion']; 2101 $serviceobj->offer = 1; 2102 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj); 2103 } 2104 $servicecache[$service['servicename']] = $serviceobj; 2105 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) { 2106 $obj = new stdClass(); 2107 $obj->rpcid = $dataobject->id; 2108 $obj->serviceid = $serviceobj->id; 2109 $DB->insert_record('mnet_service2rpc', $obj, true); 2110 } 2111 } 2112 } 2113 } 2114 // finished with methods we publish, now do subscribable methods 2115 foreach($subscribes as $service => $methods) { 2116 if (!array_key_exists($service, $servicecache)) { 2117 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) { 2118 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993"); 2119 continue; 2120 } 2121 $servicecache[$service] = $serviceobj; 2122 } else { 2123 $serviceobj = $servicecache[$service]; 2124 } 2125 foreach ($methods as $method => $xmlrpcpath) { 2126 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) { 2127 $remoterpc = (object)array( 2128 'functionname' => $method, 2129 'xmlrpcpath' => $xmlrpcpath, 2130 'plugintype' => $type, 2131 'pluginname' => $plugin, 2132 'enabled' => 1, 2133 ); 2134 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true); 2135 } 2136 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) { 2137 $obj = new stdClass(); 2138 $obj->rpcid = $rpcid; 2139 $obj->serviceid = $serviceobj->id; 2140 $DB->insert_record('mnet_remote_service2rpc', $obj, true); 2141 } 2142 $subscribemethodservices[$method][] = $service; 2143 } 2144 } 2145 2146 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) { 2147 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) { 2148 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id)); 2149 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) { 2150 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id)); 2151 } 2152 } 2153 2154 return true; 2155 } 2156 2157 /** 2158 * Given some sort of reflection function/method object, return a profile array, ready to be serialized and stored 2159 * 2160 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information 2161 * 2162 * @return array associative array with function/method information 2163 */ 2164 function admin_mnet_method_profile(ReflectionFunctionAbstract $function) { 2165 $commentlines = admin_mnet_method_get_docblock($function); 2166 $getkey = function($key) use ($commentlines) { 2167 return array_values(array_filter($commentlines, function($line) use ($key) { 2168 return $line[0] == $key; 2169 })); 2170 }; 2171 $returnline = $getkey('@return'); 2172 return array ( 2173 'parameters' => array_map(function($line) { 2174 return array( 2175 'name' => trim($line[2], " \t\n\r\0\x0B$"), 2176 'type' => $line[1], 2177 'description' => $line[3] 2178 ); 2179 }, $getkey('@param')), 2180 2181 'return' => array( 2182 'type' => !empty($returnline[0][1]) ? $returnline[0][1] : 'void', 2183 'description' => !empty($returnline[0][2]) ? $returnline[0][2] : '' 2184 ) 2185 ); 2186 } 2187 2188 /** 2189 * Given some sort of reflection function/method object, return an array of docblock lines, where each line is an array of 2190 * keywords/descriptions 2191 * 2192 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information 2193 * 2194 * @return array docblock converted in to an array 2195 */ 2196 function admin_mnet_method_get_docblock(ReflectionFunctionAbstract $function) { 2197 return array_map(function($line) { 2198 $text = trim($line, " \t\n\r\0\x0B*/"); 2199 if (strpos($text, '@param') === 0) { 2200 return preg_split('/\s+/', $text, 4); 2201 } 2202 2203 if (strpos($text, '@return') === 0) { 2204 return preg_split('/\s+/', $text, 3); 2205 } 2206 2207 return array($text); 2208 }, explode("\n", $function->getDocComment())); 2209 } 2210 2211 /** 2212 * Given some sort of reflection function/method object, return just the help text 2213 * 2214 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information 2215 * 2216 * @return string docblock help text 2217 */ 2218 function admin_mnet_method_get_help(ReflectionFunctionAbstract $function) { 2219 $helplines = array_map(function($line) { 2220 return implode(' ', $line); 2221 }, array_values(array_filter(admin_mnet_method_get_docblock($function), function($line) { 2222 return strpos($line[0], '@') !== 0 && !empty($line[0]); 2223 }))); 2224 2225 return implode("\n", $helplines); 2226 } 2227 2228 /** 2229 * This function verifies that the database is not using an unsupported storage engine. 2230 * 2231 * @param environment_results $result object to update, if relevant 2232 * @return environment_results|null updated results object, or null if the storage engine is supported 2233 */ 2234 function check_database_storage_engine(environment_results $result) { 2235 global $DB; 2236 2237 // Check if MySQL is the DB family (this will also be the same for MariaDB). 2238 if ($DB->get_dbfamily() == 'mysql') { 2239 // Get the database engine we will either be using to install the tables, or what we are currently using. 2240 $engine = $DB->get_dbengine(); 2241 // Check if MyISAM is the storage engine that will be used, if so, do not proceed and display an error. 2242 if ($engine == 'MyISAM') { 2243 $result->setInfo('unsupported_db_storage_engine'); 2244 $result->setStatus(false); 2245 return $result; 2246 } 2247 } 2248 2249 return null; 2250 } 2251 2252 /** 2253 * Method used to check the usage of slasharguments config and display a warning message. 2254 * 2255 * @param environment_results $result object to update, if relevant. 2256 * @return environment_results|null updated results or null if slasharguments is disabled. 2257 */ 2258 function check_slasharguments(environment_results $result){ 2259 global $CFG; 2260 2261 if (!during_initial_install() && empty($CFG->slasharguments)) { 2262 $result->setInfo('slasharguments'); 2263 $result->setStatus(false); 2264 return $result; 2265 } 2266 2267 return null; 2268 } 2269 2270 /** 2271 * This function verifies if the database has tables using innoDB Antelope row format. 2272 * 2273 * @param environment_results $result 2274 * @return environment_results|null updated results object, or null if no Antelope table has been found. 2275 */ 2276 function check_database_tables_row_format(environment_results $result) { 2277 global $DB; 2278 2279 if ($DB->get_dbfamily() == 'mysql') { 2280 $generator = $DB->get_manager()->generator; 2281 2282 foreach ($DB->get_tables(false) as $table) { 2283 $columns = $DB->get_columns($table, false); 2284 $size = $generator->guess_antelope_row_size($columns); 2285 $format = $DB->get_row_format($table); 2286 2287 if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) { 2288 continue; 2289 } 2290 2291 if ($format === 'Compact' or $format === 'Redundant') { 2292 $result->setInfo('unsupported_db_table_row_format'); 2293 $result->setStatus(false); 2294 return $result; 2295 } 2296 } 2297 } 2298 2299 return null; 2300 } 2301 2302 /** 2303 * This function verfies that the database has tables using InnoDB Antelope row format. 2304 * 2305 * @param environment_results $result 2306 * @return environment_results|null updated results object, or null if no Antelope table has been found. 2307 */ 2308 function check_mysql_file_format(environment_results $result) { 2309 global $DB; 2310 2311 if ($DB->get_dbfamily() == 'mysql') { 2312 $collation = $DB->get_dbcollation(); 2313 $collationinfo = explode('_', $collation); 2314 $charset = reset($collationinfo); 2315 2316 if ($charset == 'utf8mb4') { 2317 if ($DB->get_row_format() !== "Barracuda") { 2318 $result->setInfo('mysql_full_unicode_support#File_format'); 2319 $result->setStatus(false); 2320 return $result; 2321 } 2322 } 2323 } 2324 return null; 2325 } 2326 2327 /** 2328 * This function verfies that the database has a setting of one file per table. This is required for 'utf8mb4'. 2329 * 2330 * @param environment_results $result 2331 * @return environment_results|null updated results object, or null if innodb_file_per_table = 1. 2332 */ 2333 function check_mysql_file_per_table(environment_results $result) { 2334 global $DB; 2335 2336 if ($DB->get_dbfamily() == 'mysql') { 2337 $collation = $DB->get_dbcollation(); 2338 $collationinfo = explode('_', $collation); 2339 $charset = reset($collationinfo); 2340 2341 if ($charset == 'utf8mb4') { 2342 if (!$DB->is_file_per_table_enabled()) { 2343 $result->setInfo('mysql_full_unicode_support#File_per_table'); 2344 $result->setStatus(false); 2345 return $result; 2346 } 2347 } 2348 } 2349 return null; 2350 } 2351 2352 /** 2353 * This function verfies that the database has the setting of large prefix enabled. This is required for 'utf8mb4'. 2354 * 2355 * @param environment_results $result 2356 * @return environment_results|null updated results object, or null if innodb_large_prefix = 1. 2357 */ 2358 function check_mysql_large_prefix(environment_results $result) { 2359 global $DB; 2360 2361 if ($DB->get_dbfamily() == 'mysql') { 2362 $collation = $DB->get_dbcollation(); 2363 $collationinfo = explode('_', $collation); 2364 $charset = reset($collationinfo); 2365 2366 if ($charset == 'utf8mb4') { 2367 if (!$DB->is_large_prefix_enabled()) { 2368 $result->setInfo('mysql_full_unicode_support#Large_prefix'); 2369 $result->setStatus(false); 2370 return $result; 2371 } 2372 } 2373 } 2374 return null; 2375 } 2376 2377 /** 2378 * This function checks the database to see if it is using incomplete unicode support. 2379 * 2380 * @param environment_results $result $result 2381 * @return environment_results|null updated results object, or null if unicode is fully supported. 2382 */ 2383 function check_mysql_incomplete_unicode_support(environment_results $result) { 2384 global $DB; 2385 2386 if ($DB->get_dbfamily() == 'mysql') { 2387 $collation = $DB->get_dbcollation(); 2388 $collationinfo = explode('_', $collation); 2389 $charset = reset($collationinfo); 2390 2391 if ($charset == 'utf8') { 2392 $result->setInfo('mysql_full_unicode_support'); 2393 $result->setStatus(false); 2394 return $result; 2395 } 2396 } 2397 return null; 2398 } 2399 2400 /** 2401 * Check if the site is being served using an ssl url. 2402 * 2403 * Note this does not really perform any request neither looks for proxies or 2404 * other situations. Just looks to wwwroot and warn if it's not using https. 2405 * 2406 * @param environment_results $result $result 2407 * @return environment_results|null updated results object, or null if the site is https. 2408 */ 2409 function check_is_https(environment_results $result) { 2410 global $CFG; 2411 2412 // Only if is defined, non-empty and whatever core tell us. 2413 if (!empty($CFG->wwwroot) && !is_https()) { 2414 $result->setInfo('site not https'); 2415 $result->setStatus(false); 2416 return $result; 2417 } 2418 return null; 2419 } 2420 2421 /** 2422 * Check if the site is using 64 bits PHP. 2423 * 2424 * @param environment_results $result 2425 * @return environment_results|null updated results object, or null if the site is using 64 bits PHP. 2426 */ 2427 function check_sixtyfour_bits(environment_results $result) { 2428 2429 if (PHP_INT_SIZE === 4) { 2430 $result->setInfo('php not 64 bits'); 2431 $result->setStatus(false); 2432 return $result; 2433 } 2434 return null; 2435 } 2436 2437 /** 2438 * Check if the igbinary extension installed is buggy one 2439 * 2440 * There are a few php-igbinary versions that are buggy and 2441 * return any unserialised array with wrong index. This defeats 2442 * key() and next() operations on them. 2443 * 2444 * This library is used by MUC and also by memcached and redis 2445 * when available. 2446 * 2447 * Let's inform if there is some problem when: 2448 * - php 7.2 is being used (php 7.3 and up are immune). 2449 * - the igbinary extension is installed. 2450 * - the version of the extension is between 3.2.2 and 3.2.4. 2451 * - the buggy behaviour is reproduced. 2452 * 2453 * @param environment_results $result object to update, if relevant. 2454 * @return environment_results|null updated results or null. 2455 */ 2456 function check_igbinary322_version(environment_results $result) { 2457 2458 // No problem if using PHP version 7.3 and up. 2459 $phpversion = normalize_version(phpversion()); 2460 if (version_compare($phpversion, '7.3', '>=')) { 2461 return null; 2462 } 2463 2464 // No problem if igbinary is not installed.. 2465 if (!function_exists('igbinary_serialize')) { 2466 return null; 2467 } 2468 2469 // No problem if using igbinary < 3.2.2 or > 3.2.4. 2470 $igbinaryversion = normalize_version(phpversion('igbinary')); 2471 if (version_compare($igbinaryversion, '3.2.2', '<') or version_compare($igbinaryversion, '3.2.4', '>')) { 2472 return null; 2473 } 2474 2475 // Let's verify the real behaviour to see if the bug is around. 2476 // Note that we need this extra check because they released 3.2.5 with 3.2.4 version number, so 2477 // over the paper, there are 3.2.4 working versions (3.2.5 ones with messed reflection version). 2478 $data = [1, 2, 3]; 2479 $data = igbinary_unserialize(igbinary_serialize($data)); 2480 if (key($data) === 0) { 2481 return null; 2482 } 2483 2484 // Arrived here, we are using PHP 7.2 and a buggy verified igbinary version, let's inform and don't allow to continue. 2485 $result->setInfo('igbinary version problem'); 2486 $result->setStatus(false); 2487 return $result; 2488 } 2489 2490 /** 2491 * This function checks that the database prefix ($CFG->prefix) is <= 10 2492 * 2493 * @param environment_results $result 2494 * @return environment_results|null updated results object, or null if the prefix check is passing ok. 2495 */ 2496 function check_db_prefix_length(environment_results $result) { 2497 global $CFG; 2498 2499 $prefixlen = strlen($CFG->prefix) ?? 0; 2500 if ($prefixlen > 10) { 2501 $parameters = (object)['current' => $prefixlen, 'maximum' => 10]; 2502 $result->setFeedbackStr(['dbprefixtoolong', 'admin', $parameters]); 2503 $result->setInfo('db prefix too long'); 2504 $result->setStatus(false); 2505 return $result; 2506 } 2507 return null; // All, good. By returning null we hide the check. 2508 } 2509 2510 /** 2511 * Assert the upgrade key is provided, if it is defined. 2512 * 2513 * The upgrade key can be defined in the main config.php as $CFG->upgradekey. If 2514 * it is defined there, then its value must be provided every time the site is 2515 * being upgraded, regardless the administrator is logged in or not. 2516 * 2517 * This is supposed to be used at certain places in /admin/index.php only. 2518 * 2519 * @param string|null $upgradekeyhash the SHA-1 of the value provided by the user 2520 */ 2521 function check_upgrade_key($upgradekeyhash) { 2522 global $CFG, $PAGE; 2523 2524 if (isset($CFG->config_php_settings['upgradekey'])) { 2525 if ($upgradekeyhash === null or $upgradekeyhash !== sha1($CFG->config_php_settings['upgradekey'])) { 2526 if (!$PAGE->headerprinted) { 2527 $PAGE->set_title(get_string('upgradekeyreq', 'admin')); 2528 $output = $PAGE->get_renderer('core', 'admin'); 2529 echo $output->upgradekey_form_page(new moodle_url('/admin/index.php', array('cache' => 0))); 2530 die(); 2531 } else { 2532 // This should not happen. 2533 die('Upgrade locked'); 2534 } 2535 } 2536 } 2537 } 2538 2539 /** 2540 * Helper procedure/macro for installing remote plugins at admin/index.php 2541 * 2542 * Does not return, always redirects or exits. 2543 * 2544 * @param array $installable list of \core\update\remote_info 2545 * @param bool $confirmed false: display the validation screen, true: proceed installation 2546 * @param string $heading validation screen heading 2547 * @param moodle_url|string|null $continue URL to proceed with installation at the validation screen 2548 * @param moodle_url|string|null $return URL to go back on cancelling at the validation screen 2549 */ 2550 function upgrade_install_plugins(array $installable, $confirmed, $heading='', $continue=null, $return=null) { 2551 global $CFG, $PAGE; 2552 2553 if (empty($return)) { 2554 $return = $PAGE->url; 2555 } 2556 2557 if (!empty($CFG->disableupdateautodeploy)) { 2558 redirect($return); 2559 } 2560 2561 if (empty($installable)) { 2562 redirect($return); 2563 } 2564 2565 $pluginman = core_plugin_manager::instance(); 2566 2567 if ($confirmed) { 2568 // Installation confirmed at the validation results page. 2569 if (!$pluginman->install_plugins($installable, true, true)) { 2570 throw new moodle_exception('install_plugins_failed', 'core_plugin', $return); 2571 } 2572 2573 // Always redirect to admin/index.php to perform the database upgrade. 2574 // Do not throw away the existing $PAGE->url parameters such as 2575 // confirmupgrade or confirmrelease if $PAGE->url is a superset of the 2576 // URL we must go to. 2577 $mustgoto = new moodle_url('/admin/index.php', array('cache' => 0, 'confirmplugincheck' => 0)); 2578 if ($mustgoto->compare($PAGE->url, URL_MATCH_PARAMS)) { 2579 redirect($PAGE->url); 2580 } else { 2581 redirect($mustgoto); 2582 } 2583 2584 } else { 2585 $output = $PAGE->get_renderer('core', 'admin'); 2586 echo $output->header(); 2587 if ($heading) { 2588 echo $output->heading($heading, 3); 2589 } 2590 echo html_writer::start_tag('pre', array('class' => 'plugin-install-console')); 2591 $validated = $pluginman->install_plugins($installable, false, false); 2592 echo html_writer::end_tag('pre'); 2593 if ($validated) { 2594 echo $output->plugins_management_confirm_buttons($continue, $return); 2595 } else { 2596 echo $output->plugins_management_confirm_buttons(null, $return); 2597 } 2598 echo $output->footer(); 2599 die(); 2600 } 2601 } 2602 /** 2603 * Method used to check the installed unoconv version. 2604 * 2605 * @param environment_results $result object to update, if relevant. 2606 * @return environment_results|null updated results or null if unoconv path is not executable. 2607 */ 2608 function check_unoconv_version(environment_results $result) { 2609 global $CFG; 2610 2611 if (!during_initial_install() && !empty($CFG->pathtounoconv) && file_is_executable(trim($CFG->pathtounoconv))) { 2612 $currentversion = 0; 2613 $supportedversion = 0.7; 2614 $unoconvbin = \escapeshellarg($CFG->pathtounoconv); 2615 $command = "$unoconvbin --version"; 2616 exec($command, $output); 2617 2618 // If the command execution returned some output, then get the unoconv version. 2619 if ($output) { 2620 foreach ($output as $response) { 2621 if (preg_match('/unoconv (\\d+\\.\\d+)/', $response, $matches)) { 2622 $currentversion = (float)$matches[1]; 2623 } 2624 } 2625 } 2626 2627 if ($currentversion < $supportedversion) { 2628 $result->setInfo('unoconv version not supported'); 2629 $result->setStatus(false); 2630 return $result; 2631 } 2632 } 2633 return null; 2634 } 2635 2636 /** 2637 * Checks for up-to-date TLS libraries. NOTE: this is not currently used, see MDL-57262. 2638 * 2639 * @param environment_results $result object to update, if relevant. 2640 * @return environment_results|null updated results or null if unoconv path is not executable. 2641 */ 2642 function check_tls_libraries(environment_results $result) { 2643 global $CFG; 2644 2645 if (!function_exists('curl_version')) { 2646 $result->setInfo('cURL PHP extension is not installed'); 2647 $result->setStatus(false); 2648 return $result; 2649 } 2650 2651 if (!\core\upgrade\util::validate_php_curl_tls(curl_version(), PHP_ZTS)) { 2652 $result->setInfo('invalid ssl/tls configuration'); 2653 $result->setStatus(false); 2654 return $result; 2655 } 2656 2657 if (!\core\upgrade\util::can_use_tls12(curl_version(), php_uname('r'))) { 2658 $result->setInfo('ssl/tls configuration not supported'); 2659 $result->setStatus(false); 2660 return $result; 2661 } 2662 2663 return null; 2664 } 2665 2666 /** 2667 * Check if recommended version of libcurl is installed or not. 2668 * 2669 * @param environment_results $result object to update, if relevant. 2670 * @return environment_results|null updated results or null. 2671 */ 2672 function check_libcurl_version(environment_results $result) { 2673 2674 if (!function_exists('curl_version')) { 2675 $result->setInfo('cURL PHP extension is not installed'); 2676 $result->setStatus(false); 2677 return $result; 2678 } 2679 2680 // Supported version and version number. 2681 $supportedversion = 0x071304; 2682 $supportedversionstring = "7.19.4"; 2683 2684 // Installed version. 2685 $curlinfo = curl_version(); 2686 $currentversion = $curlinfo['version_number']; 2687 2688 if ($currentversion < $supportedversion) { 2689 // Test fail. 2690 // Set info, we want to let user know how to resolve the problem. 2691 $result->setInfo('Libcurl version check'); 2692 $result->setNeededVersion($supportedversionstring); 2693 $result->setCurrentVersion($curlinfo['version']); 2694 $result->setStatus(false); 2695 return $result; 2696 } 2697 2698 return null; 2699 } 2700 2701 /** 2702 * Environment check for the php setting max_input_vars 2703 * 2704 * @param environment_results $result 2705 * @return environment_results|null 2706 */ 2707 function check_max_input_vars(environment_results $result) { 2708 $max = (int)ini_get('max_input_vars'); 2709 if ($max < 5000) { 2710 $result->setInfo('max_input_vars'); 2711 $result->setStatus(false); 2712 if (PHP_VERSION_ID >= 80000) { 2713 // For PHP8 this check is required. 2714 $result->setLevel('required'); 2715 $result->setFeedbackStr('settingmaxinputvarsrequired'); 2716 } else { 2717 // For PHP7 this check is optional (recommended). 2718 $result->setFeedbackStr('settingmaxinputvars'); 2719 } 2720 return $result; 2721 } 2722 return null; 2723 } 2724 2725 /** 2726 * Check whether the admin directory has been configured and warn if so. 2727 * 2728 * The admin directory has been deprecated since Moodle 4.0. 2729 * 2730 * @param environment_results $result 2731 * @return null|environment_results 2732 */ 2733 function check_admin_dir_usage(environment_results $result): ?environment_results { 2734 global $CFG; 2735 2736 if (empty($CFG->admin)) { 2737 return null; 2738 } 2739 2740 if ($CFG->admin === 'admin') { 2741 return null; 2742 } 2743 2744 $result->setInfo('admin_dir_usage'); 2745 $result->setStatus(false); 2746 2747 return $result; 2748 } 2749 2750 /** 2751 * Check whether the XML-RPC protocol is enabled and warn if so. 2752 * 2753 * The XML-RPC protocol will be removed in a future version (4.1) as it is no longer supported by PHP. 2754 * 2755 * See MDL-70889 for further information. 2756 * 2757 * @param environment_results $result 2758 * @return null|environment_results 2759 */ 2760 function check_xmlrpc_usage(environment_results $result): ?environment_results { 2761 global $CFG; 2762 2763 // Checking Web Service protocols. 2764 if (!empty($CFG->webserviceprotocols)) { 2765 $plugins = array_flip(explode(',', $CFG->webserviceprotocols)); 2766 if (array_key_exists('xmlrpc', $plugins)) { 2767 $result->setInfo('xmlrpc_webservice_usage'); 2768 $result->setFeedbackStr('xmlrpcwebserviceenabled'); 2769 return $result; 2770 } 2771 } 2772 2773 if (isset($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode == 'strict') { 2774 // Checking Mnet hosts. 2775 $mnethosts = mnet_get_hosts(); 2776 if ($mnethosts) { 2777 $actualhost = 0; 2778 foreach ($mnethosts as $mnethost) { 2779 if ($mnethost->id != $CFG->mnet_all_hosts_id) { 2780 $actualhost++; 2781 } 2782 } 2783 if ($actualhost > 0) { 2784 $result->setInfo('xmlrpc_mnet_usage'); 2785 $result->setFeedbackStr('xmlrpcmnetenabled'); 2786 return $result; 2787 } 2788 } 2789 2790 // Checking Mahara. 2791 $portfolios = \core\plugininfo\portfolio::get_enabled_plugins(); 2792 if (array_key_exists('mahara', $portfolios)) { 2793 $result->setInfo('xmlrpc_mahara_usage'); 2794 $result->setFeedbackStr('xmlrpcmaharaenabled'); 2795 return $result; 2796 } 2797 } 2798 2799 return null; 2800 } 2801 2802 /** 2803 * Check whether the mod_assignment is currently being used. 2804 * 2805 * @param environment_results $result 2806 * @return environment_results|null 2807 */ 2808 function check_mod_assignment(environment_results $result): ?environment_results { 2809 global $DB, $CFG; 2810 2811 // Check the number of records. 2812 if ($DB->get_manager()->table_exists('assignment') && $DB->count_records('assignment') > 0) { 2813 $result->setInfo('Assignment 2.2 is in use'); 2814 $result->setFeedbackStr('modassignmentinuse'); 2815 return $result; 2816 } 2817 2818 // Check for mod_assignment subplugins. 2819 if (is_dir($CFG->dirroot . '/mod/assignment/type')) { 2820 $result->setInfo('Assignment 2.2 subplugins present'); 2821 $result->setFeedbackStr('modassignmentsubpluginsexist'); 2822 return $result; 2823 } 2824 2825 return null; 2826 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body