Location: PHPKode > projects > CMSmelborp > CMSmelborp/showtemplate.php
<?php
/*******************************************************************************
License:
	Copyright 2005 Ryan Morehart
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
Filename: /showtemplate.php

Purpose: Allows previews of templates by creating pages with random data.

Processed parameters: (TYPE - NAME - DESCRIPTION - DEFAULT)
	GET - name - name of template to display
	GET - loopdisp - number of times to display loop data - $template_display_loop_repeat in
		settings.php
	GET - benchmark - displays benchmarking data for the requested template
*******************************************************************************/

require_once 'includes/user_standard.php';

if($lock_external_site === true)
{
	?>
	<html>
	<head>
		<title>Site locked</title>
	</head>
	<body>
		<h1>Site locked</h1>
		<p>This site has been locked by an administrator. Please try again later.</p>
	</body>
	</html>
	<?php
	die();
}

// Used to create the fake data for the needed page elements
function randomString($length = 50)
{
	// Seed random number generator
	srand((double) microtime() * 1000000);

	// Create string
	$string = '';
	for($i = 0; $i < $length; $i++)
	{
		$string .= chr(rand(33, 125));
	}
	
	// Post processing
	$string = str_replace(chr(96), ' ', $string);
	$string = htmlspecialchars($string, ENT_QUOTES);
	return $string;
}

// Ensure that a template was requested
if(isset($_GET['template']))
{
	// Save name for easy use
	$templateName = $_GET['template'];
}
else
{
	?>
	<html>
	<head><title>Error retrieving template</title></head>
	<body>
	<p>You must specify a template to display.</p>
	</body>
	</html>
	<?php
	die();
}

// Get template
$template = getTemplate($connection, $templateName);
if($template === false)
{
	?>
	<html>
	<head><title>Error retrieving template</title></head>
	<body>
	<p>An error occured while retrieving the template <?php echo $templateName; ?>.</p>
	</body>
	</html>
	<?php
	die();
}

// Find the template needs
$templateNeeds = parseTemplate($connection, $template, false);
// Determine how many times we should each loop
if(isset($_GET['loopdisp']) && is_numeric($_GET['loopdisp']))
{
	$loopDisplayTimes = $_GET['loopdisp'];
}
else
{
	$loopDisplayTimes = $template_display_loop_repeat;
}

// Create a 'page' with the needs filled in
$page = array();
$templateNeedNames = array_keys($templateNeeds);
$templateNeedTypes = array_values($templateNeeds);
for($i = 0; $i < count($templateNeeds); $i++)
{
	if(!is_array($templateNeedTypes[$i]))
	{
		$page['content'][$templateNeedNames[$i]] = 'MAIN_' . randomString(
			rand($template_preview_data_min_length,	$template_preview_data_max_length));
	}
	else
	{
		if($loopDisplayTimes >= 1)
		{
			$page['content'][$templateNeedNames[$i]] = array();
			$loopElementNeeds = array_keys($templateNeedTypes[$i]);
			for($loopElement = 0; $loopElement < count($loopElementNeeds); $loopElement++)
			{
				for($j = 1; $j <= $loopDisplayTimes; $j++)
				{
					$page['content'][$templateNeedNames[$i]][$j][$loopElementNeeds[$loopElement]] =
						'LOOP_' . $j . '_' . randomString(rand($template_preview_data_min_length,
						$template_preview_data_max_length));
				}
			}
		}
	}	
}

//echo "Template: ";			var_dump($template);
//echo "Template needs: ";		var_dump($templateNeeds);
//echo "Page: ";				var_dump($page);

// Parse
$output = parseTemplate($connection, $template, $page);

if(!isset($_GET['benchmark']) || !$enable_benchmarking)
{
	// Output
	echo $output;
}
else
{
	// Save end time
	$endTime = microtime();
	
	// Figure out difference in times
	$beginTime = explode(' ', $beginTime);
	$endTime = explode(' ', $endTime);
	$seconds = $endTime[1] - $beginTime[1];
	$totalTime = ($endTime[1] - $beginTime[1]) + ($endTime[0] - $beginTime[0]);
	
	// Display
	header('Content-type: text/plain');
	echo "The page was completed in $totalTime seconds.\n\n";
	echo "Output:\n$output";
}
?>
Return current item: CMSmelborp