source: moodle/trunk/fuentes/admin/index.php @ 1348

Last change on this file since 1348 was 1348, checked in by jrpelegrina, 3 years ago

revert patch

File size: 35.5 KB
Line 
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
27if (!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
33if (version_compare(phpversion(), '5.4.4') < 0) {
34    $phpversion = phpversion();
35    // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
36    echo "Moodle 2.7 or later requires at least PHP 5.4.4 (currently using version $phpversion).<br />";
37    echo "Please upgrade your server software or install older Moodle version.";
38    die();
39}
40
41// make sure iconv is available and actually works
42if (!function_exists('iconv')) {
43    // this should not happen, this must be very borked install
44    echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
45    die();
46}
47
48// Make sure php5-json is available.
49if (!function_exists('json_encode') || !function_exists('json_decode')) {
50    // This also shouldn't happen.
51    echo 'Moodle requires the json PHP extension. Please install or enable the json extension.';
52    die();
53}
54
55define('NO_OUTPUT_BUFFERING', true);
56
57if (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
67if ((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
87require('../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.
91if (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.
96core_component::get_core_subsystems();
97
98require_once($CFG->libdir.'/adminlib.php');    // various admin-only functions
99require_once($CFG->libdir.'/upgradelib.php');  // general upgrade/install related functions
100
101$confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL); // Core upgrade confirmed?
102$confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL); // Core release info and server checks confirmed?
103$confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL); // Plugins check page confirmed?
104$showallplugins = optional_param('showallplugins', 0, PARAM_BOOL); // Show all plugins on the plugins check page?
105$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL); // GPL license confirmed for installation?
106$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL); // Should check for available updates?
107$newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW); // Plugin installation requested at moodle.org/plugins.
108$upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM); // Hash of provided upgrade key.
109$installdep = optional_param('installdep', null, PARAM_COMPONENT); // Install given missing dependency (required plugin).
110$installdepx = optional_param('installdepx', false, PARAM_BOOL); // Install all missing dependencies.
111$confirminstalldep = optional_param('confirminstalldep', false, PARAM_BOOL); // Installing dependencies confirmed.
112$abortinstall = optional_param('abortinstall', null, PARAM_COMPONENT); // Cancel installation of the given new plugin.
113$abortinstallx = optional_param('abortinstallx', null, PARAM_BOOL); // Cancel installation of all new plugins.
114$confirmabortinstall = optional_param('confirmabortinstall', false, PARAM_BOOL); // Installation cancel confirmed.
115$abortupgrade = optional_param('abortupgrade', null, PARAM_COMPONENT); // Cancel upgrade of the given existing plugin.
116$abortupgradex = optional_param('abortupgradex', null, PARAM_BOOL); // Cancel upgrade of all upgradable plugins.
117$confirmabortupgrade = optional_param('confirmabortupgrade', false, PARAM_BOOL); // Upgrade cancel confirmed.
118$installupdate = optional_param('installupdate', null, PARAM_COMPONENT); // Install given available update.
119$installupdateversion = optional_param('installupdateversion', null, PARAM_INT); // Version of the available update to install.
120$installupdatex = optional_param('installupdatex', false, PARAM_BOOL); // Install all available plugin updates.
121$confirminstallupdate = optional_param('confirminstallupdate', false, PARAM_BOOL); // Available update(s) install confirmed?
122
123if (!empty($CFG->disableupdateautodeploy)) {
124    // Invalidate all requests to install plugins via the admin UI.
125    $newaddonreq = null;
126    $installdep = null;
127    $installdepx = false;
128    $abortupgrade = null;
129    $abortupgradex = null;
130    $installupdate = null;
131    $installupdateversion = null;
132    $installupdatex = false;
133}
134
135// Set up PAGE.
136$url = new moodle_url('/admin/index.php');
137$url->param('cache', $cache);
138if (isset($upgradekeyhash)) {
139    $url->param('upgradekeyhash', $upgradekeyhash);
140}
141$PAGE->set_url($url);
142unset($url);
143
144// Are we returning from an add-on installation request at moodle.org/plugins?
145if ($newaddonreq and !$cache and empty($CFG->disableupdateautodeploy)) {
146    $target = new moodle_url('/admin/tool/installaddon/index.php', array(
147        'installaddonrequest' => $newaddonreq,
148        'confirm' => 0));
149    if (!isloggedin() or isguestuser()) {
150        // Login and go the the add-on tool page.
151        $SESSION->wantsurl = $target->out();
152        redirect(get_login_url());
153    }
154    redirect($target);
155}
156
157$PAGE->set_pagelayout('admin'); // Set a default pagelayout
158
159$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
160
161// Check some PHP server settings
162
163if (ini_get_bool('session.auto_start')) {
164    print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
165}
166
167if (!ini_get_bool('file_uploads')) {
168    print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
169}
170
171if (is_float_problem()) {
172    print_error('phpfloatproblem', 'admin', '', $documentationlink);
173}
174
175// Set some necessary variables during set-up to avoid PHP warnings later on this page
176if (!isset($CFG->release)) {
177    $CFG->release = '';
178}
179if (!isset($CFG->version)) {
180    $CFG->version = '';
181}
182if (!isset($CFG->branch)) {
183    $CFG->branch = '';
184}
185
186$version = null;
187$release = null;
188$branch = null;
189require("$CFG->dirroot/version.php");       // defines $version, $release, $branch and $maturity
190$CFG->target_release = $release;            // used during installation and upgrades
191
192if (!$version or !$release) {
193    print_error('withoutversion', 'debug'); // without version, stop
194}
195
196if (!core_tables_exist()) {
197    $PAGE->set_pagelayout('maintenance');
198    $PAGE->set_popup_notification_allowed(false);
199
200    // fake some settings
201    $CFG->docroot = 'http://docs.moodle.org';
202
203    $strinstallation = get_string('installation', 'install');
204
205    // remove current session content completely
206    \core\session\manager::terminate_current();
207
208    if (empty($agreelicense)) {
209        $strlicense = get_string('license');
210
211        $PAGE->navbar->add($strlicense);
212        $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
213        $PAGE->set_heading($strinstallation);
214        $PAGE->set_cacheable(false);
215
216        $output = $PAGE->get_renderer('core', 'admin');
217        echo $output->install_licence_page();
218        die();
219    }
220    if (empty($confirmrelease)) {
221        require_once($CFG->libdir.'/environmentlib.php');
222        list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
223        $strcurrentrelease = get_string('currentrelease');
224
225        $PAGE->navbar->add($strcurrentrelease);
226        $PAGE->set_title($strinstallation);
227        $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
228        $PAGE->set_cacheable(false);
229
230        $output = $PAGE->get_renderer('core', 'admin');
231        echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
232        die();
233    }
234
235    // check plugin dependencies
236    $failed = array();
237    if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
238        $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
239        $PAGE->set_title($strinstallation);
240        $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
241
242        $output = $PAGE->get_renderer('core', 'admin');
243        $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
244        echo $output->unsatisfied_dependencies_page($version, $failed, $url);
245        die();
246    }
247    unset($failed);
248
249    //TODO: add a page with list of non-standard plugins here
250
251    $strdatabasesetup = get_string('databasesetup');
252    upgrade_init_javascript();
253
254    $PAGE->navbar->add($strdatabasesetup);
255    $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
256    $PAGE->set_heading($strinstallation);
257    $PAGE->set_cacheable(false);
258
259    $output = $PAGE->get_renderer('core', 'admin');
260    echo $output->header();
261
262    if (!$DB->setup_is_unicodedb()) {
263        if (!$DB->change_db_encoding()) {
264            // If could not convert successfully, throw error, and prevent installation
265            print_error('unicoderequired', 'admin');
266        }
267    }
268
269    install_core($version, true);
270}
271
272
273// Check version of Moodle code on disk compared with database
274// and upgrade if possible.
275
276if (!$cache) {
277    // Do not try to do anything fancy in non-cached mode,
278    // this prevents themes from fetching data from non-existent tables.
279    $PAGE->set_pagelayout('maintenance');
280    $PAGE->set_popup_notification_allowed(false);
281}
282
283$stradministration = get_string('administration');
284$PAGE->set_context(context_system::instance());
285
286if (empty($CFG->version)) {
287    print_error('missingconfigversion', 'debug');
288}
289
290// Detect config cache inconsistency, this happens when you switch branches on dev servers.
291if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
292    purge_all_caches();
293    redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
294}
295
296if (!$cache and $version > $CFG->version) {  // upgrade
297
298    $PAGE->set_url(new moodle_url($PAGE->url, array(
299        'confirmupgrade' => $confirmupgrade,
300        'confirmrelease' => $confirmrelease,
301        'confirmplugincheck' => $confirmplugins,
302    )));
303
304    check_upgrade_key($upgradekeyhash);
305
306    // Warning about upgrading a test site.
307    $testsite = false;
308    if (defined('BEHAT_SITE_RUNNING')) {
309        $testsite = 'behat';
310    }
311
312    // We purge all of MUC's caches here.
313    // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
314    // This ensures a real config object is loaded and the stores will be purged.
315    // This is the only way we can purge custom caches such as memcache or APC.
316    // Note: all other calls to caches will still used the disabled API.
317    cache_helper::purge_all(true);
318    // We then purge the regular caches.
319    purge_all_caches();
320
321    $output = $PAGE->get_renderer('core', 'admin');
322
323    if (upgrade_stale_php_files_present()) {
324        $PAGE->set_title($stradministration);
325        $PAGE->set_cacheable(false);
326
327        echo $output->upgrade_stale_php_files_page();
328        die();
329    }
330
331    if (empty($confirmupgrade)) {
332        $a = new stdClass();
333        $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
334        $a->newversion = "$release (".sprintf('%.2f', $version).")";
335        $strdatabasechecking = get_string('databasechecking', '', $a);
336
337        $PAGE->set_title($stradministration);
338        $PAGE->set_heading($strdatabasechecking);
339        $PAGE->set_cacheable(false);
340
341        echo $output->upgrade_confirm_page($a->newversion, $maturity, $testsite);
342        die();
343
344    } else if (empty($confirmrelease)){
345        require_once($CFG->libdir.'/environmentlib.php');
346        list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
347        $strcurrentrelease = get_string('currentrelease');
348
349        $PAGE->navbar->add($strcurrentrelease);
350        $PAGE->set_title($strcurrentrelease);
351        $PAGE->set_heading($strcurrentrelease);
352        $PAGE->set_cacheable(false);
353
354        echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
355        die();
356
357    } else if (empty($confirmplugins)) {
358        $strplugincheck = get_string('plugincheck');
359
360        $PAGE->navbar->add($strplugincheck);
361        $PAGE->set_title($strplugincheck);
362        $PAGE->set_heading($strplugincheck);
363        $PAGE->set_cacheable(false);
364
365        $pluginman = core_plugin_manager::instance();
366
367        // Check for available updates.
368        if ($fetchupdates) {
369            // No sesskey support guaranteed here, because sessions might not work yet.
370            $updateschecker = \core\update\checker::instance();
371            if ($updateschecker->enabled()) {
372                $updateschecker->fetch();
373            }
374            redirect($PAGE->url);
375        }
376
377        // Cancel all plugin installations.
378        if ($abortinstallx) {
379            // No sesskey support guaranteed here, because sessions might not work yet.
380            $abortables = $pluginman->list_cancellable_installations();
381            if ($abortables) {
382                if ($confirmabortinstall) {
383                    foreach ($abortables as $plugin) {
384                        $pluginman->cancel_plugin_installation($plugin->component);
385                    }
386                    redirect($PAGE->url);
387                } else {
388                    $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx, 'confirmabortinstall' => 1));
389                    echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
390                    die();
391                }
392            }
393            redirect($PAGE->url);
394        }
395
396        // Cancel single plugin installation.
397        if ($abortinstall) {
398            // No sesskey support guaranteed here, because sessions might not work yet.
399            if ($confirmabortinstall) {
400                $pluginman->cancel_plugin_installation($abortinstall);
401                redirect($PAGE->url);
402            } else {
403                $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
404                $abortable = $pluginman->get_plugin_info($abortinstall);
405                if ($pluginman->can_cancel_plugin_installation($abortable)) {
406                    echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
407                    die();
408                }
409                redirect($PAGE->url);
410            }
411        }
412
413        // Cancel all plugins upgrades (that is, restore archived versions).
414        if ($abortupgradex) {
415            // No sesskey support guaranteed here, because sessions might not work yet.
416            $restorable = $pluginman->list_restorable_archives();
417            if ($restorable) {
418                upgrade_install_plugins($restorable, $confirmabortupgrade,
419                    get_string('cancelupgradehead', 'core_plugin'),
420                    new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
421                );
422            }
423            redirect($PAGE->url);
424        }
425
426        // Cancel single plugin upgrade (that is, install the archived version).
427        if ($abortupgrade) {
428            // No sesskey support guaranteed here, because sessions might not work yet.
429            $restorable = $pluginman->list_restorable_archives();
430            if (isset($restorable[$abortupgrade])) {
431                $restorable = array($restorable[$abortupgrade]);
432                upgrade_install_plugins($restorable, $confirmabortupgrade,
433                    get_string('cancelupgradehead', 'core_plugin'),
434                    new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
435                );
436            }
437            redirect($PAGE->url);
438        }
439
440        // Install all available missing dependencies.
441        if ($installdepx) {
442            // No sesskey support guaranteed here, because sessions might not work yet.
443            $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
444            upgrade_install_plugins($installable, $confirminstalldep,
445                get_string('dependencyinstallhead', 'core_plugin'),
446                new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
447            );
448        }
449
450        // Install single available missing dependency.
451        if ($installdep) {
452            // No sesskey support guaranteed here, because sessions might not work yet.
453            $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
454            if (!empty($installable[$installdep])) {
455                $installable = array($installable[$installdep]);
456                upgrade_install_plugins($installable, $confirminstalldep,
457                    get_string('dependencyinstallhead', 'core_plugin'),
458                    new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
459                );
460            }
461        }
462
463        // Install all available updates.
464        if ($installupdatex) {
465            // No sesskey support guaranteed here, because sessions might not work yet.
466            $installable = $pluginman->filter_installable($pluginman->available_updates());
467            upgrade_install_plugins($installable, $confirminstallupdate,
468                get_string('updateavailableinstallallhead', 'core_admin'),
469                new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
470            );
471        }
472
473        // Install single available update.
474        if ($installupdate and $installupdateversion) {
475            // No sesskey support guaranteed here, because sessions might not work yet.
476            if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
477                $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
478                upgrade_install_plugins($installable, $confirminstallupdate,
479                    get_string('updateavailableinstallallhead', 'core_admin'),
480                    new moodle_url($PAGE->url, array('installupdate' => $installupdate,
481                        'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
482                    )
483                );
484            }
485        }
486
487        echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
488                $version, $showallplugins, $PAGE->url, new moodle_url($PAGE->url, array('confirmplugincheck' => 1)));
489        die();
490
491    } else {
492        // Always verify plugin dependencies!
493        $failed = array();
494        if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
495            echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
496            die();
497        }
498        unset($failed);
499
500        // Launch main upgrade.
501        upgrade_core($version, true);
502    }
503} else if ($version < $CFG->version) {
504    // better stop here, we can not continue with plugin upgrades or anything else
505    throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
506}
507
508// Updated human-readable release version if necessary
509if (!$cache and $release <> $CFG->release) {  // Update the release version
510    set_config('release', $release);
511}
512
513if (!$cache and $branch <> $CFG->branch) {  // Update the branch
514    set_config('branch', $branch);
515}
516
517if (!$cache and moodle_needs_upgrading()) {
518
519    $PAGE->set_url(new moodle_url($PAGE->url, array('confirmplugincheck' => $confirmplugins)));
520
521    check_upgrade_key($upgradekeyhash);
522
523    if (!$PAGE->headerprinted) {
524        // means core upgrade or installation was not already done
525
526        $pluginman = core_plugin_manager::instance();
527        $output = $PAGE->get_renderer('core', 'admin');
528
529        if (!$confirmplugins) {
530            $strplugincheck = get_string('plugincheck');
531
532            $PAGE->navbar->add($strplugincheck);
533            $PAGE->set_title($strplugincheck);
534            $PAGE->set_heading($strplugincheck);
535            $PAGE->set_cacheable(false);
536
537            // Check for available updates.
538            if ($fetchupdates) {
539                require_sesskey();
540                $updateschecker = \core\update\checker::instance();
541                if ($updateschecker->enabled()) {
542                    $updateschecker->fetch();
543                }
544                redirect($PAGE->url);
545            }
546
547            // Cancel all plugin installations.
548            if ($abortinstallx) {
549                require_sesskey();
550                $abortables = $pluginman->list_cancellable_installations();
551                if ($abortables) {
552                    if ($confirmabortinstall) {
553                        foreach ($abortables as $plugin) {
554                            $pluginman->cancel_plugin_installation($plugin->component);
555                        }
556                        redirect($PAGE->url);
557                    } else {
558                        $continue = new moodle_url($PAGE->url, array('abortinstallx' => $abortinstallx,
559                            'confirmabortinstall' => 1));
560                        echo $output->upgrade_confirm_abort_install_page($abortables, $continue);
561                        die();
562                    }
563                }
564                redirect($PAGE->url);
565            }
566
567            // Cancel single plugin installation.
568            if ($abortinstall) {
569                require_sesskey();
570                if ($confirmabortinstall) {
571                    $pluginman->cancel_plugin_installation($abortinstall);
572                    redirect($PAGE->url);
573                } else {
574                    $continue = new moodle_url($PAGE->url, array('abortinstall' => $abortinstall, 'confirmabortinstall' => 1));
575                    $abortable = $pluginman->get_plugin_info($abortinstall);
576                    if ($pluginman->can_cancel_plugin_installation($abortable)) {
577                        echo $output->upgrade_confirm_abort_install_page(array($abortable), $continue);
578                        die();
579                    }
580                    redirect($PAGE->url);
581                }
582            }
583
584            // Cancel all plugins upgrades (that is, restore archived versions).
585            if ($abortupgradex) {
586                require_sesskey();
587                $restorable = $pluginman->list_restorable_archives();
588                if ($restorable) {
589                    upgrade_install_plugins($restorable, $confirmabortupgrade,
590                        get_string('cancelupgradehead', 'core_plugin'),
591                        new moodle_url($PAGE->url, array('abortupgradex' => 1, 'confirmabortupgrade' => 1))
592                    );
593                }
594                redirect($PAGE->url);
595            }
596
597            // Cancel single plugin upgrade (that is, install the archived version).
598            if ($abortupgrade) {
599                require_sesskey();
600                $restorable = $pluginman->list_restorable_archives();
601                if (isset($restorable[$abortupgrade])) {
602                    $restorable = array($restorable[$abortupgrade]);
603                    upgrade_install_plugins($restorable, $confirmabortupgrade,
604                        get_string('cancelupgradehead', 'core_plugin'),
605                        new moodle_url($PAGE->url, array('abortupgrade' => $abortupgrade, 'confirmabortupgrade' => 1))
606                    );
607                }
608                redirect($PAGE->url);
609            }
610
611            // Install all available missing dependencies.
612            if ($installdepx) {
613                require_sesskey();
614                $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
615                upgrade_install_plugins($installable, $confirminstalldep,
616                    get_string('dependencyinstallhead', 'core_plugin'),
617                    new moodle_url($PAGE->url, array('installdepx' => 1, 'confirminstalldep' => 1))
618                );
619            }
620
621            // Install single available missing dependency.
622            if ($installdep) {
623                require_sesskey();
624                $installable = $pluginman->filter_installable($pluginman->missing_dependencies(true));
625                if (!empty($installable[$installdep])) {
626                    $installable = array($installable[$installdep]);
627                    upgrade_install_plugins($installable, $confirminstalldep,
628                        get_string('dependencyinstallhead', 'core_plugin'),
629                        new moodle_url($PAGE->url, array('installdep' => $installdep, 'confirminstalldep' => 1))
630                    );
631                }
632            }
633
634            // Install all available updates.
635            if ($installupdatex) {
636                require_sesskey();
637                $installable = $pluginman->filter_installable($pluginman->available_updates());
638                upgrade_install_plugins($installable, $confirminstallupdate,
639                    get_string('updateavailableinstallallhead', 'core_admin'),
640                    new moodle_url($PAGE->url, array('installupdatex' => 1, 'confirminstallupdate' => 1))
641                );
642            }
643
644            // Install single available update.
645            if ($installupdate and $installupdateversion) {
646                require_sesskey();
647                if ($pluginman->is_remote_plugin_installable($installupdate, $installupdateversion)) {
648                    $installable = array($pluginman->get_remote_plugin_info($installupdate, $installupdateversion, true));
649                    upgrade_install_plugins($installable, $confirminstallupdate,
650                        get_string('updateavailableinstallallhead', 'core_admin'),
651                        new moodle_url($PAGE->url, array('installupdate' => $installupdate,
652                            'installupdateversion' => $installupdateversion, 'confirminstallupdate' => 1)
653                        )
654                    );
655                }
656            }
657
658            // Show plugins info.
659            echo $output->upgrade_plugin_check_page($pluginman, \core\update\checker::instance(),
660                    $version, $showallplugins,
661                    new moodle_url($PAGE->url),
662                    new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
663            die();
664        }
665
666        // Make sure plugin dependencies are always checked.
667        $failed = array();
668        if (!$pluginman->all_plugins_ok($version, $failed)) {
669            $output = $PAGE->get_renderer('core', 'admin');
670            echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
671            die();
672        }
673        unset($failed);
674    }
675
676    // install/upgrade all plugins and other parts
677    upgrade_noncore(true);
678}
679
680// If this is the first install, indicate that this site is fully configured
681// except the admin password
682if (during_initial_install()) {
683    set_config('rolesactive', 1); // after this, during_initial_install will return false.
684    set_config('adminsetuppending', 1);
685    // we need this redirect to setup proper session
686    upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
687}
688
689// make sure admin user is created - this is the last step because we need
690// session to be working properly in order to edit admin account
691 if (!empty($CFG->adminsetuppending)) {
692    $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
693    if (!$sessionstarted) {
694        redirect("index.php?sessionstarted=1&lang=$CFG->lang");
695    } else {
696        $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
697        if (!$sessionverify) {
698            $SESSION->sessionverify = 1;
699            redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
700        } else {
701            if (empty($SESSION->sessionverify)) {
702                print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
703            }
704            unset($SESSION->sessionverify);
705        }
706    }
707
708    // Cleanup SESSION to make sure other code does not complain in the future.
709    unset($SESSION->has_timed_out);
710    unset($SESSION->wantsurl);
711
712    // at this stage there can be only one admin unless more were added by install - users may change username, so do not rely on that
713    $adminids = explode(',', $CFG->siteadmins);
714    $adminuser = get_complete_user_data('id', reset($adminids));
715
716    if ($adminuser->password === 'adminsetuppending') {
717        // prevent installation hijacking
718        if ($adminuser->lastip !== getremoteaddr()) {
719            print_error('installhijacked', 'admin');
720        }
721        // login user and let him set password and admin details
722        $adminuser->newadminuser = 1;
723        complete_user_login($adminuser);
724        redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
725
726    } else {
727        unset_config('adminsetuppending');
728    }
729
730} else {
731    // just make sure upgrade logging is properly terminated
732    upgrade_finished('upgradesettings.php');
733}
734
735if (has_capability('moodle/site:config', context_system::instance())) {
736    if ($fetchupdates) {
737        require_sesskey();
738        $updateschecker = \core\update\checker::instance();
739        if ($updateschecker->enabled()) {
740            $updateschecker->fetch();
741        }
742        redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
743    }
744}
745
746// Now we can be sure everything was upgraded and caches work fine,
747// redirect if necessary to make sure caching is enabled.
748if (!$cache) {
749    redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
750}
751
752// Check for valid admin user - no guest autologin
753require_login(0, false);
754if (isguestuser()) {
755    // Login as real user!
756    $SESSION->wantsurl = (string)new moodle_url('/admin/index.php');
757    redirect(get_login_url());
758}
759$context = context_system::instance();
760
761if (!has_capability('moodle/site:config', $context)) {
762    // Do not throw exception display an empty page with administration menu if visible for current user.
763    $PAGE->set_title($SITE->fullname);
764    $PAGE->set_heading($SITE->fullname);
765    echo $OUTPUT->header();
766    echo $OUTPUT->footer();
767    exit;
768}
769
770// check that site is properly customized
771$site = get_site();
772if (empty($site->shortname)) {
773    // probably new installation - lets return to frontpage after this step
774    // remove settings that we want uninitialised
775    unset_config('registerauth');
776    unset_config('timezone'); // Force admin to select timezone!
777    redirect('upgradesettings.php?return=site');
778}
779
780// setup critical warnings before printing admin tree block
781$insecuredataroot = is_dataroot_insecure(true);
782$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
783
784$adminroot = admin_get_root();
785
786// Check if there are any new admin settings which have still yet to be set
787if (any_new_admin_settings($adminroot)){
788    redirect('upgradesettings.php');
789}
790
791// Return to original page that started the plugin uninstallation if necessary.
792if (isset($SESSION->pluginuninstallreturn)) {
793    $return = $SESSION->pluginuninstallreturn;
794    unset($SESSION->pluginuninstallreturn);
795    if ($return) {
796        redirect($return);
797    }
798}
799
800// Everything should now be set up, and the user is an admin
801
802// Print default admin page with notifications.
803$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
804
805// We make the assumption that at least one schedule task should run once per day.
806$lastcron = $DB->get_field_sql('SELECT MAX(lastruntime) FROM {task_scheduled}');
807$cronoverdue = ($lastcron < time() - 3600 * 24);
808$dbproblems = $DB->diagnose();
809$maintenancemode = !empty($CFG->maintenance_enabled);
810
811// Available updates for Moodle core.
812$updateschecker = \core\update\checker::instance();
813$availableupdates = array();
814$availableupdatesfetch = null;
815
816if ($updateschecker->enabled()) {
817    // Only compute the update information when it is going to be displayed to the user.
818    $availableupdates['core'] = $updateschecker->get_update_info('core',
819        array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
820
821    // Available updates for contributed plugins
822    $pluginman = core_plugin_manager::instance();
823    foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
824        foreach ($plugintypeinstances as $pluginname => $plugininfo) {
825            $pluginavailableupdates = $plugininfo->available_updates();
826            if (!empty($pluginavailableupdates)) {
827                foreach ($pluginavailableupdates as $pluginavailableupdate) {
828                    if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
829                        $availableupdates[$plugintype.'_'.$pluginname] = array();
830                    }
831                    $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
832                }
833            }
834        }
835    }
836
837    // The timestamp of the most recent check for available updates
838    $availableupdatesfetch = $updateschecker->get_last_timefetched();
839}
840
841$buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
842//check if the site is registered on Moodle.org
843$registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
844// Check if there are any cache warnings.
845$cachewarnings = cache_helper::warnings();
846
847admin_externalpage_setup('adminnotifications');
848
849$output = $PAGE->get_renderer('core', 'admin');
850
851echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed, $cronoverdue, $dbproblems,
852                                       $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
853                                       $registered, $cachewarnings);
Note: See TracBrowser for help on using the repository browser.