comparison modules/update/update.module @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 589fb7c02327
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: update.module,v 1.17 2008/01/30 10:14:42 goba Exp $
3
4 /**
5 * @file
6 * The "Update status" module checks for available updates of Drupal core and
7 * any installed contributed modules and themes. It warns site administrators
8 * if newer releases are available via the system status report
9 * (admin/reports/status), the module and theme pages, and optionally via email.
10 */
11
12 /**
13 * URL to check for updates, if a given project doesn't define its own.
14 */
15 define('UPDATE_DEFAULT_URL', 'http://updates.drupal.org/release-history');
16
17 // These are internally used constants for this code, do not modify.
18
19 /**
20 * Project is missing security update(s).
21 */
22 define('UPDATE_NOT_SECURE', 1);
23
24 /**
25 * Current release has been unpublished and is no longer available.
26 */
27 define('UPDATE_REVOKED', 2);
28
29 /**
30 * Current release is no longer supported by the project maintainer.
31 */
32 define('UPDATE_NOT_SUPPORTED', 3);
33
34 /**
35 * Project has a new release available, but it is not a security release.
36 */
37 define('UPDATE_NOT_CURRENT', 4);
38
39 /**
40 * Project is up to date.
41 */
42 define('UPDATE_CURRENT', 5);
43
44 /**
45 * Project's status cannot be checked.
46 */
47 define('UPDATE_NOT_CHECKED', -1);
48
49 /**
50 * No available update data was found for project.
51 */
52 define('UPDATE_UNKNOWN', -2);
53
54
55 /**
56 * Implementation of hook_help().
57 */
58 function update_help($path, $arg) {
59 switch ($path) {
60 case 'admin/reports/updates':
61 $output = '<p>'. t('Here you can find information about available updates for your installed modules and themes. Note that each module or theme is part of a "project", which may or may not have the same name, and might include multiple modules or themes within it.') .'</p>';
62 $output .= '<p>'. t('To extend the functionality or to change the look of your site, a number of contributed <a href="@modules">modules</a> and <a href="@themes">themes</a> are available.', array('@modules' => 'http://drupal.org/project/modules', '@themes' => 'http://drupal.org/project/themes')) .'</p>';
63 return $output;
64 case 'admin/build/themes':
65 case 'admin/build/modules':
66 include_once './includes/install.inc';
67 $status = update_requirements('runtime');
68 foreach (array('core', 'contrib') as $report_type) {
69 $type = 'update_'. $report_type;
70 if (isset($status[$type]['severity'])) {
71 if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
72 drupal_set_message($status[$type]['description'], 'error');
73 }
74 elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
75 drupal_set_message($status[$type]['description'], 'warning');
76 }
77 }
78 }
79 return '<p>'. t('See the <a href="@available_updates">available updates</a> page for information on installed modules and themes with new versions released.', array('@available_updates' => url('admin/reports/updates'))) .'</p>';
80
81 case 'admin/reports/updates/settings':
82 case 'admin/reports/status':
83 // These two pages don't need additional nagging.
84 break;
85
86 case 'admin/help#update':
87 $output = '<p>'. t("The Update status module periodically checks for new versions of your site's software (including contributed modules and themes), and alerts you to available updates.") .'</p>';
88 $output .= '<p>'. t('The <a href="@update-report">report of available updates</a> will alert you when new releases are available for download. You may configure options for update checking frequency and notifications at the <a href="@update-settings">Update status module settings page</a>.', array('@update-report' => url('admin/reports/updates'), '@update-settings' => url('admin/reports/updates/settings'))) .'</p>';
89 $output .= '<p>'. t('Please note that in order to provide this information, anonymous usage statistics are sent to drupal.org. If desired, you may disable the Update status module from the <a href="@modules">module administration page</a>.', array('@modules' => url('admin/build/modules'))) .'</p>';
90 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@update">Update status module</a>.', array('@update' => 'http://drupal.org/handbook/modules/update')) .'</p>';
91 return $output;
92
93 default:
94 // Otherwise, if we're on *any* admin page and there's a security
95 // update missing, print an error message about it.
96 if (arg(0) == 'admin' && strpos($path, '#') === FALSE
97 && user_access('administer site configuration')) {
98 include_once './includes/install.inc';
99 $status = update_requirements('runtime');
100 foreach (array('core', 'contrib') as $report_type) {
101 $type = 'update_'. $report_type;
102 if (isset($status[$type])
103 && isset($status[$type]['reason'])
104 && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
105 drupal_set_message($status[$type]['description'], 'error');
106 }
107 }
108 }
109
110 }
111 }
112
113 /**
114 * Implementation of hook_menu().
115 */
116 function update_menu() {
117 $items = array();
118
119 $items['admin/reports/updates'] = array(
120 'title' => 'Available updates',
121 'description' => 'Get a status report about available updates for your installed modules and themes.',
122 'page callback' => 'update_status',
123 'access arguments' => array('administer site configuration'),
124 'file' => 'update.report.inc',
125 'weight' => 10,
126 );
127 $items['admin/reports/updates/list'] = array(
128 'title' => 'List',
129 'page callback' => 'update_status',
130 'access arguments' => array('administer site configuration'),
131 'file' => 'update.report.inc',
132 'type' => MENU_DEFAULT_LOCAL_TASK,
133 );
134 $items['admin/reports/updates/settings'] = array(
135 'title' => 'Settings',
136 'page callback' => 'drupal_get_form',
137 'page arguments' => array('update_settings'),
138 'access arguments' => array('administer site configuration'),
139 'file' => 'update.settings.inc',
140 'type' => MENU_LOCAL_TASK,
141 );
142 $items['admin/reports/updates/check'] = array(
143 'title' => 'Manual update check',
144 'page callback' => 'update_manual_status',
145 'access arguments' => array('administer site configuration'),
146 'file' => 'update.fetch.inc',
147 'type' => MENU_CALLBACK,
148 );
149
150 return $items;
151 }
152
153 /**
154 * Implementation of the hook_theme() registry.
155 */
156 function update_theme() {
157 return array(
158 'update_settings' => array(
159 'arguments' => array('form' => NULL),
160 ),
161 'update_report' => array(
162 'arguments' => array('data' => NULL),
163 ),
164 'update_version' => array(
165 'arguments' => array('version' => NULL, 'tag' => NULL, 'class' => NULL),
166 ),
167 );
168 }
169
170 /**
171 * Implementation of hook_requirements.
172 *
173 * @return
174 * An array describing the status of the site regarding available updates.
175 * If there is no update data, only one record will be returned, indicating
176 * that the status of core can't be determined. If data is available, there
177 * will be two records: one for core, and another for all of contrib
178 * (assuming there are any contributed modules or themes enabled on the
179 * site). In addition to the fields expected by hook_requirements ('value',
180 * 'severity', and optionally 'description'), this array will contain a
181 * 'reason' attribute, which is an integer constant to indicate why the
182 * given status is being returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or
183 * UPDATE_UNKNOWN). This is used for generating the appropriate e-mail
184 * notification messages during update_cron(), and might be useful for other
185 * modules that invoke update_requirements() to find out if the site is up
186 * to date or not.
187 *
188 * @see _update_message_text()
189 * @see _update_cron_notify()
190 */
191 function update_requirements($phase) {
192 if ($phase == 'runtime') {
193 if ($available = update_get_available(FALSE)) {
194 module_load_include('inc', 'update', 'update.compare');
195 $data = update_calculate_project_data($available);
196 // First, populate the requirements for core:
197 $requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
198 // We don't want to check drupal a second time.
199 unset($data['drupal']);
200 if (!empty($data)) {
201 // Now, sort our $data array based on each project's status. The
202 // status constants are numbered in the right order of precedence, so
203 // we just need to make sure the projects are sorted in ascending
204 // order of status, and we can look at the first project we find.
205 uasort($data, '_update_project_status_sort');
206 $first_project = reset($data);
207 $requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
208 }
209 }
210 else {
211 $requirements['update_core']['title'] = t('Drupal core update status');
212 $requirements['update_core']['value'] = t('No update data available');
213 $requirements['update_core']['severity'] = REQUIREMENT_WARNING;
214 $requirements['update_core']['reason'] = UPDATE_UNKNOWN;
215 $requirements['update_core']['description'] = _update_no_data();
216 }
217 return $requirements;
218 }
219 }
220
221 /**
222 * Private helper method to fill in the requirements array.
223 *
224 * This is shared for both core and contrib to generate the right elements in
225 * the array for hook_requirements().
226 *
227 * @param $project
228 * Array of information about the project we're testing as returned by
229 * update_calculate_project_data().
230 * @param $type
231 * What kind of project is this ('core' or 'contrib').
232 *
233 * @return
234 * An array to be included in the nested $requirements array.
235 *
236 * @see hook_requirements()
237 * @see update_requirements()
238 * @see update_calculate_project_data()
239 */
240 function _update_requirement_check($project, $type) {
241 $requirement = array();
242 if ($type == 'core') {
243 $requirement['title'] = t('Drupal core update status');
244 }
245 else {
246 $requirement['title'] = t('Module and theme update status');
247 }
248 $status = $project['status'];
249 if ($status != UPDATE_CURRENT) {
250 $requirement['reason'] = $status;
251 $requirement['description'] = _update_message_text($type, $status, TRUE);
252 $requirement['severity'] = REQUIREMENT_ERROR;
253 }
254 switch ($status) {
255 case UPDATE_NOT_SECURE:
256 $requirement_label = t('Not secure!');
257 break;
258 case UPDATE_REVOKED:
259 $requirement_label = t('Revoked!');
260 break;
261 case UPDATE_NOT_SUPPORTED:
262 $requirement_label = t('Unsupported release');
263 break;
264 case UPDATE_NOT_CURRENT:
265 $requirement_label = t('Out of date');
266 $requirement['severity'] = variable_get('update_notification_threshold', 'all') == 'all' ? REQUIREMENT_ERROR : REQUIREMENT_WARNING;
267 break;
268 case UPDATE_UNKNOWN:
269 case UPDATE_NOT_CHECKED:
270 $requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
271 $requirement['severity'] = REQUIREMENT_WARNING;
272 break;
273 default:
274 $requirement_label = t('Up to date');
275 }
276 if ($status != UPDATE_CURRENT && $type == 'core' && isset($project['recommended'])) {
277 $requirement_label .= ' '. t('(version @version available)', array('@version' => $project['recommended']));
278 }
279 $requirement['value'] = l($requirement_label, 'admin/reports/updates');
280 return $requirement;
281 }
282
283 /**
284 * Implementation of hook_cron().
285 */
286 function update_cron() {
287 $frequency = variable_get('update_check_frequency', 1);
288 $interval = 60 * 60 * 24 * $frequency;
289 if (time() - variable_get('update_last_check', 0) > $interval) {
290 update_refresh();
291 _update_cron_notify();
292 }
293 }
294
295 /**
296 * Implementation of hook_form_alter().
297 *
298 * Adds a submit handler to the system modules and themes forms, so that if a
299 * site admin saves either form, we invalidate the cache of available updates.
300 *
301 * @see update_invalidate_cache()
302 */
303 function update_form_alter(&$form, $form_state, $form_id) {
304 if ($form_id == 'system_modules' || $form_id == 'system_themes' ) {
305 $form['#submit'][] = 'update_invalidate_cache';
306 }
307 }
308
309 /**
310 * Prints a warning message when there is no data about available updates.
311 */
312 function _update_no_data() {
313 $destination = drupal_get_destination();
314 return t('No information is available about potential new releases for currently installed modules and themes. To check for updates, you may need to <a href="@run_cron">run cron</a> or you can <a href="@check_manually">check manually</a>. Please note that checking for available updates can take a long time, so please be patient.', array(
315 '@run_cron' => url('admin/reports/status/run-cron', array('query' => $destination)),
316 '@check_manually' => url('admin/reports/updates/check', array('query' => $destination)),
317 ));
318 }
319
320 /**
321 * Internal helper to try to get the update information from the cache
322 * if possible, and to refresh the cache when necessary.
323 *
324 * In addition to checking the cache lifetime, this function also ensures that
325 * there are no .info files for enabled modules or themes that have a newer
326 * modification timestamp than the last time we checked for available update
327 * data. If any .info file was modified, it almost certainly means a new
328 * version of something was installed. Without fresh available update data,
329 * the logic in update_calculate_project_data() will be wrong and produce
330 * confusing, bogus results.
331 *
332 * @param $refresh
333 * Boolean to indicate if this method should refresh the cache automatically
334 * if there's no data.
335 *
336 * @see update_refresh()
337 * @see update_get_projects()
338 */
339 function update_get_available($refresh = FALSE) {
340 module_load_include('inc', 'update', 'update.compare');
341 $available = array();
342
343 // First, make sure that none of the .info files have a change time
344 // newer than the last time we checked for available updates.
345 $needs_refresh = FALSE;
346 $last_check = variable_get('update_last_check', 0);
347 $projects = update_get_projects();
348 foreach ($projects as $key => $project) {
349 if ($project['info']['_info_file_ctime'] > $last_check) {
350 $needs_refresh = TRUE;
351 break;
352 }
353 }
354 if (!$needs_refresh && ($cache = cache_get('update_info', 'cache_update'))
355 && $cache->expire > time()) {
356 $available = $cache->data;
357 }
358 elseif ($needs_refresh || $refresh) {
359 // If we need to refresh due to a newer .info file, ignore the argument
360 // and force the refresh (e.g., even for update_requirements()) to prevent
361 // bogus results.
362 $available = update_refresh();
363 }
364 return $available;
365 }
366
367 /**
368 * Implementation of hook_flush_caches().
369 *
370 * The function update.php (among others) calls this hook to flush the caches.
371 * Since we're running update.php, we are likely to install a new version of
372 * something, in which case, we want to check for available update data again.
373 */
374 function update_flush_caches() {
375 return array('cache_update');
376 }
377
378 /**
379 * Invalidates any cached data relating to update status.
380 */
381 function update_invalidate_cache() {
382 cache_clear_all('*', 'cache_update', TRUE);
383 }
384
385 /**
386 * Wrapper to load the include file and then refresh the release data.
387 */
388 function update_refresh() {
389 module_load_include('inc', 'update', 'update.fetch');
390 return _update_refresh();
391 }
392
393 /**
394 * Implementation of hook_mail().
395 *
396 * Constructs the email notification message when the site is out of date.
397 *
398 * @param $key
399 * Unique key to indicate what message to build, always 'status_notify'.
400 * @param $message
401 * Reference to the message array being built.
402 * @param $params
403 * Array of parameters to indicate what kind of text to include in the
404 * message body. This is a keyed array of message type ('core' or 'contrib')
405 * as the keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for
406 * the values.
407 *
408 * @see drupal_mail()
409 * @see _update_cron_notify()
410 * @see _update_message_text()
411 */
412 function update_mail($key, &$message, $params) {
413 $language = $message['language'];
414 $langcode = $language->language;
415 $message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => variable_get('site_name', 'Drupal')), $langcode);
416 foreach ($params as $msg_type => $msg_reason) {
417 $message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $language);
418 }
419 $message['body'][] = t('See the available updates page for more information:', array(), $langcode) ."\n". url('admin/reports/updates', array('absolute' => TRUE, 'language' => $language));
420 }
421
422 /**
423 * Helper function to return the appropriate message text when the site is out
424 * of date or missing a security update.
425 *
426 * These error messages are shared by both update_requirements() for the
427 * site-wide status report at admin/reports/status and in the body of the
428 * notification emails generated by update_cron().
429 *
430 * @param $msg_type
431 * String to indicate what kind of message to generate. Can be either
432 * 'core' or 'contrib'.
433 * @param $msg_reason
434 * Integer constant specifying why message is generated.
435 * @param $report_link
436 * Boolean that controls if a link to the updates report should be added.
437 * @param $language
438 * An optional language object to use.
439 * @return
440 * The properly translated error message for the given key.
441 */
442 function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $language = NULL) {
443 $langcode = isset($language) ? $language->language : NULL;
444 $text = '';
445 switch ($msg_reason) {
446 case UPDATE_NOT_SECURE:
447 if ($msg_type == 'core') {
448 $text = t('There is a security update available for your version of Drupal. To ensure the security of your server, you should update immediately!', array(), $langcode);
449 }
450 else {
451 $text = t('There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately!', array(), $langcode);
452 }
453 break;
454
455 case UPDATE_REVOKED:
456 if ($msg_type == 'core') {
457 $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', array(), $langcode);
458 }
459 else {
460 $text = t('The installed version of at least one of your modules or themes has been revoked and is no longer available for download. Upgrading or disabling is strongly recommended!', array(), $langcode);
461 }
462 break;
463
464 case UPDATE_NOT_SUPPORTED:
465 if ($msg_type == 'core') {
466 $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', array(), $langcode);
467 }
468 else {
469 $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or disabling is strongly recommended! Please see the project homepage for more details.', array(), $langcode);
470 }
471 break;
472
473 case UPDATE_NOT_CURRENT:
474 if ($msg_type == 'core') {
475 $text = t('There are updates available for your version of Drupal. To ensure the proper functioning of your site, you should update as soon as possible.', array(), $langcode);
476 }
477 else {
478 $text = t('There are updates available for one or more of your modules or themes. To ensure the proper functioning of your site, you should update as soon as possible.', array(), $langcode);
479 }
480 break;
481
482 case UPDATE_UNKNOWN:
483 case UPDATE_NOT_CHECKED:
484 if ($msg_type == 'core') {
485 $text = t('There was a problem determining the status of available updates for your version of Drupal.', array(), $langcode);
486 }
487 else {
488 $text = t('There was a problem determining the status of available updates for one or more of your modules or themes.', array(), $langcode);
489 }
490 break;
491 }
492
493 if ($report_link) {
494 $text .= ' '. t('See the <a href="@available_updates">available updates</a> page for more information.', array('@available_updates' => url('admin/reports/updates', array('language' => $language))), $langcode);
495 }
496
497 return $text;
498 }
499
500 /**
501 * Private sort function to order projects based on their status.
502 *
503 * @see update_requirements()
504 * @see uasort()
505 */
506 function _update_project_status_sort($a, $b) {
507 // The status constants are numerically in the right order, so we can
508 // usually subtract the two to compare in the order we want. However,
509 // negative status values should be treated as if they are huge, since we
510 // always want them at the bottom of the list.
511 $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
512 $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
513 return $a_status - $b_status;
514 }