diff --git a/browscap.module b/browscap.module index 3c16b71..6dc993f 100644 --- a/browscap.module +++ b/browscap.module @@ -96,12 +96,39 @@ function browscap_help($path, $arg) { } /** + * Implements hook_init(). + */ +function browscap_init() { + // Record the user agent and set a flag to denote that the user agent was + // recorded during init() so it does not need to be recorded during exit() + _record_user_agent(TRUE, user_access('bypass browscap monitoring')); +} + +/** * Implements hook_exit(). */ function browscap_exit() { - // Record the current user agent if monitoring is enabled and the user does not - // have permission to bypass monitoring - if (variable_get('browscap_enable_user_agent_log', FALSE) == TRUE && user_access('bypass browscap monitoring') != TRUE) { + // Record the user agent + _record_user_agent(); +} + +/** + * Helper function to record a user agent(). + * + * @param boolean $skip_futher_recording + * Optional flag to denote if user agent monitoring should be skipped for the rest + * of the page request. If true, monitoring will be disabled. + * @param boolean $bypass_monitoring + * Optional flag to denote if the current user has permission to bypass monitoring + */ +function _record_user_agent($skip_futher_recording = FALSE, $bypass_monitoring = FALSE) { + // Create a static variable to track the status of user agent monitoring for + // the entire page request + static $record_user_agent = TRUE; + + // Record the current user agent if recording and logging are enabled and the + // user does not have permission to bypass monitoring + if ($record_user_agent == TRUE && $bypass_monitoring != TRUE && variable_get('browscap_enable_user_agent_log', FALSE) == TRUE) { // Get browscap data about the current user agent $user_agent = browscap_get_browser(); @@ -132,6 +159,12 @@ function browscap_exit() { )) ->expression('counter', 'counter + 1') ->execute(); + } + + // Change the status of user agent monitoring when necessary + if ($skip_futher_recording == TRUE) { + // Disable user agent monitoring for the rest of the page request + $record_user_agent = FALSE; } }