root/branches/release-42/php/lib/function.mtvar.php @ 2873

Revision 2873, 6.1 kB (checked in by bchoate, 16 months ago)

Fixed indirect variable usage. BugId:80893

Line 
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
8function 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        }
114        elseif (is_array($value)) {
115            if ( isset($index) ) {
116                if (is_numeric($index)) {
117                    $return_val = $value[ $index ];
118                } else {
119                    unset($value); # fall through to any 'default'
120                }
121            }
122            elseif ( isset($func) ) {
123                $func = strtolower($func);
124                if ( 'pop' == $func ) {
125                    $return_val = array_pop($value);
126                    $vars[$name] = $value;
127                }
128                elseif ( 'shift' == $func ) {
129                    $return_val = array_shift($value);
130                    $vars[$name] = $value;
131                }
132                elseif ( 'count' == $func ) {
133                    $return_val = count($value);
134                }
135                else {
136                    return $ctx->error(
137                        $ctx->mt->translate("'[_1]' is not a valid function for an array.", $func)
138                    );
139                }
140            }
141            else {
142                if (!array_key_exists('to_json', $args) && $args['to_json']) {
143                    $glue = $args['glue'];
144                    if (!isset($glue))
145                        $glue = '';
146                    $return_val = implode($glue, $value);
147                }
148            }
149        }
150        if ( array_key_exists('op', $args) ) {
151            $op = $args['op'];
152            $rvalue = $args['value'];
153            if ( $op && isset($value) && !is_array($value) ) {
154                $return_val = _math_operation($op, $value, $rvalue);
155                if (!isset($return_val)) {
156                    return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $value, $op, $rvalue));
157            }}
158        }
159    }
160
161    if ($return_val == '') {
162        if (isset($args['default'])) {
163            $return_val = $args['default'];
164        }
165    }
166    if (isset($args['escape'])) {
167        $esc = strtolower($args['escape']);
168        if ($esc == 'js') {
169            $return_val = encode_js($return_val);
170        } elseif ($esc == 'html') {
171            if (version_compare(phpversion(), '4.3.0', '>=')) {
172                global $mt;
173                $charset = $mt->config('PublishCharset');
174                $return_val = htmlentities($return_val, ENT_COMPAT, $charset);
175            } else {
176                $return_val = htmlentities($return_val, ENT_COMPAT);
177            }
178        } elseif ($esc == 'url') {
179            $return_val = urlencode($return_val);
180            $return_val = preg_replace('/\+/', '%20', $return_val);
181        }
182    }
183    return $return_val;
184}
Note: See TracBrowser for help on using the browser.