MDL-32329 check for plugin dependencies during the pre-install checking
[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
eab044a0 33if (version_compare(phpversion(), '5.3.2') < 0) {
00be1916 34 $phpversion = phpversion();
35 // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
eab044a0
PS
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.";
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}
47if (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.';
3ba28588 50 die();
695940df
PS
51}
52
cbad562e 53define('NO_OUTPUT_BUFFERING', true);
00be1916 54
55require('../config.php');
bc76b3c0 56require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
57require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
55585f3a 58require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
00be1916 59
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);
b9934a17 64$showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
00be1916 65$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
55585f3a 66$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
00be1916 67
68// Check some PHP server settings
69
a6855934 70$PAGE->set_url('/admin/index.php');
6d80fad4 71$PAGE->set_pagelayout('admin'); // Set a default pagelayout
ee73b1ff 72
00be1916 73$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
74
75if (ini_get_bool('session.auto_start')) {
76 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
77}
78
79if (ini_get_bool('magic_quotes_runtime')) {
80 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
81}
82
83if (!ini_get_bool('file_uploads')) {
84 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
85}
86
87if (is_float_problem()) {
88 print_error('phpfloatproblem', 'admin', '', $documentationlink);
89}
90
00be1916 91// Set some necessary variables during set-up to avoid PHP warnings later on this page
00be1916 92if (!isset($CFG->release)) {
bc76b3c0 93 $CFG->release = '';
00be1916 94}
95if (!isset($CFG->version)) {
bc76b3c0 96 $CFG->version = '';
00be1916 97}
ad394588
AB
98if (!isset($CFG->branch)) {
99 $CFG->branch = '';
100}
00be1916 101
102$version = null;
103$release = null;
ed01233a
AB
104$branch = null;
105require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
00be1916 106$CFG->target_release = $release; // used during installation and upgrades
107
108if (!$version or !$release) {
109 print_error('withoutversion', 'debug'); // without version, stop
110}
111
00be1916 112// Turn off xmlstrictheaders during upgrade.
113$origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
114$CFG->xmlstrictheaders = false;
115
3316fe24 116if (!core_tables_exist()) {
78946b9b 117 $PAGE->set_pagelayout('maintenance');
d3875524 118 $PAGE->set_popup_notification_allowed(false);
00be1916 119
120 // fake some settings
121 $CFG->docroot = 'http://docs.moodle.org';
122
123 $strinstallation = get_string('installation', 'install');
124
125 // remove current session content completely
126 session_get_instance()->terminate_current();
127
128 if (empty($agreelicense)) {
129 $strlicense = get_string('license');
cc359566 130
69d77c23 131 $PAGE->navbar->add($strlicense);
132 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
133 $PAGE->set_heading($strinstallation);
134 $PAGE->set_cacheable(false);
cc359566 135
da2fdc3f 136 $output = $PAGE->get_renderer('core', 'admin');
cc359566 137 echo $output->install_licence_page();
3ba28588 138 die();
249ab745 139 }
00be1916 140 if (empty($confirmrelease)) {
cc359566
TH
141 require_once($CFG->libdir.'/environmentlib.php');
142 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
bc76b3c0 143 $strcurrentrelease = get_string('currentrelease');
cc359566 144
69d77c23 145 $PAGE->navbar->add($strcurrentrelease);
1a4d6a5b
DM
146 $PAGE->set_title($strinstallation);
147 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
69d77c23 148 $PAGE->set_cacheable(false);
249ab745 149
cc359566 150 $output = $PAGE->get_renderer('core', 'admin');
8d1da748 151 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
3ba28588 152 die();
1dd24519 153 }
154
39f15cc7
DM
155 // check plugin dependencies
156 $failed = array();
157 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
158 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
159 $PAGE->set_title($strinstallation);
160 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
161
162 $output = $PAGE->get_renderer('core', 'admin');
163 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
164 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
165 die();
166 }
167 unset($failed);
168
8d1da748
PS
169 //TODO: add a page with list of non-standard plugins here
170
bc76b3c0 171 $strdatabasesetup = get_string('databasesetup');
543f54d3 172 upgrade_init_javascript();
cc359566 173
69d77c23 174 $PAGE->navbar->add($strdatabasesetup);
175 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
2ea05146 176 $PAGE->set_heading($strinstallation);
69d77c23 177 $PAGE->set_cacheable(false);
cc359566 178
da2fdc3f
TH
179 $output = $PAGE->get_renderer('core', 'admin');
180 echo $output->header();
d02bc6ce 181
00be1916 182 if (!$DB->setup_is_unicodedb()) {
183 if (!$DB->change_db_encoding()) {
184 // If could not convert successfully, throw error, and prevent installation
185 print_error('unicoderequired', 'admin');
db5af934 186 }
db5af934 187 }
90509582 188
00be1916 189 install_core($version, true);
190}
db5af934 191
db5af934 192
00be1916 193// Check version of Moodle code on disk compared with database
194// and upgrade if possible.
db5af934 195
00be1916 196$stradministration = get_string('administration');
197$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
db5af934 198
00be1916 199if (empty($CFG->version)) {
200 print_error('missingconfigversion', 'debug');
201}
db5af934 202
00be1916 203if ($version > $CFG->version) { // upgrade
a95682b2 204 purge_all_caches();
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();
cc359566
TH
219 $a->oldversion = "$CFG->release ($CFG->version)";
220 $a->newversion = "$release ($version)";
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
255 if ($fetchupdates) {
5a08e363 256 // no sesskey support guaranteed here
4db58f5f
DM
257 if (empty($CFG->disableupdatenotifications)) {
258 available_update_checker::instance()->fetch();
259 }
96dd9237
DM
260 redirect($reloadurl);
261 }
262
cc359566 263 $output = $PAGE->get_renderer('core', 'admin');
96dd9237
DM
264 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
265 $version, $showallplugins, $reloadurl,
cc359566 266 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
00be1916 267 die();
db5af934 268
00be1916 269 } else {
270 // Launch main upgrade
271 upgrade_core($version, true);
88582df4 272 }
00be1916 273} else if ($version < $CFG->version) {
80380bd7
PS
274 // better stop here, we can not continue with plugin upgrades or anything else
275 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
00be1916 276}
277
278// Updated human-readable release version if necessary
279if ($release <> $CFG->release) { // Update the release version
bc76b3c0 280 set_config('release', $release);
00be1916 281}
282
ed01233a
AB
283if ($branch <> $CFG->branch) { // Update the branch
284 set_config('branch', $branch);
285}
286
575f245f
PS
287if (moodle_needs_upgrading()) {
288 if (!$PAGE->headerprinted) {
2361b7dd 289 // means core upgrade or installation was not already done
575f245f 290 if (!$confirmplugins) {
cc359566
TH
291 $strplugincheck = get_string('plugincheck');
292
2361b7dd 293 $PAGE->set_pagelayout('maintenance');
d3875524 294 $PAGE->set_popup_notification_allowed(false);
575f245f
PS
295 $PAGE->navbar->add($strplugincheck);
296 $PAGE->set_title($strplugincheck);
297 $PAGE->set_heading($strplugincheck);
298 $PAGE->set_cacheable(false);
b9934a17 299
96dd9237 300 if ($fetchupdates) {
5a08e363 301 // no sesskey support guaranteed here
96dd9237
DM
302 available_update_checker::instance()->fetch();
303 redirect($PAGE->url);
304 }
305
cc359566 306 $output = $PAGE->get_renderer('core', 'admin');
96dd9237
DM
307 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
308 $version, $showallplugins,
309 new moodle_url($PAGE->url),
cc359566 310 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
575f245f
PS
311 die();
312 }
313 }
314 // install/upgrade all plugins and other parts
315 upgrade_noncore(true);
316}
00be1916 317
31a99877 318// If this is the first install, indicate that this site is fully configured
319// except the admin password
320if (during_initial_install()) {
321 set_config('rolesactive', 1); // after this, during_initial_install will return false.
00be1916 322 set_config('adminsetuppending', 1);
c8ae55e9 323 // we need this redirect to setup proper session
00be1916 324 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
325}
326
327// make sure admin user is created - this is the last step because we need
328// session to be working properly in order to edit admin account
329 if (!empty($CFG->adminsetuppending)) {
330 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
331 if (!$sessionstarted) {
332 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
333 } else {
334 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
335 if (!$sessionverify) {
336 $SESSION->sessionverify = 1;
337 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
88582df4 338 } else {
00be1916 339 if (empty($SESSION->sessionverify)) {
340 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
88582df4 341 }
00be1916 342 unset($SESSION->sessionverify);
88582df4 343 }
00be1916 344 }
88582df4 345
a7c9609b 346 // 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
347 $adminids = explode(',', $CFG->siteadmins);
348 $adminuser = get_complete_user_data('id', reset($adminids));
88582df4 349
00be1916 350 if ($adminuser->password === 'adminsetuppending') {
351 // prevent installation hijacking
352 if ($adminuser->lastip !== getremoteaddr()) {
353 print_error('installhijacked', 'admin');
35d6a2a4 354 }
00be1916 355 // login user and let him set password and admin details
356 $adminuser->newadminuser = 1;
0342fc36 357 complete_user_login($adminuser);
00be1916 358 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
db5af934 359
5c144d60 360 } else {
00be1916 361 unset_config('adminsetuppending');
4da1a0a1 362 }
363
00be1916 364} else {
365 // just make sure upgrade logging is properly terminated
366 upgrade_finished('upgradesettings.php');
367}
b3732604 368
00be1916 369// Turn xmlstrictheaders back on now.
370$CFG->xmlstrictheaders = $origxmlstrictheaders;
371unset($origxmlstrictheaders);
372
373// Check for valid admin user - no guest autologin
374require_login(0, false);
375$context = get_context_instance(CONTEXT_SYSTEM);
376require_capability('moodle/site:config', $context);
377
378// check that site is properly customized
379$site = get_site();
380if (empty($site->shortname)) {
381 // probably new installation - lets return to frontpage after this step
382 // remove settings that we want uninitialised
383 unset_config('registerauth');
384 redirect('upgradesettings.php?return=site');
385}
386
387// Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
388if (!empty($id) and $id == $CFG->siteidentifier) {
389 set_config('registered', time());
390}
391
392// setup critical warnings before printing admin tree block
bc76b3c0 393$insecuredataroot = is_dataroot_insecure(true);
00be1916 394$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
395
396$adminroot = admin_get_root();
397
398// Check if there are any new admin settings which have still yet to be set
399if (any_new_admin_settings($adminroot)){
400 redirect('upgradesettings.php');
401}
402
403// Everything should now be set up, and the user is an admin
404
405// Print default admin page with notifications.
cc359566 406$errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
a95682b2 407
00be1916 408$lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
cc359566
TH
409$cronoverdue = ($lastcron < time() - 3600 * 24);
410$dbproblems = $DB->diagnose();
411$maintenancemode = !empty($CFG->maintenance_enabled);
74944b73 412
55585f3a 413$updateschecker = available_update_checker::instance();
c6f008e7
DM
414$availableupdates = $updateschecker->get_update_info('core',
415 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
55585f3a
DM
416$availableupdatesfetch = $updateschecker->get_last_timefetched();
417
cc359566 418admin_externalpage_setup('adminnotifications');
55585f3a
DM
419
420if ($fetchupdates) {
421 require_sesskey();
422 $updateschecker->fetch();
423 redirect($PAGE->url);
424}
425
cc359566
TH
426$output = $PAGE->get_renderer('core', 'admin');
427echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
55585f3a 428 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch);