update.fetch.inc

  1. 7.x drupal-7.x/modules/update/update.fetch.inc
  2. 6.x drupal-6.x/modules/update/update.fetch.inc

Code required only when fetching information about available updates.

File

drupal-6.x/modules/update/update.fetch.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Code required only when fetching information about available updates.
  5. */
  6. /**
  7. * Callback to manually check the update status without cron.
  8. */
  9. function update_manual_status() {
  10. if (_update_refresh()) {
  11. drupal_set_message(t('Attempted to fetch information about all available new releases and updates.'));
  12. }
  13. else {
  14. drupal_set_message(t('Unable to fetch any information about available new releases and updates.'), 'error');
  15. }
  16. drupal_goto('admin/reports/updates');
  17. }
  18. /**
  19. * Fetch project info via XML from a central server.
  20. */
  21. function _update_refresh() {
  22. static $fail = array();
  23. global $base_url;
  24. module_load_include('inc', 'update', 'update.compare');
  25. // Since we're fetching new available update data, we want to clear
  26. // our cache of both the projects we care about, and the current update
  27. // status of the site. We do *not* want to clear the cache of available
  28. // releases just yet, since that data (even if it's stale) can be useful
  29. // during update_get_projects(); for example, to modules that implement
  30. // hook_system_info_alter() such as cvs_deploy.
  31. _update_cache_clear('update_project_projects');
  32. _update_cache_clear('update_project_data');
  33. $available = array();
  34. $data = array();
  35. $site_key = md5($base_url . drupal_get_private_key());
  36. $projects = update_get_projects();
  37. // Now that we have the list of projects, we should also clear our cache of
  38. // available release data, since even if we fail to fetch new data, we need
  39. // to clear out the stale data at this point.
  40. _update_cache_clear('update_available_releases');
  41. $max_fetch_attempts = variable_get('update_max_fetch_attempts', UPDATE_MAX_FETCH_ATTEMPTS);
  42. foreach ($projects as $key => $project) {
  43. $url = _update_build_fetch_url($project, $site_key);
  44. $fetch_url_base = _update_get_fetch_url_base($project);
  45. if (empty($fail[$fetch_url_base]) || count($fail[$fetch_url_base]) < $max_fetch_attempts) {
  46. $xml = drupal_http_request($url);
  47. if (isset($xml->data)) {
  48. $data[] = $xml->data;
  49. }
  50. else {
  51. // Connection likely broken; prepare to give up.
  52. $fail[$fetch_url_base][$key] = 1;
  53. }
  54. }
  55. else {
  56. // Didn't bother trying to fetch.
  57. $fail[$fetch_url_base][$key] = 1;
  58. }
  59. }
  60. if ($data) {
  61. $parser = new update_xml_parser;
  62. $available = $parser->parse($data);
  63. }
  64. if (!empty($available) && is_array($available)) {
  65. // Record the projects where we failed to fetch data.
  66. foreach ($fail as $fetch_url_base => $failures) {
  67. foreach ($failures as $key => $value) {
  68. $available[$key]['project_status'] = 'not-fetched';
  69. }
  70. }
  71. $frequency = variable_get('update_check_frequency', 1);
  72. _update_cache_set('update_available_releases', $available, time() + (60 * 60 * 24 * $frequency));
  73. watchdog('update', 'Attempted to fetch information about all available new releases and updates.', array(), WATCHDOG_NOTICE, l(t('view'), 'admin/reports/updates'));
  74. }
  75. else {
  76. watchdog('update', 'Unable to fetch any information about available new releases and updates.', array(), WATCHDOG_ERROR, l(t('view'), 'admin/reports/updates'));
  77. }
  78. // Whether this worked or not, we did just (try to) check for updates.
  79. variable_set('update_last_check', time());
  80. return $available;
  81. }
  82. /**
  83. * Generates the URL to fetch information about project updates.
  84. *
  85. * This figures out the right URL to use, based on the project's .info file
  86. * and the global defaults. Appends optional query arguments when the site is
  87. * configured to report usage stats.
  88. *
  89. * @param $project
  90. * The array of project information from update_get_projects().
  91. * @param $site_key
  92. * The anonymous site key hash (optional).
  93. *
  94. * @see update_refresh()
  95. * @see update_get_projects()
  96. */
  97. function _update_build_fetch_url($project, $site_key = '') {
  98. $name = $project['name'];
  99. $url = _update_get_fetch_url_base($project);
  100. $url .= '/'. $name .'/'. DRUPAL_CORE_COMPATIBILITY;
  101. // Only append a site_key and the version information if we have a site_key
  102. // in the first place, and if this is not a disabled module or theme. We do
  103. // not want to record usage statistics for disabled code.
  104. if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) {
  105. $url .= (strpos($url, '?') === TRUE) ? '&' : '?';
  106. $url .= 'site_key=';
  107. $url .= rawurlencode($site_key);
  108. if (!empty($project['info']['version'])) {
  109. $url .= '&version=';
  110. $url .= rawurlencode($project['info']['version']);
  111. }
  112. }
  113. return $url;
  114. }
  115. /**
  116. * Return the base of the URL to fetch available update data for a project.
  117. *
  118. * @param $project
  119. * The array of project information from update_get_projects().
  120. * @return
  121. * The base of the URL used for fetching available update data. This does
  122. * not include the path elements to specify a particular project, version,
  123. * site_key, etc.
  124. *
  125. * @see _update_build_fetch_url()
  126. */
  127. function _update_get_fetch_url_base($project) {
  128. return isset($project['info']['project status url']) ? $project['info']['project status url'] : variable_get('update_fetch_url', UPDATE_DEFAULT_URL);
  129. }
  130. /**
  131. * Perform any notifications that should be done once cron fetches new data.
  132. *
  133. * This method checks the status of the site using the new data and depending
  134. * on the configuration of the site, notifies administrators via email if there
  135. * are new releases or missing security updates.
  136. *
  137. * @see update_requirements()
  138. */
  139. function _update_cron_notify() {
  140. include_once './includes/install.inc';
  141. $status = update_requirements('runtime');
  142. $params = array();
  143. $notify_all = (variable_get('update_notification_threshold', 'all') == 'all');
  144. foreach (array('core', 'contrib') as $report_type) {
  145. $type = 'update_'. $report_type;
  146. if (isset($status[$type]['severity'])
  147. && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) {
  148. $params[$report_type] = $status[$type]['reason'];
  149. }
  150. }
  151. if (!empty($params)) {
  152. $notify_list = variable_get('update_notify_emails', '');
  153. if (!empty($notify_list)) {
  154. $default_language = language_default();
  155. foreach ($notify_list as $target) {
  156. if ($target_user = user_load(array('mail' => $target))) {
  157. $target_language = user_preferred_language($target_user);
  158. }
  159. else {
  160. $target_language = $default_language;
  161. }
  162. drupal_mail('update', 'status_notify', $target, $target_language, $params);
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * XML Parser object to read Drupal's release history info files.
  169. * This uses PHP4's lame XML parsing, but it works.
  170. */
  171. class update_xml_parser {
  172. var $projects = array();
  173. var $current_project;
  174. var $current_release;
  175. var $current_term;
  176. var $current_tag;
  177. var $current_object;
  178. /**
  179. * Parse an array of XML data files.
  180. */
  181. function parse($data) {
  182. foreach ($data as $datum) {
  183. $parser = xml_parser_create();
  184. xml_set_object($parser, $this);
  185. xml_set_element_handler($parser, 'start', 'end');
  186. xml_set_character_data_handler($parser, "data");
  187. xml_parse($parser, $datum);
  188. xml_parser_free($parser);
  189. }
  190. return $this->projects;
  191. }
  192. function start($parser, $name, $attr) {
  193. $this->current_tag = $name;
  194. switch ($name) {
  195. case 'PROJECT':
  196. unset($this->current_object);
  197. $this->current_project = array();
  198. $this->current_object = &$this->current_project;
  199. break;
  200. case 'RELEASE':
  201. unset($this->current_object);
  202. $this->current_release = array();
  203. $this->current_object = &$this->current_release;
  204. break;
  205. case 'TERM':
  206. unset($this->current_object);
  207. $this->current_term = array();
  208. $this->current_object = &$this->current_term;
  209. break;
  210. case 'FILE':
  211. unset($this->current_object);
  212. $this->current_file = array();
  213. $this->current_object = &$this->current_file;
  214. break;
  215. }
  216. }
  217. function end($parser, $name) {
  218. switch ($name) {
  219. case 'PROJECT':
  220. unset($this->current_object);
  221. $this->projects[$this->current_project['short_name']] = $this->current_project;
  222. $this->current_project = array();
  223. break;
  224. case 'RELEASE':
  225. unset($this->current_object);
  226. $this->current_project['releases'][$this->current_release['version']] = $this->current_release;
  227. break;
  228. case 'RELEASES':
  229. $this->current_object = &$this->current_project;
  230. break;
  231. case 'TERM':
  232. unset($this->current_object);
  233. $term_name = $this->current_term['name'];
  234. if (!isset($this->current_release['terms'])) {
  235. $this->current_release['terms'] = array();
  236. }
  237. if (!isset($this->current_release['terms'][$term_name])) {
  238. $this->current_release['terms'][$term_name] = array();
  239. }
  240. $this->current_release['terms'][$term_name][] = $this->current_term['value'];
  241. break;
  242. case 'TERMS':
  243. $this->current_object = &$this->current_release;
  244. break;
  245. case 'FILE':
  246. unset($this->current_object);
  247. $this->current_release['files'][] = $this->current_file;
  248. break;
  249. case 'FILES':
  250. $this->current_object = &$this->current_release;
  251. break;
  252. default:
  253. $this->current_object[strtolower($this->current_tag)] = trim($this->current_object[strtolower($this->current_tag)]);
  254. $this->current_tag = '';
  255. }
  256. }
  257. function data($parser, $data) {
  258. if ($this->current_tag && !in_array($this->current_tag, array('PROJECT', 'RELEASE', 'RELEASES', 'TERM', 'TERMS', 'FILE', 'FILES'))) {
  259. $tag = strtolower($this->current_tag);
  260. if (isset($this->current_object[$tag])) {
  261. $this->current_object[$tag] .= $data;
  262. }
  263. else {
  264. $this->current_object[$tag] = $data;
  265. }
  266. }
  267. }
  268. }