/** * Takes unknown classes and loads plugin files for them * class name format: Smarty_PluginType_PluginName * plugin filename format: plugintype.pluginname.php * * @param \Smarty $smarty * @param string $plugin_name class plugin name to load * @param bool $check check if already loaded * * @return bool|string * @throws \SmartyException */ public static function loadPlugin(Smarty $smarty, $plugin_name, $check) { // if function or class exists, exit silently (already loaded) if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) { return true; } // Plugin name is expected to be: Smarty_[Type]_[Name] $_name_parts = explode('_', $plugin_name, 3); // class name must have three parts to be valid plugin // count($_name_parts) < 3 === !isset($_name_parts[2]) if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') { throw new SmartyException("plugin {$plugin_name} is not a valid name format"); } // if type is "internal", get plugin from sysplugins if (strtolower($_name_parts[1]) == 'internal') { $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php'; if (isset($smarty->_is_file_cache[$file]) ? $smarty->_is_file_cache[$file] : ($smarty->_is_file_cache[$file] = is_file($file))) { require_once $file; return $file; } else { return false; } } // plugin filename is expected to be: [type].[name].php $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php"; // loop through plugin dirs and find the plugin foreach ($smarty->getPluginsDir() as $_plugin_dir) { $names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename)); foreach ($names as $file) { if (isset($smarty->_is_file_cache[$file]) ? $smarty->_is_file_cache[$file] : ($smarty->_is_file_cache[$file] = is_file($file))) { require_once $file; return $file; } if ($smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_plugin_dir)) { // try PHP include_path $file = Smarty_Internal_Get_Include_Path::getIncludePath($file); if ($file !== false) { require_once $file; return $file; } } } } // no plugin loaded return false; }
/** * build template filepath by traversing the template_dir array * * @param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object * * @return string fully qualified filepath * @throws SmartyException */ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = $source->name; // absolute file ? if ($file[0] == '/' || $file[1] == ':') { $file = $source->smarty->_realpath($file); return is_file($file) ? $file : false; } // go relative to a given template? if ($file[0] == '.' && $_template && $_template->parent instanceof Smarty_Internal_Template && preg_match('#^[.]{1,2}[\\\\/]#', $file)) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $path = dirname($_template->parent->source->filepath) . DS . $file; // normalize path $path = $source->smarty->_realpath($path); // files relative to a template only get one shot return is_file($path) ? $path : false; } $_directories = $source->smarty->getTemplateDir(null, $source->isConfig); // template_dir index? if ($file[0] == '[' && preg_match('#^\\[([^\\]]+)\\](.+)$#', $file, $fileMatch)) { $index = $fileMatch[1]; $_directory = null; // try string indexes if (isset($_directories[$index])) { $_directory = $_directories[$index]; } elseif (is_numeric($index)) { // try numeric index $index = (int) $index; if (isset($_directories[$index])) { $_directory = $_directories[$index]; } else { // try at location index $keys = array_keys($_directories); $_directory = $_directories[$keys[$index]]; } } if ($_directory) { $path = $_directory . $fileMatch[2]; $path = $source->smarty->_realpath($path); if (is_file($path)) { return $path; } } else { // index not found return false; } } // relative file name? foreach ($_directories as $_directory) { $path = $_directory . $file; if (is_file($path)) { return $source->smarty->_realpath($path); } } // Could be relative to cwd $path = $source->smarty->_realpath($file); if (is_file($path)) { return $path; } // Use include path ? if ($source->smarty->use_include_path) { return Smarty_Internal_Get_Include_Path::getIncludePath($_directories, $file, $source->smarty); } return false; }
/** * build template filepath by traversing the template_dir array * * @param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object * @return string fully qualified filepath * @throws SmartyException if default template handler is registered but not callable */ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = $source->name; if ($source instanceof Smarty_Config_Source) { $_directories = $source->smarty->getConfigDir(); $_default_handler = $source->smarty->default_config_handler_func; } else { $_directories = $source->smarty->getTemplateDir(); $_default_handler = $source->smarty->default_template_handler_func; } // go relative to a given template? $_file_is_dotted = $file[0] == '.' && ($file[1] == '.' || $file[1] == '/' || $file[1] == "\\"); if ($_template && $_template->parent instanceof Smarty_Internal_Template && $_file_is_dotted) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $file = dirname($_template->parent->source->filepath) . DS . $file; $_file_exact_match = true; if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { // the path gained from the parent template is relative to the current working directory // as expansions (like include_path) have already been done $file = getcwd() . DS . $file; } } // resolve relative path if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { // don't we all just love windows? $_path = str_replace('\\', '/', $file); $_was_relative_prefix = $file[0] == '.' ? substr($file, 0, strpos($_path, '/')) : null; $_path = DS . trim($file, '/'); $_was_relative = true; } else { // don't we all just love windows? $_path = str_replace('\\', '/', $file); } $_path = $this->normalizePath($_path, false); if (DS != '/') { // don't we all just love windows? $_path = str_replace('/', '\\', $_path); } // revert to relative if (isset($_was_relative)) { if (isset($_was_relative_prefix)) { $_path = $_was_relative_prefix . $_path; } else { $_path = substr($_path, 1); } } // this is only required for directories $file = rtrim($_path, '/\\'); // files relative to a template only get one shot if (isset($_file_exact_match)) { return $this->fileExists($source, $file) ? $file : false; } // template_dir index? if (preg_match('#^\\[(?P<key>[^\\]]+)\\](?P<file>.+)$#', $file, $match)) { $_directory = null; // try string indexes if (isset($_directories[$match['key']])) { $_directory = $_directories[$match['key']]; } else { if (is_numeric($match['key'])) { // try numeric index $match['key'] = (int) $match['key']; if (isset($_directories[$match['key']])) { $_directory = $_directories[$match['key']]; } else { // try at location index $keys = array_keys($_directories); $_directory = $_directories[$keys[$match['key']]]; } } } if ($_directory) { $_file = substr($file, strpos($file, ']') + 1); $_filepath = $_directory . $_file; if ($this->fileExists($source, $_filepath)) { return $_filepath; } } } $_stream_resolve_include_path = function_exists('stream_resolve_include_path'); // relative file name? if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { foreach ($_directories as $_directory) { $_filepath = $_directory . $file; if ($this->fileExists($source, $_filepath)) { return $this->normalizePath($_filepath); } if ($source->smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_directory)) { // try PHP include_path if ($_stream_resolve_include_path) { $_filepath = stream_resolve_include_path($_filepath); } else { $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath); } if ($_filepath !== false) { if ($this->fileExists($source, $_filepath)) { return $this->normalizePath($_filepath); } } } } } // try absolute filepath if ($this->fileExists($source, $file)) { return $file; } // no tpl file found if ($_default_handler) { if (!is_callable($_default_handler)) { if ($source instanceof Smarty_Config_Source) { throw new SmartyException("Default config handler not callable"); } else { throw new SmartyException("Default template handler not callable"); } } $_return = call_user_func_array($_default_handler, array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty)); if (is_string($_return)) { $source->timestamp = @filemtime($_return); $source->exists = !!$source->timestamp; return $_return; } elseif ($_return === true) { $source->content = $_content; $source->timestamp = $_timestamp; $source->exists = true; return $_filepath; } } // give up return false; }
/** * diagnose Smarty setup * If $errors is secified, the diagnostic report will be appended to the array, rather than being output. * * @param Smarty $smarty Smarty instance to test * @param array $errors array to push results into rather than outputting them * * @return bool status, true if everything is fine, false else */ public static function testInstall(Smarty $smarty, &$errors = null) { $status = true; if ($errors === null) { echo "<PRE>\n"; echo "Smarty Installation test...\n"; echo "Testing template directory...\n"; } $_stream_resolve_include_path = function_exists('stream_resolve_include_path'); // test if all registered template_dir are accessible foreach ($smarty->getTemplateDir() as $template_dir) { $_template_dir = $template_dir; $template_dir = realpath($template_dir); // resolve include_path or fail existence if (!$template_dir) { if ($smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_template_dir)) { // try PHP include_path if ($_stream_resolve_include_path) { $template_dir = stream_resolve_include_path($_template_dir); } else { $template_dir = Smarty_Internal_Get_Include_Path::getIncludePath($_template_dir); } if ($template_dir !== false) { if ($errors === null) { echo "{$template_dir} is OK.\n"; } continue; } else { $status = false; $message = "FAILED: {$_template_dir} does not exist (and couldn't be found in include_path either)"; if ($errors === null) { echo $message . ".\n"; } else { $errors['template_dir'] = $message; } continue; } } else { $status = false; $message = "FAILED: {$_template_dir} does not exist"; if ($errors === null) { echo $message . ".\n"; } else { $errors['template_dir'] = $message; } continue; } } if (!is_dir($template_dir)) { $status = false; $message = "FAILED: {$template_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { $errors['template_dir'] = $message; } } elseif (!is_readable($template_dir)) { $status = false; $message = "FAILED: {$template_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['template_dir'] = $message; } } else { if ($errors === null) { echo "{$template_dir} is OK.\n"; } } } if ($errors === null) { echo "Testing compile directory...\n"; } // test if registered compile_dir is accessible $__compile_dir = $smarty->getCompileDir(); $_compile_dir = realpath($__compile_dir); if (!$_compile_dir) { $status = false; $message = "FAILED: {$__compile_dir} does not exist"; if ($errors === null) { echo $message . ".\n"; } else { $errors['compile_dir'] = $message; } } elseif (!is_dir($_compile_dir)) { $status = false; $message = "FAILED: {$_compile_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { $errors['compile_dir'] = $message; } } elseif (!is_readable($_compile_dir)) { $status = false; $message = "FAILED: {$_compile_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['compile_dir'] = $message; } } elseif (!is_writable($_compile_dir)) { $status = false; $message = "FAILED: {$_compile_dir} is not writable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['compile_dir'] = $message; } } else { if ($errors === null) { echo "{$_compile_dir} is OK.\n"; } } if ($errors === null) { echo "Testing plugins directory...\n"; } // test if all registered plugins_dir are accessible // and if core plugins directory is still registered $_core_plugins_dir = realpath(dirname(__FILE__) . '/../plugins'); $_core_plugins_available = false; foreach ($smarty->getPluginsDir() as $plugin_dir) { $_plugin_dir = $plugin_dir; $plugin_dir = realpath($plugin_dir); // resolve include_path or fail existence if (!$plugin_dir) { if ($smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_plugin_dir)) { // try PHP include_path if ($_stream_resolve_include_path) { $plugin_dir = stream_resolve_include_path($_plugin_dir); } else { $plugin_dir = Smarty_Internal_Get_Include_Path::getIncludePath($_plugin_dir); } if ($plugin_dir !== false) { if ($errors === null) { echo "{$plugin_dir} is OK.\n"; } continue; } else { $status = false; $message = "FAILED: {$_plugin_dir} does not exist (and couldn't be found in include_path either)"; if ($errors === null) { echo $message . ".\n"; } else { $errors['plugins_dir'] = $message; } continue; } } else { $status = false; $message = "FAILED: {$_plugin_dir} does not exist"; if ($errors === null) { echo $message . ".\n"; } else { $errors['plugins_dir'] = $message; } continue; } } if (!is_dir($plugin_dir)) { $status = false; $message = "FAILED: {$plugin_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { $errors['plugins_dir'] = $message; } } elseif (!is_readable($plugin_dir)) { $status = false; $message = "FAILED: {$plugin_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['plugins_dir'] = $message; } } elseif ($_core_plugins_dir && $_core_plugins_dir == realpath($plugin_dir)) { $_core_plugins_available = true; if ($errors === null) { echo "{$plugin_dir} is OK.\n"; } } else { if ($errors === null) { echo "{$plugin_dir} is OK.\n"; } } } if (!$_core_plugins_available) { $status = false; $message = "WARNING: Smarty's own libs/plugins is not available"; if ($errors === null) { echo $message . ".\n"; } elseif (!isset($errors['plugins_dir'])) { $errors['plugins_dir'] = $message; } } if ($errors === null) { echo "Testing cache directory...\n"; } // test if all registered cache_dir is accessible $__cache_dir = $smarty->getCacheDir(); $_cache_dir = realpath($__cache_dir); if (!$_cache_dir) { $status = false; $message = "FAILED: {$__cache_dir} does not exist"; if ($errors === null) { echo $message . ".\n"; } else { $errors['cache_dir'] = $message; } } elseif (!is_dir($_cache_dir)) { $status = false; $message = "FAILED: {$_cache_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { $errors['cache_dir'] = $message; } } elseif (!is_readable($_cache_dir)) { $status = false; $message = "FAILED: {$_cache_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['cache_dir'] = $message; } } elseif (!is_writable($_cache_dir)) { $status = false; $message = "FAILED: {$_cache_dir} is not writable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['cache_dir'] = $message; } } else { if ($errors === null) { echo "{$_cache_dir} is OK.\n"; } } if ($errors === null) { echo "Testing configs directory...\n"; } // test if all registered config_dir are accessible foreach ($smarty->getConfigDir() as $config_dir) { $_config_dir = $config_dir; $config_dir = realpath($config_dir); // resolve include_path or fail existence if (!$config_dir) { if ($smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_config_dir)) { // try PHP include_path if ($_stream_resolve_include_path) { $config_dir = stream_resolve_include_path($_config_dir); } else { $config_dir = Smarty_Internal_Get_Include_Path::getIncludePath($_config_dir); } if ($config_dir !== false) { if ($errors === null) { echo "{$config_dir} is OK.\n"; } continue; } else { $status = false; $message = "FAILED: {$_config_dir} does not exist (and couldn't be found in include_path either)"; if ($errors === null) { echo $message . ".\n"; } else { $errors['config_dir'] = $message; } continue; } } else { $status = false; $message = "FAILED: {$_config_dir} does not exist"; if ($errors === null) { echo $message . ".\n"; } else { $errors['config_dir'] = $message; } continue; } } if (!is_dir($config_dir)) { $status = false; $message = "FAILED: {$config_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { $errors['config_dir'] = $message; } } elseif (!is_readable($config_dir)) { $status = false; $message = "FAILED: {$config_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { $errors['config_dir'] = $message; } } else { if ($errors === null) { echo "{$config_dir} is OK.\n"; } } } if ($errors === null) { echo "Testing sysplugin files...\n"; } // test if sysplugins are available $source = SMARTY_SYSPLUGINS_DIR; if (is_dir($source)) { $expected = array("smarty_cacheresource.php" => true, "smarty_cacheresource_custom.php" => true, "smarty_cacheresource_keyvaluestore.php" => true, "smarty_config_source.php" => true, "smarty_internal_cacheresource_file.php" => true, "smarty_internal_compile_append.php" => true, "smarty_internal_compile_assign.php" => true, "smarty_internal_compile_block.php" => true, "smarty_internal_compile_break.php" => true, "smarty_internal_compile_call.php" => true, "smarty_internal_compile_capture.php" => true, "smarty_internal_compile_config_load.php" => true, "smarty_internal_compile_continue.php" => true, "smarty_internal_compile_debug.php" => true, "smarty_internal_compile_eval.php" => true, "smarty_internal_compile_extends.php" => true, "smarty_internal_compile_for.php" => true, "smarty_internal_compile_foreach.php" => true, "smarty_internal_compile_function.php" => true, "smarty_internal_compile_if.php" => true, "smarty_internal_compile_include.php" => true, "smarty_internal_compile_include_php.php" => true, "smarty_internal_compile_insert.php" => true, "smarty_internal_compile_ldelim.php" => true, "smarty_internal_compile_nocache.php" => true, "smarty_internal_compile_private_block_plugin.php" => true, "smarty_internal_compile_private_function_plugin.php" => true, "smarty_internal_compile_private_modifier.php" => true, "smarty_internal_compile_private_object_block_function.php" => true, "smarty_internal_compile_private_object_function.php" => true, "smarty_internal_compile_private_print_expression.php" => true, "smarty_internal_compile_private_registered_block.php" => true, "smarty_internal_compile_private_registered_function.php" => true, "smarty_internal_compile_private_special_variable.php" => true, "smarty_internal_compile_rdelim.php" => true, "smarty_internal_compile_section.php" => true, "smarty_internal_compile_setfilter.php" => true, "smarty_internal_compile_while.php" => true, "smarty_internal_compilebase.php" => true, "smarty_internal_init.php" => true, "smarty_internal_config_file_compiler.php" => true, "smarty_internal_configfilelexer.php" => true, "smarty_internal_configfileparser.php" => true, "smarty_internal_data.php" => true, "smarty_internal_debug.php" => true, "smarty_internal_filter_handler.php" => true, "smarty_internal_function_call_handler.php" => true, "smarty_internal_get_include_path.php" => true, "smarty_internal_nocache_insert.php" => true, "smarty_internal_parsetree.php" => true, "smarty_internal_resource_eval.php" => true, "smarty_internal_resource_extends.php" => true, "smarty_internal_resource_file.php" => true, "smarty_internal_resource_registered.php" => true, "smarty_internal_resource_stream.php" => true, "smarty_internal_resource_string.php" => true, "smarty_internal_smartytemplatecompiler.php" => true, "smarty_internal_template.php" => true, "smarty_internal_templatebase.php" => true, "smarty_internal_templatecompilerbase.php" => true, "smarty_internal_templatelexer.php" => true, "smarty_internal_templateparser.php" => true, "smarty_internal_utility.php" => true, "smarty_internal_write_file.php" => true, "smarty_resource.php" => true, "smarty_resource_custom.php" => true, "smarty_resource_recompiled.php" => true, "smarty_resource_uncompiled.php" => true, "smarty_security.php" => true); $iterator = new DirectoryIterator($source); foreach ($iterator as $file) { if (!$file->isDot()) { $filename = $file->getFilename(); if (isset($expected[$filename])) { unset($expected[$filename]); } } } if ($expected) { $status = false; $message = "FAILED: files missing from libs/sysplugins: " . join(', ', array_keys($expected)); if ($errors === null) { echo $message . ".\n"; } else { $errors['sysplugins'] = $message; } } elseif ($errors === null) { echo "... OK\n"; } } else { $status = false; $message = "FAILED: " . SMARTY_SYSPLUGINS_DIR . ' is not a directory'; if ($errors === null) { echo $message . ".\n"; } else { $errors['sysplugins_dir_constant'] = $message; } } if ($errors === null) { echo "Testing plugin files...\n"; } // test if core plugins are available $source = SMARTY_PLUGINS_DIR; if (is_dir($source)) { $expected = array("block.textformat.php" => true, "function.counter.php" => true, "function.cycle.php" => true, "function.fetch.php" => true, "function.html_checkboxes.php" => true, "function.html_image.php" => true, "function.html_options.php" => true, "function.html_radios.php" => true, "function.html_select_date.php" => true, "function.html_select_time.php" => true, "function.html_table.php" => true, "function.mailto.php" => true, "function.math.php" => true, "modifier.capitalize.php" => true, "modifier.date_format.php" => true, "modifier.debug_print_var.php" => true, "modifier.escape.php" => true, "modifier.regex_replace.php" => true, "modifier.replace.php" => true, "modifier.spacify.php" => true, "modifier.truncate.php" => true, "modifiercompiler.cat.php" => true, "modifiercompiler.count_characters.php" => true, "modifiercompiler.count_paragraphs.php" => true, "modifiercompiler.count_sentences.php" => true, "modifiercompiler.count_words.php" => true, "modifiercompiler.default.php" => true, "modifiercompiler.escape.php" => true, "modifiercompiler.from_charset.php" => true, "modifiercompiler.indent.php" => true, "modifiercompiler.lower.php" => true, "modifiercompiler.noprint.php" => true, "modifiercompiler.string_format.php" => true, "modifiercompiler.strip.php" => true, "modifiercompiler.strip_tags.php" => true, "modifiercompiler.to_charset.php" => true, "modifiercompiler.unescape.php" => true, "modifiercompiler.upper.php" => true, "modifiercompiler.wordwrap.php" => true, "outputfilter.trimwhitespace.php" => true, "shared.escape_special_chars.php" => true, "shared.literal_compiler_param.php" => true, "shared.make_timestamp.php" => true, "shared.mb_str_replace.php" => true, "shared.mb_unicode.php" => true, "shared.mb_wordwrap.php" => true, "variablefilter.htmlspecialchars.php" => true); $iterator = new DirectoryIterator($source); foreach ($iterator as $file) { if (!$file->isDot()) { $filename = $file->getFilename(); if (isset($expected[$filename])) { unset($expected[$filename]); } } } if ($expected) { $status = false; $message = "FAILED: files missing from libs/plugins: " . join(', ', array_keys($expected)); if ($errors === null) { echo $message . ".\n"; } else { $errors['plugins'] = $message; } } elseif ($errors === null) { echo "... OK\n"; } } else { $status = false; $message = "FAILED: " . SMARTY_PLUGINS_DIR . ' is not a directory'; if ($errors === null) { echo $message . ".\n"; } else { $errors['plugins_dir_constant'] = $message; } } if ($errors === null) { echo "Tests complete.\n"; echo "</PRE>\n"; } return $status; }
/** * get system filepath to template */ public function buildTemplateFilepath($file = null) { if ($file == null) { $file = $this->resource_name; } // relative file name? if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { foreach ((array) $this->smarty->template_dir as $_template_dir) { if (strpos('/\\', substr($_template_dir, -1)) === false) { $_template_dir .= DS; } $_filepath = $_template_dir . $file; if (file_exists($_filepath)) { return $_filepath; } if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_template_dir)) { // try PHP include_path if (($_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath)) !== false) { return $_filepath; } } } } // try absolute filepath if (file_exists($file)) { return $file; } // no tpl file found if (!empty($this->smarty->default_template_handler_func)) { if (!is_callable($this->smarty->default_template_handler_func)) { throw new SmartyException("Default template handler not callable"); } else { $_return = call_user_func_array($this->smarty->default_template_handler_func, array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, $this)); if (is_string($_return)) { return $_return; } elseif ($_return === true) { return $file; } } } return false; }
/** * return array with include path directories * * @return array */ public static function getIncludePathDirs(Smarty $smarty) { Smarty_Internal_Get_Include_Path::isNewIncludePath($smarty); return self::$_include_dirs; }
/** * build template filepath by traversing the template_dir array * * @param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object * * @return UTF8String fully qualified filepath * @throws SmartyException */ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = $source->name; preg_match('#^(?P<absolute>[\\\\/]|[a-zA-Z]:[\\\\/])|(\\[(?P<index>[^\\]]+)\\])|(?P<rel>\\.[\\\\/])#', $file, $fileMatch); // save basename if (!empty($fileMatch['absolute'])) { $file = $this->normalizePath($file); return is_file($file) ? $file : false; } // go relative to a given template? if (!empty($fileMatch['rel']) && $_template && $_template->parent instanceof Smarty_Internal_Template) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $path = dirname($_template->parent->source->filepath) . DS . $file; // normalize path $path = $this->normalizePath($path); // files relative to a template only get one shot return is_file($path) ? $path : false; } if ($source->isConfig) { $_directories = $source->smarty->getConfigDir(); } else { $_directories = $source->smarty->getTemplateDir(); } // template_dir index? if (!empty($fileMatch['index'])) { $index = $fileMatch['index']; $_directory = null; // try string indexes if (isset($_directories[$index])) { $_directory = $_directories[$index]; } elseif (is_numeric($index)) { // try numeric index $index = (int) $index; if (isset($_directories[$index])) { $_directory = $_directories[$index]; } else { // try at location index $keys = array_keys($_directories); $_directory = $_directories[$keys[$index]]; } } if ($_directory) { preg_match('#\\](.+)$#', $file, $fileMatch); $path = $_directory . $fileMatch[1]; $path = $this->normalizePath($path); if (is_file($path)) { return $path; } } else { // index not found return false; } } // relative file name? foreach ($_directories as $_directory) { $_filepath = $_directory . $file; $path = $this->normalizePath($_filepath); if (is_file($path)) { return $path; } if ($source->smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_directory)) { // try PHP include_path if (function_exists('stream_resolve_include_path')) { $_filepath = stream_resolve_include_path($_filepath); } else { $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath); } if ($_filepath !== false) { $path = $this->normalizePath($_filepath); if (is_file($path)) { return $path; } } } } // Could be relative to cwd $path = $this->normalizePath(getcwd() . DS . $file); return is_file($path) ? $path : false; }
/** * Takes unknown classes and loads plugin files for them * class name format: Smarty_PluginType_PluginName * plugin filename format: plugintype.pluginname.php * * @param \Smarty $smarty * @param string $plugin_name class plugin name to load * @param bool $check check if already loaded * * @return bool|string * @throws \SmartyException */ public static function loadPlugin(Smarty $smarty, $plugin_name, $check) { // if function or class exists, exit silently (already loaded) if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) { return true; } if (!preg_match('#^smarty_((internal)|([^_]+))_(.+)$#i', $plugin_name, $match)) { throw new SmartyException("plugin {$plugin_name} is not a valid name format"); } if (!empty($match[2])) { $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php'; if (isset($smarty->_is_file_cache[$file])) { if ($smarty->_is_file_cache[$file] !== false) { return $smarty->_is_file_cache[$file]; } else { return false; } } else { if (is_file($file)) { $smarty->_is_file_cache[$file] = $file; require_once $file; return $file; } else { $smarty->_is_file_cache[$file] = false; return false; } } } // plugin filename is expected to be: [type].[name].php $_plugin_filename = "{$match[1]}.{$match[4]}.php"; $_lower_filename = strtolower($_plugin_filename); $_different = $_lower_filename != $_plugin_filename; // loop through plugin dirs and find the plugin $names = array(); foreach ($smarty->getPluginsDir() as $_plugin_dir) { $names[] = $_plugin_dir . $_plugin_filename; if ($_different) { $names[] = $_plugin_dir . $_lower_filename; } } foreach ($names as $path) { $file = $smarty->use_include_path ? $smarty->_realpath($path, false) : $path; if (isset($smarty->_is_file_cache[$file])) { if ($smarty->_is_file_cache[$file] !== false) { return $smarty->_is_file_cache[$file]; } } if (is_file($file)) { $smarty->_is_file_cache[$file] = $file; require_once $file; return $file; } $smarty->_is_file_cache[$file] = false; } if ($smarty->use_include_path) { // try PHP include_path $path = Smarty_Internal_Get_Include_Path::getIncludePath($names, null, $smarty); if ($path !== false) { $smarty->_is_file_cache[$path] = $path; require_once $path; return $path; } } // no plugin loaded return false; }
protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = $source->name; if ($source instanceof Smarty_Config_Source) { $_directories = $source->smarty->getConfigDir(); $_default_handler = $source->smarty->default_config_handler_func; } else { $_directories = $source->smarty->getTemplateDir(); $_default_handler = $source->smarty->default_template_handler_func; } $_file_is_dotted = $file[0] == '.' && ($file[1] == '.' || $file[1] == '/' || $file[1] == "\\"); if ($_template && $_template->parent instanceof Smarty_Internal_Template && $_file_is_dotted) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $file = dirname($_template->parent->source->filepath) . DS . $file; $_file_exact_match = true; if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { $file = getcwd() . DS . $file; } } if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { $_path = DS . trim($file, '/'); $_was_relative = true; } else { $_path = str_replace('\\', '/', $file); } $_path = $this->normalizePath($_path, false); if (DS != '/') { $_path = str_replace('/', '\\', $_path); } if (isset($_was_relative)) { $_path = substr($_path, 1); } $file = rtrim($_path, '/\\'); if (isset($_file_exact_match)) { return $this->fileExists($source, $file) ? $file : false; } if (preg_match('#^\\[(?P<key>[^\\]]+)\\](?P<file>.+)$#', $file, $match)) { $_directory = null; if (isset($_directories[$match['key']])) { $_directory = $_directories[$match['key']]; } elseif (is_numeric($match['key'])) { $match['key'] = (int) $match['key']; if (isset($_directories[$match['key']])) { $_directory = $_directories[$match['key']]; } else { $keys = array_keys($_directories); $_directory = $_directories[$keys[$match['key']]]; } } if ($_directory) { $_file = substr($file, strpos($file, ']') + 1); $_filepath = $_directory . $_file; if ($this->fileExists($source, $_filepath)) { return $_filepath; } } } $_stream_resolve_include_path = function_exists('stream_resolve_include_path'); if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { foreach ($_directories as $_directory) { $_filepath = $_directory . $file; if ($this->fileExists($source, $_filepath)) { return $this->normalizePath($_filepath); } if ($source->smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_directory)) { if ($_stream_resolve_include_path) { $_filepath = stream_resolve_include_path($_filepath); } else { $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath); } if ($_filepath !== false) { if ($this->fileExists($source, $_filepath)) { return $this->normalizePath($_filepath); } } } } } if ($this->fileExists($source, $file)) { return $file; } if ($_default_handler) { if (!is_callable($_default_handler)) { if ($source instanceof Smarty_Config_Source) { throw new SmartyException("Default config handler not callable"); } else { throw new SmartyException("Default template handler not callable"); } } $_return = call_user_func_array($_default_handler, array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty)); if (is_string($_return)) { $source->timestamp = file_exists($_return) ? filemtime($_return) : 0; $source->exists = !!$source->timestamp; return $_return; } elseif ($_return === true) { $source->content = $_content; $source->timestamp = $_timestamp; $source->exists = true; return $_filepath; } } return false; }
/** * build template filepath by traversing the template_dir array * * @param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object * * @return string fully qualified filepath * @throws SmartyException */ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = str_replace(array('\\', '/./'), '/', $source->name); if ($source->isConfig) { $_directories = $source->smarty->getConfigDir(); } else { $_directories = $source->smarty->getTemplateDir(); } preg_match('#^((?P<absolute>[\\/]|[a-zA-Z]:[\\/])|(\\[(?P<index>[^\\]]+)\\])|((?P<rel1>\\.[\\/])?(?P<rel2>(\\.\\.[\\/])*))|(?P<skip>[\\/]))?(?P<file>.+)$#', $file, $fileMatch); // save basename if (!empty($fileMatch['absolute'])) { return is_file($file) ? $file : false; } // go relative to a given template? if ($_template && $_template->parent instanceof Smarty_Internal_Template && (!empty($fileMatch['rel1']) || !empty($fileMatch['rel2']))) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $path = dirname($_template->parent->source->filepath); if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $path)) { // the path gained from the parent template is relative to the current working directory // as expansions (like include_path) have already been done $path = str_replace('\\', '/', getcwd()) . '/' . $path; } // normalize path $path = str_replace(array('\\', './'), array('/', ''), $path); // simple relative if (!empty($fileMatch['rel1'])) { $file = $path . '/' . $fileMatch['file']; } else { for ($i = 1; $i <= substr_count($fileMatch['rel2'], '../'); $i++) { $path = substr($path, 0, strrpos($path, '/')); } $file = $path . '/' . $fileMatch['file']; } // files relative to a template only get one shot return is_file($file) ? $file : false; } $_filepath = null; // template_dir index? if (!empty($fileMatch['index'])) { $index = $fileMatch['index']; $_directory = null; // try string indexes if (isset($_directories[$index])) { $_directory = $_directories[$index]; } elseif (is_numeric($index)) { // try numeric index $index = (int) $index; if (isset($_directories[$index])) { $_directory = $_directories[$index]; } else { // try at location index $keys = array_keys($_directories); $_directory = $_directories[$keys[$index]]; } } if ($_directory) { $_filepath = $_directory . $fileMatch['file']; if (is_file($_filepath)) { return $_filepath; } } } // relative file name? foreach ($_directories as $_directory) { if (empty($fileMatch['rel2'])) { $_filepath = $_directory . $fileMatch['file']; } else { if (false === strpos($_directory, '..')) { for ($i = 1; $i <= substr_count($fileMatch['rel2'], '../') + 1; $i++) { $_directory = substr($_directory, 0, strrpos($_directory, '/')); } $_filepath = $_directory . '/' . $fileMatch['file']; } else { $_filepath = $_directory . $file; } } if (is_file($_filepath)) { return $_filepath; } if ($source->smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_directory)) { // try PHP include_path if (function_exists('stream_resolve_include_path')) { $_filepath = stream_resolve_include_path($_filepath); } else { $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath); } if ($_filepath !== false) { if (is_file($_filepath)) { return $_filepath; } } } } // Could be relative to cwd $path = str_replace('\\', '/', getcwd()); if (empty($fileMatch['rel2'])) { $file = $path . '/' . $fileMatch['file']; } else { for ($i = 1; $i <= substr_count($fileMatch['rel2'], '../'); $i++) { $path = substr($path, 0, strrpos($path, '/')); } $file = $path . '/' . $fileMatch['file']; } return is_file($file) ? $file : false; }
/** * build template filepath by traversing the template_dir array * * @param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object * @return string fully qualified filepath * @throws SmartyException if default template handler is registered but not callable */ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = $source->name; if ($source instanceof Smarty_Config_Source) { $_directories = $source->smarty->getConfigDir(); $_default_handler = $source->smarty->default_config_handler_func; } else { $_directories = $source->smarty->getTemplateDir(); $_default_handler = $source->smarty->default_template_handler_func; } // go relative to a given template? $_file_is_dotted = $file[0] == '.' && ($file[1] == '.' || $file[1] == '/' || $file[1] == "\\"); if ($_template && $_template->parent instanceof Smarty_Internal_Template && $_file_is_dotted) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends') { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $file = dirname($_template->parent->source->filepath) . DS . $file; $_file_exact_match = true; } elseif ($_file_is_dotted) { throw new SmartyException("Template '{$file}' may not start with ../ or ./'"); } // resolve relative path if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { $_path = DS . trim($file, '/\\'); $_was_relative = true; } else { $_path = $file; } // don't we all just love windows? $_path = str_replace('\\', '/', $_path); // resolve simples $_path = preg_replace('#(/\\./(\\./)*)|/{2,}#', '/', $_path); // resolve parents while (true) { $_parent = strpos($_path, '/../'); if ($_parent === false) { break; } else { if ($_parent === 0) { $_path = substr($_path, 3); break; } } $_pos = strrpos($_path, '/', $_parent - strlen($_path) - 1); if ($_pos === false) { // don't we all just love windows? $_pos = $_parent; } $_path = substr_replace($_path, '', $_pos, $_parent + 3 - $_pos); } if (DS != '/') { // don't we all just love windows? $_path = str_replace('/', '\\', $_path); } // revert to relative if (isset($_was_relative)) { $_path = substr($_path, 1); } // this is only required for directories $file = rtrim($_path, '/\\'); // files relative to a template only get one shot if (isset($_file_exact_match)) { return file_exists($file) ? $file : false; } // template_dir index? if (preg_match('#^\\[(?<key>[^\\]]+)\\](?<file>.+)$#', $file, $match)) { if ($match['file'][0] == '.' && ($match['file'][1] == '.' || $match['file'][1] == '/' || $match['file'][1] == "\\")) { throw new SmartyException("Template '{$match['file']}' may not start with ../ or ./'"); } $_directory = null; // try string indexes if (isset($_directories[$match['key']])) { $_directory = $_directories[$match['key']]; } else { if (is_numeric($match['key'])) { // try numeric index $match['key'] = (int) $match['key']; if (isset($_directories[$match['key']])) { $_directory = $_directories[$match['key']]; } else { // try at location index $keys = array_keys($_directories); $_directory = $_directories[$keys[$match['key']]]; } } } if ($_directory) { $_file = substr($file, strpos($file, ']') + 1); $_filepath = $_directory . $_file; if (file_exists($_filepath)) { return $_filepath; } } } // relative file name? if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) { foreach ($_directories as $_directory) { $_filepath = $_directory . $file; if (file_exists($_filepath)) { return $_filepath; } if ($source->smarty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_directory)) { // try PHP include_path if (($_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath)) !== false) { return $_filepath; } } } } // try absolute filepath if (file_exists($file)) { return $file; } // no tpl file found if ($_default_handler) { if (!is_callable($_default_handler)) { if ($source instanceof Smarty_Config_Source) { throw new SmartyException("Default config handler not callable"); } else { throw new SmartyException("Default template handler not callable"); } } $_return = call_user_func_array($_default_handler, array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty)); if (is_string($_return)) { return $_return; } elseif ($_return === true) { $source->content = $_content; $source->timestamp = $_timestamp; return $_filepath; } } // give up return false; }
/** * Return full file path from PHP include_path * * @param string[] $dirs * @param string $file * @param \Smarty $smarty * * @return bool|string full filepath or false * */ public static function getIncludePath($dirs, $file, Smarty $smarty) { if (!(isset(self::$_has_stream_include) ? self::$_has_stream_include : (self::$_has_stream_include = function_exists('stream_resolve_include_path')))) { self::isNewIncludePath($smarty); } // try PHP include_path foreach ($dirs as $dir) { $dir_n = isset(self::$number[$dir]) ? self::$number[$dir] : (self::$number[$dir] = self::$counter++); if (isset(self::$isFile[$dir_n][$file])) { if (self::$isFile[$dir_n][$file]) { return self::$isFile[$dir_n][$file]; } else { continue; } } if (isset(self::$_user_dirs[$dir_n])) { if (false === self::$_user_dirs[$dir_n]) { continue; } else { $dir = self::$_user_dirs[$dir_n]; } } else { if ($dir[0] == '/' || $dir[1] == ':') { $dir = str_ireplace(getcwd(), '.', $dir); if ($dir[0] == '/' || $dir[1] == ':') { self::$_user_dirs[$dir_n] = false; continue; } } $dir = substr($dir, 2); self::$_user_dirs[$dir_n] = $dir; } if (self::$_has_stream_include) { $path = stream_resolve_include_path($dir . (isset($file) ? $file : '')); if ($path) { return self::$isFile[$dir_n][$file] = $path; } } else { foreach (self::$_include_dirs as $key => $_i_path) { $path = isset(self::$isPath[$key][$dir_n]) ? self::$isPath[$key][$dir_n] : (self::$isPath[$key][$dir_n] = is_dir($_dir_path = $_i_path . $dir) ? $_dir_path : false); if ($path === false) { continue; } if (isset($file)) { $_file = self::$isFile[$dir_n][$file] = is_file($path . $file) ? $path . $file : false; if ($_file) { return $_file; } } else { // no file was given return directory path return $path; } } } } return false; }
/** * Check if directory of file resource is trusted. * * @param string $filepath * @param null|bool $isConfig * * @return bool true if directory is trusted * @throws \SmartyException if directory is not trusted */ public function isTrustedResourceDir($filepath, $isConfig = null) { if ($this->_include_path_status !== $this->smarty->use_include_path) { foreach ($this->_include_dir as $directory) { unset($this->_resource_dir[$directory]); } if ($this->smarty->use_include_path) { $this->_include_dir = array(); $_dirs = Smarty_Internal_Get_Include_Path::getIncludePathDirs($this->smarty); foreach ($_dirs as $directory) { $this->_include_dir[] = $directory; $this->_resource_dir[$directory] = true; } } $this->_include_path_status = $this->smarty->use_include_path; } if ($isConfig !== true && (!isset($this->smarty->_cache['template_dir_new']) || $this->smarty->_cache['template_dir_new'])) { $_dir = $this->smarty->getTemplateDir(); if ($this->_template_dir !== $_dir) { foreach ($this->_template_dir as $directory) { unset($this->_resource_dir[$directory]); } foreach ($_dir as $directory) { $this->_resource_dir[$directory] = true; } $this->_template_dir = $_dir; } $this->smarty->_cache['template_dir_new'] = false; } if ($isConfig !== false && (!isset($this->smarty->_cache['config_dir_new']) || $this->smarty->_cache['config_dir_new'])) { $_dir = $this->smarty->getConfigDir(); if ($this->_config_dir !== $_dir) { foreach ($this->_config_dir as $directory) { unset($this->_resource_dir[$directory]); } foreach ($_dir as $directory) { $this->_resource_dir[$directory] = true; } $this->_config_dir = $_dir; } $this->smarty->_cache['config_dir_new'] = false; } if ($this->_secure_dir !== (array) $this->secure_dir) { foreach ($this->_secure_dir as $directory) { unset($this->_resource_dir[$directory]); } foreach ((array) $this->secure_dir as $directory) { $directory = $this->smarty->_realpath($directory . DS, true); $this->_resource_dir[$directory] = true; } $this->_secure_dir = (array) $this->secure_dir; } $this->_resource_dir = $this->_checkDir($filepath, $this->_resource_dir); return true; }
public function loadPlugin($plugin_name, $check = true) { if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) { return true; } $_name_parts = explode('_', $plugin_name, 3); if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') { throw new SmartyException("plugin {$plugin_name} is not a valid name format"); return false; } if (strtolower($_name_parts[1]) == 'internal') { $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php'; if (file_exists($file)) { require_once $file; return $file; } else { return false; } } $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php"; $_stream_resolve_include_path = function_exists('stream_resolve_include_path'); foreach ($this->getPluginsDir() as $_plugin_dir) { $names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename)); foreach ($names as $file) { if (file_exists($file)) { require_once $file; return $file; } if ($this->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_plugin_dir)) { if ($_stream_resolve_include_path) { $file = stream_resolve_include_path($file); } else { $file = Smarty_Internal_Get_Include_Path::getIncludePath($file); } if ($file !== false) { require_once $file; return $file; } } } } return false; }
/** * build template filepath by traversing the template_dir array * * @param Smarty_Template_Source $source source object * @param Smarty_Internal_Template $_template template object * * @return string fully qualified filepath * @throws SmartyException */ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { $file = $source->name; // absolute file ? if ($file[0] == '/' || $file[1] == ':') { $file = $source->smarty->_realpath($file, true); return is_file($file) ? $file : false; } // go relative to a given template? if ($file[0] == '.' && $_template && isset($_template->parent) && $_template->parent->_objType == 2 && preg_match('#^[.]{1,2}[\\\\/]#', $file)) { if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !isset($_template->parent->_cache['allow_relative_path'])) { throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } $path = dirname($_template->parent->source->filepath) . DS . $file; // normalize path $path = $source->smarty->_realpath($path); // files relative to a template only get one shot return is_file($path) ? $path : false; } // normalize DS if (strpos($file, DS == '/' ? '\\' : '/') !== false) { $file = str_replace(DS == '/' ? '\\' : '/', DS, $file); } $_directories = $source->smarty->getTemplateDir(null, $source->isConfig); // template_dir index? if ($file[0] == '[' && preg_match('#^\\[([^\\]]+)\\](.+)$#', $file, $fileMatch)) { $file = $fileMatch[2]; $_indices = explode(',', $fileMatch[1]); $_index_dirs = array(); foreach ($_indices as $index) { $index = trim($index); // try string indexes if (isset($_directories[$index])) { $_index_dirs[] = $_directories[$index]; } elseif (is_numeric($index)) { // try numeric index $index = (int) $index; if (isset($_directories[$index])) { $_index_dirs[] = $_directories[$index]; } else { // try at location index $keys = array_keys($_directories); if (isset($_directories[$keys[$index]])) { $_index_dirs[] = $_directories[$keys[$index]]; } } } } if (empty($_index_dirs)) { // index not found return false; } else { $_directories = $_index_dirs; } } // relative file name? foreach ($_directories as $_directory) { $path = $_directory . $file; if (is_file($path)) { return strpos($path, '.' . DS) !== false ? $source->smarty->_realpath($path) : $path; } } if (!isset($_index_dirs)) { // Could be relative to cwd $path = $source->smarty->_realpath($file, true); if (is_file($path)) { return $path; } } // Use include path ? if ($source->smarty->use_include_path) { return Smarty_Internal_Get_Include_Path::getIncludePath($_directories, $file, $source->smarty); } return false; }
/** * Takes unknown classes and loads plugin files for them * class name format: Smarty_PluginType_PluginName * plugin filename format: plugintype.pluginname.php * * @param string $plugin_name class plugin name to load * @param bool $check check if already loaded * @return string |boolean filepath of loaded file or false */ public function loadPlugin($plugin_name, $check = true) { // if function or class exists, exit silently (already loaded) if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) return true; // Plugin name is expected to be: Smarty_[Type]_[Name] $_plugin_name = strtolower($plugin_name); $_name_parts = explode('_', $_plugin_name, 3); // class name must have three parts to be valid plugin if (count($_name_parts) < 3 || $_name_parts[0] !== 'smarty') { throw new SmartyException("plugin {$plugin_name} is not a valid name format"); return false; } // if type is "internal", get plugin from sysplugins if ($_name_parts[1] == 'internal') { $file = SMARTY_SYSPLUGINS_DIR . $_plugin_name . '.php'; if (file_exists($file)) { require_once($file); return $file; } else { return false; } } // plugin filename is expected to be: [type].[name].php $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php"; // loop through plugin dirs and find the plugin foreach($this->getPluginsDir() as $_plugin_dir) { $file = $_plugin_dir . $_plugin_filename; if (file_exists($file)) { require_once($file); return $file; } if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) { // try PHP include_path if (($file = Smarty_Internal_Get_Include_Path::getIncludePath($file)) !== false) { require_once($file); return $file; } } } // no plugin loaded return false; }