Location: PHPKode > projects > OpenNitro > trunk/Nitro/Page.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Page                                                             |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library 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 Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: Page.inc.php 218 2008-04-01 19:59:05Z oskarsson $
//
// This file contains the Nitro Page generation class
//

/**
 * Nitro Page
 *
 * This is the main engine of Nitro. Here the page objects are retreived,
 * initialized and drawn onto the page template.
 *
 * @package	Nitro
 * @subpackage	Base
 * @author 		Siggi Oskarsson
 * @version 	$Revision: 1.34 $
 * @copyright	2004 June Systems BV
 */

/**
 * Defines used by this class
 */
define ('NITRO_DEFAULT_TEMPLATENAME', "Defaults/Templates/Default.tpl");

/**
 * Require the Nitro template engine
 */
require_once "Nitro/Template.inc.php";

/**
 * Nitro Page Class
 *
 * This class creates a new page, with all linked objects, and draws it with
 * the selected template.
 *
 * <CODE>
 * Usage:
 *   $Page = new NitroPage();
 *   $Page->InitPageFromString($_GET["P"]);
 *   echo $Page->Draw();
 * </CODE>
 *
 * @package	Nitro
 * @subpackage	Base
 * @access	public
 */
class NitroPage {
	/**
	 * ID string of the module
	 * @var	string
	 */
	var $IDString;
	/**
	 * ID of the Page being rendered
	 * @var	int
	 */
	var $PageID;
	/**
	 * ID of the PageData being rendered
	 * @var int
	 */
	var $PageDataID;
	/**
	 * Language of the page to render
	 * @var string
	 */
	var $Language;
	/**
	 * Version of the current page
	 * @var string
	 */
	var $Version;
	/**
	 * Edit date of the current page
	 * @var string
	 */
	var $EditedOn;
	/**
	 * Array containing all drawn objects
	 * @var array
	 */
	var $Data = Array();
	/**
	 * ID of the template to render the page on
	 * @var int
	 */
	var $TemplateID;
	/**
	 * Name of the default Nitro template to use if TemplateID is not set
	 * @var string
	 */
	var $DefaultTemplate = "Defaults/Templates/Default.tpl";
	/**
	 * Name of the default Nitro error template to use if TemplateID is not set
	 * @var string
	 */
	var $DefaultErrorTemplate = "Defaults/Templates/Error.tpl";
	/**
	 * Array containing all objects
	 * @var array
	 */
	var $Objects = Array();
	/**
	 * Title of the Page
	 * @var string
	 */
	var $Title = NULL;
	/**
	 * is caching allowed?
	 * @var boolean
	 */
	var $CachingAllowed = TRUE;
	/**
	 * is page caching allowed?
	 * @var boolean
	 */
	var $PageCachingAllowed = TRUE;
	/**
	 * Dissallow page caching, because of module not allowed to be cached?
	 * @var boolean
	 */
	var $noPageCache;
	/**
	 * All Errors are placed in this var
	 * @var array
	 */
	var $Error;
	/**
	 * Last Error code
	 * @var int
	 */
	var $ErrorCode;
	

	var $DrawObjectCallBackFunctions = Array();
	var $DrawTemplateVariableCallBackFunctions = Array();
	var $DrawCallBackFunctions = Array();

	/**
	 * Nitro Page constructor
	 *
	 * The function initializes the nitro page class and sets the default class
	 * variables to use in the rendering. These values could be overridden after
	 * the class was initialized.
	 *
	 * Variables set:
	 *  - $this->DB
	 *  - $this->Sess
	 *  - $this->Conf
	 *  - $this->Security
	 *  - $this->SecurityGroups
	 *  - $this->Language
	 *
	 * @access	public
	 */
	function NitroPage() {
		DebugGroup("Page", "Init", "New Page init", __FILE__, __LINE__, DEBUG_APPL_OK);
		global $DB, $__NSess, $__NitroConf;
		$this->DB =& $DB["Nitro"];
		$this->Sess =& $__NSess;
		$this->Conf =& $__NitroConf->CONF;
		if ($this->Security = $this->Conf["Settings"]["Security"] && is_array($this->Sess->SecurityGroups)) {
			$this->SecurityGroups = $this->Sess->SecurityGroups;
		} else {
			$this->SecurityGroups = Array();
		}
		if (!count($this->SecurityGroups)) $this->SecurityGroups[] = 0;
	
		$this->Language = $this->Sess->Language;
		DebugCloseGroup(DEBUG_APPL_OK);
	}
	
	/**
	 * Initialize Page from System string
	 *
	 * This function loads the page class with the page defined by the System
	 * ID string given. The Systems ID string is the readable unique string
	 * of the page given by the module itself.
	 * This function uses InitPageFromSystemString to further load the page.
	 *
	 * @see InitPageFromSystemString()
	 * @param	string	$SystemID	System ID of page to load
	 * @access	public
	 */
	function InitPageFromSystemString($SystemID = FALSE){
		DebugGroup("Page", "InitPageFromSystemString", "Init page with: ".$SystemID, __FILE__, __LINE__, DEBUG_APPL_OK);

		$RV = $this->InitPageFromString($IDString, $SystemID);

		DebugCloseGroup(DEBUG_APPL_OK);
		return $RV;
	}

	/**
	 * Initialize Page from ID string
	 *
	 * This function loads the page class with the page defined by the ID string
	 * given. The ID string is the non-readable, random, unique string of the page
	 * created by Nitro on creation of the page.
	 *
	 * @param	string	$IDString	ID string of page to load
	 * @access	public
	 */
	function InitPageFromString($IDString = FALSE, $SystemID = FALSE) {
		DebugGroup("Page", "InitPageFromString", "Init page with: ".$IDString, __FILE__, __LINE__, DEBUG_APPL_OK);
		if (count($this->Sess->PageStack)) {
			Debug("Page", "InitPageFromString", "Session stack is filled, setting page to view", __FILE__, __LINE__, DEBUG_APPL_OK);
			$this->IDString = array_pop($this->Sess->PageStack);
			if (!count($this->Sess->PageStack)) {
				// Last stack element => redir
				if (!$URL = $this->Conf["Settings"]["PageURL"]) {
					$URL = "/index.php?P=";
				}
				$this->Sess->SaveToDisk();
				Header("Location: ".$URL.$this->IDString);
				exit;
			}
		} else {
			$this->IDString = $IDString;
		}

		//dit is een workaround zodat er niet meerdere pageobjecten van verschillende talen worden aangeroepen
		$Query = "
			SELECT
				PD.PageDataID,
				PD.PageID,
				PD.Version,
				PD.Title,
				PD.TemplateID,
				UNIX_TIMESTAMP(PD.EditedOn) AS EditedOn
			FROM Page AS P
			".($this->Security ? "
			INNER JOIN Page_SecurityGroup AS PSP
				 ON PSP.PageID = P.PageID
				AND PSP.SecurityGroupID IN (".implode(",", $this->SecurityGroups).")
			" : "")."
			LEFT JOIN PageData AS PD
				ON PD.PageID = P.PageID
				AND (PD.Language = ".NitroPrepareDB($this->Language)." OR PD.Language = '')
				AND PD.Published = 1
				AND (PD.VisibleFrom <= CURRENT_TIMESTAMP OR PD.VisibleFrom IS NULL)
				AND (PD.VisibleTill >= CURRENT_TIMESTAMP OR PD.VisibleTill IS NULL)
			WHERE ".($SystemID !== FALSE ? "P.SystemIDString = ".NitroPrepareDB($SystemID) : "P.IDString = ".NitroPrepareDB($this->IDString))."
			ORDER BY PD.Language DESC, PD.Version DESC
		";
		$Result = $this->DB->query($Query);
		if ($Result->numRows()) {
			$Row = $Result->fetchArray();
			$this->PageID = $Row["PageID"];
			$this->PageDataID = $Row["PageDataID"];
			$this->TemplateID = $Row["TemplateID"];
			$this->Title = $Row["Title"];
			$this->Version = $Row["Version"];
			$this->EditedOn = $Row["EditedOn"];				
		} else {
			$this->PageError(($SystemID !== FALSE ? NitroGetPageIDString($SystemID) : $this->IDString));
		}
		DebugCloseGroup(DEBUG_APPL_OK);
		return TRUE;
	}
	
	function LoadPageData()
	{
		$Query = "
			SELECT PD.PageDataID,
				PO.Settings AS RuntimeSettings, PO.TemplateVariable, PO.SortOrder,
				O.ObjectID, O.IDString AS ObjectIDString, O.DefaultSettings,
				OD.Settings, OD.Version AS ObjectVersion, UNIX_TIMESTAMP(OD.EditedOn) AS ObjectLastEdited,
				M.File, M.Class
			FROM (PageData AS PD, PageObjects AS PO,
						Object AS O,
						Module AS M)
			LEFT JOIN ObjectData AS OD
				 ON OD.ObjectID = O.ObjectID
				AND (OD.Language = ".NitroPrepareDB($this->Language)." OR OD.Language = '')
				AND OD.Published = 1
				AND (OD.VisibleFrom <= NOW() OR OD.VisibleFrom IS NULL)
				AND (OD.VisibleTill >= NOW() OR OD.VisibleTill IS NULL)
			WHERE PO.PageDataID = PD.PageDataID
				AND O.ObjectID = PO.ObjectID
				AND O.ModuleID = M.ModuleID
				AND PD.PageDataID = ".(int)$this->PageDataID."
			ORDER BY PO.SortOrder, OD.Language ASC, OD.Version ASC
		";
		$Result = $this->DB->query($Query);
		if ($Result->numRows() && (int)$this->PageDataID) {
			DebugGroup("Page", "InitPageFromString", "Fetching results and sorting versions etc.", __FILE__, __LINE__, DEBUG_APPL_OK);
			$Page = Array();
			while($Row = $Result->fetchArray()){
				// Convert Edited dates to unix timestamp
				$Row['EditedOn'] = strtotime($Row['EditedOn']);
				$Row['ObjectLastEdited'] = strtotime($Row['ObjectLastEdited']);
				$Page[$Row["PageDataID"]."_".$Row["TemplateVariable"]."_".$Row["SortOrder"]] = $Row;
			}
			DebugCloseGroup(DEBUG_APPL_OK);

			DebugGroup("Page", "InitPageFromString", "FillObjects", __FILE__, __LINE__, DEBUG_APPL_OK);
			foreach($Page AS $Row) {
				Debug("Page", "InitPageFromString", "Adding object to Object Array: ".$Row["Class"], __FILE__, __LINE__, DEBUG_APPL_OK);
				$this->Objects[] = array(
					"Class" => $Row["Class"],
					"File" => $Row["File"],
					"DefaultSettings" => $Row["DefaultSettings"],
					"Settings" => $Row["Settings"],
					"RuntimeSettings" => $Row["RuntimeSettings"],
					"Variable" => $Row["TemplateVariable"],
					"ObjectID" => $Row["ObjectID"],
					"ObjectLastEdited" => $Row["ObjectLastEdited"],
					"Object" => FALSE
				);
			}
			DebugCloseGroup(DEBUG_APPL_OK);
		}
		$Result->free();
	}

	/**
	 * Catch page error if loading failed
	 *
	 * This function catches all errors of the loading of the page. This generally occurs
	 * if the user failed to link a template to the page, or the user has no security
	 * rights to view the page etc.etc.
	 *
	 * @param	string	$IDString	ID string of page check for error
	 * @access	private
	 */
	function PageError($IDString) {
		DebugGroup("Page", "PageError", "Error occurred", __FILE__, __LINE__, DEBUG_APPL_ERR);
		$Query = "SELECT P.PageID, PD.TemplateID, PD.Title, T.IDString AS TemplateIDString
							".($this->Security ? ", PSP.SecurityGroupID AS SecurityOK" : ", 1 AS SecurityOK")."
							FROM Page AS P
							LEFT JOIN PageData AS PD
								 ON PD.PageID = P.PageID
								AND (PD.Language = ".NitroPrepareDB($this->Language)." OR PD.Language = '')
								AND PD.Version = 1
							LEFT JOIN Template AS T
								 ON T.TemplateID = PD.TemplateID
							".($this->Security ? " LEFT JOIN Page_SecurityGroup AS PSP ON P.PageID = PSP.PageID AND PSP.SecurityGroupID IN (".implode(",", $this->SecurityGroups).")" : "")."
							WHERE P.IDString = ".NitroPrepareDB($IDString)."
							GROUP BY P.PageID, PD.TemplateID, PD.Title, T.IDString, PSP.SecurityGroupID
							";
							//echo '<pre>'.$Query; exit;
		$Result = $this->DB->query($Query);
		if ($Result->numRows()) {
			$Row = $Result->fetchArray();
			$this->PageID = $Row["PageID"];
			if (!$Row["SecurityOK"]) {
				// page is secured and user has no rights
				if ($this->Sess->SessionExpired) {
					$this->Error = "Your session has expired, please log on";
					$this->ErrorCode = 401; // unautorized
				} else {
					$this->Error = "Page is secured and you have no rights";
					$this->ErrorCode = 401; // unautorized
				}
				Debug("Page", "PageError", $this->Error, __FILE__, __LINE__, DEBUG_APPL_ERR);
				$this->TemplateID = $Row["TemplateID"];
				$this->Data["Body"][] = $this->Sess->DrawLoginScreen();
			} elseif ($Row["TemplateIDString"]) {
				// empty page, no objects
				$this->Error = "Page contains no data";
				$this->ErrorCode = 206; // partial content
				Debug("Page", "PageError", $this->Error, __FILE__, __LINE__, DEBUG_APPL_ERR);
				$this->TemplateID = $Row["TemplateID"];
				$this->Data["Body"][] = "Error, this page contains no data!";
			} elseif (strlen($Row["TemplateID"])) {
				// template does not exist -> use default
				$this->Error = "Template does not exist";
				$this->ErrorCode = 206; // partial content
				Debug("Page", "PageError", $this->Error, __FILE__, __LINE__, DEBUG_APPL_ERR);
				$this->TemplateID = FALSE;
				$this->Data["Body"][] = "Error, the template used on this page does not exist!";
			} else {
				// page data does not exist, language missing?
				$this->Error = "Page does not exist in this language";
				$this->ErrorCode = 404; // page does not exist
				Debug("Page", "PageError", $this->Error, __FILE__, __LINE__, DEBUG_APPL_ERR);
				$this->TemplateID = FALSE;
				$this->Data["Body"][] = "Error, this page does not exist in this language!";
			}
		} else {
			// page does not exist
			$this->Error = "No result set, page does not exist";
			$this->ErrorCode = 404; // page does not exist
			Debug("Page", "PageError", $this->Error, __FILE__, __LINE__, DEBUG_APPL_ERR);
			$this->TemplateID = FALSE;
			$this->Data["Body"][] = "Error, this page does not exist!";
		}
		$Result->free();
		DebugCloseGroup(DEBUG_APPL_ERR);
	}

	/**
	 * Set the page template (not used any more)
	 */
	function SetTemplate($TemplateID = 0) {
		DebugGroup("Page", "SetTemplate", "Setting template using: ".$TemplateID, __FILE__, __LINE__, DEBUG_APPL_OK);
		$RV = FALSE;
		$Query = "SELECT IDString FROM Template WHERE TemplateID = ".(int)$TemplateID;
		if (!$this->Template2Use = $this->DB->getOne($Query)) {
			Debug("Page", "SetTemplate", "Template not found, using default", __FILE__, __LINE__, DEBUG_APPL_ERR);
			// template not found, using default nitro
			$Query = "SELECT TemplateID, IDString FROM Template WHERE IDString = ".NitroPrepareDB($this->Conf["Default"]["Template"]);
			if (!list($this->TemplateID, $this->Template2Use) = $this->DB->getRow($Query, 0, NITRODB_NUM)) {
				Debug("Page", "SetTemplate", "Default template also not found, using Nitro default", __FILE__, __LINE__, DEBUG_APPL_ERR);
				if (file_exists(NITRO_PATH."/Defaults/Templates/Default.tpl")) {
					$this->Template2Use = "file:".NITRO_PATH."Defaults/Templates/Default.tpl";
					$RV = TRUE;
				} else {
					Debug("Page", "SetTemplate", "Default Nitro template not found, dying....", __FILE__, __LINE__, DEBUG_APPL_ERR);
					echo "There were no templates found and the Nitro template directory cannot be found<BR><BR>Please correct these errors and try again.<BR>";
					exit;
				}
			} else {
				$this->Template2Use = "Nitro:".$this->Template2Use;
				$RV = TRUE;
			}
		} else {
			$this->Template2Use = "Nitro:".$this->Template2Use;
			$RV = TRUE;
		}
		Debug("Page", "SetTemplate", "Template set: ".$this->Template2Use, __FILE__, __LINE__, DEBUG_APPL_OK);
		DebugCloseGroup(DEBUG_APPL_OK);
		return $RV;
	}

	/**
	 * Load the objects of the template on to the page
	 *
	 * This function retreives, initializes and pre-processes all the objects associated
	 * with the template the page is using. These objects are added to the objects the 
	 * page itself already initialized.
	 *
	 * @access	private
	 */
	function SetTemplateObjects(){
		DebugGroup("Page", "SetTemplateObjects", "Setting template objects and preprocessing on page: ".$this->TemplateID, __FILE__, __LINE__, DEBUG_APPL_OK);
		if ($this->TemplateID) {
			$Query = "SELECT O.ObjectID, O.IDString AS ObjectIDString, O.DefaultSettings,
									OD.Version AS ObjectVersion, OD.Settings, OD.EditedOn AS ObjectLastEdited,
									TOb.Settings AS RuntimeSettings, TOb.TemplateVariable,
									M.File, M.Class
								FROM TemplateObjects AS TOb
								INNER JOIN Object AS O
									 ON O.ObjectID = TOb.ObjectID
								INNER JOIN Module AS M
									 ON M.ModuleID = O.ModuleID
								LEFT JOIN ObjectData AS OD
									 ON OD.ObjectID = O.ObjectID
									AND (OD.Language = ".NitroPrepareDB($this->Language)." OR OD.Language = '')
									AND OD.Published = 1
									AND (OD.VisibleFrom <= CURRENT_TIMESTAMP OR OD.VisibleFrom IS NULL)
									AND (OD.VisibleTill >= CURRENT_TIMESTAMP OR OD.VisibleTill IS NULL)
								WHERE TOb.TemplateID = $this->TemplateID
								ORDER BY TOb.TemplateVariable, O.ObjectID, OD.Language ASC, OD.Version ASC
								";
								//echo $Query;
			$Result = $this->DB->query($Query);
			$TObjects = Array();
			while($Row = $Result->fetchArray()) {
				$TObjects[$Row["TemplateVariable"]."_".$Row["ObjectID"]] = $Row;
			}
			foreach($TObjects AS $Row) {
				Debug("Page", "SetTemplateObjects", "Object set: ".$Row["Class"]." - ".$Row["TemplateVariable"], __FILE__, __LINE__, DEBUG_APPL_OK);
				// Set and check Settings correctly for the module
				$Row["DefaultSettings"]	= @unserialize($Row["DefaultSettings"]);
				$Row["Settings"]				= @unserialize($Row["Settings"]);
				$Row["RuntimeSettings"]	= @unserialize($Row["RuntimeSettings"]);
				
				if (!is_array($Row["DefaultSettings"])) $Row["DefaultSettings"] = Array();
				if (!is_array($Row["Settings"])) $Row["Settings"] = Array();
				if (!is_array($Row["RuntimeSettings"])) $Row["RuntimeSettings"] = Array();
				$Settings = array_merge($Row["DefaultSettings"], $Row["Settings"]);
				
				Debug("Page", "SetTemplateObjects", "Creating object and preprocessing: ".$Row["Class"], __FILE__, __LINE__, DEBUG_APPL_OK);
				if ($temp = $this->InitModule($Row["File"], $Row["Class"], $Row["ObjectID"], $Settings)) {
					// set version if != 1 and IDString of object
					if ($Row["ObjectVersion"] > 1)	$temp->ObjectVersion = $Row["ObjectVersion"];
					if ($Row["ObjectIDString"])			$temp->ObjectIDString = $Row["ObjectIDString"];
					if ($Row["ObjectLastEdited"])		$temp->ObjectLastEdited = $Row["ObjectLastEdited"];

					// PRE-PROCESSING
					$temp->PreProcess();

					Debug("Page", "SetTemplateObjects", "FillObject: ".$Row["Class"]." - ".$Row["TemplateVariable"], __FILE__, __LINE__, DEBUG_APPL_OK);
					// Add to objects array
					$this->Objects[] = Array("Class" => $Row["Class"],
															"File" => $Row["File"],
															"Settings" => $Row["Settings"],
															"RuntimeSettings" => $Row["RuntimeSettings"],
															"Variable" => $Row["TemplateVariable"],
															"ObjectID" => $Row["ObjectID"],
															"ObjectLastEdited" => $Row["ObjectLastEdited"],
															"Object" => $temp
														);
				} else {
					Debug("Page", "SetTemplateObjects", "Failed creating object: ".$Row["File"]." => ".$Row["Class"], __FILE__, __LINE__, DEBUG_APPL_ERR);
				}
				unset($temp);
			}
			$Result->free();
		}
		DebugCloseGroup(DEBUG_APPL_OK);
	}

	/**
	 * Draw the page
	 *
	 * This function processes all the objects and than calls their draw function. After
	 * this it calls the PostProcess function of the objects and draws
	 * this using the template to use.
	 *
	 * @see PostProcess()
	 * @access	public
	 * @return	string	Rendered page
	 */
	function Draw(){
		global $__NSess, $__NitroPageGlobals;
		
		DebugGroup("Page", "Draw", "Drawing page", __FILE__, __LINE__, DEBUG_APPL_OK);
		if ($this->TemplateID) { //$this->SetTemplate($this->TemplateID)) {
			Debug("Page", "Draw", "Template set successfully", __FILE__, __LINE__, DEBUG_APPL_OK);
			
			if ($this->CachingAllowed) {
				$CacheDir = NitroGetTmpCacheDir(Array('Objects/'));
				$PageCacheDir = NitroGetTmpCacheDir(Array('Pages/'));
				// It is possible to set global parameters in templates.
				// The page class will cache these, just in case they are needed
				// in another template or script....
				$globalsCacheFile = $PageCacheDir.$this->IDString.'_globals_L'.NitroGetUserLanguage().'_.ser';
				$__NitroPageGlobals = @unserialize(file_get_contents($globalsCacheFile));
				$pageCacheFile = $PageCacheDir.$this->IDString.'_L'.NitroGetUserLanguage().'_V'.$this->Version.'_LE'.$this->EditedOn.'_.html';
			} else {
				$this->PageCachingAllowed = FALSE; // set page cache to false if caching is not allowed
			}

			if (
				$this->PageCachingAllowed
				&&
				NitroGetConfig('Settings/PageCache')
				&&
				file_exists($pageCacheFile)
				&&
				@filemtime($pageCacheFile) > (time() - (60*NitroGetConfig('Settings/PageCache')))
				&&
				!(array_key_exists('ByPassCache', $_GET) && $_GET['ByPassCache'])
			) {
				Debug("Page", "Draw", "Page cache found, using ...", __FILE__, __LINE__, DEBUG_APPL_OK);
				$RV = file_get_contents($pageCacheFile);
			} else {
				Debug("Page", "Draw", "Load page data", __FILE__, __LINE__, DEBUG_APPL_OK);
				if (!strlen($this->Error)) $this->LoadPageData();
				
				// CREATING AND PRE-PROCESSING
				DebugGroup("Page", "Draw", "Creating and Pre-Processing objects", __FILE__, __LINE__, DEBUG_APPL_OK);
				for($i = 0; $i < count($this->Objects); $i++) {
					Debug("Page", "Draw", "Creating and Pre-Processing ".$this->Objects[$i]["Class"]." - ".$this->Objects[$i]["Variable"], __FILE__, __LINE__, DEBUG_APPL_OK);

					Debug("Page", "InitPageFromString", "Setting object settings", __FILE__, __LINE__, DEBUG_APPL_OK);
					// Set and check Settings correctly for the module
					$this->Objects[$i]["DefaultSettings"] = unserialize($this->Objects[$i]["DefaultSettings"]);
					$this->Objects[$i]["Settings"] = unserialize($this->Objects[$i]["Settings"]);
					$this->Objects[$i]["RuntimeSettings"] = unserialize($this->Objects[$i]["RuntimeSettings"]);
					if (!is_array($this->Objects[$i]["DefaultSettings"])) $this->Objects[$i]["DefaultSettings"] = Array();
					if (!is_array($this->Objects[$i]["Settings"])) $this->Objects[$i]["Settings"] = Array();
					if (!is_array($this->Objects[$i]["RuntimeSettings"])) $this->Objects[$i]["RuntimeSettings"] = Array(); 
					$Settings = array_merge($this->Objects[$i]["DefaultSettings"], $this->Objects[$i]["Settings"]);
					
					if ($this->Objects[$i]["Object"] = $this->InitModule($this->Objects[$i]["File"], $this->Objects[$i]["Class"], $this->Objects[$i]["ObjectID"], $Settings)) {
						// set version if != 1 and IDString of object
						if ($this->Objects[$i]["ObjectVersion"] > 1) $this->Objects[$i]["Object"]->ObjectVersion = $this->Objects[$i]["ObjectVersion"];
						if ($this->Objects[$i]["ObjectIDString"]) $this->Objects[$i]["Object"]->ObjectIDString = $this->Objects[$i]["ObjectIDString"];
						if ($this->Objects[$i]["ObjectLastEdited"]) $this->Objects[$i]["Object"]->ObjectLastEdited = $this->Objects[$i]["ObjectLastEdited"];
	
						// PRE-PROCESSING
						$this->Objects[$i]["Object"]->PreProcess();
					}
				}
				$this->SetTemplateObjects($this->TemplateID);

				Debug("Page", "Draw", "Initialize Template engine", __FILE__, __LINE__, DEBUG_APPL_OK);
				$Page = new NitroTemplate($this->TemplateID);
				if (array_key_exists('ByPassCache', $_GET) && $_GET['ByPassCache']) $Page->Caching(FALSE);
				$Page->Assign('Title', $this->Title);
				$Page->Assign('UserID', $__NSess->UserID);
				$Page->Assign('Language', $__NSess->Language);
	
				// PROCESSING
				DebugGroup("Page", "Draw", "Processing objects", __FILE__, __LINE__, DEBUG_APPL_OK);
				for($i = 0; $i < count($this->Objects); $i++) {
					Debug("Page", "Draw", "Processing ".$this->Objects[$i]["Class"]." - ".$this->Objects[$i]["Variable"], __FILE__, __LINE__, DEBUG_APPL_OK);
					if (is_object($this->Objects[$i]["Object"])) {
						$this->Objects[$i]["Object"]->Process();
					}
				}
				DebugCloseGroup(DEBUG_APPL_OK);
				
				// DRAWING
				foreach($this->Objects AS $ID => $Row) {
					DebugGroup("Page", "Draw", "Drawing Object ".$Row["ObjectID"].", ".$Row["Class"]." - ".$Row["Variable"], __FILE__, __LINE__, DEBUG_APPL_OK);
					Debug("Page", "Draw", "Calling Draw function of object ".$Row["Class"]." - ".$Row["Variable"], __FILE__, __LINE__, DEBUG_APPL_OK);
					// if caching is on AND object is cachable AND cache exists AND cache is not older than 1 day
					if (
						$this->CachingAllowed
						&&
						NitroGetConfig('Settings/ObjectCache')
						&&
						($cacheID = $Row["Object"]->isCacheable($Row["RuntimeSettings"]))
						&&
						@file_exists($cacheFile = $CacheDir.$cacheID)
						&&
						@filemtime($cacheFile) > (time() - (60*NitroGetConfig('Settings/ObjectCache')))
						&&
						!(array_key_exists('ByPassCache', $_GET) && $_GET['ByPassCache'])
					) {
						Debug("Page", "Draw", "Using Cache ($cacheID)", __FILE__, __LINE__, DEBUG_APPL_OK);
						$drawRV = file_get_contents($cacheFile);
					} else {
						$drawRV = $Row["Object"]->Draw($Row["RuntimeSettings"]);
						if (isset($cacheID) && $cacheID) {
							//write Cache
							if ($fp = @fopen($cacheFile, 'w')) {
								fwrite($fp, $drawRV);
								fclose($fp);
							}
						} else {
							$this->noPageCache = TRUE; // set the noPageCache flag, at least 1 object on page cannot be cached
						}
					}
					$this->Data[$Row["Variable"]][] = $drawRV;
					Debug("Page", "Draw", "Object Drawn", __FILE__, __LINE__, DEBUG_APPL_OK);
					DebugCloseGroup(DEBUG_APPL_OK);
				}
				
				if ($this->CachingAllowed) { // always overwrite this file!!
					if ($fp = @fopen($globalsCacheFile, 'w')) {
						fwrite($fp, serialize($__NitroPageGlobals));
						fclose($fp);
					}
				}
				DebugCloseGroup(DEBUG_APPL_OK);
				
				// POST PROCESSING
				DebugGroup("Page", "Draw", "Post-Processing objects", __FILE__, __LINE__, DEBUG_APPL_OK);
				for($i = 0; $i < count($this->Objects); $i++) {
					Debug("Page", "Draw", "Post-Processing ".$this->Objects[$i]["Class"]." - ".$this->Objects[$i]["Variable"], __FILE__, __LINE__, DEBUG_APPL_OK);
					if (is_object($this->Objects[$i]["Object"])) {
						$this->Objects[$i]["Object"]->PostProcess();
					}
				}
				DebugCloseGroup(DEBUG_APPL_OK);
	
				foreach($this->Data AS $ID => $Values) {
					$TempValue = "";
					foreach($Values AS $Value) {
						// Pluggable callback functions
						// Should this be here?
						if (count($this->DrawObjectCallBackFunctions) > 0) {
							foreach($this->DrawObjectCallBackFunctions AS $callBackFunction) {
								//$TempValue.= $callBackFunction($Value);
							}
						} else {
							$TempValue.= $Value;
						}
					}

					// Pluggable callback functions
					// Should this be here?
					if (count($this->DrawTemplateVariableCallBackFunctions) > 0) {
						foreach($this->DrawTemplateVariableCallBackFunctions AS $callBackFunction) {
							//$callBackFunction($TempValue);
						}
					}
					$Page->assign($ID, $TempValue);
				}
				Debug("Page", "Draw", "Drawing page using template engine", __FILE__, __LINE__, DEBUG_APPL_OK);
				$RV = $Page->fetch($this->DefaultTemplate);

				// Pluggable callback functions
				// Should this be here?
				if (count($this->DrawCallBackFunctions) > 0) {
					foreach($this->DrawCallBackFunctions AS $callBackFunction) {
						//$callBackFunction($RV);
					}
				}

				Debug("Page", "Draw", "Drawing page DONE", __FILE__, __LINE__, DEBUG_APPL_OK);
	
				if (!$this->noPageCache && $this->PageCachingAllowed) {
					Debug("Page", "Draw", "Creating cache of page", __FILE__, __LINE__, DEBUG_APPL_OK);
					if ($fp = @fopen($pageCacheFile, 'w')) {
						fwrite($fp, $RV);
						fclose($fp);
					}
				}
			}
		} else {
			Debug("Page", "Draw", "Failed setting Template, show Error", __FILE__, __LINE__, DEBUG_APPL_ERR);
			if (file_exists(TEMPLATE_PATH.'Error.tpl')) {
				$ErrorTemplate = TEMPLATE_PATH.'Error.tpl';
			} else {
				$ErrorTemplate = NITRO_PATH.$this->DefaultErrorTemplate;
			}
			$Page = new NitroTemplate();
			$Page->Assign('Body', $this->Error);
			$RV = $Page->fetch($ErrorTemplate);
		}
		DebugCloseGroup(DEBUG_APPL_OK);
		return $RV;
	}

	function InitModule($ModuleFile, $ModuleClass, $ObjectID, $Settings) {
		DebugGroup(__CLASS__, __FUNCTION__, "Creating instance of module $ModuleFile => $ModuleClass", __FILE__, __LINE__, DEBUG_APPL_OK);
		if (include_once $ModuleFile) {
			if (class_exists($ModuleClass)) {
				if (@eval('return $Module = new '.$ModuleClass.'($ObjectID, $Settings);')) {
					if (!is_object($Module)) {
						Debug(__CLASS__, __FUNCTION__, "Inintialized object is not an object", __FILE__, __LINE__, DEBUG_APPL_ERR);
						$Module = FALSE;
					}
				} else {
					Debug(__CLASS__, __FUNCTION__, "Failed creating new instance of module", __FILE__, __LINE__, DEBUG_APPL_ERR);
					$Module = FALSE;
				}
			} else {
				Debug(__CLASS__, __FUNCTION__, "Class $ModuleClass does not exist", __FILE__, __LINE__, DEBUG_APPL_ERR);
				$Module = FALSE;
			}
		} else {
			Debug(__CLASS__, __FUNCTION__, "Failed to include file $ModuleFile", __FILE__, __LINE__, DEBUG_APPL_ERR);
			$Module = FALSE;
		}

		DebugCloseGroup(DEBUG_APPL_OK);
		return $Module;
	}
}
?>
Return current item: OpenNitro