Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Load requested plugins
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * @param array $plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
// $plugins
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_core_load_plugins($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
Chris PeBenito 696b41
    foreach ($params['plugins'] as $_plugin_info) {
Chris PeBenito 696b41
        list($_type, $_name, $_tpl_file, $_tpl_line, $_delayed_loading) = $_plugin_info;
Chris PeBenito 696b41
        $_plugin = &$smarty->_plugins[$_type][$_name];
Chris PeBenito 696b41
Chris PeBenito 696b41
        /*
Chris PeBenito 696b41
         * We do not load plugin more than once for each instance of Smarty.
Chris PeBenito 696b41
         * The following code checks for that. The plugin can also be
Chris PeBenito 696b41
         * registered dynamically at runtime, in which case template file
Chris PeBenito 696b41
         * and line number will be unknown, so we fill them in.
Chris PeBenito 696b41
         *
Chris PeBenito 696b41
         * The final element of the info array is a flag that indicates
Chris PeBenito 696b41
         * whether the dynamically registered plugin function has been
Chris PeBenito 696b41
         * checked for existence yet or not.
Chris PeBenito 696b41
         */
Chris PeBenito 696b41
        if (isset($_plugin)) {
Chris PeBenito 696b41
            if (empty($_plugin[3])) {
Chris PeBenito 696b41
                if (!is_callable($_plugin[0])) {
Chris PeBenito 696b41
                    $smarty->_trigger_fatal_error("[plugin] $_type '$_name' is not implemented", $_tpl_file, $_tpl_line, __FILE__, __LINE__);
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $_plugin[1] = $_tpl_file;
Chris PeBenito 696b41
                    $_plugin[2] = $_tpl_line;
Chris PeBenito 696b41
                    $_plugin[3] = true;
Chris PeBenito 696b41
                    if (!isset($_plugin[4])) $_plugin[4] = true; /* cacheable */
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            continue;
Chris PeBenito 696b41
        } else if ($_type == 'insert') {
Chris PeBenito 696b41
            /*
Chris PeBenito 696b41
             * For backwards compatibility, we check for insert functions in
Chris PeBenito 696b41
             * the symbol table before trying to load them as a plugin.
Chris PeBenito 696b41
             */
Chris PeBenito 696b41
            $_plugin_func = 'insert_' . $_name;
Chris PeBenito 696b41
            if (function_exists($_plugin_func)) {
Chris PeBenito 696b41
                $_plugin = array($_plugin_func, $_tpl_file, $_tpl_line, true, false);
Chris PeBenito 696b41
                continue;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $_plugin_file = $smarty->_get_plugin_filepath($_type, $_name);
Chris PeBenito 696b41
Chris PeBenito 696b41
        if (! $_found = ($_plugin_file != false)) {
Chris PeBenito 696b41
            $_message = "could not load plugin file '$_type.$_name.php'\n";
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        /*
Chris PeBenito 696b41
         * If plugin file is found, it -must- provide the properly named
Chris PeBenito 696b41
         * plugin function. In case it doesn't, simply output the error and
Chris PeBenito 696b41
         * do not fall back on any other method.
Chris PeBenito 696b41
         */
Chris PeBenito 696b41
        if ($_found) {
Chris PeBenito 696b41
            include_once $_plugin_file;
Chris PeBenito 696b41
Chris PeBenito 696b41
            $_plugin_func = 'smarty_' . $_type . '_' . $_name;
Chris PeBenito 696b41
            if (!function_exists($_plugin_func)) {
Chris PeBenito 696b41
                $smarty->_trigger_fatal_error("[plugin] function $_plugin_func() not found in $_plugin_file", $_tpl_file, $_tpl_line, __FILE__, __LINE__);
Chris PeBenito 696b41
                continue;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        /*
Chris PeBenito 696b41
         * In case of insert plugins, their code may be loaded later via
Chris PeBenito 696b41
         * 'script' attribute.
Chris PeBenito 696b41
         */
Chris PeBenito 696b41
        else if ($_type == 'insert' && $_delayed_loading) {
Chris PeBenito 696b41
            $_plugin_func = 'smarty_' . $_type . '_' . $_name;
Chris PeBenito 696b41
            $_found = true;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        /*
Chris PeBenito 696b41
         * Plugin specific processing and error checking.
Chris PeBenito 696b41
         */
Chris PeBenito 696b41
        if (!$_found) {
Chris PeBenito 696b41
            if ($_type == 'modifier') {
Chris PeBenito 696b41
                /*
Chris PeBenito 696b41
                 * In case modifier falls back on using PHP functions
Chris PeBenito 696b41
                 * directly, we only allow those specified in the security
Chris PeBenito 696b41
                 * context.
Chris PeBenito 696b41
                 */
Chris PeBenito 696b41
                if ($smarty->security && !in_array($_name, $smarty->security_settings['MODIFIER_FUNCS'])) {
Chris PeBenito 696b41
                    $_message = "(secure mode) modifier '$_name' is not allowed";
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    if (!function_exists($_name)) {
Chris PeBenito 696b41
                        $_message = "modifier '$_name' is not implemented";
Chris PeBenito 696b41
                    } else {
Chris PeBenito 696b41
                        $_plugin_func = $_name;
Chris PeBenito 696b41
                        $_found = true;
Chris PeBenito 696b41
                    }
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
            } else if ($_type == 'function') {
Chris PeBenito 696b41
                /*
Chris PeBenito 696b41
                 * This is a catch-all situation.
Chris PeBenito 696b41
                 */
Chris PeBenito 696b41
                $_message = "unknown tag - '$_name'";
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if ($_found) {
Chris PeBenito 696b41
            $smarty->_plugins[$_type][$_name] = array($_plugin_func, $_tpl_file, $_tpl_line, true, true);
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            // output error
Chris PeBenito 696b41
            $smarty->_trigger_fatal_error('[plugin] ' . $_message, $_tpl_file, $_tpl_line, __FILE__, __LINE__);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>