<?php

header('Access-Control-Allow-Origin: *');
ini_set('display_errors', '0');
# !!!!Change it by your Authorization token!!!!!
$token = "Your token goes here";
$api_url = "https://api.merk.cz:443/";
$api_method = "suggest";
function return_empty()
{
    echo "[]";
    exit;
}
if ($_GET == NULL) {
    return_empty();
}
$opts = array('http' => array('method' => "GET", 'header' => "Authorization: Token " . $token));
$context = stream_context_create($opts);
$results = file_get_contents($api_url . $api_method . '?' . http_build_query($_GET), false, $context);
if ($results) {
    echo $results;
} else {
    return_empty();
}
function top_exit($link_id, $site_id, $timescale, $max_num)
{
    // List top exit destinations
    $sub_sql = get_date($timescale);
    $sql_query = "SELECT exit_page,COUNT(*) AS totals FROM logging_log WHERE site_id='{$site_id}' AND exit_page LIKE 'http://%' {$sub_sql} GROUP BY exit_page ORDER BY totals DESC LIMIT {$max_num};";
    $result = mysql_query($sql_query, $link_id);
    if (!$result) {
        echo "Database error !";
    }
    $counter = 0;
    while ($dbdata = mysql_fetch_row($result)) {
        $output_array[$counter][0] = $counter + 1;
        // ranking
        $output_array[$counter][1] = "<a href='{$dbdata['0']}'>{$dbdata['0']}</a>";
        // exit URL
        $output_array[$counter][2] = $dbdata[1];
        // requests
        $counter++;
    }
    if ($counter == 0) {
        $output_array = return_empty();
    }
    echo $output_array[0][0];
    return $output_array;
}