PageRenderTime 68ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/luc_client_utils.php

https://bitbucket.org/gawainlynch/statpress-visitors
PHP | 372 lines | 318 code | 39 blank | 15 comment | 77 complexity | 46dc0055df22555182f0c90748626bf0 MD5 | raw file
  1. <?php
  2. function luc_StatAppend()
  3. {
  4. global $wpdb, $userdata, $_STATPRESS, $StatPressV_Option;
  5. $table_name = STATPRESS_V_TABLE_NAME;
  6. get_currentuserinfo();
  7. $feed = '';
  8. // Time
  9. $timestamp = current_time('timestamp');
  10. $vdate = gmdate("Ymd", $timestamp);
  11. $vtime = gmdate("H:i:s", $timestamp);
  12. // IP
  13. $ipAddress = luc_get_ip();
  14. if (luc_CheckBanIP($ipAddress) === true)
  15. return '';
  16. // Use GeoIP? http://geolite.maxmind.com/download/geoip/api/php/
  17. if ($StatPressV_Option['StatPressV_Use_GeoIP'] == 'checked' & function_exists('geoip_open'))
  18. { // Open the database to read and save info
  19. $gi = geoip_open(luc_GeoIP_dbname('country'), GEOIP_STANDARD);
  20. $geoip_isok = true;
  21. }
  22. // URL (requested)
  23. $urlRequested = luc_StatPressV_URL();
  24. if (preg_match("/.ico$/i", $urlRequested))
  25. return '';
  26. if (preg_match("/favicon.ico/i", $urlRequested))
  27. return '';
  28. if (preg_match("/.css$/i", $urlRequested))
  29. return '';
  30. if (preg_match("/.js$/i", $urlRequested))
  31. return '';
  32. if (stristr($urlRequested, "/wp-content/plugins") != false)
  33. return '';
  34. if (stristr($urlRequested, "/wp-content/themes") != false)
  35. return '';
  36. if (stristr($urlRequested, "/wp-content/uploads") != false)
  37. return '';
  38. $referrer = (isset ($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : '');
  39. $userAgent = (isset ($_SERVER['HTTP_USER_AGENT']) ? htmlentities($_SERVER['HTTP_USER_AGENT']) : '');
  40. $spider = luc_GetSpider($userAgent);
  41. $spambot = luc_CheckSpamBot($userAgent);
  42. if (($spambot !== null) or ($userAgent == ''))
  43. {
  44. if ($StatPressV_Option['StatPressV_Dont_Collect_SpamBot'] == 'checked')
  45. {
  46. if ($userAgent != '')
  47. return ''; // It is a spambot either way, don't log it
  48. else
  49. if (($userAgent == '') and ($StatPressV_Option['StatPressV_Dont_Collect_BlankUA'] == 'checked'))
  50. return ''; // It is a blank UA and we are treating it as a spamnbot
  51. }
  52. else
  53. {
  54. if (stripos($spambot, 'Spam Bot') > 0)
  55. $spider = $spambot;
  56. else
  57. $spider = $spambot . ' Spam Bot';
  58. }
  59. }
  60. if ($StatPressV_Option['StatPressV_BotScout_API_Key'] == 'checked')
  61. {
  62. if (luc_BotScout_Check_IP($ipAddress))
  63. $spider = $spambot;
  64. }
  65. if (($spider != '') and ($StatPressV_Option['StatPressV_Dont_Collect_Spider'] == 'checked'))
  66. return '';
  67. if ($spider != '')
  68. {
  69. $os = '';
  70. $browser = '';
  71. }
  72. else
  73. {
  74. // Trap feeds
  75. $prsurl = parse_url(get_bloginfo('url'));
  76. $feed = luc_StatPressV_Is_Feed($prsurl['scheme'] . '://' . $prsurl['host'] . htmlentities($_SERVER['REQUEST_URI']));
  77. // Get OS and browser
  78. $os = luc_GetOS($userAgent);
  79. $browser = luc_GetBrowser($userAgent);
  80. $refsearch = luc_GetSE($referrer);
  81. if ($refsearch !== null)
  82. list ($searchengine, $search_phrase) = explode("|", $refsearch);
  83. else
  84. {
  85. $searchengine = "";
  86. $search_phrase = "";
  87. }
  88. }
  89. $domain = strtoupper(luc_Domain($ipAddress));
  90. $code = explode(';', htmlentities($_SERVER['HTTP_ACCEPT_LANGUAGE']));
  91. $code = explode(',', $code[0]);
  92. $lang = explode('-', $code[0]);
  93. $language = $lang[0];
  94. if ($geoip_isok === true)
  95. {
  96. $cc = geoip_country_code_by_addr($gi, $ipAddress);
  97. if ($cc !== false)
  98. $country = $cc;
  99. else
  100. $country = NULL;
  101. }
  102. else
  103. $country = $lang[1];
  104. // Auto-delete visits if...
  105. $today = gmdate('Ymd', current_time('timestamp'));
  106. if ($today <> $StatPressV_Option['StatPressV_Delete_Today'])
  107. {
  108. $StatPressV_Option['StatPressV_Delete_Today'] = $today;
  109. // luc_options_update($StatPressV_Option);
  110. if ($StatPressV_Option['StatPressV_AutoDelete_spider'] != '')
  111. {
  112. $t = gmdate("Ymd", strtotime('-' . $StatPressV_Option['StatPressV_AutoDelete_spider']));
  113. $results = $wpdb->query("DELETE FROM $table_name WHERE date < '" . $t . "' AND spider <> ''");
  114. $results = $wpdb->query('OPTIMIZE TABLE ' . $table_name);
  115. }
  116. if ($StatPressV_Option['StatPressV_AutoDelete'] != '')
  117. {
  118. $t = gmdate("Ymd", strtotime('-' . $StatPressV_Option['StatPressV_AutoDelete']));
  119. $results = $wpdb->query("DELETE FROM $table_name WHERE date < '" . $t . "'");
  120. $results = $wpdb->query('OPTIMIZE TABLE ' . $table_name);
  121. }
  122. }
  123. if ((!is_user_logged_in()) or ($StatPressV_Option['StatPressV_Dont_Collect_Logged_User'] != 'checked'))
  124. {
  125. $result = $wpdb->insert(STATPRESS_V_TABLE_NAME, array (
  126. 'date' => $vdate,
  127. 'time' => $vtime,
  128. 'ip' => $ipAddress,
  129. 'urlrequested' => mysql_real_escape_string(strip_tags($urlRequested)),
  130. 'agent' => mysql_real_escape_string(strip_tags($userAgent)),
  131. 'referrer' => mysql_real_escape_string(strip_tags($referrer)),
  132. 'search' => mysql_real_escape_string(strip_tags($search_phrase)),
  133. 'nation' => mysql_real_escape_string(strip_tags($domain)),
  134. 'os' => mysql_real_escape_string(strip_tags($os)),
  135. 'browser' => mysql_real_escape_string(strip_tags($browser)),
  136. 'searchengine' => mysql_real_escape_string(strip_tags($searchengine)),
  137. 'spider' => mysql_real_escape_string(strip_tags($spider)),
  138. 'feed' => $feed,
  139. 'user' => $userdata->user_login,
  140. 'timestamp' => $timestamp,
  141. 'language' => mysql_real_escape_string(strip_tags($language)),
  142. 'country' => mysql_real_escape_string(strip_tags($country))
  143. ), array ('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'));
  144. }
  145. }
  146. function StatPress_Print($body = '')
  147. {
  148. echo luc_StatPressV_Vars($body);
  149. }
  150. function luc_GetSE($referrer = null)
  151. {
  152. $key = null;
  153. $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/searchengine.dat');
  154. foreach ($lines as $line_num => $se)
  155. {
  156. list($name, $url, $key, $stop) = explode("|", $se);
  157. if (stripos($referrer, $url) === false)
  158. continue;
  159. // trovato se
  160. if (stripos($key,$url) !== false) // detection of searchs engines without URL like Google
  161. { $query_search = explode($key,$referrer);
  162. $query_search = explode($stop,$query_search[1]);
  163. return ($name . "|" . urlencode($query_search[0]));
  164. }
  165. // detection of search engine with URL like Google
  166. $variables = luc_GetQueryPairs($referrer);
  167. $i = count($variables);
  168. while ($i--)
  169. {
  170. $tab = explode("=", $variables[$i]);
  171. if ($tab[0] == $key)
  172. return ($name . "|" . urlencode($tab[1]));
  173. }
  174. }
  175. return null;
  176. }
  177. function luc_GetSpider($agent = null)
  178. {
  179. $agent = str_replace(" ", "", $agent);
  180. $key = null;
  181. $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/spider.dat');
  182. if (file_exists(STATPRESS_V_PLUGIN_PATH . '-custom/spider.dat'))
  183. $lines = array_merge($lines, file(STATPRESS_V_PLUGIN_PATH . '-custom/spider.dat'));
  184. foreach ($lines as $line_num => $spider)
  185. {
  186. list ($name, $key) = explode("|", $spider);
  187. if (stripos($agent, $key) === false)
  188. continue;
  189. // trovato
  190. return $name;
  191. }
  192. return null;
  193. }
  194. function luc_GetOS($arg)
  195. {
  196. $arg = str_replace(" ", "", $arg);
  197. $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/os.dat');
  198. foreach ($lines as $line_num => $os)
  199. {
  200. list ($os_name, $os_id) = explode("|", $os);
  201. if (stripos($arg, $os_id) === false)
  202. continue;
  203. return $os_name;
  204. }
  205. return '';
  206. }
  207. function luc_GetBrowser($arg)
  208. {
  209. $arg = str_replace(" ", "", $arg);
  210. $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/browser.dat');
  211. foreach ($lines as $line_num => $browser)
  212. {
  213. list ($name, $id) = explode("|", $browser);
  214. if (stripos($arg, $id) === false)
  215. continue;
  216. return $name;
  217. }
  218. return '';
  219. }
  220. function luc_GetQueryPairs($url)
  221. {
  222. $parsed_url = parse_url($url);
  223. $tab = parse_url($url);
  224. $host = $tab['host'];
  225. if (key_exists("query", $tab))
  226. {
  227. $query = $tab["query"];
  228. $query = str_replace("&amp;", "&", $query);
  229. $query = urldecode($query);
  230. $query = str_replace("?", "&", $query);
  231. return explode("&", $query);
  232. }
  233. else
  234. {
  235. return null;
  236. }
  237. }
  238. function luc_CheckBanIP($arg)
  239. {
  240. if (file_exists(STATPRESS_V_PLUGIN_PATH . '-custom/banips.dat'))
  241. $lines = file(STATPRESS_V_PLUGIN_PATH . '-custom/banips.dat');
  242. else
  243. $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/banips.dat');
  244. if ($lines !== false)
  245. {
  246. foreach ($lines as $banip)
  247. {
  248. if (@ preg_match('/^' . rtrim($banip, "\r\n") . '$/', $arg))
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. function luc_CheckSpamBot($agent = null)
  255. {
  256. $agent = str_replace(" ", "", $agent);
  257. $key = null;
  258. $lines = file(STATPRESS_V_PLUGIN_PATH . '/def/spambot.dat');
  259. if (file_exists(STATPRESS_V_PLUGIN_PATH . '-custom/spambot.dat'))
  260. $lines = array_merge($lines, file(STATPRESS_V_PLUGIN_PATH . '-custom/spambot.dat'));
  261. foreach ($lines as $line_num => $spambot)
  262. {
  263. list ($name, $key) = explode("|", $spambot);
  264. if (stripos($agent, $key) === false)
  265. continue;
  266. return $name;
  267. }
  268. return null;
  269. }
  270. function luc_StatPressV_Is_Feed($url)
  271. {
  272. if (stristr($url, get_bloginfo('comments_atom_url')) != FALSE)
  273. return 'COMMENT ATOM';
  274. elseif (stristr($url, get_bloginfo('comments_rss2_url')) != FALSE)
  275. return 'COMMENT RSS';
  276. elseif (stristr($url, get_bloginfo('rdf_url')) != FALSE)
  277. return 'RDF';
  278. elseif (stristr($url, get_bloginfo('atom_url')) != FALSE)
  279. return 'ATOM';
  280. elseif (stristr($url, get_bloginfo('rss_url')) != FALSE)
  281. return 'RSS';
  282. elseif (stristr($url, get_bloginfo('rss2_url')) != FALSE)
  283. return 'RSS2';
  284. elseif (stristr($url, 'wp-feed.php') != FALSE)
  285. return 'RSS2';
  286. else if (stristr($url, '/feed') != FALSE)
  287. return 'RSS2';
  288. return '';
  289. }
  290. function luc_BotScout_Check_IP($ip)
  291. {
  292. global $StatPressV_Option;
  293. $apikey = $StatPressV_Option['StatPressV_BotScout_API_Key'];
  294. $apiquery = "http://botscout.com/test/?key=$apikey&ip=$ip";
  295. $data = file_get_contents($apiquery);
  296. // take the returned value and parse it (standard API, not XML)
  297. $botdata = explode('|', $data);
  298. // $botdata[0] = Y|N
  299. // $botdata[1] = IP
  300. // $botdata[2] = Count in database
  301. if ($botdata == 'Y')
  302. return true;
  303. return false;
  304. }
  305. function luc_get_ip()
  306. {
  307. if ($_SERVER)
  308. {
  309. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && luc_ip_not_private($_SERVER['HTTP_X_FORWARDED_FOR']))
  310. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  311. elseif (isset($_SERVER['HTTP_CLIENT_IP']) && luc_ip_not_private($_SERVER['HTTP_CLIENT_IP']))
  312. $ip = $_SERVER['HTTP_CLIENT_IP'];
  313. else
  314. $ip = $_SERVER['REMOTE_ADDR'];
  315. }
  316. else
  317. {
  318. if (getenv('HTTP_X_FORWARDED_FOR') && luc_ip_not_private(getenv('HTTP_X_FORWARDED_FOR')))
  319. $ip = getenv('HTTP_X_FORWARDED_FOR');
  320. elseif (getenv('HTTP_CLIENT_IP') && luc_ip_not_private(getenv('HTTP_CLIENT_IP')))
  321. $ip = getenv('HTTP_CLIENT_IP');
  322. else
  323. $ip = getenv('REMOTE_ADDR');
  324. }
  325. return $ip;
  326. }
  327. function luc_ip_not_private($ip)
  328. {
  329. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE))
  330. return true;
  331. return false;
  332. }
  333. ?>