Location: PHPKode > projects > Cynus > cynus/templates_func.php
<?php
/**
 * @file templates_func.php - Template Functions
 * @Id $Id: templates_func.php,v 1.5.2.1 2004/08/01 19:21:30 brett Exp $
 *
 * Cynus - a web-based content manager
 * Copyright (C) 2003 Brett and Jason Profitt
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */


function templates_home() {
	global $config;
	set_page_title('Templates');
	$submenu=array(
		'Home' => 'index.php',
		'Templates Home' => ''
	);
	$content .= cynus_submenu($submenu);
	
	$menu=array(
		'templates:Add a New Template:templates.php?action=new:/images/templates_add.png',
		'templates:List/Modify Templates:templates.php?action=list:/images/templates_edit.png'
	);
	$content .= cynus_menu($menu, 'base');
	return $content;
}



function templates_new() {
	global $config;
	set_page_title('New Template');
	$submenu=array(
		'Home' => 'index.php',
		'Templates Home' => 'templates.php',
		'New Template' => ''
	);
	$content .= cynus_submenu($submenu);
	if($_POST['sent']==1) {
		$dir="$config[cynus_base_dir]/templates";
		if($_FILES['template']['name']=='') {
			$error = "<div class=\"form-error\">You did not upload a template ";
		}
		elseif(file_exists("$dir/{$_FILES[template][name]}")) {
			$error = "<div class=\"form-error\">A template already exists with the filename. Change the name of the " .
				"file on your computer and then send it again.</div>\n";
		}
		if($_POST['name']=='') {
			$error .= "<div class=\"form-error\">You must give the template a name.</div>\n";
		}
		else{
			$query="SELECT `id` from `$config[sql_prefix]templates` WHERE `name`='$_POST[name]'";
			$name_check=mysql_request($query);
			if($name_check['id'] != '') {
				$error .= "<div class=\"form-error\">A template already exists with this name.</div>\n";
			}
		}
		$template_text=implode("", file($_FILES['template']['tmp_name']));
		#and now we're checking that this is a properly formatted page
		#yes, this can be fooled very easily, but it at least does *some* checking
		$required_strings=array('html', 'head', 'title', '%%TITLE%%', '/title', '</head>', 'body',
			'%%CONTENT%%', '/body', '/html');
			
		foreach($required_strings as $string) {
			if(!stristr($template_text, $string)) {
				$error .= "<div class=\"form-error\">Your template does not contain \"" . htmlspecialchars($string) . "\" anywhere.</div>\n";
			}
		}
		
		if($error != '') {
			$_POST['sent']=0;
			$content .= $error;
			$content .= templates_new();
		}
		else{
			$template_file="$dir/{$_FILES[template][name]}";
			if (move_uploaded_file($_FILES['template']['tmp_name'], $template_file)) {
				umask(0000);
				chmod($template_file, 0644);
				
				$query="INSERT into `$config[sql_prefix]templates` (`name`, `file`, `default`) VALUES ('$_POST[name]', '{$_FILES[template][name]}', '$_POST[default]')";
				mysql_query($query);
				if($_POST['default']==1) {
					$id=mysql_insert_id();
					$query="UPDATE `$config[sql_prefix]templates` SET `default`='0' WHERE `id` != '$id'";
					mysql_query($query);
				}
				header('Location: templates.php?action=new&done=1');
			}
			else{
				$content .= 'Something went wrong. It could be that the templates directory is not writeable, or does not exist.';
			}
		}
	}
	else{
		if($_GET['done']==1) {
			$content .= 'Successfully added the new template!<br /><br />';
		}
		if($_POST['default']==1) {$checked=" checked";}
		$_POST['name']=stripslashes($_POST['name']);
		$content .= <<<___eofh
Make sure your template follows the guidelines and requirements or it will not work.<br />
<form method="POST" action="templates.php?action=new" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
<input type="hidden" name="sent" value="1" />
Template Name: <input type="text" name="name" value="$_POST[name]" maxlength="50" /><br />
File Location: <input type="file" name="template" /><br />
<input type="checkbox" name="default" value="1"$checked> Make this the default template.<br />
<input type="submit" value="Add Template" />
</form>
___eofh;
	}
	return $content;
}




function templates_list() {
	global $config;
	set_page_title('List Templates');
	$submenu=array(
		'Home' => 'index.php',
		'Templates Home' => 'templates.php',
		'Listing Templates' => ''
	);
	$content .= cynus_submenu($submenu);
	
	$row1='row1';
	$row2='row2';
	$content .= <<<___eofh
<table>
	<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td class="table-header">Template Name</td>
	</tr>\n
___eofh;

	$query="SELECT * from `$config[sql_prefix]templates` ORDER by `name`";
	$result=mysql_query($query);
	while($each_template=mysql_fetch_assoc($result)) {
		if($each_template['default']==1) {$default='style=" font-weight:bold"';}
		else{$default='';}
		
		$content .= <<<___eofh
	<tr>
		<td class="$row1"><a href="templates.php?action=delete&id=$each_template[id]">Delete</a></td>
		<td class="$row1"><a href="templates.php?action=edit&id=$each_template[id]">Edit</a></td>
		<td class="$row1"><a href="templates.php?action=view&id=$each_template[id]">View</a></td>
		<td class="$row1"$default>$each_template[name]</td>
	</tr>\n
___eofh;
		swap($row1, $row2);
		$printed=1;
	}
	if($printed != 1) {
		$content .= '<td class="row1" colspan="4">No templates found</td>';
	}
	$content .= <<<___eofh
</table>
<div class="small">Default template is <span style="font-weight:bold">bold</span></div>
___eofh;
	return $content;
}



/**********************
View Template - templates_view()
Shows a template by just printing it out from file
**********************/
function templates_view() {
	global $config;
	
	if($_GET['id']=='') {
		cynus_error('You must select a template to view!');
	}
	
	$query="SELECT * from `$config[sql_prefix]templates` WHERE `id`='$_GET[id]'";
	$template=mysql_request($query);
	
	if($template['name']=='') {
		cynus_error('You have selected a template that does not exist!');
	}
	
	
	#ok, we have a good template, we can go ahead and redirect to the template
	
	header("Location: templates/$template[file]");
	exit;

}



/**********************
Edit Template - templates_edit()
Allows you to upload a new template and change the name of it
**********************/
function templates_edit() {
	global $config;
	
	set_page_title('Edit Templates');
	$submenu=array(
		'Home' => 'index.php',
		'Templates Home' => 'templates.php',
		'Editting a  Template' => ''
	);
	$content .= cynus_submenu($submenu);

	if($_GET['id']=='') {
		cynus_error('You must select a template to edit!');
	}
	
	$query="SELECT * from `$config[sql_prefix]templates` WHERE `id`='$_GET[id]'";
	$template=mysql_request($query);
	
	if($template['name']=='') {
		cynus_error('You have selected a template that does not exist!');
	}
	
	#if they sent the template, then let's see what they want to do
	if($_POST['sent']==1) {
		if($_FILES['template']['name'] != '') {
			$dir="$config[cynus_base_dir]/templates";
			if(file_exists("$dir/{$_FILES[template][name]}") && $template['file'] != $_FILES['template']['name']) {
				$error = "<div class=\"form-error\">A template already exists with the filename. Change the name of the " .
					"file on your computer and then send it again.</div>\n";
			}
		}
		if($_POST['name']=='') {
			$error .= "<div class=\"form-error\">You must give the template a name.</div>\n";
		}
		else{
			$query="SELECT `id` from `$config[sql_prefix]templates` WHERE `name`='$_POST[name]'";
			$name_check=mysql_request($query);
			if($name_check['id'] != '' && $_POST['name'] != $template['name']) {
				$error .= "<div class=\"form-error\">A template already exists with this name.</div>\n";
			}
		}
		
		if($error != '') {
			$_POST['sent']=0;
			$content .= $error;
			$content .= templates_edit();
		}
		else{
			if($_FILES['template']['name'] != ''){$file=$_FILES['template']['name'];}
			else{$file=$template['file'];}
			$query="UPDATE `$config[sql_prefix]templates` SET `name`='$_POST[name]', `file`='$file', `default`='$_POST[default]' WHERE `id`='$_GET[id]'";
			mysql_query($query);
			if($_POST['default']==1) {
				$query="UPDATE `$config[sql_prefix]templates` SET `default`='0' WHERE `id` != '$_GET[id]'";
				mysql_query($query);
			}
			if($_FILES['template']['name'] != '') {
				unlink("$dir/$template[file]");
				$template_file="$dir/{$_FILES[template][name]}";
				if (!move_uploaded_file($_FILES['template']['tmp_name'], $template_file)) {
					$content .= 'Something went wrong. It could be that the templates directory is not writeable, or does not exist.';
					return $content;
				}
				else{
					umask(0000);
					chmod($template_file, 0644);
				}
			}
			header('Location: templates.php?action=list');
		}
	
	}
	else{
		if($_POST['default']==1 || $template['default']) {$checked=" checked";}
		$name=set_default($template['name'], stripslashes($_POST['name']));
		$content .= <<<___eofh
Make sure your template follows the guidelines and requirements or it will not work.<br />
<form method="POST" action="templates.php?action=edit&id=$_GET[id]" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
<input type="hidden" name="sent" value="1" />
Template Name: <input type="text" name="name" value="$name" maxlength="50" /><br />
File Location: <input type="file" name="template" /><br />
<input type="checkbox" name="default" value="1"$checked> Make this the default template.<br />
<input type="submit" value="Edit Template" /><br />
The template will only change looks if you upload a new one.
</form>
___eofh;
	
	}
	
	return $content;
}



function templates_delete() {
	global $config;
	
	set_page_title('Delete Templates');
	$submenu=array(
		'Home' => 'index.php',
		'Templates Home' => 'templates.php',
		'List/Modify Templates' => 'templates.php?action=list',
		'Deleting a  Template' => ''
	);
	$content .= cynus_submenu($submenu);
	
	if($_GET['id']=='') {
		cynus_error('You must select a template to delete!');
	}
	
	$query="SELECT * from `$config[sql_prefix]templates` WHERE `id`='$_GET[id]'";
	$template=mysql_request($query);
	
	if($template['name']=='') {
		cynus_error('You selected a template that does not exist!');
	}
	
	if($_GET['flag']=='yes') {
		if(@unlink("$config[cynus_base_dir]/templates/$template[file]")) {
			$query="DELETE from `$config[sql_prefix]templates` WHERE `id`='$_GET[id]'";
			mysql_query($query);
			header('Location: templates.php?action=list');
		}
		else{
			$content .= 'Unable to delete the template file "' . $template['file'] . '", you will have to do this manually.';
			$_GET['flag']=='';
		}
	}
	else{
	
		$content .= <<<___eofh
Are you sure you want to delete the "$template[name]" template?<br />
<br />
<a href="templates.php?action=delete&id=$_GET[id]&flag=yes">Yes</a> / 
<a href="templates.php?action=list">No</a>

___eofh;
	
	}
	
	
	return $content;
}


?>
Return current item: Cynus