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.3.3') < 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.5 or later requires at least PHP 5.3.3 (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);
60 require('../config.php');
61 require_once($CFG->libdir
.'/adminlib.php'); // various admin-only functions
62 require_once($CFG->libdir
.'/upgradelib.php'); // general upgrade/install related functions
63 require_once($CFG->libdir
.'/pluginlib.php'); // available updates notifications
65 $id = optional_param('id', '', PARAM_TEXT
);
66 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL
);
67 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL
);
68 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL
);
69 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL
);
70 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL
);
71 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL
);
72 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW
);
73 $cache = optional_param('cache', 0, PARAM_BOOL
);
76 $url = new moodle_url('/admin/index.php');
77 $url->param('cache', $cache);
81 // Are we returning from an add-on installation request at moodle.org/plugins?
82 if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall
)) {
83 $target = new moodle_url('/admin/tool/installaddon/index.php', array(
84 'installaddonrequest' => $newaddonreq,
86 if (!isloggedin() or isguestuser()) {
87 // Login and go the the add-on tool page.
88 $SESSION->wantsurl
= $target->out();
89 redirect(get_login_url());
94 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
96 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
98 // Check some PHP server settings
100 if (ini_get_bool('session.auto_start')) {
101 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
104 if (ini_get_bool('magic_quotes_runtime')) {
105 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
108 if (!ini_get_bool('file_uploads')) {
109 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
112 if (is_float_problem()) {
113 print_error('phpfloatproblem', 'admin', '', $documentationlink);
116 // Set some necessary variables during set-up to avoid PHP warnings later on this page
117 if (!isset($CFG->release
)) {
120 if (!isset($CFG->version
)) {
123 if (!isset($CFG->branch
)) {
130 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
131 $CFG->target_release
= $release; // used during installation and upgrades
133 if (!$version or !$release) {
134 print_error('withoutversion', 'debug'); // without version, stop
137 if (!core_tables_exist()) {
138 $PAGE->set_pagelayout('maintenance');
139 $PAGE->set_popup_notification_allowed(false);
141 // fake some settings
142 $CFG->docroot
= 'http://docs.moodle.org';
144 $strinstallation = get_string('installation', 'install');
146 // remove current session content completely
147 session_get_instance()->terminate_current();
149 if (empty($agreelicense)) {
150 $strlicense = get_string('license');
152 $PAGE->navbar
->add($strlicense);
153 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release
);
154 $PAGE->set_heading($strinstallation);
155 $PAGE->set_cacheable(false);
157 $output = $PAGE->get_renderer('core', 'admin');
158 echo $output->install_licence_page();
161 if (empty($confirmrelease)) {
162 require_once($CFG->libdir
.'/environmentlib.php');
163 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE
);
164 $strcurrentrelease = get_string('currentrelease');
166 $PAGE->navbar
->add($strcurrentrelease);
167 $PAGE->set_title($strinstallation);
168 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release
);
169 $PAGE->set_cacheable(false);
171 $output = $PAGE->get_renderer('core', 'admin');
172 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
176 // check plugin dependencies
178 if (!plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
179 $PAGE->navbar
->add(get_string('pluginscheck', 'admin'));
180 $PAGE->set_title($strinstallation);
181 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release
);
183 $output = $PAGE->get_renderer('core', 'admin');
184 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang
));
185 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
190 //TODO: add a page with list of non-standard plugins here
192 $strdatabasesetup = get_string('databasesetup');
193 upgrade_init_javascript();
195 $PAGE->navbar
->add($strdatabasesetup);
196 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release
);
197 $PAGE->set_heading($strinstallation);
198 $PAGE->set_cacheable(false);
200 $output = $PAGE->get_renderer('core', 'admin');
201 echo $output->header();
203 if (!$DB->setup_is_unicodedb()) {
204 if (!$DB->change_db_encoding()) {
205 // If could not convert successfully, throw error, and prevent installation
206 print_error('unicoderequired', 'admin');
210 install_core($version, true);
214 // Check version of Moodle code on disk compared with database
215 // and upgrade if possible.
217 $stradministration = get_string('administration');
218 $PAGE->set_context(context_system
::instance());
220 if (empty($CFG->version
)) {
221 print_error('missingconfigversion', 'debug');
224 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
226 if ($CFG->version
!= $DB->get_field('config', 'value', array('name'=>'version'))) {
228 redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
232 if ($version > $CFG->version
) { // upgrade
234 // Warning about upgrading a test site.
236 if (defined('BEHAT_SITE_RUNNING')) {
240 // We purge all of MUC's caches here.
241 // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
242 // This ensures a real config object is loaded and the stores will be purged.
243 // This is the only way we can purge custom caches such as memcache or APC.
244 // Note: all other calls to caches will still used the disabled API.
245 cache_helper
::purge_all(true);
246 // We then purge the regular caches.
249 $PAGE->set_pagelayout('maintenance');
250 $PAGE->set_popup_notification_allowed(false);
252 /** @var core_admin_renderer $output */
253 $output = $PAGE->get_renderer('core', 'admin');
255 if (upgrade_stale_php_files_present()) {
256 $PAGE->set_title($stradministration);
257 $PAGE->set_cacheable(false);
259 echo $output->upgrade_stale_php_files_page();
263 if (empty($confirmupgrade)) {
265 $a->oldversion
= "$CFG->release (".sprintf('%.2f', $CFG->version
).")";
266 $a->newversion
= "$release (".sprintf('%.2f', $version).")";
267 $strdatabasechecking = get_string('databasechecking', '', $a);
269 $PAGE->set_title($stradministration);
270 $PAGE->set_heading($strdatabasechecking);
271 $PAGE->set_cacheable(false);
273 echo $output->upgrade_confirm_page($a->newversion
, $maturity, $testsite);
276 } else if (empty($confirmrelease)){
277 require_once($CFG->libdir
.'/environmentlib.php');
278 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE
);
279 $strcurrentrelease = get_string('currentrelease');
281 $PAGE->navbar
->add($strcurrentrelease);
282 $PAGE->set_title($strcurrentrelease);
283 $PAGE->set_heading($strcurrentrelease);
284 $PAGE->set_cacheable(false);
286 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
289 } else if (empty($confirmplugins)) {
290 $strplugincheck = get_string('plugincheck');
292 $PAGE->navbar
->add($strplugincheck);
293 $PAGE->set_title($strplugincheck);
294 $PAGE->set_heading($strplugincheck);
295 $PAGE->set_cacheable(false);
297 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
300 $updateschecker = available_update_checker
::instance();
301 if ($updateschecker->enabled()) {
302 // No sesskey support guaranteed here, because sessions might not work yet.
303 $updateschecker->fetch();
305 redirect($reloadurl);
308 $deployer = available_update_deployer
::instance();
309 if ($deployer->enabled()) {
310 $deployer->initialize($reloadurl, $reloadurl);
312 $deploydata = $deployer->submitted_data();
313 if (!empty($deploydata)) {
314 // No sesskey support guaranteed here, because sessions might not work yet.
315 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
320 echo $output->upgrade_plugin_check_page(plugin_manager
::instance(), available_update_checker
::instance(),
321 $version, $showallplugins, $reloadurl,
322 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
326 // Always verify plugin dependencies!
328 if (!plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
329 $PAGE->set_pagelayout('maintenance');
330 $PAGE->set_popup_notification_allowed(false);
331 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
332 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
337 // Launch main upgrade.
338 upgrade_core($version, true);
340 } else if ($version < $CFG->version
) {
341 // better stop here, we can not continue with plugin upgrades or anything else
342 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
345 // Updated human-readable release version if necessary
346 if ($release <> $CFG->release
) { // Update the release version
347 set_config('release', $release);
350 if ($branch <> $CFG->branch
) { // Update the branch
351 set_config('branch', $branch);
354 if (moodle_needs_upgrading()) {
355 if (!$PAGE->headerprinted
) {
356 // means core upgrade or installation was not already done
358 /** @var core_admin_renderer $output */
359 $output = $PAGE->get_renderer('core', 'admin');
361 if (!$confirmplugins) {
362 $strplugincheck = get_string('plugincheck');
364 $PAGE->set_pagelayout('maintenance');
365 $PAGE->set_popup_notification_allowed(false);
366 $PAGE->navbar
->add($strplugincheck);
367 $PAGE->set_title($strplugincheck);
368 $PAGE->set_heading($strplugincheck);
369 $PAGE->set_cacheable(false);
372 $updateschecker = available_update_checker
::instance();
373 if ($updateschecker->enabled()) {
375 $updateschecker->fetch();
377 redirect($reloadurl);
380 $deployer = available_update_deployer
::instance();
381 if ($deployer->enabled()) {
382 $deployer->initialize($PAGE->url
, $PAGE->url
);
384 $deploydata = $deployer->submitted_data();
385 if (!empty($deploydata)) {
387 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
392 // Show plugins info.
393 echo $output->upgrade_plugin_check_page(plugin_manager
::instance(), available_update_checker
::instance(),
394 $version, $showallplugins,
395 new moodle_url($PAGE->url
),
396 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
400 // Always verify plugin dependencies!
402 if (!plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
403 $PAGE->set_pagelayout('maintenance');
404 $PAGE->set_popup_notification_allowed(false);
405 $reloadurl = new moodle_url('/admin/index.php', array('cache' => 0));
406 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
411 // install/upgrade all plugins and other parts
412 upgrade_noncore(true);
415 // If this is the first install, indicate that this site is fully configured
416 // except the admin password
417 if (during_initial_install()) {
418 set_config('rolesactive', 1); // after this, during_initial_install will return false.
419 set_config('adminsetuppending', 1);
420 // we need this redirect to setup proper session
421 upgrade_finished("index.php?sessionstarted=1&lang=$CFG->lang");
424 // make sure admin user is created - this is the last step because we need
425 // session to be working properly in order to edit admin account
426 if (!empty($CFG->adminsetuppending
)) {
427 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL
);
428 if (!$sessionstarted) {
429 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
431 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL
);
432 if (!$sessionverify) {
433 $SESSION->sessionverify
= 1;
434 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
436 if (empty($SESSION->sessionverify
)) {
437 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
439 unset($SESSION->sessionverify
);
443 // Cleanup SESSION to make sure other code does not complain in the future.
444 unset($SESSION->has_timed_out
);
445 unset($SESSION->wantsurl
);
447 // 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
448 $adminids = explode(',', $CFG->siteadmins
);
449 $adminuser = get_complete_user_data('id', reset($adminids));
451 if ($adminuser->password
=== 'adminsetuppending') {
452 // prevent installation hijacking
453 if ($adminuser->lastip
!== getremoteaddr()) {
454 print_error('installhijacked', 'admin');
456 // login user and let him set password and admin details
457 $adminuser->newadminuser
= 1;
458 complete_user_login($adminuser);
459 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
462 unset_config('adminsetuppending');
466 // just make sure upgrade logging is properly terminated
467 upgrade_finished('upgradesettings.php');
470 if (has_capability('moodle/site:config', context_system
::instance())) {
472 $updateschecker = available_update_checker
::instance();
473 if ($updateschecker->enabled()) {
475 $updateschecker->fetch();
477 redirect(new moodle_url('/admin/index.php', array('cache' => 0)));
481 // Now we can be sure everything was upgraded and caches work fine,
482 // redirect if necessary to make sure caching is enabled.
483 if (!$cache and !optional_param('sesskey', '', PARAM_RAW
)) {
484 redirect(new moodle_url($PAGE->url
, array('cache' => 1)));
487 // Check for valid admin user - no guest autologin
488 require_login(0, false);
490 // Login as real user!
491 $SESSION->wantsurl
= (string)new moodle_url('/admin/index.php');
492 redirect(get_login_url());
494 $context = context_system
::instance();
495 require_capability('moodle/site:config', $context);
497 // check that site is properly customized
499 if (empty($site->shortname
)) {
500 // probably new installation - lets return to frontpage after this step
501 // remove settings that we want uninitialised
502 unset_config('registerauth');
503 redirect('upgradesettings.php?return=site');
506 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
507 if (!empty($id) and $id == $CFG->siteidentifier
) {
508 set_config('registered', time());
511 // setup critical warnings before printing admin tree block
512 $insecuredataroot = is_dataroot_insecure(true);
513 $SESSION->admin_critical_warning
= ($insecuredataroot==INSECURE_DATAROOT_ERROR
);
515 $adminroot = admin_get_root();
517 // Check if there are any new admin settings which have still yet to be set
518 if (any_new_admin_settings($adminroot)){
519 redirect('upgradesettings.php');
522 // Everything should now be set up, and the user is an admin
524 // Print default admin page with notifications.
525 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
527 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
528 $cronoverdue = ($lastcron < time() - 3600 * 24);
529 $dbproblems = $DB->diagnose();
530 $maintenancemode = !empty($CFG->maintenance_enabled
);
532 // Available updates for Moodle core
533 $updateschecker = available_update_checker
::instance();
534 $availableupdates = array();
535 $availableupdates['core'] = $updateschecker->get_update_info('core',
536 array('minmaturity' => $CFG->updateminmaturity
, 'notifybuilds' => $CFG->updatenotifybuilds
));
538 // Available updates for contributed plugins
539 $pluginman = plugin_manager
::instance();
540 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
541 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
542 if (!empty($plugininfo->availableupdates
)) {
543 foreach ($plugininfo->availableupdates
as $pluginavailableupdate) {
544 if ($pluginavailableupdate->version
> $plugininfo->versiondisk
) {
545 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
546 $availableupdates[$plugintype.'_'.$pluginname] = array();
548 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
555 // The timestamp of the most recent check for available updates
556 $availableupdatesfetch = $updateschecker->get_last_timefetched();
558 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv
('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
559 //check if the site is registered on Moodle.org
560 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL
, 'confirmed' => 1));
562 admin_externalpage_setup('adminnotifications');
564 $output = $PAGE->get_renderer('core', 'admin');
565 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
566 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,