PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/block.mtif.php

http://github.com/movabletype/movabletype
PHP | 171 lines | 154 code | 8 blank | 9 comment | 48 complexity | 0ff833cbc1e2f40becbfe58913de0d9d MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php
  2. # Movable Type (r) (C) 2001-2020 Six Apart Ltd. All Rights Reserved.
  3. # This code cannot be redistributed without permission from www.sixapart.com.
  4. # For more information, consult your Movable Type license.
  5. #
  6. # $Id$
  7. function smarty_block_mtif($args, $content, &$ctx, &$repeat) {
  8. if (!isset($content)) {
  9. $result = 0;
  10. $name = isset($args['name'])
  11. ? $args['name'] : $args['var'];
  12. if (isset($name)) {
  13. unset($ctx->__stash['__cond_tag__']);
  14. # pick off any {...} or [...] from the name.
  15. if (preg_match('/^(.+)([\[\{])(.+)[\]\}]$/', $name, $matches)) {
  16. $name = $matches[1];
  17. $br = $matches[2];
  18. $ref = $matches[3];
  19. if (preg_match('/^\\\\\$(.+)/', $ref, $ref_matches)) {
  20. $ref = $vars[$ref_matches[1]];
  21. if (!isset($ref))
  22. $ref = chr(0);
  23. }
  24. $br == '[' ? $index = $ref : $key = $ref;
  25. } else {
  26. if (array_key_exists('index', $args))
  27. $index = $args['index'];
  28. else if (array_key_exists('key', $args))
  29. $key = $args['key'];
  30. }
  31. if (preg_match('/^$/', $name)) {
  32. $name = $vars[$name];
  33. if (!isset($name))
  34. return $ctx->error($ctx->mt->translate(
  35. "You used an [_1] tag without a valid name attribute.", "<MT$tag>" ));
  36. }
  37. if (isset($name)) {
  38. $value = $ctx->__stash['vars'][$name];
  39. require_once("MTUtil.php");
  40. if (is_hash($value)) {
  41. if ( isset($key) ) {
  42. if ($key != chr(0)) {
  43. $val = $value[$key];
  44. } else {
  45. unset($value);
  46. }
  47. }
  48. else {
  49. $val = $value;
  50. }
  51. }
  52. elseif (is_array($value)) {
  53. if ( isset($index) ) {
  54. if (is_numeric($index)) {
  55. $val = $value[ $index ];
  56. } else {
  57. unset($value); # fall through to any 'default'
  58. }
  59. }
  60. else {
  61. $val = $value;
  62. }
  63. }
  64. else {
  65. $val = $value;
  66. }
  67. }
  68. } elseif (isset($args['tag'])) {
  69. $tag = $args['tag'];
  70. $tag = preg_replace('/^mt:?/i', '', $tag);
  71. $largs = $args; // local arguments without 'tag' element
  72. unset($largs['tag']);
  73. // Disable error handler temporarily
  74. // for disabling trigger_error function.
  75. set_error_handler('_dummy_error_handler');
  76. try {
  77. $val = $ctx->tag($tag, $largs);
  78. } catch (exception $e) {
  79. $val = '';
  80. }
  81. restore_error_handler();
  82. }
  83. if ( !is_array($value)
  84. && preg_match('/^smarty_fun_[a-f0-9]+$/', $value) ) {
  85. if (function_exists($val)) {
  86. ob_start();
  87. $val($ctx, array());
  88. $val = ob_get_contents();
  89. ob_end_clean();
  90. } else {
  91. $val = '';
  92. }
  93. }
  94. if(isset($args['tag']))
  95. $ctx->__stash['__cond_tag__'] = $args['tag'];
  96. else {
  97. if (isset($args['name']))
  98. $var_key = $args['name'];
  99. else if(isset($args['var']))
  100. $var_key = $args['var'];
  101. $ctx->__stash['__cond_name__'] = $var_key;
  102. }
  103. $ctx->__stash['__cond_value__'] = $val;
  104. if ( array_key_exists('op', $args) ) {
  105. $op = $args['op'];
  106. $rvalue = $args['value'];
  107. if ( $op && isset($value) && !is_array($value) ) {
  108. $val = _math_operation($op, $val, $rvalue);
  109. if (!isset($val)) {
  110. return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", array( $value, $op, $rvalue )));
  111. }
  112. }
  113. }
  114. if (array_key_exists('eq', $args)) {
  115. $val2 = $args['eq'];
  116. $result = $val == $val2 ? 1 : 0;
  117. } elseif (array_key_exists('ne', $args)) {
  118. $val2 = $args['ne'];
  119. $result = $val != $val2 ? 1 : 0;
  120. } elseif (array_key_exists('gt', $args)) {
  121. $val2 = $args['gt'];
  122. $result = $val > $val2 ? 1 : 0;
  123. } elseif (array_key_exists('lt', $args)) {
  124. $val2 = $args['lt'];
  125. $result = $val < $val2 ? 1 : 0;
  126. } elseif (array_key_exists('ge', $args)) {
  127. $val2 = $args['ge'];
  128. $result = $val >= $val2 ? 1 : 0;
  129. } elseif (array_key_exists('le', $args)) {
  130. $val2 = $args['le'];
  131. $result = $val <= $val2 ? 1 : 0;
  132. } elseif (array_key_exists('like', $args)) {
  133. $patt = $args['like'];
  134. $opt = "";
  135. if (preg_match("/^\/.+\/([si]+)?$/", $patt, $matches)) {
  136. $patt = preg_replace("/^\/|\/([si]+)?$/", "", $patt);
  137. if ($matches[1])
  138. $opt = $matches[1];
  139. } else {
  140. $patt = preg_replace("!/!", "\\/", $patt);
  141. }
  142. $result = preg_match("/$patt/$opt", $val) ? 1 : 0;
  143. } elseif (array_key_exists('test', $args)) {
  144. $expr = 'return (' . $args['test'] . ') ? 1 : 0;';
  145. // export vars into local variable namespace, then eval expr
  146. extract($ctx->__stash['vars']);
  147. $result = eval($expr);
  148. if ($result === FALSE) {
  149. die("error in expression [" . $args['test'] . "]");
  150. }
  151. } else {
  152. $result = isset($val) && $val ? 1 : 0;
  153. }
  154. return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $result);
  155. } else {
  156. $vars =& $ctx->__stash['vars'];
  157. return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
  158. }
  159. }
  160. function _dummy_error_handler() {
  161. return TRUE;
  162. }
  163. ?>