PageRenderTime 23ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/guest_counter.php

https://github.com/Nijikokun/NinkoBB
PHP | 229 lines | 145 code | 26 blank | 58 comment | 13 complexity | 0dcdc09bb7aa4256fd16d3a033e35211 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Guest Counter
  4. Description: Counts Guests / Bots Online
  5. Version: 1.0
  6. Author: Nijiko
  7. Author URI: http://ninkobb.com
  8. */
  9. /**
  10. * Add hook to common to update guests
  11. */
  12. add_hook('common', 'update_guests', array());
  13. /**
  14. * List of bots
  15. */
  16. $config['bot_list'] = array(
  17. 'AdsBot [Google]' => 'AdsBot\-Google',
  18. 'Alexa [Bot]' => 'ia\_archiver',
  19. 'Alta Vista [Bot]' => 'Scooter/',
  20. 'Ask Jeeves [Bot]' => 'Ask Jeeves',
  21. 'Baidu [Spider]' => 'Baiduspider\+\(',
  22. 'Exabot [Bot]' => 'Exabot/',
  23. 'FAST Enterprise [Crawler]' => 'FAST Enterprise Crawler',
  24. 'FAST WebCrawler [Crawler]' => 'FAST\-WebCrawler/',
  25. 'Francis [Bot]' => 'http://www.neomo.de/',
  26. 'Gigabot [Bot]' => 'Gigabot/',
  27. 'Google Adsense [Bot]' => 'Mediapartners-Google',
  28. 'Google Desktop' => 'Google Desktop',
  29. 'Google Feedfetcher' => 'Feedfetcher-Google',
  30. 'Google [Bot]' => 'Googlebot',
  31. 'Heise IT-Markt [Crawler]' => 'heise\-IT\-Markt-Crawler',
  32. 'Heritrix [Crawler]' => 'heritrix/1\.',
  33. 'IBM Research [Bot]' => 'ibm.com/cs/crawler',
  34. 'ICCrawler - ICjobs' => 'ICCrawler - ICjobs',
  35. 'ichiro [Crawler]' => 'ichiro/2',
  36. 'Majestic-12 [Bot]' => 'MJ12bot/',
  37. 'Metager [Bot]' => 'MetagerBot/',
  38. 'MSN NewsBlogs' => 'msnbot-NewsBlogs/',
  39. 'MSN [Bot]' => 'msnbot/',
  40. 'MSNbot Media' => 'msnbot\-media/',
  41. 'NG-Search [Bot]' => 'NG-Search/',
  42. 'Nutch [Bot]' => 'http\://lucene.apache.org/nutch/',
  43. 'Nutch/CVS [Bot]' => 'NutchCVS/',
  44. 'OmniExplorer [Bot]' => 'OmniExplorer_Bot/',
  45. 'Online link [Validator]' => 'online link validator',
  46. 'psbot [Picsearch]' => 'psbot/0',
  47. 'Seekport [Bot]' => 'Seekbot/',
  48. 'Sensis [Crawler]' => 'Sensis Web Crawler',
  49. 'SEO Crawler' => 'SEO search Crawler/',
  50. 'Seoma [Crawler]' => 'Seoma \[SEO Crawler\]',
  51. 'SEOSearch [Crawler]' => 'SEOsearch/',
  52. 'Snappy [Bot]' => 'Snappy/1.1 \( http://www.urltrends.com/ \)',
  53. 'Steeler [Crawler]' => 'http://www.tkl.iis.u-tokyo.ac.jp/~crawler/',
  54. 'Synoo [Bot]' => 'SynooBot/',
  55. 'Telekom [Bot]' => 'crawleradmin.t-info@telekom.de',
  56. 'TurnitinBot [Bot]' => 'TurnitinBot/',
  57. 'Voyager [Bot]' => 'voyager/1.0',
  58. 'W3 [Sitesearch]' => 'W3 SiteSearch Crawler',
  59. 'W3C [Linkcheck]' => 'W3C-checklink/',
  60. 'W3C [Validator]' => 'W3C_*Validator',
  61. 'WiseNut [Bot]' => 'http://www.WISEnutbot.com',
  62. 'YaCy [Bot]' => 'yacybot',
  63. 'Yahoo MMCrawler [Bot]' => 'Yahoo-MMCrawler/',
  64. 'Yahoo Slurp [Bot]' => 'Yahoo! DE Slurp',
  65. 'Yahoo [Bot]' => 'Yahoo! Slurp',
  66. 'YahooSeeker [Bot]' => 'YahooSeeker/',
  67. );
  68. /**
  69. * Installs Guest counter
  70. * @global resource
  71. */
  72. function install_guest_counter()
  73. {
  74. global $database;
  75. // Create the table `guests`
  76. $database->query("CREATE TABLE IF NOT EXISTS `guests` (`ip` text NOT NULL, `visit` text NOT NULL, `type` text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;") or die(mysql_error());
  77. }
  78. /**
  79. * Installs Guest counter
  80. * @global resource
  81. */
  82. function uninstall_guest_counter()
  83. {
  84. global $database;
  85. // Delete
  86. $database->query("DROP TABLE IF EXISTS `guests`") or die(mysql_error());
  87. }
  88. /**
  89. * Cleans up the guest array
  90. * @global array
  91. * @param string $referrer
  92. * @return boolean
  93. */
  94. function is_bot($referrer)
  95. {
  96. global $config;
  97. foreach($config['bot_list'] as $key => $bot)
  98. {
  99. if(preg_match("#{$bot}#iU", $referrer))
  100. {
  101. return $key;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Cleans up the guest array
  108. * @global array
  109. * @global resource
  110. */
  111. function update_guests()
  112. {
  113. global $config, $database;
  114. // The time between them
  115. $time_between = time() - $config['user_online_timeout'];
  116. $time = time();
  117. // Clean up the database of old guests
  118. $result = $database->query("DELETE FROM `guests` WHERE `visit` < '{$time_between}'");
  119. // Insert a new one
  120. if(!$_SESSION['logged_in'])
  121. {
  122. $bot_check = is_bot($_SERVER["HTTP_USER_AGENT"]);
  123. // Are they a bot or a guest?
  124. if(is_string($bot_check))
  125. {
  126. $type = $bot_check;
  127. }
  128. else
  129. {
  130. $type = "GUEST";
  131. }
  132. // Grab the hostname
  133. $host = gethostname();
  134. // Check to see if they already exist.
  135. $result = $database->query( "SELECT * FROM `guests` WHERE `ip` = '{$host}'" );
  136. if($database->num($result) < 1)
  137. {
  138. // Insert them in there.
  139. $database->query("INSERT INTO `guests` (`visit`,`ip`,`type`) VALUES ('{$time}', '{$host}', '{$type}')");
  140. }
  141. else
  142. {
  143. // Insert them in there.
  144. $database->query("UPDATE `guests` SET `visit` = '{$time}' WHERE `ip` = '{$host}'");
  145. }
  146. }
  147. }
  148. /**
  149. * Checks to see what guests are online, can check for all together or bots alone.
  150. * @global array
  151. * @global resource
  152. * @param boolean $all check for all together?
  153. * @param boolean $bots just for bots?
  154. * @return array
  155. */
  156. function guests_online($all = false, $bots = false)
  157. {
  158. global $config, $database;
  159. if($all)
  160. {
  161. $result = $database->query( "SELECT * FROM `guests` ORDER BY `visit`" );
  162. }
  163. else if($bots)
  164. {
  165. $result = $database->query( "SELECT * FROM `guests` WHERE `type` != 'GUEST' ORDER BY `visit`" );
  166. }
  167. else
  168. {
  169. $result = $database->query( "SELECT * FROM `guests` WHERE `type` = 'GUEST' ORDER BY `visit`" );
  170. }
  171. // is there a result?
  172. if($database->num($result) < 1)
  173. {
  174. return array('count' => 0, 'users' => false);
  175. }
  176. else
  177. {
  178. // The overall count
  179. $online['count'] = $database->num($result);
  180. $count = 1;
  181. // Making the list
  182. if($bot || $all)
  183. {
  184. while($row = $database->fetch($result))
  185. {
  186. if($row['type'] != "GUEST")
  187. {
  188. // the seperator
  189. $seperator = ", ";
  190. // the bot
  191. $username = "<span class='bot'>{$row['type']}</span>";
  192. // add to list
  193. $online['users'] .= "{$username}{$seperator}";
  194. }
  195. }
  196. // simple fix for commas
  197. $online['users'] = substr($online['users'], 0, -2);
  198. }
  199. // Returns array
  200. return $online;
  201. }
  202. }
  203. ?>