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

Размер файла: 12.62Kb
<?php
class online{
	var $user_agent;
	var $remote_addr;
	var $position;
	function online(){
		$db_user = 'root';
		$db_pass = '';
		$db_name = 'dc';
		if (!$this->online = @mysql_connect('localhost',$db_user,$db_pass)){
			$this->online = false;
		}
		else{
			if (!mysql_select_db($db_name,$this->online)){
				$this->online = false;
			}
			else{
				if (mysql_query("CREATE TABLE `online` (
				`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
				`position` VARCHAR( 100 ) NOT NULL ,
				`time` INT( 30 ) NOT NULL ,
				`user_agent` VARCHAR( 100 ) NOT NULL ,
				`remote_addr` VARCHAR( 30 ) NOT NULL ,
				PRIMARY KEY ( `id` )
				) TYPE = MYISAM CHARACTER SET cp1251 COLLATE cp1251_general_ci;")){
					$this->online = false;
				}
				if (mysql_query("CREATE TABLE `hosts` (
				`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
				`remote_addr` VARCHAR( 20 ) NOT NULL ,
				`user_agent` VARCHAR( 100 ) NOT NULL ,
				`date1` INT( 10 ) NOT NULL ,
				`date2` INT( 10 ) NOT NULL ,
				PRIMARY KEY ( `id` )
				) TYPE = MYISAM CHARACTER SET cp1251 COLLATE cp1251_general_ci;")){
					$this->online = false;
				}
				if (mysql_query("CREATE TABLE `hits` (
				`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
				`remote_addr` VARCHAR( 20 ) NOT NULL ,
				`user_agent` VARCHAR( 100 ) NOT NULL ,
				`date1` INT( 10 ) NOT NULL ,
				`date2` INT( 10 ) NOT NULL ,
				PRIMARY KEY ( `id` )
				) TYPE = MYISAM CHARACTER SET cp1251 COLLATE cp1251_general_ci;")){
					$this->online = false;
				}
				if (mysql_query('ALTER TABLE `online` ADD `visit` INT( 30 ) NOT NULL ;')){
					if (mysql_query('ALTER TABLE `online` ADD `referer` VARCHAR( 50 ) NOT NULL ;')){
						$this->online = false;
					}
				}
			}
		}
		if (!empty($this->online)){
			mysql_query('DELETE FROM `online` WHERE `time` < "'.(time()-600).'"',$this->online);
			mysql_query('DELETE FROM `hits` WHERE `date1` != "'.date('m',time()).'"');
			mysql_query('DELETE FROM `hosts` WHERE `date1` != "'.date('m',time()).'"');
			mysql_query('SET `character_set_client` = "cp1251"',$this->online);
			mysql_query('SET `character_set_results` = "cp1251"',$this->online);
			mysql_query('SET `collation_connection` = "cp1251_general_ci"',$this->online);
		}
	}
	function user_agent(){
		if (!empty($_SERVER['HTTP_USER_AGENT'])){
			$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
			if (strstr($this->user_agent,'\"') || strstr($this->user_agent,"\'")){
				$this->user_agent = stripslashes($this->user_agent);
			}
			$this->user_agent = str_replace("$","$$",str_replace("\r","",str_replace("\n","",$this->user_agent)));
			$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")=>""));
			$this->user_agent = htmlspecialchars($this->user_agent);
			$this->user_agent = mysql_escape_string($this->user_agent);
			return $this->user_agent;
		}
	}
	function add($position = 'на главной странице.'){
		if (!empty($this->online)){
			$this->user_agent();
			if (!empty($this->user_agent)){
				$this->position = $position;
				$this->remote_addr = is_numeric(str_replace('.','',$_SERVER['REMOTE_ADDR'])) ? mysql_escape_string($_SERVER['REMOTE_ADDR']) : '127.0.0.1';
				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))){
					if ($this->position != $select->position){
						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);
					}
					else{
						mysql_query('UPDATE `online` SET `time` = "'.time().'" WHERE `user_agent` = "'.$this->user_agent.'" AND `remote_addr` = "'.$this->remote_addr.'"',$this->online);
					}
				}
				else{
					$referer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
					if (strstr($referer,'http://')){
						$referer = str_replace('http://','',$referer);
					}
					$referer = explode('/',$referer);
					$referer = $referer['0'];
					if (!preg_match('/[^a-zA-Z0-9\-.]/i', $referer) && (strlen($referer) >= 5) && strlen($referer) <= 30){
						if (@file_get_contents('http://'.$referer)){
							$referer = 'от '.strtolower(mysql_escape_string($referer));
						}
						else{
							$referer = 'от нечего делать.';
						}
					}
					else{
						$referer = 'от нечего делать';
					}
					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);
				}
				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'))){
					mysql_query('INSERT INTO `hosts` SET `user_agent` = "'.$this->user_agent.'", `remote_addr` = "'.$this->remote_addr.'", `date1` = "'.date('m',time()).'", `date2` = "'.date('d',time()).'"');
				}
				mysql_query('INSERT INTO `hits` SET `user_agent` = "'.$this->user_agent.'", `remote_addr` = "'.$this->remote_addr.'", `date1` = "'.date('m',time()).'", `date2` = "'.date('d',time()).'"');
			}
		}
	}
	function listing($number = '10'){
		if (!empty($this->online)){
			if (isset($_GET['p']) && !is_array($_GET['p'])){
				$p = intval($_GET['p']);
			}
			else{
				$p = '0';
			}
			$count = mysql_query('SELECT COUNT(`id`) FROM `online`',$this->online);
			list($total_links) = mysql_fetch_array($count);
			$total_pages = ceil($total_links/$number);
			$p = ($total_links <= $number && $p != '0') ? '0' : $p;
			$p = ($p < 0 || $p > ($total_pages-1)) ? '0' : $p;

			$while = mysql_query('SELECT `user_agent`, `position`, `remote_addr`, `time`, `referer`, `visit` FROM `online` ORDER BY `time` DESC LIMIT '.($number*$p).', '.$number,$this->online);
			if (mysql_num_rows($while) != '0'){
				$c = '0';
				while($obj = mysql_fetch_object($while)){
					$us_agent = stripslashes($obj->user_agent);
					$us_agent = explode('/',$us_agent);
					$us_agent = $us_agent['0'];
					$mtime = date('i:s',(time()-$obj->time));
					if ((time()-$obj->time) == '0'){
						$mtime = 'только что обновил страницу.';
					}
					else
					if ((time()-$obj->time) >= 60){
						$mtime = $mtime.' мин. назад обновил страницу.';
					}
					else{
						$mtime = $mtime.' сек. назад обновил страницу.';
					}
					$c++;
					if ($c%2 == '0'){
						echo '<div style="background-color:#A2C9F4;color:#114F93;border:1px solid #114F93;">'.$us_agent.' - '.$mtime.'<hr />'.
						stripslashes($obj->user_agent).'( '.stripslashes($obj->remote_addr).' )<br />'.
						'Пришел к нам '.stripslashes($obj->referer).' в '.date('H:i:s',$obj->visit).'<br />'.
						'Был замечен '.date('d.m H:i:s',$obj->time).' '.stripslashes($obj->position).'<br /></div>';
					}
					else{
						echo '<div style="background-color:#114F93;color:#A2C9F4;border:1px solid #A2C9F4;">'.$us_agent.' - '.$mtime.'<hr />'.
						stripslashes($obj->user_agent).'( '.stripslashes($obj->remote_addr).' )<br />'.
						'Пришел к нам '.stripslashes($obj->referer).' в '.date('H:i:s',$obj->visit).'<br />'.
						'Был замечен '.date('d.m H:i:s',$obj->time).' '.stripslashes($obj->position).'</div>';
					}
				}
				if ($total_links > $number){
					echo '<p><hr />';
					if ($p > '0'){
						echo '<a href="'.rootlink.'who.php?p='.($p-1).'" style="color:#2C75B1;">Назад('.($p-1).')</a>';
					}
					if ($p < ($total_pages-1)){
						echo '<a href="'.rootlink.'who.php?p='.($p+1).'" style="color:#2C75B1;">Далее('.($p+1).')</a>';
					}
					echo '</p>';
				}
			}
			else{
				echo '<p>На сайте никого нет.</p>';
			}
		}
		else{
			echo '<p>Соединение с базой не установлено.</p>';
		}
	}
	function statistic(){
		$hits = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hits` FROM `hits` WHERE `date2` = "'.date('d',time()).'"'));
		if ($hits->hits != '0'){
			$hosts = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hosts` FROM `hosts` WHERE `date2` = "'.date('d',time()).'"'));
			$allhosts = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hosts` FROM `hosts` WHERE `date1` = "'.date('m',time()).'"'));
			$allhits = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hits` FROM `hits` WHERE `date1` = "'.date('m',time()).'"'));
			$rhits = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hits` FROM `hits` WHERE `date1` = "'.date('m',time()).'" AND `remote_addr` = "'.$this->remote_addr.'"'));
			$rhosts = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `hosts` FROM `hosts` WHERE `date1` = "'.date('m',time()).'" AND `remote_addr` = "'.$this->remote_addr.'"'));
			$ronline = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `onl` FROM `online` WHERE `remote_addr` = "'.$this->remote_addr.'"'));
			echo '<div style="background-color:#114F93;color:#A2C9F4;border:1px solid #A2C9F4;">'.
			'Сегодня обработано:<br />'.
			'Хитов '.$hits->hits.'<br />'.
			'Хостов '.$hosts->hosts.'<br />'.
			'За текущий месяц обработано:<br />'.
			'Хитов '.$allhits->hits.'<br />'.
			'Хостов '.$allhosts->hosts.'<br />'.
			'Всего с текущего ip:<br />'.
			'Хитов '.$rhits->hits.'<br />'.
			'Хостов '.$rhosts->hosts.'<br />'.
			'Сейчас online '.$ronline->onl.'<br />'.
			'</div>';
		}
	}
	function a_header($title = 'Список браузеров, online.'){
		if (strstr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml')){
			header("Content-type:application/xhtml+xml; charset=utf-8");
		}
		else{
			header("Content-type:text/html; charset=utf-8");
		}
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
		header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
		header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
		header('Pragma: no-cache');
		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">'.
		'<head><title>'.$title.'</title><meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=utf-8" />'.
		'<meta http-equiv="Cache-Control" content="no-cache,no-store,must-revalidate" />'.
		'<meta http-equiv="Pragma" content="no-cache" />'.
		'<style type="text/css">'.
		'body {font-family: arial; font-size: 12px; color: #000000; background-color: #ffffff;}'.
		'a {color:#2C75B1;}a:visited{color:#2C75B1;}</style>'.
		'</head><body>';
	}
	function count_onl(){
		if (!empty($this->online)){
			$count_onl = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `id` FROM `online`',$this->online));
			if (strstr($_SERVER['SCRIPT_NAME'],'who.php')){
				if (isset($_GET['statistic'])){
					echo '<p><br /><a href="'.rootlink.'who.php">Список online</a>'.
					'<br /><a href="'.rootlink.'">На главную</a></p>';
				}
				else{
					echo '<p><br /><a href="'.rootlink.'who.php?statistic">Статистика</a>'.
					'<br /><a href="'.rootlink.'">На главную</a></p>';
				}
			}
			else{
				echo '<p><br /><a href="'.rootlink.'who.php">Сейчас на сайте '.$count_onl->id.'</a></p>';
			}
		}
	}
	function end_online(){
		if (!empty($this->online)){
			mysql_close($this->online);
		}
		echo '</body></html>';
	}
	function image(){
		if (!empty($this->online)){
			$count_onl = mysql_fetch_object(mysql_query('SELECT COUNT(`id`) AS `id` FROM `online`',$this->online));
			header("Content-type: image/jpeg");
            $string = $count_onl->id;
            $im = imagecreatefromjpeg('/home/dc/www/online/online/online/onl/image.jpeg');
            $grey = imagecolorallocate($im, 128, 128, 128);
            $black = imagecolorallocate($im, 0, 0, 0);
            $font = '/home/dc/www/online/online/online/onl/font.ttf';

            imagettftext($im, 18,5,3,31, $grey, $font, $string);
            imagettftext($im, 18,5,2,30, $black, $font, $string);
            imagejpeg($im);
            imagedestroy($im);
		}
	}
	function add_image(){
		echo '<img src="'.rootlink.'view.php?'.rand(100000,900000).'" alt="" />';
	}
}
?>