<?
/**
* Application Class for weblication
* @author Andreas Altendorfer <hide@address.com>
* @date 2003-12-06
*/
global $app;
class Application
{
var $prefix; // used for filenames, tagnames, ....
// eg "myApp" -> myApp.xml, myApp_menu.xml, ....
var $title; // Application Title
var $version; // The XML-File read is for version x of this app
var $build; // The Build-Number to identify unique builds
var $description; // What's this Application about
var $authors; // array of authors defined in XML-File
var $copyright; // Copyright of the Application (from XML-File)
var $htmltemplate; // HTML-Template
var $contentmenu; // Used as [APP:CONTENTMENU]
// vars calculated from prefix
var $path; // fq path of all runtimefiles
var $xml_filename; // fq name of this application's xml-definition
var $xml; // the XML-Object defining this application
//////////////////////////////////////////////////////////////////////
// INTERFACE
//////////////////////////////////////////////////////////////////////
// Constructor
function Application( $iPrefix ) {
global $app,$aid;
if ( !isset($aid) ) $aid = "welcome";
$this->prefix = $iPrefix;
$app = &$this;
}
function getPath() {
return( $this->path );
}
// Main run
function run() {
$rc = "";
if ( $this->init($rc) ) // Will do headers and cookies, load files
{
if ( $this->parse($rc) ) // parse files and create objects
{
if ( $this->execute($rc) ) // execute the loaded objects structure
{
}
else {
$rc .= Fatal( "Application: execute failed" );
}
}
else {
$rc .= Fatal( "Application: parse failed" );
}
}
else {
$rc .= Fatal( "Application: init failed" );
}
echo str_replace( "{", "<",
str_replace( "}", ">", $rc ));
$this->xml->free();
} // -- function run
//////////////////////////////////////////////////////////////////////
// PRIVATE
//////////////////////////////////////////////////////////////////////
function init( &$rc ) {
global $cfg;
Header( "Content-Type: text/html; charset=utf-8" );
Header( "pragma: no-cache" );
Header( "pragma: no-proxy" );
Header( "Expires: 10");
$ok = false;
if ( $this->prefix != "" ) {
// Setup filenames
$this->path = $cfg["application"] . "/" . $this->prefix . "/";
$this->xml_filename = $this->path . $this->prefix . ".xml";
// load files
$this->xml = new xml( $this->xml_filename );
if ( ! $y = $this->xml->load() ) {
Append( $rc, Error( "Application: xmlfile load failed " . $this->xml_filename ));
}
$ok = true;
Append( $rc, Remark( "APPLICATION OBJECT INITIALIZED AS *". $this->prefix . "*\n" .
" Path=" . $this->path . "\n" .
" XML =" . $this->xml_filename . "\n" )
);
}
else {
Append( $rc, "SYNTAXERROR: class Application.prefix is empty" );
}
return( $ok );
}
function parse( &$rc ) {
Append( $rc, Remark( "APPLICATION " .$this->prefix . " PARSE ..." ));
// Set Members to values from XML-File
// Attributes of class weblication
$this->version = $this->xml->element->getAttr("version");
$this->build = $this->xml->element->getAttr("build");
$this->title = $this->xml->element->findElement("title");
// Get Authors-Block from XML
$authors = array();
$n = $this->xml->element->findElements($authors, "authors");
if ( $n == 1 ) {
$this->authors = array();
$na = $authors[0]->findElements( $this->authors, "author" );
} else
{
Fatal( $n . " AUTHORS BLOCK(S) in " . $this->xml_filename );
}
// Get Copyright and description from XML
$this->copyright = $this->xml->element->findElement("copyright");
$this->description= $this->xml->element->findElement("description");
$dummy = $this->xml->element->findElement("htmltemplate");
if ( ! $dummy ) $this->htmltemplate = "index.html";
else $this->htmltemplate = $dummy->data;
return( true );
}
function debug( $msg="" ) {
$rc .= "<pre>Execute Application:
prefix : ". $this->prefix . "
version : ". $this->version . "
build : ". $this->build . "
copyright : ". $this->copyright->data . "
description : ". $this->description->data . "
html-template : ". $this->htmltemplate . "
Authors : ";
while ( list( $k, $ae ) = each( $this->authors ) ) {
$rc .= trim($ae->data) . ", email=" .
$ae->getAttr("email") . "\n ";
}
$rc .= "</pre>";
return( $rc );
}
function buildin( $cmd ) {
switch( $cmd ) {
case "DATE": $rc .= date("m/d/Y"); break;
case "YYYYMMDD": $rc = date("Ymd"); break;
case "YYYY-MM-DD": $rc = date("Y-m-d"); break;
case "TIME":
case "HH:MM": $rc = date("H:i"); break;
case "D.M.Y":
case "DD.MM.YYYY": $rc = date("d.m.Y"); break;
case "D. M Y" : $rc = date("d. M Y");break;
case "YYYY" : $rc = date("Y"); break;
case "YY" : $rc = date("y"); break;
case "MM" : $rc = date("m"); break;
case "DD" : $rc = date("d"); break;
default: $rc = $cmd; break;
}
return( $rc );
}
function doCommand( $parameter ) {
switch( $parameter ) {
case "TITLE" : $rc = $this->title->data; break;
case "HH:MM":
case "TIME": $rc = date("H:i"); break;
case "DATE": $rc = date("m/d/Y"); break;
case "MAIN" : $rc = $this->main(); break;
case "CONTENTMENU":
$rc = $this->contentMenu();
break;
case "@": $rc = translate( $parameter ); break;
case "DOWNLOADLINK":
$rc = "doCommand: ".$this->downloadLink( $parameter ); break;
case "RIMAGE":
case "LIMAGE":
case "IMAGE": $rc .= $this->incImage( $parameter); break;
default: $rc = $parameter; break;
}
return( $rc );
}
function doMenu( $parameter ) {
global $cfg;
$m = new Menu( $cfg["menu"] ."/" . $parameter );
return( $m->display() );
}
function incImage($parameter,$objectname="") {
switch( $objectname ) {
case "LIMAGE": $ext = " align=left "; break;
case "RIMAGE": $ext = " align=right"; break;
default: $ext = ""; break;
}
$rc = "<img src='content/articles/img/".
$parameter."' border=0 hspace=5 vspace=5 ".$ext.">";
return($rc);
}
function runCommand( $cmd ) {
$cmdout = "";
$cmd_name_start = strpos( $cmd, ":" );
if( !$cmd_name_start ) {
return( $this->buildin( $cmd ));
}
$objectname = substr( $cmd, 0, $cmd_name_start );
$parameter = substr( $cmd, $cmd_name_start+1 );
switch( $objectname ) {
case "APP" : $cmdout .= $this->doCommand( $parameter ); break;
case "MENU": $cmdout .= $this->doMenu( $parameter ); break;
case "MODULE": $cmdout.=$this->runModule( $parameter); break;
case "@": $cmdout .= translate( $parameter ); break;
case "DOWNLOADLINK":
$cmdout .= "runCommand: ".$this->downloadLink( $parameter ); break;
case "CONTENTMENU":
$cmdout .= $this->contentMenu();
break;
case "RIMAGE":
case "LIMAGE":
case "IMAGE": $cmdout .= $this->incImage( $parameter); break;
case "HH:MM":
case "TIME": $cmdout .= date("H:i"); break;
case "DATE": $cmdout .= date("m/d/Y"); break;
default: $cmdout .= $cmd; break;
}
return( $cmdout );
}
function fillTemplate( $sIn ) {
$sOut = "";
$right= $sIn;
$cnt = 0;
while ( ($cnt++ < 16 ) && ( $s1 = strpos( $right, "{" )) )
{
$sOut .= substr( $right,0,$s1 );
$r1 = substr( $right,$s1+1 );
$s2 = strpos( $r1, "}" );
if ( ! $s2 ) {
$sOut .= $r1;
$right = "";
break;
}
$cmd = substr( $r1, 0, $s2 );
$sOut .= $this->runCommand( $cmd );
$right = substr( $r1, $s2+1 );
}
$sOut .= $right;
return( $sOut );
}
function execute( &$rc ) {
Append( $rc, Remark( "APPLICATION " .$this->prefix . " EXECUTE ..." ));
Append( $rc, Remark( $this->debug()));
// Load Template
$fname = $this->path . "/" . $this->htmltemplate;
$f = fopen( $fname, "r" );
if ( !$f ) {
Append( $rc, Error( "APPLICATION " .$this->prefix . "EXECUTE ... ".
"Could not load html-template " . $fname ));
return( false );
}
$rct = "";
while( $l = fgets( $f, 4096 ) ) {
$rct .= $l;
}
$rc1 = $this->fillTemplate( $rct );
$cnt=1;
while( $cnt > 0 ) {
$rc1 = $this->replaceActions( $cnt, substr($rc1,0) );
};
Append( $rc, $rc1);
fclose( $f );
return( true );
}
function contentMenu() {
global $aid,$cfg;
$x = new xml( $cfg["articles"] . "/" . $aid . ".xml");
if ( $x->load() ) {
$a = $x->element->findElement("contentmenu");
if ( $a ) {
$myaid = $a->getAttr("aid");
$fn = $cfg["menu"] . "/" . $myaid . ".xml";
} else {
$fn = $cfg["menu"] . "/" . $aid . ".xml";
}
}
else {
$fn = $cfg["menu"] . "/" . $aid . ".xml";
}
if ( file_exists( $fn ) ) {
$m = new Menu( $fn );
return( $m->display() );
}
return( "" );
}
function downloadLink( $link ) {
global $cfg;
return( $cfg["download-prefix"] . $link .
$cfg["download-middle"] . $link .
$cfg["download-suffix"] );
}
function linkToArticle( &$article ) {
global $cfg;
$t = ($article->title != "") ? $article->title : $article->aid;
return( $cfg["link-prefix"] . $article->aid .
$cfg["link-middle"] . $t .
$cfg["link-suffix"] );
}
function doAction( &$cnt, $cmd ) {
global $cfg;
$cmdout = "";
$cmd_name_start = strpos( $cmd, ":" );
if( !$cmd_name_start ) {
return( $this->buildin( "<font size=-2>[" . $cmd ."]</font>" ));
}
$objectname = substr( $cmd, 0, $cmd_name_start );
$parameter = substr( $cmd, $cmd_name_start+1 );
$cnt++;
switch( $objectname )
{
case "EXTURL" : $cmdout .= $this->exturl( $parameter );
break;
case "LOCALURL":$cmdout .= $this->localurl( $parameter );
break;
case "HH:MM": $cmdout .= date("H:i"); break;
case "TIME": $cmdout .= date("H:i"); break;
case "DATE": $cmdout .= date("m/d/Y"); break;
case "LINKTOMODULE": $cmdout .= $this->linktomodule( $parameter );
break;
case "EMAIL": $cmdout .= "<a href=mailto:".$parameter.">".$parameter."</a>";
break;
case "MODULE":$cmdout .= $this->runModule( $parameter );
break;
case "RIMAGE":
case "LIMAGE":
case "IMAGE": $cmdout .= $this->incImage( $parameter,$objectname); break;
case "ARTICLE":
case "LINK":
$axml = new xml( $cfg["articles"] . "/" . $parameter . ".xml");
if ( $axml->load() ) {
$a = new Article( $axml->element );
if ( $objectname == "ARTICLE" ) {
$cmdout .= $a->display();
}
else {
$cmdout .= $this->linkToArticle( $a );
}
}
break;
case "CONTENTMENU":
$cmdout .= $this->contentMenu();
break;
case "DOWNLOADLINK":
$cmdout .= $this->downloadLink( $parameter ); break;
case "@": $cmdout .= translate( $parameter ); break;
default: $cmdout .= "[" . $cmd ."]"; $cnt--; break;
}
return( $cmdout );
}
function exturl( $parameter )
{
return($this->localurl($parameter,"target=_new"));
}
function localurl( $parameter, $ext="" )
{
$a = split(",",$parameter,2);
$url = $a[0];
$title = ( $a[1] == "" ) ? $url : $a[1];
return( "<a href=\"". $url . "\" ".$ext.">".
$title . "</a>" );
}
function linktomodule( $parameter ) {
return( "<a href=\"". $PHP_SELF."?type=module&aid=".$parameter."\">".
$parameter ."</a>" );
}
function replaceActions( &$cnt, $sIn ) {
$sOut = "";
$right= $sIn;
$cnt = 0;
while ( ( $s1 = strpos( $right, "[" )) )
{
$sOut .= substr( $right,0,$s1 );
$r1 = substr( $right,$s1+1 );
$s2 = strpos( $r1, "]" );
if ( ! $s2 ) {
$sOut .= $r1;
$right = "";
break;
}
$cmd = substr( $r1, 0, $s2 );
$sOut .= $this->doAction( $cnt, $cmd );
$right = substr( $r1, $s2+1 );
}
$sOut .= $right;
if ( $sOut == "" ) return( $sIn );
return( $sOut );
}
function runModule( $modname ) {
$module = new Module( $modname );
return( $module->execute() );
}
function main() {
global $mid, $id, $type, $aid;
switch( $type ) {
case "module": $rc .= $this->runModule($aid); break;
case "document":
case "article":$rc .= $this->runModule("article"); break;
case "none" :
default:
$aid = "welcome";
$rc .= $this->runModule("article");
break;
}
return($rc);
}
} //- class Application
?>