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