<title>Visitor</title>
<div id="pagetitle">VISITOR</div>
<div id="batascontent" class="clearfix">
	<div id="tomboltambah">
		<a href="?m=visitor_read" class="addbutton">Mark as Read</a>
		<a href="?m=visitor_hapus" class="addbutton">Remove All</a>
	</div>
	<div id="tableres">
		<table width="100%" cellpadding="0" cellspacing="0" border="0" class="display" id="example">
			<thead>
				<tr align="left">
					<th>NO</th>
					<th>IP ADDRESS</th>
					<th>DATE</th>
					<th>TIME</th>
					<th>HITS</th>
					<th>AGENT</th>
				</tr>
			</thead>
			<tbody>
				<?php
					$nomor = 1;
					$tahun = date("Y");
					$db = mysqli_query($koneksi, "SELECT * FROM cmas_visitor WHERE YEAR(visitor_date)='$tahun' ORDER BY visitor_id DESC LIMIT 1000");
					while($dt = mysqli_fetch_array($db)) {
				?>
					<tr> 
						<td align="center" width="1%">
							<?php echo $nomor; ?>
						</td>
						<?php if ($dt['visitor_status'] != 0) { ?>
							<td>
								<a href="?m=visitor_details&id=<?php echo $dt['visitor_id']; ?>">
									<b><?php echo $dt['visitor_ip']; ?></b>
								</a>
							</td>
							<td width="10%">
								<b><?php echo date_format(date_create($dt['visitor_date']), 'd/m/Y'); ?></b>
							</td>
							<td width="10%">
								<b><?php echo date_format(date_create($dt['visitor_date']), 'H:i:s'); ?></b>
							</td> 
							<td align="center" width="1%">
								<b><?php echo $dt['visitor_hits']; ?></b>
							</td>
							<td>
								<b><?php echo $dt['visitor_agent']; ?></b>
							</td>

						<?php } else { ?>
							<td>
								<a href="?m=visitor_details&id=<?php echo $dt['visitor_id']; ?>">
									<?php echo $dt['visitor_ip']; ?>
								</a>
							</td>
							<td width="10%">
								<?php echo date_format(date_create($dt['visitor_date']), 'd/m/Y'); ?>
							</td>
							<td width="10%">
								<?php echo date_format(date_create($dt['visitor_date']), 'H:i:s'); ?>
							</td>
							<td align="center" width="1%">
								<?php echo $dt['visitor_hits']; ?>
							</td>
							<td>
								<?php echo $dt['visitor_agent']; ?>
							</td>
						<?php } ?>
					</tr>
				<?php $nomor++; } ?> 
			</tbody>
		</table>
	</div>
	<br>
	<?php
		$user_agent     =   $_SERVER['HTTP_USER_AGENT'];

		function get_client_ip_env() {
		 $ipaddress = '';
		 if (getenv('HTTP_CLIENT_IP'))
		  $ipaddress = getenv('HTTP_CLIENT_IP');
		 else if(getenv('HTTP_X_FORWARDED_FOR'))
		  $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
		 else if(getenv('HTTP_X_FORWARDED'))
		  $ipaddress = getenv('HTTP_X_FORWARDED');
		 else if(getenv('HTTP_FORWARDED_FOR'))
		  $ipaddress = getenv('HTTP_FORWARDED_FOR');
		 else if(getenv('HTTP_FORWARDED'))
		  $ipaddress = getenv('HTTP_FORWARDED');
		 else if(getenv('REMOTE_ADDR'))
		  $ipaddress = getenv('REMOTE_ADDR');
		 else
		  $ipaddress = 'UNKNOWN IP Address';

		 return $ipaddress;
		}

		function get_os(){ 
			global $user_agent;
			$os_platform    =   "Unknown OS Platform";
			$daftar_os      =   array(
			   '/windows nt 6.2/i'     =>  'Windows 8',
			   '/windows nt 6.1/i'     =>  'Windows 7',
			   '/windows nt 6.0/i'     =>  'Windows Vista',
			   '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
			   '/windows nt 5.1/i'     =>  'Windows XP',
			   '/windows xp/i'         =>  'Windows XP',
			   '/windows nt 5.0/i'     =>  'Windows 2000',
			   '/windows me/i'         =>  'Windows ME',
			   '/win98/i'              =>  'Windows 98',
			   '/win95/i'              =>  'Windows 95',
			   '/win16/i'              =>  'Windows 3.11',
			   '/macintosh|mac os x/i' =>  'Mac OS X',
			   '/mac_powerpc/i'        =>  'Mac OS 9',
			   '/linux/i'              =>  'Linux',
			   '/ubuntu/i'             =>  'Ubuntu',
			   '/iphone/i'             =>  'iPhone',
			   '/ipod/i'               =>  'iPod',
			   '/ipad/i'               =>  'iPad',
			   '/android/i'            =>  'Android',
			   '/blackberry/i'         =>  'BlackBerry',
			   '/webos/i'              =>  'Mobile'
								);

			foreach ($daftar_os as $regex => $value) { 
				if (preg_match($regex, $user_agent)) {
					$os_platform    =   $value;
				}
			}   
			return $os_platform;
		}

		function getting_browser(){
			global $user_agent;
			$browser        	=   "Unknown Browser";
			$daftar_browser  	=   array(
									'/msie/i'       =>  'Internet Explorer',
									'/firefox/i'    =>  'Firefox',
									'/safari/i'     =>  'Safari',
									'/chrome/i'     =>  'Chrome',
									'/opera/i'      =>  'Opera',
									'/netscape/i'   =>  'Netscape',
									'/maxthon/i'    =>  'Maxthon',
									'/konqueror/i'  =>  'Konqueror',
									'/mobile/i'     =>  'Handheld Browser'
								);

			foreach ($daftar_browser as $regex => $value) { 
				if (preg_match($regex, $user_agent)) {
					$browser    =   $value;
				}
			}
			return $browser;
		}
		
		$user_os        =   get_os();
		$user_browser   =   getting_browser();
		$ip_user  		= 	get_client_ip_env();
		
		$sdb = mysqli_query($koneksi, "SELECT COUNT(visitor_ip) as JumlahIP FROM cmas_visitor");
		$sdt = mysqli_fetch_array($sdb);
		
		$hdb = mysqli_query($koneksi, "SELECT SUM(visitor_hits) as JumlahHits FROM cmas_visitor");
		$hdt = mysqli_fetch_array($hdb);

	?>
	<table class="table">
		<tbody>
			<tr class="success">
				<td>Total Visitor</td>
				<td><?php echo $sdt['JumlahIP']; ?> Visitors</td>
			</tr>
			<tr class="success">
				<td>Total Hits</td>
				<td><?php echo $hdt['JumlahHits']; ?> Hits</td>
			</tr>
			<tr class="success">
				<td>Your IP Address</td>
				<td><?php echo $ip_user; ?></td>
			</tr>
			<tr class="danger">
				<td>Browser</td>
				<td><?php echo $user_browser; ?></td>
			</tr>
			<tr class="info">
				<td>Operating System</td>
				<td><?php echo $user_os; ?></td>
			</tr>
		</tbody>
	</table>
</div>