<?php
/*------------------------------------------------
 * Photo viewer    
 * Copyright © FinBoss 2016
 * finboss.ua@gmail.com 
 *
 *
 * License: Commercial open source software (COSS) 
 *------------------------------------------------
 */

//ob_end_clean();
ob_implicit_flush(true);
@ini_set("output_buffering", "off");
header('Content-Type: text/html; charset=utf-8');

// контроль доступа по IP
$a_allow = false;
if (is_file('../'.'.allowed_ip')){
	$a_ips = file('../'.'.allowed_ip');
	
	foreach ($a_ips as $a_ip){
		$a_ip = trim($a_ip);
		if (strpos($a_ip, '*') !== false){
			
			// process 127.0.*.*
			
			$remote = explode('.', $_SERVER['REMOTE_ADDR']);
			$a = explode('.', $a_ip);
			
			$true_count = 0;
			for ($i=0;$i<4;$i++){
				switch ($i){
					case 0:
					case 1:
						if ($remote[$i] != $a[$i]){
							break 2;
						}else{
							$true_count++;
						}
						break;
					case 2:
					case 3:
						if ($a[$i] == '*'){
							// пропускаем
							$true_count++;
						}else{
							if ($remote[$i] != $a[$i]){
								break 2;
							}else{
								$true_count++;
							}
						}
						
				}
			}
			if ($true_count == 4){
				$a_allow = true;
				break;
			}
			
		}else{
			if ($_SERVER['REMOTE_ADDR'] == trim($a_ip)){
				$a_allow = true;
				break;
			}
		}
	}
	
}
if ($a_allow === false){
	die('доступ для IP '.$_SERVER['REMOTE_ADDR'].' заблокирован');
}


error_reporting (E_ALL);

if (isset($_GET['delete'])){
	if (is_file('files/'.$_GET['delete'])){
		unlink('files/'.$_GET['delete']);
	}
}

$d = dir("./files");
$title = "Просмотр фото";
$_file = "Файл:";
$_load = "Загрузить";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" >
<title><?php echo $title; ?></title>
</head>
<body style="font-family:Trebuchet MS; font-size: 10pt;" bgcolor="#FEF4EB">

<table width="100%" bgcolor="#416e98">
<tr valign="middle">
<td><span style="font-size:25px; color:#FFF; font-family:Trebuchet MS"><?php echo $title; ?></span></td>
<td align="right"><span style="font-size:15px; color:#FFF; font-family:Trebuchet MS"></span></td>
</tr>
</table>
<?php 
echo "<br>";

$log = array();

while (false !== ($entry = $d->read())) {
	if ($entry != ".")
		if ($entry != "..")
			if ($entry != "index.php"){
				$log[$entry] = date("Y m d H i s", filectime("./files/".$entry));
			}
}
$d->close();

ksort($log);
echo '<a href="upload_photo.php">'."Закачать".'</a>';
echo '<br/><a href="translit_photo.php">'."Транслит".'</a>';
echo "<br/><br/>";
echo '<table border=1 bordercolor=white style="border-collapse : collapse" width="100%" height="80%">';
$i = 0;
echo '<tr><td valign=top align=left width=20%><div style="height:100%;overflow:scroll">';
$i = 1;
foreach ($log as $entry => $value){
	echo $i++.') <b><span style="cursor:hand;color:blue" onclick="document.getElementById(\'photo_name\').innerText=\''.$entry.'\';document.getElementById(\'photo\').src=\''."files/".$entry.'\';">'.$entry.'</span></b>
	<a href="view_photo.php?delete='.$entry.'" title="Удалить">X</a>
	<br/>';
}


foreach ($log as $entry => $value){
	echo "<td width='80%' valign='top'><center>";
	echo "<b><span style='color:red' id='photo_name'>$entry</span></b><br>";
	echo "<img id='photo' src='files/$entry' width=90%/>";
	echo "</td>";
	break;
}

echo '</div></td></tr>';
echo '</table>';
echo "Всего:".count($log);
echo '<br/>';



?>

</body></html>