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