Skip to content

Commit eca44ab

Browse files
author
Marius Cramer
committed
Synchronized changes and improvements between server and interface part for template library
1 parent 1643497 commit eca44ab

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

interface/lib/classes/tpl.inc.php

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ public function setVar($k, $v = null)
234234
if (is_array($k)) {
235235
foreach($k as $key => $value){
236236
$key = ($this->OPTIONS['CASELESS']) ? strtolower(trim($key)) : trim($key);
237-
if (preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $key) && $value !== null ) {
237+
if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $key) && $value !== null ) {
238238
$this->_vars[$key] = $value;
239239
}
240240
}
241241
} else {
242-
if (preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $k) && $v !== null) {
242+
if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $k) && $v !== null) {
243243
if ($this->OPTIONS['CASELESS']) $k = strtolower($k);
244244
$this->_vars[trim($k)] = $v;
245245
} else {
@@ -287,7 +287,7 @@ public function unsetVar()
287287
for ($i = 0; $i < $num_args; $i++) {
288288
$var = func_get_arg($i);
289289
if ($this->OPTIONS['CASELESS']) $var = strtolower($var);
290-
if (!preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $var)) continue;
290+
if (!preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $var)) continue;
291291
unset($this->_vars[$var]);
292292
}
293293
return true;
@@ -344,7 +344,7 @@ public function setContextVars()
344344
*/
345345
public function setLoop($k, $v)
346346
{
347-
if (is_array($v) && preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $k)) {
347+
if (is_array($v) && preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $k)) {
348348
$k = ($this->OPTIONS['CASELESS']) ? strtolower(trim($k)) : trim($k);
349349
$this->_arrvars[$k] = array();
350350
if ($this->OPTIONS['SET_LOOP_VAR'] && !empty($v)) $this->setvar($k, 1);
@@ -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,7 +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)
1009+
private function _parseIf($varname, $value = null, $op = null, $namespace = null, $format = null)
10301010
{
10311011
if (isset($namespace)) $namespace = substr($namespace, 0, -1);
10321012
$comp_str = ''; // used for extended if statements
@@ -1065,9 +1045,19 @@ private function _parseIf($varname, $value = null, $op = null, $namespace = null
10651045
}
10661046
}
10671047
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;
1048+
$retstr = '(('.$retstr.'[\''.$varname.'\'] !== null) ? '.$retstr.'[\''.$varname.'\'] : $this->_vars[\''.$varname.'\'])';
1049+
if(isset($format) && isset($value) && $format == 'version') {
1050+
return 'version_compare(' . $retstr . ', \'' . $value . '\', \'' . (!empty($op) ? $op : '==') . '\')';
1051+
} else {
1052+
return $retstr.$comp_str;
1053+
}
1054+
}
1055+
else {
1056+
if(isset($format) && isset($value) && $format == 'version') {
1057+
return 'version_compare(' . $retstr."['".$varname."']" . ', \'' . $value . '\', \'' . (!empty($op) ? $op : '==') . '\')';
1058+
} else {
1059+
return $retstr."['".$varname."']".$comp_str;
1060+
}
10711061
}
10721062
}
10731063

@@ -1186,26 +1176,36 @@ private function _parseTag ($args)
11861176
$wholetag = $args[0];
11871177
$openclose = $args[1];
11881178
$tag = strtolower($args[2]);
1189-
$newline = $args[9];
1190-
1191-
if ($tag == 'else') return '<?php } else { ?>'.$newline;
1179+
1180+
if ($tag == 'else') return '<?php } else { ?>';
11921181
if ($tag == 'tmpl_include') return $wholetag; // ignore tmpl_include tags
11931182

11941183
if (preg_match("/^<\/|{\/|<!--\/$/s", $openclose) || preg_match("/^end[if|loop|unless|comment]$/", $tag)) {
11951184
if ($tag == 'loop' || $tag == 'endloop') array_pop($this->_namespace);
11961185
if ($tag == 'comment' || $tag == 'endcomment') {
1197-
return '<?php */ ?>'.$newline;
1186+
return '<?php */ ?>';
11981187
} else {
1199-
return '<?php } ?>'.$newline;
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
}
12101210

12111211
$var = ($this->OPTIONS['CASELESS']) ? strtolower($name) : $name;
@@ -1229,30 +1229,30 @@ private function _parseTag ($args)
12291229
if (empty($escape) && (!empty($this->OPTIONS['DEFAULT_ESCAPE']) && strtolower($this->OPTIONS['DEFAULT_ESCAPE']) != 'none')) {
12301230
$escape = strtolower($this->OPTIONS['DEFAULT_ESCAPE']);
12311231
}
1232-
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace)." ?>$newline";
1232+
return '<?php '.$this->_parseVar ($wholetag, $tag, $var, @$escape, @$format, @$namespace)." ?>\n";
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) .') { ?>';
12361236

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

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

12431243
case 'loop':
1244-
return '<?php '. $this->_parseLoop($var) .'?>'.$newline;
1244+
return '<?php '. $this->_parseLoop($var) .'?>';
12451245

12461246
case 'comment':
12471247
if (empty($var)) { // full open/close style comment
1248-
return '<?php /* ?>'.$newline;
1248+
return '<?php /* ?>';
12491249
} else { // just ignore tag if it was a one line comment
12501250
return;
12511251
}
12521252

12531253
case 'phpinclude':
12541254
if ($this->OPTIONS['ENABLE_PHPINCLUDE']) {
1255-
return '<?php include(\''.$file.'\'); ?>'.$newline;
1255+
return '<?php include(\''.$file.'\'); ?>';
12561256
}
12571257

12581258
case 'include':

server/lib/classes/tpl.inc.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ function unsetVar () {
299299
* @return array
300300
* @access public
301301
*/
302-
function getVars () {
303-
if (empty($this->_vars)) return false;
304-
return $this->_vars;
302+
public function getVars()
303+
{
304+
return empty($this->_vars) ? false : $this->_vars;
305305
}
306306

307307

@@ -361,6 +361,8 @@ function setLoop ($k, $v) {
361361
if ($this->OPTIONS['SET_LOOP_VAR'] && !empty($v)) $this->setvar($k, 1);
362362
if (($this->_arrvars[$k] = $this->_arrayBuild($v)) == false) {
363363
vlibTemplateError::raiseError('VT_WARNING_INVALID_ARR', WARNING, $k);
364+
} else {
365+
$this->vars['_'.$k.'_num'] = count($v);
364366
}
365367
}
366368
return true;

0 commit comments

Comments
 (0)