<?php
	
	if(!$dprotect) die ('Access Denied!');
	
	$galleryDetails = $core->galleryDetailsForId($link[2]);
	$smarty->assign('gt', $galleryDetails['name']);
		
	switch($link[3]) {
		
		case 'add':
			if($_POST['action'] == 'add' && is_uploaded_file($_FILES['photo']['tmp_name'])) {
				$ext = $core->extension($_FILES['photo']['name']);
				if(!($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif')) {
					$_SESSION['msg']['title'] = 'Invalid file type (acceptable: jpg, jpeg, png, gif).';
					$_SESSION['msg']['err'] = true;
				}
				if($_FILES["photo"]["size"] > $config['maximgsize']*1024000) {
					$_SESSION['msg']['title'] = 'The file is to large (max. '.$config['maximgsize'].' MB).';
					$_SESSION['msg']['err'] = true;
				}
				if(!$_SESSION['msg']) {
					$core->convertUploadedImage($_FILES['photo']);
					$filename = substr(md5(time()+rand(0, 5000)), 0, 8).'.'.$ext;					
					move_uploaded_file($_FILES['photo']['tmp_name'], $config['imagedir'].'/'.$filename);
					$core->createThumbnail($config['imagedir'].'/'.$filename, $config['thumbdir'].'/'.$filename);
					$core->addImage($link[2], $filename);
				}				
				header('Location: '.$config['url'].'admin/photos/'.$link[2]);
				exit();
			}
		break;
		
		case 'del':
			if($_POST['action']=='del') {
				if($_POST['confirm']) {
					$core->delImage($link[4]);
					header('Location: '.$config['url'].'admin/photos/'.$link[2]);
					exit();
				}
				$_SESSION['msg']['title'] = 'You have to confirm removal.';
				$_SESSION['msg']['err'] = true;	
			}
		break;
		
		case 'swap':
			$core->swapImage($link[2], $link[4], $link[5]);
			header('Location: '.$config['url'].'admin/photos/'.$link[2]);
			exit();
		break;
		
		default:
			$smarty->assign('list', $core->imagesForGallery($link[2]));
			
	}	
	
?>