PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/admin/index.php

#
PHP | 369 lines | 237 code | 71 blank | 61 comment | 46 complexity | 4e2d37fd268aa2c346cca4193e1ee305 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Main administration script.
  18. *
  19. * @package core
  20. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. // Check that config.php exists, if not then call the install script
  24. if (!file_exists('../config.php')) {
  25. header('Location: ../install.php');
  26. die();
  27. }
  28. // Check that PHP is of a sufficient version as soon as possible
  29. if (version_compare(phpversion(), '5.3.2') < 0) {
  30. $phpversion = phpversion();
  31. // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
  32. echo "Moodle 2.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).<br />";
  33. echo "Please upgrade your server software or install older Moodle version.";
  34. die();
  35. }
  36. // make sure iconv is available and actually works
  37. if (!function_exists('iconv')) {
  38. // this should not happen, this must be very borked install
  39. echo 'Moodle requires the iconv PHP extension. Please install or enable the iconv extension.';
  40. die();
  41. }
  42. if (iconv('UTF-8', 'UTF-8//IGNORE', 'abc') !== 'abc') {
  43. // known to be broken in mid-2011 MAMP installations
  44. echo 'Broken iconv PHP extension detected, installation/upgrade can not continue.';
  45. die();
  46. }
  47. define('NO_OUTPUT_BUFFERING', true);
  48. require('../config.php');
  49. require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
  50. require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
  51. $id = optional_param('id', '', PARAM_TEXT);
  52. $confirmupgrade = optional_param('confirmupgrade', 0, PARAM_BOOL);
  53. $confirmrelease = optional_param('confirmrelease', 0, PARAM_BOOL);
  54. $confirmplugins = optional_param('confirmplugincheck', 0, PARAM_BOOL);
  55. $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
  56. $agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
  57. // Check some PHP server settings
  58. $PAGE->set_url('/admin/index.php');
  59. $PAGE->set_pagelayout('admin'); // Set a default pagelayout
  60. $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
  61. if (ini_get_bool('session.auto_start')) {
  62. print_error('phpvaroff', 'debug', '', (object)array('name'=>'session.auto_start', 'link'=>$documentationlink));
  63. }
  64. if (ini_get_bool('magic_quotes_runtime')) {
  65. print_error('phpvaroff', 'debug', '', (object)array('name'=>'magic_quotes_runtime', 'link'=>$documentationlink));
  66. }
  67. if (!ini_get_bool('file_uploads')) {
  68. print_error('phpvaron', 'debug', '', (object)array('name'=>'file_uploads', 'link'=>$documentationlink));
  69. }
  70. if (is_float_problem()) {
  71. print_error('phpfloatproblem', 'admin', '', $documentationlink);
  72. }
  73. // Set some necessary variables during set-up to avoid PHP warnings later on this page
  74. if (!isset($CFG->release)) {
  75. $CFG->release = '';
  76. }
  77. if (!isset($CFG->version)) {
  78. $CFG->version = '';
  79. }
  80. $version = null;
  81. $release = null;
  82. require("$CFG->dirroot/version.php"); // defines $version, $release and $maturity
  83. $CFG->target_release = $release; // used during installation and upgrades
  84. if (!$version or !$release) {
  85. print_error('withoutversion', 'debug'); // without version, stop
  86. }
  87. if (!isset($maturity)) {
  88. // Fallback for now. Should probably be removed in the future.
  89. $maturity = MATURITY_STABLE;
  90. }
  91. // Turn off xmlstrictheaders during upgrade.
  92. $origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
  93. $CFG->xmlstrictheaders = false;
  94. if (!core_tables_exist()) {
  95. $PAGE->set_pagelayout('maintenance');
  96. $PAGE->set_popup_notification_allowed(false);
  97. // fake some settings
  98. $CFG->docroot = 'http://docs.moodle.org';
  99. $strinstallation = get_string('installation', 'install');
  100. // remove current session content completely
  101. session_get_instance()->terminate_current();
  102. if (empty($agreelicense)) {
  103. $strlicense = get_string('license');
  104. $PAGE->navbar->add($strlicense);
  105. $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
  106. $PAGE->set_heading($strinstallation);
  107. $PAGE->set_cacheable(false);
  108. $output = $PAGE->get_renderer('core', 'admin');
  109. echo $output->install_licence_page();
  110. die();
  111. }
  112. if (empty($confirmrelease)) {
  113. require_once($CFG->libdir.'/environmentlib.php');
  114. list($envstatus, $environment_results) = check_moodle_environment(normalize_version($release), ENV_SELECT_RELEASE);
  115. $strcurrentrelease = get_string('currentrelease');
  116. $PAGE->navbar->add($strcurrentrelease);
  117. $PAGE->set_title($strinstallation);
  118. $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
  119. $PAGE->set_cacheable(false);
  120. $output = $PAGE->get_renderer('core', 'admin');
  121. echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
  122. die();
  123. }
  124. //TODO: add a page with list of non-standard plugins here
  125. $strdatabasesetup = get_string('databasesetup');
  126. upgrade_init_javascript();
  127. $PAGE->navbar->add($strdatabasesetup);
  128. $PAGE->set_title($strinstallation.' - Moodle '.$CFG->target_release);
  129. $PAGE->set_heading($strinstallation);
  130. $PAGE->set_cacheable(false);
  131. $output = $PAGE->get_renderer('core', 'admin');
  132. echo $output->header();
  133. if (!$DB->setup_is_unicodedb()) {
  134. if (!$DB->change_db_encoding()) {
  135. // If could not convert successfully, throw error, and prevent installation
  136. print_error('unicoderequired', 'admin');
  137. }
  138. }
  139. install_core($version, true);
  140. }
  141. // Check version of Moodle code on disk compared with database
  142. // and upgrade if possible.
  143. $stradministration = get_string('administration');
  144. $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
  145. if (empty($CFG->version)) {
  146. print_error('missingconfigversion', 'debug');
  147. }
  148. if ($version > $CFG->version) { // upgrade
  149. purge_all_caches();
  150. $PAGE->set_pagelayout('maintenance');
  151. $PAGE->set_popup_notification_allowed(false);
  152. if (empty($confirmupgrade)) {
  153. $a->oldversion = "$CFG->release ($CFG->version)";
  154. $a->newversion = "$release ($version)";
  155. $strdatabasechecking = get_string('databasechecking', '', $a);
  156. $PAGE->set_title($stradministration);
  157. $PAGE->set_heading($strdatabasechecking);
  158. $PAGE->set_cacheable(false);
  159. $output = $PAGE->get_renderer('core', 'admin');
  160. echo $output->upgrade_confirm_page($a->newversion, $maturity);
  161. die();
  162. } else if (empty($confirmrelease)){
  163. require_once($CFG->libdir.'/environmentlib.php');
  164. list($envstatus, $environment_results) = check_moodle_environment($release, ENV_SELECT_RELEASE);
  165. $strcurrentrelease = get_string('currentrelease');
  166. $PAGE->navbar->add($strcurrentrelease);
  167. $PAGE->set_title($strcurrentrelease);
  168. $PAGE->set_heading($strcurrentrelease);
  169. $PAGE->set_cacheable(false);
  170. $output = $PAGE->get_renderer('core', 'admin');
  171. echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
  172. die();
  173. } else if (empty($confirmplugins)) {
  174. $strplugincheck = get_string('plugincheck');
  175. $PAGE->navbar->add($strplugincheck);
  176. $PAGE->set_title($strplugincheck);
  177. $PAGE->set_heading($strplugincheck);
  178. $PAGE->set_cacheable(false);
  179. $output = $PAGE->get_renderer('core', 'admin');
  180. echo $output->upgrade_plugin_check_page(plugin_manager::instance(), $version, $showallplugins,
  181. new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1)),
  182. new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1)));
  183. die();
  184. } else {
  185. // Launch main upgrade
  186. upgrade_core($version, true);
  187. }
  188. } else if ($version < $CFG->version) {
  189. // better stop here, we can not continue with plugin upgrades or anything else
  190. throw new moodle_exception('downgradedcore', 'error', new moodle_url('/admin/'));
  191. }
  192. // Updated human-readable release version if necessary
  193. if ($release <> $CFG->release) { // Update the release version
  194. set_config('release', $release);
  195. }
  196. if (moodle_needs_upgrading()) {
  197. if (!$PAGE->headerprinted) {
  198. // means core upgrade or installation was not already done
  199. if (!$confirmplugins) {
  200. $strplugincheck = get_string('plugincheck');
  201. $PAGE->set_pagelayout('maintenance');
  202. $PAGE->set_popup_notification_allowed(false);
  203. $PAGE->navbar->add($strplugincheck);
  204. $PAGE->set_title($strplugincheck);
  205. $PAGE->set_heading($strplugincheck);
  206. $PAGE->set_cacheable(false);
  207. $output = $PAGE->get_renderer('core', 'admin');
  208. echo $output->upgrade_plugin_check_page(plugin_manager::instance(), $version, $showallplugins,
  209. new moodle_url('/admin/index.php'),
  210. new moodle_url('/admin/index.php', array('confirmplugincheck'=>1)));
  211. die();
  212. }
  213. }
  214. // install/upgrade all plugins and other parts
  215. upgrade_noncore(true);
  216. }
  217. // If this is the first install, indicate that this site is fully configured
  218. // except the admin password
  219. if (during_initial_install()) {
  220. set_config('rolesactive', 1); // after this, during_initial_install will return false.
  221. set_config('adminsetuppending', 1);
  222. // we need this redirect to setup proper session
  223. upgrade_finished("index.php?sessionstarted=1&amp;lang=$CFG->lang");
  224. }
  225. // make sure admin user is created - this is the last step because we need
  226. // session to be working properly in order to edit admin account
  227. if (!empty($CFG->adminsetuppending)) {
  228. $sessionstarted = optional_param('sessionstarted', 0, PARAM_BOOL);
  229. if (!$sessionstarted) {
  230. redirect("index.php?sessionstarted=1&lang=$CFG->lang");
  231. } else {
  232. $sessionverify = optional_param('sessionverify', 0, PARAM_BOOL);
  233. if (!$sessionverify) {
  234. $SESSION->sessionverify = 1;
  235. redirect("index.php?sessionstarted=1&sessionverify=1&lang=$CFG->lang");
  236. } else {
  237. if (empty($SESSION->sessionverify)) {
  238. print_error('installsessionerror', 'admin', "index.php?sessionstarted=1&lang=$CFG->lang");
  239. }
  240. unset($SESSION->sessionverify);
  241. }
  242. }
  243. // 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
  244. $adminuser = get_complete_user_data('id', reset(explode(',', $CFG->siteadmins)));
  245. if ($adminuser->password === 'adminsetuppending') {
  246. // prevent installation hijacking
  247. if ($adminuser->lastip !== getremoteaddr()) {
  248. print_error('installhijacked', 'admin');
  249. }
  250. // login user and let him set password and admin details
  251. $adminuser->newadminuser = 1;
  252. complete_user_login($adminuser);
  253. redirect("$CFG->wwwroot/user/editadvanced.php?id=$adminuser->id"); // Edit thyself
  254. } else {
  255. unset_config('adminsetuppending');
  256. }
  257. } else {
  258. // just make sure upgrade logging is properly terminated
  259. upgrade_finished('upgradesettings.php');
  260. }
  261. // Turn xmlstrictheaders back on now.
  262. $CFG->xmlstrictheaders = $origxmlstrictheaders;
  263. unset($origxmlstrictheaders);
  264. // Check for valid admin user - no guest autologin
  265. require_login(0, false);
  266. $context = get_context_instance(CONTEXT_SYSTEM);
  267. require_capability('moodle/site:config', $context);
  268. // check that site is properly customized
  269. $site = get_site();
  270. if (empty($site->shortname)) {
  271. // probably new installation - lets return to frontpage after this step
  272. // remove settings that we want uninitialised
  273. unset_config('registerauth');
  274. redirect('upgradesettings.php?return=site');
  275. }
  276. // Check if we are returning from moodle.org registration and if so, we mark that fact to remove reminders
  277. if (!empty($id) and $id == $CFG->siteidentifier) {
  278. set_config('registered', time());
  279. }
  280. // setup critical warnings before printing admin tree block
  281. $insecuredataroot = is_dataroot_insecure(true);
  282. $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
  283. $adminroot = admin_get_root();
  284. // Check if there are any new admin settings which have still yet to be set
  285. if (any_new_admin_settings($adminroot)){
  286. redirect('upgradesettings.php');
  287. }
  288. // Everything should now be set up, and the user is an admin
  289. // Print default admin page with notifications.
  290. $errorsdisplayed = defined('WARN_DISPLAY_ERRORS_ENABLED');
  291. $lastcron = $DB->get_field_sql('SELECT MAX(lastcron) FROM {modules}');
  292. $cronoverdue = ($lastcron < time() - 3600 * 24);
  293. $dbproblems = $DB->diagnose();
  294. $maintenancemode = !empty($CFG->maintenance_enabled);
  295. admin_externalpage_setup('adminnotifications');
  296. $output = $PAGE->get_renderer('core', 'admin');
  297. echo $output->admin_notifications_page($maturity, $insecuredataroot, $errorsdisplayed,
  298. $cronoverdue, $dbproblems, $maintenancemode);