浏览代码

* htmlentities on matches/gettext
* recursively resolve all includes

Nathaniel van Diepen 7 年之前
父节点
当前提交
2bf9622158
共有 1 个文件被更改,包括 19 次插入17 次删除
  1. 19 17
      Data/template.class.php

+ 19 - 17
Data/template.class.php

@@ -38,17 +38,19 @@
 						}, $output);
 					},
 					'include'=>function(&$output, &$ignored = null){
-						$output = preg_replace_callback(static::$regex['include'], function($matches) use(&$ignored){
-							$path = static::$basedir.'/'.$matches[1];
-							if(file_exists($path)){
-								$output = file_get_contents($path);
-								if(!is_null($ignored)){
-									static::$parsers['ignore']($output, $ignored);
+						while(preg_match(static::$regex['include'], $output)){
+							$output = preg_replace_callback(static::$regex['include'], function($matches) use(&$ignored){
+								$path = static::$basedir.'/'.$matches[1];
+								if(file_exists($path)){
+									$output = file_get_contents($path);
+									if(!is_null($ignored)){
+										static::$parsers['ignore']($output, $ignored);
+									}
+									return $output;
 								}
-								return $output;
-							}
-							return '';
-						}, $output);
+								return '';
+							}, $output);
+						}
 					},
 					'each'=>[
 						function(&$output, $data){
@@ -123,13 +125,13 @@
 										return $data[$item] ?? '';
 									}
 								}, array_slice($matches, 1));
-								return _(sprintf(...$args));
+								return htmlentities(_(sprintf(...$args)));
 							}, $output);
 						},
 						function(&$output){
 							$output = preg_replace_callback(static::$regex['gettext'], function($matches){
 								if(count($matches) > 2){
-									$output = "<?= sprintf(_({$matches[1]})";
+									$output = "<?=htmlentities(sprintf(_({$matches[1]})";
 									foreach(array_slice($matches, 2) as $item){
 										if(preg_match(static::$regex['gettext_string'], $item)){
 											$output .= ", $item";
@@ -138,9 +140,9 @@
 										}
 									}
 								}else{
-									$output = "<?= _({$matches[1]}";
+									$output = "<?=htmlentities(_({$matches[1]}";
 								}
-								return "{$output}); ?>";
+								return "{$output})); ?>";
 							}, $output);
 						}
 					],
@@ -175,12 +177,12 @@
 					'match'=>[
 						function(&$output, $data){
 							$output = preg_replace_callback(static::$regex['match'], function($matches) use($data){
-								return $data[$matches[1]] ?? '';
+								return htmlentities($data[$matches[1]] ?? '');
 							}, $output);
 						},
 						function(&$output){
 							$output = preg_replace_callback(static::$regex['match'], function($matches){
-								return "<?=(\$data[".var_export($matches[1], true)."] ?? ''); ?>";
+								return "<?=htmlentities(\$data[".var_export($matches[1], true)."] ?? ''); ?>";
 							}, $output);
 						}
 					],
@@ -190,7 +192,7 @@
 						},
 						function(&$output){
 							$output = preg_replace_callback(static::$regex['parentmatch'], function($matches){
-								return "<?=(\$parent[count(\$parent)-1][".var_export($matches[1], true)."] ?? ''); ?>";
+								return "<?=htmlentities(\$parent[count(\$parent)-1][".var_export($matches[1], true)."] ?? ''); ?>";
 							}, $output);
 						}
 					],