Merge branch 'MDL-26767' of git://github.com/timhunt/moodle
[moodle.git] / admin / index.php
blobf40ad17512a4ca299096f29e67dff688e620f651
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.2.0') < 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 "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion). ";
37 echo "Please upgrade your server software or use latest Moodle 1.9.x instead.";
38 die;
41 define('NO_OUTPUT_BUFFERING', true);
43 require('../config.php');
44 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
45 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
47 $id = optional_param('id', '', PARAM_TEXT);
48 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
49 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
50 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
51 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
53 // Check some PHP server settings
55 $PAGE->set_url('/admin/index.php');
56 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
58 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
60 if (ini_get_bool('session.auto_start')) {
61 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
64 if (ini_get_bool('magic_quotes_runtime')) {
65 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
68 if (!ini_get_bool('file_uploads')) {
69 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
72 if (is_float_problem()) {
73 print_error('phpfloatproblem', 'admin', '', $documentationlink);
76 // Set some necessary variables during set-up to avoid PHP warnings later on this page
77 if (!isset($CFG->framename)) {
78 $CFG->framename = '_top';
80 if (!isset($CFG->release)) {
81 $CFG->release = '';
83 if (!isset($CFG->version)) {
84 $CFG->version = '';
87 $version = null;
88 $release = null;
89 require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
90 $CFG->target_release = $release; // used during installation and upgrades
92 if (!$version or !$release) {
93 print_error('withoutversion', 'debug'); // without version, stop
96 // Turn off xmlstrictheaders during upgrade.
97 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
98 $CFG->xmlstrictheaders = false;
100 if (!core_tables_exist()) {
101 $PAGE->set_pagelayout('maintenance');
102 $PAGE->set_popup_notification_allowed(false);
104 // fake some settings
105 $CFG->docroot = 'http://docs.moodle.org';
107 $strinstallation = get_string('installation', 'install');
109 // remove current session content completely
110 session_get_instance()->terminate_current();
112 if (empty($agreelicense)) {
113 $strlicense = get_string('license');
114 $PAGE->navbar->add($strlicense);
115 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
116 $PAGE->set_heading($strinstallation);
117 $PAGE->set_cacheable(false);
118 echo $OUTPUT->header();
119 echo $OUTPUT->heading('<a href="http://moodle.org">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment');
120 echo $OUTPUT->heading(get_string('copyrightnotice'));
121 $copyrightnotice = text_to_html(get_string('gpl3'));
122 $copyrightnotice = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $copyrightnotice); // extremely ugly validation hack
123 echo $OUTPUT->box($copyrightnotice, 'copyrightnotice');
124 echo '<br />';
125 $continue = new single_button(new moodle_url('/admin/index.php', array('lang'=>$CFG->lang, 'agreelicense'=>1)), get_string('continue'), 'get');
126 echo $OUTPUT->confirm(get_string('doyouagree'), $continue, "http://docs.moodle.org/en/License");
127 echo $OUTPUT->footer();
128 die;
130 if (empty($confirmrelease)) {
131 $strcurrentrelease = get_string('currentrelease');
132 $PAGE->navbar->add($strcurrentrelease);
133 $PAGE->set_title($strinstallation);
134 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
135 $PAGE->set_cacheable(false);
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading("Moodle $release");
139 if (isset($maturity)) {
140 // main version.php declares moodle code maturity
141 if ($maturity < MATURITY_STABLE) {
142 $maturitylevel = get_string('maturity'.$maturity, 'admin');
143 echo $OUTPUT->box(
144 $OUTPUT->container(get_string('maturitycorewarning', 'admin', $maturitylevel)) .
145 $OUTPUT->container($OUTPUT->doc_link('admin/versions', get_string('morehelp'))),
146 'generalbox maturitywarning');
150 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
151 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
152 echo $OUTPUT->box($releasenoteslink, 'generalbox releasenoteslink');
154 require_once($CFG->libdir.'/environmentlib.php');
155 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
156 print_upgrade_reload("index.php?agreelicense=1&amp;lang=$CFG->lang");
157 } else {
158 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
159 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php', array('agreelicense'=>1, 'confirmrelease'=>1, 'lang'=>$CFG->lang)));
162 echo $OUTPUT->footer();
163 die;
166 $strdatabasesetup = get_string('databasesetup');
167 upgrade_init_javascript();
168 $PAGE->navbar->add($strdatabasesetup);
169 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
170 $PAGE->set_heading($strinstallation);
171 $PAGE->set_cacheable(false);
172 echo $OUTPUT->header();
174 if (!$DB->setup_is_unicodedb()) {
175 if (!$DB->change_db_encoding()) {
176 // If could not convert successfully, throw error, and prevent installation
177 print_error('unicoderequired', 'admin');
181 install_core($version, true);
185 // Check version of Moodle code on disk compared with database
186 // and upgrade if possible.
188 $stradministration = get_string('administration');
189 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
191 if (empty($CFG->version)) {
192 print_error('missingconfigversion', 'debug');
195 if ($version > $CFG->version) { // upgrade
196 $PAGE->set_pagelayout('maintenance');
197 $PAGE->set_popup_notification_allowed(false);
199 $a->oldversion = "$CFG->release ($CFG->version)";
200 $a->newversion = "$release ($version)";
201 $strdatabasechecking = get_string('databasechecking', '', $a);
203 if (empty($confirmupgrade)) {
204 $PAGE->set_title($stradministration);
205 $PAGE->set_heading($strdatabasechecking);
206 $PAGE->set_cacheable(false);
207 echo $OUTPUT->header();
208 if (isset($maturity)) {
209 // main version.php declares moodle code maturity
210 if ($maturity < MATURITY_STABLE) {
211 $maturitylevel = get_string('maturity'.$maturity, 'admin');
212 echo $OUTPUT->box(
213 $OUTPUT->container(get_string('maturitycorewarning', 'admin', $maturitylevel)) .
214 $OUTPUT->container($OUTPUT->doc_link('admin/versions', get_string('morehelp'))),
215 'generalbox maturitywarning');
218 $continueurl = new moodle_url('index.php', array('confirmupgrade' => 1));
219 $cancelurl = new moodle_url('index.php');
220 echo $OUTPUT->confirm(get_string('upgradesure', 'admin', $a->newversion), $continueurl, $cancelurl);
221 echo $OUTPUT->footer();
222 exit;
224 } else if (empty($confirmrelease)){
225 $strcurrentrelease = get_string('currentrelease');
226 $PAGE->navbar->add($strcurrentrelease);
227 $PAGE->set_title($strcurrentrelease);
228 $PAGE->set_heading($strcurrentrelease);
229 $PAGE->set_cacheable(false);
230 echo $OUTPUT->header();
231 echo $OUTPUT->heading("Moodle $release");
232 $releasenoteslink = get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes');
233 $releasenoteslink = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $releasenoteslink); // extremely ugly validation hack
234 echo $OUTPUT->box($releasenoteslink);
236 require_once($CFG->libdir.'/environmentlib.php');
237 if (!check_moodle_environment($release, $environment_results, true, ENV_SELECT_RELEASE)) {
238 print_upgrade_reload('index.php?confirmupgrade=1');
239 } else {
240 echo $OUTPUT->notification(get_string('environmentok', 'admin'), 'notifysuccess');
241 if (empty($CFG->skiplangupgrade)) {
242 echo $OUTPUT->box_start('generalbox', 'notice');
243 print_string('langpackwillbeupdated', 'admin');
244 echo $OUTPUT->box_end();
246 echo $OUTPUT->continue_button('index.php?confirmupgrade=1&confirmrelease=1');
249 echo $OUTPUT->footer();
250 die;
252 } elseif (empty($confirmplugins)) {
253 $strplugincheck = get_string('plugincheck');
254 $PAGE->navbar->add($strplugincheck);
255 $PAGE->set_title($strplugincheck);
256 $PAGE->set_heading($strplugincheck);
257 $PAGE->set_cacheable(false);
258 echo $OUTPUT->header();
259 echo $OUTPUT->heading($strplugincheck);
260 echo $OUTPUT->box_start('generalbox', 'notice');
261 print_string('pluginchecknotice');
262 echo $OUTPUT->box_end();
263 print_plugin_tables();
264 print_upgrade_reload('index.php?confirmupgrade=1&amp;confirmrelease=1');
265 $button = new single_button(new moodle_url('index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
266 $button->class = 'continuebutton';
267 echo $OUTPUT->render($button);
268 echo $OUTPUT->footer();
269 die();
271 } else {
272 // Launch main upgrade
273 upgrade_core($version, true);
275 } else if ($version < $CFG->version) {
276 // better stop here, we can not continue with plugin upgrades or anything else
277 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
280 // Updated human-readable release version if necessary
281 if ($release <> $CFG->release) { // Update the release version
282 set_config('release', $release);
285 if (moodle_needs_upgrading()) {
286 if (!$PAGE->headerprinted) {
287 // means core upgrade or installation was not already done
288 if (!$confirmplugins) {
289 $PAGE->set_pagelayout('maintenance');
290 $PAGE->set_popup_notification_allowed(false);
291 $strplugincheck = get_string('plugincheck');
292 $PAGE->navbar->add($strplugincheck);
293 $PAGE->set_title($strplugincheck);
294 $PAGE->set_heading($strplugincheck);
295 $PAGE->set_cacheable(false);
296 echo $OUTPUT->header();
297 echo $OUTPUT->heading($strplugincheck);
298 echo $OUTPUT->box_start('generalbox', 'notice');
299 print_string('pluginchecknotice');
300 echo $OUTPUT->box_end();
301 print_plugin_tables();
302 print_upgrade_reload('index.php');
303 $button = new single_button(new moodle_url('index.php', array('confirmplugincheck'=>1)), get_string('upgradestart', 'admin'), 'get');
304 $button->class = 'continuebutton';
305 echo $OUTPUT->render($button);
306 echo $OUTPUT->footer();
307 die();
310 // install/upgrade all plugins and other parts
311 upgrade_noncore(true);
314 // If this is the first install, indicate that this site is fully configured
315 // except the admin password
316 if (during_initial_install()) {
317 set_config('rolesactive', 1); // after this, during_initial_install will return false.
318 set_config('adminsetuppending', 1);
319 // we need this redirect to setup proper session
320 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
323 // make sure admin user is created - this is the last step because we need
324 // session to be working properly in order to edit admin account
325 if (!empty($CFG->adminsetuppending)) {
326 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
327 if (!$sessionstarted) {
328 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
329 } else {
330 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
331 if (!$sessionverify) {
332 $SESSION->sessionverify = 1;
333 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
334 } else {
335 if (empty($SESSION->sessionverify)) {
336 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
338 unset($SESSION->sessionverify);
342 // at this stage there can be only one admin - users may change username, so do not rely on that
343 $adminuser = get_complete_user_data('id', $CFG->siteadmins);
345 if ($adminuser->password === 'adminsetuppending') {
346 // prevent installation hijacking
347 if ($adminuser->lastip !== getremoteaddr()) {
348 print_error('installhijacked', 'admin');
350 // login user and let him set password and admin details
351 $adminuser->newadminuser = 1;
352 message_set_default_message_preferences($adminuser);
353 complete_user_login($adminuser, false);
354 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
356 } else {
357 unset_config('adminsetuppending');
360 } else {
361 // just make sure upgrade logging is properly terminated
362 upgrade_finished('upgradesettings.php');
365 // Turn xmlstrictheaders back on now.
366 $CFG->xmlstrictheaders = $origxmlstrictheaders;
367 unset($origxmlstrictheaders);
369 // Check for valid admin user - no guest autologin
370 require_login(0, false);
371 $context = get_context_instance(CONTEXT_SYSTEM);
372 require_capability('moodle/site:config', $context);
374 // check that site is properly customized
375 $site = get_site();
376 if (empty($site->shortname)) {
377 // probably new installation - lets return to frontpage after this step
378 // remove settings that we want uninitialised
379 unset_config('registerauth');
380 redirect('upgradesettings.php?return=site');
383 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
384 if (!empty($id) and $id == $CFG->siteidentifier) {
385 set_config('registered', time());
388 // setup critical warnings before printing admin tree block
389 $insecuredataroot = is_dataroot_insecure(true);
390 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
392 $adminroot = admin_get_root();
394 // Check if there are any new admin settings which have still yet to be set
395 if (any_new_admin_settings($adminroot)){
396 redirect('upgradesettings.php');
399 // Everything should now be set up, and the user is an admin
401 // Print default admin page with notifications.
402 admin_externalpage_setup('adminnotifications');
403 echo $OUTPUT->header();
405 if ($insecuredataroot == INSECURE_DATAROOT_WARNING) {
406 echo $OUTPUT->box(get_string('datarootsecuritywarning', 'admin', $CFG->dataroot), 'generalbox adminwarning');
407 } else if ($insecuredataroot == INSECURE_DATAROOT_ERROR) {
408 echo $OUTPUT->box(get_string('datarootsecurityerror', 'admin', $CFG->dataroot), 'generalbox adminerror');
412 if (defined('WARN_DISPLAY_ERRORS_ENABLED')) {
413 echo $OUTPUT->box(get_string('displayerrorswarning', 'admin'), 'generalbox adminwarning');
416 // If no recently cron run
417 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
418 if (time() - $lastcron > 3600 * 24) {
419 $helpbutton = $OUTPUT->help_icon('cron', 'admin');
420 echo $OUTPUT->box(get_string('cronwarning', 'admin').'&nbsp;'.$helpbutton, 'generalbox adminwarning');
423 // Hidden bloglevel upgrade
424 $showbloglevelupgrade = ($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete);
425 if ($showbloglevelupgrade) {
426 echo $OUTPUT->box(get_string('bloglevelupgradenotice', 'admin'), 'generalbox adminwarning');
429 // diagnose DB, especially the sloppy MyISAM tables
430 $diagnose = $DB->diagnose();
431 if ($diagnose !== NULL) {
432 echo $OUTPUT->box($diagnose, 'generalbox adminwarning');
435 // Alert if we are currently in maintenance mode
436 if (!empty($CFG->maintenance_enabled)) {
437 echo $OUTPUT->box(get_string('sitemaintenancewarning2', 'admin', "$CFG->wwwroot/$CFG->admin/settings.php?section=maintenancemode"), 'generalbox adminwarning');
440 //////////////////////////////////////////////////////////////////////////////////////////////////
441 //// IT IS ILLEGAL AND A VIOLATION OF THE GPL TO HIDE, REMOVE OR MODIFY THIS COPYRIGHT NOTICE ///
442 $copyrighttext = '<a href="http://moodle.org/">Moodle</a> '.
443 '<a href="http://docs.moodle.org/en/Release" title="'.$CFG->version.'">'.$CFG->release.'</a><br />'.
444 'Copyright &copy; 1999 onwards, Martin Dougiamas<br />'.
445 'and <a href="http://docs.moodle.org/en/Credits">many other contributors</a>.<br />'.
446 '<a href="http://docs.moodle.org/en/License">GNU Public License</a>';
447 echo $OUTPUT->box($copyrighttext, 'copyright');
448 //////////////////////////////////////////////////////////////////////////////////////////////////
450 echo $OUTPUT->footer();