Source for file OCSP_TEMPLATE.phpclass

Documentation is available at OCSP_TEMPLATE.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file OCSP_TEMPLATE.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package common
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-25.07.2008
  12.   * @version $Id: OCSP_TEMPLATE.phpclass,v 1.11 2008/12/10 15:39:06 peterkrebs Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_class('OCSP_OBJ',__OCSP_PHPINCPATH__ "common/OCSP_OBJ.phpclass");
  20.     require_once dirname(__FILE___OCSP_DIRSEP_ "pcf_templates.phpinc";
  21.     
  22. /**
  23.   * openCSP class OCSP_TEMPLATE
  24.   *
  25.   * @project Open CSP-Management
  26.   * @package common
  27.   *
  28.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  29.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  30.   *
  31.   * @since pk-25.07.2008
  32.   * @version $Id: OCSP_TEMPLATE.phpclass,v 1.11 2008/12/10 15:39:06 peterkrebs Exp $
  33.   */
  34. class OCSP_TEMPLATE extends OCSP_OBJ
  35. {
  36.     // ---------------------------------------------------------------------------
  37.     // constants
  38.     // ---------------------------------------------------------------------------
  39.     
  40.     /**
  41.      * @constant string CLASS_SRC_FILE
  42.      */
  43.     const CLASS_SRC_FILE = __FILE__;
  44.  
  45.     // ---------------------------------------------------------------------------
  46.     // class (static)
  47.     // ---------------------------------------------------------------------------
  48.     
  49.     /*** class vars ------------------------------------------------------ */
  50.     
  51.     /*** class methods --------------------------------------------------- */
  52.  
  53.     /**
  54.       * returns an array with fields found in the template
  55.       *
  56.       * each array element has a NAME and a TYPE index
  57.       *
  58.       * @param string $aTemplate 
  59.       * @param char $varSign 
  60.       * @param bool $debug 
  61.       *
  62.       * @return array 
  63.       */    
  64.     public static function getVars($aTemplate,$varSign='$',$debug=False)
  65.     {
  66.         $obj_tmpl new OCSP_TEMPLATE($aTemplate,$varSign);
  67.         return $obj_tmpl->getTmplVars();
  68.     }
  69.     
  70.     /**
  71.      * parses a template
  72.      *
  73.      * @param string $aTemplate 
  74.      * @param array $aArr 
  75.      * @param boolean $debug 
  76.      * @param char $varSign 
  77.      * 
  78.      * @return string 
  79.      */
  80.     public static function parse($aTemplate,$aArr=NULL,$debug=False,$varSign='$')
  81.     {
  82.         if ($debugechoDebugMethod(__FILE__,get_class("static"),"OCSP_TEMPLATE::parse()");            
  83.         //return pcf_tmpl_parse($aTemplate,$aArr,$debug,$varSign);
  84.         $obj_tmpl new OCSP_TEMPLATE($aTemplate,$varSign,$debug);
  85.         return $obj_tmpl->getParsedTemplate($aArr,$debug);
  86.     }
  87.     
  88.     /**
  89.      * appends a query string to an url
  90.      *
  91.      * @param string $url 
  92.      * @param string $queryString 
  93.      * 
  94.      * @return string 
  95.      */
  96.     public static function appendQueryString($url,$queryString)
  97.     {
  98.         $url trim($url);
  99.         $queryString str_replace("&amp;","&",trim($queryString));
  100.         
  101.         if ((substr($queryString,0,1== "?"|| (substr($queryString,0,1== "&"))
  102.         {
  103.             $queryString substr($queryString,1);
  104.         }
  105.              
  106.         if (strstr($url,"?"))
  107.         {
  108.             if ((substr($url,-1== "?"|| (substr($url,-1== "&"))
  109.             {
  110.                 return $url $queryString;
  111.             else {
  112.                 return $url "&" $queryString;
  113.             }
  114.         else {
  115.             return $url "?" $queryString;
  116.         }
  117.     }
  118.     
  119.         
  120.     // ---------------------------------------------------------------------------
  121.     // object vars
  122.     // ---------------------------------------------------------------------------
  123.     
  124.     /*** compostion --------------------------------------------------- */
  125.     
  126.     /*** attributes  -------------------------------------------------- */
  127.     
  128.     /**
  129.      * the template
  130.      *
  131.      * @var string $myTemplate 
  132.      */
  133.     protected $myTemplate = "";
  134.     
  135.     /**
  136.      * the template values to replace
  137.      * 
  138.      * @var array $myValues 
  139.      */
  140.     protected $myValues = array();
  141.  
  142.     /**
  143.      * the char sign used (character which encloses the placeholders)
  144.      * 
  145.      * @var char $varSign 
  146.      */
  147.     protected $varSign = '$';
  148.     
  149.  
  150.     /**
  151.      * simple vars
  152.      * 
  153.      * @var array simpleVars
  154.      */
  155.     protected $simpleVars = array();
  156.     
  157.     /**
  158.      * complex vars
  159.      * 
  160.      * @var array complexVars
  161.      */
  162.     protected $complexVars = array();
  163.     
  164.     // ---------------------------------------------------------------------------
  165.     // factory / construct
  166.     // ---------------------------------------------------------------------------
  167.     
  168.     /**
  169.      * construct
  170.      *
  171.      * @param string $aTemplate 
  172.      * @param array $dataArr 
  173.      * @param char $varSign 
  174.      * @param boolean $debug 
  175.      */
  176.     public function __construct($aTemplate=Null,$varSign='$',$debug=False)
  177.     {
  178.         $this->varSign      = $varSign;    
  179.         $this->setTemplate($aTemplate,$debug);
  180.     }
  181.     
  182.     // ---------------------------------------------------------------------------
  183.     // getter / setter
  184.     // ---------------------------------------------------------------------------    
  185.  
  186.     /**
  187.      * sets the template
  188.      *
  189.      * @param string $aTemplate 
  190.      * @param boolean $debug 
  191.      */
  192.     public function setTemplate($aTemplate,$debug=False)
  193.     {
  194.         if ($this->myTemplate != $aTemplate)
  195.         {
  196.             $this->myTemplate = $aTemplate;
  197.             $this->simpleVars = array();
  198.             $this->complexVars = array();
  199.         
  200.             if (!empty($aTemplate))
  201.             {
  202.                 $this->_extractVars($this->myTemplate,$debug);
  203.             }
  204.         }
  205.     }
  206.     
  207.     public function getTmplVars()
  208.     {
  209.         $arr_ret array();
  210.         foreach($this->complexVars as $str_type => $arr_vars)
  211.         {
  212.             foreach($arr_vars as $str_key => $arr_varDesc)
  213.             {
  214.                 $arr_ret[$arr_varDesc['NAME']] array('NAME'=>$arr_varDesc['NAME'],'TYPE'=>$str_type);
  215.             }
  216.         }
  217.         foreach($this->simpleVars as $str_type => $arr_vars)
  218.         {
  219.             if ($str_type != PCF_TMPLVAR_ASIS)
  220.             {
  221.                 foreach($arr_vars as $str_key => $arr_varDesc)
  222.                 {
  223.                     $arr_ret[$arr_varDesc['NAME']] array('NAME'=>$arr_varDesc['NAME'],'TYPE'=>$str_type);
  224.                 }
  225.             }
  226.         }
  227.         return $arr_ret;
  228.     }
  229.     
  230.     // ---------------------------------------------------------------------------
  231.     // parser
  232.     // ---------------------------------------------------------------------------    
  233.         
  234.     /**
  235.      * extracts the template placeholders out of the template
  236.      *
  237.      * @param string $aTemplate 
  238.      * @param boolean $debug 
  239.      */
  240.     protected function _getTemplatePlaceholders($aTemplate,$debug=False)
  241.     {
  242.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::getTemplatePlaceholders");
  243.  
  244.         $arr_ret array();
  245.         
  246.         $int_pos strpos($aTemplate,$this->varSign);
  247.         while($int_pos !== False)
  248.         {
  249.             $int_pos +=strlen($this->varSign);
  250.             $str_part=substr($aTemplate,$int_pos);
  251.             
  252.             // check for complex
  253.             if (strpos($str_part,PCF_TMPLVAR_IF=== 0)
  254.             {
  255.                 $int_pos strpos($str_part,PCF_TMPLVAR_IF.$this->varSign);
  256.                 if ($int_pos !== False)
  257.                 {
  258.                     $arr_ret[]=substr($str_part,0,$int_pos);
  259.                     $int_pos += strlen(PCF_TMPLVAR_IF.$this->varSign);
  260.                     $aTemplate substr($str_part,$int_pos);
  261.                 else {
  262.                     $aTemplate $str_part;
  263.                 }
  264.             else if (strpos($str_part,PCF_TMPLVAR_DBLOOKUP=== 0{
  265.                 $int_pos strpos($str_part,PCF_TMPLVAR_DBLOOKUP.$this->varSign);
  266.                 if ($int_pos !== False)
  267.                 {
  268.                     $arr_ret[]=substr($str_part,0,$int_pos);
  269.                     $int_pos += strlen(PCF_TMPLVAR_DBLOOKUP.$this->varSign);
  270.                     $aTemplate substr($str_part,$int_pos);
  271.                 else {
  272.                     $aTemplate $str_part;
  273.                 }                
  274.             else if (strpos($str_part,PCF_TMPLVAR_ASIS=== 0{
  275.                 $aTemplate substr($str_part,strlen($this->varSign));
  276.             else {
  277.                 $int_pos strpos($str_part,$this->varSign);
  278.                 if ($int_pos !== False)
  279.                 {
  280.                     $arr_ret[]=substr($str_part,0,$int_pos);
  281.                     $int_pos += strlen($this->varSign);
  282.                     $aTemplate substr($str_part,$int_pos);
  283.                 else {
  284.                     $aTemplate $str_part;
  285.                 }
  286.             }
  287.             $int_pos strpos($aTemplate,$this->varSign);             
  288.         }        
  289.         return $arr_ret;
  290.     }
  291.  
  292.     /**
  293.      * checks a subpart of a placeholder if it's an array
  294.      *
  295.      * @param string $placeholderPart 
  296.      * 
  297.      * @return array 
  298.      */
  299.     protected function _checkArray(&$placeholderPart)
  300.     {
  301.         if ((strpos($placeholderPart,"#"=== 0&& strstr($placeholderPart,"|"))
  302.         {
  303.             $arr_ret explode("|",substr($placeholderPart,1));
  304.             if (sizeof($arr_ret1)
  305.             {
  306.                 if ($arr_name $this->_checkFormatDate($arr_ret[0]))
  307.                 {
  308.                     $arr_ret[0$arr_name;
  309.                 elseif ($arr_name $this->_checkFormatPrintf($arr_ret[0])) {
  310.                     $arr_ret[0$arr_name;
  311.                 elseif ($arr_name $this->_checkFormatJavascript($arr_ret[0])) {
  312.                     $arr_ret[0$arr_name;
  313.                 elseif ($arr_name $this->_checkFormatIntval($arr_ret[0])) {
  314.                     $arr_ret[0$arr_name;
  315.                 else {
  316.                     $arr_ret[0array('NAME' => $arr_ret[0]);
  317.                 }
  318.                 return $arr_ret;
  319.             else 
  320.                 $placeholderPart str_replace("|","",substr($placeholderPart,strlen("#")));
  321.                 return False;
  322.             }
  323.         else {
  324.             return False;
  325.         }
  326.     }
  327.     
  328.     /**
  329.      * checks if a placeholderPart includes a date format (-D...~VARNAME)
  330.      *
  331.      * @param string $placeholderPart 
  332.      * @return array 
  333.      */
  334.     protected function _checkFormatDate(&$placeholderPart)
  335.     {
  336.         if (strpos($placeholderPart,"-D"=== 0)
  337.         {
  338.             if ($arr_format explode("~",substr($placeholderPart,strlen("-D")),2))
  339.             {
  340.                 if (sizeof($arr_ret1)
  341.                 {
  342.                     $arr_ret['NAME'$arr_format[1];
  343.                     $arr_ret['FORMATTYPE'"D";
  344.                     $arr_ret['FORMAT'$arr_format[0];
  345.                     return $arr_ret;
  346.                 else {
  347.                     $placeholderPart substr($placeholderPart,strlen("-D"));
  348.                 }
  349.             }
  350.         
  351.         return False;
  352.     }
  353.     
  354.     protected function _checkFormatPrintf(&$placeholderPart)
  355.     {
  356.         if (strpos($placeholderPart,"-P"=== 0)
  357.         {
  358.             if ($arr_format explode("~",substr($placeholderPart,strlen("-P")),2))
  359.             {
  360.                 if (sizeof($arr_ret1)
  361.                 {
  362.                     $arr_ret['NAME'$arr_format[1];
  363.                     $arr_ret['FORMATTYPE'"P";
  364.                     $arr_ret['FORMAT'$arr_format[0];
  365.                     return $arr_ret;
  366.                 else {
  367.                     $placeholderPart substr($placeholderPart,strlen("-P"));
  368.                 }
  369.             }
  370.         
  371.         return False;        
  372.     }
  373.     
  374.     protected function _checkFormatJavascript(&$placeholderPart)
  375.     {
  376.         if (strpos($placeholderPart,"-JS"=== 0)
  377.         {
  378.             if ($arr_format explode("~",substr($placeholderPart,strlen("-JS")),2))
  379.             {
  380.                 if (sizeof($arr_ret1)
  381.                 {
  382.                     $arr_ret['NAME'$arr_format[1];
  383.                     $arr_ret['FORMATTYPE'"JS";
  384.                     $arr_ret['FORMAT'$arr_format[0];
  385.                     return $arr_ret;
  386.                 else {
  387.                     $placeholderPart substr($placeholderPart,strlen("-JS"));
  388.                 }
  389.             }
  390.         
  391.         return False;        
  392.     }    
  393.     
  394.     protected function _checkFormatIntval(&$placeholderPart,$debug=False)
  395.     {
  396.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::_checkFormatIntval()");
  397.         if (strpos($placeholderPart,"!"=== 0)
  398.         {
  399.             $arr_ret['NAME'substr($placeholderPart,strlen("!"));
  400.             $arr_ret['FORMATTYPE'"!";
  401.             if ($debugechoDebugLine(__FILE__,__LINE__,"Returns: <pre>" print_r($arr_ret,True"</pre>");
  402.             return $arr_ret;
  403.         
  404.         return False;        
  405.     }        
  406.     
  407.     protected function _extractVars($aTemplate,$debug=False)
  408.     {        
  409.  
  410.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::_extractVars({$this->varSign});");
  411.         
  412.         if ($arr_parts = $this->_getTemplatePlaceholders($aTemplate,$debug))
  413.         {
  414.             foreach($arr_parts as $idx => $str_part)
  415.             {                
  416.                 // complex templates
  417.                 
  418.                 if (strpos($str_part,PCF_TMPLVAR_IF) === 0)
  419.                 {
  420.         
  421.                     if ($debug) echoDebugLine(__FILE__,__LINE__,nl2br(htmlspecialchars($str_part)));
  422.                     
  423.                     $str_varName=md5($str_part);
  424.                     $this->complexVars[PCF_TMPLVAR_IF][$str_varName]['REPLACE'$this->varSign . $str_part PCF_TMPLVAR_IF $this->varSign;
  425.                     $str_part substr($str_part,strlen(PCF_TMPLVAR_IF));
  426.                     if (strpos($str_part,$this->varSign.PCF_TMPLVAR_IF))
  427.                     {
  428.                         throw new exception("INSIDE IF NOT IMPLEMENTED");
  429.                     }
  430.                     $arr_cond = explode('|',$str_part);
  431.                     $this->complexVars[PCF_TMPLVAR_IF][$str_varName]['NAME']$arr_cond[0];
  432.                     $this->complexVars[PCF_TMPLVAR_IF][$str_varName]['SUBTMPL']=$str_part;
  433.                     unset($arr_cond[0]);
  434.                     foreach($arr_cond as $str_cond)
  435.                     {
  436.                         if ($arr_condParts = explode("~",$str_cond,2))
  437.                         {
  438.                             $this->complexVars[PCF_TMPLVAR_IF][$str_varName]['CONDITIONS'][$arr_condParts[0]] $arr_condParts[1];
  439.                         }    
  440.                     }
  441.                     continue; 
  442.                 }
  443.             
  444.                 if (strpos($str_part,PCF_TMPLVAR_DBLOOKUP) === 0)
  445.                 {
  446.                     $str_varName=md5($str_part);
  447.                     $this->complexVars[PCF_TMPLVAR_DBLOOKUP][$str_varName]['REPLACE'$this->varSign . $str_part PCF_TMPLVAR_DBLOOKUP $this->varSign;
  448.                     $str_query trim(substr($str_part,strlen(PCF_TMPLVAR_DBLOOKUP)));
  449.                     $this->complexVars[PCF_TMPLVAR_DBLOOKUP][$str_varName]['NAME''QUERY' $str_varName;
  450.                     $this->complexVars[PCF_TMPLVAR_DBLOOKUP][$str_varName]['QUERY'$str_query;
  451.                     $this->_extractVars($str_query,$debug);
  452.                     continue;
  453.                 }    
  454.  
  455.                 // simple templates
  456.                 if (strpos($str_part,PCF_TMPLVAR_ASIS))
  457.                 {
  458.                     $this->simpleVars[PCF_TMPLVAR_ASIS][array(
  459.                             'REPLACE' => $this->varSign . $str_part $this->
  460.                         );
  461.                     continue;
  462.                 }
  463.                 
  464.                 
  465.                 $arr_types = array(PCF_TMPLVAR_ARR,PCF_TMPLVAR_GET,PCF_TMPLVAR_SESSION,PCF_TMPLVAR_POST,PCF_TMPLVAR_BASE64,PCF_TMPLVAR_SERVER,PCF_TMPLVAR_GLOBALS);
  466.                 foreach($arr_types as $cha_Type)
  467.                 {
  468.                     if (strpos($str_part,$cha_Type) === 0)
  469.                     {
  470.                         $str_replace = $this->varSign . $str_part $this->varSign;
  471.                         $str_part=substr($str_part,strlen($cha_Type));
  472.                         if ($arr_desc $this->_checkArray($str_part))
  473.                         {                        
  474.                             $arr_name=$arr_desc[0];
  475.                             unset($arr_desc[0]);
  476.                             $tmp_array=array(
  477.                                     'REPLACE'    => $str_replace,
  478.                                     'KEYS'        => $arr_desc
  479.                                 );
  480.                             $this->simpleVars[$cha_Type][implode('|',$arr_desc)]=array_merge($tmp_array,$arr_name);
  481.                             continue 2;
  482.                         }
  483.     
  484.                         if ($arr_desc = $this->_checkFormatDate($str_part))
  485.                         {
  486.                             $arr_desc['REPLACE'] = $str_replace;
  487.                             $this->simpleVars[$cha_Type][$str_replace$arr_desc;
  488.                             continue 2;
  489.                         }
  490.  
  491.                         if ($arr_desc = $this->_checkFormatPrintf($str_part))
  492.                         {
  493.                             $arr_desc['REPLACE'] = $str_replace;
  494.                             $this->simpleVars[$cha_Type][$str_replace$arr_desc;
  495.                             continue 2;
  496.                         }    
  497.  
  498.                         if ($arr_desc = $this->_checkFormatJavascript($str_part))
  499.                         {
  500.                             $arr_desc['REPLACE'] = $str_replace;
  501.                             $this->simpleVars[$cha_Type][$str_replace$arr_desc;
  502.                             continue 2;
  503.                         }
  504.  
  505.                         if ($arr_desc = $this->_checkFormatIntval($str_part))
  506.                         {
  507.                             $arr_desc['REPLACE'] = $str_replace;
  508.                             $this->simpleVars[$cha_Type][$str_replace$arr_desc;
  509.                             continue 2;
  510.                         }                        
  511.                         
  512.                         $this->simpleVars[$cha_Type][$str_partarray(
  513.                             'NAME' => $str_part,
  514.                             'REPLACE' => $str_replace
  515.                         );
  516.                         continue 2;
  517.                     }                    
  518.                 }    
  519.                 
  520.                 if ($debug) echoDebugLine(__FILE__,__LINE__,"<p>" . htmlspecialchars($str_part) . "</p>");
  521.             }
  522.             
  523.             if (isset($this->complexVars[PCF_TMPLVAR_IF]))
  524.             {
  525.                 foreach($this->complexVars[PCF_TMPLVAR_IFas $str_name => $arr_var)
  526.                 {                                
  527.                     if (!isset($this->complexVars[PCF_TMPLVAR_IF][$str_name]['EXTRACTED']|| $this->complexVars[PCF_TMPLVAR_IF][$str_name]['EXTRACTED'])
  528.                     { // to avoid endless loops
  529.                         $this->complexVars[PCF_TMPLVAR_IF][$str_name]['EXTRACTED'True;
  530.                         $this->_extractVars($arr_var['SUBTMPL'],$debug);
  531.                     }
  532.                 }
  533.             }
  534.             
  535.             if ($debug) 
  536.             {
  537.                 echoDebugLine(__FILE__,__LINE__,"<p>SimpleVars:</p><pre>" . print_r($this->simpleVars,True"</pre>");
  538.                 echoDebugLine(__FILE__,__LINE__,"<p>ComplexVars:</p><pre>" print_r($this->complexVars,True"</pre>");
  539.             }
  540.         }
  541.     }
  542.     
  543.     protected function _parseComplex($aTmpl,$dataArray,$strict=False,$debug=False)
  544.     {
  545.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::parseComplex()","<pre>" . htmlspecialchars(print_r($this->complexVars,True)) "</pre><p>" htmlspecialchars($aTmpl"</p>");
  546.         
  547.         $str_return $aTmpl;
  548.         if (isset($this->complexVars[PCF_TMPLVAR_IF]))
  549.         {
  550.             
  551.             foreach($this->complexVars[PCF_TMPLVAR_IFas $cha_type => $arr_vars)
  552.             {    
  553.                 if ($strict && !isset($dataArray[$arr_vars['NAME']]))
  554.                 {
  555.                     continue;
  556.                 }
  557.                 
  558.                 if (strpos($str_return,$arr_vars['REPLACE']) !== False)
  559.                 {
  560.                     
  561.                     
  562.                     if ($debug) echoDebugLine(__FILE__,__LINE__,"<p>" . htmlspecialchars($arr_vars['REPLACE']) . " found</p>");                        
  563.                     $str_value = "";    
  564.                     if (!isset($dataArray[$arr_vars['NAME']]))
  565.                     {
  566.                         if (isset($arr_vars['CONDITIONS']['NULL'])) 
  567.                         {
  568.                             $str_value = $arr_vars['CONDITIONS']['DEFAULT'];
  569.                         } else if (isset($arr_vars['CONDITIONS']['DEFAULT'])) {
  570.                             $str_value = $arr_vars['CONDITIONS']['DEFAULT'];
  571.                         } else {
  572.                             $str_value = "";
  573.                         }                             
  574.                     } else {
  575.                         if (isset($arr_vars['CONDITIONS'][$dataArray[$arr_vars['NAME']]]))
  576.                         {
  577.                             $str_value = $arr_vars['CONDITIONS'][$dataArray[$arr_vars['NAME']]];    
  578.                         } else {
  579.                             if (!empty($dataArray[$arr_vars['NAME']]) && isset($arr_vars['CONDITIONS']['NOTNULL'])) {
  580.                                 $str_value = $arr_vars['CONDITIONS']['NOTNULL'];            
  581.                             } else if (isset($arr_vars['CONDITIONS']['DEFAULT'])) {
  582.                                 $str_value = $arr_vars['CONDITIONS']['DEFAULT'];
  583.                             }
  584.                         }
  585.                     } 
  586.         
  587.                     if (!empty($str_value))
  588.                     {
  589.                         $str_value = $this->_parseComplex($str_value,$dataArray,$strict,$debug);
  590.                         $str_value $this->_parseSimple($str_value,$dataArray,$strict,$debug);                
  591.                     }
  592.                     if ($debug) echoDebugLine(__FILE__,__LINE__,$arr_vars['NAME'] . ":<br />" . htmlspecialchars($str_value));
  593.                     //echoDebugLine(__FILE__,__LINE__,"<p>\$str_return</p><pre>" . htmlspecialchars($str_return) . "</pre>");
  594.                     $str_return = str_replace($arr_vars['REPLACE'],$str_value,$str_return);
  595.                     //echoDebugLine(__FILE__,__LINE__,"<p>\$str_return</p><pre>" . htmlspecialchars($str_return) . "</pre><hr />");                    
  596.                 }            
  597.             }
  598.         }
  599.         return $str_return;        
  600.     }
  601.  
  602.     protected function _parseSimple($aTmpl,$dataArray,$strict=False,$debug=False)
  603.     {
  604.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::parseSimple()");
  605.         
  606.         $str_return = $aTmpl;
  607.         foreach($this->simpleVars as $cha_type => $arr_typeVars)
  608.         {
  609.             if ($debug) echoDebugLine(__FILE__,__LINE__,"<h1>Type{$cha_type}</h1>");
  610.             foreach($arr_typeVars as $str_name => $arr_varDesc)
  611.             {
  612.                 if ($strict && !isset($dataArray[$arr_varDesc['NAME']]))
  613.                 {
  614.                     continue;
  615.                 }
  616.                 
  617.                 if (strpos($str_return,$arr_varDesc['REPLACE']) !== False)
  618.                 {
  619.                     $str_value = "";
  620.                     switch($cha_type)
  621.                     {
  622.                         case PCF_TMPLVAR_ARR:
  623.                             $str_value = (isset($dataArray[$arr_varDesc['NAME']]) ? $dataArray[$arr_varDesc['NAME']] : ""); 
  624.                             $str_value = $this->_getVarValue($str_value,$arr_varDesc,$debug);
  625.                             break;
  626.                         case PCF_TMPLVAR_BASE64:
  627.                             if ($str_value = isset($dataArray[$arr_varDesc['NAME']]))
  628.                             { 
  629.                                 if (is_array($str_value))
  630.                                 {
  631.                                     $str_value = serialize($str_value);
  632.                                 }                             
  633.                                 $str_value = base64_encode($str_value);
  634.                             } else $str_value = "";                            
  635.                             break;
  636.                         case PCF_TMPLVAR_GET:
  637.                             if ($debug) echoDebugLine(__FILE__,__LINE__,"<h2>GET_VAR{$arr_varDesc['NAME']}</h2><pre>" . print_r($_GET,True) . "</pre>");
  638.                             $str_value=(isset($_GET['oaxArg'][$arr_varDesc['NAME']]) ? $_GET['oaxArg'][$arr_varDesc['NAME']] : "");
  639.                             $str_value=(isset($_GET[$arr_varDesc['NAME']]) ? $_GET[$arr_varDesc['NAME']] : $val);
  640.                                $str_value = $this->_getVarValue($str_value,$arr_varDesc,$debug);
  641.                             break;
  642.                         case PCF_TMPLVAR_SESSION:
  643.                             if ($str_value OCSP_SESSION::getInstance(True)->getValue($arr_varDesc['NAME']))
  644.                             {
  645.                                 $str_value = $this->_getVarValue($str_value,$arr_varDesc,$debug);
  646.                             }
  647.                             break;
  648.                         case PCF_TMPLVAR_POST:
  649.                             $str_value = (isset($_POST[$arr_varDesc['NAME']]) ? $_POST[$arr_varDesc['NAME']] : Null);                         
  650.                             $str_value = $this->_getVarValue($str_value,$arr_varDesc,$debug);
  651.                             break;
  652.                         case PCF_TMPLVAR_SERVER:
  653.                             $str_value (isset($_SERVER[$arr_varDesc['NAME']]$_SERVER[$arr_varDesc['NAME']] Null);                         
  654.                             $str_value $this->_getVarValue($str_value,$arr_varDesc,$debug);                            
  655.                             break;    
  656.                         case PCF_TMPLVAR_GLOBALS:
  657.                             if ($str_value OCSP_CONF::getInstance()->getValue($arr_varDesc['NAME']))
  658.                             {
  659.                                 $str_value = $this->_getVarValue($str_value,$arr_varDesc,$debug);
  660.                                 break;                                                        
  661.                             }
  662.                             if (isset($GLOBALS[$arr_varDesc['NAME']]))
  663.                             {
  664.                                 $str_value = $this->_getVarValue($GLOBALS[$arr_varDesc['NAME']],$arr_varDesc,$debug);
  665.                                 break;
  666.                             }
  667.                             // compat 
  668.                             if (isset($GLOBALS['OCSP_CONF'][$arr_varDesc['NAME']]))
  669.                             {
  670.                                 $str_value = $this->_getVarValue($GLOBALS['OCSP_CONF'][$arr_varDesc['NAME']],$arr_varDesc,$debug);
  671.                                 break;
  672.                             }                            
  673.                             if (isset($GLOBALS['OCSP_VAL'][$arr_varDesc['NAME']]))
  674.                             {
  675.                                 $str_value = $this->_getVarValue($GLOBALS['OCSP_VAL'][$arr_varDesc['NAME']],$arr_varDesc,$debug);
  676.                                 break;
  677.                             }    
  678.                             break;                            
  679.                     }
  680.                     //echoDebugLine(__FILE__,__LINE__,"<p>{$cha_type}[{$str_name}] :" . htmlspecialchars($arr_varDesc['REPLACE']) . "</p><pre>" . htmlspecialchars($str_return) . "</pre>");                    
  681.                     $str_return = str_replace($arr_varDesc['REPLACE'],$str_value,$str_return);
  682.                     //echoDebugLine(__FILE__,__LINE__,"<pre>" . htmlspecialchars($str_return) . "</pre><p>{$cha_type}[{$str_name}]</p><hr />");        
  683.                 }                
  684.             }
  685.         }
  686.                 
  687.         return $str_return;
  688.     }
  689.     
  690.     /**
  691.      * checks the format an returns the proper formated value
  692.      *
  693.      * @param mixed $aValue
  694.      * @param array $varDesc
  695.      * @param boolean $debug
  696.      * 
  697.      * @return string
  698.      */
  699.     protected function _getVarValue($aValue,$varDesc,$debug)
  700.     {
  701.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::_getVarValue()",print_r($varDesc,True));
  702.         
  703.         $str_return = $aValue;
  704.         if (isset($varDesc['KEYS']) && is_array($varDesc['KEYS']))
  705.         { // we have an array
  706.             if (is_array($aValue))
  707.             {
  708.                 $arr_value = $arr_value;
  709.                 foreach($varDesc['KEYS'] as $str_key)
  710.                 {
  711.                     if (isset($arr_value[$str_key]))
  712.                     {
  713.                         $arr_value = $arr_value[$str_key];    
  714.                     }
  715.                 }
  716.                 $str_return = $arr_value;
  717.             }
  718.         }
  719.  
  720.         if (isset($varDesc['FORMATTYPE']))
  721.         {
  722.             switch($varDesc['FORMATTYPE'])
  723.             {
  724.                 case "D"// date format
  725.                     pcf_require_class("OCSP_DATE","common/");
  726.                     $obj_date = new OCSP_DATE();
  727.                     $obj_date->setDateStr($str_return);
  728.                     $str_return $obj_date->dateStr($varDesc['FORMAT']);
  729.                     if ($debugechoDebugLine(__FILE__,__LINE__,"Value: " intval($str_return));
  730.                     return $str_return;
  731.                 case "P"// printf
  732.                     if (!empty($varDesc['FORMAT']))
  733.                     {
  734.                         $str_return = sprintf($varDesc['FORMAT'],$str_return);
  735.                     }
  736.                     if ($debug) echoDebugLine(__FILE__,__LINE__,"Value: " . intval($str_return));
  737.                     return $str_return;
  738.                 case "JS"// js escaped
  739.                     if (!empty($varDesc['FORMAT']))
  740.                     {
  741.                         $str_return = str_replace($varDesc['FORMAT'],"\\" . $varDesc['FORMAT'], $str_return); 
  742.                     } else {
  743.                         $str_return = pcf_js_escape($str_return);
  744.                     }
  745.                     if ($debug) echoDebugLine(__FILE__,__LINE__,"Value: " . intval($str_return));
  746.                     return $str_return;
  747.                 case "!"// intval
  748.                     if ($debug) echoDebugLine(__FILE__,__LINE__,"Value: " . intval($str_return));
  749.                     return intval($str_return);                                
  750.             }
  751.         }
  752.         
  753.         if ($debug) echoDebugLine(__FILE__,__LINE__,"Value: " . intval($str_return));
  754.         return $str_return;
  755.         
  756.         
  757.     }
  758.     
  759.     /**
  760.      * parse special values of a template
  761.      *
  762.      * @param string $strTmpl
  763.      * @param array $dataArray
  764.      * @param boolean $debug
  765.      * 
  766.      * @return string
  767.      */
  768.     public function parseSpecials($strTmpl,$dataArray,$debug)
  769.     {
  770.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::parseSpecials()");
  771.         return pcf_tmpl_repl_specials($strTmpl,$dataArray);
  772.     }
  773.     
  774.     /**
  775.      * returns the parsed template
  776.      * 
  777.      * @param array $dataArray
  778.      * @param boolean $debug
  779.      */
  780.     public function getParsedTemplate($dataArray,$debug=False)
  781.     {        
  782.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::getParsedTemplate();");
  783.                 
  784.         $str_return = $this->_parseComplex($this->myTemplate,$dataArray,False,$debug);
  785.         $str_return $this->_parseSimple($str_return,$dataArray,False,$debug);
  786.  
  787.         return $str_return;
  788.     }
  789.  
  790.     /**
  791.      * parses only the set values and keeps other placeholders
  792.      * 
  793.      * @param array $dataArray
  794.      * @param boolean $debug
  795.      * 
  796.      * @return string
  797.      */
  798.     public function getValueParsedTemplate($dataArray,$debug=False)
  799.     {
  800.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"OCSP_TEMPLATE::getValueParsedTemplate();");
  801.                 
  802.         $str_return = $this->_parseComplex($this->myTemplate,$dataArray,True,$debug);
  803.         $str_return $this->_parseSimple($str_return,$dataArray,True,$debug);
  804.  
  805.         return $str_return;        
  806.     }
  807.     
  808. }
  809.  

Documentation generated on Thu, 08 Jan 2009 17:47:11 +0100 by phpDocumentor 1.4.0a2