Example #1
0
function fs_api_get_country_code($ip_address)
{
    require_once dirname(__FILE__) . '/ip2country.php';
    return fs_ip2c($ip_address);
}
Example #2
0
function fs_add_comment_flag($link)
{
    $FS_PATH = fs_get_firestats_path();
    if (!$FS_PATH) {
        return;
    }
    require_once $FS_PATH . '/php/ip2country.php';
    $ip = get_comment_author_IP();
    $code = fs_ip2c($ip);
    if (!$code) {
        return $link;
    }
    return $link . ' ' . fs_get_country_flag_url($code);
}
Example #3
0
function fs_rebuild_country_codes($value)
{
    $reset = $value == 0;
    $num_to_build = 1000;
    $fsdb =& fs_get_db_conn();
    $hits = fs_hits_table();
    if ($reset) {
        if (false === $fsdb->get_results("UPDATE `{$hits}` SET `country_code` = '0'")) {
            return fs_db_error();
        }
    }
    $res = $fsdb->get_results("SELECT DISTINCT(IP) FROM `{$hits}` WHERE `country_code` = '0' LIMIT {$num_to_build}");
    if ($res === false) {
        return fs_db_error();
    } else {
        $chunk_size = 200;
        $c = count($res);
        $index = 0;
        if ($c > 0) {
            while ($index < $c) {
                $ii = 0;
                $sql = "UPDATE `{$hits}` SET `country_code` = CASE ";
                $ips = '';
                while ($ii < $chunk_size && $index < $c) {
                    $record = $res[$index++];
                    $ii++;
                    $ip = $record->IP;
                    if ($ips == '') {
                        $ips .= "'{$ip}'";
                    } else {
                        $ips .= ",'{$ip}'";
                    }
                    $intcode = fs_ip2c($ip, true);
                    if ($intcode != 0) {
                        $sql .= "WHEN IP='{$ip}' THEN '{$intcode}' ";
                    } else {
                        $sql .= "WHEN IP='{$ip}' THEN NULL ";
                    }
                }
                $sql .= " ELSE `IP` END WHERE IP IN ({$ips})";
                $r2 = $fsdb->query($sql);
                if ($r2 === false) {
                    return fs_db_error();
                }
            }
        }
        return $index;
    }
}
Example #4
0
function fs_add_hit_immediate__($user_id, $site_id, $time = null)
{
    if (!fs_db_valid()) {
        return fs_get_database_status_message();
    }
    $fsdb =& fs_get_db_conn();
    $d = fs_get_hit_data($fsdb, $user_id, $site_id);
    $user_id = $d->user_id;
    $site_id = $d->site_id;
    $remoteaddr = $d->remoteaddr;
    $useragent = $d->useragent;
    $url = $d->url;
    $referer = $d->referer;
    if ($time === null) {
        $time = "NOW()";
    } else {
        $time = $fsdb->escape($time);
    }
    $useragents = fs_useragents_table();
    $urls = fs_urls_table();
    $excluded_ips = fs_excluded_ips_table();
    if ($fsdb->query("START TRANSACTION") === false) {
        return fs_debug_rollback();
    }
    // insert to user agent table (no duplicates)
    $ret = $fsdb->query("INSERT IGNORE INTO `{$useragents}` (`useragent`,`md5`) VALUES ({$useragent} ,MD5(`useragent`))");
    if ($ret === false) {
        return fs_debug_rollback();
    }
    // if we actually inserted a new useragent, we need to match it against existing filters.
    if ($ret > 0) {
        $bots = fs_bots_table();
        $ret = $fsdb->get_row("SELECT ua.id id,count(wildcard) c\n\t\t\t\tFROM {$bots} RIGHT JOIN {$useragents} ua \n\t\t\t\tON useragent REGEXP wildcard \n\t\t\t\tWHERE useragent = {$useragent}\n\t\t\t\tGROUP BY useragent");
        if ($ret === false) {
            return fs_debug_rollback();
        }
        $ret = $fsdb->query("UPDATE {$useragents} SET match_bots='{$ret->c}' WHERE id='{$ret->id}'");
        if ($ret === false) {
            return fs_debug_rollback();
        }
    }
    $save_excluded = fs_get_save_excluded_records() === 'true';
    $c = $fsdb->get_var("SELECT COUNT(ip) FROM `{$excluded_ips}` WHERE `ip` = " . $remoteaddr);
    if ($c === false) {
        return fs_debug_rollback();
    }
    $c = (int) $c;
    $excluded_ip = $c > 0 ? 1 : 0;
    $excluded_users = fs_get_local_option('firestats_excluded_users');
    if ($excluded_users === false) {
        return fs_debug_rollback();
    }
    $excluded_user = $user_id && $excluded_users && in_array($user_id, explode(",", $excluded_users)) ? 1 : 0;
    // get index of useragent in table, can't use LAST_INSERT_ID() here because of the no-dups policy
    $ua_info = $fsdb->get_row("SELECT id,match_bots from `{$useragents}` WHERE `useragent` = {$useragent}");
    $excluded_useragent = $ua_info->match_bots > 0;
    // check if we want to save this
    if (!$save_excluded && ($excluded_useragent || $excluded_user || $excluded_ip)) {
        return true;
    }
    $useragent_id = $ua_info->id;
    if ($useragent_id === false) {
        return fs_debug_rollback();
    }
    // insert to urls table (no duplicates)
    $url = $url ? "{$url}" : "''";
    if ($fsdb->query("INSERT IGNORE INTO `{$urls}` (`url`,`md5`,`add_time`,`host`) \n\t\t\t\t\t VALUES ({$url},MD5(url),{$time},substring_index(substring_index(`url`,'/',3),'/',-1))") === false) {
        return fs_debug_rollback();
    }
    // get index of url in table, can't use LAST_INSERT_ID() here because of the no-dups policy
    $url_id = $fsdb->get_var("SELECT id from " . fs_urls_table() . " WHERE `url` = {$url}");
    if ($url_id === false) {
        return fs_debug_rollback();
    }
    if ($url_id == null) {
        return fs_debug_rollback();
    }
    // update site id of url to current site id.
    // this is only done for the url and not for the referrer:
    // we don't know the site id of the referrer. if it will appear as a url it will be assigned the site_id.
    if (false === $fsdb->get_var("UPDATE `{$urls}` SET `site_id` = {$site_id} WHERE `id` = {$url_id}")) {
        return fs_debug_rollback();
    }
    // insert referers into urls table (no duplicates)
    $referer = $referer ? "{$referer}" : "''";
    require_once FS_ABS_PATH . '/php/searchengines.php';
    $search_engine_id = "NULL";
    $search_terms = "NULL";
    $referrer_breakdown = null;
    if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) {
        $search_params = fs_get_search_terms_and_engine($_SERVER['HTTP_REFERER'], $referrer_breakdown);
        if ($search_params) {
            $id = $search_params->engine_id;
            $terms = $search_params->search_terms;
            if (!empty($id)) {
                $search_engine_id = $fsdb->escape($id);
            }
            if (!empty($terms)) {
                $search_terms = $fsdb->escape($terms);
            }
        }
    }
    $has_scheme = isset($referrer_breakdown['scheme']);
    $optional_host = $has_scheme ? ",`host`" : "";
    $optional_host_query = $has_scheme ? ",substring_index(substring_index(`url`,'/',3),'/',-1)" : "";
    if ($fsdb->query("INSERT IGNORE INTO `{$urls}`(`url`,`md5`,`add_time`,`search_engine_id`,`search_terms` {$optional_host}) VALUES ({$referer},MD5(url),{$time},{$search_engine_id} ,{$search_terms} {$optional_host_query})") === false) {
        return fs_debug_rollback();
    }
    // get index of url in table, can't use LAST_INSERT_ID() here because of the no-dups policy
    $referer_id = $fsdb->get_var("SELECT id from {$urls} WHERE `url` = {$referer}");
    if ($referer_id === false) {
        return fs_debug_rollback();
    }
    if ($referer_id == null) {
        echo "FireStats : Error getting referrer id  ";
        return fs_debug_rollback();
    }
    require_once dirname(__FILE__) . '/ip2country.php';
    $ip2c_res = fs_ip2c($d->ip_address, true);
    $ccode = $ip2c_res ? $fsdb->escape($ip2c_res) : "NULL";
    // insert to database.
    $sql = "INSERT IGNORE INTO " . fs_hits_table() . "\n\t\t\t(site_id,ip,timestamp,url_id,referer_id,useragent_id,session_id,excluded_ip,excluded_user,user_id,country_code) \n\t\t\t\t\tVALUES ({$site_id},\n\t\t\t\t\t\t\t{$remoteaddr},\n\t\t\t\t\t\t\t{$time},\n\t\t\t\t\t\t\t{$url_id},\n\t\t\t\t\t\t\t{$referer_id},\n\t\t\t\t\t\t\t{$useragent_id},\n\t\t\t\t\t\t\t" . (isset($session_id) ? "{$session_id}" : "NULL") . ",\n\t\t\t\t\t\t\t{$excluded_ip},\n\t\t\t\t\t\t\t{$excluded_user},\n\t\t\t\t\t\t\t" . ($user_id ? "{$user_id}" : "NULL") . ",\n\t\t\t\t\t\t\t{$ccode}\n\t\t\t\t\t\t\t)";
    if ($fsdb->query($sql) === false) {
        return fs_debug_rollback();
    }
    if ($fsdb->query("COMMIT") === false) {
        return fs_debug_rollback();
    }
    return true;
}