<?php
/*------------------------------------------------
 * Photo viewer    
 * Copyright © FinBoss 2016
 *
 *
 * License: Commercial open source software (COSS) 
 *------------------------------------------------
 */


@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);
date_default_timezone_set ('Europe/Moscow');

$title = "Транслит";

function translit($text)
{
	$ru = explode('-', "А-а-Б-б-В-в-Ґ-ґ-Г-г-Д-д-Е-е-Ё-ё-Є-є-Ж-ж-З-з-И-и-І-і-Ї-ї-Й-й-К-к-Л-л-М-м-Н-н-О-о-П-п-Р-р-С-с-Т-т-У-у-Ф-ф-Х-х-Ц-ц-Ч-ч-Ш-ш-Щ-щ-Ъ-ъ-Ы-ы-Ь-ь-Э-э-Ю-ю-Я-я"); 
	$en = explode('-', "A-a-B-b-V-v-G-g-G-g-D-d-E-e-E-e-E-e-ZH-zh-Z-z-I-i-I-i-I-i-J-j-K-k-L-l-M-m-N-n-O-o-P-p-R-r-S-s-T-t-U-u-F-f-H-h-TS-ts-CH-ch-SH-sh-SCH-sch---Y-y---E-e-YU-yu-YA-ya");

 	$res = str_replace($ru, $en, $text);
	$res = preg_replace("/[\s]+/ui", '-', $res);		// u = UTF-8
	$res = strtolower(preg_replace("/[^0-9a-zа-я\-]+/ui", '', $res));
 	
    return $res;  
}

$output = '';

if (isset($_POST['input'])){
	$input_array = explode("\r\n", $_POST['input']);
	foreach ($input_array as $string){
		$output .= translit(trim($string))."\r\n";
	}
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<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>
 
<br>
<a href="upload_photo.php">Закачать</a>
<br/><a href="view_photo.php">Просмотр фото</a>
<br><br>
<center>
<form method=post >
<table border=1 bordercolor=black width="90%" height="70%">

<tr height=20>
<td valign=top align=center>Введите текст построчно</td>
<td valign=top align=center>Результат</td>
</tr>

<tr>
<td valign=top align=center>
<textarea name=input cols=60 rows=40><?php if (isset($_POST['input'])) echo $_POST['input']; ?></textarea>
</td>
<td valign=top align=center>
<textarea readonly cols=60 rows=40><?php echo $output; ?></textarea>
</td>
</tr>
</table>
<p><input type="submit" value=Отправить></p>
</form>
<br/>
</center>
</body></html>