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 require('../config.php');
51 require_once($CFG->libdir
.'/adminlib.php'); // various admin-only functions
52 require_once($CFG->libdir
.'/upgradelib.php'); // general upgrade/install related functions
53 require_once($CFG->libdir
.'/pluginlib.php'); // available updates notifications
55 $id = optional_param('id', '', PARAM_TEXT
);
56 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL
);
57 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL
);
58 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL
);
59 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL
);
60 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL
);
61 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL
);
62 $newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW
);
64 // Check some PHP server settings
66 if (is_null($newaddonreq)) {
67 $PAGE->set_url('/admin/index.php');
69 // We need to set the eventual add-on installation request in the $PAGE's URL
70 // so that it is stored in $SESSION->wantsurl and the admin is redirected
71 // correctly once they are logged-in.
72 $PAGE->set_url('/admin/index.php', array('installaddonrequest' => $newaddonreq));
74 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
76 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
78 if (ini_get_bool('session.auto_start')) {
79 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
82 if (ini_get_bool('magic_quotes_runtime')) {
83 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
86 if (!ini_get_bool('file_uploads')) {
87 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
90 if (is_float_problem()) {
91 print_error('phpfloatproblem', 'admin', '', $documentationlink);
94 // Set some necessary variables during set-up to avoid PHP warnings later on this page
95 if (!isset($CFG->release
)) {
98 if (!isset($CFG->version
)) {
101 if (!isset($CFG->branch
)) {
108 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
109 $CFG->target_release
= $release; // used during installation and upgrades
111 if (!$version or !$release) {
112 print_error('withoutversion', 'debug'); // without version, stop
115 if (!core_tables_exist()) {
116 $PAGE->set_pagelayout('maintenance');
117 $PAGE->set_popup_notification_allowed(false);
119 // fake some settings
120 $CFG->docroot
= 'http://docs.moodle.org';
122 $strinstallation = get_string('installation', 'install');
124 // remove current session content completely
125 session_get_instance()->terminate_current();
127 if (empty($agreelicense)) {
128 $strlicense = get_string('license');
130 $PAGE->navbar
->add($strlicense);
131 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release
);
132 $PAGE->set_heading($strinstallation);
133 $PAGE->set_cacheable(false);
135 $output = $PAGE->get_renderer('core', 'admin');
136 echo $output->install_licence_page();
139 if (empty($confirmrelease)) {
140 require_once($CFG->libdir
.'/environmentlib.php');
141 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE
);
142 $strcurrentrelease = get_string('currentrelease');
144 $PAGE->navbar
->add($strcurrentrelease);
145 $PAGE->set_title($strinstallation);
146 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release
);
147 $PAGE->set_cacheable(false);
149 $output = $PAGE->get_renderer('core', 'admin');
150 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
154 // check plugin dependencies
156 if (!plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
157 $PAGE->navbar
->add(get_string('pluginscheck', 'admin'));
158 $PAGE->set_title($strinstallation);
159 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release
);
161 $output = $PAGE->get_renderer('core', 'admin');
162 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang
));
163 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
168 //TODO: add a page with list of non-standard plugins here
170 $strdatabasesetup = get_string('databasesetup');
171 upgrade_init_javascript();
173 $PAGE->navbar
->add($strdatabasesetup);
174 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release
);
175 $PAGE->set_heading($strinstallation);
176 $PAGE->set_cacheable(false);
178 $output = $PAGE->get_renderer('core', 'admin');
179 echo $output->header();
181 if (!$DB->setup_is_unicodedb()) {
182 if (!$DB->change_db_encoding()) {
183 // If could not convert successfully, throw error, and prevent installation
184 print_error('unicoderequired', 'admin');
188 install_core($version, true);
192 // Check version of Moodle code on disk compared with database
193 // and upgrade if possible.
195 $stradministration = get_string('administration');
196 $PAGE->set_context(context_system
::instance());
198 if (empty($CFG->version
)) {
199 print_error('missingconfigversion', 'debug');
202 if ($version > $CFG->version
) { // upgrade
205 $PAGE->set_pagelayout('maintenance');
206 $PAGE->set_popup_notification_allowed(false);
208 if (upgrade_stale_php_files_present()) {
209 $PAGE->set_title($stradministration);
210 $PAGE->set_cacheable(false);
212 $output = $PAGE->get_renderer('core', 'admin');
213 echo $output->upgrade_stale_php_files_page();
217 if (empty($confirmupgrade)) {
219 $a->oldversion
= "$CFG->release (".sprintf('%.2f', $CFG->version
).")";
220 $a->newversion
= "$release (".sprintf('%.2f', $version).")";
221 $strdatabasechecking = get_string('databasechecking', '', $a);
223 $PAGE->set_title($stradministration);
224 $PAGE->set_heading($strdatabasechecking);
225 $PAGE->set_cacheable(false);
227 $output = $PAGE->get_renderer('core', 'admin');
228 echo $output->upgrade_confirm_page($a->newversion
, $maturity);
231 } else if (empty($confirmrelease)){
232 require_once($CFG->libdir
.'/environmentlib.php');
233 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE
);
234 $strcurrentrelease = get_string('currentrelease');
236 $PAGE->navbar
->add($strcurrentrelease);
237 $PAGE->set_title($strcurrentrelease);
238 $PAGE->set_heading($strcurrentrelease);
239 $PAGE->set_cacheable(false);
241 $output = $PAGE->get_renderer('core', 'admin');
242 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
245 } else if (empty($confirmplugins)) {
246 $strplugincheck = get_string('plugincheck');
248 $PAGE->navbar
->add($strplugincheck);
249 $PAGE->set_title($strplugincheck);
250 $PAGE->set_heading($strplugincheck);
251 $PAGE->set_cacheable(false);
253 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
255 // check plugin dependencies first
257 if (!plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
258 $output = $PAGE->get_renderer('core', 'admin');
259 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
265 // no sesskey support guaranteed here
266 if (empty($CFG->disableupdatenotifications
)) {
267 available_update_checker
::instance()->fetch();
269 redirect($reloadurl);
272 $output = $PAGE->get_renderer('core', 'admin');
274 $deployer = available_update_deployer
::instance();
275 if ($deployer->enabled()) {
276 $deployer->initialize($reloadurl, $reloadurl);
278 $deploydata = $deployer->submitted_data();
279 if (!empty($deploydata)) {
280 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
285 echo $output->upgrade_plugin_check_page(plugin_manager
::instance(), available_update_checker
::instance(),
286 $version, $showallplugins, $reloadurl,
287 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
291 // Launch main upgrade
292 upgrade_core($version, true);
294 } else if ($version < $CFG->version
) {
295 // better stop here, we can not continue with plugin upgrades or anything else
296 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
299 // Updated human-readable release version if necessary
300 if ($release <> $CFG->release
) { // Update the release version
301 set_config('release', $release);
304 if ($branch <> $CFG->branch
) { // Update the branch
305 set_config('branch', $branch);
308 if (moodle_needs_upgrading()) {
309 if (!$PAGE->headerprinted
) {
310 // means core upgrade or installation was not already done
311 if (!$confirmplugins) {
312 $strplugincheck = get_string('plugincheck');
314 $PAGE->set_pagelayout('maintenance');
315 $PAGE->set_popup_notification_allowed(false);
316 $PAGE->navbar
->add($strplugincheck);
317 $PAGE->set_title($strplugincheck);
318 $PAGE->set_heading($strplugincheck);
319 $PAGE->set_cacheable(false);
322 // no sesskey support guaranteed here
323 available_update_checker
::instance()->fetch();
324 redirect($PAGE->url
);
327 $output = $PAGE->get_renderer('core', 'admin');
329 $deployer = available_update_deployer
::instance();
330 if ($deployer->enabled()) {
331 $deployer->initialize($PAGE->url
, $PAGE->url
);
333 $deploydata = $deployer->submitted_data();
334 if (!empty($deploydata)) {
335 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
340 // check plugin dependencies first
342 if (!plugin_manager
::instance()->all_plugins_ok($version, $failed)) {
343 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url
);
348 // dependencies check passed, let's rock!
349 echo $output->upgrade_plugin_check_page(plugin_manager
::instance(), available_update_checker
::instance(),
350 $version, $showallplugins,
351 new moodle_url($PAGE->url
),
352 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
356 // install/upgrade all plugins and other parts
357 upgrade_noncore(true);
360 // If this is the first install, indicate that this site is fully configured
361 // except the admin password
362 if (during_initial_install()) {
363 set_config('rolesactive', 1); // after this, during_initial_install will return false.
364 set_config('adminsetuppending', 1);
365 // we need this redirect to setup proper session
366 upgrade_finished("index.php?sessionstarted=1&lang=$CFG->lang");
369 // make sure admin user is created - this is the last step because we need
370 // session to be working properly in order to edit admin account
371 if (!empty($CFG->adminsetuppending
)) {
372 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL
);
373 if (!$sessionstarted) {
374 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
376 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL
);
377 if (!$sessionverify) {
378 $SESSION->sessionverify
= 1;
379 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
381 if (empty($SESSION->sessionverify
)) {
382 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
384 unset($SESSION->sessionverify
);
388 // Cleanup SESSION to make sure other code does not complain in the future.
389 unset($SESSION->has_timed_out
);
390 unset($SESSION->wantsurl
);
392 // 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
393 $adminids = explode(',', $CFG->siteadmins
);
394 $adminuser = get_complete_user_data('id', reset($adminids));
396 if ($adminuser->password
=== 'adminsetuppending') {
397 // prevent installation hijacking
398 if ($adminuser->lastip
!== getremoteaddr()) {
399 print_error('installhijacked', 'admin');
401 // login user and let him set password and admin details
402 $adminuser->newadminuser
= 1;
403 complete_user_login($adminuser);
404 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
407 unset_config('adminsetuppending');
411 // just make sure upgrade logging is properly terminated
412 upgrade_finished('upgradesettings.php');
415 // Check for valid admin user - no guest autologin
416 require_login(0, false);
417 $context = context_system
::instance();
418 require_capability('moodle/site:config', $context);
420 // check that site is properly customized
422 if (empty($site->shortname
)) {
423 // probably new installation - lets return to frontpage after this step
424 // remove settings that we want uninitialised
425 unset_config('registerauth');
426 redirect('upgradesettings.php?return=site');
429 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
430 if (!empty($id) and $id == $CFG->siteidentifier
) {
431 set_config('registered', time());
434 // Check if we are returning from an add-on installation request at moodle.org/plugins
435 if (!is_null($newaddonreq)) {
436 if (!empty($CFG->disableonclickaddoninstall
)) {
437 // The feature is disabled in config.php, ignore the request.
439 redirect(new moodle_url('/admin/tool/installaddon/index.php', array(
440 'installaddonrequest' => $newaddonreq,
445 // setup critical warnings before printing admin tree block
446 $insecuredataroot = is_dataroot_insecure(true);
447 $SESSION->admin_critical_warning
= ($insecuredataroot==INSECURE_DATAROOT_ERROR
);
449 $adminroot = admin_get_root();
451 // Check if there are any new admin settings which have still yet to be set
452 if (any_new_admin_settings($adminroot)){
453 redirect('upgradesettings.php');
456 // Everything should now be set up, and the user is an admin
458 // Print default admin page with notifications.
459 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
461 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
462 $cronoverdue = ($lastcron < time() - 3600 * 24);
463 $dbproblems = $DB->diagnose();
464 $maintenancemode = !empty($CFG->maintenance_enabled
);
466 // Available updates for Moodle core
467 $updateschecker = available_update_checker
::instance();
468 $availableupdates = array();
469 $availableupdates['core'] = $updateschecker->get_update_info('core',
470 array('minmaturity' => $CFG->updateminmaturity
, 'notifybuilds' => $CFG->updatenotifybuilds
));
472 // Available updates for contributed plugins
473 $pluginman = plugin_manager
::instance();
474 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
475 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
476 if (!empty($plugininfo->availableupdates
)) {
477 foreach ($plugininfo->availableupdates
as $pluginavailableupdate) {
478 if ($pluginavailableupdate->version
> $plugininfo->versiondisk
) {
479 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
480 $availableupdates[$plugintype.'_'.$pluginname] = array();
482 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
489 // The timestamp of the most recent check for available updates
490 $availableupdatesfetch = $updateschecker->get_last_timefetched();
492 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv
('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
493 //check if the site is registered on Moodle.org
494 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL
, 'confirmed' => 1));
496 admin_externalpage_setup('adminnotifications');
500 $updateschecker->fetch();
501 redirect($PAGE->url
);
504 $output = $PAGE->get_renderer('core', 'admin');
505 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
506 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,