1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
<?php
/**
* @file
* The "Update status" module checks for available updates of Drupal core and
* any installed contributed modules and themes. It warns site administrators
* if newer releases are available via the system status report
* (admin/reports/status), the module and theme pages, and optionally via email.
*/
/**
* URL to check for updates, if a given project doesn't define its own.
*/
define('UPDATE_DEFAULT_URL', 'http://updates.drupal.org/release-history');
// These are internally used constants for this code, do not modify.
/**
* Project is missing security update(s).
*/
define('UPDATE_NOT_SECURE', 1);
/**
* Current release has been unpublished and is no longer available.
*/
define('UPDATE_REVOKED', 2);
/**
* Current release is no longer supported by the project maintainer.
*/
define('UPDATE_NOT_SUPPORTED', 3);
/**
* Project has a new release available, but it is not a security release.
*/
define('UPDATE_NOT_CURRENT', 4);
/**
* Project is up to date.
*/
define('UPDATE_CURRENT', 5);
/**
* Project's status cannot be checked.
*/
define('UPDATE_NOT_CHECKED', -1);
/**
* No available update data was found for project.
*/
define('UPDATE_UNKNOWN', -2);
/**
* There was a failure fetching available update data for this project.
*/
define('UPDATE_NOT_FETCHED', -3);
/**
* Maximum number of attempts to fetch available update data from a given host.
*/
define('UPDATE_MAX_FETCH_ATTEMPTS', 2);
/**
* Implementation of hook_help().
*/
function update_help($path, $arg) {
switch ($path) {
case 'admin/reports/updates':
$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>';
$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>';
return $output;
case 'admin/build/themes':
case 'admin/build/modules':
include_once './includes/install.inc';
$status = update_requirements('runtime');
if (isset($status['update_contrib']['severity'])) {
if ($status['update_contrib']['severity'] == REQUIREMENT_ERROR) {
drupal_set_message($status['update_contrib']['description'], 'error');
}
elseif ($status['update_contrib']['severity'] == REQUIREMENT_WARNING) {
drupal_set_message($status['update_contrib']['description'], 'warning');
}
}
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>';
case 'admin/reports/updates/settings':
case 'admin/reports/status':
// These two pages don't need additional nagging.
break;
case 'admin/help#update':
$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>';
$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>';
$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>';
$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>';
return $output;
default:
// Otherwise, if we're on *any* admin page and there's a security
// update missing, print an error message about it.
if (arg(0) == 'admin' && strpos($path, '#') === FALSE
&& user_access('administer site configuration')) {
include_once './includes/install.inc';
$status = update_requirements('runtime');
foreach (array('core', 'contrib') as $report_type) {
$type = 'update_'. $report_type;
if (isset($status[$type])
&& isset($status[$type]['reason'])
&& $status[$type]['reason'] === UPDATE_NOT_SECURE) {
drupal_set_message($status[$type]['description'], 'error');
}
}
}
}
}
/**
* Implementation of hook_menu().
*/
function update_menu() {
$items = array();
$items['admin/reports/updates'] = array(
'title' => 'Available updates',
'description' => 'Get a status report about available updates for your installed modules and themes.',
'page callback' => 'update_status',
'access arguments' => array('administer site configuration'),
'file' => 'update.report.inc',
'weight' => 10,
);
$items['admin/reports/updates/list'] = array(
'title' => 'List',
'page callback' => 'update_status',
'access arguments' => array('administer site configuration'),
'file' => 'update.report.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/reports/updates/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('update_settings'),
'access arguments' => array('administer site configuration'),
'file' => 'update.settings.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/reports/updates/check'] = array(
'title' => 'Manual update check',
'page callback' => 'update_manual_status',
'access arguments' => array('administer site configuration'),
'file' => 'update.fetch.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of the hook_theme() registry.
*/
function update_theme() {
return array(
'update_settings' => array(
'arguments' => array('form' => NULL),
),
'update_report' => array(
'arguments' => array('data' => NULL),
),
'update_version' => array(
'arguments' => array('version' => NULL, 'tag' => NULL, 'class' => NULL),
),
);
}
/**
* Implementation of hook_requirements().
*
* @return
* An array describing the status of the site regarding available updates.
* If there is no update data, only one record will be returned, indicating
* that the status of core can't be determined. If data is available, there
* will be two records: one for core, and another for all of contrib
* (assuming there are any contributed modules or themes enabled on the
* site). In addition to the fields expected by hook_requirements ('value',
* 'severity', and optionally 'description'), this array will contain a
* 'reason' attribute, which is an integer constant to indicate why the
* given status is being returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or
* UPDATE_UNKNOWN). This is used for generating the appropriate e-mail
* notification messages during update_cron(), and might be useful for other
* modules that invoke update_requirements() to find out if the site is up
* to date or not.
*
* @see _update_message_text()
* @see _update_cron_notify()
*/
function update_requirements($phase) {
if ($phase == 'runtime') {
if ($available = update_get_available(FALSE)) {
module_load_include('inc', 'update', 'update.compare');
$data = update_calculate_project_data($available);
// First, populate the requirements for core:
//$requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
// We don't want to check drupal a second time.
unset($data['drupal']);
if (!empty($data)) {
// Now, sort our $data array based on each project's status. The
// status constants are numbered in the right order of precedence, so
// we just need to make sure the projects are sorted in ascending
// order of status, and we can look at the first project we find.
uasort($data, '_update_project_status_sort');
$first_project = reset($data);
$requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
}
}
else {
$requirements['update_core']['title'] = t('Drupal core update status');
$requirements['update_core']['value'] = t('No update data available');
$requirements['update_core']['severity'] = REQUIREMENT_WARNING;
$requirements['update_core']['reason'] = UPDATE_UNKNOWN;
$requirements['update_core']['description'] = _update_no_data();
}
return $requirements;
}
}
/**
* Private helper method to fill in the requirements array.
*
* This is shared for both core and contrib to generate the right elements in
* the array for hook_requirements().
*
* @param $project
* Array of information about the project we're testing as returned by
* update_calculate_project_data().
* @param $type
* What kind of project is this ('core' or 'contrib').
*
* @return
* An array to be included in the nested $requirements array.
*
* @see hook_requirements()
* @see update_requirements()
* @see update_calculate_project_data()
*/
function _update_requirement_check($project, $type) {
$requirement = array();
if ($type == 'core') {
$requirement['title'] = t('Drupal core update status');
}
else {
$requirement['title'] = t('Module and theme update status');
}
$status = $project['status'];
if ($status != UPDATE_CURRENT) {
$requirement['reason'] = $status;
$requirement['description'] = _update_message_text($type, $status, TRUE);
$requirement['severity'] = REQUIREMENT_ERROR;
}
switch ($status) {
case UPDATE_NOT_SECURE:
$requirement_label = t('Not secure!');
break;
case UPDATE_REVOKED:
$requirement_label = t('Revoked!');
break;
case UPDATE_NOT_SUPPORTED:
$requirement_label = t('Unsupported release');
break;
case UPDATE_NOT_CURRENT:
$requirement_label = t('Out of date');
$requirement['severity'] = REQUIREMENT_WARNING;
break;
case UPDATE_UNKNOWN:
case UPDATE_NOT_CHECKED:
case UPDATE_NOT_FETCHED:
$requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
$requirement['severity'] = REQUIREMENT_WARNING;
break;
default:
$requirement_label = t('Up to date');
}
if ($status != UPDATE_CURRENT && $type == 'core' && isset($project['recommended'])) {
$requirement_label .= ' '. t('(version @version available)', array('@version' => $project['recommended']));
}
$requirement['value'] = l($requirement_label, 'admin/reports/updates');
return $requirement;
}
/**
* Implementation of hook_cron().
*/
function update_cron() {
$frequency = variable_get('update_check_frequency', 1);
$interval = 60 * 60 * 24 * $frequency;
// Cron should check for updates if there is no update data cached or if the
// configured update interval has elapsed.
if (!_update_cache_get('update_available_releases') || ((time() - variable_get('update_last_check', 0)) > $interval)) {
update_refresh();
_update_cron_notify();
}
}
/**
* Implementation of hook_form_alter().
*
* Adds a submit handler to the system modules and themes forms, so that if a
* site admin saves either form, we invalidate the cache of available updates.
*
* @see update_invalidate_cache()
*/
function update_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'system_modules' || $form_id == 'system_themes_form' ) {
$form['#submit'][] = 'update_invalidate_cache';
}
}
/**
* Prints a warning message when there is no data about available updates.
*/
function _update_no_data() {
$destination = drupal_get_destination();
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(
'@run_cron' => url('admin/reports/status/run-cron', array('query' => $destination)),
'@check_manually' => url('admin/reports/updates/check', array('query' => $destination)),
));
}
/**
* Internal helper to try to get the update information from the cache
* if possible, and to refresh the cache when necessary.
*
* In addition to checking the cache lifetime, this function also ensures that
* there are no .info files for enabled modules or themes that have a newer
* modification timestamp than the last time we checked for available update
* data. If any .info file was modified, it almost certainly means a new
* version of something was installed. Without fresh available update data,
* the logic in update_calculate_project_data() will be wrong and produce
* confusing, bogus results.
*
* @param $refresh
* Boolean to indicate if this method should refresh the cache automatically
* if there's no data.
*
* @see update_refresh()
* @see update_get_projects()
*/
function update_get_available($refresh = FALSE) {
module_load_include('inc', 'update', 'update.compare');
$available = array();
// First, make sure that none of the .info files have a change time
// newer than the last time we checked for available updates.
$needs_refresh = FALSE;
$last_check = variable_get('update_last_check', 0);
$projects = update_get_projects();
foreach ($projects as $key => $project) {
if ($project['info']['_info_file_ctime'] > $last_check) {
$needs_refresh = TRUE;
break;
}
}
if (!$needs_refresh && ($cache = _update_cache_get('update_available_releases')) && $cache->expire > time()) {
$available = $cache->data;
}
elseif ($needs_refresh || $refresh) {
// If we need to refresh due to a newer .info file, ignore the argument
// and force the refresh (e.g., even for update_requirements()) to prevent
// bogus results.
$available = update_refresh();
}
return $available;
}
/**
* Wrapper to load the include file and then refresh the release data.
*/
function update_refresh() {
module_load_include('inc', 'update', 'update.fetch');
return _update_refresh();
}
/**
* Implementation of hook_mail().
*
* Constructs the email notification message when the site is out of date.
*
* @param $key
* Unique key to indicate what message to build, always 'status_notify'.
* @param $message
* Reference to the message array being built.
* @param $params
* Array of parameters to indicate what kind of text to include in the
* message body. This is a keyed array of message type ('core' or 'contrib')
* as the keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for
* the values.
*
* @see drupal_mail()
* @see _update_cron_notify()
* @see _update_message_text()
*/
function update_mail($key, &$message, $params) {
$language = $message['language'];
$langcode = $language->language;
$message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => variable_get('site_name', 'Drupal')), $langcode);
foreach ($params as $msg_type => $msg_reason) {
$message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $language);
}
$message['body'][] = t('See the available updates page for more information:', array(), $langcode) ."\n". url('admin/reports/updates', array('absolute' => TRUE, 'language' => $language));
}
/**
* Helper function to return the appropriate message text when the site is out
* of date or missing a security update.
*
* These error messages are shared by both update_requirements() for the
* site-wide status report at admin/reports/status and in the body of the
* notification emails generated by update_cron().
*
* @param $msg_type
* String to indicate what kind of message to generate. Can be either
* 'core' or 'contrib'.
* @param $msg_reason
* Integer constant specifying why message is generated.
* @param $report_link
* Boolean that controls if a link to the updates report should be added.
* @param $language
* An optional language object to use.
* @return
* The properly translated error message for the given key.
*/
function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $language = NULL) {
$langcode = isset($language) ? $language->language : NULL;
$text = '';
switch ($msg_reason) {
case UPDATE_NOT_SECURE:
if ($msg_type == 'core') {
$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);
}
else {
$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);
}
break;
case UPDATE_REVOKED:
if ($msg_type == 'core') {
$text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', array(), $langcode);
}
else {
$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);
}
break;
case UPDATE_NOT_SUPPORTED:
if ($msg_type == 'core') {
$text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', array(), $langcode);
}
else {
$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);
}
break;
case UPDATE_NOT_CURRENT:
if ($msg_type == 'core') {
$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);
}
else {
$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);
}
break;
case UPDATE_UNKNOWN:
case UPDATE_NOT_CHECKED:
case UPDATE_NOT_FETCHED:
if ($msg_type == 'core') {
$text = t('There was a problem determining the status of available updates for your version of Drupal.', array(), $langcode);
}
else {
$text = t('There was a problem determining the status of available updates for one or more of your modules or themes.', array(), $langcode);
}
break;
}
if ($report_link) {
$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);
}
return $text;
}
/**
* Private sort function to order projects based on their status.
*
* @see update_requirements()
* @see uasort()
*/
function _update_project_status_sort($a, $b) {
// The status constants are numerically in the right order, so we can
// usually subtract the two to compare in the order we want. However,
// negative status values should be treated as if they are huge, since we
// always want them at the bottom of the list.
$a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
$b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
return $a_status - $b_status;
}
/**
* @defgroup update_status_cache Private update status cache system
* @{
*
* We specifically do NOT use the core cache API for saving the fetched data
* about available updates. It is vitally important that this cache is only
* cleared when we're populating it after successfully fetching new available
* update data. Usage of the core cache API results in all sorts of potential
* problems that would result in attempting to fetch available update data all
* the time, including if a site has a "minimum cache lifetime" (which is both
* a minimum and a maximum) defined, or if a site uses memcache or another
* plug-able cache system that assumes volatile caches.
*
* Update module still uses the {cache_update} table, but instead of using
* cache_set(), cache_get(), and cache_clear_all(), there are private helper
* functions that implement these same basic tasks but ensure that the cache
* is not prematurely cleared, and that the data is always stored in the
* database, even if memcache or another cache backend is in use.
*/
/**
* Store data in the private update status cache table.
*
* Note: this function completely ignores the {cache_update}.headers field
* since that is meaningless for the kinds of data we're caching.
*
* @param $cid
* The cache ID to save the data with.
* @param $data
* The data to store.
* @param $expire
* One of the following values:
* - CACHE_PERMANENT: Indicates that the item should never be removed except
* by explicitly using _update_cache_clear() or update_invalidate_cache().
* - A Unix timestamp: Indicates that the item should be kept at least until
* the given time, after which it will be invalidated.
*/
function _update_cache_set($cid, $data, $expire) {
$serialized = 0;
if (is_object($data) || is_array($data)) {
$data = serialize($data);
$serialized = 1;
}
$created = time();
db_query("UPDATE {cache_update} SET data = %b, created = %d, expire = %d, serialized = %d WHERE cid = '%s'", $data, $created, $expire, $serialized, $cid);
if (!db_affected_rows()) {
@db_query("INSERT INTO {cache_update} (cid, data, created, expire, serialized) VALUES ('%s', %b, %d, %d, %d)", $cid, $data, $created, $expire, $serialized);
}
}
/**
* Retrieve data from the private update status cache table.
*
* @param $cid
* The cache ID to retrieve.
* @return
* The data for the given cache ID, or NULL if the ID was not found.
*/
function _update_cache_get($cid) {
$cache = db_fetch_object(db_query("SELECT data, created, expire, serialized FROM {cache_update} WHERE cid = '%s'", $cid));
if (isset($cache->data)) {
$cache->data = db_decode_blob($cache->data);
if ($cache->serialized) {
$cache->data = unserialize($cache->data);
}
}
return $cache;
}
/**
* Invalidates specific cached data relating to update status.
*
* @param $cid
* Optional cache ID of the record to clear from the private update module
* cache. If empty, all records will be cleared from the table.
*/
function _update_cache_clear($cid = NULL) {
if (empty($cid)) {
db_query("TRUNCATE TABLE {cache_update}");
}
else {
db_query("DELETE FROM {cache_update} WHERE cid = '%s'", $cid);
}
}
/**
* Implementation of hook_flush_caches().
*
* Called from update.php (among others) to flush the caches.
* Since we're running update.php, we are likely to install a new version of
* something, in which case, we want to check for available update data again.
* However, because we have our own caching system, we need to directly clear
* the database table ourselves at this point and return nothing, for example,
* on sites that use memcache where cache_clear_all() won't know how to purge
* this data.
*
* However, we only want to do this from update.php, since otherwise, we'd
* lose all the available update data on every cron run. So, we specifically
* check if the site is in MAINTENANCE_MODE == 'update' (which indicates
* update.php is running, not update module... alas for overloaded names).
*/
function update_flush_caches() {
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
_update_cache_clear();
}
return array();
}
/**
* Invalidates all cached data relating to update status.
*/
function update_invalidate_cache() {
_update_cache_clear();
}
/**
* @} End of "defgroup update_status_cache".
*/
|