⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 template.compiler.php

📁 投稿 文章管理 员文章评论回复无限引用简易留言功能简易RSS功...
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php
class Template_Compiler extends Template {
	// public configuration variables
	var $left_delimiter			= "";
	var $right_delimiter		= "";
	var $plugins_dir			= "";
	var $template_dir			= "";
	var $reserved_template_varname = "";
	var $default_modifiers		= array();

	var $php_extract_vars		=	true;	// Set this to false if you do not want the $this->_tpl variables to be extracted for use by PHP code inside the template.
	var $error			=	true;
	// private internal variables
	var $_vars			=	array();	// stores all internal assigned variables
	var $_plugins		=	array();	// stores all internal plugins
	var $_linenum		=	0;		// the current line number in the file we are processing
	var $_file			=	"";		// the current file we are processing
	var $_literal		=	array();	// stores all literal blocks
	var $_foreachelse_stack		=	array();
	var $_for_stack		=	0;
	var $_sectionelse_stack	 =   array();	// keeps track of whether section had 'else' part
	var $_DreamCMSelse_stack =   array();	// keeps track of whether section had 'else' part
	var $_switch_stack		=	array();
	var $_tag_stack			=	array();
	var $_require_stack		=	array();	// stores all files that are "required" inside of the template
	var $_php_blocks		=	array();	// stores all of the php blocks
	var $_error_level		=	null;
	var $_sl_md5			=	'69d2cf521ab5d33d99f192a670937618';

	var $_db_qstr_regexp	=	null;		// regexps are setup in the constructor
	var $_si_qstr_regexp	=	null;
	var $_qstr_regexp		=	null;
	var $_func_regexp		=	null;
	var $_var_bracket_regexp=	null;
	var $_dvar_regexp		=	null;
	var $_cvar_regexp		=	null;
	var $_svar_regexp		=	null;
	var $_mod_regexp		=	null;
	var $_var_regexp		=	null;
    var $_obj_params_regexp =   null;
	var $_templatelite_vars	=	array();

	function Template_compiler(){
		// matches double quoted strings:
		// "foobar"
		// "foo\"bar"
		// "foobar" . "foo\"bar"
		$this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';

		// matches single quoted strings:
		// 'foobar'
		// 'foo\'bar'
		$this->_si_qstr_regexp = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';

		// matches single or double quoted strings
		$this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_si_qstr_regexp . ')';

		// matches bracket portion of vars
		// [0]
		// [foo]
		// [$bar]
		// [#bar#]
		$this->_var_bracket_regexp = '\[[\$|\#]?\w+\#?\]';
//		$this->_var_bracket_regexp = '\[\$?[\w\.]+\]';

		// matches section vars:
		// %foo.bar%
		$this->_svar_regexp = '\%\w+\.\w+\%';

		// matches $ vars (not objects):
		// $foo
		// $foo[0]
		// $foo[$bar]
		// $foo[5][blah]
//		$this->_dvar_regexp = '\$[a-zA-Z0-9_]{1,}(?:' . $this->_var_bracket_regexp . ')*(?:' . $this->_var_bracket_regexp . ')*';
		$this->_dvar_regexp = '\$[a-zA-Z0-9_]{1,}(?:' . $this->_var_bracket_regexp . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*';

		// matches config vars:
		// #foo#
		// #foobar123_foo#
		$this->_cvar_regexp = '\#[a-zA-Z0-9_]{1,}(?:' . $this->_var_bracket_regexp . ')*(?:' . $this->_var_bracket_regexp . ')*\#';

		// matches valid variable syntax:
		// $foo
		// 'text'
		// "text"
		$this->_var_regexp = '(?:(?:' . $this->_dvar_regexp . '|' . $this->_cvar_regexp . ')|' . $this->_qstr_regexp . ')';

		// matches valid modifier syntax:
		// |foo
		// |@foo
		// |foo:"bar"
		// |foo:$bar
		// |foo:"bar":$foobar
		// |foo|bar
		$this->_mod_regexp = '(?:\|@?[0-9a-zA-Z_]+(?::(?>-?\w+|' . $this->_dvar_regexp . '|' . $this->_qstr_regexp .'))*)';

		// matches valid function name:
		// foo123
		// _foo_bar
		$this->_func_regexp = '[a-zA-Z_:]+';
//		$this->_func_regexp = '[a-zA-Z_]\w*';

	}

	function _compile_file($file_contents){
		$ldq 	= preg_quote($this->left_delimiter);
		$rdq 	= preg_quote($this->right_delimiter);
		$_match	= array();		// a temp variable for the current regex match
		$tags	= array();		// all original tags
		$text	= array();		// all original text
		$compiled_text	= '';
		$compiled_tags	= array();		// all tags and stuff

		$this->_require_stack = array();

		$this->_load_filters();

		if (count($this->_plugins['prefilter']) > 0){
			foreach ($this->_plugins['prefilter'] as $function){
				if ($function === false){
					continue;
				}
				$file_contents = $function($file_contents, $this);
			}
		}
		// remove all comments
		$file_contents = preg_replace("!{$ldq}\*.*?\*{$rdq}!se","",$file_contents);

		// replace all php start and end tags
//		$file_contents = preg_replace('%(<\?(?!php|=|$))%i', '<?php echo \'\\1\'? >'."\n", $file_contents);
		
//2007-7-22 23:41 过滤PHP标签
        /* match anything resembling php tags */
        if (preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?\s*php\s*[\"\']?)~is', $file_contents, $sp_match)) {
             /* replace tags with placeholders to prevent recursive replacements */
             $sp_match[1] = array_unique($sp_match[1]);
             /* process each one */
             for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
                 if ($this->php_handling == "DreamCMS_PHP_PASSTHRU") {
                        /* echo php contents */
                        $file_contents = str_replace($sp_match[1][$curr_sp], '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>', $file_contents);
                    } else if ($this->php_handling == "DreamCMS_PHP_QUOTE") {
                        /* quote php tags */
                        $file_contents = str_replace($sp_match[1][$curr_sp], htmlspecialchars($sp_match[1][$curr_sp]), $file_contents);
                    } else if ($this->php_handling == "DreamCMS_PHP_REMOVE") {
                        /* remove php tags */
                        $file_contents = str_replace($sp_match[1][$curr_sp], '', $file_contents);
                    } else {
                        /* DreamCMS_PHP_ALLOW, but echo non php starting tags */
                        $sp_match[1][$curr_sp] = preg_replace('~(<\?(?!php|=|$))~i', '<?php echo \'\\1\'?>', $sp_match[1][$curr_sp]);
                        $file_contents = str_replace($sp_match[1][$curr_sp], $sp_match[1][$curr_sp], $file_contents);
                    }
                }
            }
//2007-7-22 23:46 
		// remove literal blocks

		preg_match_all("!{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}!s", $file_contents, $_match);
		$this->_literal = $_match[1];
		$file_contents = preg_replace("!{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}!s", stripslashes($ldq . "literal" . $rdq), $file_contents);

		// remove php blocks
		preg_match_all("!{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}!s", $file_contents, $_match);
		$this->_php_blocks = $_match[1];
		$file_contents = preg_replace("!{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}!s", stripslashes($ldq . "php" . $rdq), $file_contents);

		// gather all template tags
		preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $file_contents, $_match);
		$tags = $_match[1];

		// put all of the non-template tag text blocks into an array, using the template tags as delimiters
		$text = preg_split("!{$ldq}.*?{$rdq}!s", $file_contents);

		// compile template tags
		$count_tags = count($tags);
		for ($i = 0, $for_max = $count_tags; $i < $for_max; $i++){
			$this->_linenum += substr_count($text[$i], "\n");
			$compiled_tags[] = $this->_compile_tag($tags[$i]);
			$this->_linenum += substr_count($tags[$i], "\n");
		}

		// build the compiled template by replacing and interleaving text blocks and compiled tags
		$count_compiled_tags = count($compiled_tags);
		for ($i = 0, $for_max = $count_compiled_tags; $i < $for_max; $i++){
			if ($compiled_tags[$i] == '') {
				$text[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text[$i+1]);
			}
			$compiled_text .= $text[$i].$compiled_tags[$i];
		}
		$compiled_text .= $text[$i];

		foreach ($this->_require_stack as $key => $value){
			$compiled_text = '<?php require_once(\''. $this->_get_plugin_dir($key) . $key . '\'); $this->register_' . $value[0] . '("' . $value[1] . '", "' . $value[2] . '"); ?>' . $compiled_text;
		}

		// remove unnecessary close/open tags
		$compiled_text = preg_replace('!\?>\n?<\?php!', '', $compiled_text);

		if (count($this->_plugins['postfilter']) > 0){
			foreach ($this->_plugins['postfilter'] as $function){
				if ($function === false){
					continue;
				}
				$compiled_text = $function($compiled_text, $this);
			}
		}
//2007-7-29 21:15 error_reporting/function_exists
		$this->error && $compiled_text = "<?php error_reporting(0);!defined('DCPATH') && exit('What are you doing?');?>\n".$compiled_text;
		return $compiled_text;
	}

	function _compile_tag($tag){
		$_match		= array();		// stores the tags
		$_result	= "";			// the compiled tag
		$_variable	= "";			// the compiled variable

		// extract the tag command, modifier and arguments
		preg_match_all('/(?:(' . $this->_var_regexp . '|' . $this->_svar_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*)(?:\s*[,\.]\s*)?)(?:\s+(.*))?/xs', $tag, $_match);

		if ($_match[1][0]{0} == '$' || ($_match[1][0]{0} == '#' && $_match[1][0]{strlen($_match[1][0]) - 1} == '#') || $_match[1][0]{0} == "'" || $_match[1][0]{0} == '"' || $_match[1][0]{0} == '%'){
			$_result = $this->_parse_variables($_match[1], $_match[2]);
			return "<?php echo $_result; ?>";
		}
		// process a function
		$tag_command = $_match[1][0];
		$tag_modifiers = !empty($_match[2][0]) ? $_match[2][0] : null;
		$tag_arguments = !empty($_match[3][0]) ? $_match[3][0] : null;
		$_result = $this->_parse_function($tag_command, $tag_modifiers, $tag_arguments);
		return $_result;
	}

	function _parse_function($function, $modifiers, $arguments){
		if(strpos($function,'DreamCMS:')!==false){
			list($function,$module)=explode(':',$function);
		}
		switch ($function) {
			case 'include':
				if (!function_exists('compile_include')){
					include_once(TEMPLATE_LITE_DIR . "internal/compile.include.php");
				}
				return compile_include($arguments, $this);
				break;
			case 'DreamCMS':
				$arguments.=' module="'.$module.'"';
				$_args = $this->_parse_arguments($arguments);
//				if(isset($_args['loop'])){
//					$_args['loop']=in_array(strtolower($this->_dequote($_args['loop'])),array("y","yes",'true'))?true:false;
//				}else{
//					$_args['loop']=in_array(strtolower($module),array("list","catalog"))?true:false;
//				}
				if($module && isset($_args['loop'])){//2.0
//				if(isset($_args['module'])&&in_array($this->_dequote($_args['module']),$this->config['Loop_Module'])){//3.0
//				if(isset($_args['module'])){//3.0
					$arguments='';
//					$_args['loop'] && $arguments.="name=".str_replace('"','',str_replace("'",'',$_args['loop']))." ";
					$arguments.="loop=\$".(isset($_args['alias'])?$this->_dequote($_args['alias']):$this->_dequote($_args['module']));
					isset($_args['start']) && $arguments.="start={$_args['start']} ";
					isset($_args['step']) && $arguments.="step={$_args['step']} ";
					isset($_args['max']) && $arguments.="max={$_args['max']} ";
				//	isset($_args['show']) && $arguments.='show="'.$_args['show'].'" ';
					if (!function_exists('compile_dreamcms')){
						include_once(TEMPLATE_LITE_DIR . "internal/compile.dreamcms.php");
					}
					$_DreamCMS_Loop=compile_dreamcms($arguments, $this);
				}
				if (!isset($_args['module'])){
					$this->trigger_error("missing 'module' attribute in 'DreamCMS'", E_USER_ERROR, __FILE__, __LINE__);
				}
				foreach ($_args as $key => $value){
					if (is_bool($value)){
						$value = $value ? 'true' : 'false';
					}
					$arg_list[] = "'$key' => $value";
				}
				return '<?php $this->_run_DreamCMS(array(' . implode(', ', (array)$arg_list) . ')); ?>'.$_DreamCMS_Loop;
				break;
			case 'DreamCMSelse':

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -