root/branches/release-27/php/lib/block.mtif.php @ 1187

Revision 1187, 5.7 kB (checked in by takayama, 23 months ago)

Fixed BugId:64642
* When any emtries is not found, localized vars is restored.

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_block_mtif($args, $content, &$ctx, &$repeat) {
9    if (!isset($content)) {
10        $result = 0;
11        $name = isset($args['name'])
12          ? $args['name'] : $args['var'];
13        if (isset($name)) {
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 a [_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                }
49                elseif (is_array($value)) {
50                    if ( isset($index) ) {
51                        if (is_numeric($index)) {
52                            $val = $value[ $index ];
53                        } else {
54                            unset($value); # fall through to any 'default'
55                        }
56                    }
57                }
58                if (!isset($val))
59                    $val = $value;
60            }
61        } elseif (isset($args['tag'])) {
62            $tag = $args['tag'];
63            $tag = preg_replace('/^mt:?/i', '', $tag);
64            $largs = $args; // local arguments without 'tag' element
65            unset($largs['tag']);
66            $val = $ctx->tag($tag, $largs);
67        }
68        if ( !is_array($value)
69          && preg_match('/^smarty_fun_[a-f0-9]+$/', $value) ) {
70            if (function_exists($val)) {
71                ob_start();
72                $val($ctx, array());
73                $val = ob_get_contents();
74                ob_end_clean();
75            } else {
76                $val = '';
77            }
78        }
79
80        if (isset($args['name']))
81            $var_key = $args['name'];
82        else if(isset($args['var']))
83            $var_key = $args['var'];
84        else if(isset($args['tag']))
85            $var_key = $args['tag'];
86        require_once("function.mtsetvar.php");
87        smarty_function_mtsetvar(array('name' => '__name__', 'value' => $var_key), $ctx);
88        smarty_function_mtsetvar(array('name' => '__value__', 'value' => $val), $ctx);
89
90        if ( array_key_exists('op', $args) ) {
91            $op = $args['op'];
92            $rvalue = $args['value'];
93            if ( $op && isset($value) && !is_array($value) ) {
94                $val = _math_operation($op, $val, $rvalue);
95                if (!isset($val)) {
96                    return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $val, $op, $rvalue));
97            }}
98        }
99        if (array_key_exists('eq', $args)) {
100            $val2 = $args['eq'];
101            $result = $val == $val2 ? 1 : 0;
102        } elseif (array_key_exists('ne', $args)) {
103            $val2 = $args['ne'];
104            $result = $val != $val2 ? 1 : 0;
105        } elseif (array_key_exists('gt', $args)) {
106            $val2 = $args['gt'];
107            $result = $val > $val2 ? 1 : 0;
108        } elseif (array_key_exists('lt', $args)) {
109            $val2 = $args['lt'];
110            $result = $val < $val2 ? 1 : 0;
111        } elseif (array_key_exists('ge', $args)) {
112            $val2 = $args['ge'];
113            $result = $val >= $val2 ? 1 : 0;
114        } elseif (array_key_exists('le', $args)) {
115            $val2 = $args['le'];
116            $result = $val <= $val2 ? 1 : 0;
117        } elseif (array_key_exists('like', $args)) {
118            $patt = $args['like'];
119            $opt = "";
120            if (preg_match("/^\/.+\/([si]+)?$/", $patt, $matches)) {
121                $patt = preg_replace("/^\/|\/([si]+)?$/", "", $patt);
122                if ($matches[1])
123                    $opt = $matches[1];
124            } else {
125                $patt = preg_replace("!/!", "\\/", $patt);
126            }
127            $result = preg_match("/$patt/$opt", $val) ? 1 : 0;
128        } elseif (array_key_exists('test', $args)) {
129            $expr = 'return (' . $args['test'] . ') ? 1 : 0;';
130            // export vars into local variable namespace, then eval expr
131            extract($ctx->__stash['vars']);
132            $result = eval($expr);
133            if ($result === FALSE) {
134                die("error in expression [" . $args['test'] . "]");
135            }
136        } else {
137            $result = isset($val) && $val ? 1 : 0;
138        }
139        return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $result);
140    } else {
141        $vars =& $ctx->__stash['vars'];
142        unset($vars['__name__']);
143        unset($vars['__value__']);
144        return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
145    }
146}
147?>
Note: See TracBrowser for help on using the browser.