Merge branch 'w42_MDL-29866_m22_setupinit' of git://github.com/skodak/moodle
[moodle.git] / admin / index.php
blobd13aa3bd365428c6e3c24731bea77fbf2ffe55d8
1 <?php
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.
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/>.
18 /**
19 * Main administration script.
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
26 // Check that config.php exists, if not then call the install script
27 if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
29 die;
32 // Check that PHP is of a sufficient version as soon as possible
33 if (version_compare(phpversion(), '5.3.2') < 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.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).<br />";
37 echo "Please upgrade your server software or install older Moodle version.";
38 die;
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.';
45 die();
47 if (iconv('UTF-8', 'UTF-8//IGNORE', 'abc') !== 'abc') {
48 // known to be broken in mid-2011 MAMP installations
49 echo 'Broken iconv PHP extension detected, installation/upgrade can not continue.';
50 die;
53 define('NO_OUTPUT_BUFFERING', true);
55 require('../config.php');
56 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
57 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
59 $id = optional_param('id', '', PARAM_TEXT);
60 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
61 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
62 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
63 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
64 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
66 // Check some PHP server settings
68 $PAGE->set_url('/admin/index.php');
69 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
71 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
73 if (ini_get_bool('session.auto_start')) {
74 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
77 if (ini_get_bool('magic_quotes_runtime')) {
78 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
81 if (!ini_get_bool('file_uploads')) {
82 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
85 if (is_float_problem()) {
86 print_error('phpfloatproblem', 'admin', '', $documentationlink);
89 // Set some necessary variables during set-up to avoid PHP warnings later on this page
90 if (!isset($CFG->release)) {
91 $CFG->release = '';
93 if (!isset($CFG->version)) {
94 $CFG->version = '';
97 $version = null;
98 $release = null;
99 require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
100 $CFG->target_release = $release; // used during installation and upgrades
102 if (!$version or !$release) {
103 print_error('withoutversion', 'debug'); // without version, stop
106 // Turn off xmlstrictheaders during upgrade.
107 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
108 $CFG->xmlstrictheaders = false;
110 if (!core_tables_exist()) {
111 $PAGE->set_pagelayout('maintenance');
112 $PAGE->set_popup_notification_allowed(false);
114 // fake some settings
115 $CFG->docroot = 'http://docs.moodle.org';
117 $strinstallation = get_string('installation', 'install');
119 // remove current session content completely
120 session_get_instance()->terminate_current();
122 if (empty($agreelicense)) {
123 $strlicense = get_string('license');
124 $PAGE->navbar->add($strlicense);
125 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
126 $PAGE->set_heading($strinstallation);
127 $PAGE->set_cacheable(false);
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading('<a href="http://moodle.org">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment');
130 echo $OUTPUT->heading(get_string('copyrightnotice'));
131 $copyrightnotice = text_to_html(get_string('gpl3'));
132 $copyrightnotice = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $copyrightnotice); // extremely ugly validation hack
133 echo $OUTPUT->box($copyrightnotice, 'copyrightnotice');
134 echo '<br />';
135 $continue = new single_button(new moodle_url('/admin/index.php', array('lang'=>$CFG->lang, 'agreelicense'=>1)), get_string('continue'), 'get');
136 echo $OUTPUT->confirm(get_string('doyouagree'), $continue, "http://docs.moodle.org/dev/License");
137 echo $OUTPUT->footer();
138 die;
140 if (empty($confirmrelease)) {
141 $strcurrentrelease = get_string('currentrelease');
142 $PAGE->navbar->add($strcurrentrelease);
143 $PAGE->set_title($strinstallation);
144 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
145 $PAGE->set_cacheable(false);
146 echo $OUTPUT->header();
147 echo $OUTPUT->heading("Moodle $release");
149 if (isset($maturity)) {
150 // main version.php declares moodle code maturity
151 if ($maturity < MATURITY_STABLE) {
152 $maturitylevel = get_string('maturity'.$maturity, 'admin');
153 echo $OUTPUT->box(
154 $OUTPUT->container(get_string('maturitycorewarning', 'admin', $maturitylevel)) .
155 $OUTPUT->container($OUTPUT->doc_link('admin/versions', get_string('morehelp'))),
156 'generalbox maturitywarning');
160 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/dev/Releases');
161 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
162 echo $OUTPUT->box($releasenoteslink, 'generalbox releasenoteslink');
164 require_once($CFG->libdir.'/environmentlib.php');
165 if (!check_moodle_environment(normalize_version($release), $environment_results, true, ENV_SELECT_RELEASE)) {
166 print_upgrade_reload("index.php?agreelicense=1&amp;lang=$CFG->lang");
167 } else {
168 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
169 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php', array('agreelicense'=>1, 'confirmrelease'=>1, 'lang'=>$CFG->lang)));
172 echo $OUTPUT->footer();
173 die;
176 $strdatabasesetup = get_string('databasesetup');
177 upgrade_init_javascript();
178 $PAGE->navbar->add($strdatabasesetup);
179 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
180 $PAGE->set_heading($strinstallation);
181 $PAGE->set_cacheable(false);
182 echo $OUTPUT->header();
184 if (!$DB->setup_is_unicodedb()) {
185 if (!$DB->change_db_encoding()) {
186 // If could not convert successfully, throw error, and prevent installation
187 print_error('unicoderequired', 'admin');
191 install_core($version, true);
195 // Check version of Moodle code on disk compared with database
196 // and upgrade if possible.
198 $stradministration = get_string('administration');
199 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
201 if (empty($CFG->version)) {
202 print_error('missingconfigversion', 'debug');
205 if ($version > $CFG->version) { // upgrade
206 purge_all_caches();
207 $PAGE->set_pagelayout('maintenance');
208 $PAGE->set_popup_notification_allowed(false);
210 $a->oldversion = "$CFG->release ($CFG->version)";
211 $a->newversion = "$release ($version)";
212 $strdatabasechecking = get_string('databasechecking', '', $a);
214 if (empty($confirmupgrade)) {
215 $PAGE->set_title($stradministration);
216 $PAGE->set_heading($strdatabasechecking);
217 $PAGE->set_cacheable(false);
218 echo $OUTPUT->header();
219 if (isset($maturity)) {
220 // main version.php declares moodle code maturity
221 if ($maturity < MATURITY_STABLE) {
222 $maturitylevel = get_string('maturity'.$maturity, 'admin');
223 echo $OUTPUT->box(
224 $OUTPUT->container(get_string('maturitycorewarning', 'admin', $maturitylevel)) .
225 $OUTPUT->container($OUTPUT->doc_link('admin/versions', get_string('morehelp'))),
226 'generalbox maturitywarning');
229 $continueurl = new moodle_url('index.php', array('confirmupgrade' => 1));
230 $cancelurl = new moodle_url('index.php');
231 echo $OUTPUT->confirm(get_string('upgradesure', 'admin', $a->newversion), $continueurl, $cancelurl);
232 echo $OUTPUT->footer();
233 exit;
235 } else if (empty($confirmrelease)){
236 $strcurrentrelease = get_string('currentrelease');
237 $PAGE->navbar->add($strcurrentrelease);
238 $PAGE->set_title($strcurrentrelease);
239 $PAGE->set_heading($strcurrentrelease);
240 $PAGE->set_cacheable(false);
241 echo $OUTPUT->header();
242 echo $OUTPUT->heading("Moodle $release");
243 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/dev/Releases');
244 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
245 echo $OUTPUT->box($releasenoteslink);
247 require_once($CFG->libdir.'/environmentlib.php');
248 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
249 print_upgrade_reload('index.php?confirmupgrade=1');
250 } else {
251 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
252 if (empty($CFG->skiplangupgrade) and current_language() !== 'en') {
253 echo $OUTPUT->box_start('generalbox', 'notice');
254 print_string('langpackwillbeupdated', 'admin');
255 echo $OUTPUT->box_end();
257 echo $OUTPUT->continue_button('index.php?confirmupgrade=1&confirmrelease=1');
260 echo $OUTPUT->footer();
261 die;
263 } elseif (empty($confirmplugins)) {
264 $strplugincheck = get_string('plugincheck');
265 $PAGE->navbar->add($strplugincheck);
266 $PAGE->set_title($strplugincheck);
267 $PAGE->set_heading($strplugincheck);
268 $PAGE->set_cacheable(false);
269 $output = $PAGE->get_renderer('core', 'admin');
270 $pluginman = plugin_manager::instance();
272 echo $output->header();
273 echo $output->box_start('generalbox');
274 echo $output->container(get_string('pluginchecknotice', 'core_plugin'), 'generalbox', 'notice');
275 echo $output->plugins_check($pluginman->get_plugins(), array('full' => $showallplugins));
276 echo $output->box_end();
277 print_upgrade_reload('index.php?confirmupgrade=1&amp;confirmrelease=1');
278 $button = new single_button(new moodle_url('index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
279 $button->class = 'continuebutton';
280 echo $output->render($button);
281 echo $output->footer();
282 die();
284 } else {
285 // Launch main upgrade
286 upgrade_core($version, true);
288 } else if ($version < $CFG->version) {
289 // better stop here, we can not continue with plugin upgrades or anything else
290 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
293 // Updated human-readable release version if necessary
294 if ($release <> $CFG->release) { // Update the release version
295 set_config('release', $release);
298 if (moodle_needs_upgrading()) {
299 if (!$PAGE->headerprinted) {
300 // means core upgrade or installation was not already done
301 if (!$confirmplugins) {
302 $PAGE->set_pagelayout('maintenance');
303 $PAGE->set_popup_notification_allowed(false);
304 $strplugincheck = get_string('plugincheck');
305 $PAGE->navbar->add($strplugincheck);
306 $PAGE->set_title($strplugincheck);
307 $PAGE->set_heading($strplugincheck);
308 $PAGE->set_cacheable(false);
309 $output = $PAGE->get_renderer('core', 'admin');
310 $pluginman = plugin_manager::instance();
312 echo $output->header();
313 echo $output->box_start('generalbox');
314 echo $output->container(get_string('pluginchecknotice', 'core_plugin'), 'generalbox', 'notice');
315 echo $output->plugins_check($pluginman->get_plugins(), array('full' => $showallplugins));
316 echo $output->box_end();
317 print_upgrade_reload('index.php');
318 $button = new single_button(new moodle_url('index.php', array('confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
319 $button->class = 'continuebutton';
320 echo $output->render($button);
321 echo $output->footer();
322 die();
325 // install/upgrade all plugins and other parts
326 upgrade_noncore(true);
329 // If this is the first install, indicate that this site is fully configured
330 // except the admin password
331 if (during_initial_install()) {
332 set_config('rolesactive', 1); // after this, during_initial_install will return false.
333 set_config('adminsetuppending', 1);
334 // we need this redirect to setup proper session
335 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
338 // make sure admin user is created - this is the last step because we need
339 // session to be working properly in order to edit admin account
340 if (!empty($CFG->adminsetuppending)) {
341 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
342 if (!$sessionstarted) {
343 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
344 } else {
345 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
346 if (!$sessionverify) {
347 $SESSION->sessionverify = 1;
348 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
349 } else {
350 if (empty($SESSION->sessionverify)) {
351 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
353 unset($SESSION->sessionverify);
357 // at this stage there can be only one admin - users may change username, so do not rely on that
358 $adminuser = get_complete_user_data('id', $CFG->siteadmins);
360 if ($adminuser->password === 'adminsetuppending') {
361 // prevent installation hijacking
362 if ($adminuser->lastip !== getremoteaddr()) {
363 print_error('installhijacked', 'admin');
365 // login user and let him set password and admin details
366 $adminuser->newadminuser = 1;
367 complete_user_login($adminuser);
368 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
370 } else {
371 unset_config('adminsetuppending');
374 } else {
375 // just make sure upgrade logging is properly terminated
376 upgrade_finished('upgradesettings.php');
379 // Turn xmlstrictheaders back on now.
380 $CFG->xmlstrictheaders = $origxmlstrictheaders;
381 unset($origxmlstrictheaders);
383 // Check for valid admin user - no guest autologin
384 require_login(0, false);
385 $context = get_context_instance(CONTEXT_SYSTEM);
386 require_capability('moodle/site:config', $context);
388 // check that site is properly customized
389 $site = get_site();
390 if (empty($site->shortname)) {
391 // probably new installation - lets return to frontpage after this step
392 // remove settings that we want uninitialised
393 unset_config('registerauth');
394 redirect('upgradesettings.php?return=site');
397 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
398 if (!empty($id) and $id == $CFG->siteidentifier) {
399 set_config('registered', time());
402 // setup critical warnings before printing admin tree block
403 $insecuredataroot = is_dataroot_insecure(true);
404 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
406 $adminroot = admin_get_root();
408 // Check if there are any new admin settings which have still yet to be set
409 if (any_new_admin_settings($adminroot)){
410 redirect('upgradesettings.php');
413 // Everything should now be set up, and the user is an admin
415 // Print default admin page with notifications.
416 admin_externalpage_setup('adminnotifications');
417 echo $OUTPUT->header();
419 // Unstable code warning
420 if (isset($maturity)) {
421 if ($maturity < MATURITY_STABLE) {
422 $maturitylevel = get_string('maturity'.$maturity, 'admin');
423 echo $OUTPUT->box(
424 get_string('maturitycoreinfo', 'admin', $maturitylevel) . ' ' .
425 $OUTPUT->doc_link('admin/versions', get_string('morehelp')),
426 'generalbox adminwarning maturityinfo');
430 if ($insecuredataroot == INSECURE_DATAROOT_WARNING) {
431 echo $OUTPUT->box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot), 'generalbox adminwarning');
432 } else if ($insecuredataroot == INSECURE_DATAROOT_ERROR) {
433 echo $OUTPUT->box(get_string('datarootsecurityerror', 'admin', $CFG->dataroot), 'generalbox adminerror');
437 if (defined('WARN_DISPLAY_ERRORS_ENABLED')) {
438 echo $OUTPUT->box(get_string('displayerrorswarning', 'admin'), 'generalbox adminwarning');
441 // If no recently cron run
442 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
443 if (time() - $lastcron > 3600 * 24) {
444 $helpbutton = $OUTPUT->help_icon('cron', 'admin');
445 echo $OUTPUT->box(get_string('cronwarning', 'admin').'&nbsp;'.$helpbutton, 'generalbox adminwarning');
448 // diagnose DB, especially the sloppy MyISAM tables
449 $diagnose = $DB->diagnose();
450 if ($diagnose !== NULL) {
451 echo $OUTPUT->box($diagnose, 'generalbox adminwarning');
454 // Alert if we are currently in maintenance mode
455 if (!empty($CFG->maintenance_enabled)) {
456 echo $OUTPUT->box(get_string('sitemaintenancewarning2', 'admin', "$CFG->wwwroot/$CFG->admin/settings.php?section=maintenancemode"), 'generalbox adminwarning');
459 //////////////////////////////////////////////////////////////////////////////////////////////////
460 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
461 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
462 '<a href="http://docs.moodle.org/dev/Releases" title="'.$CFG->version.'">'.$CFG->release.'</a><br />'.
463 'Copyright &copy; 1999 onwards, Martin Dougiamas<br />'.
464 'and <a href="http://docs.moodle.org/dev/Credits">many other contributors</a>.<br />'.
465 '<a href="http://docs.moodle.org/dev/License">GNU Public License</a>';
466 echo $OUTPUT->box($copyrighttext, 'copyright');
467 //////////////////////////////////////////////////////////////////////////////////////////////////
469 echo $OUTPUT->footer();