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

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


// контроль доступа по IP
$a_allow = false;
$a_ips = '';
if (is_file('.allowed_ip')){
	$a_ips = file('.allowed_ip');
}elseif (is_file('../.allowed_ip')){
	$a_ips = file('../.allowed_ip');
}

if (is_array($a_ips)){
	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);

date_default_timezone_set('Europe/Moscow');


$d = dir(".");
$title = "File viewer";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" >
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
<script type="text/javascript">
function ctrlSelectMenu (elem, evt)
{
	if (!evt)
		evt = event;

	var target = evt.target || evt.srcElement;
	if (target.tagName == 'INPUT') {
		var checked = target.checked;
		var List = document.forms[0];
		for (var i = 0; i < List.length; i++){
			List[i].checked = checked;
		}
	}
}
</script>
</head>
<body style="font-family:Trebuchet MS; font-size: 10pt;" bgcolor="#f5f8fb">

<table width="100%" bgcolor="#416e98">
<tr valign="middle">
<td><span style="font-size:25px; color:#FFF; font-family:Trebuchet MS"> </span></td>
<td align="right"><span style="font-size:15px; color:#FFF; font-family:Trebuchet MS"><?php echo $title; ?></span></td>
</tr>
</table>
<center>
<?php 
echo <<<HTML
<form method="post" enctype="multipart/form-data" name="excelform" autocomplete="off">
HTML;


if (isset($_POST['delete'])){
	foreach ($_POST['del_file'] as $file){
		unlink($file);
	}
}

$d = dir(".");

$log = array();

while (false !== ($entry = $d->read())) {
	if ($entry != ".")
		if ($entry != "..")
			if ($entry != "index.php")
				if ($entry != ".htaccess"){
					$log[] = $entry;
				}
}
$d->close();


echo '<form method="post" enctype="multipart/form-data" name="excelform" autocomplete="off">';

arsort($log);
echo '<table  border=1 bordercolor="#416e98" style="border-collapse : collapse; margin:5px;">';

	echo '<tr>';
	echo '<td align=center>Имя файла</td>';
	echo '<td align=center>Размер, байт</td>';
	echo '<td align=center>Отметить <br />для удаления<br /><input type=checkbox name="select_all" value="1" onclick="ctrlSelectMenu(this, event)" /></td>';
	echo '</tr>';
$i = 0;	
foreach ($log as $entry){
	echo '<tr>';
	echo '<td><a href="'.$entry.'" target="_blank">'.$entry.'</a></td>';
	echo '<td align=right>'.filesize($entry).'</td>';
	echo '<td align=center><input type=checkbox id="del_cb['.$i++.']" name="del_file[]" value="'.$entry.'" /> </td>';
	echo '</tr>';
}


	echo '<tr>';
	echo '<td align=center> </td>';
	echo '<td align=center> </td>';
	echo '<td align=center><input type=submit name=delete value="Удалить" /></td>';
	echo '</tr>';
	
echo '</table></form>';
	

?>
</center>
</body></html>