root/branches/release-34/php/lib/MTViewer.php @ 1799

Revision 1799, 15.8 kB (checked in by takayama, 20 months ago)

Fixed BugId:69678
* Fixed a condition bug

  • Property svn:keywords set to Author Date Id Revision
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
8include_once("Smarty.class.php");
9class MTViewer extends Smarty {
10    var $varstack = array();
11    var $__stash;
12    var $mt;
13    var $last_ts = 0;
14    var $id;
15
16    var $path_sep;
17
18    var $conditionals = array(
19        'mtparentcategory' => 1,
20        'mttoplevelparent' => 1,
21        'mtunless' => 1,
22        'mtfolderheader' => 1,
23        'mtfolderfooter' => 1,
24        'mtpagesheader' => 1,
25        'mtpagesfooter' => 1,
26        'mthassubfolders' => 1,
27        'mthasparentfolders' => 1,
28        'mthasparentfolder' => 1,
29        'mtauthorhasentry' => 1,
30        'mtauthorhaspage' => 1,
31    );
32    var $sanitized = array(
33        'mtcommentauthor' => 1,
34        'mtcommentemail' => 1,
35        'mtcommenturl' => 1,
36        'mtcommentbody' => 1,
37        'mtpingtitle' => 1,
38        'mtpingurl' => 1,
39        'mtpingexcerpt' => 1,
40        'mtpingblogname' => 1,
41    );
42    var $nofollowed = array(
43        'mtcommentauthorlink' => 1,
44        'mtcommenturl' => 1,
45        'mtcommentbody' => 1,
46        'mtpings' => 1,
47    );
48    var $global_attr = array(
49        'filters' => 1,
50        'trim_to' => 1,
51        'trim' => 1,
52        'ltrim' => 1,
53        'rtrim' => 1,
54        'decode_html' => 1,
55        'decode_xml' => 1,
56        'remove_html' => 1,
57        'dirify' => 1,
58        'sanitize' => 1,
59        'encode_html' => 1,
60        'encode_xml' => 1,
61        'encode_js' => 1,
62        'encode_php' => 1,
63        'encode_url' => 1,
64        'upper_case' => 1,
65        'lower_case' => 1,
66        'strip_linefeeds' => 1,
67        'space_pad' => 1,
68        'zero_pad' => 1,
69        'sprintf' => 1,
70        'wrap_text' => 1,
71        'setvar' => 1,
72         # native smarty modifiers
73        'regex_replace' => 1,
74        'capitalize' => 1,
75        'count_characters' => 1,
76        'cat' => 1,
77        'count_paragraphs' => 1,
78        'count_sentences' => 1,
79        'count_words' => 1,
80        'date_format' => 1,
81        '_default' => 'default',
82        'escape' => 1,
83        'indent' => 1,
84        'nl2br' => 1,
85        'replace' => 1,
86        'spacify' => 1,
87        'string_format' => 1,
88        'strip' => 1,
89        'strip_tags' => 1,
90        'truncate' => 1,
91        'wordwrap' => 1,
92    );
93    var $needs_tokens = array(
94        'mtsubcategories' => 1,
95        'mttoplevelcategories' => 1,
96        'mtsubfolers' => 1,
97        'mttoplevelfolders' => 1,
98        'mtcommentreplies' => 1,
99        'mtsetvartemplate' => 1,
100    );
101
102    function MTViewer(&$mt) {
103        // prevents an unknown index error within Smarty.class.php
104        $this->id = md5(uniqid('MTViewer',true));
105        $_COOKIE['SMARTY_DEBUG'] = 0;
106        $GLOBALS['HTTP_COOKIE_VARS']['SMARTY_DEBUG'] = 0;
107        $this->path_sep = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') ? ';' : ':';
108        $this->Smarty();
109        $this->mt =& $mt;
110        $this->__stash =& $this->_tpl_vars;
111        $this->left_delimiter = "{{";
112        $this->right_delimiter = "}}";
113        $this->load_filter('pre', 'mt_to_smarty');
114        $this->register_block('mtdynamic', array(&$this, 'smarty_block_dynamic'),
115                              false);
116        $this->register_block('mtelse', array(&$this, 'smarty_block_else'));
117        $this->register_block('mtelseif', array(&$this, 'smarty_block_elseif'));
118
119        # Unregister the 'core' regex_replace so we can replace it
120        $this->register_modifier('regex_replace', array(&$this, 'regex_replace'));
121    }
122
123    function add_plugin_dir($plugin_dir) {
124        ini_set('include_path', $plugin_dir . $this->path_sep . ini_get('include_path'));
125        $this->plugins_dir[] = $plugin_dir;
126    }
127
128    function regex_replace($string, $search, $replace) {
129        if (preg_match('!([a-zA-Z\s]+)$!s', $search, $match) && (preg_match('/[eg]/', $match[1]))) {
130            if (strpos($match[1], "g") !== false)
131                $global = 1;
132            /* remove eval-modifier from $search */
133            $search = substr($search, 0, -strlen($match[1])) . preg_replace('![eg\s]+!', '', $match[1]);
134        }
135        return preg_replace($search, $replace, $string, $global ? -1 : 1);
136    }
137
138    function add_token_tag($name) {
139        $this->needs_tokens[$name] = 1;
140    }
141
142    function add_conditional_tag($name, $code = null, $cacheable = null, $cache_attrs = null) {
143        $this->conditionals[$name] = 1;
144        if (isset($code)) {
145            $this->register_block($name, $code, $cacheable, $cache_attrs);
146        }
147    }
148
149    function add_global_filter($name, $code = null) {
150        $this->global_attr[$name] = 1;
151        if (isset($code)) {
152            $this->register_modifier($name, $code);
153        }
154    }
155
156    function error($err, $error_type = E_USER_ERROR) {
157        trigger_error($err, $error_type);
158        return '';
159    }
160
161    function this_tag() {
162        $ts = $this->_tag_stack[count($this->_tag_stack)-1];
163        if ($ts) {
164            return $ts[0];
165        } else {
166            return null;
167        }
168    }
169
170    function stash($name,$value=null) {
171        if(isset($this->__stash[$name]))
172            $old_val = $this->__stash[$name];
173        else
174            $old_val = null;
175        if(func_num_args() > 1)
176            $this->__stash[$name] = $value;
177        return $old_val;
178    }
179
180    function localize($vars) {
181        foreach ($vars as $v) {
182            if (!isset($this->varstack[$v])) $this->varstack[$v] = array();
183            $this->varstack[$v][] = isset($this->__stash[$v]) ? $this->__stash[$v] : null;
184        }
185    }
186
187    function restore($vars) {
188        foreach ($vars as $v) {
189            $this->__stash[$v] = (isset($this->varstack[$v]) && count($this->varstack[$v]) > 0) ? array_pop($this->varstack[$v]) : null;
190        }
191    }
192
193    function last_ts($ts = 0) {
194        if ($ts > 0) {
195            $ts = preg_replace('/[ :-]/', '', $ts);
196            if ($ts > $this->last_ts) {
197                $this->last_ts = $ts;
198            }
199        }
200        return $this->last_ts;
201    }
202
203    function smarty_block_dynamic($param, $content, &$smarty) {
204        return $content;
205    }
206
207    function _hdlr_if($args, $content, &$ctx, &$repeat, $cond_tag = 1) {
208        if (!isset($content)) {
209            if (!isset($args['elseif'])) {
210                $ctx->localize(array('conditional', 'else_content', 'elseif_content', 'elseif_conditional'));
211                unset($ctx->_tpl_vars['conditional']);
212                unset($ctx->_tpl_vars['else_content']);
213                unset($ctx->_tpl_vars['elseif_content']);
214                unset($ctx->_tpl_vars['elseif_conditional']);
215            }
216            if ($cond_tag == '1' or $cond_tag == '0')
217                $ctx->_tpl_vars['conditional'] = $cond_tag;
218            else
219                $ctx->_tpl_vars['conditional'] = $ctx->_tpl_vars[$cond_tag];
220        } else {
221            if (!$ctx->_tpl_vars['conditional']) {
222                if (isset($ctx->_tpl_vars['else_content'])) {
223                    $content = $ctx->_tpl_vars['else_content'];
224                } else {
225                    $content = '';
226                }
227            }
228            else {
229                if (isset($ctx->_tpl_vars['elseif_content'])) {
230                    $content = $ctx->_tpl_vars['elseif_content'];
231                }
232            }
233            if (!isset($args['elseif'])) {
234                $ctx->restore(array('conditional', 'else_content', 'elseif_content', 'elseif_conditional'));
235            }
236        }
237        return $content;
238    }
239
240    function smarty_block_elseif($args, $content, &$ctx, &$repeat) {
241        return $this->smarty_block_else($args, $content, $ctx, $repeat);
242    }
243
244    function smarty_block_else($args, $content, &$ctx, &$repeat) {
245        if (isset($ctx->_tpl_vars['elseif_content'])
246            or ($ctx->_tpl_vars['conditional'])) {
247            $repeat = false;
248            return '';
249        }
250        if (!isset($args['name']) && !isset($args['var']) && !isset($args['tag'])) {
251            require_once("function.mtgetvar.php");
252            $var = smarty_function_mtgetvar(array('name' => '__name__'), $ctx);
253            if (isset($var) && $var != '')
254                $args['name'] = $var;
255        }
256        if (count($args) >= 1) { # else-if case
257            require_once("block.mtif.php");
258            $args['elseif'] = 1;
259            if (!isset($content)) {
260                $out = smarty_block_mtif($args, $content, $ctx, $repeat);
261                if ($ctx->_tpl_vars['conditional']) {
262                    $ctx->_tpl_vars['elseif_conditional'] = 1;
263                    unset($ctx->_tpl_vars['conditional']);
264                }
265            } else {
266                // $out = smarty_block_mtif($args, $content, $ctx, $repeat);
267                if ($ctx->_tpl_vars['elseif_conditional']) {
268                    $ctx->_tpl_vars['elseif_content'] = $content;
269                    $ctx->_tpl_vars['conditional'] = 1;
270                }
271            }
272            return '';
273        }
274        if (!isset($content)) {
275            if ($ctx->_tpl_vars['conditional'])
276                $repeat = false;
277        } else {
278            $else_content = $ctx->_tpl_vars['else_content'];
279            $else_content .= $content;
280            $ctx->_tpl_vars['else_content'] = $else_content;
281        }
282        return '';
283    }
284
285    function _hdlr_date($args, &$ctx) {
286        $ts = null;
287        if (isset($args['ts'])) {
288            $ts = $args['ts'];
289        }
290        $ts or $ts = $ctx->stash('current_timestamp');
291        $ts = preg_replace('![^0-9]!', '', $ts);
292        $blog = $ctx->stash('blog');
293        if ($ts == '') {
294            $t = time();
295            if ($args['utc']) {
296                $ts = gmtime($t);
297            } else {
298                $ts = offset_time_list($t, $blog);
299            }
300            $ts = sprintf("%04d%02d%02d%02d%02d%02d",
301                $ts[5]+1900, $ts[4]+1, $ts[3], $ts[2], $ts[1], $ts[0]);
302        }
303        if (isset($args['utc'])) {
304            if (!is_array($blog)) {
305                $blog = $ctx->mt->db->fetch_blog($blog);
306            }
307            preg_match('/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/', $ts, $matches);
308            list($all, $y, $mo, $d, $h, $m, $s) = $matches;
309            $so = $blog['blog_server_offset'];
310            $timelocal = mktime($h, $m, $s, $mo, $d, $y);
311            $localtime = localtime($timelocal);
312            if ($localtime[8])
313                $so += 1;
314            $partial_hour_offset = 60 * abs($so - intval($so));
315            $four_digit_offset = sprintf('%s%02d%02d', $so < 0 ? '-' : '+',
316                                         abs($so), $partial_hour_offset);
317            $ts = gmdate('YmdHis', strtotime("$y-$mo-$d $h:$m:$s $four_digit_offset"));
318        }
319        if (isset($args['format_name'])) {
320            if ($format = $args['format_name']) {
321                $tz = 'Z';
322                if (!$args['utc']) {
323                    $blog = $ctx->stash('blog');
324                    if (!is_array($blog)) {
325                        $blog = $ctx->mt->db->fetch_blog($blog);
326                    }
327                    $so = $blog['blog_server_offset'];
328                    $partial_hour_offset = 60 * abs($so - intval($so));
329                    if ($format == 'rfc822') {
330                        $tz = sprintf("%s%02d%02d", $so < 0 ? '-' : '+',
331                                  abs($so), $partial_hour_offset);
332                    }
333                    elseif ($format == 'iso8601') {
334                        $tz = sprintf("%s%02d:%02d", $so < 0 ? '-' : '+',
335                                  abs($so), $partial_hour_offset);
336                    }
337                }
338                if ($format == 'rfc822') {
339                    $args['format'] = '%a, %d %b %Y %H:%M:%S ' . $tz;
340                    $args['language'] = 'en';
341                }
342                elseif ($format == 'iso8601') {
343                    $args['format'] = '%Y-%m-%dT%H:%M:%S'. $tz;
344                }
345            }
346        }
347        if (!isset($args['format'])) $args['format'] = null;
348        require_once("MTUtil.php");
349        return format_ts($args['format'], $ts, $blog, isset($args['language']) ? $args['language'] : null);
350    }
351
352    function tag($tag, $args = array()) {
353        $tag = preg_replace('/^mt:?/i', '', strtolower($tag));
354        if ((array_key_exists('mt' . $tag, $this->conditionals)) || (preg_match('/^if/i', $tag) || preg_match('/^has/', $tag) || preg_match('/[a-z](header|footer|previous|next)$/i', $tag))) {
355            list($hdlr) = $this->handler_for($tag);
356            if (!$hdlr) {
357                $fntag = 'smarty_block_mt' . $tag;
358                if (!function_exists($fntag))
359                    @include_once("block.mt$tag.php");
360                if (function_exists($fntag))
361                    $hdlr = $fntag;
362            }
363            if ($hdlr) {
364                $hdlr($args, NULL, $this, $repeat = true);
365                if ($repeat) {
366                    $content = 'true';
367                    $this->_tag_stack[] = array("mt$tag", $args);
368                    $content = $hdlr($args, $content, $this, $repeat = false);
369                    array_pop($this->_tag_stack);
370                    return isset($content) && ($content === 'true');
371                } else {
372                    return false;
373                }
374            }
375        } else {
376            list($hdlr) = $this->handler_for("mt" . $tag);
377            if (!$hdlr) {
378                $fntag = 'smarty_function_mt'.$tag;
379                if (!function_exists($fntag))
380                    @include_once("function.mt$tag.php");
381                if (function_exists($fntag))
382                    $hdlr = $fntag;
383            }
384            if ($hdlr) {
385                $this->_tag_stack[] = array("mt$tag", $args);
386                $content = $hdlr($args, $this);
387                foreach ($args as $k => $v) {
388                    if (array_key_exists($k, $this->global_attr)) {
389                        $fnmod = 'smarty_modifier_' . $k;
390                        if (!function_exists($fnmod))
391                            $this->load_modifier($k);
392                        if (function_exists($fnmod))
393                            $content = $fnmod($content, $v);
394                    }
395                }
396                array_pop($this->_tag_stack);
397                return $content;
398            }
399        }
400        return $this->error("Tag &lt;mt$tag&gt; does not exist.");
401    }
402
403    function load_modifier($name) {
404        $params = array('plugins' => array(array('modifier', $name, null, null, false)));
405        smarty_core_load_plugins($params, $this);
406        return true;
407    }
408
409    function register_tag_handler($tag, $fn, $type) {
410        if (substr($tag, 0, 2) != 'mt') {
411            $tag = 'mt' . $tag;
412        }
413        if ($type == 'block')
414            $this->register_block($tag, $fn);
415        elseif ($type == 'function')
416            $this->register_function($tag, $fn);
417        $old_handler = $this->_handlers[$tag];
418        $this->_handlers[$tag] = array( $fn, $type );
419        if ($old_handler) {
420            $fn = $old_handler[0];
421        } else {
422            if ($type == 'block')
423                $fn = create_function('$args, $content, &$ctx, &$repeat', 'if (!isset($content)) @include_once "block.' . $tag . '.php"; if (function_exists("smarty_block_' . $tag . '")) { return smarty_block_' . $tag . '($args, $content, $ctx, $repeat); } $repeat = false; return "";');
424            elseif ($type == 'function') {
425                $fn = create_function('$args, &$ctx', '@include_once "function.' . $tag . '.php"; if (function_exists("smarty_function_' . $tag . '")) { return smarty_function_' . $tag . '($args, $ctx); } return "";');
426            }
427        }
428        return $fn;
429    }
430    function add_container_tag($tag, $fn = null) {
431        return $this->register_tag_handler($tag, $fn, 'block');
432    }
433    function add_tag($tag, $fn) {
434        return $this->register_tag_handler($tag, $fn, 'function');
435    }
436    function handler_for($tag) {
437        if (isset($this->_handlers[$tag]))
438            return $this->_handlers[$tag];
439        else
440            return null;
441    }
442}
443?>
Note: See TracBrowser for help on using the browser.