Просмотр файла zagrcent/onl/online.php

Размер файла: 12.88Kb
  1. <?php
  2. class online{
  3. var $user_agent;
  4. var $remote_addr;
  5. var $position;
  6. function online(){
  7. $db_user = 'root';
  8. $db_pass = '';
  9. $db_name = 'zagr';
  10. if (!$this->online = @mysql_connect('localhost',$db_user,$db_pass)){
  11. $this->online = false;
  12. }
  13. else{
  14. if (!mysql_select_db($db_name,$this->online)){
  15. $this->online = false;
  16. }
  17. else{
  18. if (mysql_query("CREATE TABLE `online` (
  19. `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
  20. `position` VARCHAR( 100 ) NOT NULL ,
  21. `time` INT( 30 ) NOT NULL ,
  22. `user_agent` VARCHAR( 100 ) NOT NULL ,
  23. `remote_addr` VARCHAR( 30 ) NOT NULL ,
  24. PRIMARY KEY ( `id` )
  25. ) TYPE = MYISAM CHARACTER SET cp1251 COLLATE cp1251_general_ci;")){
  26. $this->online = false;
  27. }
  28. if (mysql_query("CREATE TABLE `hosts` (
  29. `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
  30. `remote_addr` VARCHAR( 20 ) NOT NULL ,
  31. `user_agent` VARCHAR( 100 ) NOT NULL ,
  32. `date1` INT( 10 ) NOT NULL ,
  33. `date2` INT( 10 ) NOT NULL ,
  34. PRIMARY KEY ( `id` )
  35. ) TYPE = MYISAM CHARACTER SET cp1251 COLLATE cp1251_general_ci;")){
  36. $this->online = false;
  37. }
  38. if (mysql_query("CREATE TABLE `hits` (
  39. `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
  40. `remote_addr` VARCHAR( 20 ) NOT NULL ,
  41. `user_agent` VARCHAR( 100 ) NOT NULL ,
  42. `date1` INT( 10 ) NOT NULL ,
  43. `date2` INT( 10 ) NOT NULL ,
  44. PRIMARY KEY ( `id` )
  45. ) TYPE = MYISAM CHARACTER SET cp1251 COLLATE cp1251_general_ci;")){
  46. $this->online = false;
  47. }
  48. if (mysql_query('ALTER TABLE `online` ADD `visit` INT( 30 ) NOT NULL ;')){
  49. if (mysql_query('ALTER TABLE `online` ADD `referer` VARCHAR( 50 ) NOT NULL ;')){
  50. $this->online = false;
  51. }
  52. }
  53. }
  54. }
  55. if (!empty($this->online)){
  56. mysql_query('DELETE FROM `online` WHERE `time` < "'.(time()-600).'"',$this->online);
  57. mysql_query('DELETE FROM `hits` WHERE `date1` != "'.date('m',time()).'"');
  58. mysql_query('DELETE FROM `hosts` WHERE `date1` != "'.date('m',time()).'"');
  59. mysql_query('SET `character_set_client` = "cp1251"',$this->online);
  60. mysql_query('SET `character_set_results` = "cp1251"',$this->online);
  61. mysql_query('SET `collation_connection` = "cp1251_general_ci"',$this->online);
  62. }
  63. }
  64. function user_agent(){
  65. if (!empty($_SERVER['HTTP_USER_AGENT'])){
  66. $this->user_agent = $_SERVER['HTTP_USER_AGENT'];
  67. if (strstr($this->user_agent,'\"') || strstr($this->user_agent,"\'")){
  68. $this->user_agent = stripslashes($this->user_agent);
  69. }
  70. $this->user_agent = str_replace("$","$$",str_replace("\r","",str_replace("\n","",$this->user_agent)));
  71. $this->user_agent = strtr($this->user_agent,array(chr(0)=>'',chr(1)=>'',chr(2)=>'',chr("3")=>"",chr("4")=>"",chr("5")=>"",chr("6")=>"",chr("7")=>"",chr("8")=>"",chr("9")=>"",chr("10")=>"",chr("11")=>"",chr("12")=>"",chr("13")=>"",chr("14")=>"",chr("15")=>"",chr("16")=>"",chr("17")=>"",chr("18")=>"",chr("19")=>"",chr("20")=>"",chr("21")=>"",chr("22")=>"",chr("23")=>"",chr("24")=>"",chr("25")=>"",chr("26")=>"",chr("27")=>"",chr("28")=>"",chr("29")=>"",chr("30")=>"",chr("31")=>""));
  72. $this->user_agent = htmlspecialchars($this->user_agent);
  73. $this->user_agent = mysql_escape_string($this->user_agent);
  74. return $this->user_agent;
  75. }
  76. }
  77. function add($position = 'на главной странице.'){
  78. if (!empty($this->online)){
  79. $this->user_agent();
  80. if (!empty($this->user_agent)){
  81. $this->position = $position;
  82. $this->remote_addr = is_numeric(str_replace('.','',$_SERVER['REMOTE_ADDR'])) ? mysql_escape_string($_SERVER['REMOTE_ADDR']) : '127.0.0.1';
  83. if ($select = mysql_fetch_object(mysql_query('SELECT `position` FROM `online` WHERE `user_agent` = "'.$this->user_agent.'" AND `remote_addr` = "'.$this->remote_addr.'"',$this->online))){
  84. if ($this->position != $select->position){
  85. mysql_query('UPDATE `online` SET `position` = "'.addslashes(htmlspecialchars($position)).'", `time` = "'.time().'" WHERE `user_agent` = "'.$this->user_agent.'" AND `remote_addr` = "'.$this->remote_addr.'"',$this->online);
  86. }
  87. else{
  88. mysql_query('UPDATE `online` SET `time` = "'.time().'" WHERE `user_agent` = "'.$this->user_agent.'" AND `remote_addr` = "'.$this->remote_addr.'"',$this->online);
  89. }
  90. }
  91. else{
  92. $referer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
  93. if (strstr($referer,'http://')){
  94. $referer = str_replace('http://','',$referer);
  95. }
  96. $referer = explode('/',$referer);
  97. $referer = $referer['0'];
  98. if (!preg_match('/[^a-zA-Z0-9\-.]/i', $referer) && (strlen($referer) >= 5) && strlen($referer) <= 30){
  99. if (@file_get_contents('http://'.$referer)){
  100. $referer = 'от '.strtolower(mysql_escape_string($referer));
  101. }
  102. else{
  103. $referer = 'от нечего делать.';
  104. }
  105. }
  106. else{
  107. $referer = 'от нечего делать';
  108. }
  109. mysql_query('INSERT INTO `online` SET `position` = "'.addslashes(htmlspecialchars($position)).'", `time` = "'.time().'", `user_agent` = "'.$this->user_agent.'", `remote_addr` = "'.$this->remote_addr.'", `referer` = "'.$referer.'", `visit` = "'.time().'"',$this->online);
  110. }
  111. if (!mysql_fetch_object(mysql_query('SELECT `id` FROM `hosts` WHERE `user_agent` = "'.$this->user_agent.'" AND `remote_addr` = "'.$this->remote_addr.'" AND `date1` = "'.date('m',time()).'" AND `date2` = "'.date('d',time()).'" ORDER BY `id` DESC'))){
  112. mysql_query('INSERT INTO `hosts` SET `user_agent` = "'.$this->user_agent.'", `remote_addr` = "'.$this->remote_addr.'", `date1` = "'.date('m',time()).'", `date2` = "'.date('d',time()).'"');
  113. }
  114. mysql_query('INSERT INTO `hits` SET `user_agent` = "'.$this->user_agent.'", `remote_addr` = "'.$this->remote_addr.'", `date1` = "'.date('m',time()).'", `date2` = "'.date('d',time()).'"');
  115. }
  116. }
  117. }
  118. function listing($number = '10'){
  119. if (!empty($this->online)){
  120. if (isset($_GET['p']) && !is_array($_GET['p'])){
  121. $p = intval($_GET['p']);
  122. }
  123. else{
  124. $p = '0';
  125. }
  126. $count = mysql_query('SELECT COUNT(`id`) FROM `online`',$this->online);
  127. list($total_links) = mysql_fetch_array($count);
  128. $total_pages = ceil($total_links/$number);
  129. $p = ($total_links <= $number && $p != '0') ? '0' : $p;
  130. $p = ($p < 0 || $p > ($total_pages-1)) ? '0' : $p;
  131.  
  132. $while = mysql_query('SELECT `user_agent`, `position`, `remote_addr`, `time`, `referer`, `visit` FROM `online` ORDER BY `time` DESC LIMIT '.($number*$p).', '.$number,$this->online);
  133. if (mysql_num_rows($while) != '0'){
  134. $c = '0';
  135. while($obj = mysql_fetch_object($while)){
  136. $us_agent = stripslashes($obj->user_agent);
  137. $us_agent = explode('/',$us_agent);
  138. $us_agent = $us_agent['0'];
  139. $mtime = date('i:s',(time()-$obj->time));
  140. if ((time()-$obj->time) == '0'){
  141. $mtime = 'только что обновил страницу.';
  142. }
  143. else
  144. if ((time()-$obj->time) >= 60){
  145. $mtime = $mtime.' мин. назад обновил страницу.';
  146. }
  147. else{
  148. $mtime = $mtime.' сек. назад обновил страницу.';
  149. }
  150. $c++;
  151. if ($c%2 == '0'){
  152. echo '<div style="background-color:#A2C9F4;color:#114F93;border:1px solid #114F93;">'.$us_agent.' - '.$mtime.'<hr />'.
  153. stripslashes($obj->user_agent).'( '.stripslashes($obj->remote_addr).' )<br />'.
  154. 'Пришел к нам '.stripslashes($obj->referer).' в '.date('H:i:s',$obj->visit).'<br />'.
  155. 'Был замечен '.date('d.m H:i:s',$obj->time).' '.stripslashes($obj->position).'<br /></div>';
  156. }
  157. else{
  158. echo '<div style="background-color:#114F93;color:#A2C9F4;border:1px solid #A2C9F4;">'.$us_agent.' - '.$mtime.'<hr />'.
  159. stripslashes($obj->user_agent).'( '.stripslashes($obj->remote_addr).' )<br />'.
  160. 'Пришел к нам '.stripslashes($obj->referer).' в '.date('H:i:s',$obj->visit).'<br />'.
  161. 'Был замечен '.date('d.m H:i:s',$obj->time).' '.stripslashes($obj->position).'</div>';
  162. }
  163. }
  164. if ($total_links > $number){
  165. echo '<p><hr />';
  166. if ($p > '0'){
  167. echo '<a href="'.rootlink.'who.php?p='.($p-1).'" style="color:#2C75B1;">Назад('.($p-1).')</a>';
  168. }
  169. if ($p < ($total_pages-1)){
  170. echo '<a href="'.rootlink.'who.php?p='.($p+1).'" style="color:#2C75B1;">Далее('.($p+1).')</a>';
  171. }
  172. echo '</p>';
  173. }
  174. }
  175. else{
  176. echo '<p>На сайте никого нет.</p>';
  177. }
  178. }
  179. else{
  180. echo '<p>Соединение с базой не установлено.</p>';
  181. }
  182. }
  183. function statistic(){
  184. $hits = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hits` FROM `hits` WHERE `date2` = "'.date('d',time()).'"'));
  185. if ($hits->hits != '0'){
  186. $hosts = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hosts` FROM `hosts` WHERE `date2` = "'.date('d',time()).'"'));
  187. $allhosts = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hosts` FROM `hosts` WHERE `date1` = "'.date('m',time()).'"'));
  188. $allhits = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hits` FROM `hits` WHERE `date1` = "'.date('m',time()).'"'));
  189. $rhits = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hits` FROM `hits` WHERE `date1` = "'.date('m',time()).'" AND `remote_addr` = "'.$this->remote_addr.'"'));
  190. $rhosts = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hosts` FROM `hosts` WHERE `date1` = "'.date('m',time()).'" AND `remote_addr` = "'.$this->remote_addr.'"'));
  191. $ronline = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `onl` FROM `online` WHERE `remote_addr` = "'.$this->remote_addr.'"'));
  192. echo '<div style="background-color:#114F93;color:#A2C9F4;border:1px solid #A2C9F4;">'.
  193. 'Сегодня обработано:<br />'.
  194. 'Хитов '.$hits->hits.'<br />'.
  195. 'Хостов '.$hosts->hosts.'<br />'.
  196. 'За текущий месяц обработано:<br />'.
  197. 'Хитов '.$allhits->hits.'<br />'.
  198. 'Хостов '.$allhosts->hosts.'<br />'.
  199. 'Всего с текущего ip:<br />'.
  200. 'Хитов '.$rhits->hits.'<br />'.
  201. 'Хостов '.$rhosts->hosts.'<br />'.
  202. 'Сейчас online '.$ronline->onl.'<br />'.
  203. '</div>';
  204. }
  205. }
  206. function a_header($title = 'Список браузеров, online.'){
  207. if (strstr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml')){
  208. header("Content-type:application/xhtml+xml; charset=utf-8");
  209. }
  210. else{
  211. header("Content-type:text/html; charset=utf-8");
  212. }
  213. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  214. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  215. header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
  216. header('Pragma: no-cache');
  217. echo '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">'.
  218. '<head><title>'.$title.'</title><meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=utf-8" />'.
  219. '<meta http-equiv="Cache-Control" content="no-cache,no-store,must-revalidate" />'.
  220. '<meta http-equiv="Pragma" content="no-cache" />'.
  221. '<style type="text/css">'.
  222. 'body {font-family: arial; font-size: 12px; color: #000000; background-color: #ffffff;}'.
  223. 'a {color:#2C75B1;}a:visited{color:#2C75B1;}</style>'.
  224. '</head><body>';
  225. }
  226. function count_onl(){
  227. if (!empty($this->online)){
  228. $count_onl = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `id` FROM `online`',$this->online));
  229. if (strstr($_SERVER['SCRIPT_NAME'],'who.php')){
  230. if (isset($_GET['statistic'])){
  231. echo '<p><br /><a href="'.rootlink.'who.php">Список online</a>'.
  232. '<br /><a href="'.rootlink.'">На главную</a></p>';
  233. }
  234. else{
  235. echo '<p><br /><a href="'.rootlink.'who.php?statistic">Статистика</a>'.
  236. '<br /><a href="'.rootlink.'">На главную</a></p>';
  237. }
  238. }
  239. else{
  240. echo '<p><br /><a href="'.rootlink.'who.php">Сейчас на сайте '.$count_onl->id.'</a></p>';
  241. }
  242. }
  243. }
  244. function end_online(){
  245. if (!empty($this->online)){
  246. mysql_close($this->online);
  247. }
  248. echo '</body></html>';
  249. }
  250. function image(){
  251. if (!empty($this->online)){
  252. $count_onl = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `id` FROM `online`',$this->online));
  253. header("Content-type: image/jpeg");
  254. $string = $count_onl->id;
  255. $im = imagecreatefromjpeg('/home/dc/www/online/online/online/onl/image.jpeg');
  256. $grey = imagecolorallocate($im, 128, 128, 128);
  257. $black = imagecolorallocate($im, 0, 0, 0);
  258. $font = '/home/dc/www/online/online/online/onl/font.ttf';
  259.  
  260. imagettftext($im, 18,5,3,31, $grey, $font, $string);
  261. imagettftext($im, 18,5,2,30, $black, $font, $string);
  262. imagejpeg($im);
  263. imagedestroy($im);
  264. }
  265. }
  266. function add_image(){
  267. echo '<img src="'.rootlink.'view.php?'.rand(100000,900000).'" alt="" />';
  268. }
  269. }
  270. ?>