MDL-35238 Fetch available updates using the 1.1 version of the API
[moodle.git] / admin / index.php
blob2f88126da1a7d94f490fe2fd97d1d730b6b978d5
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();
48 define('NO_OUTPUT_BUFFERING', true);
50 require('../config.php');
51 require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
52 require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
53 require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
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);
59 $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
60 $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
61 $fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
63 // Check some PHP server settings
65 $PAGE->set_url('/admin/index.php');
66 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
68 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
70 if (ini_get_bool('session.auto_start')) {
71 print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
74 if (ini_get_bool('magic_quotes_runtime')) {
75 print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
78 if (!ini_get_bool('file_uploads')) {
79 print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
82 if (is_float_problem()) {
83 print_error('phpfloatproblem', 'admin', '', $documentationlink);
86 // Set some necessary variables during set-up to avoid PHP warnings later on this page
87 if (!isset($CFG->release)) {
88 $CFG->release = '';
90 if (!isset($CFG->version)) {
91 $CFG->version = '';
93 if (!isset($CFG->branch)) {
94 $CFG->branch = '';
97 $version = null;
98 $release = null;
99 $branch = null;
100 require("$CFG->dirroot/version.php"); // defines $version, $release, $branch and $maturity
101 $CFG->target_release = $release; // used during installation and upgrades
103 if (!$version or !$release) {
104 print_error('withoutversion', 'debug'); // without version, stop
107 if (!core_tables_exist()) {
108 $PAGE->set_pagelayout('maintenance');
109 $PAGE->set_popup_notification_allowed(false);
111 // fake some settings
112 $CFG->docroot = 'http://docs.moodle.org';
114 $strinstallation = get_string('installation', 'install');
116 // remove current session content completely
117 session_get_instance()->terminate_current();
119 if (empty($agreelicense)) {
120 $strlicense = get_string('license');
122 $PAGE->navbar->add($strlicense);
123 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
124 $PAGE->set_heading($strinstallation);
125 $PAGE->set_cacheable(false);
127 $output = $PAGE->get_renderer('core', 'admin');
128 echo $output->install_licence_page();
129 die();
131 if (empty($confirmrelease)) {
132 require_once($CFG->libdir.'/environmentlib.php');
133 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
134 $strcurrentrelease = get_string('currentrelease');
136 $PAGE->navbar->add($strcurrentrelease);
137 $PAGE->set_title($strinstallation);
138 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
139 $PAGE->set_cacheable(false);
141 $output = $PAGE->get_renderer('core', 'admin');
142 echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
143 die();
146 // check plugin dependencies
147 $failed = array();
148 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
149 $PAGE->navbar->add(get_string('pluginscheck', 'admin'));
150 $PAGE->set_title($strinstallation);
151 $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
153 $output = $PAGE->get_renderer('core', 'admin');
154 $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
155 echo $output->unsatisfied_dependencies_page($version, $failed, $url);
156 die();
158 unset($failed);
160 //TODO: add a page with list of non-standard plugins here
162 $strdatabasesetup = get_string('databasesetup');
163 upgrade_init_javascript();
165 $PAGE->navbar->add($strdatabasesetup);
166 $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
167 $PAGE->set_heading($strinstallation);
168 $PAGE->set_cacheable(false);
170 $output = $PAGE->get_renderer('core', 'admin');
171 echo $output->header();
173 if (!$DB->setup_is_unicodedb()) {
174 if (!$DB->change_db_encoding()) {
175 // If could not convert successfully, throw error, and prevent installation
176 print_error('unicoderequired', 'admin');
180 install_core($version, true);
184 // Check version of Moodle code on disk compared with database
185 // and upgrade if possible.
187 $stradministration = get_string('administration');
188 $PAGE->set_context(context_system::instance());
190 if (empty($CFG->version)) {
191 print_error('missingconfigversion', 'debug');
194 if ($version > $CFG->version) { // upgrade
195 purge_all_caches();
196 $PAGE->set_pagelayout('maintenance');
197 $PAGE->set_popup_notification_allowed(false);
199 if (upgrade_stale_php_files_present()) {
200 $PAGE->set_title($stradministration);
201 $PAGE->set_cacheable(false);
203 $output = $PAGE->get_renderer('core', 'admin');
204 echo $output->upgrade_stale_php_files_page();
205 die();
208 if (empty($confirmupgrade)) {
209 $a = new stdClass();
210 $a->oldversion = "$CFG->release ($CFG->version)";
211 $a->newversion = "$release ($version)";
212 $strdatabasechecking = get_string('databasechecking', '', $a);
214 $PAGE->set_title($stradministration);
215 $PAGE->set_heading($strdatabasechecking);
216 $PAGE->set_cacheable(false);
218 $output = $PAGE->get_renderer('core', 'admin');
219 echo $output->upgrade_confirm_page($a->newversion, $maturity);
220 die();
222 } else if (empty($confirmrelease)){
223 require_once($CFG->libdir.'/environmentlib.php');
224 list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
225 $strcurrentrelease = get_string('currentrelease');
227 $PAGE->navbar->add($strcurrentrelease);
228 $PAGE->set_title($strcurrentrelease);
229 $PAGE->set_heading($strcurrentrelease);
230 $PAGE->set_cacheable(false);
232 $output = $PAGE->get_renderer('core', 'admin');
233 echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
234 die();
236 } else if (empty($confirmplugins)) {
237 $strplugincheck = get_string('plugincheck');
239 $PAGE->navbar->add($strplugincheck);
240 $PAGE->set_title($strplugincheck);
241 $PAGE->set_heading($strplugincheck);
242 $PAGE->set_cacheable(false);
244 $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
246 // check plugin dependencies first
247 $failed = array();
248 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
249 $output = $PAGE->get_renderer('core', 'admin');
250 echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
251 die();
253 unset($failed);
255 if ($fetchupdates) {
256 // no sesskey support guaranteed here
257 if (empty($CFG->disableupdatenotifications)) {
258 available_update_checker::instance()->fetch();
260 redirect($reloadurl);
263 $output = $PAGE->get_renderer('core', 'admin');
265 $deployer = available_update_deployer::instance();
266 if ($deployer->enabled()) {
267 $deployer->initialize($reloadurl, $reloadurl);
269 $deploydata = $deployer->submitted_data();
270 if (!empty($deploydata)) {
271 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
272 die();
276 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
277 $version, $showallplugins, $reloadurl,
278 new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
279 die();
281 } else {
282 // Launch main upgrade
283 upgrade_core($version, true);
285 } else if ($version < $CFG->version) {
286 // better stop here, we can not continue with plugin upgrades or anything else
287 throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
290 // Updated human-readable release version if necessary
291 if ($release <> $CFG->release) { // Update the release version
292 set_config('release', $release);
295 if ($branch <> $CFG->branch) { // Update the branch
296 set_config('branch', $branch);
299 if (moodle_needs_upgrading()) {
300 if (!$PAGE->headerprinted) {
301 // means core upgrade or installation was not already done
302 if (!$confirmplugins) {
303 $strplugincheck = get_string('plugincheck');
305 $PAGE->set_pagelayout('maintenance');
306 $PAGE->set_popup_notification_allowed(false);
307 $PAGE->navbar->add($strplugincheck);
308 $PAGE->set_title($strplugincheck);
309 $PAGE->set_heading($strplugincheck);
310 $PAGE->set_cacheable(false);
312 if ($fetchupdates) {
313 // no sesskey support guaranteed here
314 available_update_checker::instance()->fetch();
315 redirect($PAGE->url);
318 $output = $PAGE->get_renderer('core', 'admin');
320 $deployer = available_update_deployer::instance();
321 if ($deployer->enabled()) {
322 $deployer->initialize($PAGE->url, $PAGE->url);
324 $deploydata = $deployer->submitted_data();
325 if (!empty($deploydata)) {
326 echo $output->upgrade_plugin_confirm_deploy_page($deployer, $deploydata);
327 die();
331 // check plugin dependencies first
332 $failed = array();
333 if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
334 echo $output->unsatisfied_dependencies_page($version, $failed, $PAGE->url);
335 die();
337 unset($failed);
339 // dependencies check passed, let's rock!
340 echo $output->upgrade_plugin_check_page(plugin_manager::instance(), available_update_checker::instance(),
341 $version, $showallplugins,
342 new moodle_url($PAGE->url),
343 new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
344 die();
347 // install/upgrade all plugins and other parts
348 upgrade_noncore(true);
351 // If this is the first install, indicate that this site is fully configured
352 // except the admin password
353 if (during_initial_install()) {
354 set_config('rolesactive', 1); // after this, during_initial_install will return false.
355 set_config('adminsetuppending', 1);
356 // we need this redirect to setup proper session
357 upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
360 // make sure admin user is created - this is the last step because we need
361 // session to be working properly in order to edit admin account
362 if (!empty($CFG->adminsetuppending)) {
363 $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
364 if (!$sessionstarted) {
365 redirect("index.php?sessionstarted=1&lang=$CFG->lang");
366 } else {
367 $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
368 if (!$sessionverify) {
369 $SESSION->sessionverify = 1;
370 redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
371 } else {
372 if (empty($SESSION->sessionverify)) {
373 print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
375 unset($SESSION->sessionverify);
379 // 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
380 $adminids = explode(',', $CFG->siteadmins);
381 $adminuser = get_complete_user_data('id', reset($adminids));
383 if ($adminuser->password === 'adminsetuppending') {
384 // prevent installation hijacking
385 if ($adminuser->lastip !== getremoteaddr()) {
386 print_error('installhijacked', 'admin');
388 // login user and let him set password and admin details
389 $adminuser->newadminuser = 1;
390 complete_user_login($adminuser);
391 redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
393 } else {
394 unset_config('adminsetuppending');
397 } else {
398 // just make sure upgrade logging is properly terminated
399 upgrade_finished('upgradesettings.php');
402 // Check for valid admin user - no guest autologin
403 require_login(0, false);
404 $context = context_system::instance();
405 require_capability('moodle/site:config', $context);
407 // check that site is properly customized
408 $site = get_site();
409 if (empty($site->shortname)) {
410 // probably new installation - lets return to frontpage after this step
411 // remove settings that we want uninitialised
412 unset_config('registerauth');
413 redirect('upgradesettings.php?return=site');
416 // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
417 if (!empty($id) and $id == $CFG->siteidentifier) {
418 set_config('registered', time());
421 // setup critical warnings before printing admin tree block
422 $insecuredataroot = is_dataroot_insecure(true);
423 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
425 $adminroot = admin_get_root();
427 // Check if there are any new admin settings which have still yet to be set
428 if (any_new_admin_settings($adminroot)){
429 redirect('upgradesettings.php');
432 // Everything should now be set up, and the user is an admin
434 // Print default admin page with notifications.
435 $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
437 $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
438 $cronoverdue = ($lastcron < time() - 3600 * 24);
439 $dbproblems = $DB->diagnose();
440 $maintenancemode = !empty($CFG->maintenance_enabled);
442 // Available updates for Moodle core
443 $updateschecker = available_update_checker::instance();
444 $availableupdates = array();
445 $availableupdates['core'] = $updateschecker->get_update_info('core',
446 array('minmaturity' => $CFG->updateminmaturity, 'notifybuilds' => $CFG->updatenotifybuilds));
448 // Available updates for contributed plugins
449 $pluginman = plugin_manager::instance();
450 foreach ($pluginman->get_plugins() as $plugintype => $plugintypeinstances) {
451 foreach ($plugintypeinstances as $pluginname => $plugininfo) {
452 if (!empty($plugininfo->availableupdates)) {
453 foreach ($plugininfo->availableupdates as $pluginavailableupdate) {
454 if ($pluginavailableupdate->version > $plugininfo->versiondisk) {
455 if (!isset($availableupdates[$plugintype.'_'.$pluginname])) {
456 $availableupdates[$plugintype.'_'.$pluginname] = array();
458 $availableupdates[$plugintype.'_'.$pluginname][] = $pluginavailableupdate;
465 // The timestamp of the most recent check for available updates
466 $availableupdatesfetch = $updateschecker->get_last_timefetched();
468 $buggyiconvnomb = (!function_exists('mb_convert_encoding') and @iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'€') !== '100€');
469 //check if the site is registered on Moodle.org
470 $registered = $DB->count_records('registration_hubs', array('huburl' => HUB_MOODLEORGHUBURL, 'confirmed' => 1));
472 admin_externalpage_setup('adminnotifications');
474 if ($fetchupdates) {
475 require_sesskey();
476 $updateschecker->fetch();
477 redirect($PAGE->url);
480 $output = $PAGE->get_renderer('core', 'admin');
481 echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
482 $cronoverdue, $dbproblems, $maintenancemode, $availableupdates, $availableupdatesfetch, $buggyiconvnomb,
483 $registered);