authorize.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @file
  4. * Administrative script for running authorized file operations.
  5. *
  6. * Using this script, the site owner (the user actually owning the files on
  7. * the webserver) can authorize certain file-related operations to proceed
  8. * with elevated privileges, for example to deploy and upgrade modules or
  9. * themes. Users should not visit this page directly, but instead use an
  10. * administrative user interface which knows how to redirect the user to this
  11. * script as part of a multistep process. This script actually performs the
  12. * selected operations without loading all of Drupal, to be able to more
  13. * gracefully recover from errors. Access to the script is controlled by a
  14. * global killswitch in settings.php ('allow_authorize_operations') and via
  15. * the 'administer software updates' permission.
  16. *
  17. * There are helper functions for setting up an operation to run via this
  18. * system in modules/system/system.module. For more information, see:
  19. * @link authorize Authorized operation helper functions @endlink
  20. */
  21. /**
  22. * Root directory of Drupal installation.
  23. */
  24. define('DRUPAL_ROOT', getcwd());
  25. /**
  26. * Global flag to identify update.php and authorize.php runs, and so
  27. * avoid various unwanted operations, such as hook_init() and
  28. * hook_exit() invokes, css/js preprocessing and translation, and
  29. * solve some theming issues. This flag is checked on several places
  30. * in Drupal code (not just authorize.php).
  31. */
  32. define('MAINTENANCE_MODE', 'update');
  33. /**
  34. * Renders a 403 access denied page for authorize.php.
  35. */
  36. function authorize_access_denied_page() {
  37. drupal_add_http_header('Status', '403 Forbidden');
  38. watchdog('access denied', 'authorize.php', NULL, WATCHDOG_WARNING);
  39. drupal_set_title('Access denied');
  40. return t('You are not allowed to access this page.');
  41. }
  42. /**
  43. * Determines if the current user is allowed to run authorize.php.
  44. *
  45. * The killswitch in settings.php overrides all else, otherwise, the user must
  46. * have access to the 'administer software updates' permission.
  47. *
  48. * @return
  49. * TRUE if the current user can run authorize.php, otherwise FALSE.
  50. */
  51. function authorize_access_allowed() {
  52. return variable_get('allow_authorize_operations', TRUE) && user_access('administer software updates');
  53. }
  54. // *** Real work of the script begins here. ***
  55. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  56. require_once DRUPAL_ROOT . '/includes/common.inc';
  57. require_once DRUPAL_ROOT . '/includes/file.inc';
  58. require_once DRUPAL_ROOT . '/includes/module.inc';
  59. require_once DRUPAL_ROOT . '/includes/ajax.inc';
  60. // We prepare only a minimal bootstrap. This includes the database and
  61. // variables, however, so we have access to the class autoloader registry.
  62. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  63. // This must go after drupal_bootstrap(), which unsets globals!
  64. global $conf;
  65. // We have to enable the user and system modules, even to check access and
  66. // display errors via the maintenance theme.
  67. $module_list['system']['filename'] = 'modules/system/system.module';
  68. $module_list['user']['filename'] = 'modules/user/user.module';
  69. module_list(TRUE, FALSE, FALSE, $module_list);
  70. drupal_load('module', 'system');
  71. drupal_load('module', 'user');
  72. // We also want to have the language system available, but we do *NOT* want to
  73. // actually call drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE), since that would
  74. // also force us through the DRUPAL_BOOTSTRAP_PAGE_HEADER phase, which loads
  75. // all the modules, and that's exactly what we're trying to avoid.
  76. drupal_language_initialize();
  77. // Initialize the maintenance theme for this administrative script.
  78. drupal_maintenance_theme();
  79. $output = '';
  80. $show_messages = TRUE;
  81. if (authorize_access_allowed()) {
  82. // Load both the Form API and Batch API.
  83. require_once DRUPAL_ROOT . '/includes/form.inc';
  84. require_once DRUPAL_ROOT . '/includes/batch.inc';
  85. // Load the code that drives the authorize process.
  86. require_once DRUPAL_ROOT . '/includes/authorize.inc';
  87. // For the sake of Batch API and a few other low-level functions, we need to
  88. // initialize the URL path into $_GET['q']. However, we do not want to raise
  89. // our bootstrap level, nor do we want to call drupal_initialize_path(),
  90. // since that is assuming that modules are loaded and invoking hooks.
  91. // However, all we really care is if we're in the middle of a batch, in which
  92. // case $_GET['q'] will already be set, we just initialize it to an empty
  93. // string if it's not already defined.
  94. if (!isset($_GET['q'])) {
  95. $_GET['q'] = '';
  96. }
  97. if (isset($_SESSION['authorize_operation']['page_title'])) {
  98. drupal_set_title($_SESSION['authorize_operation']['page_title']);
  99. }
  100. else {
  101. drupal_set_title(t('Authorize file system changes'));
  102. }
  103. // See if we've run the operation and need to display a report.
  104. if (isset($_SESSION['authorize_results']) && $results = $_SESSION['authorize_results']) {
  105. // Clear the session out.
  106. unset($_SESSION['authorize_results']);
  107. unset($_SESSION['authorize_operation']);
  108. unset($_SESSION['authorize_filetransfer_info']);
  109. if (!empty($results['page_title'])) {
  110. drupal_set_title($results['page_title']);
  111. }
  112. if (!empty($results['page_message'])) {
  113. drupal_set_message($results['page_message']['message'], $results['page_message']['type']);
  114. }
  115. $output = theme('authorize_report', array('messages' => $results['messages']));
  116. $links = array();
  117. if (is_array($results['tasks'])) {
  118. $links += $results['tasks'];
  119. }
  120. else {
  121. $links = array_merge($links, array(
  122. l(t('Administration pages'), 'admin'),
  123. l(t('Front page'), '<front>'),
  124. ));
  125. }
  126. $output .= theme('item_list', array('items' => $links, 'title' => t('Next steps')));
  127. }
  128. // If a batch is running, let it run.
  129. elseif (isset($_GET['batch'])) {
  130. $output = _batch_page();
  131. }
  132. else {
  133. if (empty($_SESSION['authorize_operation']) || empty($_SESSION['authorize_filetransfer_info'])) {
  134. $output = t('It appears you have reached this page in error.');
  135. }
  136. elseif (!$batch = batch_get()) {
  137. // We have a batch to process, show the filetransfer form.
  138. $elements = drupal_get_form('authorize_filetransfer_form');
  139. $output = drupal_render($elements);
  140. }
  141. }
  142. // We defer the display of messages until all operations are done.
  143. $show_messages = !(($batch = batch_get()) && isset($batch['running']));
  144. }
  145. else {
  146. $output = authorize_access_denied_page();
  147. }
  148. if (!empty($output)) {
  149. print theme('update_page', array('content' => $output, 'show_messages' => $show_messages));
  150. }