authorize.php

  1. drupal
    1. 7 authorize.php
    2. 8 core/authorize.php

Administrative script for running authorized file operations.

Using this script, the site owner (the user actually owning the files on the webserver) can authorize certain file-related operations to proceed with elevated privileges, for example to deploy and upgrade modules or themes. Users should not visit this page directly, but instead use an administrative user interface which knows how to redirect the user to this script as part of a multistep process. This script actually performs the selected operations without loading all of Drupal, to be able to more gracefully recover from errors. Access to the script is controlled by a global killswitch in settings.php ('allow_authorize_operations') and via the 'administer software updates' permission.

There are helper functions for setting up an operation to run via this system in modules/system/system.module. For more information, see: Authorized operation helper functions

Globals

NameDescription
$conf

Functions & methods

NameDescription
authorize_access_allowedDetermines if the current user is allowed to run authorize.php.
authorize_access_denied_pageRenders a 403 access denied page for authorize.php.

Constants

NameDescription
DRUPAL_ROOTRoot directory of Drupal installation.
MAINTENANCE_MODEGlobal flag to identify update.php and authorize.php runs, and so avoid various unwanted operations, such as hook_init() and hook_exit() invokes, css/js preprocessing and translation, and solve some theming issues. This flag is checked on several…

File

core/authorize.php
View source
  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. // Change the directory to the Drupal root.
  22. chdir('..');
  23. /**
  24. * Root directory of Drupal installation.
  25. */
  26. define('DRUPAL_ROOT', getcwd());
  27. /**
  28. * Global flag to identify update.php and authorize.php runs, and so
  29. * avoid various unwanted operations, such as hook_init() and
  30. * hook_exit() invokes, css/js preprocessing and translation, and
  31. * solve some theming issues. This flag is checked on several places
  32. * in Drupal code (not just authorize.php).
  33. */
  34. const MAINTENANCE_MODE = 'update';
  35. /**
  36. * Renders a 403 access denied page for authorize.php.
  37. */
  38. function authorize_access_denied_page() {
  39. drupal_add_http_header('Status', '403 Forbidden');
  40. watchdog('access denied', 'authorize.php', NULL, WATCHDOG_WARNING);
  41. drupal_set_title('Access denied');
  42. return t('You are not allowed to access this page.');
  43. }
  44. /**
  45. * Determines if the current user is allowed to run authorize.php.
  46. *
  47. * The killswitch in settings.php overrides all else, otherwise, the user must
  48. * have access to the 'administer software updates' permission.
  49. *
  50. * @return
  51. * TRUE if the current user can run authorize.php, otherwise FALSE.
  52. */
  53. function authorize_access_allowed() {
  54. return variable_get('allow_authorize_operations', TRUE) && user_access('administer software updates');
  55. }
  56. // *** Real work of the script begins here. ***
  57. require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
  58. require_once DRUPAL_ROOT . '/core/includes/session.inc';
  59. require_once DRUPAL_ROOT . '/core/includes/common.inc';
  60. require_once DRUPAL_ROOT . '/core/includes/file.inc';
  61. require_once DRUPAL_ROOT . '/core/includes/module.inc';
  62. require_once DRUPAL_ROOT . '/core/includes/ajax.inc';
  63. // We prepare only a minimal bootstrap. This includes the database and
  64. // variables, however, so we have access to the class autoloader registry.
  65. drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
  66. // This must go after drupal_bootstrap(), which unsets globals!
  67. global $conf;
  68. // We have to enable the user and system modules, even to check access and
  69. // display errors via the maintenance theme.
  70. $module_list['system']['filename'] = 'core/modules/system/system.module';
  71. $module_list['user']['filename'] = 'core/modules/user/user.module';
  72. module_list(TRUE, FALSE, FALSE, $module_list);
  73. drupal_load('module', 'system');
  74. drupal_load('module', 'user');
  75. // We also want to have the language system available, but we do *NOT* want to
  76. // actually call drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE), since that would
  77. // also force us through the DRUPAL_BOOTSTRAP_PAGE_HEADER phase, which loads
  78. // all the modules, and that's exactly what we're trying to avoid.
  79. drupal_language_initialize();
  80. // Initialize the maintenance theme for this administrative script.
  81. drupal_maintenance_theme();
  82. $output = '';
  83. $show_messages = TRUE;
  84. if (authorize_access_allowed()) {
  85. // Load both the Form API and Batch API.
  86. require_once DRUPAL_ROOT . '/core/includes/form.inc';
  87. require_once DRUPAL_ROOT . '/core/includes/batch.inc';
  88. // Load the code that drives the authorize process.
  89. require_once DRUPAL_ROOT . '/core/includes/authorize.inc';
  90. if (isset($_SESSION['authorize_operation']['page_title'])) {
  91. drupal_set_title($_SESSION['authorize_operation']['page_title']);
  92. }
  93. else {
  94. drupal_set_title(t('Authorize file system changes'));
  95. }
  96. // See if we've run the operation and need to display a report.
  97. if (isset($_SESSION['authorize_results']) && $results = $_SESSION['authorize_results']) {
  98. // Clear the session out.
  99. unset($_SESSION['authorize_results']);
  100. unset($_SESSION['authorize_operation']);
  101. unset($_SESSION['authorize_filetransfer_info']);
  102. if (!empty($results['page_title'])) {
  103. drupal_set_title($results['page_title']);
  104. }
  105. if (!empty($results['page_message'])) {
  106. drupal_set_message($results['page_message']['message'], $results['page_message']['type']);
  107. }
  108. $output = theme('authorize_report', array('messages' => $results['messages']));
  109. $links = array();
  110. if (is_array($results['tasks'])) {
  111. $links += $results['tasks'];
  112. }
  113. else {
  114. $links = array_merge($links, array(
  115. l(t('Administration pages'), 'admin'),
  116. l(t('Front page'), '<front>'),
  117. ));
  118. }
  119. $output .= theme('item_list', array('items' => $links, 'title' => t('Next steps')));
  120. }
  121. // If a batch is running, let it run.
  122. elseif (isset($_GET['batch'])) {
  123. $output = _batch_page();
  124. }
  125. else {
  126. if (empty($_SESSION['authorize_operation']) || empty($_SESSION['authorize_filetransfer_info'])) {
  127. $output = t('It appears you have reached this page in error.');
  128. }
  129. elseif (!$batch = batch_get()) {
  130. // We have a batch to process, show the filetransfer form.
  131. $elements = drupal_get_form('authorize_filetransfer_form');
  132. $output = drupal_render($elements);
  133. }
  134. }
  135. // We defer the display of messages until all operations are done.
  136. $show_messages = !(($batch = batch_get()) && isset($batch['running']));
  137. }
  138. else {
  139. $output = authorize_access_denied_page();
  140. }
  141. if (!empty($output)) {
  142. print theme('update_page', array('content' => $output, 'show_messages' => $show_messages));
  143. }
Login or register to post comments