import.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @file
  4. * Browscap data import functions.
  5. */
  6. /**
  7. * Helper function to update the browscap data.
  8. *
  9. * @param boolean $cron
  10. * Optional import environment. If false, display status messages to the user in addition to logging information with the watchdog.
  11. */
  12. function _browscap_import($cron = TRUE) {
  13. // Check the local browscap data version number
  14. $local_version = variable_get('browscap_version', 0);
  15. // Retrieve the current browscap data version number using HTTP
  16. $current_version = drupal_http_request('http://tempdownloads.browserscap.com/versions/version-number.php');
  17. // Log an error if the browscap version number could not be retrieved
  18. if (isset($current_version->error)) {
  19. // Log a message with the watchdog
  20. watchdog('browscap', "Couldn't check version: %error", array('%error' => $current_version->error), WATCHDOG_ERROR);
  21. // Display a message to the user if the update process was triggered manually
  22. if ($cron == FALSE) {
  23. drupal_set_message(t("Couldn't check version: %error", array('%error' => $current_version->error)), 'error');
  24. }
  25. return;
  26. }
  27. // Sanitize the returned version number
  28. $current_version = check_plain(trim($current_version->data));
  29. // Compare the current and local version numbers to determine if the browscap
  30. // data requires updating
  31. if ($current_version == $local_version) {
  32. // Log a message with the watchdog
  33. watchdog('browscap', 'No new version of browscap to import');
  34. // Display a message to the user if the update process was triggered manually
  35. if ($cron == FALSE) {
  36. drupal_set_message(t('No new version of browscap to import'));
  37. }
  38. return;
  39. }
  40. // Retrieve the browscap data using HTTP
  41. $browscap_data = drupal_http_request('http://tempdownloads.browserscap.com/stream.php?BrowsCapINI');
  42. // Log an error if the browscap data could not be retrieved
  43. if (isset($browscap_data->error) || empty($browscap_data)) {
  44. // Log a message with the watchdog
  45. watchdog('browscap', "Couldn't retrieve updated browscap: %error", array('%error' => $browscap_data->error), WATCHDOG_ERROR);
  46. // Display a message to the user if the update process was triggered manually
  47. if ($cron == FALSE) {
  48. drupal_set_message(t("Couldn't retrieve updated browscap: %error", array('%error' => $browscap_data->error)), 'error');
  49. }
  50. return;
  51. }
  52. // Parse the returned browscap data
  53. // The parse_ini_string function is preferred but only available in PHP 5.3.0
  54. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  55. // Retrieve the browscap data
  56. $browscap_data = $browscap_data->data;
  57. // Replace 'true' and 'false' with '1' and '0'
  58. $browscap_data = strtr($browscap_data, array("=true\r" => "=1\r", "=false\r" => "=0\r"));
  59. // Parse the browscap data as a string
  60. $browscap_data = parse_ini_string($browscap_data, TRUE, INI_SCANNER_RAW);
  61. }
  62. else {
  63. // Create a path and filename
  64. $server = $_SERVER['SERVER_NAME'];
  65. $path = variable_get('file_temporary_path', '/tmp');
  66. $file = "$path/browscap_$server.ini";
  67. // Write the browscap data to a file
  68. $browscap_file = fopen($file, "w");
  69. fwrite($browscap_file, $browscap_data->data);
  70. fclose($browscap_file);
  71. // Parse the browscap data as a file
  72. $browscap_data = parse_ini_file($file, TRUE);
  73. }
  74. if ($browscap_data) {
  75. // Find the version information
  76. // The version information is the first entry in the array
  77. $version = array_shift($browscap_data);
  78. // Store the data available for each user agent
  79. foreach ($browscap_data as $key => $values) {
  80. // Store the current value
  81. $e = $values;
  82. // Create an array to hold the last parent
  83. $last_parent = array();
  84. // Recurse through the available user agent information
  85. while (isset($values['Parent']) && $values['Parent'] !== $last_parent) {
  86. $values = isset($browscap_data[$values['Parent']]) ? $browscap_data[$values['Parent']] : array();
  87. $e = array_merge($values, $e);
  88. $last_parent = $values;
  89. }
  90. // Replace '*?' with '%_'
  91. $user_agent = strtr($key, '*?', '%_');
  92. // Change all array keys to lowercase
  93. $e = array_change_key_case($e);
  94. // Delete all data about the current user agent from the database
  95. db_delete('browscap')
  96. ->condition('useragent', $user_agent)
  97. ->execute();
  98. // Insert all data about the current user agent into the database
  99. db_insert('browscap')
  100. ->fields(array(
  101. 'useragent' => $user_agent,
  102. 'data' => serialize($e)
  103. ))
  104. ->execute();
  105. }
  106. // Clear the browscap data cache
  107. cache_clear_all('*', 'cache_browscap', TRUE);
  108. // Update the browscap version
  109. variable_set('browscap_version', $current_version);
  110. // Log a message with the watchdog
  111. watchdog('browscap', 'New version of browscap imported: %version', array('%version' => $current_version));
  112. // Display a message to the user if the update process was triggered manually
  113. if ($cron == FALSE) {
  114. drupal_set_message(t('New version of browscap imported: %version', array('%version' => $current_version)));
  115. }
  116. }
  117. }