Merge branch 'MDL-32903' of git://github.com/netspotau/moodle-mod_assign
[moodle.git] / admin / index.php
blob5b01be2749b01435d1df46bd19d859e3dc1d6f45
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
58 require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
60 $id = optional_param('id', '', PARAM_TEXT);
61 $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
62 $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
63 $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
64 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
65 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
66 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
68 // Check some PHP server settings
70 $PAGE->set_url('/admin/index.php');
71 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
73 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
75 if (ini_get_bool('session.auto_start')) {
76 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
79 if (ini_get_bool('magic_quotes_runtime')) {
80 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
83 if (!ini_get_bool('file_uploads')) {
84 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
87 if (is_float_problem()) {
88 print_error('phpfloatproblem', 'admin', '', $documentationlink);
91 // Set some necessary variables during set-up to avoid PHP warnings later on this page
92 if (!isset($CFG->release)) {
93 $CFG->release = '';
95 if (!isset($CFG->version)) {
96 $CFG->version = '';
98 if (!isset($CFG->branch)) {
99 $CFG->branch = '';
102 $version = null;
103 $release = null;
104 $branch = null;
105 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
106 $CFG->target_release = $release; // used during installation and upgrades
108 if (!$version or !$release) {
109 print_error('withoutversion', 'debug'); // without version, stop
112 // Turn off xmlstrictheaders during upgrade.
113 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
114 $CFG->xmlstrictheaders = false;
116 if (!core_tables_exist()) {
117 $PAGE->set_pagelayout('maintenance');
118 $PAGE->set_popup_notification_allowed(false);
120 // fake some settings
121 $CFG->docroot = 'http://docs.moodle.org';
123 $strinstallation = get_string('installation', 'install');
125 // remove current session content completely
126 session_get_instance()->terminate_current();
128 if (empty($agreelicense)) {
129 $strlicense = get_string('license');
131 $PAGE->navbar->add($strlicense);
132 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
133 $PAGE->set_heading($strinstallation);
134 $PAGE->set_cacheable(false);
136 $output = $PAGE->get_renderer('core', 'admin');
137 echo $output->install_licence_page();
138 die();
140 if (empty($confirmrelease)) {
141 require_once($CFG->libdir.'/environmentlib.php');
142 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
143 $strcurrentrelease = get_string('currentrelease');
145 $PAGE->navbar->add($strcurrentrelease);
146 $PAGE->set_title($strinstallation);
147 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
148 $PAGE->set_cacheable(false);
150 $output = $PAGE->get_renderer('core', 'admin');
151 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
152 die();
155 //TODO: add a page with list of non-standard plugins here
157 $strdatabasesetup = get_string('databasesetup');
158 upgrade_init_javascript();
160 $PAGE->navbar->add($strdatabasesetup);
161 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
162 $PAGE->set_heading($strinstallation);
163 $PAGE->set_cacheable(false);
165 $output = $PAGE->get_renderer('core', 'admin');
166 echo $output->header();
168 if (!$DB->setup_is_unicodedb()) {
169 if (!$DB->change_db_encoding()) {
170 // If could not convert successfully, throw error, and prevent installation
171 print_error('unicoderequired', 'admin');
175 install_core($version, true);
179 // Check version of Moodle code on disk compared with database
180 // and upgrade if possible.
182 $stradministration = get_string('administration');
183 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
185 if (empty($CFG->version)) {
186 print_error('missingconfigversion', 'debug');
189 if ($version > $CFG->version) { // upgrade
190 purge_all_caches();
191 $PAGE->set_pagelayout('maintenance');
192 $PAGE->set_popup_notification_allowed(false);
194 if (upgrade_stale_php_files_present()) {
195 $PAGE->set_title($stradministration);
196 $PAGE->set_cacheable(false);
198 $output = $PAGE->get_renderer('core', 'admin');
199 echo $output->upgrade_stale_php_files_page();
200 die();
203 if (empty($confirmupgrade)) {
204 $a = new stdClass();
205 $a->oldversion = "$CFG->release ($CFG->version)";
206 $a->newversion = "$release ($version)";
207 $strdatabasechecking = get_string('databasechecking', '', $a);
209 $PAGE->set_title($stradministration);
210 $PAGE->set_heading($strdatabasechecking);
211 $PAGE->set_cacheable(false);
213 $output = $PAGE->get_renderer('core', 'admin');
214 echo $output->upgrade_confirm_page($a->newversion, $maturity);
215 die();
217 } else if (empty($confirmrelease)){
218 require_once($CFG->libdir.'/environmentlib.php');
219 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
220 $strcurrentrelease = get_string('currentrelease');
222 $PAGE->navbar->add($strcurrentrelease);
223 $PAGE->set_title($strcurrentrelease);
224 $PAGE->set_heading($strcurrentrelease);
225 $PAGE->set_cacheable(false);
227 $output = $PAGE->get_renderer('core', 'admin');
228 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
229 die();
231 } else if (empty($confirmplugins)) {
232 $strplugincheck = get_string('plugincheck');
234 $PAGE->navbar->add($strplugincheck);
235 $PAGE->set_title($strplugincheck);
236 $PAGE->set_heading($strplugincheck);
237 $PAGE->set_cacheable(false);
239 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
241 if ($fetchupdates) {
242 // no sesskey support guaranteed here
243 if (empty($CFG->disableupdatenotifications)) {
244 available_update_checker::instance()->fetch();
246 redirect($reloadurl);
249 $output = $PAGE->get_renderer('core', 'admin');
250 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
251 $version, $showallplugins, $reloadurl,
252 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
253 die();
255 } else {
256 // Launch main upgrade
257 upgrade_core($version, true);
259 } else if ($version < $CFG->version) {
260 // better stop here, we can not continue with plugin upgrades or anything else
261 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
264 // Updated human-readable release version if necessary
265 if ($release <> $CFG->release) { // Update the release version
266 set_config('release', $release);
269 if ($branch <> $CFG->branch) { // Update the branch
270 set_config('branch', $branch);
273 if (moodle_needs_upgrading()) {
274 if (!$PAGE->headerprinted) {
275 // means core upgrade or installation was not already done
276 if (!$confirmplugins) {
277 $strplugincheck = get_string('plugincheck');
279 $PAGE->set_pagelayout('maintenance');
280 $PAGE->set_popup_notification_allowed(false);
281 $PAGE->navbar->add($strplugincheck);
282 $PAGE->set_title($strplugincheck);
283 $PAGE->set_heading($strplugincheck);
284 $PAGE->set_cacheable(false);
286 if ($fetchupdates) {
287 // no sesskey support guaranteed here
288 available_update_checker::instance()->fetch();
289 redirect($PAGE->url);
292 $output = $PAGE->get_renderer('core', 'admin');
293 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
294 $version, $showallplugins,
295 new moodle_url($PAGE->url),
296 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
297 die();
300 // install/upgrade all plugins and other parts
301 upgrade_noncore(true);
304 // If this is the first install, indicate that this site is fully configured
305 // except the admin password
306 if (during_initial_install()) {
307 set_config('rolesactive', 1); // after this, during_initial_install will return false.
308 set_config('adminsetuppending', 1);
309 // we need this redirect to setup proper session
310 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
313 // make sure admin user is created - this is the last step because we need
314 // session to be working properly in order to edit admin account
315 if (!empty($CFG->adminsetuppending)) {
316 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
317 if (!$sessionstarted) {
318 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
319 } else {
320 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
321 if (!$sessionverify) {
322 $SESSION->sessionverify = 1;
323 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
324 } else {
325 if (empty($SESSION->sessionverify)) {
326 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
328 unset($SESSION->sessionverify);
332 // 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
333 $adminids = explode(',', $CFG->siteadmins);
334 $adminuser = get_complete_user_data('id', reset($adminids));
336 if ($adminuser->password === 'adminsetuppending') {
337 // prevent installation hijacking
338 if ($adminuser->lastip !== getremoteaddr()) {
339 print_error('installhijacked', 'admin');
341 // login user and let him set password and admin details
342 $adminuser->newadminuser = 1;
343 complete_user_login($adminuser);
344 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
346 } else {
347 unset_config('adminsetuppending');
350 } else {
351 // just make sure upgrade logging is properly terminated
352 upgrade_finished('upgradesettings.php');
355 // Turn xmlstrictheaders back on now.
356 $CFG->xmlstrictheaders = $origxmlstrictheaders;
357 unset($origxmlstrictheaders);
359 // Check for valid admin user - no guest autologin
360 require_login(0, false);
361 $context = get_context_instance(CONTEXT_SYSTEM);
362 require_capability('moodle/site:config', $context);
364 // check that site is properly customized
365 $site = get_site();
366 if (empty($site->shortname)) {
367 // probably new installation - lets return to frontpage after this step
368 // remove settings that we want uninitialised
369 unset_config('registerauth');
370 redirect('upgradesettings.php?return=site');
373 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
374 if (!empty($id) and $id == $CFG->siteidentifier) {
375 set_config('registered', time());
378 // setup critical warnings before printing admin tree block
379 $insecuredataroot = is_dataroot_insecure(true);
380 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
382 $adminroot = admin_get_root();
384 // Check if there are any new admin settings which have still yet to be set
385 if (any_new_admin_settings($adminroot)){
386 redirect('upgradesettings.php');
389 // Everything should now be set up, and the user is an admin
391 // Print default admin page with notifications.
392 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
394 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
395 $cronoverdue = ($lastcron < time() - 3600 * 24);
396 $dbproblems = $DB->diagnose();
397 $maintenancemode = !empty($CFG->maintenance_enabled);
399 $updateschecker = available_update_checker::instance();
400 $availableupdates = $updateschecker->get_update_info('core',
401 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
402 $availableupdatesfetch = $updateschecker->get_last_timefetched();
404 admin_externalpage_setup('adminnotifications');
406 if ($fetchupdates) {
407 require_sesskey();
408 $updateschecker->fetch();
409 redirect($PAGE->url);
412 $output = $PAGE->get_renderer('core', 'admin');
413 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
414 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch);