<?php
if (! defined ( "ADMIN_FILE" ))
	die ( "Illegal File Access" );
function ThemeSelect($uri = "") {
	$dirtheme = opendir ( 'themes' );
	$theme = array ();
	while ( $file = readdir ( $dirtheme ) ) {
		if (! is_file ( $dirtheme . '/' . $file ) && ! is_link ( $dirtheme . '/' . $file )) {
			$filename = trim ( $file );
			$displayname = $filename;
			if ($displayname != "." && $displayname != "..")
				$theme [$displayname] = $filename;
		}
	}
	closedir ( $dirtheme );
	@asort ( $theme );
	@reset ( $theme );
	
	$theme_select = '<select name="folder">';
	while ( list ( $displayname, $filename ) = @each ( $theme ) ) {
		$selected = ((strtolower ( $uri ) == strtolower ( $filename )) ? ' selected="selected"' : '');
		$theme_select .= '<option value="' . $filename . '"' . $selected . '>' . ucwords ( $displayname ) . '</option>';
	}
	$theme_select .= '</select>';
	return $theme_select;
}
function Theme() {
	begin_frame ( "Темы", 100 );
	print ( "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\">\n" );
	print ( "<tr><td class=\"colhead\" align=\"left\">Имя</td><td class=\"colhead\">Удалить</td><td class=\"colhead\">Редактировать</td></tr>" );
	$res = sql_query ( "SELECT id, name FROM stylesheets" ) or sqlerr ( __FILE__, __LINE__ );
	while ( $row = mysql_fetch_array ( $res ) ) {
		$id = $row ["id"];
		print ( "<form method=\"post\" action=\"?op=ThemeDelete\"><tr><td>$row[name]</td><td><input type=\"submit\" name=\"delete\" value=\"Удалить\"><input type=\"hidden\" name=\"id\" value=\"$id\"></form></td><form method=\"post\" action=\"?op=ThemeEdit\"><input type=\"hidden\" name=\"id\" value=\"$id\"><td><input type=\"submit\" name=\"edit\" value=\"Редактировать\"></td></tr></form>" );
	}
	print ( "</table>" );
	print ( "<form method=\"post\" action=\"?op=ThemeCreate\"><input type=\"submit\" value=\"Создать тему\"><br />*Памятка: После того, как вы создали создали тему, чтобы небуло запар, проверьте, также-ли называется у вас таблица стилей - .css, как и сама тема.</form>" );
}
function ThemeCreate() {
	begin_frame ( "Создание темы", 100 );
	print ( "<form method=\"post\" action=\"?op=ThemeCreate\"><table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\"><tr><td><center>Введите название темы <input type=\"text\" value=\"Введите\" name=\"name\"><br />
Введите папку в которой находиться тема " . ThemeSelect () . "<br /></center>
</td></tr><tr><td colspan=\"2\" align=\"center\">" );
	print ( "<input type=\"submit\" value=\"Создать тему\" name=\"create\">" );
	print ( "</td></tr></table></form>" );
	if ($_POST ["name"] && $_POST ["folder"]) {
		$name = sqlesc ( $_POST ["name"] );
		$uri = sqlesc ( $_POST ["folder"] );
		$theme = sql_query ( "INSERT INTO stylesheets (uri, name) VALUES($uri, $name)" ) or sqlerr ( __FILE__, __LINE__ );
		print ( "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\"><tr><td><form method=\"post\" action=\"?op=Theme\"><input type=\"submit\" value=\"Вернуться\" name=\"backup\"></form><td></tr></table>" );
	}
}
function ThemeDelete() {
	begin_frame ( "Вы успешно удалили тему", 100 );
	sql_query ( "DELETE FROM stylesheets WHERE id = " . intval ( $_POST [id] ) . " LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
	print ( "<form method=\"post\" action=\"?op=Theme\"><center><input type=\"submit\" value=\"Выполнено!\"</center></form>" );

}
function ThemeEdit() {
	begin_frame ( "Редактирование темы", 100 );
	$ope = sql_query ( "SELECT name, uri FROM stylesheets WHERE id = '$_POST[id]'" ) or sqlerr ( __FILE__, __LINE__ );
	$open = mysql_fetch_array ( $ope );
	$name = $open ["name"];
	$uri = $open ["uri"];
	$all = "<form method=\"post\" action=\"?op=ThemeEdit\"><table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\"><tr><td><input type=\"hidden\" value=\"$_POST[id]\" name=\"id\"><center>Введите название темы <input type=\"text\" value=\"$name\" name=\"nama\"><br />
Введите папку в которой находиться тема " . ThemeSelect ( $uri ) . "<br /></center>
</td></tr><tr><td colspan=\"2\" align=\"center\">
<input type=\"submit\" value=\"Отредактировать\" name=\"create\">
</td></tr></table></form>";
	if ($_POST ["nama"] && $_POST ["id"] && $_POST ["folder"]) {
		$id = ( int ) $_POST ["id"];
		$nama = sqlesc ( $_POST ["nama"] );
		$ura = sqlesc ( $_POST ["folder"] );
		$theme = sql_query ( "UPDATE stylesheets SET name = $nama, uri = $ura WHERE id = $id" ) or sqlerr ( __FILE__, __LINE__ );
		$all = "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\"><form method=\"post\" action=\"?op=Theme\"><input type=\"submit\" value=\"Вернуться\"></form></table>";
	}
	print ( "$all" );
}
switch ($op) {
	case "Theme" :
		Theme ();
		break;
	case "ThemeCreate" :
		ThemeCreate ();
		break;
	case "ThemeDelete" :
		ThemeDelete ();
		break;
	case "ThemeEdit" :
		ThemeEdit ();
		break;
}

?>