Source for file pcf_templates.phpinc
Documentation is available at pcf_templates.phpinc
* template parse functions
* @project Open CSP-Management
* @author Peter Krebs <p.krebs@lvus.at>
* @copyright (c) 2002-2003 by Peter Krebs and Landesverlag Unternehmensservice
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version pk-04-12-21 documentation changed
define ('PCF_TMPLVAR_GET',"^");
define ('PCF_TMPLVAR_ARR',"*");
define ('PCF_TMPLVAR_SESSION',"~");
define ('PCF_TMPLVAR_POST',"§");
define ('PCF_TMPLVAR_SERVER',"°");
* Global Array $GLOBALS values
define ('PCF_TMPLVAR_GLOBALS',"@");
define ('PCF_TMPLVAR_IF',"?");
define ('PCF_TMPLVAR_DBLOOKUP',"%");
define ('PCF_TMPLVAR_ASIS',"\\");
define ('PCF_TMPLVAR_BASE64',"ß");
define ('PCF_TMPL_MAXVARLENGTH',128);
define ('PCF_TMPLVAR_INCLUDE',"<");
* splits the template into the replace string of a complex var
* and the rest of the template
* example "LINKTYPE....?$ asdfsadf " will return "$?LINKTYPE....?$" and set $aTemplate to " asdfsadf "
* @param string $aTemplate
if ($debug) echo
"<p><b>pcf_tmpl_findComplexReplace(...,type=$type,varsign=$varSign)</b> (".__FILE__.
")</p>";
if ($debug) echo
"<blockquote><pre styl=\"font-size:60%\">".
htmlspecialchars($aTemplate).
"</pre></blockquote>";
if ($tmp=
strstr($aTemplate,$type.
$varSign)) { // find the end tag
$aTemplate=
substr($tmp,2); // cut the end tag from $aTemplate;
while (strstr($ret2,$varSign.
$type) &&
($i <
100)) { // we have an enclosed tag of the same type
// and got the end of the inner type
$i++
; // $i < 100 to avoid endless loop
if ($tmp=
strstr($aTemplate,$type.
$varSign)) { // find the end tag of the outer type
$aTemplate=
substr($tmp,2); // cut the end tag from $aTemplate;
$ret.=
$type.
$varSign.
$ret2;
return $varSign.
$type.
$ret.
$type.
$varSign;
* returns an array with fields and types
* array[SIMPLE|COMPLEX][TYPE][name]
* each array element has a NAME, a TYPE and a replace string
* note COMPLEX elements are not splitted up
* @param string $aTemplate
* @version pk-05-06-27 BUGFIX forgotten to add PCF_TMPLVAR_SERVER to simple type :-)
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"<p><b>pcf_templ_getVarArray(...,varsign=$varSign)</b></p>");
$varArr=
array('SIMPLE'=>
array(),'COMPLEX'=>
array());
while($aTemplate=
strstr($aTemplate,$varSign)) {
$type =
substr($aTemplate,1,1);
$aTemplate=
substr($aTemplate,2);
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"<p>Type: $type found</p>");
// simple types ends with $varSign and do not have other vars included
$varArr['SIMPLE'][$type][$tmp[0]]=
$tmp[0];
$aTemplate=
(isset
($tmp[1]) ?
$tmp[1] :
""); // <pk-07-01-09 /> E_ALL
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"Type: PCF_TMPLVAR_ASIS");
$str_tmpTmpl =
$aTemplate;
if ($arr_tmp =
explode($varSign,$str_tmpTmpl,2))
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"\$str_replace: " .
$str_replace);
$aTemplate =
$str_tmpTmpl;
$str_replace .=
substr($arr_tmp[0],0,-
1);
$aTemplate =
$str_tmpTmpl;
} else if (empty($arr_tmp[1])) {
break(2); // the loop and switch
} else if (empty($arr_tmp[0])) {
$str_replace .=
$arr_tmp[0];
$str_tmpTmpl =
$arr_tmp[1];
break(2); // the loop and switch
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"\$str_replace: " .
$str_replace .
"<br />\n\$aTemplate: " .
$aTemplate);
$varArr['SIMPLE'][$type][$str_replace] =
$str_replace;
if ($tmp=
explode("|",$replStr,2)) {
while (isset
($varArr['COMPLEX'][$idx]) &&
($varArr['COMPLEX'][$idx]['REPLACE'] !=
$replStr)) {
$varArr['COMPLEX'][$idx]=
array(
'CMD' =>
substr($tmp[1],0,-
2) // remove the end sign
$s_query=
substr($s_query,0,-
2);
while (isset
($varArr['COMPLEX'][$idx]) &&
($varArr['COMPLEX'][$idx]['REPLACE'] !=
$replStr)) {
$varArr['COMPLEX'][$idx]=
array(
if ($debug) echo
"<hr><p><b>TYPE: $type</b></p><blockquote style=\"font-size:50%\">".
htmlspecialchars($aTemplate).
"</blockquote>";
* returns an array with variable names
* @param string $aTemplate
if ($debug) echoDebugMethod(__FILE__
,"","pcf_templ_getVarNamesArr()","",0);
// check simple vars for format information
foreach($a_varArr['SIMPLE'] as $type =>
$varLst) {
foreach($varLst as $name) {
$a_Names[$type][]=
$substr($name,1);
$a_Names[$type][]=
$nArr[1];
foreach($a_varArr['COMPLEX'] as $a_cVar) {
$a_Names[$a_cVar['TYPE']]=
$a_cVar['NAME'];
* returns an array with fields found in the template
* each array element has a NAME and a TYPE index
* @param string $aTemplate
if ($debug) echoDebug(__FILE__
,"<p><b>pcf_tmpl_getVars(...)</b></p>");
foreach($a_varArr['COMPLEX'] as $a_cVar) { // do the comlex first to avoid overwriting simple input values
$a_ret[$a_cVar['NAME']]=
$a_cVar; // the same name which should normaly not used in templates
foreach($a_varArr['SIMPLE'] as $s_type=>
$a_typeVars) {
if ($debug) echo
"<p>Type: $s_type</p><pre>".
print_r($a_typeVars,True).
"</pre>";
foreach($a_typeVars as $s_var) {
$a_ret[$s_var]=
array('NAME'=>
$s_var,'TYPE'=>
$s_type);
* replaces specialvields ($#....$)
* @param string $aTemplate the template to parse
* @param array $aArr the Array of the $* fields
* @version pk-06-11-14 #GROUPSIN added
* @version pk-08-06-27 $@OCSP|PHPINCPATH$ added
// <pk-05-06-21> special fields
if (strstr($aTemplate,"\$#DATAARRAY\$")) {
if (strstr($aTemplate,"\$#PHPSESSID\$")) {
// <pk-05-11-25> special date/time fileds
if (strstr($aTemplate,"\$#NOW\$")) {
$aTemplate=
str_replace("\$#NOW\$",$s_dateTime,$aTemplate);
if (strstr($aTemplate,"\$#NOW_DATE\$")) {
$aTemplate=
str_replace("\$#NOW_DATE\$",$s_dateTime,$aTemplate);
if (strstr($aTemplate,"\$#NOW_TIME\$")) {
$aTemplate=
str_replace("\$#NOW_TIME\$",$s_dateTime,$aTemplate);
if (strstr($aTemplate,"\$#GROUPSIN\$")) { // <pk-06-11-14>
* replaces all simple datafields ($*FIELD$) with the value of $arr
* NOTE: fields which are not set are not removed
* @param string $aTemplate the template to parse
* @param array $aArr the Array of the $* fields
if (!empty($aTemplate) &&
is_array($aArr)) {
foreach($aArr as $s_key =>
$s_val) {
* parses a template and replaces the values found
* @param string $aTemplate the template to parse
* @param array $aArr the Array of the $* fields
* @version pk-05-01-14 DEFAULT added to if
* @version pk-05-05-12 rewrite of the function
* @version pk-06-03-20 $*-JS['|"]~....$ added
* @version pk-06-09-15 $b_valIsTemplate added
* @version pk-07-10-05 $_GET['oaxArg'] added to $_GET
* @version pk-08-06-27 ASIS
* NOTE if formats are added make sure the are removed in $this::pcf_templ_getVarNames()
function pcf_tmpl_parse($aTemplate,$aArr=
NULL,$debug=
False,$varSign=
"\$")
if (empty($aTemplate)) return False;
//$aTemplate=pcf_tmpl_repl_values($aTemplate,$aArr,$debug,$varSign);
if (!strstr($aTemplate,$varSign))
// first replace all simpleVars
foreach($varArr['SIMPLE'] as $type =>
$varLst) {
foreach($varLst as $name) {
if (substr($name,0,1)==
".") { // <pk-06-09-15
if (strstr($name,"|")) { // multidimensonal array value
unset
($subArrKeys[0]); // is $idx
$val=
(isset
($_GET['oaxArg'][$idx]) ?
$_GET['oaxArg'][$idx] :
"");
$val=
(isset
($_GET[$idx]) ?
$_GET[$idx] :
$val);
// <pk-05-06-21> Dateformat added
$date->setDateStr($aArr[$nArr[1]]);
$val=
$date->dateStr(substr($nArr[0],2),$debug);
} else if ((substr($idx,0,2) ==
"-P") &&
strstr($name,"~")) {
if ($debug) echo
"<p>sprintf(".
substr($nArr[0],2).
",".
$aArr[$nArr[1]].
")</p>";
} else if ((substr($idx,0,3) ==
"-JS") &&
strstr($name,"~")) { //<pk-06-03-20>
$c_escapeChar=
substr($idx,3,1);
if ($debug) echo
"<p>sprintf(".
$c_escapeChar.
",".
$aArr[$nArr[1]].
")</p>";
} else if (substr($idx,0,1) ==
"!") {
// <pk-07-12-14> force intval
if (isset
($aArr[$idx])) {
} else if (isset
($aArr[$idx])) {
$val=
(isset
($_SESSION[$idx]) ?
$_SESSION[$idx] :
""); // <pk-07-08-01> E_ALL
$subArrKeys =
$arr_tmpSubKeys;
if (isset
($OCSP_CONF[$idx]))
} else if (isset
($OCSP_VAL[$idx])) {
if ($debug) echoDebugLine(__FILE__
,__LINE__
,$name .
": " .
$val);
$b_valIsTemplate =
False;
if ($isSubArr &&
sizeof($subArrKeys)) {
foreach($subArrKeys as $subKey) {
if ($b_valIsTemplate) { // pk-06-09-15
$aTemplate=
str_replace($varSign.
$type.
$name.
$varSign,$val,$aTemplate);
foreach($varArr['COMPLEX'] as $key=>
$cVar) {
$varArr['COMPLEX'][$key]['REPLACE']=
str_replace($varSign.
$type.
$name.
$varSign,$val,$varArr['COMPLEX'][$key]['REPLACE']);
foreach($varArr['COMPLEX'] as $cVar) {
$condVal=
$aArr[$cVar['NAME']];
$condStr=
pcf_tmpl_parse($cVar['CMD'],$aArr,$debug,$varSign); // parse the all condition to resolve enclosed if statements
if ($debug) echo
"<h2>condStr:</h2><<pre style=\"background-color:red\">".
htmlspecialchars($condStr).
"</pre>";
if (!($tmpArr=
explode("|",$condStr))) { // $condStr should be free of any vars we can split it
$tmpArr=
array($condStr); // only one Condition
reset($tmpArr);$found=
False;$val=
"";$default=
"";
while((!$found) &&
(list
($idx,$cmdStr)=
each($tmpArr))) {
if (!$found) $val=
$default;
$aTemplate=
str_replace($cVar['REPLACE'],$val,$aTemplate);
if ($debug) echo
"<p>Query: ".
$val['NAME'].
":<br />".
$query.
"</p>";
$aTemplate =
str_replace($cVar['REPLACE'],$dbVal,$aTemplate);
ocsp_logError(__FILE__
,__LINE__
,"no database connection",E_WARNING);
* reads $aFile in $GLOBALS['PROJECT']['TEMPLATEPATH']
* parses the template with $aArr and returns it
* returns if the template file was found
* @param string $aFileName
* @version pk-07-08-06 enable admin-tmpl
if ($debug) echoDebugLine(__FILE__
,__LINE__
,"<b>pcf_tmpl_parseFile($aFileName,\$aArr,\$debug)</b>");
$str_tmplFile=
$aFileName;
$str_tmplFile=
$GLOBALS['OCSP']['TEMPLATEPATH'].
$aFileName;
// the file is not in the template path
if ($debug) echo
"<p>FILE NOT FOUND in TEMPLATEPATH: $aFileName</p>\n";
* parses am admin templdate (under $GLOBALS['OCSP']['ADMINPATH'] ../admin-tmpl)
* @param string $aFileName
if ($debug) echo
"<p><b>pcf_tmpl_parseFile($aFileName,\$aArr,\$debug)</b> (".__FILE__.
")</p>";
// the file is not in the template path
if ($debug) echo
"<p>FILE NOT FOUND in TEMPLATEPATH: ".
substr($GLOBALS['OCSP']['ADMINPATH'],0,-
1).
"</p>\n";
* echos pcf_tmpl_parseFile
* @param string $aFileName
* @version pk-05-02-14 now calls pcf_tmpl_parseFile
if ($debug) echo
"<p><b>pcf_tmpl_write($aFileName,\$aArr,\$debug)</b> (".__FILE__.
"</p><blockquote>";
if ($debug) echo
"</blockquote>";
if ($debug) echo
"</blockquote>";
* reads $aFile in $GLOBALS['PROJECT']['TEMPLATEPATH']
* parses the template with $aArr and returns it as string
* @param string $aFileName
* @param bool $fromTmplDir
function pcf_tmpl_get($aFileName,$aArr=
NULL,$debug=
False,$fromTmplDir=
True) {
if ($debug) echo
"<p><b>pcf_tmpl_get($aFileName,$aArr,$debug)</b></p>";
if ($fromTmplDir) $fileName=
$GLOBALS['PROJECT']['TEMPLATEPATH'].
$aFileName;
else $fileName=
$aFileName;
if ($fp=
@fopen($fileName,"r")) {
if (!isset
($aArr['_TMPLDIR'])) $aArr['_TMPLDIR']=
dirname($aFileName);
if ($debug) echo
"<p>Could not open: ".
$GLOBALS['PROJECT']['TEMPLATEPATH'].
$aFileName.
"</p>";
* converts german specialchars into theire
* html representation (ß ....)
if ($debug) echo
"<p><b>pcf_tmpl_htmlgermChars(\$aText,$debug)</b></p>";
Documentation generated on Thu, 08 Jan 2009 17:47:50 +0100 by phpDocumentor 1.4.0a2