<?php
/*
* weathermap5rrd - dlib.php
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Copyright (C)
* 2007 Zdenek Styblik <hide@address.com>
*
* The Initial Developer of the Original Code is
* 2007 Zdenek Styblik <hide@address.com>
*
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* Authors:
* Zdenek Styblik ~ 2007-2008
*
*/
class defaultLib {
// name of the script; usually index.php
public $scriptname;
// *pure* xml
public $xmloutput;
// xsl
public $template;
// xsl files to import
public $import;
// config file path
public $configfile;
// main page layout
private $layout;
private $moduleClass;
private $auto_index = 0;
// debug option
// $debug = 0; no debug output;
// $debug = 1; append debug output;
private $debug = 0;
// ready-up
function __construct()
{
# we *have to* start xml with root tag
$this->xmloutput = "<root>\n";
# default page layout
$this->layout = "layout.xsl"; // NULL==XML out only;
# we'll use this later
$this->import = new StdClass;
if (!defined('PATH'))
define('PATH', "");
if (!defined('MODULE'))
define('MODULE', 'index');
}
// clean-up
function __destruct()
{
$this->xmloutput;
$this->import;
$this->layout;
$this->template;
}
// go
public function start()
{
session_start();
$_SESSION["DEBUG"] = new StdClass;
$_SESSION["DEBUG"]->debug = new StdClass;
$_SESSION["DEBUG"]->debug->entry = array();
$this->scriptname = $this->getScriptname();
try
{
$module = MODULE;
# if $_GET['module'] is empty, then load "default" module.
if (isset($_GET['module']) && !empty($_GET['module']) && !is_null($_GET['module']))
{
$module = $this->stripMe($_GET['module']);
}
# if $module.php exists, then proceed, else set default module
if (!file_exists("{$this->getPath()}modules/{$module}.php"))
{
$module = MODULE;
}
// not even default module is readable?
if (!file_exists("{$this->getPath()}modules/{$module}.php"))
throw new Exception("Unable to open default module '{$this->getPath()}modules/{$module}.php'. Terminated.");
$this->setContent($module);
if (property_exists($this->moduleClass, 'settings'))
$this->handleSettings($this->moduleClass->settings);
// append debug info
if ($this->debug == 1 && isset($_SESSION["DEBUG"]->debug->entry) && is_array($_SESSION["DEBUG"]->debug->entry))
$this->xmloutput.= $this->objToXML($_SESSION["DEBUG"]);
if (!is_null($this->layout))
{
if (file_exists("{$this->getPath()}xsl/layout/{$this->layout}"))
{
$this->template = $this->xslPrepare($this->import, "root", "{$this->getPath()}xsl/layout/{$this->layout}");
header("Content-type: text/html; charset=utf-8");
# enclose XML <root> tag, el Stupido
$this->xmloutput.= "</root>\n";
print $this->transform($this->xmloutput, $this->template);
}
else
{
# Layout is nowhere to find. What do you want to do? Sit in the corner and cry! :)
throw new Exception("Layout({$this->getPath()}xsl/layout/{$this->layout}) not found.\n This means critical error.");
}
}
else
{
$this->xmloutput.= "</root>\n";
header("Content-type: text/xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo $this->xmloutput;
}
}
catch (Exception $e)
{
echo "Caught exception: ", $e->getMessage(), ";\n";
}
}
# set content
# @return: void;
private function setContent($module) {
require_once("{$this->getPath()}modules/{$module}.php");
$this->moduleClass = new $module;
$this->moduleClass->init();
}
# handle settings from module
# @return: void;
private function handleSettings($input = null) {
if (!is_null($input) && is_object($input)) {
if (property_exists($input, "layout")) {
$this->layout = $input->layout;
}
if (property_exists($input, "import")) {
$this->import = $input->import;
}
if (property_exists($input, "xmlobject")) {
$this->xmloutput.= $this->objToXML($input->xmlobject);
}
} else {
# dummy
}
}
### Xml / Xsl ###
# desc: prepares main page xsl layout with css/imports/etc.
# $import:
# $template:
# $file:
# @return: string(well formed xsl);
public function xslPrepare($import = null, $templatename = null, $file) {
if (is_null($templatename))
die("xslPrepare: template name can't be NULL!");
$xsl = "";
if (!is_null($import) && !is_null($templatename))
{
$xsl = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n";
foreach ($import as $k=>$v)
{
if (file_exists($v))
{
$xsl.= "<xsl:import href=\"{$v}\" />\n";
}
}
$xsl.= "<xsl:output method=\"xml\" encoding=\"utf-8\" indent=\"yes\" doctype-public=\"-//W3C//DTD XHTML 1.0 Transitional//EN\" doctype-system=\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"/>";
$xsl.= "<xsl:template match=\"{$templatename}\" name=\"{$templatename}\">\n";
if (file_exists($file))
{
$xsl.= file_get_contents($file);
}
$xsl.= "</xsl:template>\n</xsl:stylesheet>";
}
else
{
$xsl = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n";
$xsl.= "<xsl:output method=\"xml\" encoding=\"utf-8\" indent=\"yes\" doctype-public=\"-//W3C//DTD XHTML 1.0 Transitional//EN\" doctype-system=\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"/>";
$xsl.= "<xsl:template match=\"{$templatename}\" name=\"{$templatename}\">\n";
if (file_exists($file))
{
$xsl.= file_get_contents($file);
}
$xsl.= "</xsl:template>\n</xsl:stylesheet>";
}
return $xsl;
}
# desc: converts php object into xml tree
# $obj: std Class;
# @return: string;
public function objToXML($obj, $parent = null, $deep = 1)
{
$xmloutput = "";
if (is_object($obj) || is_array($obj))
{
foreach ($obj as $k=>$v)
{
if (is_object($v))
{
if (is_numeric($k))
{
$xmloutput.= self::objToXML($v, $parent, $deep+1);
}
else
{
$xmloutput.= str_repeat("\t", $deep);
$xmloutput.="<{$k}>\n";
$xmloutput.= self::objToXML($v, $k, $deep+1);
$xmloutput.= str_repeat("\t", $deep);
$xmloutput.="</{$k}>\n";
}
}
elseif (is_array($v))
{
foreach($v as $c=>$d)
{
$xmloutput.= str_repeat("\t", $deep);
$xmloutput.="<{$k}>\n";
$xmloutput.= self::objToXML($d, $parent, $deep+1);
$xmloutput.= str_repeat("\t", $deep);
$xmloutput.="</{$k}>\n";
}
}
else
{
for ($a =0; $a < $deep; $a++)
{
$xmloutput.= "\t";
}
$xmloutput.= "<{$k}>{$v}</{$k}>\n";
}
}
}
return $xmloutput;
}
# $xml: string; !!!well formated xml!!!
# $xsltemplate: string; !!!well formated xsl!!!
# @return: string(xhtml);
public function transform($xml, $xsltemplate)
{
### MANUAL DEBUG ###
#echo $this->xmloutput; exit();
#echo $this->template; exit();
# load XSL file
$xsl = new DomDocument();
$xsl->loadXML($xsltemplate);
# load XML file
$inputdom = new DomDocument();
$inputdom->loadXML($xml);
# load XsltProcessor and fed it with XSL file
$proc = new XsltProcessor();
$xsl = $proc->importStylesheet($xsl);
# transform XML + XSL --> XHTML and do output
$newdom = $proc->transformToDoc($inputdom);
$newdom->formatOutput = true;
$retval = $newdom->saveXML();
$retval = preg_replace('/ xmlns=""/', '', $retval);
return $retval;
}
public function replacer($input, $tag) {
$replaced = $input;
if ( is_array($tag) ) {
foreach ($tag as $key=>$value) {
$taglen = strlen($key);
$fullTag = "{" . $key . "}";
$replacement = $value;
$replaced = preg_replace("/$fullTag/", "{$value}", $replaced);
}
}
return $replaced;
}
### ENDo Xsl ###
### Common ###
# add slashes with \n -> <br /> conv.
# var: string
public function stripMe($string, $forcequote = 0, $replacebr = 1)
{
if(!isset($string))
{
$string="";
}
$string = trim($string);
if($forcequote == 1)
{
$string = addslashes($string);
}
elseif (!get_magic_quotes_gpc() && $forcequote == 0)
{
$string=addslashes($string);
}
if ($replacebr == 1)
{
$string=ereg_replace("\n","<br />", $string);
}
return $string;
}
# desc: returns path to the files
# @return: string
protected function getPath()
{
return PATH;
}
# desc: returns debug state
# @return: bit;
private function getDebugState()
{
return $this->debug;
}
public function getHostname()
{
if (isset($_SERVER['HTTP_HOST']))
{
return $_SERVER['HTTP_HOST'];
}
else
{
return HTTP_HOST;
}
}
# return the name of this script
# var: none
# todo: http/https
# @return: string;
public function getScriptname()
{
if (isset($_SERVER['HTTP_HOST']))
{
$host = "http://" . $_SERVER["HTTP_HOST"];
}
else
{
$host = HTTP_HOST;
}
$returnMe = $host . $_SERVER['PHP_SELF'];
return $returnMe;
}
public function getDebugMsg() {
return $_SESSION["DEBUG"];
}
### ENDo Common ###
} // class defaultLib
# $msg: string;
# @return: void;
function setDebugMsg($msg = NULL)
{
if (!empty($msg) && !is_null($msg))
{
$newentry = new StdClass;
$newentry->text = $msg;
$_SESSION["DEBUG"]->debug->entry[] = $newentry;
unset($newentry);
}
}
?>