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

	Copyright (C) 2009  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 tpl_content {
	
	private $HDL;
	private $spos;		//block start offset
	private $epos;		//block end offset
	private $depth;		//block end offset
	private $of;		//output function to use;
		
	public function __construct($hdl, $spos, $epos, $depth, $of){
		//echo "$depth] New Block content @ $spos:$epos<br/>\n";
		$this->HDL = $hdl;
		$this->spos = $spos;
		$this->epos = $epos;
		$this->depth= $depth;
		$this->of=$of;
	}
	
	public function plugin(){ return 'tpl_content'; }
	public function ref(){ return FALSE; }
	public function start(){ return $this->spos; }
	public function end(){ return $this->epos; } 
	
	
	public function parse($l, $g, $return){
		//echo "$this->depth] Parsing Content @ $this->spos:$this->epos (".($this->epos-$this->spos)."bytes) Lref=$l, Gref=$g<br/>\n";
		fseek($this->HDL, $this->spos);
		$buf = fread($this->HDL, ($this->epos-$this->spos) );
		if ( $l !== NULL AND FALSE !== strpos($buf, '$') ){
			tpl_data::fetch_reset($l);
			while( FALSE !== ($data = tpl_data::fetch_strings($l) ) ){
				if ( CORE::iserror($data) ){
					errors::raise("Invalid Local Data context in content @ $this->spos:$this->epos, no '\$' variable will be replaced !", CORE_LOG_ERROR, 'TPL');
					break; 
				}
				//echo "$this->depth] Replacing Content Local var $$data[0] => $data[1] <br/>\n";
				$buf = str_replace("$$data[0]", $data[1], $buf);
				if ( FALSE === strpos($buf, '$') ){ break; }
			}
		}
		if ( $g === NULL OR FALSE === strpos($buf, '%') ){ 
			if ( $return ){ return $buf; }
			call_user_func($this->of, $buf);
			return TRUE; 
		}
		tpl_data::fetch_reset($g);
		while( FALSE !== ($data = tpl_data::fetch_strings($g) ) ){
			if ( CORE::iserror($data) ){
				errors::raise("Invalid Global Data context in content @ $this->spos:$this->epos, no '%' variable will be replaced !", CORE_LOG_ERROR, 'TPL');
				break; 
			}
			//echo "$this->depth] Replacing Content Global var %$data[0] => $data[1]<br/>\n";
			$buf = str_replace("%".$data[0], $data[1], $buf);
			if ( FALSE === strpos($buf, '%') ){ break; }
		}
		if ( $return ){ return $buf; }
		call_user_func($this->of, $buf);
		return TRUE;
	}
	
	public function parse_before($l, $g, $return, $bref){ return $this->parse($l, $g, $return); }
	public function parse_after($l, $g, $return, $bref){ return $this->parse($l, $g, $return); }
	public function parse_between($l, $g, $return, $bref){ return $this->parse($l, $g, $return); }
}
Return current item: Simple Way to Usenet