<?php

include("system_header.php");

admin_only();



// change category cover
if(isset($_GET['photo_to_make_cover']) and $_GET['photo_to_make_cover']!='' and isset($_GET['category_box']) and $_GET['category_box']!=''){
	// verify key to make cover
	if($_GET['key']!=md5($_SESSION['session_secret'].$_GET['photo_to_make_cover'])){
		// convert the javascript/text value like "nice%20landscape" to "nice-landscape"
		$_GET['photo_to_make_cover'] = string_to_url($_GET['photo_to_make_cover']);
		// delete old cover
		@unlink("files/".$_GET['category_box']."/thumbnail.jpg");
		copy("files/".$_GET['category_box']."/".$_GET['photo_to_make_cover']."_thumb.jpg", "files/".$_GET['category_box']."/thumbnail.jpg");
		exit;
		
	}
}


// delete a photo ?!
if(isset($_GET['photo_to_delete']) and $_GET['photo_to_delete']!='' and isset($_GET['category_box']) and $_GET['category_box']!=''){
	
	// convert the javascript string like "Blue lanscape" to file string "blue-landscape"
	$_GET['photo_to_delete'] = string_to_url($_GET['photo_to_delete']); 
	
	// verify key to delete photo
	if($_GET['key']!=md5("dlt".$_SESSION['session_secret']."img")){
		
		echo '<span class="photo_edit_error"><strong>Cannot delete (key error)</strong></span>';
		exit;
		
	} else {
		
		unlink("files/".$_GET['category_box']."/".$_GET['photo_to_delete'].".jpg");
		unlink("files/".$_GET['category_box']."/".$_GET['photo_to_delete']."_thumb.jpg");
		unlink("files/".$_GET['category_box']."/".$_GET['photo_to_delete']."_source.jpg");
		exit;
		
	}
}

// veify key to edit photo
if($_GET['key']!=md5($_SESSION['session_secret'].$_GET['original_category_box'])){
	echo '<span class="photo_edit_error"><strong>Key error</strong></span>';
	exit;
}


// !!! convert them to remove spaces and replace with dashes, etc.
$_GET['original_category_box'] = string_to_url($_GET['original_category_box']);
$_GET['category_box'] = string_to_url($_GET['category_box']);

$_GET['original_title_box'] = string_to_url($_GET['original_title_box']);
$_GET['title_box'] = string_to_url($_GET['title_box']);



if($_GET['original_category_box']=='' or $_GET['category_box']=='' or $_GET['original_title_box']=='' or $_GET['title_box']==''){
	echo '<span class="photo_edit_error"><strong>Missing data (title or category)</strong></span>';
	exit;
}

if($_GET['original_category_box']!=$_GET['category_box'] or $_GET['original_title_box']!=$_GET['title_box']){
	
	// we checked that folder or name is different, so now only check if destination file alrady exists
	if(file_exists("files/".$_GET['category_box']."/".$_GET['title_box'].".jpg")){
		echo '<span class="photo_edit_error"><strong>File name already exists</strong></span>';
		exit;
	}
	
	// move photo, thumb and source
	rename("files/".$_GET['original_category_box']."/".$_GET['original_title_box'].".jpg", "files/".$_GET['category_box']."/".$_GET['title_box'].".jpg");
	rename("files/".$_GET['original_category_box']."/".$_GET['original_title_box']."_thumb.jpg", "files/".$_GET['category_box']."/".$_GET['title_box']."_thumb.jpg");
	rename("files/".$_GET['original_category_box']."/".$_GET['original_title_box']."_source.jpg", "files/".$_GET['category_box']."/".$_GET['title_box']."_source.jpg");
	
	echo '<script type="text/javascript">alert("Done");</script>';
	
	echo '<span class="photo_edit_confirmation"><strong>Data saved</strong></span>';
	exit;
	
} else {
	echo '<span class="photo_edit_error"><strong>No editing needed</strong></span>';
	exit;
}


?>



