<?php

/******************************************************
 This script updates the columns click_counter and last_click in table 'media'
 after a user clicked  a media link in the result listing.
 *******************************************************/

    error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);
    $url        = '';
    $query      = '';
    $db         = '0';
    $prefix     = '';
    $client_ip  = '';

    $url        = trim(substr($_GET['url'], 0, 1024));
    $query      = trim(substr($_GET['query'], 0, 100));
    $db         = trim(substr($_GET['db'], 0, 1));
    $prefix     = trim(substr($_GET['prefix'], 0, 20));
    $client_ip  = trim(substr($_GET['client_ip'], 0, 255));

    $url    = str_replace("-_-", "&", $url);   //      decrypt the & character
    $url    = str_replace("_-_", "+", $url);   //      decrypt the + character
    $time   = time();

    header("Location: $url");       //  this is what the user really wants to get when clicking the object
    //  Okay, we will let him go. But also we will store the destination.

    define("_SECURE",1);            // define secure constant

    $include_dir  = "../include";
    $settings_dir = "../settings";
    include "$include_dir/commonfuncs.php";
    include "$settings_dir/database.php";

    $prefix = cleaninput($prefix);

    //      if requested by Search-form, overwrite default db number
    if ($db > 0 && $db <= 5) {
        $dbu_act = $db;
    }

    //      if requested by Search-form, overwrite default table prefix
    if ($prefix != 0 ) {
        $mysql_table_prefix = $prefix;

    }

    //      get active database
    if ($dbu_act == '1') {
        $db_con     = db1_connect() ;
        $success    = @mysql_select_db ($database1, $db_con);
        if ($prefix != 0 ) {    //      if requested by Search-form, overwrite default table prefix
            $mysql_table_prefix = $prefix;
        } else {
            $mysql_table_prefix = $mysql_table_prefix1;
        }
    }

    if ($dbu_act == '2') {
        $db_con = db2_connect() ;
        $success    = @mysql_select_db ($database2, $db_con);
        if ($prefix != 0 ) {    //      if requested by Search-form, overwrite default table prefix
            $mysql_table_prefix = $prefix;
        } else {
            $mysql_table_prefix = $mysql_table_prefix2;
        }
    }

    if ($dbu_act == '3') {
        $db_con = db3_connect() ;
        $success    = @mysql_select_db ($database3, $db_con);
        if ($prefix != 0 ) {    //      if requested by Search-form, overwrite default table prefix
            $mysql_table_prefix = $prefix;
        } else {
            $mysql_table_prefix = $mysql_table_prefix3;
        }
    }

    if ($dbu_act == '4') {
        $db_con = db4_connect() ;
        $success    = @mysql_select_db ($database4, $db_con);
        if ($prefix != 0 ) {    //      if requested by Search-form, overwrite default table prefix
            $mysql_table_prefix = $prefix;
        } else {
            $mysql_table_prefix = $mysql_table_prefix4;
        }
    }

    if ($dbu_act == '5') {
        $db_con = db5_connect() ;
        $success    = @mysql_select_db ($database5, $db_con);
        if ($prefix != 0 ) {    //      if requested by Search-form, overwrite default table prefix
            $mysql_table_prefix = $prefix;
        } else {
            $mysql_table_prefix = $mysql_table_prefix5;
        }
    }

    $plus_nr = '';
    @include "".$settings_dir."/db".$dbu_act."/conf_".$mysql_table_prefix.".php";
    if (!$plus_nr) {
        include "/admin/settings/backup/Sphider-plus_default-configuration.php";
    }

    $url        = convert_url(mysql_real_escape_string($url));
    $query      = mysql_real_escape_string($query);
    $client_ip  = mysql_real_escape_string($client_ip);

    //  try to update the link in actual database
    $result = mysql_query("select last_click from ".$mysql_table_prefix."media  where media_link = '$url' LIMIT 1");
    if ($debug > '0') echo mysql_error();
    if (mysql_num_rows($result)> 0) {
        $last_click = mysql_result($result, '0');   //  get time of last click
        if ($last_click+$click_wait < $time) {      //  prevent  promoted clicks, else remember this click
            mysql_query ("update ".$mysql_table_prefix."media set click_counter=click_counter+1, last_click='$time', last_query='$query', ip='$client_ip' where media_link = '$url' LIMIT 1");
            if ($debug > '0') echo mysql_error();
        }
    }

    exit ('');      //  Good-bye, we've got your media click.

?>