<?php
class Templates
{
	function ShowAll()
	{
		$p = (isset($_REQUEST['path']) && !empty($_REQUEST['path'])) ? '/' . $_REQUEST['path'] : '';
		
		$d = BASE_DIR . 'theme' . $p;
		$handle = opendir($d); 
		$folders = array();
		while ($file = readdir ($handle))
		{ 
			if($file != '.' && $file != '..' && $file != 'js' && $file != 'images' && $file != 'css') 
			{ 
				$f = '';
				$method = (isset($_REQUEST['showfiles']) && $_REQUEST['showfiles']==1) ? 'is_file' : 'is_dir';
				if($method($d.'/'.$file))
				{ 
					$f->Name = $file;
				} 
				array_push($folders,$f);
			} 
			
		} 
		$GLOBALS['tmpl']->assign('folders', $folders);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/templates/showall.tpl'));
	}
	
	function ShowFiles()
	{
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$f = BASE_DIR . 'theme/' . $_REQUEST['path'] . '/' . $_REQUEST['file'];
			
			if(is_writable($f))
			{
				$fd = @fopen(BASE_DIR . 'theme/' . $_REQUEST['path'] . '/' . $_REQUEST['file'], "w");
				$fc = stripslashes($_POST['file_content']);
				if(!perm('templates_phpcode'))
				{
					$fc =  eregi_replace('{php}(.*){/php}','',$fc);
					$fc =  str_replace(array('{/php'),'',$fc);
				}
				fwrite($fd, $fc);
				fclose($fd);
			}
		}
		
		$topnav = explode('/', $_REQUEST['path']);
		
		
		$d = BASE_DIR . 'theme/' . $_REQUEST['path'];
		$handle = @opendir($d); 
		$folders = array();
		while ($file = @readdir ($handle))
		{ 
			$endg = @explode('.', $file);
			
			if(@$file != '.' && @$file != '..' && (@$endg[1]=='tpl' || @$endg[1]=='css')) 
			{ 
				$f = '';
				$method = (isset($_REQUEST['showfiles']) && $_REQUEST['showfiles']==1) ? 'is_file' : 'is_dir';
				if(is_file($d.'/'.$file))
				{ 
					#echo "$file<br>";
					$f->Name = $file;
				} 
				array_push($folders,$f);
			} 
			
		} 
		
		if(isset($_REQUEST['file'])  && !empty($_REQUEST['file']))
		{
		
			$d = BASE_DIR . 'theme/' . $_REQUEST['path'] . '/' . $_REQUEST['file'];
			$d = str_replace('./','---',$d);
		
			if(file_exists($d))
			{
				$fd = @fopen($d, "r");
				$content = @fread($fd, filesize($d));
				@fclose($fd);
				$writeable = (is_writable($d)) ? 1 : 0;
			
				$GLOBALS['tmpl']->assign('writeable', $writeable);
				$GLOBALS['tmpl']->assign('file_edit', 1);
				$GLOBALS['tmpl']->assign('file_content', $content);
			} else {
				header('Location:index.php?do=templates');
			}
			
		}
		
		$GLOBALS['tmpl']->assign('topnav', $topnav);
		$GLOBALS['tmpl']->assign('folders', $folders);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/templates/showfiles.tpl'));
		
	}
}

?>