root/branches/release-34/php/lib/block.mtif.php @ 1876

Revision 1876, 5.7 kB (checked in by fumiakiy, 20 months ago)

Fixed the issue in which MTElse and MTElseIf did not work correctly when "name" or "tag" argument was omitted both in static and in dynamic. BugId:69678

Also fixed the bug case filed as BugId:79027 in PHP tag handlers.

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        require_once("function.mtsetvar.php");
81        if(isset($args['tag'])) {
82            smarty_function_mtsetvar(array('name' => '__cond_tag__', 'value' => $args['tag']), $ctx);
83        }
84        else {
85            if (isset($args['name']))
86                $var_key = $args['name'];
87            else if(isset($args['var']))
88                $var_key = $args['var'];
89            smarty_function_mtsetvar(array('name' => '__cond_name__', 'value' => $var_key), $ctx);
90        }
91        smarty_function_mtsetvar(array('name' => '__cond_value__', 'value' => $val), $ctx);
92
93        if ( array_key_exists('op', $args) ) {
94            $op = $args['op'];
95            $rvalue = $args['value'];
96            if ( $op && isset($value) && !is_array($value) ) {
97                $val = _math_operation($op, $val, $rvalue);
98                if (!isset($val)) {
99                    return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $val, $op, $rvalue));
100            }}
101        }
102        if (array_key_exists('eq', $args)) {
103            $val2 = $args['eq'];
104            $result = $val == $val2 ? 1 : 0;
105        } elseif (array_key_exists('ne', $args)) {
106            $val2 = $args['ne'];
107            $result = $val != $val2 ? 1 : 0;
108        } elseif (array_key_exists('gt', $args)) {
109            $val2 = $args['gt'];
110            $result = $val > $val2 ? 1 : 0;
111        } elseif (array_key_exists('lt', $args)) {
112            $val2 = $args['lt'];
113            $result = $val < $val2 ? 1 : 0;
114        } elseif (array_key_exists('ge', $args)) {
115            $val2 = $args['ge'];
116            $result = $val >= $val2 ? 1 : 0;
117        } elseif (array_key_exists('le', $args)) {
118            $val2 = $args['le'];
119            $result = $val <= $val2 ? 1 : 0;
120        } elseif (array_key_exists('like', $args)) {
121            $patt = $args['like'];
122            $opt = "";
123            if (preg_match("/^\/.+\/([si]+)?$/", $patt, $matches)) {
124                $patt = preg_replace("/^\/|\/([si]+)?$/", "", $patt);
125                if ($matches[1])
126                    $opt = $matches[1];
127            } else {
128                $patt = preg_replace("!/!", "\\/", $patt);
129            }
130            $result = preg_match("/$patt/$opt", $val) ? 1 : 0;
131        } elseif (array_key_exists('test', $args)) {
132            $expr = 'return (' . $args['test'] . ') ? 1 : 0;';
133            // export vars into local variable namespace, then eval expr
134            extract($ctx->__stash['vars']);
135            $result = eval($expr);
136            if ($result === FALSE) {
137                die("error in expression [" . $args['test'] . "]");
138            }
139        } else {
140            $result = isset($val) && $val ? 1 : 0;
141        }
142        return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $result);
143    } else {
144        $vars =& $ctx->__stash['vars'];
145        return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
146    }
147}
148?>
Note: See TracBrowser for help on using the browser.