Location: PHPKode > projects > DynPage > dynpage/logic/dynpage.page.inc.php
<?php
/*
DynPage V1.01 - A simple Content Management System
Copyright (C) 2009-2010 Matthias Wiede <hide@address.com>

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 3 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, see http://www.gnu.org/licenses.
*/

if (!defined ('DYNPAGE_LOGIN')) {
	exit;
}

require_once ("lib/input.inc.php");

$onLoad = "javascript:if (document.getElementById ('focus_id')) document.getElementById ('focus_id').focus();";

$FIELDS = array 
(
	"name" =>  "0;s;m",
	"url" =>  "0;s;*",
	"content" => "0;s;*"
);

function createCompatibleFilename ($name)
{
	$filename = "";
	$ignoreSpace = false;
	// Replace special chars
	$name = str_replace (
		array ("ä","Ä","ö","Ö","ü","Ü","ß"),
		array ("ae","ae","oe","oe","ue","ue","ss"),
		$name);
	
	$name = utf8_decode ($name);	
	$length = strlen($name);
	$ignoreSpace=false;	
	for ($i=0;$i<$length;$i++) {
		$c = $name[$i];

		if ($ignoreSpace && $c==' ')
			continue;
			
		if ($c==' ') {				
			$c="_"; $ignoreSpace=true;
		}
		else {
			$c = strtolower ($c);
			if (($c<"a" || $c>"z") && ($c<"0" || $c>"9") && $c!="_")
				$c="";
			else
				$ignoreSpace=false;	
		}
		$filename.=$c;
	}
	return $filename;
}


function savePage ($filename, &$pageData, $fNew=true) {
	$fh = fopen($filename, 'w');
	if (!$fh) {
		return false;
	}				
	else {
		fwrite ($fh, "<!-- name=".htmlspecialchars ($pageData["name"]).";filetime=".$pageData["filetime"].";url=".$pageData["url"]."-->\r\n");
		fwrite ($fh, $pageData["content"]);
		fclose ($fh);
		chmod  ($filename, 0666);
		
		
	}
	return true;
}

if ($pidFromFile)
	$pid = $pidFromFile;
else
	$pid = (int) getParam ("pid");

$values=array ();	
if (isset ($_SESSION["pageFileList"][$pid]))
	$values = $_SESSION["pageFileList"][$pid];

$errors=array ();

$fShowErrors = false;
if ($cmd=="new" || $cmd=="edit")
	$fShowErrors = true;
$errCnt  = loadInputParams ($FIELDS, $values, 0, $errors, true, $fShowErrors);

$errText = "";
$successText = "";

switch ($cmd) 
{

	case "new":
		if ($errCnt>0)
			$errText = "Please check marked fields.";
		else
		if (isDemo ()) {
			$errText = "Cannot create new pages in Demo mode.";		
		}
		else
		{
			$filename = ROOT_PATH."/".getConf ("content_path")."/".createCompatibleFilename ($values["name"]).".".getConf ("file_ext", "htm");
			if (file_exists ($filename)) {
				$errText = "Filename '".basename ($filename)."' just exist. Please choose another page filename.";
			}
			else {
				// Create file and save content
				$values["filetime"]=time ();
				if (!savePage ($filename, $values))
					$errText = "Cannot create file: ".$filename."";
				else {
					$values["filename"]=$filename;
					$successText = "Page was created.";
					loadPageList (true);
				}				
			}
		}
		break;

	case "edit":
		if (!isset ($_SESSION["pageFileList"][$pid])) {
			$errText = "Please select a page.";
		}
		else
		if ($errCnt>0)
			$errText = "Please check marked fields.";
		else
		if (isDemo ()) {
			$errText = "Cannot modify pages in Demo mode.";		
		}
		else	
		{			
			$pageData = $_SESSION["pageFileList"][$pid];
			if (substr(decoct(fileperms($pageData["filename"])),2)!="666") {
				$errText = $pageData["filename"]." has no file write permissions (666)";
			}
			else
			{			
				$values["filetime"]=time ();
				if (!savePage ($pageData["filename"], $values)) {
					$errText = "Cannot create file:".basename ($pageData["filename"])."";
				}
				else {			
					$successText = "Page was saved.";
					// Save changes in session
					$values["filename"] = $pageData["filename"];
					$_SESSION["pageFileList"][$pid] = $values;
				}
			}
		}
		break;

	case "delete":
		if (!isset ($_SESSION["pageFileList"][$pid])) {
			$errText = "Please select a page.";
		}
		else
		if (isDemo()) {
			$errText = "Cannot delete pages in Demo mode.";		
		}
		else {
			$pageData = $_SESSION["pageFileList"][$pid];
			if (file_exists ($pageData["filename"])) {
				if (substr(decoct(fileperms($pageData["filename"])),2)!="666") {
					$errText = $pageData["filename"]." has no file write permissions (666)";
				}
				else {
					unlink ($pageData["filename"]);
					unset ( $_SESSION["pageFileList"][$pid]);
					$successText = "Page with filename '".basename ($pageData["filename"])."' was deleted.";
					$pid = 0;
				}
			}
		}
		break;
		

}

if ($pid!=0 && $errText=="") {
	// Load page
	if (isset ($_SESSION["pageFileList"][$pid])) {
		$values = $_SESSION["pageFileList"][$pid];
	}
	else {
		$errText = "Page ID ".$pid." does not exist.";
		$pid = 0;
	}
}
?>
Return current item: DynPage