PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/function.mtvar.php

http://github.com/openmelody/melody
PHP | 194 lines | 179 code | 7 blank | 8 comment | 76 complexity | f331ee4bf7f4e251ccfa0ba3f9b23f8f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. # Movable Type (r) Open Source (C) 2001-2010 Six Apart, Ltd.
  3. # This program is distributed under the terms of the
  4. # GNU General Public License, version 2.
  5. #
  6. # $Id$
  7. function smarty_function_mtvar($args, &$ctx) {
  8. // status: complete
  9. // parameters: name
  10. if ( array_key_exists('value', $args)
  11. && !array_key_exists('op', $args) ) {
  12. require_once("function.mtsetvar.php");
  13. return smarty_function_mtsetvar($args, $ctx);
  14. }
  15. require_once("MTUtil.php");
  16. $vars =& $ctx->__stash['vars'];
  17. $value = '';
  18. $name = $args['name'];
  19. $name or $name = $args['var'];
  20. if (preg_match('/^(config|request)\.(.+)$/i', $name, $m)) {
  21. if (strtolower($m[1]) == 'config') {
  22. if (!preg_match('/password/i', $m[2])) {
  23. global $mt;
  24. return $mt->config[strtolower($m[2])];
  25. }
  26. }
  27. elseif (strtolower($m[1]) == 'request') {
  28. return $_REQUEST[$m[2]];
  29. }
  30. }
  31. if (!$name) return '';
  32. if (preg_match('/^(\w+)\((.+)\)$/', $name, $matches)) {
  33. $func = $matches[1];
  34. $name = $matches[2];
  35. } else {
  36. if (array_key_exists('function', $args))
  37. $func = $args['function'];
  38. }
  39. # pick off any {...} or [...] from the name.
  40. if (preg_match('/^(.+)([\[\{])(.+)[\]\}]$/', $name, $matches)) {
  41. $name = $matches[1];
  42. $br = $matches[2];
  43. $ref = $matches[3];
  44. if (preg_match('/^\\\\\$(.+)/', $ref, $ref_matches)) {
  45. $ref = $vars[$ref_matches[1]];
  46. if (!isset($ref))
  47. $ref = chr(0);
  48. }
  49. $br == '[' ? $index = $ref : $key = $ref;
  50. } else {
  51. if (array_key_exists('index', $args))
  52. $index = $args['index'];
  53. else if (array_key_exists('key', $args))
  54. $key = $args['key'];
  55. }
  56. if (preg_match('/^\$/', $name)) {
  57. $name = $vars[$name];
  58. if (!isset($name))
  59. return $ctx->error($ctx->mt->translate(
  60. "You used a [_1] tag without a valid name attribute.", "<MT$tag>" ));
  61. }
  62. if (isset($vars[$name]))
  63. $value = $vars[$name];
  64. if ( !is_array($value)
  65. && preg_match('/^smarty_fun_[a-f0-9]+$/', $value) ) {
  66. if (function_exists($value)) {
  67. ob_start();
  68. $value($ctx, array());
  69. $value = ob_get_contents();
  70. ob_end_clean();
  71. } else {
  72. $value = '';
  73. }
  74. }
  75. $return_val = $value;
  76. if (isset($name)) {
  77. if (is_hash($value)) {
  78. if ( isset($key) ) {
  79. if ( isset($func) ) {
  80. if ( 'delete' == strtolower($func) ) {
  81. $return_val = $value[$key];
  82. unset($value[$key]);
  83. $vars[$name] = $value;
  84. } else {
  85. return $ctx->error(
  86. $ctx->mt->translate("'[_1]' is not a valid function for a hash.", $func)
  87. );
  88. }
  89. } else {
  90. if ($key != chr(0)) {
  91. $return_val = $value[$key];
  92. } else {
  93. unset($value);
  94. }
  95. }
  96. }
  97. elseif ( isset($func) ) {
  98. if ( 'count' == strtolower($func) ) {
  99. $return_val = count(array_keys($value));
  100. }
  101. else {
  102. return $ctx->error(
  103. $ctx->mt->translate("'[_1]' is not a valid function for a hash.", $func)
  104. );
  105. }
  106. }
  107. else {
  108. if (array_key_exists('to_json', $args) && $args['to_json']) {
  109. if (function_exists('json_encode')) {
  110. $return_val = json_encode($value);
  111. } else {
  112. $return_val = '';
  113. }
  114. }
  115. }
  116. }
  117. elseif (is_array($value)) {
  118. if ( isset($index) ) {
  119. if (is_numeric($index)) {
  120. $return_val = $value[ $index ];
  121. } else {
  122. unset($value); # fall through to any 'default'
  123. }
  124. }
  125. elseif ( isset($func) ) {
  126. $func = strtolower($func);
  127. if ( 'pop' == $func ) {
  128. $return_val = array_pop($value);
  129. $vars[$name] = $value;
  130. }
  131. elseif ( 'shift' == $func ) {
  132. $return_val = array_shift($value);
  133. $vars[$name] = $value;
  134. }
  135. elseif ( 'count' == $func ) {
  136. $return_val = count($value);
  137. }
  138. else {
  139. return $ctx->error(
  140. $ctx->mt->translate("'[_1]' is not a valid function for an array.", $func)
  141. );
  142. }
  143. }
  144. else {
  145. if (array_key_exists('to_json', $args) && $args['to_json']) {
  146. if (function_exists('json_encode')) {
  147. $return_val = json_encode($value);
  148. } else {
  149. $return_val = '';
  150. }
  151. }
  152. }
  153. }
  154. if ( array_key_exists('op', $args) ) {
  155. $op = $args['op'];
  156. $rvalue = $args['value'];
  157. if ( $op && isset($value) && !is_array($value) ) {
  158. $return_val = _math_operation($op, $value, $rvalue);
  159. if (!isset($return_val)) {
  160. return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $value, $op, $rvalue));
  161. }}
  162. }
  163. }
  164. if ($return_val == '') {
  165. if (isset($args['default'])) {
  166. $return_val = $args['default'];
  167. }
  168. }
  169. if (isset($args['escape'])) {
  170. $esc = strtolower($args['escape']);
  171. if ($esc == 'js') {
  172. $return_val = encode_js($return_val);
  173. } elseif ($esc == 'html') {
  174. if (version_compare(phpversion(), '4.3.0', '>=')) {
  175. global $mt;
  176. $charset = $mt->config('PublishCharset');
  177. $return_val = htmlentities($return_val, ENT_COMPAT, $charset);
  178. } else {
  179. $return_val = htmlentities($return_val, ENT_COMPAT);
  180. }
  181. } elseif ($esc == 'url') {
  182. $return_val = urlencode($return_val);
  183. $return_val = preg_replace('/\+/', '%20', $return_val);
  184. }
  185. }
  186. return $return_val;
  187. }