net.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Finds out the protocol the user is connecting to the site with.
  4. *
  5. * @return string "http://" or "https://".
  6. */
  7. function get_protocol(){
  8. if (isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on" || $_SERVER["HTTPS"] == 1) || isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] == "https") {
  9. $protocol = "https://";
  10. }
  11. else {
  12. $protocol = "http://";
  13. }
  14. return $protocol;
  15. }
  16. /**
  17. * Finds out the IP address of the client.
  18. *
  19. * @return string Client IP address.
  20. */
  21. function get_ip(){
  22. $client = @$_SERVER["HTTP_CLIENT_IP"];
  23. $forward = @$_SERVER["HTTP_X_FORWARDED_FOR"];
  24. $remote = $_SERVER["REMOTE_ADDR"];
  25. if(filter_var($client, FILTER_VALIDATE_IP)){
  26. $ip = $client;
  27. }
  28. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  29. $ip = $forward;
  30. }
  31. else{
  32. $ip = $remote;
  33. }
  34. return $ip;
  35. }
  36. /**
  37. * Registers the page viewed by the user. If it is first page shown in his
  38. * visit, it also registers the visit.
  39. *
  40. * @param MySQL_connection $db Connection to the database.
  41. * @param string $section Site section being visited.
  42. * @param string $id ID of the entry being visited.
  43. */
  44. function stats($db, $section, $id){
  45. // Get client data
  46. $ip = get_ip();
  47. $browser_data = get_browser(null, true);
  48. $os = $browser_data["platform"];
  49. $browser = $browser_data["browser"];
  50. $uagent = $browser_data["browser_name_pattern"];
  51. //If bot, do nothing
  52. $bot_kw = Array();
  53. $bot_kw[0] = "bot";
  54. $bot_kw[1] = "spider";
  55. $bot_kw[2] = "crawl";
  56. $bot_kw[3] = ".com";
  57. $bot_kw[4] = ".ru";
  58. $bot_kw[5] = "baidu";
  59. $bot_kw[6] = "survey";
  60. $bot_kw[7] = "scan";
  61. $bot_kw[8] = "feed";
  62. $bot_kw[9] = "bing";
  63. $bot_kw[10] = "yahoo";
  64. $bot_kw[11] = "engine";
  65. $bot_kw[12] = "preview";
  66. $bot_kw[13] = "checker";
  67. $bot_kw[14] = "catalog";
  68. $bot_kw[15] = "accelerator";
  69. $bot_kw[16] = "python";
  70. $bot_kw[14] = "qt";
  71. $bot_kw[15] = "webdav";
  72. $bot_kw[16] = "http";
  73. $bot_kw[17] = "url";
  74. $bot_kw[18] = "fake";
  75. $bot_kw[19] = "library";
  76. $bot_kw[20] = "commerce";
  77. $bot_kw[21] = "htmlbot";
  78. $bot_kw[22] = "fetch";
  79. $bot_kw[23] = "googleb";
  80. $bot_kw[24] = "facebook";
  81. $bot_kw[25] = "whatsapp";
  82. $bot_kw[26] = "pinterest";
  83. $bot_kw[27] = "scrapy";
  84. $bot_kw[28] = "libwww";
  85. $bot_kw[29] = "java standard library";
  86. $bot_kw[30] = "default browser";
  87. $bot_kw[31] = "twingly recon";
  88. $bot_kw[32] = "siteexplorer";
  89. $bot_kw[33] = "yandex";
  90. $bot_kw[34] = "wotbox";
  91. $bot_kw[35] = "apache synapse";
  92. $bot_kw[36] = "catexplorador";
  93. $bot_kw[37] = "internet archive";
  94. if (in_array(strtolower($uagent), $bot_kw)){
  95. return;
  96. }
  97. //Look for a visit with the same IP in the last 30 mins.
  98. $q = mysqli_query($db, "SELECT stat_visit.id AS visitid FROM stat_view, stat_visit WHERE visit = stat_visit.id AND stat_view.dtime > DATE_SUB(now(), INTERVAL 30 MINUTE) AND ip = '$ip' AND uagent = '$uagent';");
  99. if (mysqli_num_rows($q) == 0){
  100. mysqli_query($db, "INSERT INTO stat_visit (ip, uagent, os, browser) VALUES ('$ip', '$uagent', '$os', '$browser');");
  101. $q = mysqli_query($db, "SELECT stat_visit.id AS visitid FROM stat_visit WHERE ip = '$ip' AND uagent = '$uagent' ORDER BY stat_visit.id DESC LIMIT 1;");
  102. }
  103. $r = mysqli_fetch_array($q);
  104. $visit = $r['visitid'];
  105. mysqli_query($db, "INSERT INTO stat_view (visit, section, entry) VALUES ($visit, '$section', '$id');");
  106. }
  107. ?>