/[drupal]/drupal/update.php
ViewVC logotype

Contents of /drupal/update.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.309 - (show annotations) (download) (as text)
Sat Oct 24 01:31:05 2009 UTC (5 weeks, 1 day ago) by webchick
Branch: MAIN
Changes since 1.308: +3 -1 lines
File MIME type: text/x-php
#585628 by catch: Fixed accessing update.php as anonymous user.
1 <?php
2 // $Id: update.php,v 1.308 2009/10/24 01:22:28 webchick Exp $
3
4 /**
5 * Root directory of Drupal installation.
6 */
7 define('DRUPAL_ROOT', getcwd());
8
9 /**
10 * @file
11 * Administrative page for handling updates from one Drupal version to another.
12 *
13 * Point your browser to "http://www.example.com/update.php" and follow the
14 * instructions.
15 *
16 * If you are not logged in using either the site maintenance account or an
17 * account with the "Administer software updates" permission, you will need to
18 * modify the access check statement inside your settings.php file. After
19 * finishing the upgrade, be sure to open settings.php again, and change it
20 * back to its original state!
21 */
22
23 /**
24 * Global flag to identify update.php run, and so avoid various unwanted
25 * operations, such as hook_init() and hook_exit() invokes, css/js preprocessing
26 * and translation, and solve some theming issues. This flag is checked on several
27 * places in Drupal code (not just update.php).
28 */
29 define('MAINTENANCE_MODE', 'update');
30
31 function update_selection_page() {
32 drupal_set_title('Drupal database update');
33 $output = drupal_render(drupal_get_form('update_script_selection_form'));
34
35 update_task_list('select');
36
37 return $output;
38 }
39
40 function update_script_selection_form() {
41 $form = array();
42 $count = 0;
43 $form['start'] = array(
44 '#tree' => TRUE,
45 '#type' => 'fieldset',
46 '#collapsed' => TRUE,
47 '#collapsible' => TRUE,
48 );
49
50 // Ensure system.module's updates appear first
51 $form['start']['system'] = array();
52
53 $updates = update_get_update_list();
54 foreach ($updates as $module => $update) {
55 if (!isset($update['start'])) {
56 $form['start'][$module] = array(
57 '#title' => $module,
58 '#item' => $update['warning'],
59 '#prefix' => '<div class="warning">',
60 '#suffix' => '</div>',
61 );
62 continue;
63 }
64 if (!empty($update['pending'])) {
65 $form['start'][$module] = array(
66 '#type' => 'hidden',
67 '#value' => $update['start'],
68 );
69 $form['start'][$module . '_updates'] = array(
70 '#markup' => theme('item_list', array('items' => $update['pending'], 'title' => $module . ' module')),
71 );
72 }
73 if (isset($update['pending'])) {
74 $count = $count + count($update['pending']);
75 }
76 }
77
78 if (empty($count)) {
79 drupal_set_message(t('No pending updates.'));
80 unset($form);
81 $form['links'] = array(
82 '#markup' => theme('item_list', array('items' => update_helpful_links())),
83 );
84 }
85 else {
86 $form['help'] = array(
87 '#markup' => '<p>The version of Drupal you are updating from has been automatically detected.</p>',
88 '#weight' => -5,
89 );
90 $form['start']['#title'] = format_plural($count, '1 pending update', '@count pending updates');
91 $form['has_js'] = array(
92 '#type' => 'hidden',
93 '#default_value' => FALSE,
94 );
95 $form['submit'] = array(
96 '#type' => 'submit',
97 '#value' => 'Apply pending updates',
98 );
99 }
100 return $form;
101 }
102
103 function update_helpful_links() {
104 // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
105 $links[] = '<a href="' . base_path() . '">Front page</a>';
106 $links[] = '<a href="' . base_path() . '?q=admin">Administration pages</a>';
107 return $links;
108 }
109
110 function update_results_page() {
111 drupal_set_title('Drupal database update');
112 $links = update_helpful_links();
113
114 update_task_list();
115 // Report end result
116 if (module_exists('dblog')) {
117 $log_message = ' All errors have been <a href="' . base_path() . '?q=admin/reports/dblog">logged</a>.';
118 }
119 else {
120 $log_message = ' All errors have been logged.';
121 }
122
123 if ($_SESSION['update_success']) {
124 $output = '<p>Updates were attempted. If you see no failures below, you may proceed happily to the <a href="' . base_path() . '?q=admin">administration pages</a>. Otherwise, you may need to update your database manually.' . $log_message . '</p>';
125 }
126 else {
127 list($module, $version) = array_pop(reset($_SESSION['updates_remaining']));
128 $output = '<p class="error">The update process was aborted prematurely while running <strong>update #' . $version . ' in ' . $module . '.module</strong>.' . $log_message;
129 if (module_exists('dblog')) {
130 $output .= ' You may need to check the <code>watchdog</code> database table manually.';
131 }
132 $output .= '</p>';
133 }
134
135 if (!empty($GLOBALS['update_free_access'])) {
136 $output .= "<p><strong>Reminder: don't forget to set the <code>\$update_free_access</code> value in your <code>settings.php</code> file back to <code>FALSE</code>.</strong></p>";
137 }
138
139 $output .= theme('item_list', array('items' => $links));
140
141 // Output a list of queries executed
142 if (!empty($_SESSION['update_results'])) {
143 $output .= '<div id="update-results">';
144 $output .= '<h2>The following updates returned messages</h2>';
145 foreach ($_SESSION['update_results'] as $module => $updates) {
146 $output .= '<h3>' . $module . ' module</h3>';
147 foreach ($updates as $number => $queries) {
148 if ($number != '#abort') {
149 $messages = array();
150 foreach ($queries as $query) {
151 // If there is no message for this update, don't show anything.
152 if (empty($query['query'])) {
153 continue;
154 }
155 if ($query['success']) {
156 $messages[] = '<li class="success">' . $query['query'] . '</li>';
157 }
158 else {
159 $messages[] = '<li class="failure"><strong>Failed:</strong> ' . $query['query'] . '</li>';
160 }
161 }
162
163 if ($messages) {
164 $output .= '<h4>Update #' . $number . "</h4>\n";
165 $output .= '<ul>' . implode("\n", $messages) . "</ul>\n";
166 }
167 }
168 $output .= '</ul>';
169 }
170 }
171 $output .= '</div>';
172 }
173 unset($_SESSION['update_results']);
174 unset($_SESSION['update_success']);
175
176 return $output;
177 }
178
179 function update_info_page() {
180 // Change query-strings on css/js files to enforce reload for all users.
181 _drupal_flush_css_js();
182 // Flush the cache of all data for the update status module.
183 if (db_table_exists('cache_update')) {
184 cache_clear_all('*', 'cache_update', TRUE);
185 }
186
187 update_task_list('info');
188 drupal_set_title('Drupal database update');
189 $token = drupal_get_token('update');
190 $output = '<p>Use this utility to update your database whenever a new release of Drupal or a module is installed.</p><p>For more detailed information, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>';
191 $output .= "<ol>\n";
192 $output .= "<li><strong>Back up your database</strong>. This process will change your database values and in case of emergency you may need to revert to a backup.</li>\n";
193 $output .= "<li><strong>Back up your code</strong>. Hint: when backing up module code, do not leave that backup in the 'modules' or 'sites/*/modules' directories as this may confuse Drupal's auto-discovery mechanism.</li>\n";
194 $output .= '<li>Put your site into <a href="' . base_path() . '?q=admin/config/development/maintenance">maintenance mode</a>.</li>' . "\n";
195 $output .= "<li>Install your new files in the appropriate location, as described in the handbook.</li>\n";
196 $output .= "</ol>\n";
197 $output .= "<p>When you have performed the steps above, you may proceed.</p>\n";
198 $output .= '<form method="post" action="update.php?op=selection&amp;token=' . $token . '"><p><input type="submit" value="Continue" /></p></form>';
199 $output .= "\n";
200 return $output;
201 }
202
203 function update_access_denied_page() {
204 drupal_add_http_header('403 Forbidden');
205 watchdog('access denied', 'update.php', NULL, WATCHDOG_WARNING);
206 drupal_set_title('Access denied');
207 return '<p>Access denied. You are not authorized to access this page. Please log in using either an account with the <em>administer software updates</em> permission or the site maintenance account (the account you created during installation). If you cannot log in, you will have to edit <code>settings.php</code> to bypass this access check. To do this:</p>
208 <ol>
209 <li>With a text editor find the settings.php file on your system. From the main Drupal directory that you installed all the files into, go to <code>sites/your_site_name</code> if such directory exists, or else to <code>sites/default</code> which applies otherwise.</li>
210 <li>There is a line inside your settings.php file that says <code>$update_free_access = FALSE;</code>. Change it to <code>$update_free_access = TRUE;</code>.</li>
211 <li>As soon as the update.php script is done, you must change the settings.php file back to its original form with <code>$update_free_access = FALSE;</code>.</li>
212 <li>To avoid having this problem in the future, remember to log in to your website using either an account with the <em>administer software updates</em> permission or the site maintenance account (the account you created during installation) before you backup your database at the beginning of the update process.</li>
213 </ol>';
214 }
215
216 /**
217 * Determines if the current user is allowed to run update.php.
218 *
219 * @return
220 * TRUE if the current user should be granted access, or FALSE otherwise.
221 */
222 function update_access_allowed() {
223 global $update_free_access, $user;
224
225 // Allow the global variable in settings.php to override the access check.
226 if (!empty($update_free_access)) {
227 return TRUE;
228 }
229 // Calls to user_access() might fail during the Drupal 6 to 7 update process,
230 // so we fall back on requiring that the user be logged in as user #1.
231 try {
232 require_once drupal_get_path('module', 'user') . '/user.module';
233 return user_access('administer software updates');
234 }
235 catch (Exception $e) {
236 return ($user->uid == 1);
237 }
238 }
239
240 /**
241 * Add the update task list to the current page.
242 */
243 function update_task_list($active = NULL) {
244 // Default list of tasks.
245 $tasks = array(
246 'requirements' => 'Verify requirements',
247 'info' => 'Overview',
248 'select' => 'Review updates',
249 'run' => 'Run updates',
250 'finished' => 'Review log',
251 );
252
253 drupal_add_region_content('sidebar_first', theme('task_list', array('items' => $tasks, 'active' => $active)));
254 }
255
256 /**
257 * Returns (and optionally stores) extra requirements that only apply during
258 * particular parts of the update.php process.
259 */
260 function update_extra_requirements($requirements = NULL) {
261 static $extra_requirements = array();
262 if (isset($requirements)) {
263 $extra_requirements += $requirements;
264 }
265 return $extra_requirements;
266 }
267
268 /**
269 * Check update requirements and report any errors.
270 */
271 function update_check_requirements() {
272 // Check the system module and update.php requirements only.
273 $requirements = module_invoke('system', 'requirements', 'update');
274 $requirements += update_extra_requirements();
275 $severity = drupal_requirements_severity($requirements);
276
277 // If there are issues, report them.
278 if ($severity == REQUIREMENT_ERROR) {
279 update_task_list('requirements');
280 drupal_set_title('Requirements problem');
281 $status_report = theme('status_report', array('requirements' => $requirements));
282 $status_report .= 'Please check the error messages and <a href="' . request_uri() . '">try again</a>.';
283 print theme('update_page', array('content' => $status_report));
284 exit();
285 }
286 }
287
288 // Some unavoidable errors happen because the database is not yet up-to-date.
289 // Our custom error handler is not yet installed, so we just suppress them.
290 ini_set('display_errors', FALSE);
291
292 // We prepare a minimal bootstrap for the update requirements check to avoid
293 // reaching the PHP memory limit.
294 require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
295 require_once DRUPAL_ROOT . '/includes/update.inc';
296 require_once DRUPAL_ROOT . '/includes/common.inc';
297 require_once DRUPAL_ROOT . '/includes/entity.inc';
298 update_prepare_d7_bootstrap();
299
300 // Determine if the current user has access to run update.php.
301 drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
302
303 // Only allow the requirements check to proceed if the current user has access
304 // to run updates (since it may expose sensitive information about the site's
305 // configuration).
306 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
307 if (empty($op) && update_access_allowed()) {
308 require_once DRUPAL_ROOT . '/includes/install.inc';
309 require_once DRUPAL_ROOT . '/includes/file.inc';
310 require_once DRUPAL_ROOT . '/modules/system/system.install';
311
312 // Load module basics.
313 include_once DRUPAL_ROOT . '/includes/module.inc';
314 $module_list['system']['filename'] = 'modules/system/system.module';
315 $module_list['filter']['filename'] = 'modules/filter/filter.module';
316 module_list(TRUE, FALSE, FALSE, $module_list);
317 drupal_load('module', 'system');
318 drupal_load('module', 'filter');
319
320 // Reset the module_implements() cache so that any new hook implementations
321 // in updated code are picked up.
322 module_implements('', FALSE, TRUE);
323
324 // Set up $language, since the installer components require it.
325 drupal_language_initialize();
326
327 // Set up theme system for the maintenance page.
328 drupal_maintenance_theme();
329
330 // Check the update requirements for Drupal.
331 update_check_requirements();
332
333 // Redirect to the update information page if all requirements were met.
334 install_goto('update.php?op=info');
335 }
336
337 // update_fix_d7_requirements() needs to run before bootstrapping beyond path.
338 // So bootstrap to DRUPAL_BOOTSTRAP_LANGUAGE then include unicode.inc.
339
340 drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
341 include_once DRUPAL_ROOT . '/includes/unicode.inc';
342
343 update_fix_d7_requirements();
344
345 // Now proceed with a full bootstrap.
346
347 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
348 drupal_maintenance_theme();
349
350 // Turn error reporting back on. From now on, only fatal errors (which are
351 // not passed through the error handler) will cause a message to be printed.
352 ini_set('display_errors', TRUE);
353
354 // Only proceed with updates if the user is allowed to run them.
355 if (update_access_allowed()) {
356
357 include_once DRUPAL_ROOT . '/includes/install.inc';
358 include_once DRUPAL_ROOT . '/includes/batch.inc';
359 drupal_load_updates();
360
361 update_fix_compatibility();
362
363 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
364 switch ($op) {
365 // update.php ops
366
367 case 'selection':
368 if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) {
369 $output = update_selection_page();
370 break;
371 }
372
373 case 'Apply pending updates':
374 if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) {
375 update_batch($_POST['start'], $base_url . '/update.php?op=results', $base_url . '/update.php');
376 break;
377 }
378
379 case 'info':
380 $output = update_info_page();
381 break;
382
383 case 'results':
384 $output = update_results_page();
385 break;
386
387 // Regular batch ops : defer to batch processing API
388 default:
389 update_task_list('run');
390 $output = _batch_page();
391 break;
392 }
393 }
394 else {
395 $output = update_access_denied_page();
396 }
397 if (isset($output) && $output) {
398 // Explictly start a session so that the update.php token will be accepted.
399 drupal_session_start();
400 // We defer the display of messages until all updates are done.
401 $progress_page = ($batch = batch_get()) && isset($batch['running']);
402 print theme('update_page', array('content' => $output, 'show_messages' => !$progress_page));
403 }

  ViewVC Help
Powered by ViewVC 1.1.2