update.fetch.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * @file
  4. * Code required only when fetching information about available updates.
  5. */
  6. /**
  7. * Page callback: Checks for updates and displays the update status report.
  8. *
  9. * Manually checks the update status without the use of cron.
  10. *
  11. * @see update_menu()
  12. */
  13. function update_manual_status() {
  14. _update_refresh();
  15. $batch = array(
  16. 'operations' => array(
  17. array('update_fetch_data_batch', array()),
  18. ),
  19. 'finished' => 'update_fetch_data_finished',
  20. 'title' => t('Checking available update data'),
  21. 'progress_message' => t('Trying to check available update data ...'),
  22. 'error_message' => t('Error checking available update data.'),
  23. 'file' => drupal_get_path('module', 'update') . '/update.fetch.inc',
  24. );
  25. batch_set($batch);
  26. batch_process('admin/reports/updates');
  27. }
  28. /**
  29. * Implements callback_batch_operation().
  30. *
  31. * Processes a step in batch for fetching available update data.
  32. *
  33. * @param $context
  34. * Reference to an array used for Batch API storage.
  35. */
  36. function update_fetch_data_batch(&$context) {
  37. $queue = DrupalQueue::get('update_fetch_tasks');
  38. if (empty($context['sandbox']['max'])) {
  39. $context['finished'] = 0;
  40. $context['sandbox']['max'] = $queue->numberOfItems();
  41. $context['sandbox']['progress'] = 0;
  42. $context['message'] = t('Checking available update data ...');
  43. $context['results']['updated'] = 0;
  44. $context['results']['failures'] = 0;
  45. $context['results']['processed'] = 0;
  46. }
  47. // Grab another item from the fetch queue.
  48. for ($i = 0; $i < 5; $i++) {
  49. if ($item = $queue->claimItem()) {
  50. if (_update_process_fetch_task($item->data)) {
  51. $context['results']['updated']++;
  52. $context['message'] = t('Checked available update data for %title.', array('%title' => $item->data['info']['name']));
  53. }
  54. else {
  55. $context['message'] = t('Failed to check available update data for %title.', array('%title' => $item->data['info']['name']));
  56. $context['results']['failures']++;
  57. }
  58. $context['sandbox']['progress']++;
  59. $context['results']['processed']++;
  60. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  61. $queue->deleteItem($item);
  62. }
  63. else {
  64. // If the queue is currently empty, we're done. It's possible that
  65. // another thread might have added new fetch tasks while we were
  66. // processing this batch. In that case, the usual 'finished' math could
  67. // get confused, since we'd end up processing more tasks that we thought
  68. // we had when we started and initialized 'max' with numberOfItems(). By
  69. // forcing 'finished' to be exactly 1 here, we ensure that batch
  70. // processing is terminated.
  71. $context['finished'] = 1;
  72. return;
  73. }
  74. }
  75. }
  76. /**
  77. * Implements callback_batch_finished().
  78. *
  79. * Performs actions when all fetch tasks have been completed.
  80. *
  81. * @param $success
  82. * TRUE if the batch operation was successful; FALSE if there were errors.
  83. * @param $results
  84. * An associative array of results from the batch operation, including the key
  85. * 'updated' which holds the total number of projects we fetched available
  86. * update data for.
  87. */
  88. function update_fetch_data_finished($success, $results) {
  89. if ($success) {
  90. if (!empty($results)) {
  91. if (!empty($results['updated'])) {
  92. drupal_set_message(format_plural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.'));
  93. }
  94. if (!empty($results['failures'])) {
  95. drupal_set_message(format_plural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.'), 'error');
  96. }
  97. }
  98. }
  99. else {
  100. drupal_set_message(t('An error occurred trying to get available update data.'), 'error');
  101. }
  102. }
  103. /**
  104. * Attempts to drain the queue of tasks for release history data to fetch.
  105. */
  106. function _update_fetch_data() {
  107. $queue = DrupalQueue::get('update_fetch_tasks');
  108. $end = time() + variable_get('update_max_fetch_time', UPDATE_MAX_FETCH_TIME);
  109. while (time() < $end && ($item = $queue->claimItem())) {
  110. _update_process_fetch_task($item->data);
  111. $queue->deleteItem($item);
  112. }
  113. }
  114. /**
  115. * Processes a task to fetch available update data for a single project.
  116. *
  117. * Once the release history XML data is downloaded, it is parsed and saved into
  118. * the {cache_update} table in an entry just for that project.
  119. *
  120. * @param $project
  121. * Associative array of information about the project to fetch data for.
  122. *
  123. * @return
  124. * TRUE if we fetched parsable XML, otherwise FALSE.
  125. */
  126. function _update_process_fetch_task($project) {
  127. global $base_url;
  128. $fail = &drupal_static(__FUNCTION__, array());
  129. // This can be in the middle of a long-running batch, so REQUEST_TIME won't
  130. // necessarily be valid.
  131. $now = time();
  132. if (empty($fail)) {
  133. // If we have valid data about release history XML servers that we have
  134. // failed to fetch from on previous attempts, load that from the cache.
  135. if (($cache = _update_cache_get('fetch_failures')) && ($cache->expire > $now)) {
  136. $fail = $cache->data;
  137. }
  138. }
  139. $max_fetch_attempts = variable_get('update_max_fetch_attempts', UPDATE_MAX_FETCH_ATTEMPTS);
  140. $success = FALSE;
  141. $available = array();
  142. $site_key = drupal_hmac_base64($base_url, drupal_get_private_key());
  143. $url = _update_build_fetch_url($project, $site_key);
  144. $fetch_url_base = _update_get_fetch_url_base($project);
  145. $project_name = $project['name'];
  146. if (empty($fail[$fetch_url_base]) || $fail[$fetch_url_base] < $max_fetch_attempts) {
  147. $xml = drupal_http_request($url);
  148. if (isset($xml->error)) {
  149. watchdog('update', 'Error %errorcode (%message) occurred when trying to fetch available update data for the project %project.', array('%errorcode' => $xml->code, '%message' => $xml->error, '%project' => $project_name), WATCHDOG_ERROR);
  150. }
  151. if (!isset($xml->error) && isset($xml->data)) {
  152. $data = $xml->data;
  153. }
  154. }
  155. if (!empty($data)) {
  156. $available = update_parse_xml($data);
  157. // @todo: Purge release data we don't need (http://drupal.org/node/238950).
  158. if (!empty($available)) {
  159. // Only if we fetched and parsed something sane do we return success.
  160. $success = TRUE;
  161. }
  162. }
  163. else {
  164. $available['project_status'] = 'not-fetched';
  165. if (empty($fail[$fetch_url_base])) {
  166. $fail[$fetch_url_base] = 1;
  167. }
  168. else {
  169. $fail[$fetch_url_base]++;
  170. }
  171. }
  172. $frequency = variable_get('update_check_frequency', 1);
  173. $cid = 'available_releases::' . $project_name;
  174. _update_cache_set($cid, $available, $now + (60 * 60 * 24 * $frequency));
  175. // Stash the $fail data back in the DB for the next 5 minutes.
  176. _update_cache_set('fetch_failures', $fail, $now + (60 * 5));
  177. // Whether this worked or not, we did just (try to) check for updates.
  178. variable_set('update_last_check', $now);
  179. // Now that we processed the fetch task for this project, clear out the
  180. // record in {cache_update} for this task so we're willing to fetch again.
  181. _update_cache_clear('fetch_task::' . $project_name);
  182. return $success;
  183. }
  184. /**
  185. * Clears out all the cached available update data and initiates re-fetching.
  186. */
  187. function _update_refresh() {
  188. module_load_include('inc', 'update', 'update.compare');
  189. // Since we're fetching new available update data, we want to clear
  190. // our cache of both the projects we care about, and the current update
  191. // status of the site. We do *not* want to clear the cache of available
  192. // releases just yet, since that data (even if it's stale) can be useful
  193. // during update_get_projects(); for example, to modules that implement
  194. // hook_system_info_alter() such as cvs_deploy.
  195. _update_cache_clear('update_project_projects');
  196. _update_cache_clear('update_project_data');
  197. $projects = update_get_projects();
  198. // Now that we have the list of projects, we should also clear our cache of
  199. // available release data, since even if we fail to fetch new data, we need
  200. // to clear out the stale data at this point.
  201. _update_cache_clear('available_releases::', TRUE);
  202. foreach ($projects as $key => $project) {
  203. update_create_fetch_task($project);
  204. }
  205. }
  206. /**
  207. * Adds a task to the queue for fetching release history data for a project.
  208. *
  209. * We only create a new fetch task if there's no task already in the queue for
  210. * this particular project (based on 'fetch_task::' entries in the
  211. * {cache_update} table).
  212. *
  213. * @param $project
  214. * Associative array of information about a project as created by
  215. * update_get_projects(), including keys such as 'name' (short name), and the
  216. * 'info' array with data from a .info file for the project.
  217. *
  218. * @see update_get_projects()
  219. * @see update_get_available()
  220. * @see update_refresh()
  221. * @see update_fetch_data()
  222. * @see _update_process_fetch_task()
  223. */
  224. function _update_create_fetch_task($project) {
  225. $fetch_tasks = &drupal_static(__FUNCTION__, array());
  226. if (empty($fetch_tasks)) {
  227. $fetch_tasks = _update_get_cache_multiple('fetch_task');
  228. }
  229. $cid = 'fetch_task::' . $project['name'];
  230. if (empty($fetch_tasks[$cid])) {
  231. $queue = DrupalQueue::get('update_fetch_tasks');
  232. $queue->createItem($project);
  233. // Due to race conditions, it is possible that another process already
  234. // inserted a row into the {cache_update} table and the following query will
  235. // throw an exception.
  236. // @todo: Remove the need for the manual check by relying on a queue that
  237. // enforces unique items.
  238. try {
  239. db_insert('cache_update')
  240. ->fields(array(
  241. 'cid' => $cid,
  242. 'created' => REQUEST_TIME,
  243. ))
  244. ->execute();
  245. }
  246. catch (Exception $e) {
  247. // The exception can be ignored safely.
  248. }
  249. $fetch_tasks[$cid] = REQUEST_TIME;
  250. }
  251. }
  252. /**
  253. * Generates the URL to fetch information about project updates.
  254. *
  255. * This figures out the right URL to use, based on the project's .info file and
  256. * the global defaults. Appends optional query arguments when the site is
  257. * configured to report usage stats.
  258. *
  259. * @param $project
  260. * The array of project information from update_get_projects().
  261. * @param $site_key
  262. * (optional) The anonymous site key hash. Defaults to an empty string.
  263. *
  264. * @return
  265. * The URL for fetching information about updates to the specified project.
  266. *
  267. * @see update_fetch_data()
  268. * @see _update_process_fetch_task()
  269. * @see update_get_projects()
  270. */
  271. function _update_build_fetch_url($project, $site_key = '') {
  272. $name = $project['name'];
  273. $url = _update_get_fetch_url_base($project);
  274. $url .= '/' . $name . '/' . DRUPAL_CORE_COMPATIBILITY;
  275. // Only append usage information if we have a site key and the project is
  276. // enabled. We do not want to record usage statistics for disabled projects.
  277. if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) {
  278. // Append the site key.
  279. $url .= (strpos($url, '?') !== FALSE) ? '&' : '?';
  280. $url .= 'site_key=';
  281. $url .= rawurlencode($site_key);
  282. // Append the version.
  283. if (!empty($project['info']['version'])) {
  284. $url .= '&version=';
  285. $url .= rawurlencode($project['info']['version']);
  286. }
  287. // Append the list of modules or themes enabled.
  288. $list = array_keys($project['includes']);
  289. $url .= '&list=';
  290. $url .= rawurlencode(implode(',', $list));
  291. }
  292. return $url;
  293. }
  294. /**
  295. * Returns the base of the URL to fetch available update data for a project.
  296. *
  297. * @param $project
  298. * The array of project information from update_get_projects().
  299. *
  300. * @return
  301. * The base of the URL used for fetching available update data. This does
  302. * not include the path elements to specify a particular project, version,
  303. * site_key, etc.
  304. *
  305. * @see _update_build_fetch_url()
  306. */
  307. function _update_get_fetch_url_base($project) {
  308. return isset($project['info']['project status url']) ? $project['info']['project status url'] : variable_get('update_fetch_url', UPDATE_DEFAULT_URL);
  309. }
  310. /**
  311. * Performs any notifications that should be done once cron fetches new data.
  312. *
  313. * This method checks the status of the site using the new data and, depending
  314. * on the configuration of the site, notifies administrators via e-mail if there
  315. * are new releases or missing security updates.
  316. *
  317. * @see update_requirements()
  318. */
  319. function _update_cron_notify() {
  320. module_load_install('update');
  321. $status = update_requirements('runtime');
  322. $params = array();
  323. $notify_all = (variable_get('update_notification_threshold', 'all') == 'all');
  324. foreach (array('core', 'contrib') as $report_type) {
  325. $type = 'update_' . $report_type;
  326. if (isset($status[$type]['severity'])
  327. && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) {
  328. $params[$report_type] = $status[$type]['reason'];
  329. }
  330. }
  331. if (!empty($params)) {
  332. $notify_list = variable_get('update_notify_emails', '');
  333. if (!empty($notify_list)) {
  334. $default_language = language_default();
  335. foreach ($notify_list as $target) {
  336. if ($target_user = user_load_by_mail($target)) {
  337. $target_language = user_preferred_language($target_user);
  338. }
  339. else {
  340. $target_language = $default_language;
  341. }
  342. $message = drupal_mail('update', 'status_notify', $target, $target_language, $params);
  343. // Track when the last mail was successfully sent to avoid sending
  344. // too many e-mails.
  345. if ($message['result']) {
  346. variable_set('update_last_email_notification', REQUEST_TIME);
  347. }
  348. }
  349. }
  350. }
  351. }
  352. /**
  353. * Parses the XML of the Drupal release history info files.
  354. *
  355. * @param $raw_xml
  356. * A raw XML string of available release data for a given project.
  357. *
  358. * @return
  359. * Array of parsed data about releases for a given project, or NULL if there
  360. * was an error parsing the string.
  361. */
  362. function update_parse_xml($raw_xml) {
  363. try {
  364. $xml = new SimpleXMLElement($raw_xml);
  365. }
  366. catch (Exception $e) {
  367. // SimpleXMLElement::__construct produces an E_WARNING error message for
  368. // each error found in the XML data and throws an exception if errors
  369. // were detected. Catch any exception and return failure (NULL).
  370. return;
  371. }
  372. // If there is no valid project data, the XML is invalid, so return failure.
  373. if (!isset($xml->short_name)) {
  374. return;
  375. }
  376. $short_name = (string) $xml->short_name;
  377. $data = array();
  378. foreach ($xml as $k => $v) {
  379. $data[$k] = (string) $v;
  380. }
  381. $data['releases'] = array();
  382. if (isset($xml->releases)) {
  383. foreach ($xml->releases->children() as $release) {
  384. $version = (string) $release->version;
  385. $data['releases'][$version] = array();
  386. foreach ($release->children() as $k => $v) {
  387. $data['releases'][$version][$k] = (string) $v;
  388. }
  389. $data['releases'][$version]['terms'] = array();
  390. if ($release->terms) {
  391. foreach ($release->terms->children() as $term) {
  392. if (!isset($data['releases'][$version]['terms'][(string) $term->name])) {
  393. $data['releases'][$version]['terms'][(string) $term->name] = array();
  394. }
  395. $data['releases'][$version]['terms'][(string) $term->name][] = (string) $term->value;
  396. }
  397. }
  398. }
  399. }
  400. return $data;
  401. }