<?php
	!defined('IN_MANAGE') && exit('Access Denied!');
	$action=empty($action)?'manage':trim($action);
	switch($action)
	{
		case 'manage':
			$topall=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/*');
			$topfiles=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/*.*');
			$topdirs=array_diff($topall,$topfiles);

			$m=(isset($m) && $m)?$m.'/':'';
			$all=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m.'*');
			$files=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m.'*.*');
			$dirs=array_diff($all,$files);
			$names=cache_read('template.inc.php',TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m);
			foreach($files as $k => $f)
			{
				if(!in_array(get_fileext($f),array('html','css')))
				{
					unset($files[$k]);
				}
			}
			$m=str_replace('/','',$m);
			include admin_tlp('tlp');
			break;
		case 'add':
			$topall=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/*');
			$topfiles=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/*.*');
			$topdirs=array_diff($topall,$topfiles);

			$m=(isset($m) && $m)?$m.'/':'';
			if(ISTLP)
			{
				$names=cache_read('template.inc.php',TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m);
				if($do_submit)
				{
					$files=TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m.$f;
					file_put_contents($files,stripslashes(trim($html)));
					if($name)
					{
						$names[basename($f,'.'.get_fileext($f))]=trim($name);
						cache_write('template.inc.php',$names,TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m);
					}
					admin_success($adminlang['manage_success'],ADMIN_FILE.'?mod=tlp&file=tlp&action=manage&m='.str_replace('/','',$m));
				}
				$m=str_replace('/','',$m);
				include admin_tlp('tlp-add');
			}
			else
			{
				admin_error($adminlang['manage_error']);
			}
			break;
		case 'edit':
			$topall=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/*');
			$topfiles=glob(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/*.*');
			$topdirs=array_diff($topall,$topfiles);

			$m=(isset($m) && $m)?$m.'/':'';
			$files=TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m.$f;
			if(is_file($files) && ISTLP)
			{
				$names=cache_read('template.inc.php',TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m);
				if($do_submit)
				{
					file_put_contents($files,stripslashes(trim($html)));
					if($name)
					{
						$names[basename($f,'.'.get_fileext($f))]=trim($name);
						cache_write('template.inc.php',$names,TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m);
					}
					admin_success($adminlang['manage_success'],ADMIN_FILE.'?mod=tlp&file=tlp&action=manage&m='.str_replace('/','',$m));
				}
				$m=str_replace('/','',$m);
				include admin_tlp('tlp-edit');
			}
			else
			{
				admin_error($adminlang['manage_error']);
			}
			break;
		case 'delete':
			$m=(isset($m) && $m)?$m.'/':'';
			if(is_file(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m.$f) && ISTLP)
			{
				@unlink(TPL_ROOT.$LA['lang'].'/'.$LA['template'].'/'.$m.$f);
				admin_success($adminlang['manage_success'],ADMIN_FILE.'?mod=tlp&file=tlp&action=manage&m='.str_replace('/','',$m));
			}
			else
			{
				admin_error($adminlang['manage_error']);
			}
			break;
	}
?>