ACC SHELL

Path : /srv/www/vhosts/amk/
File Upload :
Current File : /srv/www/vhosts/amk/admin.php

<?php

require("cropper.php");
require("jobs.php");

function admin_header()
{
	/*header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache"); */
?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cz" lang="cz">
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
			<meta http-equiv="cache-control" content="no-cache"/>
			<link rel="stylesheet" type="text/css" href="admin.css" media="screen"/>
			<link rel="stylesheet" type="text/css" href="../style.css" media="screen"/>
			<title>AMK Brno - administrační sekce </title>
			<?php load_cropper_script() ?>
			<?php load_datevalidation_script() ?>
			<script type="text/javascript" src="confirm.js"></script>
		</head>
	<body>
	<div class="centered">
<?php
}

function admin_footer()
{
?>
	</div>
	</body>
</html>
<?php
}

function admin_msgbox($msg)
{
	printf("<div class=\"error\">");
	printf($msg);
	printf("</div>");	
}

function admin_display_help($message)
{
	echo "<div class=\"help\">";
	echo "<h2>Nápověda</h2>";
	echo $message;	
	echo "</div>";
}

function admin_display_warning($message)
{
	echo "<div class=\"warning\">";
	echo "<h2>Upozornění</h2>";
	echo $message;	
	echo "</div>";
}

function admin_display_menu()
{
	echo "<div class=\"admin_menu\">";                         
	if(isset($_SESSION["news_id"]))
	{
		echo "<a href=\"javascript: redir('Tímto ztratíte rozpracovanou novinku, chcete pokračovat', '?action=" . STATE_ADD_NEWS_INIT . "')\">Přidat novinku</a> | ";
		echo "<a href=\"javascript: redir('Tímto ztratíte rozpracovanou novinku, chcete pokračovat', '?action=" . STATE_CHOOSE_FOR_EDITATION . "')\">Editovat novinku</a> | ";
	}
	else
	{
		echo "<a href=\"?action=" . STATE_ADD_NEWS_INIT . "\">Přidat novinku</a> | ";
		echo "<a href=\"?action=" . STATE_CHOOSE_FOR_EDITATION . "\">Editovat novinku</a> | ";
		echo "<a href=\"?action=" . STATE_DISPLAY_ADD_JOB . "\">Přidat brigádu</a> | ";
		echo "<a href=\"?action=" . STATE_LIST_JOBS . "\">Editovat brigády</a> | ";

	}
		
	
	
	echo "<a href=\"?action=" . STATE_LOGOUT . "\">Odhlásit se</a> ";
	echo "</div>";                      
}

function admin_display_login_screen()
{
?>
	<form method="POST" action="index.php" style="width: 15em; margin-left: auto; margin-right: auto">
	<input type="hidden" name="action" value="1">
	<table>
		<tr><td>jméno</td><td><input name="email" class="font_style" type="text"/></td></tr>
		<tr><td>heslo</td><td><input name="password" class="font_style" type="password"></td></tr>
		<tr style="text-align: center"><td colspan="2" ><input class="input_button"  type="submit" value="přihlásit se"></td></tr>
	</table>
	</form>

<?php
}

function admin_display_add_news()
{
	if(isset($_SESSION["news_id"]))
	{
		admin_display_filled_add_news();
		return;
	}
	
	die("obsoletni");
?>
	
	<form method="POST" action="index.php">
	<h2>Přidat novinku</h2>
	<?php
	echo "<input type=\"hidden\" name=\"action\" value=\"" . STATE_ADD_NEWS_PREVIEW . "\"/>";
	?>
	<input type="hidden" name="perex_image_id" value="0"/>
	<table style="width: 100%">
		<tr>
			<td style="width: 10em">autor</td>
			<td>
				<?php
				echo "<input type=\"hidden\" name=\"user_id\" value=\"" . $_SESSION["user_id"] . "/>";
				print_user($_SESSION["user_id"]);
				?>
			</td>
		</tr>
		<tr>
			<td>umístění obrázku</td>
			<td>
				<input type="radio" value="'left'" checked name="perex_position">vlevo</input>
				<input type="radio"  value="'right'" name="perex_position">vpravo</input>
			</td>
		</tr>
		
		<tr><td>titulek</td><td><input name="headline" type="text"></td></tr>
		<tr><td>kategorie</td><td>
			<select name="category_id">
				<?php print_category_combo("dddcategory_id") ?>					
			</select>		
			</td>			
		</tr>
		<tr>
			<td>perex</td>
			<td><textarea name="perex" rows="3"></textarea></td></tr>
		<tr>
			<td>text</td>
			<td><textarea name="text" rows="10"></textarea></td></tr>
		<tr><td colspan="2" style="text-align: center"><input type="submit" value="náhled"></td></tr>
	</table>
	</form>
<?php
}

function admin_display_filled_add_news()
{
	$query = "SELECT headline, perex, body, has_perex_image, image_position, category_id FROM news WHERE news_id = " . $_SESSION["news_id"];
	$result = mysql_query($query);
	
	if(!$result || mysql_num_rows($result) != 1)
	{
		admin_msgbox("Nepodařilo se získat data pro vyplnění formuláře");
		die();
	}
	
	$row = mysql_fetch_row($result);
	
	?>
	
	<form method="POST" action="index.php">
	<h2>Přidat novinku</h2>
	<?php
	echo "<input type=\"hidden\" name=\"action\" value=\"" . STATE_ADD_NEWS_PREVIEW . "\"/>";
	?>
	<input type="hidden" name="perex_image_id" value="0"/>
	<table style="width: 100%">
		<tr>
			<td style="width: 10em">autor</td>
			<td>
				<?php
				echo "<input type=\"hidden\" name=\"user_id\" value=\"" . $_SESSION["user_id"] . "\"/>";
				print_user($_SESSION["user_id"]);
				?>
			</td>
		</tr>
		<tr>
			<td>umístění obrázku</td>
			<td>
			<?php
				if($row[3])
				{
					
					if($row[4] == "left")
					{
						echo "<input type=\"radio\" value=\"left\" checked name=\"perex_position\">vlevo</input>";
						echo "<input type=\"radio\" value=\"right\" name=\"perex_position\">vpravo</input>";
					}
					else
					{
						echo "<input type=\"radio\" value=\"left\" name=\"perex_position\">vlevo</input>";
						echo "<input type=\"radio\"  value=\"right\" checked name=\"perex_position\">vpravo</input>";
					}
				}
				else
				{
					echo "<input type=\"radio\" name=\"perex_position\" checked value=\"none\">žádný obrázek</input>";
				} ?>
			</td>
		</tr>
		
		<?php echo "<tr><td>titulek</td><td><input name=\"headline\" type=\"text\" value=\"" . $row[0] . "\"></td></tr>";?>
		
		<tr><td>kategorie</td><td>
			<select name="category_id">
				<?php print_category_combo($row[5]) ?>					
			</select>		
			</td>			
		</tr>
		<tr>
			<td>perex</td>
		<?php echo "<td><textarea name=\"perex\" rows=\"3\">" . $row[1] . "</textarea></td></tr>"; ?>
		<tr>
			<td>text</td>
		<?php echo "<td><textarea name=\"text\" rows=\"10\">" . $row[2] ."</textarea></td></tr>"; ?>
		<tr><td colspan="2" style="text-align: center"><input type="submit" value="náhled"></td></tr>
	</table>
	</form>
	<?php
}

function get_filename()
{
	if(!isset($_SESSION["news_id"]) || !isset($_SESSION["ticks"]))
	{
		admin_msgbox("Identifikace novinky není korektní");
		var_dump(debug_backtrace());
		
		echo "ticks: " . $_SESSION["ticks"];
		echo "news_id: " . $_SESSION["news_id"];
	}
	
	return $_SESSION["news_id"] . "_" . $_SESSION["ticks"];
}

function regenerate_filename()
{
	$_SESSION["ticks"] = time();
}

function admin_display_pic_upload()
{
	admin_display_help(
		"Pokud chcete nahrát vlastní obrázek, vyberte jej pomocí tlačítka vpravo od textového pole." . 
		"Pokud žádný obrázek nezadáte, vybere se předpřipravený obrázek podle kategorie, do které článek zařadíte"
	);
	
	admin_clean_up();
	
	regenerate_filename();
	
	
	
?>
<div class="blue_box">
	<form method="POST" action="index.php" enctype="multipart/form-data">
	<h2>Nahrát obrázek</h2>
	<input type="hidden" name="action" value="
	<?php echo STATE_ADD_NEWS_CROP ?>
	">
	<table style="width: 100%">
		<tr><td>Soubor</td><td><input type="file" size="30" name="from_file"/></td></td></tr>
		<tr><td colspan="2" style="text-align: center"><input type="submit" value="Nahrát"></td></tr>
	</table>
	</form>
</div>

<?php
}

function admin_upload_pic()
{
	// first check if everything is setup the way it should be
	if(
		isset($_FILES["from_file"]["name"]) &&
		isset($_FILES["from_file"]["type"]) &&
		isset($_FILES["from_file"]["tmp_name"]) &&
		isset($_FILES["from_file"]["error"]) &&
		isset($_FILES["from_file"]["size"]))
	{
		// check if there have been any difficulties
		if($_FILES["from_file"]["error"] == 4)
		{
			
			return false;
		}
		
		if($_FILES["from_file"]["error"] != 0)
		{
			admin_msgbox("Při nahrávání souboru vznikla chyba #" . $_FILES["from_file"]["error"]);
			die();//return false;
		}		
		
		// check if it's a supported file type
		if(
			$_FILES["from_file"]["type"] != "image/jpeg" &&
			$_FILES["from_file"]["type"] != "image/pjpeg" &&
			$_FILES["from_file"]["type"] != "image/png")		
		{
			admin_msgbox("Soubor, který se snažíte nahrát, není ve formátu PNG nebo JPEG (formát GIF není podporován)" . $_FILES["from_file"]["type"]);
			die();//return;
		}
		
		// move if from temp to the perex directory
		$extension = strrchr($_FILES["from_file"]["name"], ".");
		
		$resuolt = move_uploaded_file($_FILES["from_file"]["tmp_name"], "../gfx/temp/orig" . get_filename() );
		
		// create preview
		$image = open_image("../gfx/temp/orig" . get_filename() );
	 
		if($image == false)
		{
			echo "move_uploaded_file vratilo" . ($result ? "true" : "false");
			echo file_exists($_FILES["from_file"]["tmp_name"]) ? "tmp soubor existuje" : "tmp soubor neexistuje";
			echo file_exists("../gfx/temp/orig" . get_filename()) ? "soubor existuje" : "soubor neexistuje";
			
			admin_msgbox("Chyba při otevírání obrázku (" . get_filename() . "), (" . $_FILES["from_file"]["tmp_name"] . ")");
		}
			
		imagejpeg(
			resize_image( 0, 0, imagesx($image), imagesy($image), 670, 500, $image),
			"../gfx/temp/preview" . get_filename()
		);
	}
	else
	{
		// the user might have wanted to recrop his image, that's why there where no FILES variables, jus check the files
		// alread exist
		if(file_exists("../gfx/temp/preview" . get_filename()) && file_exists("../gfx/temp/orig" . get_filename()))
		{
			// regenerate session id and relocate files
			$old_session = get_filename();
			regenerate_filename();
			
			rename("../gfx/temp/orig" . $old_session, "../gfx/temp/orig" . get_filename());
			rename("../gfx/temp/preview" . $old_session, "../gfx/temp/preview" . get_filename());			
			
			return true;
		}
		
		admin_msgbox("Při nahrávání souboru vznikla neznámá chyba");
		die();//return;
	}
	
	return true;	
}

function admin_regenerate_orig_and_preview_files()
{
	
	// check if we ever had a file in the first place
	if(!file_exists("../gfx/temp/" . get_filename() . ".jpg"))
		return;
	
		// make the current crop the original file	
	copy("../gfx/temp/" . get_filename() . ".jpg", "../gfx/temp/orig" . get_filename());
	
	// regenerate preview
	$image = open_image("../gfx/temp/orig" . get_filename());
	
	$preview = imagejpeg(
		resize_image( 0, 0, imagesx($image), imagesy($image), 670, 500, $image),
		"../gfx/temp/preview" . get_filename()
	);
}

function admin_display_gfx_preview()
{
	admin_display_help(
		"Níže je vidět obrázek, který se použije v perexu, a zmenšenina obrázku, který se použije v textu." .
		"Pokud je perex příliš rozmazaný, je potřeba vybrat fotografii s větším rozlišením nebo vybrat větší část fotografie" 
	);
	echo "<div class=\"gray_box\">";
	echo "<h2>Náhled obázku pro perex</h2>";
	echo "<img src=\"../gfx/temp/" . get_filename() . "_perex.jpg\"/>";
	echo "<h2>Náhled zmenšeného obrázku pro použití v článku</h2>";
	echo "<img src=\"../gfx/temp/" . get_filename() . "_thumb.jpg\"/>";
	echo "<h2>Možnosti</h2>";
	echo "<a href=\"index.php?action=" . STATE_ADD_NEWS_FILESELECT . "\">Vybrat jiný obrázek</a> | ";
	echo "<a href=\"index.php?action=" . STATE_ADD_NEWS_CROP . "\">Znova oříznout</a> | ";
	echo "<a href=\"index.php?action=" . STATE_ADD_NEWS_ARTICLE ."\">Pokračovat</a>";
	echo "</div>";	
}

function admin_display_preview()
{
	if(!isset($_SESSION["news_id"]))
	{
		admin_msgbox("Nastala chyba při předávání novinky");
		die();
	}
	else
	{
		echo "<div style=\"width: 580px; margin-left: auto; margin-right: auto\">";
		//echo "<h2>Náhled perexu</h2>";
		$query = "SELECT headline, perex, body, has_perex_image, category_id FROM news WHERE news_id = " . quote_smart($_SESSION["news_id"]);
		$result = mysql_query($query);
		
		if(!$result || mysql_num_rows($result) != 1)
		{
			admin_msgbox("Nastala chyba při hledání novinky");
			die();
		}
		
		$row = mysql_fetch_row($result);		

		if($row[3])
			print_news($_SESSION["news_id"], $row[0], $row[1], "../gfx/temp/" . get_filename() . "_perex.jpg");
		else
			print_news($_SESSION["news_id"], $row[0], $row[1], "../gfx/perex/default/" . $row[4] . ".png");
		
		
		//echo "<h2>Náhled textu</h2>";
		
		print_article($_SESSION["news_id"], "../gfx/temp/" . get_filename() . "_thumb.jpg");
		
		echo "</div>";	
		
		echo "<div class=\"gray_box\">";
		echo "<h2>Možnosti</h2>";
		echo "<a href=\"index.php?action=" . STATE_ADD_NEWS_ARTICLE . "\">Opravit</a> | ";
		echo "<a href=\"index.php?action=" . STATE_ADD_NEWS_PUBLISH . "\">Publikovat</a>";
		echo "</div>";	
	}
}

function admin_clean_up($filename = NULL)
{
	if((!isset($_SESSION["news_id"]) || !isset($_SESSION["ticks"])) && $filename == NULL)
		return;
		
	if($filename == NULL)
		$filename = get_filename();
		
	if(file_exists("../gfx/temp/" . $filename . "_perex.jpg"))
		unlink("../gfx/temp/" . $filename . "_perex.jpg");
		
	if(file_exists("../gfx/temp/" . $filename . "_thumb.jpg"))
		unlink("../gfx/temp/" . $filename . "_thumb.jpg");
		
	if(file_exists("../gfx/temp/" . $filename . ".jpg"))
		unlink("../gfx/temp/" . $filename . ".jpg");
	
	if(file_exists("../gfx/temp/orig" . $filename))	
		unlink("../gfx/temp/orig" . $filename);	
	
	if(file_exists("../gfx/temp/preview" . $filename))
		unlink("../gfx/temp/preview" . $filename);	
		
}

function admin_news_list()
{
	$query = 
		"SELECT news.news_id, news.headline, news.added_on,  user.name FROM news NATURAL JOIN user WHERE published = 1 ORDER BY news.added_on DESC";
	$result = mysql_query($query);
	
	echo "<table class=\"news_list\">";
	
	echo "<tr><th style=\"width: 20px\">id</th>" . 
			 	 "<th style=\"width: 150px\">titulek</th>" . 
			 	 "<th style=\"width: 70px\">datum vytvoření</th>" .
			 	 "<th style=\"width: 100px\">autor</th>" .
			 	 "<th style=\"width: 100px\">&nbsp;</th>" .
			 	 "<th style=\"width: 100px\">&nbsp;</th>";
	
	while($row = mysql_fetch_row($result))
	{
		$date = date("j. n. Y (G:i:s)", strtotime($row[2]));
		echo "<tr><td>" . $row[0] . "</td>" . 
			 	 "<td>" . $row[1] . "</td>" . 
			 	 "<td>" . $date . "</td>" .
			 	 "<td>" . $row[3] . "</td>";
			 	 
		?>
		<td class="list_button">
			<form class="verbose" action="index.php" method="POST">
			<input type="hidden" name="news_id" value="<?php echo $row[0]; ?>">
			<input type="hidden" name="action" value="<?php echo STATE_EDIT; ?>">
			<input type="submit" value="upravit">
			</form>
		</td>
		<td class="list_button">
			<form class="verbose" action="index.php" method="POST">
			<input type="hidden" name="news_id" value="<?php echo $row[0]; ?>">
			<input type="hidden" name="action" value="delete">
			<!--<input type="submit" value="odstranit">-->
			</form>
		</td>
		
		</tr>
		<?php
	}
	
	echo "</table>";
}

function admin_unpublish_news($id)
{
	// make the news unpublished
	$query = "UPDATE news SET published = 0 WHERE news_id = " . quote_smart($id);
	mysql_query($query);
	
	// setup filename mangling
	$_SESSION["news_id"] = $id;
	regenerate_filename();
	
	// move gfx back if it exists (ie default image)
	echo "pokusim se presunout soubory pro id = " . $id;
	if(file_exists("../gfx/perex/" . quote_smart($id) . ".jpg"))
	{
		echo "soubory nalezeny";
		copy("../gfx/perex/" . $_SESSION["news_id"] . ".jpg", "../gfx/temp/" . get_filename() . "_perex.jpg");
		copy("../gfx/thumb/" . $_SESSION["news_id"] . ".jpg", "../gfx/temp/" . get_filename() . "_thumb.jpg");
		copy("../gfx/img/" . $_SESSION["news_id"] . ".jpg", "../gfx/temp/" . get_filename() . ".jpg");
	}
}

function admin_publish_news()
{
	// move thumb, big picture and perex (if default imagery hasn't been used)
	if(file_exists("../gfx/temp/" . get_filename() . "_perex.jpg"))
	{
		rename("../gfx/temp/" . get_filename() . "_perex.jpg", "../gfx/perex/" . $_SESSION["news_id"] . ".jpg");
		rename("../gfx/temp/" . get_filename() . "_thumb.jpg", "../gfx/thumb/" . $_SESSION["news_id"] . ".jpg");
		rename("../gfx/temp/" . get_filename() . ".jpg", "../gfx/img/" . $_SESSION["news_id"] . ".jpg");
	}
	
	// make sure the original file is deleted
	admin_clean_up();
	
		
	publish_news($_SESSION["news_id"]);
	
}


function admin_display_debug()
{
	return;
	echo "<div class=\"gray_box\">";
	echo "user_id: " . $_SESSION["user_id"];
	echo "<br>news_id: " . (isset($_SESSION["news_id"]) ? $_SESSION["news_id"] : "not set");
	echo "<br>filename: " . get_filename();
	echo "</div>";
}

function admin_unset_news()
{
	if(isset($_SESSION["news_id"]))
	{
		admin_msgbox("Editovaná novinka byla odstraněna");
		unset($_SESSION["news_id"]);
	}	
}

function admin_list_carts()
{
	$query = "SELECT id, title FROM carts ORDER BY id ASC";
	
	$result = mysql_query($query);
	
	echo "<table class=\"news_list\">";

	while($row = mysql_fetch_row($result))
	{
		//$date = date("j. n. Y (G:i:s)", strtotime($row[2]));
		
		echo "<tr><td>" . $row[0] . "</td>" . 
			 	 "<td>" . $row[1] . "</td></tr>";
			 	 
		/*
		<td class="list_button">
			<form class="verbose" action="index.php" method="POST">
			<input type="hidden" name="cart_id" value="<?php echo $row[0]; ?>">
			<input type="hidden" name="action" value="<?php echo STATE_EDIT; ?>">
			<input type="submit" value="upravit">
			</form>
		</td>
		<td class="list_button">
			<form class="verbose" action="index.php" method="POST">
			<input type="hidden" name="cart_id" value="<?php echo $row[0]; ?>">
			<input type="hidden" name="action" value="delete">
			<!--<input type="submit" value="odstranit">-->
			</form>
		</td>
		
		</tr>*/

	echo "</table>";
}

function admin_add_cart_date()
{
	//$query = "INSERT id, date, title VALUES (,,)INTO carts
}

?>

ACC SHELL 2018