<?php
	
	if(!$dprotect) die ('Access Denied!');
	
	$downloadDetails = $core->downloadDetailsForId($link[2]);
	$smarty->assign('gt', $downloadDetails['name']);
		
	switch($link[3]) {
		
		case 'add':
			if($_POST['action'] == 'add' && is_uploaded_file($_FILES['file']['tmp_name'])) {
				$ext = $core->extension($_FILES['file']['name']);
                                $_POST['filetitle'] = trim($_POST['filetitle']);
				if($_FILES["file"]["size"] > $config['maxfilesize']*1024000) {
					$_SESSION['msg']['title'] = 'The file is to large (max. '.$config['maxfilesize'].' MB).';
					$_SESSION['msg']['err'] = true;
				}
                                if(!$_POST['filetitle']) {
                                    $_SESSION['msg']['title'] = 'No file description.';
                                    $_SESSION['msg']['err'] = true; 
                                }
				if(!$_SESSION['msg']) {					
					$filename = substr(md5(time()+rand(0, 5000)), 0, 8).'.'.$ext;					
					move_uploaded_file($_FILES['file']['tmp_name'], $config['filedir'].'/'.$filename);					
					$core->addFile($link[2], $filename, $_POST['filetitle']);
				}				
				header('Location: '.$config['url'].'admin/files/'.$link[2]);
				exit();
			} 
		break;
                
                case 'edit':
			if($_POST['action'] == 'edit' && $core->updateFile($link[4], $_POST['filetitle'])) {
				header('Location: '.$config['url'].'admin/files/'.$link[2]);
				exit();
			} elseif(!$_POST['action']) {
				$gallery = $core->fileDetailsForId($link[4]);
				foreach($gallery as $k => $v) { $_POST[$k] = $v; }
			}
		break;
		
		case 'del':
			if($_POST['action']=='del') {
				if($_POST['confirm']) {
					$core->delFile($link[4]);
					header('Location: '.$config['url'].'admin/files/'.$link[2]);
					exit();
				}
				$_SESSION['msg']['title'] = 'You have to confirm the removal.';
				$_SESSION['msg']['err'] = true;	
			}
		break;
		
		case 'swap':
			$core->swapFile($link[2], $link[4], $link[5]);
			header('Location: '.$config['url'].'admin/files/'.$link[2]);
			exit();
		break;
		
		default:
			$smarty->assign('list', $core->filesForDownload($link[2]));
			
	}	
	
?>