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