3 // This file is part of Moodle - http://moodle.org/
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.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Main administration script.
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // Check that config.php exists, if not then call the install script
27 if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
32 // Check that PHP is of a sufficient version as soon as possible
33 if (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.";
41 // make sure iconv is available and actually works
42 if (!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.';
48 define('NO_OUTPUT_BUFFERING', true);
50 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
51 or (isset($_POST['cache']) and $_POST['cache'] === '0')
52 or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
53 // Prevent caching at all cost when visiting this page directly,
54 // we redirect to self once we known no upgrades are necessary.
55 // Note: $_GET and $_POST are used here intentionally because our param cleaning is not loaded yet.
56 // Note2: the sesskey is present in all block editing hacks, we can not redirect there, so enable caching.
57 define('CACHE_DISABLE_ALL', true);
59 // Force OPcache reset if used, we do not want any stale caches
60 // when detecting if upgrade necessary or when running upgrade.
61 if (function_exists('opcache_reset')) {
70 require('../config.php');
72 // Invalidate the cache of version.php in any circumstances to help core_component
73 // detecting if the version has changed and component cache should be reset.
74 if (function_exists('opcache_invalidate')) {
75 opcache_invalidate($CFG->dirroot
. '/version.php', true);
77 // Make sure the component cache gets rebuilt if necessary, any method that
78 // indirectly calls the protected init() method is good here.
79 core_component
::get_core_subsystems();
81 require_once($CFG->libdir
.'/adminlib.php'); // various admin-only functions
82 require_once($CFG->libdir
.'/upgradelib.php'); // general upgrade/install related functions
84 $id = optional_param('id', '', PARAM_TEXT
);
85 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL
);
86 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL
);
87 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL
);
88 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL
);
89 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL
);
90 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL
);
91 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW
);
94 $url = new moodle_url('/admin/index.php');
95 $url->param('cache', $cache);
99 // Are we returning from an add-on installation request at moodle.org/plugins?
100 if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall
)) {
101 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
102 'installaddonrequest' => $newaddonreq,
104 if (!isloggedin() or isguestuser()) {
105 // Login and go the the add-on tool page.
106 $SESSION->wantsurl
= $target->out();
107 redirect(get_login_url());
112 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
114 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
116 // Check some PHP server settings
118 if (ini_get_bool('session.auto_start')) {
119 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
122 if (!ini_get_bool('file_uploads')) {
123 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
126 if (is_float_problem()) {
127 print_error('phpfloatproblem', 'admin', '', $documentationlink);
130 // Set some necessary variables during set-up to avoid PHP warnings later on this page
131 if (!isset($CFG->release
)) {
134 if (!isset($CFG->version
)) {
137 if (!isset($CFG->branch
)) {
144 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
145 $CFG->target_release
= $release; // used during installation and upgrades
147 if (!$version or !$release) {
148 print_error('withoutversion', 'debug'); // without version, stop
151 if (!core_tables_exist()) {
152 $PAGE->set_pagelayout('maintenance');
153 $PAGE->set_popup_notification_allowed(false);
155 // fake some settings
156 $CFG->docroot
= 'http://docs.moodle.org';
158 $strinstallation = get_string('installation', 'install');
160 // remove current session content completely
161 \core\session\manager
::terminate_current();
163 if (empty($agreelicense)) {
164 $strlicense = get_string('license');
166 $PAGE->navbar
->add($strlicense);
167 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release
);
168 $PAGE->set_heading($strinstallation);
169 $PAGE->set_cacheable(false);
171 /** @var core_admin_renderer $output */
172 $output = $PAGE->get_renderer('core', 'admin');
173 echo $output->install_licence_page();
176 if (empty($confirmrelease)) {
177 require_once($CFG->libdir
.'/environmentlib.php');
178 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE
);
179 $strcurrentrelease = get_string('currentrelease');
181 $PAGE->navbar
->add($strcurrentrelease);
182 $PAGE->set_title($strinstallation);
183 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release
);
184 $PAGE->set_cacheable(false);
186 /** @var core_admin_renderer $output */
187 $output = $PAGE->get_renderer('core', 'admin');
188 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
192 // check plugin dependencies
194 if (!core_plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
195 $PAGE->navbar
->add(get_string('pluginscheck', 'admin'));
196 $PAGE->set_title($strinstallation);
197 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release
);
199 $output = $PAGE->get_renderer('core', 'admin');
200 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang
));
201 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
206 //TODO: add a page with list of non-standard plugins here
208 $strdatabasesetup = get_string('databasesetup');
209 upgrade_init_javascript();
211 $PAGE->navbar
->add($strdatabasesetup);
212 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release
);
213 $PAGE->set_heading($strinstallation);
214 $PAGE->set_cacheable(false);
216 $output = $PAGE->get_renderer('core', 'admin');
217 echo $output->header();
219 if (!$DB->setup_is_unicodedb()) {
220 if (!$DB->change_db_encoding()) {
221 // If could not convert successfully, throw error, and prevent installation
222 print_error('unicoderequired', 'admin');
226 install_core($version, true);
230 // Check version of Moodle code on disk compared with database
231 // and upgrade if possible.
233 $stradministration = get_string('administration');
234 $PAGE->set_context(context_system
::instance());
236 if (empty($CFG->version
)) {
237 print_error('missingconfigversion', 'debug');
240 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
241 if ($CFG->version
!= $DB->get_field('config', 'value', array('name'=>'version'))) {
243 redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
246 if (!$cache and $version > $CFG->version
) { // upgrade
248 // Warning about upgrading a test site.
250 if (defined('BEHAT_SITE_RUNNING')) {
254 // We purge all of MUC's caches here.
255 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
256 // This ensures a real config object is loaded and the stores will be purged.
257 // This is the only way we can purge custom caches such as memcache or APC.
258 // Note: all other calls to caches will still used the disabled API.
259 cache_helper
::purge_all(true);
260 // We then purge the regular caches.
263 $PAGE->set_pagelayout('maintenance');
264 $PAGE->set_popup_notification_allowed(false);
266 /** @var core_admin_renderer $output */
267 $output = $PAGE->get_renderer('core', 'admin');
269 if (upgrade_stale_php_files_present()) {
270 $PAGE->set_title($stradministration);
271 $PAGE->set_cacheable(false);
273 echo $output->upgrade_stale_php_files_page();
277 if (empty($confirmupgrade)) {
279 $a->oldversion
= "$CFG->release (".sprintf('%.2f', $CFG->version
).")";
280 $a->newversion
= "$release (".sprintf('%.2f', $version).")";
281 $strdatabasechecking = get_string('databasechecking', '', $a);
283 $PAGE->set_title($stradministration);
284 $PAGE->set_heading($strdatabasechecking);
285 $PAGE->set_cacheable(false);
287 echo $output->upgrade_confirm_page($a->newversion
, $maturity, $testsite);
290 } else if (empty($confirmrelease)){
291 require_once($CFG->libdir
.'/environmentlib.php');
292 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE
);
293 $strcurrentrelease = get_string('currentrelease');
295 $PAGE->navbar
->add($strcurrentrelease);
296 $PAGE->set_title($strcurrentrelease);
297 $PAGE->set_heading($strcurrentrelease);
298 $PAGE->set_cacheable(false);
300 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
303 } else if (empty($confirmplugins)) {
304 $strplugincheck = get_string('plugincheck');
306 $PAGE->navbar
->add($strplugincheck);
307 $PAGE->set_title($strplugincheck);
308 $PAGE->set_heading($strplugincheck);
309 $PAGE->set_cacheable(false);
311 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
314 // No sesskey support guaranteed here, because sessions might not work yet.
315 $updateschecker = \core\update\checker
::instance();
316 if ($updateschecker->enabled()) {
317 $updateschecker->fetch();
319 redirect($reloadurl);
322 $deployer = \core\update\deployer
::instance();
323 if ($deployer->enabled()) {
324 $deployer->initialize($reloadurl, $reloadurl);
326 $deploydata = $deployer->submitted_data();
327 if (!empty($deploydata)) {
328 // No sesskey support guaranteed here, because sessions might not work yet.
329 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
334 echo $output->upgrade_plugin_check_page(core_plugin_manager
::instance(), \core\update\checker
::instance(),
335 $version, $showallplugins, $reloadurl,
336 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1, 'cache'=>0)));
340 // Always verify plugin dependencies!
342 if (!core_plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
343 $PAGE->set_pagelayout('maintenance');
344 $PAGE->set_popup_notification_allowed(false);
345 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
346 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
351 // Launch main upgrade.
352 upgrade_core($version, true);
354 } else if ($version < $CFG->version
) {
355 // better stop here, we can not continue with plugin upgrades or anything else
356 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
359 // Updated human-readable release version if necessary
360 if (!$cache and $release <> $CFG->release
) { // Update the release version
361 set_config('release', $release);
364 if (!$cache and $branch <> $CFG->branch
) { // Update the branch
365 set_config('branch', $branch);
368 if (!$cache and moodle_needs_upgrading()) {
369 if (!$PAGE->headerprinted
) {
370 // means core upgrade or installation was not already done
372 /** @var core_admin_renderer $output */
373 $output = $PAGE->get_renderer('core', 'admin');
375 if (!$confirmplugins) {
376 $strplugincheck = get_string('plugincheck');
378 $PAGE->set_pagelayout('maintenance');
379 $PAGE->set_popup_notification_allowed(false);
380 $PAGE->navbar
->add($strplugincheck);
381 $PAGE->set_title($strplugincheck);
382 $PAGE->set_heading($strplugincheck);
383 $PAGE->set_cacheable(false);
387 $updateschecker = \core\update\checker
::instance();
388 if ($updateschecker->enabled()) {
389 $updateschecker->fetch();
391 redirect($PAGE->url
);
394 $deployer = \core\update\deployer
::instance();
395 if ($deployer->enabled()) {
396 $deployer->initialize($PAGE->url
, $PAGE->url
);
398 $deploydata = $deployer->submitted_data();
399 if (!empty($deploydata)) {
401 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
406 // Show plugins info.
407 echo $output->upgrade_plugin_check_page(core_plugin_manager
::instance(), \core\update\checker
::instance(),
408 $version, $showallplugins,
409 new moodle_url($PAGE->url
),
410 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1, 'cache'=>0)));
414 // Make sure plugin dependencies are always checked.
416 if (!core_plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
417 $PAGE->set_pagelayout('maintenance');
418 $PAGE->set_popup_notification_allowed(false);
419 $reloadurl = new moodle_url('/admin/index.php', array('cache' => 0));
420 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
426 // install/upgrade all plugins and other parts
427 upgrade_noncore(true);
430 // If this is the first install, indicate that this site is fully configured
431 // except the admin password
432 if (during_initial_install()) {
433 set_config('rolesactive', 1); // after this, during_initial_install will return false.
434 set_config('adminsetuppending', 1);
435 // we need this redirect to setup proper session
436 upgrade_finished("index.php?sessionstarted=1&lang=$CFG->lang");
439 // make sure admin user is created - this is the last step because we need
440 // session to be working properly in order to edit admin account
441 if (!empty($CFG->adminsetuppending
)) {
442 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL
);
443 if (!$sessionstarted) {
444 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
446 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL
);
447 if (!$sessionverify) {
448 $SESSION->sessionverify
= 1;
449 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
451 if (empty($SESSION->sessionverify
)) {
452 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
454 unset($SESSION->sessionverify
);
458 // Cleanup SESSION to make sure other code does not complain in the future.
459 unset($SESSION->has_timed_out
);
460 unset($SESSION->wantsurl
);
462 // 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
463 $adminids = explode(',', $CFG->siteadmins
);
464 $adminuser = get_complete_user_data('id', reset($adminids));
466 if ($adminuser->password
=== 'adminsetuppending') {
467 // prevent installation hijacking
468 if ($adminuser->lastip
!== getremoteaddr()) {
469 print_error('installhijacked', 'admin');
471 // login user and let him set password and admin details
472 $adminuser->newadminuser
= 1;
473 complete_user_login($adminuser);
474 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
477 unset_config('adminsetuppending');
481 // just make sure upgrade logging is properly terminated
482 upgrade_finished('upgradesettings.php');
485 if (has_capability('moodle/site:config', context_system
::instance())) {
488 $updateschecker = \core\update\checker
::instance();
489 if ($updateschecker->enabled()) {
490 $updateschecker->fetch();
492 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
496 // Now we can be sure everything was upgraded and caches work fine,
497 // redirect if necessary to make sure caching is enabled.
499 redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
502 // Check for valid admin user - no guest autologin
503 require_login(0, false);
505 // Login as real user!
506 $SESSION->wantsurl
= (string)new moodle_url('/admin/index.php');
507 redirect(get_login_url());
509 $context = context_system
::instance();
510 require_capability('moodle/site:config', $context);
512 // check that site is properly customized
514 if (empty($site->shortname
)) {
515 // probably new installation - lets return to frontpage after this step
516 // remove settings that we want uninitialised
517 unset_config('registerauth');
518 redirect('upgradesettings.php?return=site');
521 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
522 if (!empty($id) and $id == $CFG->siteidentifier
) {
523 set_config('registered', time());
526 // setup critical warnings before printing admin tree block
527 $insecuredataroot = is_dataroot_insecure(true);
528 $SESSION->admin_critical_warning
= ($insecuredataroot==INSECURE_DATAROOT_ERROR
);
530 $adminroot = admin_get_root();
532 // Check if there are any new admin settings which have still yet to be set
533 if (any_new_admin_settings($adminroot)){
534 redirect('upgradesettings.php');
537 // Return to original page that started the plugin uninstallation if necessary.
538 if (isset($SESSION->pluginuninstallreturn
)) {
539 $return = $SESSION->pluginuninstallreturn
;
540 unset($SESSION->pluginuninstallreturn
);
546 // Everything should now be set up, and the user is an admin
548 // Print default admin page with notifications.
549 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
551 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
552 $cronoverdue = ($lastcron < time() - 3600 * 24);
553 $dbproblems = $DB->diagnose();
554 $maintenancemode = !empty($CFG->maintenance_enabled
);
556 // Available updates for Moodle core
557 $updateschecker = \core\update\checker
::instance();
558 $availableupdates = array();
559 $availableupdates['core'] = $updateschecker->get_update_info('core',
560 array('minmaturity' => $CFG->updateminmaturity
, 'notifybuilds' => $CFG->updatenotifybuilds
));
562 // Available updates for contributed plugins
563 $pluginman = core_plugin_manager
::instance();
564 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
565 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
566 if (!empty($plugininfo->availableupdates
)) {
567 foreach ($plugininfo->availableupdates
as $pluginavailableupdate) {
568 if ($pluginavailableupdate->version
> $plugininfo->versiondisk
) {
569 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
570 $availableupdates[$plugintype.'_'.$pluginname] = array();
572 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
579 // The timestamp of the most recent check for available updates
580 $availableupdatesfetch = $updateschecker->get_last_timefetched();
582 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv
('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
583 //check if the site is registered on Moodle.org
584 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL
, 'confirmed' => 1));
586 admin_externalpage_setup('adminnotifications');
588 $output = $PAGE->get_renderer('core', 'admin');
589 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
590 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,