<?php

/*
 * Plugin:  Template Filter Prototype (for Life Type 1.1)
 * Version: 1.0
 * Author:  Pan Ying(http://www.pactofshadow.com/lifetype/~nest)
 * Contact: panying2000@gmail.com
 * Release: 2006.10.4
 *   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.                  
 */

	include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );	
	//include_once( PLOG_CLASS_PATH."class/template/menu/menu.class.php" );
	
	//var $arrayPluginTemplateFilterSmartyPostFilter;
	//var $arrayPluginTemplateFilterSmartyPreFilter;
	
	
	class PluginTemplateFilter extends PluginBase
	{
	
		function PluginTemplateFilter()
		{
			
		    global $arrayPluginTemplateFilterSmartyPostFilter;
		    global $arrayPluginTemplateFilterSmartyPreFilter;
		    
			$this->PluginBase();
			
			$this->id = "templatefilter";
			$this->author = "EagleKing";
            
      		//no menu now ...      		
      		
      		if (!isset($arrayPluginTemplateFilterSmartyPostFilter))
      			$arrayPluginTemplateFilterSmartyPostFilter = array();
      		
      		//this is after compile
      		
      		//test only , for test  the plugin is work or not 
      		$arrayPluginTemplateFilterSmartyPostFilter[] = Array(
      		  "pattern" => "'</head>'",
      		  "replace" => "</head>PluginTemplateFilterSmartyPostFilter Apllyed here",
      		  "templatename" => "all",
      		  "group" => 0 ,
      		  "onlymatch" => false,
      		  "disabled" => true
      		);    
      		
      		//add text before document end , could add Google Analytics here 
      		$arrayPluginTemplateFilterSmartyPostFilter[] = Array(
      		  "pattern" => "/<\/body>/i",
      		  "replace" => "<script src=\"http://www.google-analytics.com/urchin.js\" type=\"text/javascript\">
</script>
<script type=\"text/javascript\">
_uacct = \"type your own code\";
urchinTracker();
</script></body>",
      		  "templatename" => "footer.template",
      		  "group" => 0,
      		  "onlymatch" => false ,
      		  "disabled" => false
      		);
      		
      		if (!isset($arrayPluginTemplateFilterSmartyPreFilter))
      			$arrayPluginTemplateFilterSmartyPreFilter = array();
      	
      	    //this is before complie
      	    
      	    //replace <!-- \[authImage plugin\] --> to AuthImage
      		$arrayPluginTemplateFilterSmartyPreFilter[] = Array(
      		  "pattern" => "'<!-- \[authImage plugin\] -->'",
      		  "replace" => "{if \$authimage->isEnabled()}
<div><label for=\"authImage\">authImage</label><input type=\"text\" name=\"authImage\" id=\"authImage\" value=\"\" />&nbsp;{\$authimage->show()}</div>
{/if}",
      		  "templatename" => "commentform.template",
      		  "group" => 5 ,
      		  "onlymatch" => false,
      		  "disabled" => false
      		);   
      		
      		//has install authImage in template ?
      		$arrayPluginTemplateFilterSmartyPreFilter[] = Array(
      		  "pattern" => "/authImage/i",
      		  "replace" => "",
      		  "templatename" => "commentform.template",
      		  "group" => 5,
      		  "onlymatch" => true,
      		  "disabled" => false
      		); 
      		
      		//try to find a correct postion to put AuthImage anyway
      		$arrayPluginTemplateFilterSmartyPreFilter[] = Array(
      		  "pattern" => "/<\/form>/i",
      		  "replace" => "{if \$authimage->isEnabled()}
<div><label for=\"authImage\">authImage</label><input type=\"text\" name=\"authImage\" id=\"authImage\" value=\"\" />&nbsp;{\$authimage->show()}</div>
{/if} 
</form>",
      		  "templatename" => "commentform.template",
      		  "group" => 5 ,
      		  "onlymatch" => false,
      		  "disabled" => false
      		);
      		          
		}		
	}
	
	function PluginTemplateFilterSmartyPostFilter($tpl_output, &$smarty)
	{
		global $arrayPluginTemplateFilterSmartyPostFilter;
		
	    //echo "PluginTemplateFilterSmartyPostFilter call here".$smarty->_current_file;
	    
	    if (!is_array($arrayPluginTemplateFilterSmartyPostFilter))
	      return $tpl_output;
	    
	    $patharray = explode ('/', $smarty->_current_file);
	    if (count($patharray)<=0)
	       $templatename = "";
	    else
	       $templatename = $patharray[count($patharray)-1];
	       
	    if (count($patharray)<=1)
	       $templatedir = "";
	    else
	       $templatedir = $patharray[count($patharray)-2];
	    
	    if ($templatedir == "wizard" ||
	         $templatedir == "admin" ||
	         $templatedir == "rss")
	         return $tpl_output;
	    
	    $proccessedgroup = Array();
	    
	    foreach ($arrayPluginTemplateFilterSmartyPostFilter as $filter)
	    {
	    	
	    	//var_dump($filter);
	    	    	
	    	if ( $filter["group"] != 0
	    	 && isset($proccessedgroup[$filter["group"]]) 
	    	 && $proccessedgroup[$filter["group"]] == "1")
	    	 continue;
	    	 
	        if ($filter["disabled"])
	          continue;
	        
	    	if ($filter["templatename"] == "all" ||
	    	   $filter["templatename"] == $templatename)
	    	{
	    		
	    	   if ($filter["onlymatch"])
	    	   {
	    	     if (preg_match($filter["pattern"],$tpl_output) >0)
	    	     	$proccessedgroup[$filter["group"]] = "1";
	    	   }
	    	   else
	    	   {
	    	   	 $tpl_output_after =
       				preg_replace($filter["pattern"],
                    $filter["replace"], $tpl_output);
                    
	             if ($filter["group"] == 0 || $tpl_output_after != $tpl_output)
	             {	               	
	                  
	               	  $tpl_output = $tpl_output_after;
	               	  $proccessedgroup[$filter["group"]] = "1";
	             }
	    	   }  
                 	    	
	    	}	    	
	    }	    
	                  
	    
	    return $tpl_output;
	}
	
	function PluginTemplateFilterSmartyPreFilter($tpl_output, &$smarty)
	{
		global $arrayPluginTemplateFilterSmartyPreFilter;
		
	    //echo "PluginTemplateFilterSmartyPreFilter call here".$smarty->_current_file;
	    
	    if (!is_array($arrayPluginTemplateFilterSmartyPreFilter))
	      return $tpl_output;
	    
	    $patharray = explode ('/', $smarty->_current_file);
	    if (count($patharray)<=0)
	       $templatename = "";
	    else
	       $templatename = $patharray[count($patharray)-1];
	       
	    if (count($patharray)<=1)
	       $templatedir = "";
	    else
	       $templatedir = $patharray[count($patharray)-2];
	    
	    if ($templatedir == "wizard" ||
	         $templatedir == "admin" ||
	         $templatedir == "rss")
	         return $tpl_output;
	       
	    $proccessedgroup = Array();
	    
	    foreach ($arrayPluginTemplateFilterSmartyPreFilter as $filter)
	    {
	    	
	    	//var_dump($filter);
	    	    	
	    	if ( $filter["group"] != 0
	    	 && isset($proccessedgroup[$filter["group"]]) 
	    	 && $proccessedgroup[$filter["group"]] == "1")
	    	 continue;
	    	 
	    	if ($filter["disabled"])
	          continue;
	        
	    	if ($filter["templatename"] == "all" ||
	    	   $filter["templatename"] == $templatename)
	    	{
	    		
	    	   if ($filter["onlymatch"])
	    	   {
	    	     if (preg_match($filter["pattern"],$tpl_output) >0)
	    	     	$proccessedgroup[$filter["group"]] = "1";
	    	   }
	    	   else
	    	   {
	    	   	 $tpl_output_after =
       				preg_replace($filter["pattern"],
                    $filter["replace"], $tpl_output);
                    
	             if ($filter["group"] == 0 || $tpl_output_after != $tpl_output)
	             {	               	
	                  
	               	  $tpl_output = $tpl_output_after;
	               	  $proccessedgroup[$filter["group"]] = "1";
	             }
	    	   }  
                 	    	
	    	}	    	
	    }
	    
	                  
	    
	    return $tpl_output;
	}

?>