Beispiel #1
0
function smarty_block_mtif($args, $content, &$ctx, &$repeat)
{
    if (!isset($content)) {
        $result = 0;
        $name = isset($args['name']) ? $args['name'] : $args['var'];
        if (isset($name)) {
            unset($ctx->__stash['__cond_tag__']);
            # 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 an [_1] tag without a valid name attribute.", "<MT{$tag}>"));
                }
            }
            if (isset($name)) {
                $value = $ctx->__stash['vars'][$name];
                require_once "MTUtil.php";
                if (is_hash($value)) {
                    if (isset($key)) {
                        if ($key != chr(0)) {
                            $val = $value[$key];
                        } else {
                            unset($value);
                        }
                    } else {
                        $val = $value;
                    }
                } elseif (is_array($value)) {
                    if (isset($index)) {
                        if (is_numeric($index)) {
                            $val = $value[$index];
                        } else {
                            unset($value);
                            # fall through to any 'default'
                        }
                    } else {
                        $val = $value;
                    }
                } else {
                    $val = $value;
                }
            }
        } elseif (isset($args['tag'])) {
            $tag = $args['tag'];
            $tag = preg_replace('/^mt:?/i', '', $tag);
            $largs = $args;
            // local arguments without 'tag' element
            unset($largs['tag']);
            try {
                $val = $ctx->tag($tag, $largs);
            } catch (exception $e) {
                $val = '';
            }
        }
        if (!is_array($value) && preg_match('/^smarty_fun_[a-f0-9]+$/', $value)) {
            if (function_exists($val)) {
                ob_start();
                $val($ctx, array());
                $val = ob_get_contents();
                ob_end_clean();
            } else {
                $val = '';
            }
        }
        if (isset($args['tag'])) {
            $ctx->__stash['__cond_tag__'] = $args['tag'];
        } else {
            if (isset($args['name'])) {
                $var_key = $args['name'];
            } else {
                if (isset($args['var'])) {
                    $var_key = $args['var'];
                }
            }
            $ctx->__stash['__cond_name__'] = $var_key;
        }
        $ctx->__stash['__cond_value__'] = $val;
        if (array_key_exists('op', $args)) {
            $op = $args['op'];
            $rvalue = $args['value'];
            if ($op && isset($value) && !is_array($value)) {
                $val = _math_operation($op, $val, $rvalue);
                if (!isset($val)) {
                    return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", array($value, $op, $rvalue)));
                }
            }
        }
        if (array_key_exists('eq', $args)) {
            $val2 = $args['eq'];
            $result = $val == $val2 ? 1 : 0;
        } elseif (array_key_exists('ne', $args)) {
            $val2 = $args['ne'];
            $result = $val != $val2 ? 1 : 0;
        } elseif (array_key_exists('gt', $args)) {
            $val2 = $args['gt'];
            $result = $val > $val2 ? 1 : 0;
        } elseif (array_key_exists('lt', $args)) {
            $val2 = $args['lt'];
            $result = $val < $val2 ? 1 : 0;
        } elseif (array_key_exists('ge', $args)) {
            $val2 = $args['ge'];
            $result = $val >= $val2 ? 1 : 0;
        } elseif (array_key_exists('le', $args)) {
            $val2 = $args['le'];
            $result = $val <= $val2 ? 1 : 0;
        } elseif (array_key_exists('like', $args)) {
            $patt = $args['like'];
            $opt = "";
            if (preg_match("/^\\/.+\\/([si]+)?\$/", $patt, $matches)) {
                $patt = preg_replace("/^\\/|\\/([si]+)?\$/", "", $patt);
                if ($matches[1]) {
                    $opt = $matches[1];
                }
            } else {
                $patt = preg_replace("!/!", "\\/", $patt);
            }
            $result = preg_match("/{$patt}/{$opt}", $val) ? 1 : 0;
        } elseif (array_key_exists('test', $args)) {
            $expr = 'return (' . $args['test'] . ') ? 1 : 0;';
            // export vars into local variable namespace, then eval expr
            extract($ctx->__stash['vars']);
            $result = eval($expr);
            if ($result === FALSE) {
                die("error in expression [" . $args['test'] . "]");
            }
        } else {
            $result = isset($val) && $val ? 1 : 0;
        }
        return $ctx->_hdlr_if($args, $content, $ctx, $repeat, $result);
    } else {
        $vars =& $ctx->__stash['vars'];
        return $ctx->_hdlr_if($args, $content, $ctx, $repeat);
    }
}
Beispiel #2
0
function smarty_function_mtsetvar($args, &$ctx)
{
    // status: complete
    // parameters: name, value
    $name = $args['name'];
    $name or $name = $args['var'];
    if (!$name) {
        return '';
    }
    $value = $args['value'];
    $vars =& $ctx->__stash['vars'];
    if (strtolower($name) == 'page_layout') {
        # replaces page layout for current page
        require_once "MTUtil.php";
        $columns = get_page_column($value);
        $vars['page_columns'] = $columns;
        $vars['page_layout'] = $value;
    }
    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}>"));
        }
    }
    $existing = $vars[$name];
    require_once "MTUtil.php";
    if (isset($key)) {
        if (!isset($existing)) {
            $existing = array($key => $value);
        } elseif (is_hash($existing)) {
            $existing = $existing[$key];
        } else {
            return $ctx->error($ctx->mt->translate("'[_1]' is not a hash.", $name));
        }
    } elseif (isset($index)) {
        if (!isset($existing)) {
            $existing[$index] = $value;
        } elseif (is_array($existing)) {
            if (is_numeric($index)) {
                $existing = $existing[$index];
            } else {
                return $ctx->error($ctx->mt->translate("Invalid index."));
            }
        } else {
            return $ctx->error($ctx->mt->translate("'[_1]' is not an array.", $name));
        }
    }
    if (array_key_exists('append', $args) && $args['append']) {
        $value = isset($existing) ? $existing . $value : $value;
    } elseif (array_key_exists('prepend', $args) && $args['prepend']) {
        $value = isset($existing) ? $value . $existing : $value;
    } elseif (isset($existing) && array_key_exists('op', $args)) {
        $op = $args['op'];
        $value = _math_operation($op, $existing, $value);
        if (!isset($value)) {
            return $ctx->error($ctx->mt->translate("[_1] [_2] [_3] is illegal.", $existing, $op, $value));
        }
    }
    $data = $vars[$name];
    if (isset($key)) {
        if (isset($func) && 'delete' == strtolower($func)) {
            unset($data[$key]);
        } else {
            $data[$key] = $value;
        }
    } elseif (isset($index)) {
        $data[$index] = $value;
    } elseif (isset($func)) {
        if ('undef' == strtolower($func)) {
            unset($data);
        } else {
            if (isset($data) && !is_array($data)) {
                return $ctx->error($ctx->mt->translate("'[_1]' is not an array.", $name));
            }
            if (!isset($data)) {
                $data = array();
            }
            if ('push' == strtolower($func)) {
                array_push($data, $value);
            } elseif ('unshift' == strtolower($func)) {
                array_unshift($data, $value);
            } else {
                return $ctx->error($ctx->mt->translate("'[_1]' is not a valid function.", $func));
            }
        }
    } else {
        $data = $value;
    }
    $hash = $ctx->stash('__inside_set_hashvar');
    if (isset($hash)) {
        $hash[$name] = $data;
        $ctx->stash('__inside_set_hashvar', $hash);
    } else {
        if (is_array($vars)) {
            $vars[$name] = $data;
        } else {
            $vars = array($name => $data);
            $ctx->__stash['vars'] =& $vars;
        }
    }
    return '';
}
Beispiel #3
0
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/i', $m[2])) {
                global $mt;
                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));
                }
            } else {
                if (array_key_exists('to_json', $args) && $args['to_json']) {
                    if (function_exists('json_encode')) {
                        $return_val = json_encode($value);
                    } else {
                        $return_val = '';
                    }
                }
            }
        }
        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.", $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') {
            if (version_compare(phpversion(), '4.3.0', '>=')) {
                global $mt;
                $charset = $mt->config('PublishCharset');
                $return_val = htmlentities($return_val, ENT_COMPAT, $charset);
            } else {
                $return_val = htmlentities($return_val, ENT_COMPAT);
            }
        } elseif ($esc == 'url') {
            $return_val = urlencode($return_val);
            $return_val = preg_replace('/\\+/', '%20', $return_val);
        }
    }
    return $return_val;
}