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