Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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