Skip to content

Commit ccdf702

Browse files
author
Marius Cramer
committed
Changed: Template class tag processing. Tags can now contain any amount of attributes.
1 parent daf67b7 commit ccdf702

File tree

2 files changed

+88
-101
lines changed

2 files changed

+88
-101
lines changed

interface/lib/classes/tpl.inc.php

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ private function _getData ($tmplfile, $do_eval=false)
864864

865865
$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
866866
$regex.= 'tmpl_([\w]+)\s*';
867-
$regex.= '(?:';
867+
$regex.= '((?:(?:';
868868
$regex.= '(?:';
869869
$regex.= '(name|format|escape|op|value|file)';
870870
$regex.= '\s*=\s*';
@@ -873,30 +873,10 @@ private function _getData ($tmplfile, $do_eval=false)
873873
$regex.= '((?<=[\"\'])';
874874
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
875875
$regex.= '[\"\']?';
876-
$regex.= ')?\s*';
877-
$regex.= '(?:';
878-
$regex.= '(?:';
879-
$regex.= '(name|format|escape|op|value)';
880-
$regex.= '\s*=\s*';
881-
$regex.= ')';
882-
$regex.= '(?:[\"\'])?';
883-
$regex.= '((?<=[\"\'])';
884-
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
885-
$regex.= '[\"\']?';
886-
$regex.= ')?\s*';
887-
$regex.= '(?:';
888-
$regex.= '(?:';
889-
$regex.= '(name|format|escape|op|value)';
890-
$regex.= '\s*=\s*';
891-
$regex.= ')';
892-
$regex.= '(?:[\"\'])?';
893-
$regex.= '((?<=[\"\'])';
894-
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
895-
$regex.= '[\"\']?';
896-
$regex.= ')?\s*';
876+
$regex.= ')?\s*)*?)';
897877
$regex.= '(?:>|\/>|}|-->){1}';
898-
$regex.= '([\r\n|\n|\r])?/i';
899-
$data = preg_replace_callback($regex, array($this, _parseTag), $data);
878+
$regex.= '/i';
879+
$data = preg_replace_callback($regex, array($this, '_parseTag'), $data);
900880

901881
if ($this->_cache) { // add cache if need be
902882
$this->_createCache($data);
@@ -1026,8 +1006,7 @@ private function _arrayBuild($arr)
10261006
* @access private
10271007
* @return string used for eval'ing
10281008
*/
1029-
private function _parseIf($varname, $value = null, $op = null, $namespace = null)
1030-
{
1009+
function _parseIf ($varname, $value=null, $op=null, $namespace=null, $format=null) {
10311010
if (isset($namespace)) $namespace = substr($namespace, 0, -1);
10321011
$comp_str = ''; // used for extended if statements
10331012

@@ -1065,9 +1044,19 @@ private function _parseIf($varname, $value = null, $op = null, $namespace = null
10651044
}
10661045
}
10671046
if ($this->OPTIONS['GLOBAL_VARS'] && empty($namespace)) {
1068-
return '(('.$retstr.'[\''.$varname.'\'] !== null) ? '.$retstr.'[\''.$varname.'\'] : $this->_vars[\''.$varname.'\'])'.$comp_str;
1069-
} else {
1070-
return $retstr."['".$varname."']".$comp_str;
1047+
$retstr = '(('.$retstr.'[\''.$varname.'\'] !== null) ? '.$retstr.'[\''.$varname.'\'] : $this->_vars[\''.$varname.'\'])';
1048+
if(isset($format) && isset($value) && $format == 'version') {
1049+
return 'version_compare(' . $retstr . ', \'' . $value . '\', ' . (!empty($op) ? $op : '==') . ')';
1050+
} else {
1051+
return $retstr.$comp_str;
1052+
}
1053+
}
1054+
else {
1055+
if(isset($format) && isset($value) && $format == 'version') {
1056+
return 'version_compare(' . $retstr."['".$varname."']" . ', \'' . $value . '\', ' . (!empty($op) ? $op : '==') . ')';
1057+
} else {
1058+
return $retstr."['".$varname."']".$comp_str;
1059+
}
10711060
}
10721061
}
10731062

@@ -1186,28 +1175,38 @@ private function _parseTag ($args)
11861175
$wholetag = $args[0];
11871176
$openclose = $args[1];
11881177
$tag = strtolower($args[2]);
1189-
$newline = $args[9];
1190-
1191-
if ($tag == 'else') return '<?php } else { ?>'.$newline;
1178+
1179+
if ($tag == 'else') return '<?php } else { ?>';
11921180
if ($tag == 'tmpl_include') return $wholetag; // ignore tmpl_include tags
11931181

11941182
if (preg_match("/^<\/|{\/|<!--\/$/s", $openclose) || preg_match("/^end[if|loop|unless|comment]$/", $tag)) {
11951183
if ($tag == 'loop' || $tag == 'endloop') array_pop($this->_namespace);
11961184
if ($tag == 'comment' || $tag == 'endcomment') {
1197-
return '<?php */ ?>'.$newline;
1198-
} else {
1199-
return '<?php } ?>'.$newline;
1185+
return '<?php */ ?>';
1186+
}
1187+
else {
1188+
return '<?php } ?>';
12001189
}
12011190
}
12021191

1203-
//* arrange attributes
1204-
for ($i=3; $i < 8; $i=($i+2)) {
1205-
if (empty($args[$i]) && empty($args[($i+1)])) break;
1206-
$key = (empty($args[$i])) ? 'name' : strtolower($args[$i]);
1207-
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
1208-
$$key = $args[($i+1)];
1192+
// arrange attributes
1193+
$tmp_atts = $args[3];
1194+
$atts = preg_split('/\s+/', $tmp_atts);
1195+
foreach($atts as $att) {
1196+
$regex = '/(?:';
1197+
$regex.= '(name|format|escape|op|value|file)';
1198+
$regex.= '\s*=\s*';
1199+
$regex.= ')?';
1200+
$regex.= '(?:[\"\'])?';
1201+
$regex.= '((?<=[\"\'])';
1202+
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
1203+
$regex.= '[\"\']?/';
1204+
if(preg_match($regex, $att, $match)) {
1205+
$key = (empty($match[1])) ? 'name' : strtolower($match[1]);
1206+
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
1207+
$$key = $match[2];
1208+
}
12091209
}
1210-
12111210
$var = ($this->OPTIONS['CASELESS']) ? strtolower($name) : $name;
12121211

12131212
if ($this->_debug && !empty($var)) {
@@ -1229,37 +1228,47 @@ private function _parseTag ($args)
12291228
if (empty($escape) && (!empty($this->OPTIONS['DEFAULT_ESCAPE']) && strtolower($this->OPTIONS['DEFAULT_ESCAPE']) != 'none')) {
12301229
$escape = strtolower($this->OPTIONS['DEFAULT_ESCAPE']);
12311230
}
1232-
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace)." ?>$newline";
1231+
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace).' ?>'."\n";
1232+
break;
12331233

12341234
case 'if':
1235-
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace) .') { ?>'.$newline;
1235+
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
1236+
break;
12361237

12371238
case 'unless':
1238-
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace) .') { ?>'.$newline;
1239+
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
1240+
break;
12391241

12401242
case 'elseif':
1241-
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace) .') { ?>'.$newline;
1243+
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
1244+
break;
12421245

12431246
case 'loop':
1244-
return '<?php '. $this->_parseLoop($var) .'?>'.$newline;
1247+
return '<?php '. $this->_parseLoop($var) .'?>';
1248+
break;
12451249

12461250
case 'comment':
12471251
if (empty($var)) { // full open/close style comment
1248-
return '<?php /* ?>'.$newline;
1249-
} else { // just ignore tag if it was a one line comment
1252+
return '<?php /* ?>';
1253+
}
1254+
else { // just ignore tag if it was a one line comment
12501255
return;
12511256
}
1257+
break;
12521258

12531259
case 'phpinclude':
12541260
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
1255-
return '<?php include(\''.$file.'\'); ?>'.$newline;
1261+
return '<?php include(\''.$file.'\'); ?>';
12561262
}
1263+
break;
12571264

12581265
case 'include':
12591266
return '<?php $this->_getData($this->_fileSearch(\''.$file.'\'), 1); ?>';
1267+
break;
12601268

12611269
case 'dyninclude':
12621270
return '<?php $this->_getData($this->_fileSearch($this->_dyninclude[\''.$name.'\']), 1); ?>';
1271+
break;
12631272

12641273
default:
12651274
if ($this->OPTIONS['STRICT']) vlibTemplateError::raiseError('VT_ERROR_INVALID_TAG', KILL, htmlspecialchars($wholetag, ENT_QUOTES));

server/lib/classes/tpl.inc.php

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ function _getData ($tmplfile, $do_eval=false) {
932932

933933
$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
934934
$regex.= 'tmpl_([\w]+)\s*';
935-
$regex.= '(?:';
935+
$regex.= '((?:(?:';
936936
$regex.= '(?:';
937937
$regex.= '(name|format|escape|op|value|file)';
938938
$regex.= '\s*=\s*';
@@ -941,40 +941,9 @@ function _getData ($tmplfile, $do_eval=false) {
941941
$regex.= '((?<=[\"\'])';
942942
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
943943
$regex.= '[\"\']?';
944-
$regex.= ')?\s*';
945-
$regex.= '(?:';
946-
$regex.= '(?:';
947-
$regex.= '(name|format|escape|op|value)';
948-
$regex.= '\s*=\s*';
949-
$regex.= ')';
950-
$regex.= '(?:[\"\'])?';
951-
$regex.= '((?<=[\"\'])';
952-
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
953-
$regex.= '[\"\']?';
954-
$regex.= ')?\s*';
955-
$regex.= '(?:';
956-
$regex.= '(?:';
957-
$regex.= '(name|format|escape|op|value)';
958-
$regex.= '\s*=\s*';
959-
$regex.= ')';
960-
$regex.= '(?:[\"\'])?';
961-
$regex.= '((?<=[\"\'])';
962-
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
963-
$regex.= '[\"\']?';
964-
$regex.= ')?\s*';
965-
$regex.= '(?:';
966-
$regex.= '(?:';
967-
$regex.= '(name|format|escape|op|value)';
968-
$regex.= '\s*=\s*';
969-
$regex.= ')';
970-
$regex.= '(?:[\"\'])?';
971-
$regex.= '((?<=[\"\'])';
972-
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
973-
$regex.= '[\"\']?';
974-
$regex.= ')?\s*';
944+
$regex.= ')?\s*)*?)';
975945
$regex.= '(?:>|\/>|}|-->){1}';
976946
$regex.= '/i';
977-
//$regex.= '([\r\n|\n|\r])?/ie';
978947
$data = preg_replace_callback($regex, array($this, '_parseTag'), $data);
979948

980949
if ($this->_cache) { // add cache if need be
@@ -1300,28 +1269,37 @@ function _parseTag ($args) {
13001269
$wholetag = $args[0];
13011270
$openclose = $args[1];
13021271
$tag = strtolower($args[2]);
1303-
$newline = $args[11];
1304-
//echo "1#$newline#2";
1305-
1306-
if ($tag == 'else') return '<?php } else { ?>'.$newline;
1272+
1273+
if ($tag == 'else') return '<?php } else { ?>';
13071274
if ($tag == 'tmpl_include') return $wholetag; // ignore tmpl_include tags
13081275

13091276
if (preg_match("/^<\/|{\/|<!--\/$/s", $openclose) || preg_match("/^end[if|loop|unless|comment]$/", $tag)) {
13101277
if ($tag == 'loop' || $tag == 'endloop') array_pop($this->_namespace);
13111278
if ($tag == 'comment' || $tag == 'endcomment') {
1312-
return '<?php */ ?>'.$newline;
1279+
return '<?php */ ?>';
13131280
}
13141281
else {
1315-
return '<?php } ?>'.$newline;
1282+
return '<?php } ?>';
13161283
}
13171284
}
13181285

13191286
// arrange attributes
1320-
for ($i=3; $i < 10; $i=($i+2)) {
1321-
if (empty($args[$i]) && empty($args[($i+1)])) break;
1322-
$key = (empty($args[$i])) ? 'name' : strtolower($args[$i]);
1323-
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
1324-
$$key = $args[($i+1)];
1287+
$tmp_atts = $args[3];
1288+
$atts = preg_split('/\s+/', $tmp_atts);
1289+
foreach($atts as $att) {
1290+
$regex = '/(?:';
1291+
$regex.= '(name|format|escape|op|value|file)';
1292+
$regex.= '\s*=\s*';
1293+
$regex.= ')?';
1294+
$regex.= '(?:[\"\'])?';
1295+
$regex.= '((?<=[\"\'])';
1296+
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
1297+
$regex.= '[\"\']?/';
1298+
if(preg_match($regex, $att, $match)) {
1299+
$key = (empty($match[1])) ? 'name' : strtolower($match[1]);
1300+
if ($key == 'name' && preg_match('/^(php)?include$/', $tag)) $key = 'file';
1301+
$$key = $match[2];
1302+
}
13251303
}
13261304

13271305
$var = ($this->OPTIONS['CASELESS']) ? strtolower($name) : $name;
@@ -1345,28 +1323,28 @@ function _parseTag ($args) {
13451323
if (empty($escape) && (!empty($this->OPTIONS['DEFAULT_ESCAPE']) && strtolower($this->OPTIONS['DEFAULT_ESCAPE']) != 'none')) {
13461324
$escape = strtolower($this->OPTIONS['DEFAULT_ESCAPE']);
13471325
}
1348-
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace).' ?>'.$newline."\n";
1326+
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace).' ?>'."\n";
13491327
break;
13501328

13511329
case 'if':
1352-
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>'.$newline;
1330+
return '<?php if ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
13531331
break;
13541332

13551333
case 'unless':
1356-
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>'.$newline;
1334+
return '<?php if (!'. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
13571335
break;
13581336

13591337
case 'elseif':
1360-
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>'.$newline;
1338+
return '<?php } elseif ('. $this->_parseIf($var, @$value, @$op, @$namespace, @$format) .') { ?>';
13611339
break;
13621340

13631341
case 'loop':
1364-
return '<?php '. $this->_parseLoop($var) .'?>'.$newline;
1342+
return '<?php '. $this->_parseLoop($var) .'?>';
13651343
break;
13661344

13671345
case 'comment':
13681346
if (empty($var)) { // full open/close style comment
1369-
return '<?php /* ?>'.$newline;
1347+
return '<?php /* ?>';
13701348
}
13711349
else { // just ignore tag if it was a one line comment
13721350
return;
@@ -1375,7 +1353,7 @@ function _parseTag ($args) {
13751353

13761354
case 'phpinclude':
13771355
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
1378-
return '<?php include(\''.$file.'\'); ?>'.$newline;
1356+
return '<?php include(\''.$file.'\'); ?>';
13791357
}
13801358
break;
13811359

0 commit comments

Comments
 (0)