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

📄 conditional_functions.php

📁 jsp程序开发系统
💻 PHP
字号:
<?php
// +-------------------------------------------------------------+
// | DeskPRO v [2.0.1 Production]
// | Copyright (C) 2001 - 2004 Headstart Solutions Limited
// | Supplied by WTN-WDYL
// | Nullified by WTN-WDYL
// | Distribution via WebForum, ForumRU and associated file dumps
// +-------------------------------------------------------------+
// | DESKPRO IS NOT FREE SOFTWARE
// +-------------------------------------------------------------+
// | License ID : Full Enterprise License =) ...
// | License Owner : WTN-WDYL Team
// +-------------------------------------------------------------+
// | $RCSfile: conditional_functions.php,v $
// | $Date: 2004/02/10 01:34:25 $
// | $Revision: 1.16 $
// +-------------------------------------------------------------+
// | File Details:
// | - Utility functions for template engine
// +-------------------------------------------------------------+

error_reporting(E_ALL ^ E_NOTICE);

##########################################################
#
#		function format_text_item: return formatted string 
#		without leading spaces and with slashes
#		for example: $in = " test''test  "
#		function return: "test\'\'test"
#
function format_text_item($in)
{
	// $in = trim($in);
	if ((substr($in, 0, 2) == "\r\n")) {
		$in = substr($in, 2);
	}

	$in = addslashes($in);
	
	return "\"$in\"";
}

##########################################################
#
#		recursive function tree_to_tertiary_operator: return 
#		tertiary operator string, which was built from tree
#
function tree_to_tertiary_operator(& $node)
{
	$out = '';
	
	$dataref = $node['data'];
	$node_items = array();

	for ($i = 0; $i < count($node['data']); $i++)
	{
		$data_item = & $node['data'][$i];

		if (is_array($data_item))
		{
		 	$if_block = '(('.$data_item['if']['conditional'].') ? (';
			$if_data = tree_to_tertiary_operator($data_item['if']);
			$if_block .= $if_data.') : ';
			
			if (!is_array($data_item['elseif']) && !is_array($data_item['else']))
			{
				$if_block .= "''";
			}
			elseif (!is_array($data_item['elseif']) && is_array($data_item['else']))
			{
				$else_block = tree_to_tertiary_operator($data_item['else']);
				$if_block .= "($else_block)";
			}
			else
			{
				$elif_block = elsif_array_to_tertiary_operator($data_item['elseif'],
					$data_item['else']);
				$if_block .= "($elif_block)";
			}
			
			array_push ($node_items, $if_block.')');
		}
		else
		{
			array_push ($node_items, format_text_item($data_item));
		}
	}	
	$out .= join('.', $node_items);
	// changed
	if (empty($out)) {
		$out = '""';
	}
	
	return $out;
}

#################################################################
#
#		function elsif_array_to_tertiary_operator: return
#		tertiary operator string, which was built from tree for 
#		elseif operator
#
#

function elsif_array_to_tertiary_operator (& $elsif_array, & $else_hash)
{
	$out = '(';
	$elsif_hash = array_shift($elsif_array);
	
	$out .= "(".$elsif_hash['conditional'].") ? (";
	$out .= tree_to_tertiary_operator($elsif_hash);
	$out .= ") : ";
	
	if (count($elsif_array) > 0)
	{
		$out .= elsif_array_to_tertiary_operator($elsif_array, $else_hash);
	}
	elseif (is_array($else_hash))
	{
		$out .= "(";
		$out .= tree_to_tertiary_operator($else_hash);
		$out .= ")";
	}
	else
	{
		$out .= "\"\"";
	}
	
	$out .= ")";
	
	return $out;
}

##############################################################
#
#		recursive function lex_queue_to_tree: return
#		tree. This function check also syntax for all lexemes
#
#

function lex_queue_to_tree(& $root_node,& $lex_queue, $level_deep)
{	
	$ignore_level_up = 0;

	do
	{
		$next_item = array_shift($lex_queue);

		if ($next_item['type'] == 'text')
		{
			array_push ($root_node['data'], $next_item['value']);
		}
		elseif ($next_item['type'] == 'if')
		{
			$conditional = array_shift($lex_queue);
			if(!($conditional['type'] == 'conditional' && $conditional['value'] != ""))
			{
				die ("If requires conditional statement");
			}
		
			$new_check_node['if']['parent'] = & $new_check_node;
			array_push ($root_node['data'], array(
				'if' => array(
					'conditional' => $conditional['value'],
				 	'data' => array()
				)
			));

			$ignore_level_up = 
			lex_queue_to_tree($root_node['data'][count($root_node['data']) - 1]['if'],
				 $lex_queue, $level_deep + 1);
		}
		elseif ($next_item['type'] == 'elseif')
		{
			if ($ignore_level_up > 0) 
				$ignore_level_up = 0;
			else
			{
				array_unshift($lex_queue, $next_item);
				return 1;
			}
		
			$conditional = array_shift($lex_queue);
			if(!($conditional['type'] == 'conditional' && $conditional['value']!=""))
			{
				die ("ElseIf requires conditional statement");
			}

			if(!(is_array($root_node['data'][count($root_node['data']) - 1])))
			{
				die ("ElseIf requires preceeding If");
			}	

			if (!is_array($root_node['data'][count($root_node['data']) - 1]['elseif']))
			{
				$root_node['data'][count($root_node['data']) - 1]['elseif'] = array();
			}
			
			array_push($root_node['data'][count($root_node['data']) - 1]['elseif'], array(
				'conditional' => $conditional['value'],
				'data' => array()
			));
		
			$ignore_level_up = 
			lex_queue_to_tree($root_node['data'][count($root_node['data']) - 1]['elseif']
				[count($root_node['data'][count($root_node['data']) - 1]['elseif']) - 1],
				 $lex_queue, $level_deep + 1);
		}
		elseif ($next_item['type'] == 'else')
		{
			if ($ignore_level_up > 0) 
				$ignore_level_up = 0;
			else
			{
				array_unshift($lex_queue, $next_item);
				return 1;
			}

			if(!(is_array($root_node['data'][count($root_node['data']) - 1])))
			{
				die ("Else requires preceeding If");
			}
		
			if (defined($root_node['data'][count($root_node['data']) - 1]['else']))
			{
				die ("Else already defined for If");
			}
			
			$root_node['data'][count($root_node['data']) - 1]['else'] 
				= array( 'data' => array() );
		
			lex_queue_to_tree($root_node['data'][count($root_node['data']) - 1]['else'],
				 $lex_queue, $level_deep + 1);
		}
		elseif ($next_item['type'] == 'endif')
		{
			if(!$level_deep)
			{
				die ("EndIf requires preceeding If");
			}

			return 0;
		}
		elseif ($next_item['type'] == 'conditional')
		{
			die ("Unexpected conditional statement, probably bug in parser\n");
		}
	} while (count($lex_queue) > 0);
	
	if ($level_deep)
	{
		die("No enough EndIf's\n");
	}
	
	return 0;
}
#######################################################################
#
#		function text_to_lexemes_queue: return hash, which contain 
#		queue with lexemes from template
#
#

function text_to_lexemes_queue($text)
{
	#list ($parse_conditional, $consume_chars) = (0, 0);
	$parse_conditional = 0;
	$consume_chars = 0;
	$out = array();
	do
	{
		$parse_conditional = 0;
		if (preg_match("/^(<\%elseif\s+)/s", $text, $matches))
		{
			$consume_chars = strlen($matches[1]);
			array_push($out, array( 'type' => 'elseif' ));
			$parse_conditional = 1;
		}
		elseif (preg_match("/^(<\%else\%>)/s",$text,$matches))
		{
			$consume_chars = strlen($matches[1]);
			array_push($out, array( 'type' => 'else' ));
		}
		elseif (preg_match("/^(<\%endif\%>)/s",$text, $matches))
		{
			$consume_chars = strlen($matches[1]);
			array_push($out, array( 'type' => 'endif' ));
		}
		elseif (preg_match("/^(<\%if\s+)/s", $text,$matches))
		{
			$consume_chars = strlen($matches[1]);
			array_push($out, array( 'type' => 'if' ));
			$parse_conditional = 1;
		}
		elseif (preg_match("/^(.)/s", $text, $matches))
		{
			$consume_chars = 1;
			$textchar = $matches[1];
			
            if ($out[count($out)-1]['type'] == 'text')
            {
                $out[count($out)-1]['value'] .= $textchar;
            }
			else
			{
				array_push($out, array('type' => 'text', 'value' => $textchar ));
			}
		}

		$text = substr($text, $consume_chars);
		if ($parse_conditional)
		{
			array_push ($out, array('type' => 'conditional', 'value' => read_conditional($text)));
		}
	} while (strlen ($text));
	
	return $out;
}
################################################################################
#
#		function read_conditional: become reference to conditional in
#		if or elseif statements.
#
#
function read_conditional(& $textref)
{
	$out='';
	$consume_chars=0;
	$exit=0;
	$parse_quoted=0;
	$quote_char=0;

	do
	{
		$parse_quoted = 0;
		
		if ((preg_match("/^(\')/", $textref,$matches)) || (preg_match("/^(\")/",$textref,$matches)))
		{
			$quote_char = $matches[1];
			$out .= $quote_char;
			$consume_chars = 1;
			$parse_quoted = 1;
		}
		elseif (preg_match("/^(\%>)/",$textref))
		{
			$consume_chars = 2;
			$exit = 1;
		}
		elseif (preg_match("/^(.)/s", $textref, $matches))
		{
			$out .= $matches[1];
			$consume_chars = 1;
		}
		elseif (!strlen($textref))
		{
			die ("Unexpected end of text while reading PHP conditional statement");
		}
		
		$textref = substr($textref, $consume_chars);
		if ($parse_quoted)
		{
			$out .= read_quoted($textref, $quote_char);
		}
	} while (! $exit);
	
	# (PHP syntax) check out with eval
	return $out;
}
####################################################################
#
#		function read_quoted: function for testing quoted text
#
#
function read_quoted (& $textref, $quote_char)
{
	$regexp_quote = AddSlashes($quote_char);
	$out = '';
	$consume_chars = 0;
	$exit_quote = 0;
	do
	{
		if ((preg_match("/^\\$regexp_quote/",$textref)) || (preg_match("/^\\\\/", $textref)))
		{
			$consume_chars = 2;
		}
		elseif (preg_match("/^$regexp_quote/", $textref))
		{
			$consume_chars = 1;
			$exit_quote = 1;
		}
		elseif (preg_match("/^(.)/s", $textref))
		{
			$consume_chars = 1;
		}
		elseif (!strlen($textref))
		{
			die ("Unexpected end of text while reading quoted string");
		}
			
		$out .= substr($textref, 0, $consume_chars);
		$textref = substr($textref, $consume_chars);
	} while (! $exit_quote);		
	
	return $out;
}

/****************************************************
	function parse_conditionals()

-----DESCRIPTION: -----------------------------------
	Parse an unparsed template through the lexical parser
	to produce an eval-ready template object.

-----ARGUMENTS: -------------------------------------
	template	Template to parse

-----RETURNS: ---------------------------------------
	The parsed template.

****************************************************/

function parse_conditionals($template) {

	$template = preg_replace("#\s?<%!--.*--!%>\s?#", '', $template);
	if ($GLOBALS['session_url']) {
		$template = preg_replace('#\.php(\?)?#ies', '\'.php{$GLOBALS[session_url]}\'.((\'\1\' != \'\') ? (\'{$GLOBALS[session_ampersand]}\') : (\'\'))', $template);
	}

	$lex_queue = text_to_lexemes_queue($template);

	$root = array('data' => array());
	if (lex_queue_to_tree($root, $lex_queue, 0)) {
		die ("Else already defined or ElseIf without If\n");
	}

	$output = tree_to_tertiary_operator($root);
	$output = get_loops($output);

	return $output;
}

/****************************************************
	function get_loops()

-----DESCRIPTION: -----------------------------------
	Replace <%loop%> occurences with callable code
	reference.

-----ARGUMENTS: -------------------------------------
	template	Template to process

-----RETURNS: ---------------------------------------
	The processed template.

****************************************************/

function get_loops ($template) {

	$template = preg_replace('#<%loop (\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*) ?%>(.*)<%endloop%>#iUse', "loop_code('\\1', '\\2')",$template);

	return $template;
}

/****************************************************
	function loop_code()

-----DESCRIPTION: -----------------------------------
	Perform loop calls for every loop in a template

-----ARGUMENTS: -------------------------------------
	loop_name	Name of the element being looped
	loop_code	Code to execute during each iteration

-----RETURNS: ---------------------------------------
	Code fragment to execute the loop

****************************************************/
function loop_code($loop_name, $loop_code) {

	$loop_name = stripslashes($loop_name);
	$loop_code = stripslashes($loop_code);

	$loop_name2 = substr($loop_name, 1);
	$loop_code = str_replace("'", "loop-temp-section", $loop_code);

	return " \" . doloop('$loop_name2', $loop_name, '$loop_code', \$GLOBALS) . \"";

}

?>

⌨️ 快捷键说明

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