<?php
if (! defined ( "ADMIN_FILE" ))
	die ( "Illegal File Access" );
function GlobalVars() {
	global $pic_base_url, $admin_file, $smilies, $privatesmilies, $zodiac;
	begin_frame ( "Все смайлы" );
	$num = 0;
	?><form action='<?=$admin_file?>.php?op=GlobalBuild' method='post'>
<table align='center' width='100%'>
	<tr><?
	foreach ( $smilies as $code => $image ) {
		if ($num == 4) {
			print ( "</tr><tr>" );
			$num = 0;
		}
		$num ++;
		print ( "<td align='center'><img src='" . $pic_base_url . "smilies/" . $image . "'><br><input type='text' value='" . $code . "' name=\"smilies['" . $image . "']\" size=10'></td>" );
	}
	?></tr>
</table><?
	end_frame ();
	begin_frame ( "Приватные смайлы" );
	$num = 0;
	?><table align='center' width='100%'>
	<tr><?
	foreach ( $privatesmilies as $code => $image ) {
		if ($num == 4) {
			print ( "</tr><tr>" );
			$num = 0;
		}
		$num ++;
		print ( "<td align='center'><img src='" . $pic_base_url . "smilies/" . $image . "'><br><input type='text' value='" . $code . "' name=\"psmilies['" . $image . "']\" size=10'></td>" );
	}
	?></tr>
</table><?
	end_frame ();
	begin_frame ( "Знаки зодиака" );
	$num = 0;
	?><table align='center' width='100%'>
	<tr><?
	foreach ( $zodiac as $image ) {
		if ($num == 4) {
			print ( "</tr><tr>" );
			$num = 0;
		}
		$num ++;
		$code = $image [0];
		$image = $image [1];
		print ( "<td align='center'><img src='" . $pic_base_url . "zodiac/" . $image . "'><br><input type='text' value='" . $code . "' name=\"zodiacs['" . $image . "']\" size=10'></td>" );
	}
	?></tr>
</table><?
	end_frame ();
	begin_frame ();
	?><table align='center' style='border: 0px;'>
	<tr>
		<td align='center' style='border: 0px;'><input type='submit'
			value='Сохранить'> <a href='<?=$admin_file?>.php?op=SmiliesAdd'><input
			type='button' value='Добавить смайл'></a></td>
	</tr>
</table>
</form><?
}
function GlobalBuild() {
	global $admin_file, $zodiac;
	if (! is_array ( $_POST ['smilies'] ) || ! is_array ( $_POST ['psmilies'] )) {
		location ( $admin_file . '.php?op=Globals', 5 );
		begin_frame ( "Смайлы и знаки зодиака" );
		stdmsg ( "Ошибка", "Не выбраны смайлы", 'error' );
		end_frame ();
		stdfoot ();
		die ();
	}
	if (! is_array ( $_POST ['zodiacs'] )) {
		location ( $admin_file . '.php?op=Globals', 5 );
		begin_frame ( "Смайлы и знаки зодиака" );
		stdmsg ( "Ошибка", "Не выбраны знаки зодиака", 'error' );
		end_frame ();
		stdfoot ();
		die ();
	}
	$content = '$smilies = array (';
	$num = 0;
	foreach ( $_POST ['smilies'] as $image => $code ) {
		if ($code && $image)
			$content .= '"' . $code . '"=>' . $image . ($num != count ( $_POST ['smilies'] ) - 1 ? ', 
' : "");
		$num ++;
	}
	$content .= ');';
	$content .= '
	$privatesmilies = array (';
	$num = 0;
	foreach ( $_POST ['psmilies'] as $image => $code ) {
		if ($code && $image)
			$content .= '"' . $code . '"=>' . $image . ($num != count ( $_POST ['psmilies'] ) - 1 ? ', 
' : "");
		$num ++;
	}
	$content .= ');';
	$content .= '
	$zodiac = array (';
	$num = 0;
	foreach ( $_POST ['zodiacs'] as $image => $code ) {
		if ($code && $image)
			$content .= 'array("' . $code . '", ' . $image . ', "' . $zodiac [$num] [2] . '")' . ($num != count ( $_POST ['zodiac'] ) - 1 ? ', 
' : "");
		$num ++;
	}
	$content .= ');';
	$content = '<?php
# IMPORTANT: Do not edit below unless you know what you are doing!
if (! defined ( "IN_TRACKER" ))
	die ( "Hacking attempt!" );
	' . $content . '
?>';
	$file = fopen ( './include/global.php', 'w' );
	fwrite ( $file, $content );
	fclose ( $file );
	location ( $admin_file . '.php?op=Globals', 3 );
	begin_frame ( "Смайлы и знаки зодиака" );
	print ( "<center>Успешно</center>" );
}
function SmiliesAdd() {
	global $admin_file;
	begin_frame ( "Смайлы и знаки зодиака" );
	?>
<form action='<?=$admin_file?>.php?op=AddBuild' method='post'>
<table width='100%' align='center'>
	<tr>
		<td align='right' width='20%'>Тип</td>
		<td align='left' width='80%'><select name='type'>
			<option value='main'>Главные</option>
			<option value='private'>Приватные</option>
		</select></td>
	</tr>
	<tr>
		<td align='right' width='20%'>Путь</td>
		<td align='left' width='80%'><input type='text' value='./pic/smilies/'
			size=25 name='path'></td>
	</tr>
	<tr>
		<td align='right' width='20%'></td>
		<td align='left' width='80%'><input type='submit' value='Добавить'></td>
	</tr>
</table>
</form>
<?
}
function AddBuild() {
	global $admin_file, $zodiac, $smilies, $privatesmilies;
	$path = $_POST ['path'];
	begin_frame ( "Смайлы и знаки зодиака" );
	if (is_dir ( $path )) {
		$dir = @opendir ( $path );
		if (! $dir) {
			location ( $admin_file . '.php?op=SmiliesAdd', 5 );
			begin_frame ( "Смайлы и знаки зодиака" );
			stdmsg ( "Ошибка", "Неправильный путь к файлу или папке", 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		while ( $file = readdir ( $dir ) ) {
			$filename = trim ( $file );
			if (is_file ( $path . '/' . $file ) && preg_match ( '#(.*?)(\.gif|\.jpg|\.png|\.jpeg|\.ico|\.bmp)#si', $filename, $matches ))
				$addingsmilies [':' . $matches [1] . ':'] = $filename;
		}
		closedir ( $dir );
	} elseif (is_file ( $path )) {
		if (! file_exists ( $path )) {
			location ( $admin_file . '.php?op=SmiliesAdd', 5 );
			begin_frame ( "Смайлы и знаки зодиака" );
			stdmsg ( "Ошибка", "Неправильный путь к файлу или папке", 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		$path = substr ( $path, strrpos ( $path, "/" ) + 1 );
		if (! preg_match ( '#(.*?)(\.gif|\.jpg|\.png|\.jpeg|\.ico|\.bmp)#si', $path, $matches )) {
			location ( $admin_file . '.php?op=SmiliesAdd', 5 );
			begin_frame ( "Смайлы и знаки зодиака" );
			stdmsg ( "Ошибка", "Неправильный тип файла " . $path, 'error' );
			end_frame ();
			stdfoot ();
			die ();
		}
		$addingsmilies [':' . $matches [1] . ':'] = $matches [0];
	}
	if ((! is_array ( $addingsmilies ) || ! $_POST ['type'])) {
		location ( $admin_file . '.php?op=SmiliesAdd', 5 );
		begin_frame ( "Смайлы и знаки зодиака" );
		stdmsg ( "Ошибка", "Не выбраны смайлы", 'error' );
		end_frame ();
		stdfoot ();
		die ();
	}
	$type = $_POST ['type'];
	foreach ( $addingsmilies as $code => $image )
		if ($type == 'private') {
			if (! in_array ( $image, $privatesmilies ))
				$privatesmilies [$code] = $image;
		} else {
			if (! in_array ( $image, $smilies ))
				$smilies [$code] = $image;
		}
	$content = '$smilies = array (';
	$num = 0;
	foreach ( $smilies as $code => $image ) {
		$content .= '"' . $code . '"=>"' . $image . '"' . ($num != count ( $smilies ) - 1 ? ', 
' : "");
		$num ++;
	}
	$content .= ');';
	$content .= '
	$privatesmilies = array (';
	$num = 0;
	foreach ( $privatesmilies as $code => $image ) {
		$content .= '"' . $code . '"=>"' . $image . '"' . ($num != count ( $privatesmilies ) - 1 ? ', 
' : "");
		$num ++;
	}
	$content .= ');';
	$content .= '
	$zodiac = array (';
	$num = 0;
	foreach ( $zodiac as $zodiacs ) {
		$content .= 'array("' . $zodiacs [0] . '", "' . $zodiacs [1] . '", "' . $zodiacs [2] . '")' . ($num != count ( $zodiac ) - 1 ? ', 
' : "");
		$num ++;
	}
	$content .= ');';
	$content = '<?php
# IMPORTANT: Do not edit below unless you know what you are doing!
if (! defined ( "IN_TRACKER" ))
	die ( "Hacking attempt!" );
	' . $content . '
?>';
	$file = fopen ( './include/global.php', 'w' );
	fwrite ( $file, $content );
	fclose ( $file );
	location ( $admin_file . '.php?op=Globals', 3 );
	begin_frame ( "Смайлы и знаки зодиака" );
	print ( "<center>Успешно</center>" );
}
switch ($op) {
	case "Globals" :
		GlobalVars ();
		break;
	case "SmiliesAdd" :
		SmiliesAdd ();
		break;
	case "AddBuild" :
		AddBuild ();
		break;
	case "GlobalBuild" :
		GlobalBuild ();
		break;
}
?>