3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Main administration script.
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');
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.";
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.';
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.';
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
)) {
93 if (!isset($CFG->version
)) {
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();
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);
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
190 $PAGE->set_pagelayout('maintenance');
191 $PAGE->set_popup_notification_allowed(false);
193 if (empty($confirmupgrade)) {
194 $a->oldversion
= "$CFG->release ($CFG->version)";
195 $a->newversion
= "$release ($version)";
196 $strdatabasechecking = get_string('databasechecking', '', $a);
198 $PAGE->set_title($stradministration);
199 $PAGE->set_heading($strdatabasechecking);
200 $PAGE->set_cacheable(false);
202 $output = $PAGE->get_renderer('core', 'admin');
203 echo $output->upgrade_confirm_page($a->newversion
, $maturity);
206 } else if (empty($confirmrelease)){
207 require_once($CFG->libdir
.'/environmentlib.php');
208 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE
);
209 $strcurrentrelease = get_string('currentrelease');
211 $PAGE->navbar
->add($strcurrentrelease);
212 $PAGE->set_title($strcurrentrelease);
213 $PAGE->set_heading($strcurrentrelease);
214 $PAGE->set_cacheable(false);
216 $output = $PAGE->get_renderer('core', 'admin');
217 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
220 } else if (empty($confirmplugins)) {
221 $strplugincheck = get_string('plugincheck');
223 $PAGE->navbar
->add($strplugincheck);
224 $PAGE->set_title($strplugincheck);
225 $PAGE->set_heading($strplugincheck);
226 $PAGE->set_cacheable(false);
228 $output = $PAGE->get_renderer('core', 'admin');
229 echo $output->upgrade_plugin_check_page(plugin_manager
::instance(), $version, $showallplugins,
230 new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1)),
231 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
235 // Launch main upgrade
236 upgrade_core($version, true);
238 } else if ($version < $CFG->version
) {
239 // better stop here, we can not continue with plugin upgrades or anything else
240 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
243 // Updated human-readable release version if necessary
244 if ($release <> $CFG->release
) { // Update the release version
245 set_config('release', $release);
248 if (moodle_needs_upgrading()) {
249 if (!$PAGE->headerprinted
) {
250 // means core upgrade or installation was not already done
251 if (!$confirmplugins) {
252 $strplugincheck = get_string('plugincheck');
254 $PAGE->set_pagelayout('maintenance');
255 $PAGE->set_popup_notification_allowed(false);
256 $PAGE->navbar
->add($strplugincheck);
257 $PAGE->set_title($strplugincheck);
258 $PAGE->set_heading($strplugincheck);
259 $PAGE->set_cacheable(false);
261 $output = $PAGE->get_renderer('core', 'admin');
262 echo $output->upgrade_plugin_check_page(plugin_manager
::instance(), $version, $showallplugins,
263 new moodle_url('/admin/index.php'),
264 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
268 // install/upgrade all plugins and other parts
269 upgrade_noncore(true);
272 // If this is the first install, indicate that this site is fully configured
273 // except the admin password
274 if (during_initial_install()) {
275 set_config('rolesactive', 1); // after this, during_initial_install will return false.
276 set_config('adminsetuppending', 1);
277 // we need this redirect to setup proper session
278 upgrade_finished("index.php?sessionstarted=1&lang=$CFG->lang");
281 // make sure admin user is created - this is the last step because we need
282 // session to be working properly in order to edit admin account
283 if (!empty($CFG->adminsetuppending
)) {
284 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL
);
285 if (!$sessionstarted) {
286 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
288 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL
);
289 if (!$sessionverify) {
290 $SESSION->sessionverify
= 1;
291 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
293 if (empty($SESSION->sessionverify
)) {
294 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
296 unset($SESSION->sessionverify
);
300 // 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
301 $adminuser = get_complete_user_data('id', reset(explode(',', $CFG->siteadmins
)));
303 if ($adminuser->password
=== 'adminsetuppending') {
304 // prevent installation hijacking
305 if ($adminuser->lastip
!== getremoteaddr()) {
306 print_error('installhijacked', 'admin');
308 // login user and let him set password and admin details
309 $adminuser->newadminuser
= 1;
310 complete_user_login($adminuser);
311 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
314 unset_config('adminsetuppending');
318 // just make sure upgrade logging is properly terminated
319 upgrade_finished('upgradesettings.php');
322 // Turn xmlstrictheaders back on now.
323 $CFG->xmlstrictheaders
= $origxmlstrictheaders;
324 unset($origxmlstrictheaders);
326 // Check for valid admin user - no guest autologin
327 require_login(0, false);
328 $context = get_context_instance(CONTEXT_SYSTEM
);
329 require_capability('moodle/site:config', $context);
331 // check that site is properly customized
333 if (empty($site->shortname
)) {
334 // probably new installation - lets return to frontpage after this step
335 // remove settings that we want uninitialised
336 unset_config('registerauth');
337 redirect('upgradesettings.php?return=site');
340 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
341 if (!empty($id) and $id == $CFG->siteidentifier
) {
342 set_config('registered', time());
345 // setup critical warnings before printing admin tree block
346 $insecuredataroot = is_dataroot_insecure(true);
347 $SESSION->admin_critical_warning
= ($insecuredataroot==INSECURE_DATAROOT_ERROR
);
349 $adminroot = admin_get_root();
351 // Check if there are any new admin settings which have still yet to be set
352 if (any_new_admin_settings($adminroot)){
353 redirect('upgradesettings.php');
356 // Everything should now be set up, and the user is an admin
358 // Print default admin page with notifications.
359 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
361 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
362 $cronoverdue = ($lastcron < time() - 3600 * 24);
363 $dbproblems = $DB->diagnose();
364 $maintenancemode = !empty($CFG->maintenance_enabled
);
366 admin_externalpage_setup('adminnotifications');
367 $output = $PAGE->get_renderer('core', 'admin');
368 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
369 $cronoverdue, $dbproblems, $maintenancemode);