compile.compile_if.php
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Template Lite compile IF tag - template internal module
*
* Type: template
* Name: compile_parse_is_expr
*/
function compile_compile_if($arguments, $elseif, $while, &$object)
{
$_result = "";
$_match = array();
$_args = array();
$_is_arg_stack = array();
// extract arguments from the equation
preg_match_all('/(?>(' . $object->_var_regexp . '|\/?' . $object->_svar_regexp . '|\/?' . $object->_func_regexp . ')(?:' . $object->_mod_regexp . '*)?|\-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\%|\+|\-|\/|\*|\@|\b\w+\b|\S+)/x', $arguments, $_match);
$_args = $_match[0];
// make sure we have balanced parenthesis
$_args_count = array_count_values($_args);
if(isset($_args_count['(']) && $_args_count['('] != $_args_count[')'])
{
$object->trigger_error("unbalanced parenthesis in if statement", E_USER_ERROR, __FILE__, __LINE__);
}
$count_args = count($_args);
for ($i = 0, $for_max = $count_args; $i < $for_max; $i++)
{
$_arg = &$_args[$i];
switch (strtolower($_arg))
{
case '!':
case '%':
case '!==':
case '==':
case '===':
case '>':
case '<':
case '!=':
case '<>':
case '<<':
case '>>':
case '<=':
case '>=':
case '&&':
case '||':
case '^':
case '&':
case '~':
case ')':
case ',':
case '+':
case '-':
case '*':
case '/':
case '@':
break;
case 'eq':
$_arg = '==';
break;
case 'ne':
case 'neq':
$_arg = '!=';
break;
case 'lt':
$_arg = '<';
break;
case 'le':
case 'lte':
$_arg = '<=';
break;
case 'gt':
$_arg = '>';
break;
case 'ge':
case 'gte':
$_arg = '>=';
break;
case 'and':
$_arg = '&&';
break;
case 'or':
$_arg = '||';
break;
case 'not':
$_arg = '!';
break;
case 'mod':
$_arg = '%';
break;
case '(':
array_push($_is_arg_stack, $i);
break;
case 'is':
if ($_args[$i-1] == ')')
{
$is_arg_start = array_pop($is_arg_stack);
}
else
{
$_is_arg_count = count($_args);
$is_arg = implode(' ', array_slice($_args, $is_arg_start, $i - $is_arg_start));
$_arg_tokens = $object->_parse_is_expr($is_arg, array_slice($_args, $i+1));
array_splice($_args, $is_arg_start, count($_args), $_arg_tokens);
$i = $_is_arg_count - count($_args);
}
break;
default:
preg_match('/(?:(' . $object->_var_regexp . '|' . $object->_svar_regexp . '|' . $object->_func_regexp . ')(' . $object->_mod_regexp . '*)(?:\s*[,\.]\s*)?)(?:\s+(.*))?/xs', $_arg, $_match);
if (isset($_match[0]{0}) && ($_match[0]{0} == '$' || ($_match[0]{0} == '#' && $_match[0]{strlen($_match[0]) - 1} == '#') || $_match[0]{0} == "'" || $_match[0]{0} == '"' || $_match[0]{0} == '%'))
{
// process a variable
$_arg = $object->_parse_variables(array($_match[1]), array($_match[2]));
}
elseif (is_numeric($_arg))
{
// pass the number through
}
elseif (function_exists($_match[0]) || $_match[0] == "empty" || $_match[0] == "isset" || $_match[0] == "unset" || strtolower($_match[0]) == "true" || strtolower($_match[0]) == "false" || strtolower($_match[0]) == "null")
{
// pass the function through
}
elseif (empty($_arg))
{
// pass the empty argument through
}
else
{
$object->trigger_error("unidentified token '$_arg'", E_USER_ERROR, __FILE__, __LINE__);
}
break;
}
}
if($while)
{
return implode(' ', $_args);
}
else
{
if ($elseif)
{
return '<?php elseif ('.implode(' ', $_args).'): ?>';
}
else
{
return '<?php if ('.implode(' ', $_args).'): ?>';
}
}
return $_result;
}
?>