PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/whoisonline/modul.php

http://avecms.googlecode.com/
PHP | 200 lines | 146 code | 32 blank | 22 comment | 14 complexity | c6d0cbca5070db0bf400331c6893e70d MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * AVE.cms - ?????? Who is online
  4. *
  5. * @package AVE.cms
  6. * @subpackage module_WhoIsOnline
  7. * @since 2.09
  8. * @filesource
  9. */
  10. if (!defined('BASE_DIR')) exit;
  11. if (defined('ACP'))
  12. {
  13. $modul['ModulName'] = 'Who is online';
  14. $modul['ModulPfad'] = 'whoisonline';
  15. $modul['ModulVersion'] = '1.0';
  16. $modul['description'] = '?????? ?????? ???????????? ??? ??????????? ?????????????? ?? ????? ????????????? ? ???-???????????.';
  17. $modul['Autor'] = '&copy;';
  18. $modul['MCopyright'] = '&copy; 2007-2010 Overdoze.Ru';
  19. $modul['Status'] = 1;
  20. $modul['IstFunktion'] = 1;
  21. $modul['ModulTemplate'] = 0;
  22. $modul['AdminEdit'] = 0;
  23. $modul['ModulFunktion'] = 'mod_online';
  24. $modul['CpEngineTagTpl'] = '[mod_online]';
  25. $modul['CpEngineTag'] = '#\\\[mod_online]#';
  26. $modul['CpPHPTag'] = "<?php mod_online(); ?>";
  27. }
  28. function mod_online()
  29. {
  30. ?>
  31. <link rel="stylesheet" type="text/css" href="<?php echo ABS_PATH; ?>modules/whoisonline/css/styles.css" />
  32. <!-- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> -->
  33. <script type="text/javascript" src="<?php echo ABS_PATH; ?>modules/whoisonline/js/widget.js"></script>
  34. <div class="onlineWidget">
  35. <div class="panel"><img class="preloader" src="<?php echo ABS_PATH; ?>modules/whoisonline/images/preloader.gif" alt="Loading.." width="22" height="22" /></div>
  36. <div class="count"></div>
  37. <div class="label">??????</div>
  38. <div class="arrow"></div>
  39. </div>
  40. <?php
  41. }
  42. if (!defined('ACP') && isset($_REQUEST['module']) && $_REQUEST['module'] == 'whoisonline')
  43. {
  44. function get_tag($tag, $xml)
  45. {
  46. $match = array();
  47. preg_match_all('/<' . $tag . '>(.*)<\/' . $tag . '>$/imU', $xml, $match);
  48. return $match[1];
  49. }
  50. function is_bot()
  51. {
  52. /* This function will check whether the visitor is a search engine robot */
  53. $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
  54. "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
  55. "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
  56. "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
  57. "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
  58. "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
  59. "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler",
  60. "TweetmemeBot", "Butterfly", "Twitturls", "Me.dium", "Twiceler");
  61. foreach ($botlist as $bot)
  62. {
  63. if (strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; // Is a bot
  64. }
  65. return false; // Not a bot
  66. }
  67. if (empty($_REQUEST['action']) || is_bot()) die();
  68. switch ($_REQUEST['action'])
  69. {
  70. case 'online':
  71. $stringIp = $_SERVER['REMOTE_ADDR'];
  72. $intIp = ip2long($stringIp);
  73. // Checking wheter the visitor is already marked as being online:
  74. $counted = $AVE_DB->Query("
  75. SELECT 1
  76. FROM " . PREFIX . "_modul_who_is_online
  77. WHERE ip = " . $intIp
  78. )->NumRows();
  79. if (! $counted)
  80. {
  81. // This user is not in the database, so we must fetch
  82. // the geoip data and insert it into the online table:
  83. if (!empty($_COOKIE['geoData']))
  84. {
  85. // A "geoData" cookie has been previously set by the script, so we will use it
  86. // Always escape any user input, including cookies:
  87. list($city, $countryName, $countryAbbrev) = explode('|', stripslashes(strip_tags($_COOKIE['geoData'])));
  88. }
  89. else
  90. {
  91. // Making an API call to Hostip:
  92. $xml = file_get_contents('http://api.hostip.info/?ip=' . $stringIp);
  93. $city = get_tag('gml:name', $xml);
  94. $city = $city[1];
  95. $countryName = get_tag('countryName', $xml);
  96. $countryName = $countryName[0];
  97. $countryAbbrev = get_tag('countryAbbrev', $xml);
  98. $countryAbbrev = $countryAbbrev[0];
  99. // Setting a cookie with the data, which is set to expire in a month:
  100. setcookie('geoData', $city . '|' . $countryName . '|' . $countryAbbrev, time()+60*60*24*30,'/');
  101. }
  102. $countryName = str_replace('(Unknown Country?)', 'UNKNOWN', $countryName);
  103. // In case the Hostip API fails:
  104. if (!$countryName)
  105. {
  106. $countryName = 'UNKNOWN';
  107. $countryAbbrev = 'XX';
  108. $city = '(Unknown City?)';
  109. }
  110. $AVE_DB->Query("
  111. INSERT INTO " . PREFIX . "_modul_who_is_online
  112. SET
  113. ip = " . $intIp . ",
  114. city = '" . addslashes($city) . "',
  115. country = '" . addslashes($countryName) . "',
  116. countrycode = '" . addslashes($countryAbbrev) . "'
  117. ");
  118. }
  119. else
  120. {
  121. // If the visitor is already online, just update the dt value of the row:
  122. $AVE_DB->Query("
  123. UPDATE " . PREFIX . "_modul_who_is_online
  124. SET dt = NOW()
  125. WHERE ip = " . $intIp
  126. );
  127. }
  128. // Removing entries not updated in the last 10 minutes:
  129. $AVE_DB->Query("
  130. DELETE
  131. FROM " . PREFIX . "_modul_who_is_online
  132. WHERE dt<SUBTIME(NOW(),'0 0:10:0')
  133. ");
  134. // Counting all the online visitors:
  135. list($totalOnline) = $AVE_DB->Query("
  136. SELECT COUNT(*)
  137. FROM " . PREFIX . "_modul_who_is_online
  138. ")->FetchArray();
  139. // Outputting the number as plain text:
  140. echo $totalOnline;
  141. exit;
  142. case 'geodata':
  143. // Selecting the top 15 countries with the most visitors:
  144. $sql = $AVE_DB->Query("
  145. SELECT
  146. countryCode,
  147. country,
  148. COUNT(*) AS total
  149. FROM " . PREFIX . "_modul_who_is_online
  150. GROUP BY countryCode
  151. ORDER BY total DESC
  152. LIMIT 15
  153. ");
  154. while ($row = $sql->FetchRow())
  155. {
  156. echo '
  157. <div class="geoRow">
  158. <div class="flag"><img src="' . ABS_PATH . 'modules/whoisonline/images/countryflags/' . mb_strtolower($row->countryCode) . '.gif" width="16" height="11" /></div>
  159. <div class="country" title="' . htmlspecialchars($row->country) . '">' . $row->country . '</div>
  160. <div class="people">' . $row->total . '</div>
  161. </div>
  162. ';
  163. }
  164. exit;
  165. }
  166. exit;
  167. }
  168. ?>