browscap.module 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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', TRUE) == TRUE) {
  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', REQUEST_TIME);
  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. // Record when the browscap information was updated.
  56. variable_set('browscap_imported', REQUEST_TIME);
  57. }
  58. }
  59. }
  60. /**
  61. * Provide data about a user agent string or the current user agent.
  62. *
  63. * @param string $user_agent
  64. * Optional user agent string to test. If empty, use the value from the
  65. * current request.
  66. *
  67. * @return array
  68. * An array of information about the user agent.
  69. */
  70. function browscap_get_browser($user_agent = NULL) {
  71. // Determine the current user agent if a user agent was not specified.
  72. if ($user_agent != NULL) {
  73. $user_agent = check_plain(trim($user_agent));
  74. }
  75. elseif ($user_agent == NULL && isset($_SERVER['HTTP_USER_AGENT'])) {
  76. $user_agent = check_plain(trim($_SERVER['HTTP_USER_AGENT']));
  77. }
  78. else {
  79. $user_agent = 'Default Browser';
  80. }
  81. // Check the cache for user agent data.
  82. $cache = cache_get($user_agent, 'cache_browscap');
  83. // Attempt to find a cached user agent.
  84. // Otherwise store the user agent data in the cache.
  85. if (!empty($cache) && ($cache->created > REQUEST_TIME - 60 * 60 * 24)) {
  86. $user_agent_properties = $cache->data;
  87. }
  88. else {
  89. // Find the user agent's properties.
  90. // The useragent column contains the wildcarded pattern to match against our
  91. // full-length string while the ORDER BY chooses the most-specific matching
  92. // pattern.
  93. $user_agent_properties = db_query("SELECT * FROM {browscap} WHERE :useragent LIKE useragent ORDER BY LENGTH(useragent) DESC", array(':useragent' => $user_agent))
  94. ->fetchObject();
  95. // Store user agent data in the cache.
  96. cache_set($user_agent, $user_agent_properties, 'cache_browscap');
  97. }
  98. // Create an array to hold the user agent's properties.
  99. $properties = array();
  100. // Return an array of user agent properties.
  101. if (isset($user_agent_properties) && isset($user_agent_properties->data)) {
  102. // Unserialize the user agent data found in the cache or the database.
  103. $properties = unserialize($user_agent_properties->data);
  104. // Set the user agent name and name pattern.
  105. $properties['useragent'] = $user_agent;
  106. $properties['browser_name_pattern'] = strtr($user_agent_properties->useragent, '%_', '*?');
  107. }
  108. else {
  109. // Set the user agent name and name pattern to 'unrecognized'.
  110. $properties['useragent'] = 'unrecognized';
  111. $properties['browser_name_pattern'] = strtr('unrecognized', '%_', '*?');
  112. }
  113. return $properties;
  114. }