update.module

  1. drupal
    1. 6 modules/update/update.module
    2. 7 modules/update/update.module
    3. 8 core/modules/update/update.module

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.

Functions & methods

NameDescription
theme_update_last_checkReturns HTML for the last time we checked for update data.
update_cache_clear_submitHelper function for use as a form submit callback.
update_clear_update_disk_cacheClear the temporary files and directories based on file age from disk.
update_create_fetch_taskWrapper to load the include file and then create a new fetch task.
update_cronImplements hook_cron().
update_delete_file_if_staleDelete stale files and directories from the Update manager disk cache.
update_fetch_dataWrapper to load the include file and then attempt to fetch update data.
update_flush_cachesImplements hook_flush_caches().
update_form_system_modules_alterImplements hook_form_FORM_ID_alter().
update_get_availableInternal helper to try to get the update information from the cache if possible, and to refresh the cache when necessary.
update_helpImplements hook_help().
update_initImplements hook_init().
update_mailImplements hook_mail().
update_manager_accessDetermine if the current user can access the updater menu items.
update_menuImplements hook_menu().
update_refreshWrapper to load the include file and then refresh the release data.
update_themeImplements hook_theme().
update_themes_disabledImplements hook_themes_disabled().
update_themes_enabledImplements hook_themes_enabled().
update_verify_update_archiveImplements hook_verify_update_archive().
_update_cache_clearInvalidates cached data relating to update status.
_update_cache_getRetrieve data from the private update status cache table.
_update_cache_setStore data in the private update status cache table.
_update_get_cached_available_releasesReturn all currently cached data about available releases for all projects.
_update_get_cache_multipleReturn an array of cache items with a given cache ID prefix.
_update_manager_cache_directoryReturn the directory where update archive files should be cached.
_update_manager_extract_directoryReturn the directory where update archive files should be extracted.
_update_manager_unique_identifierReturn a short unique identifier for this Drupal installation.
_update_message_textHelper function to return the appropriate message text when the site is out of date or missing a security update.
_update_no_dataPrints a warning message when there is no data about available updates.
_update_project_status_sortPrivate sort function to order projects based on their status.

Constants

NameDescription
UPDATE_CURRENTProject is up to date.
UPDATE_DEFAULT_URLURL to check for updates, if a given project doesn't define its own.
UPDATE_FETCH_PENDINGWe need to (re)fetch available update data for this project.
UPDATE_MAX_FETCH_ATTEMPTSMaximum number of attempts to fetch available update data from a given host.
UPDATE_MAX_FETCH_TIMEMaximum number of seconds to try fetching available update data at a time.
UPDATE_NOT_CHECKEDProject's status cannot be checked.
UPDATE_NOT_CURRENTProject has a new release available, but it is not a security release.
UPDATE_NOT_FETCHEDThere was a failure fetching available update data for this project.
UPDATE_NOT_SECUREProject is missing security update(s).
UPDATE_NOT_SUPPORTEDCurrent release is no longer supported by the project maintainer.
UPDATE_REVOKEDCurrent release has been unpublished and is no longer available.
UPDATE_UNKNOWNNo available update data was found for project.

File

modules/update/update.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * The "Update status" module checks for available updates of Drupal core and
  5. * any installed contributed modules and themes. It warns site administrators
  6. * if newer releases are available via the system status report
  7. * (admin/reports/status), the module and theme pages, and optionally via email.
  8. */
  9. /**
  10. * URL to check for updates, if a given project doesn't define its own.
  11. */
  12. define('UPDATE_DEFAULT_URL', 'http://updates.drupal.org/release-history');
  13. // These are internally used constants for this code, do not modify.
  14. /**
  15. * Project is missing security update(s).
  16. */
  17. define('UPDATE_NOT_SECURE', 1);
  18. /**
  19. * Current release has been unpublished and is no longer available.
  20. */
  21. define('UPDATE_REVOKED', 2);
  22. /**
  23. * Current release is no longer supported by the project maintainer.
  24. */
  25. define('UPDATE_NOT_SUPPORTED', 3);
  26. /**
  27. * Project has a new release available, but it is not a security release.
  28. */
  29. define('UPDATE_NOT_CURRENT', 4);
  30. /**
  31. * Project is up to date.
  32. */
  33. define('UPDATE_CURRENT', 5);
  34. /**
  35. * Project's status cannot be checked.
  36. */
  37. define('UPDATE_NOT_CHECKED', -1);
  38. /**
  39. * No available update data was found for project.
  40. */
  41. define('UPDATE_UNKNOWN', -2);
  42. /**
  43. * There was a failure fetching available update data for this project.
  44. */
  45. define('UPDATE_NOT_FETCHED', -3);
  46. /**
  47. * We need to (re)fetch available update data for this project.
  48. */
  49. define('UPDATE_FETCH_PENDING', -4);
  50. /**
  51. * Maximum number of attempts to fetch available update data from a given host.
  52. */
  53. define('UPDATE_MAX_FETCH_ATTEMPTS', 2);
  54. /**
  55. * Maximum number of seconds to try fetching available update data at a time.
  56. */
  57. define('UPDATE_MAX_FETCH_TIME', 5);
  58. /**
  59. * Implements hook_help().
  60. */
  61. function update_help($path, $arg) {
  62. switch ($path) {
  63. case 'admin/reports/updates':
  64. return '<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>';
  65. case 'admin/help#update':
  66. $output = '';
  67. $output .= '<h3>' . t('About') . '</h3>';
  68. $output .= '<p>' . t("The Update manager module periodically checks for new versions of your site's software (including contributed modules and themes), and alerts administrators to available updates. In order to provide update information, anonymous usage statistics are sent to Drupal.org. If desired, you may disable the Update manager module from the <a href='@modules'>Module administration page</a>. For more information, see the online handbook entry for <a href='@update'>Update manager module</a>.", array('@update' => 'http://drupal.org/documentation/modules/update', '@modules' => url('admin/modules'))) . '</p>';
  69. // Only explain the Update manager if it has not been disabled.
  70. if (update_manager_access()) {
  71. $output .= '<p>' . t('The Update manager also allows administrators to update and install modules and themes through the administration interface.') . '</p>';
  72. }
  73. $output .= '<h3>' . t('Uses') . '</h3>';
  74. $output .= '<dl>';
  75. $output .= '<dt>' . t('Checking for available updates') . '</dt>';
  76. $output .= '<dd>' . t('A report of <a href="@update-report">available updates</a> will alert you when new releases are available for download. You may configure options for the frequency for checking updates (which are performed during <a href="@cron">cron</a> runs) and e-mail notifications at the <a href="@update-settings">Update manager settings</a> page.', array('@update-report' => url('admin/reports/updates'), '@cron' => 'http://drupal.org/cron', '@update-settings' => url('admin/reports/updates/settings'))) . '</dd>';
  77. // Only explain the Update manager if it has not been disabled.
  78. if (update_manager_access()) {
  79. $output .= '<dt>' . t('Performing updates through the user interface') . '</dt>';
  80. $output .= '<dd>' . t('The Update manager module allows administrators to perform updates directly through the administration interface. At the top of the <a href="@modules_page">modules</a> and <a href="@themes_page">themes</a> pages you will see a link to update to new releases. This will direct you to the <a href="@update-page">update page</a> where you see a listing of all the missing updates and confirm which ones you want to upgrade. From there, you are prompted for your FTP/SSH password, which then transfers the files into your Drupal installation, overwriting your old files. More detailed instructions can be found in the <a href="@update">online handbook</a>.', array('@modules_page' => url('admin/modules'), '@themes_page' => url('admin/appearance'), '@update-page' => url('admin/reports/updates/update'), '@update' => 'http://drupal.org/documentation/modules/update')) . '</dd>';
  81. $output .= '<dt>' . t('Installing new modules and themes through the user interface') . '</dt>';
  82. $output .= '<dd>' . t('You can also install new modules and themes in the same fashion, through the <a href="@install">install page</a>, or by clicking the <em>Install new module/theme</em> link at the top of the <a href="@modules_page">modules</a> and <a href="@themes_page">themes</a> pages. In this case, you are prompted to provide either the URL to the download, or to upload a packaged release file from your local computer.', array('@modules_page' => url('admin/modules'), '@themes_page' => url('admin/appearance'), '@install' => url('admin/reports/updates/install'))) . '</dd>';
  83. }
  84. $output .= '</dl>';
  85. return $output;
  86. }
  87. }
  88. /**
  89. * Implements hook_init().
  90. */
  91. function update_init() {
  92. if (arg(0) == 'admin' && user_access('administer site configuration')) {
  93. switch ($_GET['q']) {
  94. // These pages don't need additional nagging.
  95. case 'admin/appearance/update':
  96. case 'admin/appearance/install':
  97. case 'admin/modules/update':
  98. case 'admin/modules/install':
  99. case 'admin/reports/updates':
  100. case 'admin/reports/updates/update':
  101. case 'admin/reports/updates/install':
  102. case 'admin/reports/updates/settings':
  103. case 'admin/reports/status':
  104. case 'admin/update/ready':
  105. return;
  106. // If we are on the appearance or modules list, display a detailed report
  107. // of the update status.
  108. case 'admin/appearance':
  109. case 'admin/modules':
  110. $verbose = TRUE;
  111. break;
  112. }
  113. module_load_install('update');
  114. $status = update_requirements('runtime');
  115. foreach (array('core', 'contrib') as $report_type) {
  116. $type = 'update_' . $report_type;
  117. if (!empty($verbose)) {
  118. if (isset($status[$type]['severity'])) {
  119. if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
  120. drupal_set_message($status[$type]['description'], 'error');
  121. }
  122. elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
  123. drupal_set_message($status[$type]['description'], 'warning');
  124. }
  125. }
  126. }
  127. // Otherwise, if we're on *any* admin page and there's a security
  128. // update missing, print an error message about it.
  129. else {
  130. if (isset($status[$type])
  131. && isset($status[$type]['reason'])
  132. && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
  133. drupal_set_message($status[$type]['description'], 'error');
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * Implements hook_menu().
  141. */
  142. function update_menu() {
  143. $items = array();
  144. $items['admin/reports/updates'] = array(
  145. 'title' => 'Available updates',
  146. 'description' => 'Get a status report about available updates for your installed modules and themes.',
  147. 'page callback' => 'update_status',
  148. 'access arguments' => array('administer site configuration'),
  149. 'weight' => -50,
  150. 'file' => 'update.report.inc',
  151. );
  152. $items['admin/reports/updates/list'] = array(
  153. 'title' => 'List',
  154. 'access arguments' => array('administer site configuration'),
  155. 'type' => MENU_DEFAULT_LOCAL_TASK,
  156. );
  157. $items['admin/reports/updates/settings'] = array(
  158. 'title' => 'Settings',
  159. 'page callback' => 'drupal_get_form',
  160. 'page arguments' => array('update_settings'),
  161. 'access arguments' => array('administer site configuration'),
  162. 'file' => 'update.settings.inc',
  163. 'type' => MENU_LOCAL_TASK,
  164. 'weight' => 50,
  165. );
  166. $items['admin/reports/updates/check'] = array(
  167. 'title' => 'Manual update check',
  168. 'page callback' => 'update_manual_status',
  169. 'access arguments' => array('administer site configuration'),
  170. 'type' => MENU_CALLBACK,
  171. 'file' => 'update.fetch.inc',
  172. );
  173. // We want action links for updating projects at a few different locations:
  174. // both the module and theme administration pages, and on the available
  175. // updates report itself. The menu items will be mostly identical, except the
  176. // paths and titles, so we just define them in a loop. We pass in a string
  177. // indicating what context we're entering the action from, so that can
  178. // customize the appearance as needed.
  179. $paths = array(
  180. 'report' => 'admin/reports/updates',
  181. 'module' => 'admin/modules',
  182. 'theme' => 'admin/appearance',
  183. );
  184. foreach ($paths as $context => $path) {
  185. $items[$path . '/install'] = array(
  186. 'page callback' => 'drupal_get_form',
  187. 'page arguments' => array('update_manager_install_form', $context),
  188. 'access callback' => 'update_manager_access',
  189. 'access arguments' => array(),
  190. 'weight' => 25,
  191. 'type' => MENU_LOCAL_ACTION,
  192. 'file' => 'update.manager.inc',
  193. );
  194. $items[$path . '/update'] = array(
  195. 'page callback' => 'drupal_get_form',
  196. 'page arguments' => array('update_manager_update_form', $context),
  197. 'access callback' => 'update_manager_access',
  198. 'access arguments' => array(),
  199. 'weight' => 10,
  200. 'title' => 'Update',
  201. 'type' => MENU_LOCAL_TASK,
  202. 'file' => 'update.manager.inc',
  203. );
  204. }
  205. // Customize the titles of the action links depending on where they appear.
  206. // We use += array() to let the translation extractor find these menu titles.
  207. $items['admin/reports/updates/install'] += array('title' => 'Install new module or theme');
  208. $items['admin/modules/install'] += array('title' => 'Install new module');
  209. $items['admin/appearance/install'] += array('title' => 'Install new theme');
  210. // Menu callback used for the confirmation page after all the releases
  211. // have been downloaded, asking you to backup before installing updates.
  212. $items['admin/update/ready'] = array(
  213. 'title' => 'Ready to update',
  214. 'page callback' => 'drupal_get_form',
  215. 'page arguments' => array('update_manager_update_ready_form'),
  216. 'access callback' => 'update_manager_access',
  217. 'access arguments' => array(),
  218. 'type' => MENU_CALLBACK,
  219. 'file' => 'update.manager.inc',
  220. );
  221. return $items;
  222. }
  223. /**
  224. * Determine if the current user can access the updater menu items.
  225. *
  226. * This is used as a menu system access callback. It both enforces the
  227. * 'administer software updates' permission and the global killswitch for the
  228. * authorize.php script.
  229. *
  230. * @see update_menu()
  231. */
  232. function update_manager_access() {
  233. return variable_get('allow_authorize_operations', TRUE) && user_access('administer software updates');
  234. }
  235. /**
  236. * Implements hook_theme().
  237. */
  238. function update_theme() {
  239. return array(
  240. 'update_manager_update_form' => array(
  241. 'render element' => 'form',
  242. 'file' => 'update.manager.inc',
  243. ),
  244. 'update_last_check' => array(
  245. 'variables' => array('last' => NULL),
  246. ),
  247. 'update_report' => array(
  248. 'variables' => array('data' => NULL),
  249. ),
  250. 'update_version' => array(
  251. 'variables' => array('version' => NULL, 'tag' => NULL, 'class' => array()),
  252. ),
  253. 'update_status_label' => array(
  254. 'variables' => array('status' => NULL),
  255. ),
  256. );
  257. }
  258. /**
  259. * Implements hook_cron().
  260. */
  261. function update_cron() {
  262. $frequency = variable_get('update_check_frequency', 1);
  263. $interval = 60 * 60 * 24 * $frequency;
  264. if ((REQUEST_TIME - variable_get('update_last_check', 0)) > $interval) {
  265. // If the configured update interval has elapsed, we want to invalidate
  266. // the cached data for all projects, attempt to re-fetch, and trigger any
  267. // configured notifications about the new status.
  268. update_refresh();
  269. update_fetch_data();
  270. _update_cron_notify();
  271. }
  272. else {
  273. // Otherwise, see if any individual projects are now stale or still
  274. // missing data, and if so, try to fetch the data.
  275. update_get_available(TRUE);
  276. }
  277. // Clear garbage from disk.
  278. update_clear_update_disk_cache();
  279. }
  280. /**
  281. * Implements hook_themes_enabled().
  282. *
  283. * If themes are enabled, we invalidate the cache of available updates.
  284. */
  285. function update_themes_enabled($themes) {
  286. // Clear all update module caches.
  287. _update_cache_clear();
  288. }
  289. /**
  290. * Implements hook_themes_disabled().
  291. *
  292. * If themes are disabled, we invalidate the cache of available updates.
  293. */
  294. function update_themes_disabled($themes) {
  295. // Clear all update module caches.
  296. _update_cache_clear();
  297. }
  298. /**
  299. * Implements hook_form_FORM_ID_alter().
  300. *
  301. * Adds a submit handler to the system modules form, so that if a site admin
  302. * saves the form, we invalidate the cache of available updates.
  303. *
  304. * @see _update_cache_clear()
  305. */
  306. function update_form_system_modules_alter(&$form, $form_state) {
  307. $form['#submit'][] = 'update_cache_clear_submit';
  308. }
  309. /**
  310. * Helper function for use as a form submit callback.
  311. */
  312. function update_cache_clear_submit($form, &$form_state) {
  313. // Clear all update module caches.
  314. _update_cache_clear();
  315. }
  316. /**
  317. * Prints a warning message when there is no data about available updates.
  318. */
  319. function _update_no_data() {
  320. $destination = drupal_get_destination();
  321. return t('No update information available. <a href="@run_cron">Run cron</a> or <a href="@check_manually">check manually</a>.', array(
  322. '@run_cron' => url('admin/reports/status/run-cron', array('query' => $destination)),
  323. '@check_manually' => url('admin/reports/updates/check', array('query' => $destination)),
  324. ));
  325. }
  326. /**
  327. * Internal helper to try to get the update information from the cache
  328. * if possible, and to refresh the cache when necessary.
  329. *
  330. * In addition to checking the cache lifetime, this function also ensures that
  331. * there are no .info files for enabled modules or themes that have a newer
  332. * modification timestamp than the last time we checked for available update
  333. * data. If any .info file was modified, it almost certainly means a new
  334. * version of something was installed. Without fresh available update data,
  335. * the logic in update_calculate_project_data() will be wrong and produce
  336. * confusing, bogus results.
  337. *
  338. * @param $refresh
  339. * Boolean to indicate if this method should refresh the cache automatically
  340. * if there's no data.
  341. *
  342. * @see update_refresh()
  343. * @see update_get_projects()
  344. */
  345. function update_get_available($refresh = FALSE) {
  346. module_load_include('inc', 'update', 'update.compare');
  347. $needs_refresh = FALSE;
  348. // Grab whatever data we currently have cached in the DB.
  349. $available = _update_get_cached_available_releases();
  350. $num_avail = count($available);
  351. $projects = update_get_projects();
  352. foreach ($projects as $key => $project) {
  353. // If there's no data at all, we clearly need to fetch some.
  354. if (empty($available[$key])) {
  355. update_create_fetch_task($project);
  356. $needs_refresh = TRUE;
  357. continue;
  358. }
  359. // See if the .info file is newer than the last time we checked for data,
  360. // and if so, mark this project's data as needing to be re-fetched. Any
  361. // time an admin upgrades their local installation, the .info file will
  362. // be changed, so this is the only way we can be sure we're not showing
  363. // bogus information right after they upgrade.
  364. if ($project['info']['_info_file_ctime'] > $available[$key]['last_fetch']) {
  365. $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
  366. }
  367. // If we have project data but no release data, we need to fetch. This
  368. // can be triggered when we fail to contact a release history server.
  369. if (empty($available[$key]['releases'])) {
  370. $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
  371. }
  372. // If we think this project needs to fetch, actually create the task now
  373. // and remember that we think we're missing some data.
  374. if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) {
  375. update_create_fetch_task($project);
  376. $needs_refresh = TRUE;
  377. }
  378. }
  379. if ($needs_refresh && $refresh) {
  380. // Attempt to drain the queue of fetch tasks.
  381. update_fetch_data();
  382. // After processing the queue, we've (hopefully) got better data, so pull
  383. // the latest from the cache again and use that directly.
  384. $available = _update_get_cached_available_releases();
  385. }
  386. return $available;
  387. }
  388. /**
  389. * Wrapper to load the include file and then create a new fetch task.
  390. *
  391. * @see _update_create_fetch_task()
  392. */
  393. function update_create_fetch_task($project) {
  394. module_load_include('inc', 'update', 'update.fetch');
  395. return _update_create_fetch_task($project);
  396. }
  397. /**
  398. * Wrapper to load the include file and then refresh the release data.
  399. *
  400. * @see _update_refresh()
  401. */
  402. function update_refresh() {
  403. module_load_include('inc', 'update', 'update.fetch');
  404. return _update_refresh();
  405. }
  406. /**
  407. * Wrapper to load the include file and then attempt to fetch update data.
  408. */
  409. function update_fetch_data() {
  410. module_load_include('inc', 'update', 'update.fetch');
  411. return _update_fetch_data();
  412. }
  413. /**
  414. * Return all currently cached data about available releases for all projects.
  415. *
  416. * @return
  417. * Array of data about available releases, keyed by project shortname.
  418. */
  419. function _update_get_cached_available_releases() {
  420. $data = array();
  421. $cache_items = _update_get_cache_multiple('available_releases');
  422. foreach ($cache_items as $cid => $cache) {
  423. $cache->data['last_fetch'] = $cache->created;
  424. if ($cache->expire < REQUEST_TIME) {
  425. $cache->data['fetch_status'] = UPDATE_FETCH_PENDING;
  426. }
  427. // The project shortname is embedded in the cache ID, even if there's no
  428. // data for this project in the DB at all, so use that for the indexes in
  429. // the array.
  430. $parts = explode('::', $cid, 2);
  431. $data[$parts[1]] = $cache->data;
  432. }
  433. return $data;
  434. }
  435. /**
  436. * Implements hook_mail().
  437. *
  438. * Constructs the email notification message when the site is out of date.
  439. *
  440. * @param $key
  441. * Unique key to indicate what message to build, always 'status_notify'.
  442. * @param $message
  443. * Reference to the message array being built.
  444. * @param $params
  445. * Array of parameters to indicate what kind of text to include in the
  446. * message body. This is a keyed array of message type ('core' or 'contrib')
  447. * as the keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for
  448. * the values.
  449. *
  450. * @see drupal_mail()
  451. * @see _update_cron_notify()
  452. * @see _update_message_text()
  453. */
  454. function update_mail($key, &$message, $params) {
  455. $language = $message['language'];
  456. $langcode = $language->language;
  457. $message['subject'] .= t('New release(s) available for !site_name', array('!site_name' => variable_get('site_name', 'Drupal')), array('langcode' => $langcode));
  458. foreach ($params as $msg_type => $msg_reason) {
  459. $message['body'][] = _update_message_text($msg_type, $msg_reason, FALSE, $language);
  460. }
  461. $message['body'][] = t('See the available updates page for more information:', array(), array('langcode' => $langcode)) . "\n" . url('admin/reports/updates', array('absolute' => TRUE, 'language' => $language));
  462. if (update_manager_access()) {
  463. $message['body'][] = t('You can automatically install your missing updates using the Update manager:', array(), array('langcode' => $langcode)) . "\n" . url('admin/reports/updates/update', array('absolute' => TRUE, 'language' => $language));
  464. }
  465. $settings_url = url('admin/reports/updates/settings', array('absolute' => TRUE));
  466. if (variable_get('update_notification_threshold', 'all') == 'all') {
  467. $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, !url.', array('!url' => $settings_url));
  468. }
  469. else {
  470. $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, !url.', array('!url' => $settings_url));
  471. }
  472. }
  473. /**
  474. * Helper function to return the appropriate message text when the site is out
  475. * of date or missing a security update.
  476. *
  477. * These error messages are shared by both update_requirements() for the
  478. * site-wide status report at admin/reports/status and in the body of the
  479. * notification emails generated by update_cron().
  480. *
  481. * @param $msg_type
  482. * String to indicate what kind of message to generate. Can be either
  483. * 'core' or 'contrib'.
  484. * @param $msg_reason
  485. * Integer constant specifying why message is generated.
  486. * @param $report_link
  487. * Boolean that controls if a link to the updates report should be added.
  488. * @param $language
  489. * An optional language object to use.
  490. * @return
  491. * The properly translated error message for the given key.
  492. */
  493. function _update_message_text($msg_type, $msg_reason, $report_link = FALSE, $language = NULL) {
  494. $langcode = isset($language) ? $language->language : NULL;
  495. $text = '';
  496. switch ($msg_reason) {
  497. case UPDATE_NOT_SECURE:
  498. if ($msg_type == 'core') {
  499. $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(), array('langcode' => $langcode));
  500. }
  501. else {
  502. $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(), array('langcode' => $langcode));
  503. }
  504. break;
  505. case UPDATE_REVOKED:
  506. if ($msg_type == 'core') {
  507. $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', array(), array('langcode' => $langcode));
  508. }
  509. else {
  510. $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(), array('langcode' => $langcode));
  511. }
  512. break;
  513. case UPDATE_NOT_SUPPORTED:
  514. if ($msg_type == 'core') {
  515. $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', array(), array('langcode' => $langcode));
  516. }
  517. else {
  518. $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or disabling is strongly recommended. See the project homepage for more details.', array(), array('langcode' => $langcode));
  519. }
  520. break;
  521. case UPDATE_NOT_CURRENT:
  522. if ($msg_type == 'core') {
  523. $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(), array('langcode' => $langcode));
  524. }
  525. else {
  526. $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(), array('langcode' => $langcode));
  527. }
  528. break;
  529. case UPDATE_UNKNOWN:
  530. case UPDATE_NOT_CHECKED:
  531. case UPDATE_NOT_FETCHED:
  532. case UPDATE_FETCH_PENDING:
  533. if ($msg_type == 'core') {
  534. $text = t('There was a problem checking <a href="@update-report">available updates</a> for Drupal.', array('@update-report' => url('admin/reports/updates')), array('langcode' => $langcode));
  535. }
  536. else {
  537. $text = t('There was a problem checking <a href="@update-report">available updates</a> for your modules or themes.', array('@update-report' => url('admin/reports/updates')), array('langcode' => $langcode));
  538. }
  539. break;
  540. }
  541. if ($report_link) {
  542. if (update_manager_access()) {
  543. $text .= ' ' . t('See the <a href="@available_updates">available updates</a> page for more information and to install your missing updates.', array('@available_updates' => url('admin/reports/updates/update', array('language' => $language))), array('langcode' => $langcode));
  544. }
  545. else {
  546. $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))), array('langcode' => $langcode));
  547. }
  548. }
  549. return $text;
  550. }
  551. /**
  552. * Private sort function to order projects based on their status.
  553. *
  554. * @see update_requirements()
  555. * @see uasort()
  556. */
  557. function _update_project_status_sort($a, $b) {
  558. // The status constants are numerically in the right order, so we can
  559. // usually subtract the two to compare in the order we want. However,
  560. // negative status values should be treated as if they are huge, since we
  561. // always want them at the bottom of the list.
  562. $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
  563. $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
  564. return $a_status - $b_status;
  565. }
  566. /**
  567. * Returns HTML for the last time we checked for update data.
  568. *
  569. * In addition to properly formating the given timestamp, this function also
  570. * provides a "Check manually" link that refreshes the available update and
  571. * redirects back to the same page.
  572. *
  573. * @param $variables
  574. * An associative array containing:
  575. * - 'last': The timestamp when the site last checked for available updates.
  576. *
  577. * @see theme_update_report()
  578. * @see theme_update_available_updates_form()
  579. *
  580. * @ingroup themeable
  581. */
  582. function theme_update_last_check($variables) {
  583. $last = $variables['last'];
  584. $output = '<div class="update checked">';
  585. $output .= $last ? t('Last checked: @time ago', array('@time' => format_interval(REQUEST_TIME - $last))) : t('Last checked: never');
  586. $output .= ' <span class="check-manually">(' . l(t('Check manually'), 'admin/reports/updates/check', array('query' => drupal_get_destination())) . ')</span>';
  587. $output .= "</div>\n";
  588. return $output;
  589. }
  590. /**
  591. * Implements hook_verify_update_archive().
  592. *
  593. * First, we ensure that the archive isn't a copy of Drupal core, which the
  594. * Update manager does not yet support. @see http://drupal.org/node/606592
  595. *
  596. * Then, we make sure that at least one module included in the archive file has
  597. * an .info file which claims that the code is compatible with the current
  598. * version of Drupal core.
  599. *
  600. * @see drupal_system_listing()
  601. * @see _system_rebuild_module_data()
  602. */
  603. function update_verify_update_archive($project, $archive_file, $directory) {
  604. $errors = array();
  605. // Make sure this isn't a tarball of Drupal core.
  606. if (
  607. file_exists("$directory/$project/index.php")
  608. && file_exists("$directory/$project/update.php")
  609. && file_exists("$directory/$project/includes/bootstrap.inc")
  610. && file_exists("$directory/$project/modules/node/node.module")
  611. && file_exists("$directory/$project/modules/system/system.module")
  612. ) {
  613. return array(
  614. 'no-core' => t('Automatic updating of Drupal core is not supported. See the <a href="@upgrade-guide">upgrade guide</a> for information on how to update Drupal core manually.', array('@upgrade-guide' => 'http://drupal.org/upgrade')),
  615. );
  616. }
  617. // Parse all the .info files and make sure at least one is compatible with
  618. // this version of Drupal core. If one is compatible, then the project as a
  619. // whole is considered compatible (since, for example, the project may ship
  620. // with some out-of-date modules that are not necessary for its overall
  621. // functionality).
  622. $compatible_project = FALSE;
  623. $incompatible = array();
  624. $files = file_scan_directory("$directory/$project", '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info$/', array('key' => 'name', 'min_depth' => 0));
  625. foreach ($files as $key => $file) {
  626. // Get the .info file for the module or theme this file belongs to.
  627. $info = drupal_parse_info_file($file->uri);
  628. // If the module or theme is incompatible with Drupal core, set an error.
  629. if (empty($info['core']) || $info['core'] != DRUPAL_CORE_COMPATIBILITY) {
  630. $incompatible[] = !empty($info['name']) ? $info['name'] : t('Unknown');
  631. }
  632. else {
  633. $compatible_project = TRUE;
  634. break;
  635. }
  636. }
  637. if (empty($files)) {
  638. $errors[] = t('%archive_file does not contain any .info files.', array('%archive_file' => drupal_basename($archive_file)));
  639. }
  640. elseif (!$compatible_project) {
  641. $errors[] = format_plural(
  642. count($incompatible),
  643. '%archive_file contains a version of %names that is not compatible with Drupal !version.',
  644. '%archive_file contains versions of modules or themes that are not compatible with Drupal !version: %names',
  645. array('!version' => DRUPAL_CORE_COMPATIBILITY, '%archive_file' => drupal_basename($archive_file), '%names' => implode(', ', $incompatible))
  646. );
  647. }
  648. return $errors;
  649. }
  650. /**
  651. * @defgroup update_status_cache Private update status cache system
  652. * @{
  653. * Functions to manage the update status cache.
  654. *
  655. * We specifically do NOT use the core cache API for saving the fetched data
  656. * about available updates. It is vitally important that this cache is only
  657. * cleared when we're populating it after successfully fetching new available
  658. * update data. Usage of the core cache API results in all sorts of potential
  659. * problems that would result in attempting to fetch available update data all
  660. * the time, including if a site has a "minimum cache lifetime" (which is both
  661. * a minimum and a maximum) defined, or if a site uses memcache or another
  662. * plug-able cache system that assumes volatile caches.
  663. *
  664. * Update module still uses the {cache_update} table, but instead of using
  665. * cache_set(), cache_get(), and cache_clear_all(), there are private helper
  666. * functions that implement these same basic tasks but ensure that the cache
  667. * is not prematurely cleared, and that the data is always stored in the
  668. * database, even if memcache or another cache backend is in use.
  669. */
  670. /**
  671. * Store data in the private update status cache table.
  672. *
  673. * @param $cid
  674. * The cache ID to save the data with.
  675. * @param $data
  676. * The data to store.
  677. * @param $expire
  678. * One of the following values:
  679. * - CACHE_PERMANENT: Indicates that the item should never be removed except
  680. * by explicitly using _update_cache_clear().
  681. * - A Unix timestamp: Indicates that the item should be kept at least until
  682. * the given time, after which it will be invalidated.
  683. */
  684. function _update_cache_set($cid, $data, $expire) {
  685. $fields = array(
  686. 'created' => REQUEST_TIME,
  687. 'expire' => $expire,
  688. );
  689. if (!is_string($data)) {
  690. $fields['data'] = serialize($data);
  691. $fields['serialized'] = 1;
  692. }
  693. else {
  694. $fields['data'] = $data;
  695. $fields['serialized'] = 0;
  696. }
  697. db_merge('cache_update')
  698. ->key(array('cid' => $cid))
  699. ->fields($fields)
  700. ->execute();
  701. }
  702. /**
  703. * Retrieve data from the private update status cache table.
  704. *
  705. * @param $cid
  706. * The cache ID to retrieve.
  707. * @return
  708. * The data for the given cache ID, or NULL if the ID was not found.
  709. */
  710. function _update_cache_get($cid) {
  711. $cache = db_query("SELECT data, created, expire, serialized FROM {cache_update} WHERE cid = :cid", array(':cid' => $cid))->fetchObject();
  712. if (isset($cache->data)) {
  713. if ($cache->serialized) {
  714. $cache->data = unserialize($cache->data);
  715. }
  716. }
  717. return $cache;
  718. }
  719. /**
  720. * Return an array of cache items with a given cache ID prefix.
  721. *
  722. * @return
  723. * Associative array of cache items, keyed by cache ID.
  724. */
  725. function _update_get_cache_multiple($cid_prefix) {
  726. $data = array();
  727. $result = db_select('cache_update')
  728. ->fields('cache_update', array('cid', 'data', 'created', 'expire', 'serialized'))
  729. ->condition('cache_update.cid', $cid_prefix . '::%', 'LIKE')
  730. ->execute();
  731. foreach ($result as $cache) {
  732. if ($cache) {
  733. if ($cache->serialized) {
  734. $cache->data = unserialize($cache->data);
  735. }
  736. $data[$cache->cid] = $cache;
  737. }
  738. }
  739. return $data;
  740. }
  741. /**
  742. * Invalidates cached data relating to update status.
  743. *
  744. * @param $cid
  745. * Optional cache ID of the record to clear from the private update module
  746. * cache. If empty, all records will be cleared from the table except
  747. * fetch tasks.
  748. * @param $wildcard
  749. * If $wildcard is TRUE, cache IDs starting with $cid are deleted in
  750. * addition to the exact cache ID specified by $cid.
  751. */
  752. function _update_cache_clear($cid = NULL, $wildcard = FALSE) {
  753. if (empty($cid)) {
  754. db_delete('cache_update')
  755. // Clear everything except fetch task information because these are used
  756. // to ensure that the fetch task queue items are not added multiple times.
  757. ->condition('cid', 'fetch_task::%', 'NOT LIKE')
  758. ->execute();
  759. }
  760. else {
  761. $query = db_delete('cache_update');
  762. if ($wildcard) {
  763. $query->condition('cid', $cid . '%', 'LIKE');
  764. }
  765. else {
  766. $query->condition('cid', $cid);
  767. }
  768. $query->execute();
  769. }
  770. }
  771. /**
  772. * Implements hook_flush_caches().
  773. *
  774. * Called from update.php (among others) to flush the caches.
  775. * Since we're running update.php, we are likely to install a new version of
  776. * something, in which case, we want to check for available update data again.
  777. * However, because we have our own caching system, we need to directly clear
  778. * the database table ourselves at this point and return nothing, for example,
  779. * on sites that use memcache where cache_clear_all() won't know how to purge
  780. * this data.
  781. *
  782. * However, we only want to do this from update.php, since otherwise, we'd
  783. * lose all the available update data on every cron run. So, we specifically
  784. * check if the site is in MAINTENANCE_MODE == 'update' (which indicates
  785. * update.php is running, not update module... alas for overloaded names).
  786. */
  787. function update_flush_caches() {
  788. if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
  789. _update_cache_clear();
  790. }
  791. return array();
  792. }
  793. /**
  794. * @} End of "defgroup update_status_cache".
  795. */
  796. /**
  797. * Return a short unique identifier for this Drupal installation.
  798. *
  799. * @return
  800. * An eight character string uniquely identifying this Drupal installation.
  801. */
  802. function _update_manager_unique_identifier() {
  803. $id = &drupal_static(__FUNCTION__, '');
  804. if (empty($id)) {
  805. $id = substr(hash('sha256', drupal_get_hash_salt()), 0, 8);
  806. }
  807. return $id;
  808. }
  809. /**
  810. * Return the directory where update archive files should be extracted.
  811. *
  812. * @param $create
  813. * If TRUE, attempt to create the directory if it does not already exist.
  814. *
  815. * @return
  816. * The full path to the temporary directory where update file archives
  817. * should be extracted.
  818. */
  819. function _update_manager_extract_directory($create = TRUE) {
  820. $directory = &drupal_static(__FUNCTION__, '');
  821. if (empty($directory)) {
  822. $directory = 'temporary://update-extraction-' . _update_manager_unique_identifier();
  823. if ($create && !file_exists($directory)) {
  824. mkdir($directory);
  825. }
  826. }
  827. return $directory;
  828. }
  829. /**
  830. * Return the directory where update archive files should be cached.
  831. *
  832. * @param $create
  833. * If TRUE, attempt to create the directory if it does not already exist.
  834. *
  835. * @return
  836. * The full path to the temporary directory where update file archives
  837. * should be cached.
  838. */
  839. function _update_manager_cache_directory($create = TRUE) {
  840. $directory = &drupal_static(__FUNCTION__, '');
  841. if (empty($directory)) {
  842. $directory = 'temporary://update-cache-' . _update_manager_unique_identifier();
  843. if ($create && !file_exists($directory)) {
  844. mkdir($directory);
  845. }
  846. }
  847. return $directory;
  848. }
  849. /**
  850. * Clear the temporary files and directories based on file age from disk.
  851. */
  852. function update_clear_update_disk_cache() {
  853. // List of update module cache directories. Do not create the directories if
  854. // they do not exist.
  855. $directories = array(
  856. _update_manager_cache_directory(FALSE),
  857. _update_manager_extract_directory(FALSE),
  858. );
  859. // Search for files and directories in base folder only without recursion.
  860. foreach ($directories as $directory) {
  861. file_scan_directory($directory, '/.*/', array('callback' => 'update_delete_file_if_stale', 'recurse' => FALSE));
  862. }
  863. }
  864. /**
  865. * Delete stale files and directories from the Update manager disk cache.
  866. *
  867. * Files and directories older than 6 hours and development snapshots older
  868. * than 5 minutes are considered stale. We only cache development snapshots
  869. * for 5 minutes since otherwise updated snapshots might not be downloaded as
  870. * expected.
  871. *
  872. * When checking file ages, we need to use the ctime, not the mtime
  873. * (modification time) since many (all?) tar implementations go out of their
  874. * way to set the mtime on the files they create to the timestamps recorded
  875. * in the tarball. We want to see the last time the file was changed on disk,
  876. * which is left alone by tar and correctly set to the time the archive file
  877. * was unpacked.
  878. *
  879. * @param $path
  880. * A string containing a file path or (streamwrapper) URI.
  881. */
  882. function update_delete_file_if_stale($path) {
  883. if (file_exists($path)) {
  884. $filectime = filectime($path);
  885. if (REQUEST_TIME - $filectime > DRUPAL_MAXIMUM_TEMP_FILE_AGE || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) {
  886. file_unmanaged_delete_recursive($path);
  887. }
  888. }
  889. }
Login or register to post comments