browscap.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * Replacement for PHP's get_browser() function.
  5. */
  6. // Include browscap data import and user agent recording functions.
  7. include_once 'import.inc';
  8. /**
  9. * Implements hook_permission().
  10. */
  11. function browscap_permission() {
  12. return array(
  13. 'administer browscap' => array(
  14. 'title' => t('Administer browscap'),
  15. ),
  16. );
  17. }
  18. /**
  19. * Implements hook_menu().
  20. */
  21. function browscap_menu() {
  22. $items['admin/config/system/browscap'] = array(
  23. 'title' => 'Browscap',
  24. 'description' => 'View the current browscap data version and configure automatic update settings.',
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('browscap_settings_form'),
  27. 'access arguments' => array('administer browscap'),
  28. 'file' => 'browscap.admin.inc',
  29. );
  30. return $items;
  31. }
  32. /**
  33. * Implements hook_help().
  34. */
  35. function browscap_help($path, $arg) {
  36. switch ($path) {
  37. case 'admin/config/system/browscap':
  38. return '<p>' . t('View the current browscap data version and configure automatic update settings.') . '</p>';
  39. }
  40. }
  41. /**
  42. * Implements hook_cron().
  43. */
  44. function browscap_cron() {
  45. if (variable_get('browscap_enable_automatic_updates', FALSE)) {
  46. // Check the current update timer.
  47. $automatic_update_timer = variable_get('browscap_automatic_updates_timer', 604800);
  48. // Check when the last update occurred.
  49. $last_imported = variable_get('browscap_imported', 0);
  50. // Update the browscap data if the amount of time specified by the update
  51. // timer has passed.
  52. if (($last_imported + $automatic_update_timer) < REQUEST_TIME) {
  53. // Update the browscap information.
  54. _browscap_import();
  55. }
  56. }
  57. }
  58. /**
  59. * Provide data about a user agent string or the current user agent.
  60. *
  61. * @param string $user_agent
  62. * Optional user agent string to test. If empty, use the value from the
  63. * current request.
  64. *
  65. * @return array
  66. * An array of information about the user agent.
  67. */
  68. function browscap_get_browser($user_agent = NULL) {
  69. // Determine the current user agent if a user agent was not specified.
  70. if ($user_agent != NULL) {
  71. $user_agent = check_plain(trim($user_agent));
  72. }
  73. elseif ($user_agent == NULL && isset($_SERVER['HTTP_USER_AGENT'])) {
  74. $user_agent = check_plain(trim($_SERVER['HTTP_USER_AGENT']));
  75. }
  76. else {
  77. $user_agent = 'Default Browser';
  78. }
  79. // Check the cache for user agent data.
  80. $cache = cache_get($user_agent, 'cache_browscap');
  81. // Attempt to find a cached user agent.
  82. // Otherwise store the user agent data in the cache.
  83. if (!empty($cache) && ($cache->created > REQUEST_TIME - 60 * 60 * 24)) {
  84. $user_agent_properties = $cache->data;
  85. }
  86. else {
  87. // Find the user agent's properties.
  88. // The useragent column contains the wildcarded pattern to match against our
  89. // full-length string while the ORDER BY chooses the most-specific matching
  90. // pattern.
  91. $user_agent_properties = db_query("SELECT * FROM {browscap} WHERE :useragent LIKE useragent ORDER BY LENGTH(useragent) DESC", array(':useragent' => $user_agent))
  92. ->fetchObject();
  93. // Store user agent data in the cache.
  94. cache_set($user_agent, $user_agent_properties, 'cache_browscap');
  95. }
  96. // Create an array to hold the user agent's properties.
  97. $properties = array();
  98. // Return an array of user agent properties.
  99. if (isset($user_agent_properties) && isset($user_agent_properties->data)) {
  100. // Unserialize the user agent data found in the cache or the database.
  101. $properties = unserialize($user_agent_properties->data);
  102. // Set the user agent name and name pattern.
  103. $properties['useragent'] = $user_agent;
  104. $properties['browser_name_pattern'] = strtr($user_agent_properties->useragent, '%_', '*?');
  105. }
  106. else {
  107. // Set the user agent name and name pattern to 'unrecognized'.
  108. $properties['useragent'] = 'unrecognized';
  109. $properties['browser_name_pattern'] = strtr('unrecognized', '%_', '*?');
  110. }
  111. return $properties;
  112. }