Location: PHPKode > projects > CMSmelborp > CMSmelborp/includes/pages.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: /includes/pages.php

Purpose: Holds page processing functions for both display and creation/updating.

Processed parameters: (TYPE - NAME - DESCRIPTION - DEFAULT)
	None
*******************************************************************************/

function getPage(&$connection, $name)
{
	// Ensure the name is valid
	if(!isValidPageName($connection, $name, true))
	{
		return false;
	}
	
	$page = mysql_query("SELECT * FROM pages WHERE page_name='$name';", $connection);
	
	// Ensure the page was retrieved
	if($page === false)
	{
		return false;
	}
	
	$page = mysql_fetch_array($page, MYSQL_ASSOC);
	
	// Get, parse, and save the main page data
	$mainData = mysql_query("SELECT * FROM page_main_data WHERE page_name='$name';", $connection);
	$mainData = mysql_fetch_array($mainData, MYSQL_ASSOC);
	if($mainData !== false)
	{
		$page['content'] = parsePageData($mainData['data']);
	}
	
	// Get the loop data
	$pageLoops = getLoopItems($connection, $name);
	if(is_array($pageLoops) && !empty($pageLoops))
	{
		if(isset($page['content']))
		{
			$page['content'] = array_merge($page['content'], $pageLoops);
		}
		else
		{
			$page['content'] = $pageLoops;
		}
	}
	
	return $page;
}

function parsePageData($dataString)
{
	global $page_data_sep;
	global $page_name_sep;
	
	// Break data into separate strings for each element
	//$dataElements = preg_split("/(?<!\\\){$page_data_sep}/", $dataString);
	$dataElements = explode($page_data_sep, $dataString);
	
	// Check if there is "no" content in the page data.
	if(!strstr($dataElements[0], $page_name_sep))
	{
		return $dataElements;
	}
	
	// Break each of those into name=>data combinations
	$data = array();
	foreach($dataElements as $currData)
	{
		//$name_dataCombination = preg_split("/(?<!\\\){$page_name_sep}/", $currData, 1);
		$name_dataCombination = explode($page_name_sep, $currData);
		$data[$name_dataCombination[0]] = $name_dataCombination[1];
	}
	
	return $data;
}

function createPage(&$connection, $pageName, $templateName, $description, $linkedDataPage = false)
{
	// Ensure valid page and template names were passed in
	if(!isValidPageName($connection, $pageName, false) || pageExists($connection, $pageName)
		|| !isValidTemplateName($connection, $templateName, false))
	{
		return false;
	}
	
	// Create
	$success = mysql_query('INSERT INTO pages VALUES(\''
		. $pageName . "', '"
		. $templateName . "', "
		. ($linkedDataPage ? 'true' : 'false') . ", '"
		. addslashes($description) . "');", $connection);
	if($success === false)
	{
		return false;
	}
	
	return true;
}

function setPageInfo(&$connection, $oldName, $newName, $templateUsed, $description)
{
	// Ensure we have valid page and template names
	if(!isValidPageName($connection, $oldName, true)
		|| !isValidPageName($connection, $newName)
		|| ($newName != $oldName && pageExists($connection, $newName))
		|| !isValidTemplateName($connection, $templateUsed))
	{
		return false;
	}
	
	$success = mysql_query("UPDATE pages SET "
		. "page_name='$newName', "
		. "template_name='$templateUsed', "
		. "description='" . addslashes($description) . "'"
		. " WHERE page_name='$oldName';", $connection);
	if($success === false)
	{
		return false;
	}
	
	// Now update the main and loop page data sections
	mysql_query("UPDATE page_main_data SET page_name='$newName' WHERE page_name='$oldName';");
	mysql_query("UPDATE page_loop_data SET page_name='$newName' WHERE page_name='$oldName';");
	
	return true;
}

function setPageMainData(&$connection, $name, $data)
{
	// Ensure we have valid page and template names
	if(!isValidPageName($connection, $name, true))
	{
		return false;
	}
	
	$success = mysql_query("REPLACE INTO page_main_data VALUES("
		. "'$name', "
		. "'" . addslashes($data) . "'"
		. ");", $connection);
	if($success === false)
	{
		return false;
	}
		
	return true;
}

function deletePage(&$connection, $name)
{
	// Ensure we have a valid page name
	if(!isValidPageName($connection, $name, true))
	{
		return false;
	}
	
	// Remove page from main listing
	$success = mysql_query("DELETE FROM pages WHERE page_name='$name';", $connection);
	if($success === false)
	{
		return false;
	}
	
	// Now remove the data
	mysql_query("DELETE FROM page_main_data WHERE page_name='$name';", $connection);
	mysql_query("DELETE FROM page_loop_data WHERE page_name='$name';", $connection);
	
	return true;

}

function getPageList(&$connection, $getLinkedDataPages = true)
{
	// Get the list of pages from database
	if($getLinkedDataPages)
	{
		$pages = mysql_query("SELECT * FROM pages ORDER BY page_name;", $connection);
	}
	else
	{
		$pages = mysql_query("SELECT * FROM pages WHERE linked_data_page=false ORDER BY page_name;",
			$connection);
	}
	if($pages === false)
	{
		return false;
	}
	
	// Put into an array
	$temp = fetchResults($pages);
	
	return $temp;
}

function isValidPageName(&$connection, $name, $isExistent = false)
{
	// Ensure it conains only valid characters
	if(!ereg('^[_a-zA-Z0-9]+$', $name))
	{
		return false;
	}
	
	// Check if this name is already in the database (if we are supposed to)
	if($isExistent)
	{
		return pageExists($connection, $name);
	}
	else
	{
		return true;
	}
}

function pageExists(&$connection, $name)
{
	$exists = mysql_query("SELECT * FROM pages WHERE page_name='$name';");
	if(mysql_fetch_array($exists, MYSQL_ASSOC) !== false)
	{
		return true;
	}
	else
	{
		return false;
	}
}
?>
Return current item: CMSmelborp