$confidence) { include_once "Handlers/" . $handler_number . ".php"; $device = call_user_func("_H" . $handler_number, $useragent); if ($device != NULL) { $devices[] = $device; } } // If devices have been found reduce them to a single device. if (isset($devices[0])) { // Produce an array of final devices. $final_devices = array(); foreach ($devices as $device) { foreach ($device as $fin) { $final_devices[] = $fin; } } if (count($final_devices) == 1) { // If there is only one device found then use this one. $final = $final_devices[0]; } else { // If more than one device has been found then use the // final matcher to reduce to a single device. include_once "Handlers/Calculations/FinalMatcher.php"; $final = fiftyone_degrees_finalMatcher($useragent, $final_devices); } } // If a final single device is found then read property data and // add performance measures to the output. if (isset($final)) { // Populate the resultset with the profile data and get an array // of profile ids. $profileIds = fiftyone_degrees_readProfiles($base_location, $_51d, $final); // Record the time the matching process took to complete. $time = explode(' ', microtime()); $time = $time[1] + $time[0]; $finish = $time; $total_time = round(($finish - $start), 4); $_51d['DetectionTime'] = $total_time * 1000; // Set the device ID based on the 4 profile ids provided. $_51d['DeviceID'] = $profileIds[0] . '-' . $profileIds[1] . '-' . $profileIds[2] . '-' . $profileIds[3]; // Record the user agent of the device matched against. $_51d['MatchedUserAgent'] = $final[4]; } // If there is a session record the results for future requests. if (isset($_SESSION)) { $_SESSION['51D'] = $_51d; } } else { // The session already holds the results of a previous match. // Use that one and set the detection time to zero. $_51d = $_SESSION['51D']; if ($_51d['DetectionTime'] != 0.00) { $_51d['DetectionTime'] = 0.00; $_SESSION['51D'] = $_51d; } } return $_51d; } /** * Gets the useragent associated with the current request. * * Checks other header fields for the presence of original * user agent values and returns the most appropriate one. * * return string * The most relevent UserAgent string for the current request. */ function fiftyone_degrees_GetUserAgent() { if (function_exists('getallheaders')) { $input = getallheaders(); $header_ua = "User-Agent"; $transcoder_ua = array( "x-Device-User-Agent", "X-Device-User-Agent", "X-OperaMini-Phone-UA" ); if (isset($input[$header_ua])) { return trim($input[$header_ua]); } else { foreach ($transcoder_ua as $trans_ua) { if (isset($input[$trans_ua])) { return trim($input[$trans_ua]); } } } return ""; } else { if (isset($_SERVER['HTTP_X_DEVICE_USER_AGENT'])) { return $_SERVER['HTTP_X_DEVICE_USER_AGENT']; } if (isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])) { return $_SERVER['HTTP_X_OPERAMINI_PHONE_UA']; } return $_SERVER['HTTP_USER_AGENT']; } } /** * Populates the resultset with profile data. * * Uses the Profiles file to find profile files and read * contents into the resultset. * * array $base_location * Base folder for module. * array $resultset * Resultset to be populated. * array $final * Array of offsets in the profiles file for each profile id. * return array * Array of profile ids for each offset. Used for the DeviceID. */ function fiftyone_degrees_readProfiles($base_location, &$resultset, $final) { $profileIds = array(); if (($handle = fopen($base_location . "Profiles.dat", "r")) !== FALSE) { foreach (array_slice($final, 0, 4) as $offset) { // Move to the position in the profiles file. fseek($handle, $offset); // The first line is the profile id which must be trimmed to remove // the new line character. $profileIds[] = trim(fgets($handle)); // Read the profile values and add to the resultset. while (($data = fgetcsv($handle, 28, "|")) !== FALSE && $data[0] != NULL) { switch ($data[1]) { // This is a single value property. case "0": $resultset[$data[0]] = $data[2]; break; // This is a list property with multiple values. case "1": $resultset[$data[0]] = array_slice($data, 3); break; } } } fclose($handle); } return $profileIds; }