/lib/luc_client_utils.php
PHP | 372 lines | 318 code | 39 blank | 15 comment | 77 complexity | 46dc0055df22555182f0c90748626bf0 MD5 | raw file
- <?php
- function luc_StatAppend()
- {
- global $wpdb, $userdata, $_STATPRESS, $StatPressV_Option;
- $table_name = STATPRESS_V_TABLE_NAME;
- get_currentuserinfo();
- $feed = '';
- // Time
- $timestamp = current_time('timestamp');
- $vdate = gmdate("Ymd", $timestamp);
- $vtime = gmdate("H:i:s", $timestamp);
- // IP
- $ipAddress = luc_get_ip();
- if (luc_CheckBanIP($ipAddress) === true)
- return '';
- // Use GeoIP? http://geolite.maxmind.com/download/geoip/api/php/
- if ($StatPressV_Option['StatPressV_Use_GeoIP'] == 'checked' & function_exists('geoip_open'))
- { // Open the database to read and save info
- $gi = geoip_open(luc_GeoIP_dbname('country'), GEOIP_STANDARD);
- $geoip_isok = true;
- }
- // URL (requested)
- $urlRequested = luc_StatPressV_URL();
- if (preg_match("/.ico$/i", $urlRequested))
- return '';
- if (preg_match("/favicon.ico/i", $urlRequested))
- return '';
- if (preg_match("/.css$/i", $urlRequested))
- return '';
- if (preg_match("/.js$/i", $urlRequested))
- return '';
- if (stristr($urlRequested, "/wp-content/plugins") != false)
- return '';
- if (stristr($urlRequested, "/wp-content/themes") != false)
- return '';
- if (stristr($urlRequested, "/wp-content/uploads") != false)
- return '';
- $referrer = (isset ($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : '');
- $userAgent = (isset ($_SERVER['HTTP_USER_AGENT']) ? htmlentities($_SERVER['HTTP_USER_AGENT']) : '');
- $spider = luc_GetSpider($userAgent);
- $spambot = luc_CheckSpamBot($userAgent);
- if (($spambot !== null) or ($userAgent == ''))
- {
- if ($StatPressV_Option['StatPressV_Dont_Collect_SpamBot'] == 'checked')
- {
- if ($userAgent != '')
- return ''; // It is a spambot either way, don't log it
- else
- if (($userAgent == '') and ($StatPressV_Option['StatPressV_Dont_Collect_BlankUA'] == 'checked'))
- return ''; // It is a blank UA and we are treating it as a spamnbot
- }
- else
- {
- if (stripos($spambot, 'Spam Bot') > 0)
- $spider = $spambot;
- else
- $spider = $spambot . ' Spam Bot';
- }
- }
- if ($StatPressV_Option['StatPressV_BotScout_API_Key'] == 'checked')
- {
- if (luc_BotScout_Check_IP($ipAddress))
- $spider = $spambot;
- }
- if (($spider != '') and ($StatPressV_Option['StatPressV_Dont_Collect_Spider'] == 'checked'))
- return '';
- if ($spider != '')
- {
- $os = '';
- $browser = '';
- }
- else
- {
- // Trap feeds
- $prsurl = parse_url(get_bloginfo('url'));
- $feed = luc_StatPressV_Is_Feed($prsurl['scheme'] . '://' . $prsurl['host'] . htmlentities($_SERVER['REQUEST_URI']));
- // Get OS and browser
- $os = luc_GetOS($userAgent);
- $browser = luc_GetBrowser($userAgent);
- $refsearch = luc_GetSE($referrer);
- if ($refsearch !== null)
- list ($searchengine, $search_phrase) = explode("|", $refsearch);
- else
- {
- $searchengine = "";
- $search_phrase = "";
- }
- }
- $domain = strtoupper(luc_Domain($ipAddress));
- $code = explode(';', htmlentities($_SERVER['HTTP_ACCEPT_LANGUAGE']));
- $code = explode(',', $code[0]);
- $lang = explode('-', $code[0]);
- $language = $lang[0];
- if ($geoip_isok === true)
- {
- $cc = geoip_country_code_by_addr($gi, $ipAddress);
- if ($cc !== false)
- $country = $cc;
- else
- $country = NULL;
- }
- else
- $country = $lang[1];
- // Auto-delete visits if...
- $today = gmdate('Ymd', current_time('timestamp'));
- if ($today <> $StatPressV_Option['StatPressV_Delete_Today'])
- {
- $StatPressV_Option['StatPressV_Delete_Today'] = $today;
- // luc_options_update($StatPressV_Option);
- if ($StatPressV_Option['StatPressV_AutoDelete_spider'] != '')
- {
- $t = gmdate("Ymd", strtotime('-' . $StatPressV_Option['StatPressV_AutoDelete_spider']));
- $results = $wpdb->query("DELETE FROM $table_name WHERE date < '" . $t . "' AND spider <> ''");
- $results = $wpdb->query('OPTIMIZE TABLE ' . $table_name);
- }
- if ($StatPressV_Option['StatPressV_AutoDelete'] != '')
- {
- $t = gmdate("Ymd", strtotime('-' . $StatPressV_Option['StatPressV_AutoDelete']));
- $results = $wpdb->query("DELETE FROM $table_name WHERE date < '" . $t . "'");
- $results = $wpdb->query('OPTIMIZE TABLE ' . $table_name);
- }
- }
- if ((!is_user_logged_in()) or ($StatPressV_Option['StatPressV_Dont_Collect_Logged_User'] != 'checked'))
- {
- $result = $wpdb->insert(STATPRESS_V_TABLE_NAME, array (
- 'date' => $vdate,
- 'time' => $vtime,
- 'ip' => $ipAddress,
- 'urlrequested' => mysql_real_escape_string(strip_tags($urlRequested)),
- 'agent' => mysql_real_escape_string(strip_tags($userAgent)),
- 'referrer' => mysql_real_escape_string(strip_tags($referrer)),
- 'search' => mysql_real_escape_string(strip_tags($search_phrase)),
- 'nation' => mysql_real_escape_string(strip_tags($domain)),
- 'os' => mysql_real_escape_string(strip_tags($os)),
- 'browser' => mysql_real_escape_string(strip_tags($browser)),
- 'searchengine' => mysql_real_escape_string(strip_tags($searchengine)),
- 'spider' => mysql_real_escape_string(strip_tags($spider)),
- 'feed' => $feed,
- 'user' => $userdata->user_login,
- 'timestamp' => $timestamp,
- 'language' => mysql_real_escape_string(strip_tags($language)),
- 'country' => mysql_real_escape_string(strip_tags($country))
- ), array ('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'));
- }
- }
- function StatPress_Print($body = '')
- {
- echo luc_StatPressV_Vars($body);
- }
- function luc_GetSE($referrer = null)
- {
- $key = null;
- $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/searchengine.dat');
- foreach ($lines as $line_num => $se)
- {
- list($name, $url, $key, $stop) = explode("|", $se);
- if (stripos($referrer, $url) === false)
- continue;
- // trovato se
- if (stripos($key,$url) !== false) // detection of searchs engines without URL like Google
- { $query_search = explode($key,$referrer);
- $query_search = explode($stop,$query_search[1]);
- return ($name . "|" . urlencode($query_search[0]));
- }
- // detection of search engine with URL like Google
- $variables = luc_GetQueryPairs($referrer);
- $i = count($variables);
- while ($i--)
- {
- $tab = explode("=", $variables[$i]);
- if ($tab[0] == $key)
- return ($name . "|" . urlencode($tab[1]));
- }
- }
- return null;
- }
- function luc_GetSpider($agent = null)
- {
- $agent = str_replace(" ", "", $agent);
- $key = null;
- $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/spider.dat');
- if (file_exists(STATPRESS_V_PLUGIN_PATH . '-custom/spider.dat'))
- $lines = array_merge($lines, file(STATPRESS_V_PLUGIN_PATH . '-custom/spider.dat'));
- foreach ($lines as $line_num => $spider)
- {
- list ($name, $key) = explode("|", $spider);
- if (stripos($agent, $key) === false)
- continue;
- // trovato
- return $name;
- }
- return null;
- }
- function luc_GetOS($arg)
- {
- $arg = str_replace(" ", "", $arg);
- $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/os.dat');
- foreach ($lines as $line_num => $os)
- {
- list ($os_name, $os_id) = explode("|", $os);
- if (stripos($arg, $os_id) === false)
- continue;
- return $os_name;
- }
- return '';
- }
- function luc_GetBrowser($arg)
- {
- $arg = str_replace(" ", "", $arg);
- $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/browser.dat');
- foreach ($lines as $line_num => $browser)
- {
- list ($name, $id) = explode("|", $browser);
- if (stripos($arg, $id) === false)
- continue;
- return $name;
- }
- return '';
- }
- function luc_GetQueryPairs($url)
- {
- $parsed_url = parse_url($url);
- $tab = parse_url($url);
- $host = $tab['host'];
- if (key_exists("query", $tab))
- {
- $query = $tab["query"];
- $query = str_replace("&", "&", $query);
- $query = urldecode($query);
- $query = str_replace("?", "&", $query);
- return explode("&", $query);
- }
- else
- {
- return null;
- }
- }
- function luc_CheckBanIP($arg)
- {
- if (file_exists(STATPRESS_V_PLUGIN_PATH . '-custom/banips.dat'))
- $lines = file(STATPRESS_V_PLUGIN_PATH . '-custom/banips.dat');
- else
- $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/banips.dat');
- if ($lines !== false)
- {
- foreach ($lines as $banip)
- {
- if (@ preg_match('/^' . rtrim($banip, "\r\n") . '$/', $arg))
- return true;
- }
- }
- return false;
- }
- function luc_CheckSpamBot($agent = null)
- {
- $agent = str_replace(" ", "", $agent);
- $key = null;
- $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/spambot.dat');
- if (file_exists(STATPRESS_V_PLUGIN_PATH . '-custom/spambot.dat'))
- $lines = array_merge($lines, file(STATPRESS_V_PLUGIN_PATH . '-custom/spambot.dat'));
- foreach ($lines as $line_num => $spambot)
- {
- list ($name, $key) = explode("|", $spambot);
- if (stripos($agent, $key) === false)
- continue;
- return $name;
- }
- return null;
- }
- function luc_StatPressV_Is_Feed($url)
- {
- if (stristr($url, get_bloginfo('comments_atom_url')) != FALSE)
- return 'COMMENT ATOM';
- elseif (stristr($url, get_bloginfo('comments_rss2_url')) != FALSE)
- return 'COMMENT RSS';
- elseif (stristr($url, get_bloginfo('rdf_url')) != FALSE)
- return 'RDF';
- elseif (stristr($url, get_bloginfo('atom_url')) != FALSE)
- return 'ATOM';
- elseif (stristr($url, get_bloginfo('rss_url')) != FALSE)
- return 'RSS';
- elseif (stristr($url, get_bloginfo('rss2_url')) != FALSE)
- return 'RSS2';
- elseif (stristr($url, 'wp-feed.php') != FALSE)
- return 'RSS2';
- else if (stristr($url, '/feed') != FALSE)
- return 'RSS2';
- return '';
- }
- function luc_BotScout_Check_IP($ip)
- {
- global $StatPressV_Option;
- $apikey = $StatPressV_Option['StatPressV_BotScout_API_Key'];
- $apiquery = "http://botscout.com/test/?key=$apikey&ip=$ip";
- $data = file_get_contents($apiquery);
- // take the returned value and parse it (standard API, not XML)
- $botdata = explode('|', $data);
- // $botdata[0] = Y|N
- // $botdata[1] = IP
- // $botdata[2] = Count in database
- if ($botdata == 'Y')
- return true;
- return false;
- }
- function luc_get_ip()
- {
- if ($_SERVER)
- {
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && luc_ip_not_private($_SERVER['HTTP_X_FORWARDED_FOR']))
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- elseif (isset($_SERVER['HTTP_CLIENT_IP']) && luc_ip_not_private($_SERVER['HTTP_CLIENT_IP']))
- $ip = $_SERVER['HTTP_CLIENT_IP'];
- else
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- else
- {
- if (getenv('HTTP_X_FORWARDED_FOR') && luc_ip_not_private(getenv('HTTP_X_FORWARDED_FOR')))
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- elseif (getenv('HTTP_CLIENT_IP') && luc_ip_not_private(getenv('HTTP_CLIENT_IP')))
- $ip = getenv('HTTP_CLIENT_IP');
- else
- $ip = getenv('REMOTE_ADDR');
- }
- return $ip;
- }
- function luc_ip_not_private($ip)
- {
- if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE))
- return true;
- return false;
- }
- ?>