Source for file pcf_templates.phpinc

Documentation is available at pcf_templates.phpinc

  1. <?php
  2. /**
  3.   * template parse functions
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @author     Peter Krebs <p.krebs@lvus.at>
  7.   * @copyright  (c) 2002-2003 by Peter Krebs and Landesverlag Unternehmensservice
  8.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  9.   *
  10.   * @package    common
  11.   * @category   templates
  12.   *
  13.   * @version pk-04-08-10
  14.   * @version pk-04-12-21 documentation changed
  15.   *
  16.   */
  17.  
  18.     define ('PCF_TMPLVAR_GET',"^");
  19.     define ('PCF_TMPLVAR_ARR',"*");
  20.     define ('PCF_TMPLVAR_SESSION',"~");
  21.     define ('PCF_TMPLVAR_POST',"§");
  22.     define ('PCF_TMPLVAR_SERVER',"°");
  23.  
  24.     /**
  25.       * Global Array $GLOBALS values
  26.       * @since pk-03-12-23
  27.       */
  28.     define ('PCF_TMPLVAR_GLOBALS',"@");
  29.  
  30.     /**
  31.       * @since pk-04-08-10
  32.       */
  33.     define ('PCF_TMPLVAR_IF',"?");
  34.  
  35.     /**
  36.       * @since pk-04-10-18
  37.       */
  38.     define ('PCF_TMPLVAR_DBLOOKUP',"%");
  39.  
  40.     /**
  41.       * @since pk-05-01-17
  42.       */
  43.     define ('PCF_TMPLVAR_ASIS',"\\");
  44.  
  45.     /**
  46.       * @since pk-05-08-03
  47.       */
  48.     define ('PCF_TMPLVAR_BASE64',"ß");
  49.  
  50.  
  51.     /**
  52.       * @since pk-05-05-12
  53.       */
  54.     define ('PCF_TMPL_MAXVARLENGTH',128);
  55.  
  56.     /**
  57.       * @since pk-05-08-29
  58.       */
  59.     define ('PCF_TMPLVAR_INCLUDE',"<");
  60.  
  61.     /**
  62.       * splits the template into the replace string of a complex var
  63.       * and the rest of the template
  64.       *
  65.       * example "LINKTYPE....?$ asdfsadf " will return "$?LINKTYPE....?$" and set $aTemplate to " asdfsadf "
  66.       *
  67.       * @param string $aTemplate 
  68.       * @param string $type 
  69.       * @param boolean $debug 
  70.       *
  71.       * @return string 
  72.       *
  73.       * @version pk-05-06-23
  74.       *
  75.       */
  76.     function pcf_tmpl_findComplexReplace(&$aTemplate,$type,$varSign="\$",$debug=False{
  77.         if ($debugecho "<p><b>pcf_tmpl_findComplexReplace(...,type=$type,varsign=$varSign)</b> (".__FILE__.")</p>";
  78.  
  79.         if ($debugecho "<blockquote><pre styl=\"font-size:60%\">".htmlspecialchars($aTemplate)."</pre></blockquote>";
  80.  
  81.         if ($tmp=strstr($aTemplate,$type.$varSign)) // find the end tag
  82.             $ret=substr($aTemplate,0,(strlen($aTemplate)-strlen($tmp)));
  83.             $aTemplate=substr($tmp,2);  // cut the end tag from $aTemplate;
  84.  
  85.             $ret2=$ret;$i=1;
  86.             while (strstr($ret2,$varSign.$type&& ($i 100)) {    // we have an enclosed tag of the same type
  87.                                                                     // and got the end of the inner type
  88.                 $i++;                                               // $i < 100 to avoid endless loop
  89.                 if ($tmp=strstr($aTemplate,$type.$varSign)) {       // find the end tag of the outer type
  90.                     $ret2=substr($aTemplate,0,(strlen($aTemplate)-strlen($tmp)));
  91.                     $aTemplate=substr($tmp,2);  // cut the end tag from $aTemplate;
  92.                     $ret.=$type.$varSign.$ret2;
  93.                 }
  94.                 if ($debug{
  95.                     echo "<hr><h1>$i</h1>";
  96.                     echo "<pre style=\"fontsize: 75%\">".htmlspecialchars($ret)."</pre>";
  97.                 }
  98.             }
  99.             return $varSign.$type.$ret.$type.$varSign;
  100.         }
  101.         return NULL;
  102.     }
  103.  
  104.  
  105.  
  106.     /**
  107.       * returns an array with fields and types
  108.       *
  109.       * array[SIMPLE|COMPLEX][TYPE][name]
  110.       *
  111.       * each array element has a NAME, a TYPE and a replace string
  112.       * note COMPLEX elements are not splitted up
  113.       *
  114.       * @param string $aTemplate 
  115.       * @param char $varSign 
  116.       * @param bool $debug 
  117.       *
  118.       * @return array 
  119.       *
  120.       * @version pk-05-05-12
  121.       * @version pk-05-06-27 BUGFIX forgotten to add PCF_TMPLVAR_SERVER to simple type :-)
  122.       *
  123.       */
  124.     function pcf_templ_getVarArray($aTemplate,$varSign="\$",$debug=False
  125.     {        
  126.         if ($debugechoDebugLine(__FILE__,__LINE__,"<p><b>pcf_templ_getVarArray(...,varsign=$varSign)</b></p>");
  127.  
  128.         $varArr=array('SIMPLE'=>array(),'COMPLEX'=>array());
  129.  
  130.         while($aTemplate=strstr($aTemplate,$varSign)) {
  131.             $type     =substr($aTemplate,1,1);
  132.             $aTemplate=substr($aTemplate,2);
  133.  
  134.             if ($debugechoDebugLine(__FILE__,__LINE__,"<p>Type$type found</p>");
  135.  
  136.             switch($type{
  137.                 case PCF_TMPLVAR_ARR:
  138.                 case PCF_TMPLVAR_GET:
  139.                 case PCF_TMPLVAR_GLOBALS:
  140.                 case PCF_TMPLVAR_SESSION:
  141.                 case PCF_TMPLVAR_POST:
  142.                 case PCF_TMPLVAR_SERVER:    // <pk-05-06-27 /> Bugfix forgotten it :-(
  143.                 case PCF_TMPLVAR_BASE64:    // <pk-05-08-03 />
  144.                 case PCF_TMPLVAR_INCLUDE:   // <pk-05-08-29 />
  145.                     // simple types ends with $varSign and do not have other vars included
  146.                     if (($tmp=explode($varSign,$aTemplate,2)) && (strlen($tmp[0]PCF_TMPL_MAXVARLENGTH)) {
  147.                         $varArr['SIMPLE'][$type][$tmp[0]]=$tmp[0];
  148.                         if ($debugechoDebugLine(__FILE__,__LINE__,$tmp[0]);                        
  149.                         $aTemplate=(isset($tmp[1]$tmp[1"")// <pk-07-01-09 /> E_ALL
  150.                     }
  151.                     break;
  152.                 case PCF_TMPLVAR_ASIS:          // starts with $/ and ends with /$
  153.                                                 // <pk-08-06-27>                                                
  154.                     if ($debugechoDebugLine(__FILE__,__LINE__,"Type: PCF_TMPLVAR_ASIS");
  155.                       $str_replace ""
  156.                     $str_tmpTmpl $aTemplate;
  157.                     
  158.                     do {
  159.                         if ($arr_tmp explode($varSign,$str_tmpTmpl,2))
  160.                         {
  161.                             if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>" print_r($arr_tmp,True"</pre>");                             
  162.                             if ($debugechoDebugLine(__FILE__,__LINE__,"\$str_replace: " $str_replace);
  163.                             
  164.                             if ($arr_tmp[0== PCF_TMPLVAR_ASIS{
  165.                                 $str_replace .= "\$";
  166.                                 $aTemplate $str_tmpTmpl;                                
  167.                                 break// the loop                                 
  168.                             else if (substr($str_replace,-1== PCF_TMPLVAR_ASIS{
  169.                                 $str_replace .= substr($arr_tmp[0],0,-1);
  170.                                 $aTemplate $str_tmpTmpl;                                
  171.                                 break// the loop 
  172.                             else if (empty($arr_tmp[1])) {                                
  173.                                 if ($debugechoDebugLine(__FILE__,__LINE__,"break(2)");
  174.                                 break(2)// the loop and switch
  175.                             else if (empty($arr_tmp[0])) {
  176.                                 $str_replace .= "\$";
  177.                             else {
  178.                                 $str_replace .= $arr_tmp[0];
  179.                             }
  180.                             $str_tmpTmpl $arr_tmp[1];
  181.                         else {
  182.                             if ($debugechoDebugLine(__FILE__,__LINE__,"break(2)");                            
  183.                             break(2)// the loop and switch                                                                     
  184.                         }
  185.                     while(True);
  186.                     
  187.                     if ($debugechoDebugLine(__FILE__,__LINE__,"\$str_replace: " $str_replace "<br />\n\$aTemplate: " $aTemplate);
  188.                                         
  189.                     $str_replace PCF_TMPLVAR_ASIS $str_replace PCF_TMPLVAR_ASIS;
  190.                     $varArr['SIMPLE'][$type][$str_replace$str_replace;
  191.  
  192.                     break;    
  193.                 case PCF_TMPLVAR_IF:            // starts with $? and ends with ?$
  194.                     if ($replStr=pcf_tmpl_findComplexReplace($aTemplate,$type,$varSign,$debug)) {
  195.                         if ($tmp=explode("|",$replStr,2)) {
  196.                             $name=substr($tmp[0],2);
  197.                             $idx=$name;$i=0;
  198.                             while (isset($varArr['COMPLEX'][$idx]&& ($varArr['COMPLEX'][$idx]['REPLACE'!= $replStr)) {
  199.                                 $idx=$name."#".$i++;
  200.                             }
  201.                             $varArr['COMPLEX'][$idx]=array(
  202.                                 'NAME'      => $name,
  203.                                 'REPLACE'   => $replStr,
  204.                                 'TYPE'      => $type,
  205.                                 'CMD'       => substr($tmp[1],0,-2// remove the end sign
  206.                             );
  207.                         else {
  208.                             echo "<p>".htmlspecialchars($replStr)."</p>";
  209.                         }
  210.                     }
  211.                     break;
  212.                 // <pk-05-09-20>
  213.                 case PCF_TMPLVAR_DBLOOKUP:      // starts with $% and ends with %$
  214.                     if ($replStr=pcf_tmpl_findComplexReplace($aTemplate,$type,$varSign,$debug)) {
  215.                         $s_query=substr($replStr,2);
  216.                         $s_query=substr($s_query,0,-2);
  217.                         $name=preg_replace('/[^a-z0-9_\-]/i',"_",$s_query);
  218.                         $idx=$name;$i=0;
  219.                         while (isset($varArr['COMPLEX'][$idx]&& ($varArr['COMPLEX'][$idx]['REPLACE'!= $replStr)) {
  220.                             $idx=$name."#".$i++;
  221.                         }
  222.                         $varArr['COMPLEX'][$idx]=array(
  223.                             'NAME'      => $name,
  224.                             'REPLACE'   => $replStr,
  225.                             'TYPE'      => $type,
  226.                             'CMD'       => $s_query
  227.                         );
  228.                     else {
  229.                         echo "<p>".htmlspecialchars($replStr)."</p>";
  230.                     }
  231.                     break;
  232.                     // </pk-05-09-20>
  233.                 default:
  234.                     if ($debugecho "<hr><p><b>TYPE$type</b></p><blockquote style=\"font-size:50%\">".htmlspecialchars($aTemplate)."</blockquote>";
  235.             }
  236.         }
  237.         if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>".print_r($varArr,True)."</pre>");
  238.         return $varArr;
  239.     }
  240.  
  241.     /**
  242.       * returns an array with variable names
  243.       *
  244.       * @param string $aTemplate 
  245.       * @param char $varSign 
  246.       * @param bool $debug 
  247.       *
  248.       * @return array 
  249.       *
  250.       * @version pk-06-03-20
  251.       *
  252.       */
  253.     function pcf_templ_getVarNames($aTemplate,$varSign="\$",$debug=False{
  254.         if ($debugechoDebugMethod(__FILE__,"","pcf_templ_getVarNamesArr()","",0);
  255.  
  256.         $a_Names=array();
  257.  
  258.         // first get the vars
  259.         $a_varArr=pcf_templ_getVarArray($aTemplate,$varSign,$debug);
  260.  
  261.         // check simple vars for format information
  262.         foreach($a_varArr['SIMPLE'as $type => $varLst{
  263.             foreach($varLst as $name{
  264.                 switch($type{
  265.                     case PCF_TMPLVAR_BASE64:
  266.                     case PCF_TMPLVAR_ARR:
  267.                         if ((substr($name,0,1== "#"&& strstr($name,"|")) {
  268.                             $a_Names[$type][]=$substr($name,1);
  269.                         else if (substr($name,0,1== "-" && strstr($name,"~")) {
  270.                             if ($nArr=explode("~",$name,2)) {
  271.                                 $a_Names[$type][]=$nArr[1];
  272.                             }
  273.                         else {
  274.                             $a_Names[$type][]=$name;
  275.                         }
  276.                         break;
  277.                     default:
  278.                         $a_Names[$type][]=$name;
  279.                 }
  280.             }
  281.         }
  282.  
  283.         foreach($a_varArr['COMPLEX'as $a_cVar{
  284.             $a_Names[$a_cVar['TYPE']]=$a_cVar['NAME'];
  285.         }
  286.  
  287.         return $a_Names;
  288.  
  289.     }
  290.  
  291.  
  292.     /**
  293.       * returns an array with fields found in the template
  294.       *
  295.       * each array element has a NAME and a TYPE index
  296.       *
  297.       * @param string $aTemplate 
  298.       * @param char $varSign 
  299.       * @param bool $debug 
  300.       *
  301.       * @return array 
  302.       *
  303.       * @version pk-05-05-12
  304.       * @version pk-06-12-01
  305.       *
  306.       */
  307.     function pcf_tmpl_getVars($aTemplate,$varSign='$',$debug=False{
  308.         if ($debugechoDebug(__FILE__,"<p><b>pcf_tmpl_getVars(...)</b></p>");
  309.         $a_ret=array();
  310.         if ($a_varArr=pcf_templ_getVarArray($aTemplate,$varSign,$debug)) {
  311.             foreach($a_varArr['COMPLEX'as $a_cVar{      // do the comlex first to avoid overwriting simple input values
  312.                $a_ret[$a_cVar['NAME']]=$a_cVar;             // the same name which should normaly not used in templates
  313.             }
  314.             foreach($a_varArr['SIMPLE'as $s_type=>$a_typeVars{
  315.                 if ($debugecho "<p>Type$s_type</p><pre>".print_r($a_typeVars,True)."</pre>";
  316.                 foreach($a_typeVars as $s_var{
  317.                     $a_ret[$s_var]=array('NAME'=>$s_var,'TYPE'=>$s_type);
  318.                 }
  319.             }
  320.         }
  321.         return $a_ret;
  322.  
  323.     }
  324.  
  325.     /**
  326.       * replaces specialvields ($#....$)
  327.       *
  328.       * @param string $aTemplate the template to parse
  329.       * @param array $aArr the Array of the $* fields
  330.       * @param boolean $debug 
  331.       *
  332.       * @since pk-06-09-14
  333.       * @version pk-06-11-14 #GROUPSIN added
  334.       * @version pk-08-06-27 $@OCSP|PHPINCPATH$ added
  335.       *
  336.       * @return string 
  337.       *
  338.       */
  339.     function pcf_tmpl_repl_specials($aTemplate,$aArr,$debug=False{
  340.         if ($debugechoDebugMethod(__FILE__,"","pcf_tmpl_repl_specials()");
  341.  
  342.         // <pk-05-06-21> special fields
  343.         if (strstr($aTemplate,"\$#DATAARRAY\$")) {
  344.             $aTemplate=str_replace("\$#DATAARRAY\$",print_r($aArr,True),$aTemplate);
  345.         }
  346.  
  347.         if (strstr($aTemplate,"\$#PHPSESSID\$")) {
  348.             $aTemplate=str_replace("\$#PHPSESSID\$","&amp;".session_name()."=".session_id(),$aTemplate);
  349.         }
  350.         // </pk-05-06-21>
  351.  
  352.         // <pk-05-11-25> special date/time fileds
  353.         if (strstr($aTemplate,"\$#NOW\$")) {
  354.             $s_dateTime=date('Y-m-d H:i:s',time());
  355.             $aTemplate=str_replace("\$#NOW\$",$s_dateTime,$aTemplate);
  356.         }
  357.         if (strstr($aTemplate,"\$#NOW_DATE\$")) {
  358.             $s_dateTime=date('Y-m-d',time());
  359.             $aTemplate=str_replace("\$#NOW_DATE\$",$s_dateTime,$aTemplate);
  360.         }
  361.         if (strstr($aTemplate,"\$#NOW_TIME\$")) {
  362.             $s_dateTime=date('H:i:s',time());
  363.             $aTemplate=str_replace("\$#NOW_TIME\$",$s_dateTime,$aTemplate);
  364.         }
  365.  
  366.         if (strstr($aTemplate,"\$#GROUPSIN\$")) // <pk-06-11-14>
  367.             $aTemplate=str_replace("\$#GROUPSIN\$",OCSP_OBJ::currentUser()->getGroups(',',False,$debug),$aTemplate);
  368.         }
  369.         
  370.         $aTemplate str_replace("\$@OCSP|PHPINCPATH\$",__OCSP_PHPINCPATH__,$aTemplate);
  371.         return $aTemplate;
  372.         // </pk-05-11-25>
  373.     }
  374.  
  375.     /**
  376.       * replaces all simple datafields ($*FIELD$) with the value of $arr
  377.       *
  378.       * NOTE: fields which are not set are not removed
  379.       *
  380.       * @param string $aTemplate the template to parse
  381.       * @param array $aArr the Array of the $* fields
  382.       * @param bool $debug 
  383.       * @param char $varSign 
  384.       *
  385.       * @return string 
  386.       *
  387.       * @since pk-06-09-14
  388.       *
  389.       */
  390.     function pcf_tmpl_repl_values($aTemplate,$aArr,$debug=False,$varSign="\$"{
  391.         if ($debugechoDebugMethod(__FILE__,"","pcf_tmpl_repl_values()");
  392.         if (!empty($aTemplate&& is_array($aArr)) {
  393.             foreach($aArr as $s_key => $s_val{
  394.                 if (!is_array($s_val&& !is_object($s_val)) {
  395.                     $aTemplate=str_replace($varSign.PCF_TMPLVAR_ARR.$s_key.$varSign,$s_val,$aTemplate);
  396.                 }
  397.             }
  398.         }
  399.         return $aTemplate;
  400.     }
  401.  
  402.     /**
  403.       * parses a template and replaces the values found
  404.       *
  405.       * @param string $aTemplate the template to parse
  406.       * @param array $aArr the Array of the $* fields
  407.       * @param bool $debug 
  408.       * @param char $varSign 
  409.       *
  410.       * @return string 
  411.       *
  412.       * @version pk-05-01-14 DEFAULT added to if
  413.       * @version pk-05-05-12 rewrite of the function
  414.       * @version pk-06-03-20 $*-JS['|"]~....$ added
  415.       * @version pk-06-09-15 $b_valIsTemplate added
  416.       * @version pk-07-10-05 $_GET['oaxArg'] added to $_GET
  417.       * @version pk-08-06-27 ASIS
  418.       *
  419.       *  NOTE if formats are added make sure the are removed in $this::pcf_templ_getVarNames()
  420.       *
  421.       */
  422.     function pcf_tmpl_parse($aTemplate,$aArr=NULL,$debug=False,$varSign="\$"
  423.     {       
  424.         if ($debugechoDebugLine(__FILE__,__LINE__,"<p><b>pcf_tmpl_parse(" htmlspecialchars(substr($aTemplate,0,100)) " ...)");
  425.         if ($debug 1echoDebugLine(__FILE__,__LINE__,"<p>TEMPLATE:<pre>".htmlspecialchars($aTemplate)."</pre>Values: <pre>" print_r($aArr,True)"</pre>");
  426.         
  427.         if (OCSP_OBJ::isUTF8($varSign))
  428.         {
  429.             $varSign utf8_decode($varSign);
  430.             $aTemplate utf8_decode($aTemplate);
  431.         }
  432.         if (empty($aTemplate)) return False;
  433.  
  434.         // <pk-06-09-14>
  435.         $aTemplate=pcf_tmpl_repl_specials($aTemplate,$aArr,$debug);
  436.         //$aTemplate=pcf_tmpl_repl_values($aTemplate,$aArr,$debug,$varSign);
  437.         // </pk-06-09-14>
  438.  
  439.         if (!strstr($aTemplate,$varSign))
  440.         {
  441.             return $aTemplate;
  442.         }
  443.         
  444.         $varArr=pcf_templ_getVarArray($aTemplate,$varSign,$debug,False)// <pk-06-03-20 />
  445.  
  446.         if ($debugechoDebugLine(__FILE__,__LINE__,"\$varArr: <pre>" print_r($varArr,True"/<pre>");
  447.         
  448.         // first replace all simpleVars
  449.         foreach($varArr['SIMPLE'as $type => $varLst{
  450.             foreach($varLst as $name{
  451.                 if (substr($name,0,1)=="."// <pk-06-09-15
  452.                     $name=substr($name,1);
  453.                     $b_valIsTemplate=True;
  454.                 else {
  455.                     $b_valIsTemplate=False;
  456.                 }
  457.                 if (strstr($name,"|")) {        // multidimensonal array value
  458.                     $subArrKeys=explode("|",$name);
  459.                     $idx=$subArrKeys[0];
  460.                     unset($subArrKeys[0])// is $idx
  461.                     $isSubArr=True;
  462.                 else {
  463.                     $idx=$name;
  464.                     $isSubArr=False;
  465.                 }
  466.                 switch ($type{
  467.                     case PCF_TMPLVAR_GET:
  468.                         // <pk-07-10-05>
  469.                         $val=(isset($_GET['oaxArg'][$idx]$_GET['oaxArg'][$idx"");
  470.                         $val=(isset($_GET[$idx]$_GET[$idx$val);
  471.                         // </pk-07-10-05>
  472.                         break;
  473.                     case PCF_TMPLVAR_BASE64// <pk-05-08-03 />
  474.                     case PCF_TMPLVAR_ARR:
  475.                         if ((substr($idx,0,1== "#"&& strstr($name,"|")) $idx=substr($idx,1);
  476.  
  477.                         if ((substr($idx,0,2== "-D"&& strstr($name,"~")) {
  478.                             // <pk-05-06-21> Dateformat added
  479.                             require_once __OCSP_PHPINCPATH__."common/OCSP_DATE.phpclass";
  480.                             $date=new OCSP_DATE();
  481.                             if ($nArr=explode("~",$name,2)) {
  482.                                 $date->setDateStr($aArr[$nArr[1]]);
  483.                                 $val=$date->dateStr(substr($nArr[0],2),$debug);
  484.                             }
  485.                             // </pk-05-06-21>
  486.                         else if ((substr($idx,0,2== "-P"&& strstr($name,"~")) {
  487.                             // <pk-05-06-24> Printf
  488.                             require_once __OCSP_PHPINCPATH__."common/pcf_Date.phpclass";
  489.                             $date=new pcf_Date();
  490.                             if ($nArr=explode("~",$name,2)) {
  491.                                 if ($debugecho "<p>sprintf(".substr($nArr[0],2).",".$aArr[$nArr[1]].")</p>";
  492.                                 $val=sprintf(substr($nArr[0],2),$aArr[$nArr[1]]);
  493.                             }
  494.                             // </pk-05-06-24>
  495.                         else if ((substr($idx,0,3== "-JS")  && strstr($name,"~")) //<pk-06-03-20>
  496.                             $c_escapeChar=substr($idx,3,1);
  497.                             if ($nArr=explode("~",$name,2)) {
  498.                                 if ($debugecho "<p>sprintf(".$c_escapeChar.",".$aArr[$nArr[1]].")</p>";
  499.                                 $val=pcf_makeJS_QuotedString($aArr[$nArr[1]],$c_escapeChar,$debug);
  500.                             }
  501.                         else if (substr($idx,0,1== "!"{
  502.                             // <pk-07-12-14> force intval 
  503.                             $idx=substr($idx,1);
  504.                             if (isset($aArr[$idx])) {
  505.                                 $val=intval($aArr[$idx]);
  506.                             else {
  507.                                 $val=0;
  508.                             }                            
  509.                         else if (isset($aArr[$idx])) {
  510.                             $val=$aArr[$idx];
  511.                         else {
  512.                             $val="";
  513.                         }
  514.                         
  515.                         if ($type == PCF_TMPLVAR_BASE64// <pk-05-08-03>
  516.                             $val=urlencode(base64_encode($val));
  517.                         }
  518.                         break;
  519.                     case PCF_TMPLVAR_SESSION:
  520.                         $val=(isset($_SESSION[$idx]$_SESSION[$idx"")// <pk-07-08-01> E_ALL
  521.                         break;
  522.                     case PCF_TMPLVAR_POST:
  523.                         $val=$_POST[$idx];
  524.                         break;
  525.                     case PCF_TMPLVAR_SERVER:
  526.                         $val=$_SERVER[$idx];
  527.                         break;
  528.                     case PCF_TMPLVAR_GLOBALS:
  529.                         // <pk-07-10-14>
  530.  
  531.                         $arr_tmpSubKeys Null;
  532.                         if ($val OCSP_CONF::getInstance()->getTemplateValue($name,$arr_tmpSubKeys,$debug))
  533.                         {
  534.                             $subArrKeys $arr_tmpSubKeys;
  535.                         else {
  536.                             global $OCSP_CONF,$OCSP_VAL;
  537.                             if (isset($OCSP_CONF[$idx]))
  538.                             {
  539.                                 $val=$OCSP_CONF[$idx];
  540.                             else if (isset($OCSP_VAL[$idx])) {
  541.                                 $val=$OCSP_VAL[$idx];
  542.                             else {
  543.                                 $val=$GLOBALS[$idx];
  544.                             }
  545.                         }
  546.                         // </pk-07-10-14>
  547.                         break;
  548.                     case PCF_TMPLVAR_INCLUDE:   // <pk-05-08-29>
  549.                         $val=pcf_tmpl_parseFile($idx,$aArr,$debug,$varSign);
  550.                         break;
  551.                     case PCF_TMPLVAR_ASIS:
  552.                         // <pk-08-06-27 />
  553.                            $val=substr($name,1,-1);
  554.                            $name=substr($name,1);
  555.                         if ($debugechoDebugLine(__FILE__,__LINE__,$name ": " $val);   
  556.                            $b_valIsTemplate False;
  557.                         break;
  558.                         
  559.                     default:
  560.                         $val="";
  561.                 }
  562.  
  563.                 if ($isSubArr && sizeof($subArrKeys)) {
  564.                     foreach($subArrKeys as $subKey{
  565.                         if (!is_array($val)) $val=unserialize($val);
  566.                         $val=$val[$subKey];
  567.                     }
  568.                 }
  569.  
  570.                 if ($b_valIsTemplate// pk-06-09-15
  571.                     $val=pcf_tmpl_parse($val,$aArr,$debug,$varSign);
  572.                     $name=".".$name;
  573.                 }
  574.  
  575.                 $aTemplate=str_replace($varSign.$type.$name.$varSign,$val,$aTemplate);
  576.                 foreach($varArr['COMPLEX'as $key=>$cVar{
  577.                     $varArr['COMPLEX'][$key]['REPLACE']=str_replace($varSign.$type.$name.$varSign,$val,$varArr['COMPLEX'][$key]['REPLACE']);
  578.                 }
  579.             }
  580.         }
  581.  
  582.         foreach($varArr['COMPLEX'as $cVar{
  583.             switch($cVar['TYPE']{
  584.                 case PCF_TMPLVAR_IF:
  585.                     $condVal=$aArr[$cVar['NAME']];
  586.                     $condStr=pcf_tmpl_parse($cVar['CMD'],$aArr,$debug,$varSign)// parse the all condition to resolve enclosed if statements
  587.                     if ($debugecho "<h2>condStr:</h2><<pre style=\"background-color:red\">".htmlspecialchars($condStr)."</pre>";
  588.                     if (!($tmpArr=explode("|",$condStr))) // $condStr should be free of any vars we can split it
  589.                         $tmpArr=array($condStr);            // only one Condition
  590.                     }
  591.                     reset($tmpArr);$found=False;$val="";$default="";
  592.                     while((!$found&& (list($idx,$cmdStr)=each($tmpArr))) {
  593.                         if ($cmd=explode("~",$cmdStr)) {
  594.                             switch($cmd[0]{
  595.                                 case $condVal:
  596.                                     $val=$cmd[1];
  597.                                     $found=True;
  598.                                     break;
  599.                                 case "NULL":
  600.                                     if (empty($condVal)) {
  601.                                         $val=$cmd[1];
  602.                                         $found=True;
  603.                                     }
  604.                                     break;
  605.                                 case "NOTNULL":
  606.                                     if (!empty($condVal)) {
  607.                                         $val=$cmd[1];
  608.                                         $found=True;
  609.                                     }
  610.                                     break;
  611.                                 case "DEFAULT":
  612.                                     $default=$cmd[1];
  613.                                     break;
  614.                             }
  615.                         else {
  616.                             $default=$cmdStr;
  617.                         }
  618.                     }
  619.                     if (!$found$val=$default;
  620.                     if ($debugecho "<pre>REPLACE:\n".htmlspecialchars($cVar['REPLACE'])."\nwith:\n".htmlspecialchars($val)."</pre>";
  621.                     $aTemplate=str_replace($cVar['REPLACE'],$val,$aTemplate);
  622.                     break;
  623.                 case PCF_TMPLVAR_DBLOOKUP// <pk-05-09-20>
  624.                     $query="SELECT ".pcf_tmpl_parse($cVar['CMD'],$aArr,$debug,$varSign);
  625.                     if ($debugecho "<p>Query: ".$val['NAME'].":<br />".$query."</p>";
  626.                     if (!pcf_is_instance_of($GLOBALS['OCSP_OBJ']['USRDB'],'OCSP_DB'))
  627.                     {
  628.                         $SEC_REQ['DBCONN']=True;
  629.                         include __OCSP_PHPINCPATH__."user/checkuser.phpinc";
  630.                     }
  631.                     if (OCSP_OBJ::defaultReadDBObj()->isConnected())
  632.                     {
  633.                         $dbVal=OCSP_OBJ::defaultReadDBObj()->quickQuery($query,0);
  634.                         $aTemplate str_replace($cVar['REPLACE'],$dbVal,$aTemplate);
  635.                     else {
  636.                         ocsp_logError(__FILE__,__LINE__,"no database connection",E_WARNING);
  637.                     }
  638.                     break;
  639.             }
  640.         }
  641.  
  642.  
  643.  
  644.         return $aTemplate;
  645.     }
  646.  
  647.  
  648.     /**
  649.       * reads $aFile in $GLOBALS['PROJECT']['TEMPLATEPATH']
  650.       * parses the template with $aArr and returns it
  651.       * returns if the template file was found
  652.       *
  653.       * @param string $aFileName 
  654.       * @param array $aArr 
  655.       * @param boolean $debug 
  656.       * @param char $varSign 
  657.       *
  658.       * @return string 
  659.       *
  660.       * @since pk-05-02-14
  661.       * @version pk-05-08-29
  662.       * @version pk-07-08-06 enable admin-tmpl
  663.       *
  664.       * @var string $s_tmpl 
  665.       *
  666.       */
  667.     function pcf_tmpl_parseFile($aFileName,$aArr=NULL,$debug=False,$varSign='$'{
  668.         if ($debugechoDebugLine(__FILE__,__LINE__,"<b>pcf_tmpl_parseFile($aFileName,\$aArr,\$debug)</b>");
  669.  
  670.         if (($aFileName{0}==_OCSP_DIRSEP_&& (substr($aFileName,-5== ".html"))
  671.         {
  672.             $str_tmplFile=$aFileName;
  673.         else {
  674.             $str_tmplFile=$GLOBALS['OCSP']['TEMPLATEPATH'].$aFileName;
  675.         }
  676.  
  677.         if (!($s_tmpl=@file_get_contents($str_tmplFile))) {
  678.             // the file is not in the template path
  679.             if ($debugecho "<p>FILE NOT FOUND in TEMPLATEPATH$aFileName</p>\n";
  680.             return NULL;
  681.         }
  682.         if ($debugecho "<pre>".htmlspecialchars($s_tmpl)."</pre>";
  683.         return pcf_tmpl_parse($s_tmpl,$aArr,$debug,$varSign);
  684.     }
  685.  
  686.     /**
  687.       * parses am admin templdate (under $GLOBALS['OCSP']['ADMINPATH'] ../admin-tmpl)
  688.       *
  689.       * @param string $aFileName 
  690.       * @param array $aArr 
  691.       * @param boolean $debug 
  692.       * @param char $varSign 
  693.       *
  694.       * @return string 
  695.       */
  696.     function pcf_tmpl_parseAdminFile($aFileName,$aArr=NULL,$debug=False,$varSign='$'{
  697.         if ($debugecho "<p><b>pcf_tmpl_parseFile($aFileName,\$aArr,\$debug)</b> (".__FILE__.")</p>";
  698.  
  699.         if (!($s_tmpl=@file_get_contents(substr($GLOBALS['OCSP']['ADMINPATH'],0,-1)."/".$aFileName))) {
  700.             // the file is not in the template path
  701.             if ($debugecho "<p>FILE NOT FOUND in TEMPLATEPATH: ".substr($GLOBALS['OCSP']['ADMINPATH'],0,-1)."</p>\n";
  702.             return NULL;
  703.         }
  704.         if ($debugecho "<pre>".htmlspecialchars($s_tmpl)."</pre>";
  705.         return pcf_tmpl_parse($s_tmpl,$aArr,$debug,$varSign);
  706.     }
  707.  
  708.  
  709.     /**
  710.       * echos pcf_tmpl_parseFile
  711.       *
  712.       * @param string $aFileName 
  713.       * @param array $aArr 
  714.       * @param boolean $debug 
  715.       *
  716.       * @return string 
  717.       *
  718.       * @version pk-05-02-14 now calls pcf_tmpl_parseFile
  719.       *
  720.       */
  721.     function pcf_tmpl_write($aFileName,$aArr=NULL,$debug=False{
  722.         if ($debugecho "<p><b>pcf_tmpl_write($aFileName,\$aArr,\$debug)</b> (".__FILE__."</p><blockquote>";
  723.  
  724.         if ($str=pcf_tmpl_parseFile($aFileName,$aArr,$debug)) {
  725.             if ($debugecho "</blockquote>";
  726.             echo $str;
  727.             return True;
  728.         else {
  729.             if ($debugecho "</blockquote>";
  730.             return False;
  731.         }
  732.     }
  733.  
  734.     /**
  735.       * reads $aFile in $GLOBALS['PROJECT']['TEMPLATEPATH']
  736.       * parses the template with $aArr and returns it as string
  737.       *
  738.       * @param string $aFileName 
  739.       * @param array $aArr 
  740.       * @param bool $debug 
  741.       * @param bool $fromTmplDir 
  742.       *
  743.       * @return string 
  744.       *
  745.       * @version pk-04-12-21
  746.       *
  747.       */
  748.     function pcf_tmpl_get($aFileName,$aArr=NULL,$debug=False,$fromTmplDir=True{
  749.         if ($debugecho "<p><b>pcf_tmpl_get($aFileName,$aArr,$debug)</b></p>";
  750.  
  751.         /* <pk-04-12-21> */
  752.         if ($fromTmplDir$fileName=$GLOBALS['PROJECT']['TEMPLATEPATH'].$aFileName;
  753.         else $fileName=$aFileName;
  754.         /* </pk-04-12-21> */
  755.  
  756.         if ($fp=@fopen($fileName,"r")) {
  757.             $tmpl="";
  758.             while(!feof($fp)) {$tmpl.=fgets($fp,4096);}
  759.             fclose($fp);
  760.             if ($debugecho "<pre>".htmlspecialchars($tmpl)."</pre>";
  761.             if (!isset($aArr['_TMPLDIR'])) $aArr['_TMPLDIR']=dirname($aFileName);
  762.             return pcf_tmpl_parse($tmpl,$aArr,$debug);
  763.         else {
  764.             if ($debugecho "<p>Could not open: ".$GLOBALS['PROJECT']['TEMPLATEPATH'].$aFileName."</p>";
  765.             return "";
  766.         }
  767.     }
  768.  
  769.  
  770.     /**
  771.       * converts german specialchars into theire
  772.       * html representation (&szlig ....)
  773.       *
  774.       * @param string $aText 
  775.       * @param boolean $debug 
  776.       *
  777.       * @return string 
  778.       *
  779.       * @since pk-04-10-05
  780.       *
  781.       */
  782.     function pcf_tmpl_htmlgermChars($aText,$debug=False{
  783.         if ($debugecho "<p><b>pcf_tmpl_htmlgermChars(\$aText,$debug)</b></p>";
  784.  
  785.         $aText=str_replace("ß","&szlig;",$aText);
  786.         $aText=str_replace("Ä","&Auml;",$aText);
  787.         $aText=str_replace("ä","&auml;",$aText);
  788.         $aText=str_replace("Ö","&Ouml;",$aText);
  789.         $aText=str_replace("ö","&ouml;",$aText);
  790.         $aText=str_replace("Ü","&Uuml;",$aText);
  791.         $aText=str_replace("ü","&uuml;",$aText);
  792.         $aText=str_replace("€","&euro;",$aText);
  793.  
  794.         return $aText;
  795.     }
  796.  
  797. ?>

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