/**
 * Generates javascript for client functionality from 51Degrees data.
 */
function fiftyone_degrees_get_script()
{
    global $_fiftyone_degrees_needed_properties;
    $headers = fiftyone_degrees_get_headers();
    $_fiftyone_degrees_needed_properties = array();
    $profile_scripts = array();
    $other_scripts = array();
    $output = "\n";
    for ($i = 0; $i < $headers['property_count']; $i++) {
        $property = fiftyone_degrees_read_property($i, $headers);
        if ($property['value_type_id'] == 4) {
            $_fiftyone_degrees_needed_properties[] = $property['name'];
            if (strpos($property['name'], 'Profile') !== FALSE) {
                $profile_scripts[] = $property['name'];
            } else {
                $other_scripts[] = $property['name'];
            }
        }
    }
    $_51d = fiftyone_degrees_get_device_data($_SERVER['HTTP_USER_AGENT']);
    foreach ($other_scripts as $property) {
        if (isset($_51d[$property])) {
            $output .= $_51d[$property];
            $output .= "\n";
        }
    }
    $output .= "function FODPO() { var profileIds = new Array();\n";
    $c = count($profile_scripts);
    if ($c > 0 && (isset($_SESSION['51D_ProfileIds']) === FALSE || strlen($_SESSION['51D_ProfileIds']) == 0)) {
        foreach ($profile_scripts as $property) {
            if (isset($_51d[$property])) {
                $props = $_51d[$property];
                foreach ($props as $prop) {
                    $output .= $prop;
                    $output .= "\n";
                }
            }
        }
        $output .= "document.cookie = \"51D_ProfileIds=\" + profileIds.join(\"|\");";
    }
    $output .= "}\n";
    return str_replace('document.addEventListener("load", this.loadComplete.bind(this), true);', 'window.addEventListener("load", this.loadComplete.bind(this), true);', $output);
}
Exemplo n.º 2
0
<!DOCTYPE html>
<html>
<head>
<title>51Degrees Image Optimser Gallery</title>
<?php 
fiftyone_degrees_echo_header();
?>
</head>
<body>
<?php 
fiftyone_degrees_echo_menu();
?>
<div class="content">
<?php 
require_once '../core/51Degrees.php';
$headers = fiftyone_degrees_get_headers();
$dataset_name = fiftyone_degrees_get_dataset_name($headers);
$use_auto = $dataset_name === 'Enterprise';
global $_fiftyone_degrees_image_width_parameter;
if (isset($_fiftyone_degrees_image_width_parameter)) {
    $w_param = $_fiftyone_degrees_image_width_parameter;
} else {
    $w_param = 'w';
}
global $_fiftyone_degrees_image_height_parameter;
if (isset($_fiftyone_degrees_image_height_parameter)) {
    $h_param = $_fiftyone_degrees_image_height_parameter;
} else {
    $h_param = 'h';
}
$files = scandir('Gallery');
function fiftyone_degrees_get_current_dataset_name()
{
    fiftyone_degrees_set_file_handle();
    global $_fiftyone_degrees_data_file;
    $headers = fiftyone_degrees_get_headers();
    $name = fiftyone_degrees_get_dataset_name($headers);
    fclose($_fiftyone_degrees_data_file);
    return $name;
}
/**
 * Returns array of properties associated with the device.
 *
 * @param string $useragent
 *   The useragent of the device.
 *
 * @return array
 *   Array of properties and values.
 */
function fiftyone_degrees_get_device_data($useragent)
{
    global $_fiftyone_degrees_use_array_caching;
    global $_fiftyone_degrees_data_file;
    fiftyone_degrees_set_file_handle();
    $debug_info = array();
    $start_time = microtime(TRUE);
    $info = array();
    if ($_fiftyone_degrees_use_array_caching !== FALSE) {
        global $_fiftyone_degrees_cache;
        $_fiftyone_degrees_cache = array();
    }
    $headers = fiftyone_degrees_get_headers();
    $root_char_nodes = fiftyone_degrees_read_root_node_offsets($headers);
    $root_char_nodes_count = count($root_char_nodes);
    // Unpack creates a 1 based array. array merge converts to 0 based.
    $useragent_bytes = array_merge(unpack('C*', $useragent));
    $useragent_length = count($useragent_bytes);
    $current_position = min($useragent_length, $root_char_nodes_count) - 1;
    $matched_node_indexs = array();
    $debug_info['root_nodes_evaluated'] = 0;
    $debug_info['nodes_evaluated'] = 0;
    $debug_info['string_read'] = 0;
    $debug_info['signatures_read'] = 0;
    $debug_info['signatures_compared'] = 0;
    $debug_info['difference'] = 0;
    while ($current_position > 0) {
        $node = fiftyone_degrees_read_node($root_char_nodes[$current_position], $headers);
        $debug_info['root_nodes_evaluated']++;
        $node = fiftyone_degrees_evaluate_node($node, NULL, $useragent_bytes, $useragent_length, $debug_info, $headers);
        if ($node != NULL && $node['is_complete']) {
            // Add this node's index to the list for the match in the correct order.
            $index = fiftyone_degrees_integer_binary_search($matched_node_indexs, $node['offset']);
            array_splice($matched_node_indexs, ~$index, 0, $node['offset']);
            // Check from the next character position to the left of this one.
            $current_position = $node['next_char_position'];
        } else {
            // No nodes matched at the character position.
            $current_position--;
        }
    }
    $timings = array();
    $timings['node_match_time'] = microtime(TRUE) - $start_time;
    $signatures_checked = count($matched_node_indexs);
    $info['SignaturesChecked'] = $signatures_checked;
    $method = '';
    $timings['signature_match_time'] = microtime(TRUE);
    $matched_signature = fiftyone_degrees_get_signature($matched_node_indexs, $useragent_bytes, $method, $timings, $debug_info, $headers);
    if ($matched_signature != -1) {
        $best_signature = fiftyone_degrees_read_signature($matched_signature, $headers);
        if (isset($lowest_score) == FALSE) {
            $lowest_score = 0;
        }
    } else {
        $lowest_score = PHP_INT_MAX;
        $best_signature = fiftyone_degrees_read_signature(0, $headers);
        $method = 'none';
    }
    $info['Method'] = $method;
    $timings['signature_match_time'] = microtime(TRUE) - $timings['signature_match_time'];
    $debug_info['signature_string'] = fiftyone_degrees_get_signature_string($best_signature, $headers);
    $info['Confidence'] = $lowest_score;
    $profiles = array();
    $filled_components = array();
    $feature_detection_ids = fiftyone_degrees_get_feature_detection_profile_ids();
    foreach ($feature_detection_ids as $id) {
        $profile = fiftyone_degrees_get_profile_from_id($id, $headers);
        // Make sure only one profile for each component can be added.
        if ($profile != NULL && !in_array($profile['component_id'], $filled_components)) {
            $filled_components[] = $profile['component_id'];
            $profiles[] = $profile;
        }
    }
    $timings['profile_fetch_time'] = microtime(TRUE);
    foreach ($best_signature['profile_indexs'] as $profile_offset) {
        $profile = fiftyone_degrees_read_profile($profile_offset, $headers);
        // Check if this profile's component has already been filled.
        if (!in_array($profile['component_id'], $filled_components)) {
            $filled_components[] = $profile['component_id'];
            $profiles[] = $profile;
        }
    }
    $timings['profile_fetch_time'] = microtime(TRUE) - $timings['profile_fetch_time'];
    $timings['property_fetch_time'] = microtime(TRUE);
    $_51d = fiftyone_degrees_get_property_data($profiles, $headers);
    $bandwidth = fiftyone_degrees_get_bandwidth_data();
    if ($bandwidth != NULL) {
        foreach ($bandwidth as $k => $v) {
            $_51d[$k] = $v;
        }
    }
    foreach ($info as $i_k => $i_v) {
        $_51d[$i_k] = $i_v;
    }
    $timings['property_fetch_time'] = microtime(TRUE) - $timings['property_fetch_time'];
    $end_time = microtime(TRUE);
    $duration = $end_time - $start_time;
    $_51d['Time'] = $end_time - $start_time;
    $_51d['debug_timings'] = $timings;
    $_51d['debug_info'] = $debug_info;
    global $_fiftyone_degrees_data_file_path;
    $_51d['DataFile'] = $_fiftyone_degrees_data_file_path;
    return $_51d;
}
/**
 * Returns an array with properties in the data set and their values.
 *
 * @return array
 *   Properties and their values.
 */
function fiftyone_degrees_get_meta_data()
{
    fiftyone_degrees_set_file_handle();
    $headers = fiftyone_degrees_get_headers();
    $name = fiftyone_degrees_get_dataset_name($headers);
    $cache_file_name = dirname(__FILE__) . "/51Degrees_meta_data_cache_{$headers['info']['published_year']}_{$headers['info']['published_month']}_{$headers['info']['published_day']}_{$name}.cache";
    $cache_file_name = str_replace("\\", "/", $cache_file_name);
    if (file_exists($cache_file_name)) {
        $cache = file_get_contents($cache_file_name);
        $meta_data = unserialize($cache);
        if ($meta_data !== FALSE) {
            return $meta_data;
        }
    }
    $meta_data = array();
    $meta_data['Date'] = "{$headers['info']['published_month']}/{$headers['info']['published_day']}/{$headers['info']['published_year']}";
    $meta_data['DatasetName'] = fiftyone_degrees_get_dataset_name($headers);
    $property_names = array();
    $value_indexes = array();
    for ($i = 0; $i < $headers['property_count']; $i++) {
        $property = fiftyone_degrees_read_property($i, $headers);
        $meta_data[$property['name']] = array();
        $meta_data[$property['name']]['Description'] = fiftyone_degrees_read_ascii($property['description_offset'], $headers);
        $meta_data[$property['name']]['List'] = $property['list'];
        if ($property['url_offset'] != -1) {
            $meta_data[$property['name']]['Url'] = fiftyone_degrees_read_ascii($property['url_offset'], $headers);
        }
        $property_names[$i] = $property['name'];
        $meta_data[$property['name']]['Values'] = array();
        $value_indexes[] = array('first_value_index' => $property['first_value_index'], 'last_value_index' => $property['last_value_index']);
        switch ($property['value_type_id']) {
            case 0:
                // string
            // string
            default:
                $value_type = 'string';
                break;
            case 1:
                // int
                $value_type = 'integer';
                break;
            case 2:
                // double
                $value_type = 'double';
                break;
            case 3:
                // bool
                $value_type = 'boolean';
                break;
            case 4:
                // javaScript
                $value_type = 'javascript';
                break;
        }
        $meta_data[$property['name']]['ValueType'] = $value_type;
    }
    foreach ($value_indexes as $value_index) {
        for ($i = $value_index['first_value_index']; $i <= $value_index['last_value_index']; $i++) {
            $property_value = fiftyone_degrees_read_property_value($i, $headers);
            $name = $property_names[$property_value['property_index']];
            $meta_data[$name]['Values'][$property_value['value']] = array();
            if ($property_value['description_offset'] > 0) {
                $meta_data[$name]['Values'][$property_value['value']]['Description'] = fiftyone_degrees_read_ascii($property_value['description_offset'], $headers);
            }
            if ($property_value['url_offset'] > 0) {
                $meta_data[$name]['Values'][$property_value['value']]['Url'] = fiftyone_degrees_read_ascii($property_value['url_offset'], $headers);
            }
        }
    }
    $handle = fopen($cache_file_name, "w");
    if ($handle !== FALSE) {
        $dir = new DirectoryIterator(dirname(__FILE__));
        foreach ($dir as $fileinfo) {
            if ($fileinfo->isFile()) {
                $file_extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
                if ($file_extension === "cache" && strpos($fileinfo->getFilename(), '51Degrees_meta_data_cache_') === 0) {
                    $path = $fileinfo->getRealPath();
                    @unlink($path);
                }
            }
        }
        $cache = serialize($meta_data);
        fwrite($handle, $cache);
    }
    return $meta_data;
}