MDL-28283-master Completion Future dates can now be set as completion criteria
[moodle.git] / admin / index.php
blob7e8a6fc0f4055a05747d4569aebdc4f4e32148fe
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 if (!isset($maturity)) {
107 // Fallback for now. Should probably be removed in the future.
108 $maturity = MATURITY_STABLE;
111 // Turn off xmlstrictheaders during upgrade.
112 $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
113 $CFG->xmlstrictheaders = false;
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();
137 die();
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);
151 die();
154 //TODO: add a page with list of non-standard plugins here
156 $strdatabasesetup = get_string('databasesetup');
157 upgrade_init_javascript();
159 $PAGE->navbar->add($strdatabasesetup);
160 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
161 $PAGE->set_heading($strinstallation);
162 $PAGE->set_cacheable(false);
164 $output = $PAGE->get_renderer('core', 'admin');
165 echo $output->header();
167 if (!$DB->setup_is_unicodedb()) {
168 if (!$DB->change_db_encoding()) {
169 // If could not convert successfully, throw error, and prevent installation
170 print_error('unicoderequired', 'admin');
174 install_core($version, true);
178 // Check version of Moodle code on disk compared with database
179 // and upgrade if possible.
181 $stradministration = get_string('administration');
182 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
184 if (empty($CFG->version)) {
185 print_error('missingconfigversion', 'debug');
188 if ($version > $CFG->version) { // upgrade
189 purge_all_caches();
190 $PAGE->set_pagelayout('maintenance');
191 $PAGE->set_popup_notification_allowed(false);
193 if (upgrade_stale_php_files_present()) {
194 $PAGE->set_title($stradministration);
195 $PAGE->set_cacheable(false);
197 $output = $PAGE->get_renderer('core', 'admin');
198 echo $output->upgrade_stale_php_files_page();
199 die();
202 if (empty($confirmupgrade)) {
203 $a->oldversion = "$CFG->release ($CFG->version)";
204 $a->newversion = "$release ($version)";
205 $strdatabasechecking = get_string('databasechecking', '', $a);
207 $PAGE->set_title($stradministration);
208 $PAGE->set_heading($strdatabasechecking);
209 $PAGE->set_cacheable(false);
211 $output = $PAGE->get_renderer('core', 'admin');
212 echo $output->upgrade_confirm_page($a->newversion, $maturity);
213 die();
215 } else if (empty($confirmrelease)){
216 require_once($CFG->libdir.'/environmentlib.php');
217 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
218 $strcurrentrelease = get_string('currentrelease');
220 $PAGE->navbar->add($strcurrentrelease);
221 $PAGE->set_title($strcurrentrelease);
222 $PAGE->set_heading($strcurrentrelease);
223 $PAGE->set_cacheable(false);
225 $output = $PAGE->get_renderer('core', 'admin');
226 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
227 die();
229 } else if (empty($confirmplugins)) {
230 $strplugincheck = get_string('plugincheck');
232 $PAGE->navbar->add($strplugincheck);
233 $PAGE->set_title($strplugincheck);
234 $PAGE->set_heading($strplugincheck);
235 $PAGE->set_cacheable(false);
237 $output = $PAGE->get_renderer('core', 'admin');
238 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), $version, $showallplugins,
239 new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1)),
240 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
241 die();
243 } else {
244 // Launch main upgrade
245 upgrade_core($version, true);
247 } else if ($version < $CFG->version) {
248 // better stop here, we can not continue with plugin upgrades or anything else
249 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
252 // Updated human-readable release version if necessary
253 if ($release <> $CFG->release) { // Update the release version
254 set_config('release', $release);
257 if (moodle_needs_upgrading()) {
258 if (!$PAGE->headerprinted) {
259 // means core upgrade or installation was not already done
260 if (!$confirmplugins) {
261 $strplugincheck = get_string('plugincheck');
263 $PAGE->set_pagelayout('maintenance');
264 $PAGE->set_popup_notification_allowed(false);
265 $PAGE->navbar->add($strplugincheck);
266 $PAGE->set_title($strplugincheck);
267 $PAGE->set_heading($strplugincheck);
268 $PAGE->set_cacheable(false);
270 $output = $PAGE->get_renderer('core', 'admin');
271 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), $version, $showallplugins,
272 new moodle_url('/admin/index.php'),
273 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
274 die();
277 // install/upgrade all plugins and other parts
278 upgrade_noncore(true);
281 // If this is the first install, indicate that this site is fully configured
282 // except the admin password
283 if (during_initial_install()) {
284 set_config('rolesactive', 1); // after this, during_initial_install will return false.
285 set_config('adminsetuppending', 1);
286 // we need this redirect to setup proper session
287 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
290 // make sure admin user is created - this is the last step because we need
291 // session to be working properly in order to edit admin account
292 if (!empty($CFG->adminsetuppending)) {
293 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
294 if (!$sessionstarted) {
295 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
296 } else {
297 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
298 if (!$sessionverify) {
299 $SESSION->sessionverify = 1;
300 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
301 } else {
302 if (empty($SESSION->sessionverify)) {
303 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
305 unset($SESSION->sessionverify);
309 // at this stage there can be only one admin - users may change username, so do not rely on that
310 $adminuser = get_complete_user_data('id', $CFG->siteadmins);
312 if ($adminuser->password === 'adminsetuppending') {
313 // prevent installation hijacking
314 if ($adminuser->lastip !== getremoteaddr()) {
315 print_error('installhijacked', 'admin');
317 // login user and let him set password and admin details
318 $adminuser->newadminuser = 1;
319 complete_user_login($adminuser);
320 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
322 } else {
323 unset_config('adminsetuppending');
326 } else {
327 // just make sure upgrade logging is properly terminated
328 upgrade_finished('upgradesettings.php');
331 // Turn xmlstrictheaders back on now.
332 $CFG->xmlstrictheaders = $origxmlstrictheaders;
333 unset($origxmlstrictheaders);
335 // Check for valid admin user - no guest autologin
336 require_login(0, false);
337 $context = get_context_instance(CONTEXT_SYSTEM);
338 require_capability('moodle/site:config', $context);
340 // check that site is properly customized
341 $site = get_site();
342 if (empty($site->shortname)) {
343 // probably new installation - lets return to frontpage after this step
344 // remove settings that we want uninitialised
345 unset_config('registerauth');
346 redirect('upgradesettings.php?return=site');
349 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
350 if (!empty($id) and $id == $CFG->siteidentifier) {
351 set_config('registered', time());
354 // setup critical warnings before printing admin tree block
355 $insecuredataroot = is_dataroot_insecure(true);
356 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
358 $adminroot = admin_get_root();
360 // Check if there are any new admin settings which have still yet to be set
361 if (any_new_admin_settings($adminroot)){
362 redirect('upgradesettings.php');
365 // Everything should now be set up, and the user is an admin
367 // Print default admin page with notifications.
368 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
370 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
371 $cronoverdue = ($lastcron < time() - 3600 * 24);
372 $dbproblems = $DB->diagnose();
373 $maintenancemode = !empty($CFG->maintenance_enabled);
375 admin_externalpage_setup('adminnotifications');
376 $output = $PAGE->get_renderer('core', 'admin');
377 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
378 $cronoverdue, $dbproblems, $maintenancemode);