Location: PHPKode > scripts > AskPeople-Free PHP survey application > AskPeople_2_2_2/templatemgr.php
<?php

session_start();

require_once('includes/config.inc.php');
require_once('includes/functions.inc.php');
require_once('login.inc.php');

switch ($_REQUEST['action']) { 

	case "SaveTemplate" :
	
		$tpath = "templates/" . $_POST['templatename'];

		// quick routine to prevent overwriting of other files
		$testpath = "templates/" . $_POST['oldtemplatename'];		
		if (($testpath != $tpath) && file_exists($tpath)) { // looks like trying to overwrite another file, so prevent this
			$_SESSION['statusmsg'] = "Sorry, you cannot overwrite another file.";
			header("Location: templatemgr.php?templatename=".$_REQUEST['templatename']);
			exit();
		} elseif ($tpath != $testpath) { // renaming, so delete original after saving
			$saved = saveFile($tpath,$_POST['templatehtml']);			
			$renamed = @unlink($testpath);
		} else {
			$saved = saveFile($tpath,$_POST['templatehtml']);	
		}

		if ($saved) {
			$_SESSION['statusmsg'] = "The template file was saved.";
			header("Location: templatemgr.php?templatename=".$_REQUEST['templatename']);
		} else {
			$_SESSION['statusmsg'] = "Sorry, the template file could not be saved.";
			header("Location: templatemgr.php?templatename=".$_REQUEST['templatename']);
		}

	  break;

	case "DeleteTemplate" :

	  	$deletet= "templates/" . $_REQUEST['templatename'];
	  
	  	if (file_exists($deletet) && $_REQUEST['templatename'] != "AskPeopleDefault.php") {
			$deleted = unlink($deletet);
	  	}
	
	  	if ($deleted) {
			$_SESSION['statusmsg'] = "Template deleted";
	  	} else {
			$_SESSION['statusmsg'] = "Sorry, that template could not be deleted";
	  	}
	  
	  	header("Location: index.php");

	  break;

	case "CreateTemplate" :
	
	 	include_once('surveys/apconfig.php');
	 	
		$dpath = "templates/" . $globalsettings['defaultTemplate'];
		$loaded = loadFile($dpath);

		$tpath = "templates/NewTemplate.php";

		if (!file_exists($tpath)) {
			$saved = saveFile($tpath,$loaded);	
		} else {
			$a=1;
			while ($a<=100 && $okflag != "TRUE") {
				$tpath = "templates/NewTemplate".$a.".php";
				if (!file_exists($tpath)) {
					$saved = saveFile($tpath,$loaded);
					$okflag = "TRUE";
				} else {
					$a++;
				}
			}		
		}

		if ($saved) {
			$_SESSION['statusmsg'] = "The new template was created.";
			$savedname = str_replace("templates/","",$tpath);
			header("Location: templatemgr.php?templatename=".$savedname);
		} else {
			$_SESSION['statusmsg'] = "Sorry, the new template could not be created.";
			header("Location: index.php");
		}

	  break;
	  
	case "DuplicateTemplate" :
	
		$tpath = "templates/" . urldecode($_REQUEST['templatename']);
		$loaded = loadFile($tpath);
		$tpath = "templates/" . substr(urldecode($_REQUEST['templatename']),0,-4) . " copy.php";

		if (!file_exists($tpath)) {
			$saved = saveFile($tpath,$loaded);	
		} else {
			$a=2;
			while ($a<=100 && $okflag != "TRUE") {
				$tpath = "templates/" . substr(urldecode($_REQUEST['templatename']),0,-4) . " copy".$a.".php";
				if (!file_exists($tpath)) {
					$saved = saveFile($tpath,$loaded);
					$okflag = "TRUE";
				} else {
					$a++;
				}
			}		
		}

		if ($saved) {
			$_SESSION['statusmsg'] = "The template was duplicated.";
			$savedname = str_replace("templates/","",$tpath);
			header("Location: templatemgr.php?templatename=".urlencode($savedname));
		} else {
			$_SESSION['statusmsg'] = "Sorry, the template could not be duplicated.";
			header("Location: index.php");
		}

	  break;


	case "DownloadTemplate" :
	
		$tpath = "templates/" . $_REQUEST['templatename'];
		$loaded = loadFile($tpath);
		
		$dodgy = array(" ","'",",",".");		
		$nicefilename = $_REQUEST['templatename'];

		header("Content-Type: text/plain");
		// a load of extra headers here so IE doesn't choke on the file...
		header("Pragma: public");
		header("Expires: 0"); // set expiration time
		header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
		// define filename for download as export.txt
		header("Content-Disposition: attachment; filename=\"".$nicefilename."\"");
		
		echo ($loaded);
		exit();
	
	  break;
	
	default : // same as EditTemplate
	
		if (eregi(".php",$_REQUEST['templatename'])) { // we're editing a real template, not a CSS or JS file
			$onloadfunc = "toggleVisibility('editform')";
		}

	    include('includes/header.php');
		
		$tpath = "templates/" . $_REQUEST['templatename'];
		$loaded = loadFile($tpath);

		if ($_REQUEST['otherfile'] == "true") {
			$title = "File name:";
		} elseif ($_REQUEST['askpeopleconfig'] == "true") {
			$title = "AskPeople settings:";		
		} else {
			$title = "Template name:";
		}
		
		$_SESSION['statusmsg'] = null;
		
		$outputform = "
			 <form action='templatemgr.php' method='post' class='clearbox'>
			 <h2 style='margin: 5px 0;'>{$title} <input type='text' name='templatename' value=\"{$_REQUEST['templatename']}\" style='font-size: 1em;' size='40' /></h2>
			 <br />";
				
		if (eregi(".php",$_REQUEST['templatename'])) {
			$preview = "
			   <h2><a id='previewbtn' onclick=\"toggleTemplatePreview();\" href='#'>Edit Template Code &gt;&gt;</a></h2><br />
			   <div id='preview' style='width: 99%;'>
				  <iframe id='siteview' width='97%' height='100%' src=\"templatepreview.php?templatename=templates/{$_REQUEST['templatename']}\" style='height:350px;'></iframe>			
			   </div>
			";
		}
		
		$outputform .= "
			{$preview}
			<div id='editform' style='width:98%;'>
				<textarea cols='70' rows='25' name='templatehtml'>{$loaded}</textarea>
				<input type='hidden' name='action' value='SaveTemplate' />
				<input type='hidden' name='oldtemplatename' value=\"{$_REQUEST['templatename']}\" />
			</div>
			<br /><p style='text-align:center;'><input type=\"submit\" value='Save changes' /> or <a href=\"templatemgr.php?action=DuplicateTemplate&amp;templatename={$_REQUEST['templatename']}\">Duplicate this template</a> or <a onclick=\"return confirmSubmit('Are you sure you want to permanently delete this template?')\" href='templatemgr.php?action=DeleteTemplate&amp;templatename={$_REQUEST['templatename']}'>Delete this template</a> or <a href=\"templatemgr.php?action=DownloadTemplate&amp;templatename={$_REQUEST['templatename']}\">Download this template</a></p>
			</form>	
		";
		
	  break;
}

echo ($outputform);

include('includes/footer.php');

?>
Return current item: AskPeople-Free PHP survey application