Location: PHPKode > projects > Webgenerator-X content management system > wgx_rc1.5/WG-X/wgx_xml.php
<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X 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 2 of the License, or
* (at your option) any later version.
*
* Webgenerator-X 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 Foobar; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*************************************************************************/
/*
REGNI Giorgio
Webgenerator-X
08/31/2001

 This function will evaluate any wgx xml code
 ( code between <wgx>...</wgx> )
 uses PHP sax xml parser
*/
function eval_wgx_xml( $xml_code )
{
	global $xml_depth,$xml_generated_html,$wgx_in_module,$tag_stack;
	
	/*	Explainations on global varibales
	$wgx_in_module is set to true in the engine is inside a module tag
	if in a module tag, $wgx_module_attrs are the attributes of the module tag
	then the parameters tag are read $wgx_module_params contain the parameters
	*/
	
	$xml_depth = 0;
	$xml_generated_html = '';
	$wgx_in_module = false;
	$tag_stack = array();
	
	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "wgx_startElement", "wgx_endElement");
	xml_set_character_data_handler($xml_parser, "wgx_characterData");
	xml_set_processing_instruction_handler($xml_parser, "wgx_PIHandler");

	if (!xml_parse($xml_parser, $xml_code, true)) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }

	xml_parser_free($xml_parser);
	
	return $xml_generated_html;
}

/* exec a wgx module according to $attrs
 attrs is an array, the meanings are:
name: name of module
function! data_code to load
 params is an array with all tags inside the module
*/
function wgx_call_module( $attrs,$params )
{
	global $TE,$CACHEENGINE;
	
	$mod = $attrs[NAME];
	$function = $attrs['FUNCTION'];

	eval( "\$tempmod = new $mod"."module();");
	$TE->set_module_template_root($mod);
	
	if (is_array( $params))
		$tempmod->set_param(true,$params);
	else
		$tempmod->set_param(false,'');
	
	$CacheParam= $tempmod->GetCacheParam( $function );
	$tags = $tempmod->GetCacheTags( $function );
	
	switch ($CacheParam) {
		case -1:	// never cache
		$generated=$tempmod->Generate_Output($function);
		break;
		case -2:    // store in cache now
		$generated=$tempmod->Generate_Output($function);
		$CACHEENGINE->Store_Data($mod,$function,$tags,$generated);
		break;
		// 0 means always from cache, 1..xxxx nb seconds to cache
		default:
		if ($CACHEENGINE->Is_UptoDate($mod,$function,$tags,$CacheParam)) {
			$cacheout=$CACHEENGINE->Get_Data($mod,$function,$tags);
			$generated=$cacheout['data'];
			$cacheok=$cacheout['ok'];
		}
		else 
		{
			$generated=$tempmod->Generate_Output($function);
			$CACHEENGINE->Store_Data($mod,$function,$tags,$generated);
		}
		break;
	}
	
	$TE->clear_all();
	return $generated;
}

/* exec a wgx smart module according to $attrs
 attrs is an array, the meanings are:
name: name of module
function data_code to load
 params is an array with all tags inside the module
 there must be the path to the template as a template tag in params that is the template used for this module call
 Those modules use smarty as template engine
*/
function wgx_call_smartmodule( $attrs,$params )
{
  global $CACHEENGINE;

  $mod = $attrs[NAME];
  $function = $attrs['FUNCTION'];

  eval( "\$tempmod = new $mod"."module(new Smarty());");
  
  if (is_array( $params))
    $tempmod->set_param(true,$params);
  else
    $tempmod->set_param(false,'');
  
  	$CacheParam= $tempmod->GetCacheParam( $function );
	$tags = $tempmod->GetCacheTags( $function );
	
	switch ($CacheParam) {
		case -1:	// never cache
		$generated=$tempmod->Generate_Output($function);
		break;
		case -2:    // store in cache now
		$generated=$tempmod->Generate_Output($function);
		$CACHEENGINE->Store_Data($mod,$function,$tags,$generated);
		break;
		// 0 means always from cache, 1..xxxx nb seconds to cache
		default:
		if ($CACHEENGINE->Is_UptoDate($mod,$function,$tags,$CacheParam)) {
			$cacheout=$CACHEENGINE->Get_Data($mod,$function,$tags);
			$generated=$cacheout['data'];
			$cacheok=$cacheout['ok'];
		}
		else 
		{
			$generated=$tempmod->Generate_Output($function);
			$CACHEENGINE->Store_Data($mod,$function,$tags,$generated);
		}
		break;
	}

  return $generated;
}

/*
translate a string 
attrs: string
	   language (optional)
*/
function wgx_translate( $attrs )
{
	if (isset($attrs['LANGUAGE']))
		eval (TRANSLATE ( $attrs['STRING'], "\$tmp",$attrs['LANGUAGE'] ));
	else
		eval (TRANSLATE ( $attrs['STRING'], "\$tmp"));
	
	return $tmp;
}

/*
show pub
attrs: like wgx:module name="ads"
*/
function wgx_pub( $attrs )
{
	global $TE,$CACHEENGINE;
	
	$mod = "Ads";

	eval( "\$tempmod = new $mod"."module();");
	$TE->set_module_template_root($mod);
	
	$tempmod->set_param(true,$attrs);
	
	$CacheParam= $tempmod->GetCacheParam( $function );
	$tags = $tempmod->GetCacheTags( $function );
	
	switch ($CacheParam) {
		case -1:	// never cache
		$generated=$tempmod->Generate_Output($function);
		break;
		case -2:    // store in cache now
		$generated=$tempmod->Generate_Output($function);
		$CACHEENGINE->Store_Data($mod,$function,$tags,$generated);
		break;
		// 0 means always from cache, 1..xxxx nb seconds to cache
		default:
		if ($CACHEENGINE->Is_UptoDate($mod,$function,$tags,$CacheParam)) {
			$cacheout=$CACHEENGINE->Get_Data($mod,$function,$tags);
			$generated=$cacheout['data'];
			$cacheok=$cacheout['ok'];
		}
		else 
		{
			$generated=$tempmod->Generate_Output($function);
			$CACHEENGINE->Store_Data($mod,$function,$tags,$generated);
		}
		break;
	}
	
	return $generated;
}

/*
show a search box
$attrs: type="main,ticker,sidebox"
*/
function wgx_search( $attrs )
{
	global $TE,$CACHEENGINE;
	
	if ($attrs[TYPE]=="results")
		$generated = generate_search_results($attrs[TYPE]);
	else
		$generated = generate_search_form($attrs[TYPE]);
	
	return $generated;
}

function wgx_startElement($parser, $name, $attrs) {
  global $xml_depth,$parser_debug_mode,$stringxmltagunknown,$xml_tag,
    $xml_generated_html,
    $wgx_in_module,$wgx_module_attrs,$wgx_module_params,$stringxmlparametersoutofmodule,$tag_stack,
    $sitename,$siteurl,$siteemail;
	
	// in case we are in debug mode
	if ($parser_debug_mode)
	{
    	for ($i = 0; $i < $xml_depth; $i++) {
        	print "  ";
    	}
    	print "$xml_depth-$name opened\n";
    }
	$xml_depth++;
	
	$xml_tag = strtoupper($name); // to upper just in case....
	$tag_stack[] = $xml_tag;
	
	if (!$wgx_in_module)
	switch( $xml_tag )
	{
	  	// show the site name
		case 'WGX:SITENAME':
			 $xml_generated_html.= $sitename;
			 break;
		// show the site url
	        case 'WGX:SITEURL':
			 $xml_generated_html.= $siteurl;
			 break;
		// show the site email
	        case 'WGX:SITEEMAIL':
			 $xml_generated_html.= $siteemail;
			 break; 
		//translate a string
		case 'WGX:TRANSLATE':
			 $xml_generated_html.= wgx_translate( $attrs );
			 break;
		// show ads
		case 'WGX:PUB':
			 $xml_generated_html.= wgx_pub( $attrs );
			 break;
		case 'WGX:SEARCH':
			 $xml_generated_html.= wgx_search( $attrs );
			 break;
		// call a certain module,code
		case 'WGX:MODULE':
			$wgx_module_attrs = $attrs;
			$wgx_module_params= array();
			$wgx_in_module    = true;
			break;
		// call a  module,code working with Smarty
		case 'WGX:SMARTMODULE':
			$wgx_module_attrs = $attrs;
			$wgx_module_params= array();
			$wgx_in_module    = true;
			break;
		case 'WGX:PHP':
			 break;
		default:
			$xml_generated_html.=$stringxmltagunknown.$xml_tag;
			break;
	}
}

function wgx_characterData($parser, $data) {
	global $xml_tag,$stringxmltagunknown,$xml_generated_html,$wgx_module_params,$wgx_in_module;
	
	if ($wgx_in_module==true and $xml_tag!='WGX:MODULE' and $xml_tags!='WGX:SMARTMODULE')
	{
	  $wgx_module_params[$xml_tag].=$data;
	}
}

function wgx_endElement($parser, $name) {
    global $xml_depth,$parser_debug_mode,$xml_tag,$wgx_module_attrs,$wgx_module_params,$xml_generated_html,$tag_stack;

	$xml_depth--;
	// in case we are in debug mode
	if ($parser_debug_mode)
	{
    	for ($i = 0; $i < $xml_depth; $i++) {
        	print "  ";
    	}
    	print "$xml_depth-$name closed\n";
    }
	
	$xml_tag = strtoupper($name); // to upper just in case....
	
	if (!$wgx_in_module)
	switch( $xml_tag )
	{
		// call a certain module,code
		case 'WGX:MODULE':
			$xml_generated_html.=wgx_call_module( $wgx_module_attrs,$wgx_module_params );
			$wgx_in_module    = false;
			break;
			// call a certain module,code
		case 'WGX:SMARTMODULE':
			$xml_generated_html.=wgx_call_smartmodule( $wgx_module_attrs,$wgx_module_params );
			$wgx_in_module    = false;
			break;
		default:
			break;
	}
	
	array_pop( $tag_stack );
	$xml_tag = $tag_stack[count($tag_stack)-1];
}

// xml tag starting with <?
function wgx_PIHandler($parser, $target, $data) {
	global $xml_generated_html,$stringxmltagunknown;

    switch (strtoupper($target)) {
		// some php code to eval
		// the output must be in var $output
		case 'PHP':
			$output='';
			eval($data);
			$xml_generated_html.= $output;
			break;
        default;
			$xml_generated_html.= $stringxmltagunknown.$target;
            break;
    }
}

?>
Return current item: Webgenerator-X content management system