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
 * Main administration script.
20
 *
21
 * @package    core
22
 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
// Check that config.php exists, if not then call the install script
27
if (!file_exists('../config.php')) {
28
    header('Location: ../install.php');
29
    die();
30
}
31
 
32
// Check that PHP is of a sufficient version as soon as possible.
33
require_once(__DIR__.'/../lib/phpminimumversionlib.php');
34
moodle_require_minimum_php_version();
35
 
36
// make sure iconv is available and actually works
37
if (!function_exists('iconv')) {
38
    // this should not happen, this must be very borked install
39
    echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
40
    die();
41
}
42
 
43
// Make sure xml extension is available.
44
if (!extension_loaded('xml')) {
45
    echo 'Moodle requires the xml PHP extension. Please install or enable the xml extension.';
46
    die();
47
}
48
 
49
// Make sure mbstring extension is available.
50
if (!extension_loaded('mbstring')) {
51
    echo 'Moodle requires the mbstring PHP extension. Please install or enable the mbstring extension.';
52
    die();
53
}
54
 
55
define('NO_OUTPUT_BUFFERING', true);
56
 
57
if (isset($_POST['upgradekey'])) {
58
    // Before you start reporting issues about the collision attacks against
59
    // SHA-1, you should understand that we are not actually attempting to do
60
    // any cryptography here. This is hashed purely so that the key is not
61
    // that apparent in the address bar itself. Anyone who catches the HTTP
62
    // traffic can immediately use it as a valid admin key.
63
    header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
64
    die();
65
}
66
 
67
if ((isset($_GET['cache']) and $_GET['cache'] === '0')
68
        or (isset($_POST['cache']) and $_POST['cache'] === '0')
69
        or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
70
    // Prevent caching at all cost when visiting this page directly,
71
    // we redirect to self once we known no upgrades are necessary.
72
    // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
73
    // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
74
    define('CACHE_DISABLE_ALL', true);
75
 
76
    // Force OPcache reset if used, we do not want any stale caches
77
    // when detecting if upgrade necessary or when running upgrade.
78
    if (function_exists('opcache_reset')) {
79
        opcache_reset();
80
    }
81
    $cache = 0;
82
 
83
} else {
84
    $cache = 1;
85
}
86
 
87
require('../config.php');
88
 
89
// Invalidate the cache of version.php in any circumstances to help core_component
90
// detecting if the version has changed and component cache should be reset.
91
if (function_exists('opcache_invalidate')) {
92
    opcache_invalidate($CFG->dirroot . '/version.php', true);
93
}
94
// Make sure the component cache gets rebuilt if necessary, any method that
95
// indirectly calls the protected init() method is good here.
96
core_component::get_core_subsystems();
97
 
98
if (is_major_upgrade_required() && isloggedin()) {
99
    // A major upgrade is required.
100
    // Terminate the session and redirect back here before anything DB-related happens.
101
    redirect_if_major_upgrade_required();
102
}
103
 
104
require_once($CFG->libdir.'/adminlib.php');    // various admin-only functions
105
require_once($CFG->libdir.'/upgradelib.php');  // general upgrade/install related functions
106
 
107
$confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
108
$confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
109
$confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
110
$showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
111
$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
112
$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
113
$newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
114
$upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
115
$installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
116
$installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
117
$confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
118
$abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
119
$abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
120
$confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
121
$abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
122
$abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
123
$confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
124
$installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
125
$installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
126
$installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
127
$confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
128
 
129
if (!empty($CFG->disableupdateautodeploy)) {
130
    // Invalidate all requests to install plugins via the admin UI.
131
    $newaddonreq = null;
132
    $installdep = null;
133
    $installdepx = false;
134
    $abortupgrade = null;
135
    $abortupgradex = null;
136
    $installupdate = null;
137
    $installupdateversion = null;
138
    $installupdatex = false;
139
}
140
 
141
// Set up PAGE.
142
$url = new moodle_url('/admin/index.php');
143
$url->param('cache', $cache);
144
if (isset($upgradekeyhash)) {
145
    $url->param('upgradekeyhash', $upgradekeyhash);
146
}
147
$PAGE->set_url($url);
148
unset($url);
149
 
150
// Are we returning from an add-on installation request at moodle.org/plugins?
151
if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
152
    $target = new moodle_url('/admin/tool/installaddon/index.php', array(
153
        'installaddonrequest' => $newaddonreq,
154
        'confirm' => 0));
155
    if (!isloggedin() or isguestuser()) {
156
        // Login and go the the add-on tool page.
157
        $SESSION->wantsurl = $target->out();
158
        redirect(get_login_url());
159
    }
160
    redirect($target);
161
}
162
 
163
$PAGE->set_pagelayout('admin'); // Set a default pagelayout
164
 
165
$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
166
 
167
// Check some PHP server settings
168
 
169
if (ini_get_bool('session.auto_start')) {
170
    throw new \moodle_exception('phpvaroff', 'debug', '',
171
        (object)array('name' => 'session.auto_start', 'link' => $documentationlink));
172
}
173
 
174
if (!ini_get_bool('file_uploads')) {
175
    throw new \moodle_exception('phpvaron', 'debug', '',
176
        (object)array('name' => 'file_uploads', 'link' => $documentationlink));
177
}
178
 
179
if (is_float_problem()) {
180
    throw new \moodle_exception('phpfloatproblem', 'admin', '', $documentationlink);
181
}
182
 
183
// Set some necessary variables during set-up to avoid PHP warnings later on this page
184
if (!isset($CFG->release)) {
185
    $CFG->release = '';
186
}
187
if (!isset($CFG->version)) {
188
    $CFG->version = '';
189
}
190
if (!isset($CFG->branch)) {
191
    $CFG->branch = '';
192
}
193
 
194
$version = null;
195
$release = null;
196
$branch = null;
197
require("$CFG->dirroot/version.php");       // defines $version, $release, $branch and $maturity
198
$CFG->target_release = $release;            // used during installation and upgrades
199
 
200
if (!$version or !$release) {
201
    throw new \moodle_exception('withoutversion', 'debug'); // Without version, stop.
202
}
203
 
204
if (!core_tables_exist()) {
205
    $PAGE->set_pagelayout('maintenance');
206
    $PAGE->set_popup_notification_allowed(false);
207
 
208
    // fake some settings
209
    $CFG->docroot = 'http://docs.moodle.org';
210
 
211
    $strinstallation = get_string('installation', 'install');
212
 
213
    // remove current session content completely
214
    \core\session\manager::terminate_current();
215
 
216
    if (empty($agreelicense)) {
217
        $strlicense = get_string('license');
218
 
219
        $PAGE->navbar->add($strlicense);
220
        $PAGE->set_title($strinstallation . moodle_page::TITLE_SEPARATOR . 'Moodle ' . $CFG->target_release, false);
221
        $PAGE->set_heading($strinstallation);
222
        $PAGE->set_cacheable(false);
223
 
224
        $output = $PAGE->get_renderer('core', 'admin');
225
        echo $output->install_licence_page();
226
        die();
227
    }
228
    if (empty($confirmrelease)) {
229
        require_once($CFG->libdir.'/environmentlib.php');
230
        list($envstatus, $environmentresults) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
231
        $strcurrentrelease = get_string('currentrelease');
232
 
233
        $PAGE->navbar->add($strcurrentrelease);
234
        $PAGE->set_title($strinstallation);
235
        $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
236
        $PAGE->set_cacheable(false);
237
 
238
        $output = $PAGE->get_renderer('core', 'admin');
239
        echo $output->install_environment_page($maturity, $envstatus, $environmentresults, $release);
240
        die();
241
    }
242
 
243
    // check plugin dependencies
244
    $failed = array();
245
    if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
246
        $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
247
        $PAGE->set_title($strinstallation);
248
        $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
249
 
250
        $output = $PAGE->get_renderer('core', 'admin');
251
        $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
252
        echo $output->unsatisfied_dependencies_page($version, $failed, $url);
253
        die();
254
    }
255
    unset($failed);
256
 
257
    //TODO: add a page with list of non-standard plugins here
258
 
259
    $strdatabasesetup = get_string('databasesetup');
260
    upgrade_init_javascript();
261
 
262
    $PAGE->navbar->add($strdatabasesetup);
263
    $PAGE->set_title($strinstallation . moodle_page::TITLE_SEPARATOR . $CFG->target_release, false);
264
    $PAGE->set_heading($strinstallation);
265
    $PAGE->set_cacheable(false);
266
 
267
    $output = $PAGE->get_renderer('core', 'admin');
268
    echo $output->header();
269
 
270
    if (!$DB->setup_is_unicodedb()) {
271
        if (!$DB->change_db_encoding()) {
272
            // If could not convert successfully, throw error, and prevent installation
273
            throw new \moodle_exception('unicoderequired', 'admin');
274
        }
275
    }
276
 
277
    install_core($version, true);
278
}
279
 
280
 
281
// Check version of Moodle code on disk compared with database
282
// and upgrade if possible.
283
 
284
if (!$cache) {
285
    // Do not try to do anything fancy in non-cached mode,
286
    // this prevents themes from fetching data from non-existent tables.
287
    $PAGE->set_pagelayout('maintenance');
288
    $PAGE->set_popup_notification_allowed(false);
289
}
290
 
291
$stradministration = get_string('administration');
292
$PAGE->set_context(context_system::instance());
293
 
294
if (empty($CFG->version)) {
295
    throw new \moodle_exception('missingconfigversion', 'debug');
296
}
297
 
298
// If an upgrade is running, an admin page starting a frontend upgrade could corrupt the
299
// DB if the upgrade collided with an already running upgrade process at the wrong time.
300
// Pull the value direct from the DB, this needs to *always* be correct.
301
$outagelessupgrade = !empty($DB->get_field('config', 'value', ['name' => 'outagelessupgrade']));
302
if (!$outagelessupgrade) {
303
    // Detect config cache inconsistency, this happens when you switch branches on dev servers.
304
    if ($CFG->version != $DB->get_field('config', 'value', array('name' => 'version'))) {
305
        purge_all_caches();
306
        redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
307
    }
308
 
309
    if (!$cache && $version > $CFG->version && !$outagelessupgrade) {  // Upgrade.
310
 
311
        $PAGE->set_url(new moodle_url($PAGE->url, array(
312
            'confirmupgrade' => $confirmupgrade,
313
            'confirmrelease' => $confirmrelease,
314
            'confirmplugincheck' => $confirmplugins,
315
        )));
316
 
317
        check_upgrade_key($upgradekeyhash);
318
 
319
        // Warning about upgrading a test site.
320
        $testsite = false;
321
        if (defined('BEHAT_SITE_RUNNING')) {
322
            $testsite = 'behat';
323
        }
324
 
325
        if (isset($CFG->themerev)) {
326
            // Store the themerev to restore after purging caches.
327
            $themerev = $CFG->themerev;
328
        }
329
 
330
        // We purge all of MUC's caches here.
331
        // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
332
        // This ensures a real config object is loaded and the stores will be purged.
333
        // This is the only way we can purge custom caches such as memcache or APC.
334
        // Note: all other calls to caches will still used the disabled API.
335
        cache_helper::purge_all(true);
336
        // We then purge the regular caches.
337
        purge_all_caches();
338
 
339
        if (isset($themerev)) {
340
            // Restore the themerev.
341
            set_config('themerev', $themerev);
342
        }
343
 
344
        $output = $PAGE->get_renderer('core', 'admin');
345
 
346
        if (upgrade_stale_php_files_present()) {
347
            $PAGE->set_title($stradministration);
348
            $PAGE->set_cacheable(false);
349
 
350
            echo $output->upgrade_stale_php_files_page();
351
            die();
352
        }
353
 
354
        if (empty($confirmupgrade)) {
355
            $a = new stdClass();
356
            $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
357
            $a->newversion = "$release (".sprintf('%.2f', $version).")";
358
            $strdatabasechecking = get_string('databasechecking', '', $a);
359
 
360
            $PAGE->set_title($stradministration);
361
            $PAGE->set_heading($strdatabasechecking);
362
            $PAGE->set_cacheable(false);
363
 
364
            echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
365
            die();
366
 
367
        } else if (empty($confirmrelease)) {
368
            require_once($CFG->libdir.'/environmentlib.php');
369
            list($envstatus, $environmentresults) = check_moodle_environment($release, ENV_SELECT_RELEASE);
370
            $strcurrentrelease = get_string('currentrelease');
371
 
372
            $PAGE->navbar->add($strcurrentrelease);
373
            $PAGE->set_title($strcurrentrelease);
374
            $PAGE->set_heading($strcurrentrelease);
375
            $PAGE->set_cacheable(false);
376
 
377
            echo $output->upgrade_environment_page($release, $envstatus, $environmentresults);
378
            die();
379
 
380
        } else if (empty($confirmplugins)) {
381
            $strplugincheck = get_string('plugincheck');
382
 
383
            $PAGE->navbar->add($strplugincheck);
384
            $PAGE->set_title($strplugincheck);
385
            $PAGE->set_heading($strplugincheck);
386
            $PAGE->set_cacheable(false);
387
 
388
            $pluginman = core_plugin_manager::instance();
389
 
390
            // Check for available updates.
391
            if ($fetchupdates) {
392
                // No sesskey support guaranteed here, because sessions might not work yet.
393
                $updateschecker = \core\update\checker::instance();
394
                if ($updateschecker->enabled()) {
395
                    $updateschecker->fetch();
396
                }
397
                redirect($PAGE->url);
398
            }
399
 
400
            // Cancel all plugin installations.
401
            if ($abortinstallx) {
402
                // No sesskey support guaranteed here, because sessions might not work yet.
403
                $abortables = $pluginman->list_cancellable_installations();
404
                if ($abortables) {
405
                    if ($confirmabortinstall) {
406
                        foreach ($abortables as $plugin) {
407
                            $pluginman->cancel_plugin_installation($plugin->component);
408
                        }
409
                        redirect($PAGE->url);
410
                    } else {
411
                        $continue = new moodle_url($PAGE->url, ['abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1]);
412
                        echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
413
                        die();
414
                    }
415
                }
416
                redirect($PAGE->url);
417
            }
418
 
419
            // Cancel single plugin installation.
420
            if ($abortinstall) {
421
                // No sesskey support guaranteed here, because sessions might not work yet.
422
                if ($confirmabortinstall) {
423
                    $pluginman->cancel_plugin_installation($abortinstall);
424
                    redirect($PAGE->url);
425
                } else {
426
                    $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
427
                    $abortable = $pluginman->get_plugin_info($abortinstall);
428
                    if ($pluginman->can_cancel_plugin_installation($abortable)) {
429
                        echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
430
                        die();
431
                    }
432
                    redirect($PAGE->url);
433
                }
434
            }
435
 
436
            // Cancel all plugins upgrades (that is, restore archived versions).
437
            if ($abortupgradex) {
438
                // No sesskey support guaranteed here, because sessions might not work yet.
439
                $restorable = $pluginman->list_restorable_archives();
440
                if ($restorable) {
441
                    upgrade_install_plugins($restorable, $confirmabortupgrade,
442
                        get_string('cancelupgradehead', 'core_plugin'),
443
                        new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
444
                    );
445
                }
446
                redirect($PAGE->url);
447
            }
448
 
449
            // Cancel single plugin upgrade (that is, install the archived version).
450
            if ($abortupgrade) {
451
                // No sesskey support guaranteed here, because sessions might not work yet.
452
                $restorable = $pluginman->list_restorable_archives();
453
                if (isset($restorable[$abortupgrade])) {
454
                    $restorable = array($restorable[$abortupgrade]);
455
                    upgrade_install_plugins($restorable, $confirmabortupgrade,
456
                        get_string('cancelupgradehead', 'core_plugin'),
457
                        new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
458
                    );
459
                }
460
                redirect($PAGE->url);
461
            }
462
 
463
            // Install all available missing dependencies.
464
            if ($installdepx) {
465
                // No sesskey support guaranteed here, because sessions might not work yet.
466
                $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
467
                upgrade_install_plugins($installable, $confirminstalldep,
468
                    get_string('dependencyinstallhead', 'core_plugin'),
469
                    new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
470
                );
471
            }
472
 
473
            // Install single available missing dependency.
474
            if ($installdep) {
475
                // No sesskey support guaranteed here, because sessions might not work yet.
476
                $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
477
                if (!empty($installable[$installdep])) {
478
                    $installable = array($installable[$installdep]);
479
                    upgrade_install_plugins($installable, $confirminstalldep,
480
                        get_string('dependencyinstallhead', 'core_plugin'),
481
                        new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
482
                    );
483
                }
484
            }
485
 
486
            // Install all available updates.
487
            if ($installupdatex) {
488
                // No sesskey support guaranteed here, because sessions might not work yet.
489
                $installable = $pluginman->filter_installable($pluginman->available_updates());
490
                upgrade_install_plugins($installable, $confirminstallupdate,
491
                    get_string('updateavailableinstallallhead', 'core_admin'),
492
                    new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
493
                );
494
            }
495
 
496
            // Install single available update.
497
            if ($installupdate and $installupdateversion) {
498
                // No sesskey support guaranteed here, because sessions might not work yet.
499
                if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
500
                    $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
501
                    upgrade_install_plugins($installable, $confirminstallupdate,
502
                        get_string('updateavailableinstallallhead', 'core_admin'),
503
                        new moodle_url($PAGE->url, array('installupdate' => $installupdate,
504
                            'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
505
                        )
506
                    );
507
                }
508
            }
509
 
510
            echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
511
                    $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
512
            die();
513
 
514
        } else {
515
            // Always verify plugin dependencies!
516
            $failed = array();
517
            if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed, $CFG->branch)) {
518
                echo $output->unsatisfied_dependencies_page($version, $failed, new moodle_url($PAGE->url,
519
                    array('confirmplugincheck' => 0)));
520
                die();
521
            }
522
            unset($failed);
523
 
524
            // Launch main upgrade.
525
            upgrade_core($version, true);
526
        }
527
    } else if ($version < $CFG->version) {
528
        // Better stop here, we can not continue with plugin upgrades or anything else.
529
        throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
530
    }
531
 
532
    // Updated human-readable release version if necessary.
533
    if (!$cache && $release <> $CFG->release ) {  // Update the release version.
534
        set_config('release', $release);
535
    }
536
 
537
    if (!$cache && $branch <> $CFG->branch) {  // Update the branch.
538
        set_config('branch', $branch);
539
    }
540
 
541
    if (!$cache && moodle_needs_upgrading()) {
542
 
543
        $PAGE->set_url(new moodle_url($PAGE->url, array(
544
            'confirmrelease' => $confirmrelease,
545
            'confirmplugincheck' => $confirmplugins,
546
        )));
547
 
548
        check_upgrade_key($upgradekeyhash);
549
 
550
        if (!$PAGE->headerprinted) {
551
            // Means core upgrade or installation was not already done.
552
 
553
            $pluginman = core_plugin_manager::instance();
554
            $output = $PAGE->get_renderer('core', 'admin');
555
 
556
            if (empty($confirmrelease)) {
557
                require_once($CFG->libdir . '/environmentlib.php');
558
 
559
                list($envstatus, $environmentresults) = check_moodle_environment($release, ENV_SELECT_RELEASE);
560
                $strcurrentrelease = get_string('currentrelease');
561
 
562
                $PAGE->navbar->add($strcurrentrelease);
563
                $PAGE->set_title($strcurrentrelease);
564
                $PAGE->set_heading($strcurrentrelease);
565
                $PAGE->set_cacheable(false);
566
 
567
                echo $output->upgrade_environment_page($release, $envstatus, $environmentresults);
568
                die();
569
 
570
            } else if (!$confirmplugins) {
571
                $strplugincheck = get_string('plugincheck');
572
 
573
                $PAGE->navbar->add($strplugincheck);
574
                $PAGE->set_title($strplugincheck);
575
                $PAGE->set_heading($strplugincheck);
576
                $PAGE->set_cacheable(false);
577
 
578
                // Check for available updates.
579
                if ($fetchupdates) {
580
                    require_sesskey();
581
                    $updateschecker = \core\update\checker::instance();
582
                    if ($updateschecker->enabled()) {
583
                        $updateschecker->fetch();
584
                    }
585
                    redirect($PAGE->url);
586
                }
587
 
588
                // Cancel all plugin installations.
589
                if ($abortinstallx) {
590
                    require_sesskey();
591
                    $abortables = $pluginman->list_cancellable_installations();
592
                    if ($abortables) {
593
                        if ($confirmabortinstall) {
594
                            foreach ($abortables as $plugin) {
595
                                $pluginman->cancel_plugin_installation($plugin->component);
596
                            }
597
                            redirect($PAGE->url);
598
                        } else {
599
                            $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
600
                                'confirmabortinstall' => 1));
601
                            echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
602
                            die();
603
                        }
604
                    }
605
                    redirect($PAGE->url);
606
                }
607
 
608
                // Cancel single plugin installation.
609
                if ($abortinstall) {
610
                    require_sesskey();
611
                    if ($confirmabortinstall) {
612
                        $pluginman->cancel_plugin_installation($abortinstall);
613
                        redirect($PAGE->url);
614
                    } else {
615
                        $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
616
                        $abortable = $pluginman->get_plugin_info($abortinstall);
617
                        if ($pluginman->can_cancel_plugin_installation($abortable)) {
618
                            echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
619
                            die();
620
                        }
621
                        redirect($PAGE->url);
622
                    }
623
                }
624
 
625
                // Cancel all plugins upgrades (that is, restore archived versions).
626
                if ($abortupgradex) {
627
                    require_sesskey();
628
                    $restorable = $pluginman->list_restorable_archives();
629
                    if ($restorable) {
630
                        upgrade_install_plugins($restorable, $confirmabortupgrade,
631
                            get_string('cancelupgradehead', 'core_plugin'),
632
                            new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
633
                        );
634
                    }
635
                    redirect($PAGE->url);
636
                }
637
 
638
                // Cancel single plugin upgrade (that is, install the archived version).
639
                if ($abortupgrade) {
640
                    require_sesskey();
641
                    $restorable = $pluginman->list_restorable_archives();
642
                    if (isset($restorable[$abortupgrade])) {
643
                        $restorable = array($restorable[$abortupgrade]);
644
                        upgrade_install_plugins($restorable, $confirmabortupgrade,
645
                            get_string('cancelupgradehead', 'core_plugin'),
646
                            new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
647
                        );
648
                    }
649
                    redirect($PAGE->url);
650
                }
651
 
652
                // Install all available missing dependencies.
653
                if ($installdepx) {
654
                    require_sesskey();
655
                    $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
656
                    upgrade_install_plugins($installable, $confirminstalldep,
657
                        get_string('dependencyinstallhead', 'core_plugin'),
658
                        new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
659
                    );
660
                }
661
 
662
                // Install single available missing dependency.
663
                if ($installdep) {
664
                    require_sesskey();
665
                    $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
666
                    if (!empty($installable[$installdep])) {
667
                        $installable = array($installable[$installdep]);
668
                        upgrade_install_plugins($installable, $confirminstalldep,
669
                            get_string('dependencyinstallhead', 'core_plugin'),
670
                            new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
671
                        );
672
                    }
673
                }
674
 
675
                // Install all available updates.
676
                if ($installupdatex) {
677
                    require_sesskey();
678
                    $installable = $pluginman->filter_installable($pluginman->available_updates());
679
                    upgrade_install_plugins($installable, $confirminstallupdate,
680
                        get_string('updateavailableinstallallhead', 'core_admin'),
681
                        new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
682
                    );
683
                }
684
 
685
                // Install single available update.
686
                if ($installupdate && $installupdateversion) {
687
                    require_sesskey();
688
                    if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
689
                        $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
690
                        upgrade_install_plugins($installable, $confirminstallupdate,
691
                            get_string('updateavailableinstallallhead', 'core_admin'),
692
                            new moodle_url($PAGE->url, array('installupdate' => $installupdate,
693
                                'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
694
                            )
695
                        );
696
                    }
697
                }
698
 
699
                // Show plugins info.
700
                echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
701
                        $version, $showallplugins,
702
                        new moodle_url($PAGE->url),
703
                        new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
704
                die();
705
            }
706
 
707
            // Make sure plugin dependencies are always checked.
708
            $failed = array();
709
            if (!$pluginman->all_plugins_ok($version, $failed, $CFG->branch)) {
710
                $output = $PAGE->get_renderer('core', 'admin');
711
                echo $output->unsatisfied_dependencies_page($version, $failed, new moodle_url($PAGE->url,
712
                    array('confirmplugincheck' => 0)));
713
                die();
714
            }
715
            unset($failed);
716
        }
717
 
718
        // Install/upgrade all plugins and other parts.
719
        upgrade_noncore(true);
720
    }
721
 
722
    // If this is the first install, indicate that this site is fully configured,
723
    // Except the admin password.
724
    if (during_initial_install()) {
725
        set_config('rolesactive', 1); // After this, during_initial_install will return false.
726
        set_config('adminsetuppending', 1);
727
        set_config('registrationpending', 1); // Remind to register site after all other setup is finished.
728
 
729
        // Apply default preset, if it is defined in $CFG and has a valid value.
730
        if (!empty($CFG->setsitepresetduringinstall)) {
731
            \core_adminpresets\helper::change_default_preset($CFG->setsitepresetduringinstall);
732
        }
733
 
734
        // We need this redirect to setup proper session.
735
        upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
736
    }
737
 
738
    // Make sure admin user is created - this is the last step,
739
    // We need session to be working properly in order to edit admin account.
740
    if (!empty($CFG->adminsetuppending)) {
741
        $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
742
        if (!$sessionstarted) {
743
            redirect("index.php?sessionstarted=1&lang=$CFG->lang");
744
        } else {
745
            $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
746
            if (!$sessionverify) {
747
                $SESSION->sessionverify = 1;
748
                redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
749
            } else {
750
                if (empty($SESSION->sessionverify)) {
751
                    throw new \moodle_exception('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
752
                }
753
                unset($SESSION->sessionverify);
754
            }
755
        }
756
 
757
        // Cleanup SESSION to make sure other code does not complain in the future.
758
        unset($SESSION->has_timed_out);
759
        unset($SESSION->wantsurl);
760
 
761
        // At this stage there can be only one admin unless more were added by install,
762
        // Users may change username, so do not rely on that.
763
        $adminids = explode(',', $CFG->siteadmins);
764
        $adminuser = get_complete_user_data('id', reset($adminids));
765
 
766
        if ($adminuser->password === 'adminsetuppending') {
767
            // Prevent installation hijacking.
768
            if ($adminuser->lastip !== getremoteaddr()) {
769
                throw new \moodle_exception('installhijacked', 'admin');
770
            }
771
            // Login user and let him set password and admin details.
772
            $adminuser->newadminuser = 1;
773
            complete_user_login($adminuser);
774
            redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself.
775
 
776
        } else {
777
            unset_config('adminsetuppending');
778
        }
779
 
780
    } else {
781
        // Just make sure upgrade logging is properly terminated.
782
        upgrade_finished('upgradesettings.php');
783
    }
784
}
785
 
786
if (has_capability('moodle/site:config', context_system::instance())) {
787
    if ($fetchupdates) {
788
        require_sesskey();
789
        $updateschecker = \core\update\checker::instance();
790
        if ($updateschecker->enabled()) {
791
            $updateschecker->fetch();
792
        }
793
        redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
794
    }
795
}
796
 
797
// Now we can be sure everything was upgraded and caches work fine,
798
// redirect if necessary to make sure caching is enabled.
799
if (!$cache) {
800
    redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
801
}
802
 
803
// Check for valid admin user - no guest autologin
804
require_login(0, false);
805
if (isguestuser()) {
806
    // Login as real user!
807
    $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
808
    redirect(get_login_url());
809
}
810
$context = context_system::instance();
811
 
812
if (!has_capability('moodle/site:config', $context)) {
813
    // Do not throw exception display an empty page with administration menu if visible for current user.
814
    $PAGE->set_title(get_string('home'));
815
    $PAGE->set_heading($SITE->fullname);
816
    echo $OUTPUT->header();
817
    echo $OUTPUT->footer();
818
    exit;
819
}
820
 
821
// check that site is properly customized
822
$site = get_site();
823
if (empty($site->shortname)) {
824
    // probably new installation - lets return to frontpage after this step
825
    // remove settings that we want uninitialised
826
    unset_config('registerauth');
827
    unset_config('timezone'); // Force admin to select timezone!
828
    redirect('upgradesettings.php?return=site');
829
}
830
 
831
// setup critical warnings before printing admin tree block
832
$insecuredataroot = is_dataroot_insecure(true);
833
$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
834
 
835
$adminroot = admin_get_root();
836
$PAGE->set_primary_active_tab('siteadminnode');
837
 
838
// Check if there are any new admin settings which have still yet to be set
839
if (any_new_admin_settings($adminroot)) {
840
    redirect('upgradesettings.php');
841
}
842
 
843
// Return to original page that started the plugin uninstallation if necessary.
844
if (isset($SESSION->pluginuninstallreturn)) {
845
    $return = $SESSION->pluginuninstallreturn;
846
    unset($SESSION->pluginuninstallreturn);
847
    if ($return) {
848
        redirect($return);
849
    }
850
}
851
 
852
// If site registration needs updating, redirect.
853
\core\hub\registration::registration_reminder('/admin/index.php');
854
 
855
// Everything should now be set up, and the user is an admin
856
 
857
// Print default admin page with notifications.
858
$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
859
 
860
$lastcron = get_config('tool_task', 'lastcronstart');
861
$cronoverdue = ($lastcron < time() - 3600 * 24);
862
$lastcroninterval = get_config('tool_task', 'lastcroninterval');
863
 
864
$expectedfrequency = $CFG->expectedcronfrequency ?? MINSECS;
865
$croninfrequent = !$cronoverdue && ($lastcroninterval > ($expectedfrequency + MINSECS) || $lastcron < time() - $expectedfrequency);
866
$dbproblems = $DB->diagnose();
867
$maintenancemode = !empty($CFG->maintenance_enabled);
868
 
869
// Available updates for Moodle core.
870
$updateschecker = \core\update\checker::instance();
871
$availableupdates = array();
872
$availableupdatesfetch = null;
873
 
874
if ($updateschecker->enabled()) {
875
    // Only compute the update information when it is going to be displayed to the user.
876
    $availableupdates['core'] = $updateschecker->get_update_info('core',
877
        array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
878
 
879
    // Available updates for contributed plugins
880
    $pluginman = core_plugin_manager::instance();
881
    foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
882
        foreach ($plugintypeinstances as $pluginname => $plugininfo) {
883
            $pluginavailableupdates = $plugininfo->available_updates();
884
            if (!empty($pluginavailableupdates)) {
885
                foreach ($pluginavailableupdates as $pluginavailableupdate) {
886
                    if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
887
                        $availableupdates[$plugintype.'_'.$pluginname] = array();
888
                    }
889
                    $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
890
                }
891
            }
892
        }
893
    }
894
 
895
    // The timestamp of the most recent check for available updates
896
    $availableupdatesfetch = $updateschecker->get_last_timefetched();
897
}
898
 
899
$buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
900
//check if the site is registered on Moodle.org
901
$registered = \core\hub\registration::is_registered();
902
// Check if there are any cache warnings.
903
$cachewarnings = cache_helper::warnings();
904
// Check if there are events 1 API handlers.
905
$eventshandlers = $DB->get_records_sql('SELECT DISTINCT component FROM {events_handlers}');
906
$themedesignermode = !empty($CFG->themedesignermode);
907
$mobileconfigured = !empty($CFG->enablemobilewebservice);
908
$invalidforgottenpasswordurl = !empty($CFG->forgottenpasswordurl) && empty(clean_param($CFG->forgottenpasswordurl, PARAM_URL));
909
 
910
// Check if a directory with development libraries exists.
911
if (empty($CFG->disabledevlibdirscheck) && (is_dir($CFG->dirroot.'/vendor') || is_dir($CFG->dirroot.'/node_modules'))) {
912
    $devlibdir = true;
913
} else {
914
    $devlibdir = false;
915
}
916
// Check if the site is being foced onto ssl.
917
$overridetossl = !empty($CFG->overridetossl);
918
 
919
// Check if moodle campaign content setting is enabled or not.
920
$showcampaigncontent = !isset($CFG->showcampaigncontent) || $CFG->showcampaigncontent;
921
 
922
// Encourage admins to enable the user feedback feature if it is not enabled already.
923
$showfeedbackencouragement = empty($CFG->enableuserfeedback);
924
 
925
// Check if the service and support content setting is enabled or not.
926
$servicesandsupportcontent = !isset($CFG->showservicesandsupportcontent) || $CFG->showservicesandsupportcontent;
927
 
928
// Check whether the XML-RPC protocol is enabled or not.
929
require_once($CFG->libdir . '/environmentlib.php');
930
$result = new environment_results('custom_checks');
931
$result = check_xmlrpc_usage($result);
932
$xmlrpcwarning = !is_null($result) ? get_string($result->getFeedbackStr(), 'admin') : '';
933
 
934
admin_externalpage_setup('adminnotifications');
935
 
936
$output = $PAGE->get_renderer('core', 'admin');
937
 
938
echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
939
                                       $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
940
                                       $registered, $cachewarnings, $eventshandlers, $themedesignermode, $devlibdir,
941
                                       $mobileconfigured, $overridetossl, $invalidforgottenpasswordurl, $croninfrequent,
942
                                       $showcampaigncontent, $showfeedbackencouragement, $servicesandsupportcontent,
943
                                       $xmlrpcwarning);