on demand release 2.5beta+
[moodle.git] / admin / index.php
CommitLineData
8580535b 1<?php
2
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.
14//
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/>.
17
18/**
19 * Main administration script.
20 *
d078f6d3 21 * @package core
8580535b 22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
f9903ed0 25
00be1916 26// Check that config.php exists, if not then call the install script
27if (!file_exists('../config.php')) {
28 header('Location: ../install.php');
3ba28588 29 die();
00be1916 30}
31
32// Check that PHP is of a sufficient version as soon as possible
0fd23b76 33if (version_compare(phpversion(), '5.3.3') < 0) {
00be1916 34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
0fd23b76 36 echo "Moodle 2.5 or later requires at least PHP 5.3.3 (currently using version $phpversion).<br />";
eab044a0 37 echo "Please upgrade your server software or install older Moodle version.";
3ba28588 38 die();
00be1916 39}
40
695940df
PS
41// make sure iconv is available and actually works
42if (!function_exists('iconv')) {
43 // this should not happen, this must be very borked install
7d85a4e2 44 echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
695940df
PS
45 die();
46}
695940df 47
cbad562e 48define('NO_OUTPUT_BUFFERING', true);
00be1916 49
50require('../config.php');
bc76b3c0 51require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
52require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
55585f3a 53require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
00be1916 54
55$id = optional_param('id', '', PARAM_TEXT);
56$confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
57$confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
58$confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
b9934a17 59$showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
00be1916 60$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
55585f3a 61$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
0a6a344d 62$newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW);
00be1916 63
64// Check some PHP server settings
65
0a6a344d
DM
66if (is_null($newaddonreq)) {
67 $PAGE->set_url('/admin/index.php');
68} else {
69 // We need to set the eventual add-on installation request in the $PAGE's URL
70 // so that it is stored in $SESSION->wantsurl and the admin is redirected
71 // correctly once they are logged-in.
72 $PAGE->set_url('/admin/index.php', array('installaddonrequest' => $newaddonreq));
73}
6d80fad4 74$PAGE->set_pagelayout('admin'); // Set a default pagelayout
ee73b1ff 75
00be1916 76$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
77
78if (ini_get_bool('session.auto_start')) {
79 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
80}
81
82if (ini_get_bool('magic_quotes_runtime')) {
83 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
84}
85
86if (!ini_get_bool('file_uploads')) {
87 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
88}
89
90if (is_float_problem()) {
91 print_error('phpfloatproblem', 'admin', '', $documentationlink);
92}
93
00be1916 94// Set some necessary variables during set-up to avoid PHP warnings later on this page
00be1916 95if (!isset($CFG->release)) {
bc76b3c0 96 $CFG->release = '';
00be1916 97}
98if (!isset($CFG->version)) {
bc76b3c0 99 $CFG->version = '';
00be1916 100}
ad394588
AB
101if (!isset($CFG->branch)) {
102 $CFG->branch = '';
103}
00be1916 104
105$version = null;
106$release = null;
ed01233a
AB
107$branch = null;
108require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
00be1916 109$CFG->target_release = $release; // used during installation and upgrades
110
111if (!$version or !$release) {
112 print_error('withoutversion', 'debug'); // without version, stop
113}
114
3316fe24 115if (!core_tables_exist()) {
78946b9b 116 $PAGE->set_pagelayout('maintenance');
d3875524 117 $PAGE->set_popup_notification_allowed(false);
00be1916 118
119 // fake some settings
120 $CFG->docroot = 'http://docs.moodle.org';
121
122 $strinstallation = get_string('installation', 'install');
123
124 // remove current session content completely
125 session_get_instance()->terminate_current();
126
127 if (empty($agreelicense)) {
128 $strlicense = get_string('license');
cc359566 129
69d77c23 130 $PAGE->navbar->add($strlicense);
131 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
132 $PAGE->set_heading($strinstallation);
133 $PAGE->set_cacheable(false);
cc359566 134
da2fdc3f 135 $output = $PAGE->get_renderer('core', 'admin');
cc359566 136 echo $output->install_licence_page();
3ba28588 137 die();
249ab745 138 }
00be1916 139 if (empty($confirmrelease)) {
cc359566
TH
140 require_once($CFG->libdir.'/environmentlib.php');
141 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
bc76b3c0 142 $strcurrentrelease = get_string('currentrelease');
cc359566 143
69d77c23 144 $PAGE->navbar->add($strcurrentrelease);
1a4d6a5b
DM
145 $PAGE->set_title($strinstallation);
146 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
69d77c23 147 $PAGE->set_cacheable(false);
249ab745 148
cc359566 149 $output = $PAGE->get_renderer('core', 'admin');
8d1da748 150 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
3ba28588 151 die();
1dd24519 152 }
153
39f15cc7
DM
154 // check plugin dependencies
155 $failed = array();
156 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
157 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
158 $PAGE->set_title($strinstallation);
159 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
160
161 $output = $PAGE->get_renderer('core', 'admin');
162 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
163 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
164 die();
165 }
166 unset($failed);
167
8d1da748
PS
168 //TODO: add a page with list of non-standard plugins here
169
bc76b3c0 170 $strdatabasesetup = get_string('databasesetup');
543f54d3 171 upgrade_init_javascript();
cc359566 172
69d77c23 173 $PAGE->navbar->add($strdatabasesetup);
174 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
2ea05146 175 $PAGE->set_heading($strinstallation);
69d77c23 176 $PAGE->set_cacheable(false);
cc359566 177
da2fdc3f
TH
178 $output = $PAGE->get_renderer('core', 'admin');
179 echo $output->header();
d02bc6ce 180
00be1916 181 if (!$DB->setup_is_unicodedb()) {
182 if (!$DB->change_db_encoding()) {
183 // If could not convert successfully, throw error, and prevent installation
184 print_error('unicoderequired', 'admin');
db5af934 185 }
db5af934 186 }
90509582 187
00be1916 188 install_core($version, true);
189}
db5af934 190
db5af934 191
00be1916 192// Check version of Moodle code on disk compared with database
193// and upgrade if possible.
db5af934 194
00be1916 195$stradministration = get_string('administration');
bf006d2c 196$PAGE->set_context(context_system::instance());
db5af934 197
00be1916 198if (empty($CFG->version)) {
199 print_error('missingconfigversion', 'debug');
200}
db5af934 201
00be1916 202if ($version > $CFG->version) { // upgrade
a95682b2 203 purge_all_caches();
33082590 204
78946b9b 205 $PAGE->set_pagelayout('maintenance');
d3875524 206 $PAGE->set_popup_notification_allowed(false);
d4a03c00 207
9008ec16
PS
208 if (upgrade_stale_php_files_present()) {
209 $PAGE->set_title($stradministration);
210 $PAGE->set_cacheable(false);
211
212 $output = $PAGE->get_renderer('core', 'admin');
213 echo $output->upgrade_stale_php_files_page();
214 die();
215 }
216
00be1916 217 if (empty($confirmupgrade)) {
17854cb9 218 $a = new stdClass();
c22a579b
RT
219 $a->oldversion = "$CFG->release (".sprintf('%.2f', $CFG->version).")";
220 $a->newversion = "$release (".sprintf('%.2f', $version).")";
cc359566
TH
221 $strdatabasechecking = get_string('databasechecking', '', $a);
222
1a4d6a5b
DM
223 $PAGE->set_title($stradministration);
224 $PAGE->set_heading($strdatabasechecking);
69d77c23 225 $PAGE->set_cacheable(false);
cc359566 226
da2fdc3f 227 $output = $PAGE->get_renderer('core', 'admin');
cc359566 228 echo $output->upgrade_confirm_page($a->newversion, $maturity);
3ba28588 229 die();
db5af934 230
00be1916 231 } else if (empty($confirmrelease)){
cc359566
TH
232 require_once($CFG->libdir.'/environmentlib.php');
233 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
bc76b3c0 234 $strcurrentrelease = get_string('currentrelease');
cc359566 235
69d77c23 236 $PAGE->navbar->add($strcurrentrelease);
237 $PAGE->set_title($strcurrentrelease);
52273536 238 $PAGE->set_heading($strcurrentrelease);
69d77c23 239 $PAGE->set_cacheable(false);
db5af934 240
cc359566 241 $output = $PAGE->get_renderer('core', 'admin');
faadd326
TH
242 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
243 die();
db5af934 244
cc359566 245 } else if (empty($confirmplugins)) {
00be1916 246 $strplugincheck = get_string('plugincheck');
cc359566 247
69d77c23 248 $PAGE->navbar->add($strplugincheck);
249 $PAGE->set_title($strplugincheck);
52273536 250 $PAGE->set_heading($strplugincheck);
69d77c23 251 $PAGE->set_cacheable(false);
b9934a17 252
96dd9237
DM
253 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
254
ead8ba3b
DM
255 // check plugin dependencies first
256 $failed = array();
257 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
258 $output = $PAGE->get_renderer('core', 'admin');
259 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
260 die();
261 }
262 unset($failed);
263
96dd9237 264 if ($fetchupdates) {
5a08e363 265 // no sesskey support guaranteed here
4db58f5f
DM
266 if (empty($CFG->disableupdatenotifications)) {
267 available_update_checker::instance()->fetch();
268 }
96dd9237
DM
269 redirect($reloadurl);
270 }
271
cc359566 272 $output = $PAGE->get_renderer('core', 'admin');
bcc8397a
DM
273
274 $deployer = available_update_deployer::instance();
275 if ($deployer->enabled()) {
276 $deployer->initialize($reloadurl, $reloadurl);
277
278 $deploydata = $deployer->submitted_data();
279 if (!empty($deploydata)) {
280 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
281 die();
282 }
283 }
284
96dd9237
DM
285 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
286 $version, $showallplugins, $reloadurl,
cc359566 287 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
00be1916 288 die();
db5af934 289
00be1916 290 } else {
291 // Launch main upgrade
292 upgrade_core($version, true);
88582df4 293 }
00be1916 294} else if ($version < $CFG->version) {
80380bd7
PS
295 // better stop here, we can not continue with plugin upgrades or anything else
296 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
00be1916 297}
298
299// Updated human-readable release version if necessary
300if ($release <> $CFG->release) { // Update the release version
bc76b3c0 301 set_config('release', $release);
00be1916 302}
303
ed01233a
AB
304if ($branch <> $CFG->branch) { // Update the branch
305 set_config('branch', $branch);
306}
307
575f245f
PS
308if (moodle_needs_upgrading()) {
309 if (!$PAGE->headerprinted) {
2361b7dd 310 // means core upgrade or installation was not already done
575f245f 311 if (!$confirmplugins) {
cc359566
TH
312 $strplugincheck = get_string('plugincheck');
313
2361b7dd 314 $PAGE->set_pagelayout('maintenance');
d3875524 315 $PAGE->set_popup_notification_allowed(false);
575f245f
PS
316 $PAGE->navbar->add($strplugincheck);
317 $PAGE->set_title($strplugincheck);
318 $PAGE->set_heading($strplugincheck);
319 $PAGE->set_cacheable(false);
b9934a17 320
96dd9237 321 if ($fetchupdates) {
5a08e363 322 // no sesskey support guaranteed here
96dd9237
DM
323 available_update_checker::instance()->fetch();
324 redirect($PAGE->url);
325 }
326
cc359566 327 $output = $PAGE->get_renderer('core', 'admin');
ead8ba3b 328
292dbeac
DM
329 $deployer = available_update_deployer::instance();
330 if ($deployer->enabled()) {
331 $deployer->initialize($PAGE->url, $PAGE->url);
332
333 $deploydata = $deployer->submitted_data();
334 if (!empty($deploydata)) {
335 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
336 die();
337 }
338 }
339
ead8ba3b
DM
340 // check plugin dependencies first
341 $failed = array();
342 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
343 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
344 die();
345 }
346 unset($failed);
347
348 // dependencies check passed, let's rock!
96dd9237
DM
349 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
350 $version, $showallplugins,
351 new moodle_url($PAGE->url),
cc359566 352 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
575f245f
PS
353 die();
354 }
355 }
356 // install/upgrade all plugins and other parts
357 upgrade_noncore(true);
358}
00be1916 359
31a99877 360// If this is the first install, indicate that this site is fully configured
361// except the admin password
362if (during_initial_install()) {
363 set_config('rolesactive', 1); // after this, during_initial_install will return false.
00be1916 364 set_config('adminsetuppending', 1);
c8ae55e9 365 // we need this redirect to setup proper session
00be1916 366 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
367}
368
369// make sure admin user is created - this is the last step because we need
370// session to be working properly in order to edit admin account
371 if (!empty($CFG->adminsetuppending)) {
372 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
373 if (!$sessionstarted) {
374 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
375 } else {
376 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
377 if (!$sessionverify) {
378 $SESSION->sessionverify = 1;
379 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
88582df4 380 } else {
00be1916 381 if (empty($SESSION->sessionverify)) {
382 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
88582df4 383 }
00be1916 384 unset($SESSION->sessionverify);
88582df4 385 }
00be1916 386 }
88582df4 387
4f73591a
PS
388 // Cleanup SESSION to make sure other code does not complain in the future.
389 unset($SESSION->has_timed_out);
390 unset($SESSION->wantsurl);
391
a7c9609b 392 // 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
f20edd52
PS
393 $adminids = explode(',', $CFG->siteadmins);
394 $adminuser = get_complete_user_data('id', reset($adminids));
88582df4 395
00be1916 396 if ($adminuser->password === 'adminsetuppending') {
397 // prevent installation hijacking
398 if ($adminuser->lastip !== getremoteaddr()) {
399 print_error('installhijacked', 'admin');
35d6a2a4 400 }
00be1916 401 // login user and let him set password and admin details
402 $adminuser->newadminuser = 1;
0342fc36 403 complete_user_login($adminuser);
00be1916 404 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
db5af934 405
5c144d60 406 } else {
00be1916 407 unset_config('adminsetuppending');
4da1a0a1 408 }
409
00be1916 410} else {
411 // just make sure upgrade logging is properly terminated
412 upgrade_finished('upgradesettings.php');
413}
b3732604 414
00be1916 415// Check for valid admin user - no guest autologin
416require_login(0, false);
bf006d2c 417$context = context_system::instance();
00be1916 418require_capability('moodle/site:config', $context);
419
420// check that site is properly customized
421$site = get_site();
422if (empty($site->shortname)) {
423 // probably new installation - lets return to frontpage after this step
424 // remove settings that we want uninitialised
425 unset_config('registerauth');
426 redirect('upgradesettings.php?return=site');
427}
428
429// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
430if (!empty($id) and $id == $CFG->siteidentifier) {
431 set_config('registered', time());
432}
433
0a6a344d
DM
434// Check if we are returning from an add-on installation request at moodle.org/plugins
435if (!is_null($newaddonreq)) {
436 if (!empty($CFG->disableonclickaddoninstall)) {
437 // The feature is disabled in config.php, ignore the request.
438 } else {
439 redirect(new moodle_url('/admin/tool/installaddon/index.php', array(
440 'installaddonrequest' => $newaddonreq,
441 'confirm' => 0)));
442 }
443}
444
00be1916 445// setup critical warnings before printing admin tree block
bc76b3c0 446$insecuredataroot = is_dataroot_insecure(true);
00be1916 447$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
448
449$adminroot = admin_get_root();
450
451// Check if there are any new admin settings which have still yet to be set
452if (any_new_admin_settings($adminroot)){
453 redirect('upgradesettings.php');
454}
455
456// Everything should now be set up, and the user is an admin
457
458// Print default admin page with notifications.
cc359566 459$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
a95682b2 460
00be1916 461$lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
cc359566
TH
462$cronoverdue = ($lastcron < time() - 3600 * 24);
463$dbproblems = $DB->diagnose();
464$maintenancemode = !empty($CFG->maintenance_enabled);
74944b73 465
966bd785 466// Available updates for Moodle core
55585f3a 467$updateschecker = available_update_checker::instance();
966bd785
DM
468$availableupdates = array();
469$availableupdates['core'] = $updateschecker->get_update_info('core',
c6f008e7 470 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
966bd785
DM
471
472// Available updates for contributed plugins
473$pluginman = plugin_manager::instance();
474foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
475 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
476 if (!empty($plugininfo->availableupdates)) {
477 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
478 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
479 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
480 $availableupdates[$plugintype.'_'.$pluginname] = array();
481 }
482 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
483 }
484 }
485 }
486 }
487}
488
489// The timestamp of the most recent check for available updates
55585f3a
DM
490$availableupdatesfetch = $updateschecker->get_last_timefetched();
491
0aff15c2 492$buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
b3245b75
DP
493//check if the site is registered on Moodle.org
494$registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
0aff15c2 495
cc359566 496admin_externalpage_setup('adminnotifications');
55585f3a
DM
497
498if ($fetchupdates) {
499 require_sesskey();
500 $updateschecker->fetch();
501 redirect($PAGE->url);
502}
503
cc359566
TH
504$output = $PAGE->get_renderer('core', 'admin');
505echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
b3245b75
DP
506 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
507 $registered);