Location: PHPKode > projects > Simple Way to Usenet > libs/core/parser/blocks/block_href.class.php
<?php
/**
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2007  Benjamin Gillissen
	
	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 2
	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 at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class block_href extends tpl_parser {
	
	
	private static $tagmatch 	= "#(\{--HREF)(=)(PID|IMG|SELF)(;)([\w\W]+?)(--})#";	//preg_match for lang block
	private static $tagtype		= '3';
	private static $tagarg		= '5';
	
		
	private $type;
	private $arg;
	
	public function __construct($hdl, $tag, $epos, $depth, $of){
		//echo "$depth] New block Inc @ ".$tag->start()."<br/>\n";
		parent::__construct($hdl, $tag->end(), $epos, $depth, $of);
		$this->otag=$tag;
	}

	public function end(){ return $this->otag->end(); }

	public function load_tagargs($tag=NULL){
		//echo "$this->depth] Block Inc @ ".$this->otag->start()." : Loading tagargs<br/>\n";
		if ( $tag === NULL ){ $tag = $this->otag->read(); } 
		preg_match(self::$tagmatch, $tag, $result);
		if ( FALSE === $result ){
			errors::raise('Malformed Href tag @ '.$this->otag->start(), CORE_LOG_ERROR, 'TPL');
			return FALSE;
		}
		if ( isset($result[self::$tagtype]) ){ $this->type  = trim($result[self::$tagtype]); }
		if ( isset($result[self::$tagarg]) ){ $this->arg  = trim($result[self::$tagarg]); }
		return TRUE;
	}
	
	public function recurloads(){ return TRUE; }
		
	public function plugparse($l, $g, $return){
		
		if ( $this->type == 'SELF' ){	
			//{--HREF=SELF; $toappend --}
			//TODO same as pid, with active pid and detected args + toappend
			$href = '_self';
		} elseif ( $this->type == 'PID' ){
			//return '_self';
			$buf = split(',', $this->arg);
			$pid = $buf[0];
			if ( count($buf) != 1 ){
				unset($buf[0]);
				foreach($buf as $arg){ 
					$tmp = split('=', trim($arg));
					$args[trim($tmp[0])] = trim($tmp[1]); 
				}
				unset($arg, $tmp);
			}
			unset($buf);
			//{--HREF=PID; 1, mid=$mid, cid=$cid, servid=$servid, osid=$osid--}
			if ( browsing::use_modrewrite() ){
				$pid = $this->get_sourceval($pid, $l, '$');
				$pid = $this->get_sourceval($pid, $g, '%');
				$href = browsing::basedir().'/'.pages::get_pathto($pid);
				$pargs = pages::get_args($pid);
				if ( is_array($pargs) ){
					foreach($pargs as $key){ if ( isset($args[$key]) ){ $href .= "/$args[$key]"; } }
				}
				unset($pargs,$key,$args,$pid);
			} else {
				$href = browsing::basedir()."/?pid=$pid";
				if ( isset($args) ){ foreach($args as $k => $arg){ $href .= "&$k=$arg"; } }
				unset($k,$args,$pid);
			}
		} elseif ( $this->type == 'IMG' ){
			//{--HREF=IMG; flags/flag_$DEF_langs_lang.jpg --}
			$href = themes::get_active_conf('img').trim($this->arg);	
		} elseif ( $return ){ 
			return;
		} else {
			return TRUE;
		}
		//parse href for vars;
		$href = $this->get_sourceval($href, $l, '$');
		$href = $this->get_sourceval($href, $g, '%');
		if ( $return ){ return $href; }
		call_user_func($this->of, $href);
		return TRUE; 
	}
	
	public function plugparse_before($l, $g, $return, $bref){ return $this->plugparse($l, $g, $return); }
	
	private function get_sourceval($str, $ctxt, $pref){
		if ( $ctxt !== NULL AND FALSE !== strpos($str, $pref) ){
			tpl_data::fetch_reset($ctxt);
			while( FALSE !== ($data = tpl_data::fetch_Strings($ctxt) ) ){
				if ( CORE::iserror($data) ){ 
					errors::raise("Invalid Data context in Href @ $this->spos:$this->epos, no '$pref' variable will be available as data source !", CORE_LOG_ERROR, 'TPL');
					break;
				}
				$str = str_replace($pref.$data[0], $data[1], $str); //we allow local/global vars in build href ;)
			}
		}
		return $str;
	}
}
return 1;
Return current item: Simple Way to Usenet