compile.compile_if.php 3.6 KB

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