Merge branch 'MDL-32638-workshop-files' of git://github.com/mudrd8mz/moodle
[moodle.git] / admin / index.php
blob3f8ab67260c40e53b1b4649ba8c6989f4cc5a3ba
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 = '';
99 $version = null;
100 $release = null;
101 require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
102 $CFG->target_release = $release; // used during installation and upgrades
104 if (!$version or !$release) {
105 print_error('withoutversion', 'debug'); // without version, stop
108 // Turn off xmlstrictheaders during upgrade.
109 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
110 $CFG->xmlstrictheaders = false;
112 if (!core_tables_exist()) {
113 $PAGE->set_pagelayout('maintenance');
114 $PAGE->set_popup_notification_allowed(false);
116 // fake some settings
117 $CFG->docroot = 'http://docs.moodle.org';
119 $strinstallation = get_string('installation', 'install');
121 // remove current session content completely
122 session_get_instance()->terminate_current();
124 if (empty($agreelicense)) {
125 $strlicense = get_string('license');
127 $PAGE->navbar->add($strlicense);
128 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
129 $PAGE->set_heading($strinstallation);
130 $PAGE->set_cacheable(false);
132 $output = $PAGE->get_renderer('core', 'admin');
133 echo $output->install_licence_page();
134 die();
136 if (empty($confirmrelease)) {
137 require_once($CFG->libdir.'/environmentlib.php');
138 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
139 $strcurrentrelease = get_string('currentrelease');
141 $PAGE->navbar->add($strcurrentrelease);
142 $PAGE->set_title($strinstallation);
143 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
144 $PAGE->set_cacheable(false);
146 $output = $PAGE->get_renderer('core', 'admin');
147 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
148 die();
151 //TODO: add a page with list of non-standard plugins here
153 $strdatabasesetup = get_string('databasesetup');
154 upgrade_init_javascript();
156 $PAGE->navbar->add($strdatabasesetup);
157 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
158 $PAGE->set_heading($strinstallation);
159 $PAGE->set_cacheable(false);
161 $output = $PAGE->get_renderer('core', 'admin');
162 echo $output->header();
164 if (!$DB->setup_is_unicodedb()) {
165 if (!$DB->change_db_encoding()) {
166 // If could not convert successfully, throw error, and prevent installation
167 print_error('unicoderequired', 'admin');
171 install_core($version, true);
175 // Check version of Moodle code on disk compared with database
176 // and upgrade if possible.
178 $stradministration = get_string('administration');
179 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
181 if (empty($CFG->version)) {
182 print_error('missingconfigversion', 'debug');
185 if ($version > $CFG->version) { // upgrade
186 purge_all_caches();
187 $PAGE->set_pagelayout('maintenance');
188 $PAGE->set_popup_notification_allowed(false);
190 if (upgrade_stale_php_files_present()) {
191 $PAGE->set_title($stradministration);
192 $PAGE->set_cacheable(false);
194 $output = $PAGE->get_renderer('core', 'admin');
195 echo $output->upgrade_stale_php_files_page();
196 die();
199 if (empty($confirmupgrade)) {
200 $a = new stdClass();
201 $a->oldversion = "$CFG->release ($CFG->version)";
202 $a->newversion = "$release ($version)";
203 $strdatabasechecking = get_string('databasechecking', '', $a);
205 $PAGE->set_title($stradministration);
206 $PAGE->set_heading($strdatabasechecking);
207 $PAGE->set_cacheable(false);
209 $output = $PAGE->get_renderer('core', 'admin');
210 echo $output->upgrade_confirm_page($a->newversion, $maturity);
211 die();
213 } else if (empty($confirmrelease)){
214 require_once($CFG->libdir.'/environmentlib.php');
215 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
216 $strcurrentrelease = get_string('currentrelease');
218 $PAGE->navbar->add($strcurrentrelease);
219 $PAGE->set_title($strcurrentrelease);
220 $PAGE->set_heading($strcurrentrelease);
221 $PAGE->set_cacheable(false);
223 $output = $PAGE->get_renderer('core', 'admin');
224 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
225 die();
227 } else if (empty($confirmplugins)) {
228 $strplugincheck = get_string('plugincheck');
230 $PAGE->navbar->add($strplugincheck);
231 $PAGE->set_title($strplugincheck);
232 $PAGE->set_heading($strplugincheck);
233 $PAGE->set_cacheable(false);
235 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
237 if ($fetchupdates) {
238 // no sesskey support guaranteed here
239 available_update_checker::instance()->fetch();
240 redirect($reloadurl);
243 $output = $PAGE->get_renderer('core', 'admin');
244 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
245 $version, $showallplugins, $reloadurl,
246 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
247 die();
249 } else {
250 // Launch main upgrade
251 upgrade_core($version, true);
253 } else if ($version < $CFG->version) {
254 // better stop here, we can not continue with plugin upgrades or anything else
255 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
258 // Updated human-readable release version if necessary
259 if ($release <> $CFG->release) { // Update the release version
260 set_config('release', $release);
263 if (moodle_needs_upgrading()) {
264 if (!$PAGE->headerprinted) {
265 // means core upgrade or installation was not already done
266 if (!$confirmplugins) {
267 $strplugincheck = get_string('plugincheck');
269 $PAGE->set_pagelayout('maintenance');
270 $PAGE->set_popup_notification_allowed(false);
271 $PAGE->navbar->add($strplugincheck);
272 $PAGE->set_title($strplugincheck);
273 $PAGE->set_heading($strplugincheck);
274 $PAGE->set_cacheable(false);
276 if ($fetchupdates) {
277 // no sesskey support guaranteed here
278 available_update_checker::instance()->fetch();
279 redirect($PAGE->url);
282 $output = $PAGE->get_renderer('core', 'admin');
283 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
284 $version, $showallplugins,
285 new moodle_url($PAGE->url),
286 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
287 die();
290 // install/upgrade all plugins and other parts
291 upgrade_noncore(true);
294 // If this is the first install, indicate that this site is fully configured
295 // except the admin password
296 if (during_initial_install()) {
297 set_config('rolesactive', 1); // after this, during_initial_install will return false.
298 set_config('adminsetuppending', 1);
299 // we need this redirect to setup proper session
300 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
303 // make sure admin user is created - this is the last step because we need
304 // session to be working properly in order to edit admin account
305 if (!empty($CFG->adminsetuppending)) {
306 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
307 if (!$sessionstarted) {
308 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
309 } else {
310 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
311 if (!$sessionverify) {
312 $SESSION->sessionverify = 1;
313 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
314 } else {
315 if (empty($SESSION->sessionverify)) {
316 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
318 unset($SESSION->sessionverify);
322 // 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
323 $adminids = explode(',', $CFG->siteadmins);
324 $adminuser = get_complete_user_data('id', reset($adminids));
326 if ($adminuser->password === 'adminsetuppending') {
327 // prevent installation hijacking
328 if ($adminuser->lastip !== getremoteaddr()) {
329 print_error('installhijacked', 'admin');
331 // login user and let him set password and admin details
332 $adminuser->newadminuser = 1;
333 complete_user_login($adminuser);
334 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
336 } else {
337 unset_config('adminsetuppending');
340 } else {
341 // just make sure upgrade logging is properly terminated
342 upgrade_finished('upgradesettings.php');
345 // Turn xmlstrictheaders back on now.
346 $CFG->xmlstrictheaders = $origxmlstrictheaders;
347 unset($origxmlstrictheaders);
349 // Check for valid admin user - no guest autologin
350 require_login(0, false);
351 $context = get_context_instance(CONTEXT_SYSTEM);
352 require_capability('moodle/site:config', $context);
354 // check that site is properly customized
355 $site = get_site();
356 if (empty($site->shortname)) {
357 // probably new installation - lets return to frontpage after this step
358 // remove settings that we want uninitialised
359 unset_config('registerauth');
360 redirect('upgradesettings.php?return=site');
363 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
364 if (!empty($id) and $id == $CFG->siteidentifier) {
365 set_config('registered', time());
368 // setup critical warnings before printing admin tree block
369 $insecuredataroot = is_dataroot_insecure(true);
370 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
372 $adminroot = admin_get_root();
374 // Check if there are any new admin settings which have still yet to be set
375 if (any_new_admin_settings($adminroot)){
376 redirect('upgradesettings.php');
379 // Everything should now be set up, and the user is an admin
381 // Print default admin page with notifications.
382 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
384 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
385 $cronoverdue = ($lastcron < time() - 3600 * 24);
386 $dbproblems = $DB->diagnose();
387 $maintenancemode = !empty($CFG->maintenance_enabled);
389 $updateschecker = available_update_checker::instance();
390 $availableupdates = $updateschecker->get_update_info('core',
391 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
392 $availableupdatesfetch = $updateschecker->get_last_timefetched();
394 admin_externalpage_setup('adminnotifications');
396 if ($fetchupdates) {
397 require_sesskey();
398 $updateschecker->fetch();
399 redirect($PAGE->url);
402 $output = $PAGE->get_renderer('core', 'admin');
403 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
404 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch);