/** * 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) { static $_include_path = null; static $_has_stream_include = null; if ($_include_path === null) { $_include_path = (array) explode(PATH_SEPARATOR, get_include_path()); foreach ($_include_path as $key => $_path) { $_include_path[$key] = rtrim($_path, '/\\'); } $_has_stream_include = function_exists('stream_resolve_include_path'); } // try PHP include_path foreach ($dirs as $dir) { if ($dir[0] != '/' && $dir[1] != ':') { $_d_path = $dir . isset($file) ? $file : ''; if ($_has_stream_include) { // available since PHP 5.3.2 $path = stream_resolve_include_path($_d_path); if ($path !== false && is_file($path)) { return $smarty->_realpath($path); } } foreach ($_include_path as $_i_path) { if (is_file($_i_path . DS . $_d_path)) { return $smarty->_realpath($_i_path . DS . $_d_path); } } } } return false; }
/** * Check if include path was updated * * @param \Smarty $smarty * * @return bool */ public function isNewIncludePath(Smarty $smarty) { $_i_path = get_include_path(); if ($this->_include_path != $_i_path) { $this->_include_dirs = array(); $this->_include_path = $_i_path; $_dirs = (array) explode(PATH_SEPARATOR, $_i_path); foreach ($_dirs as $_path) { if ($_path[0] != '/' && isset($dir[1]) && $dir[1] != ':') { $_path = $smarty->_realpath($_path . DS, true); } if (is_dir($_path)) { $this->_include_dirs[] = $smarty->_realpath($_path . DS, true); } } return true; } 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) { static $_include_path = ''; static $_include_dirs = array(); static $_has_stream_include = null; $_i_path = get_include_path(); if ($_include_path != $_i_path) { $_include_path = $_i_path; $_dirs = (array) explode(PATH_SEPARATOR, $_i_path); $_include_dirs = array(); foreach ($_dirs as $_path) { $_include_dirs[] = rtrim($_path, '/\\'); } $_has_stream_include = function_exists('stream_resolve_include_path'); } // try PHP include_path foreach ($dirs as $dir) { if ($dir[0] == '/' || $dir[1] == ':') { $dir = $smarty->_realpath($dir, false); } if ($dir[0] != '/' && $dir[1] != ':') { $_d_path = $dir . (isset($file) ? $file : ''); if ($_has_stream_include) { // available since PHP 5.3.2 $path = stream_resolve_include_path($_d_path); if ($path !== false && is_file($path)) { return $smarty->_realpath($path, true); } } foreach ($_include_dirs as $_i_path) { if (is_file($_i_path . DS . $_d_path)) { return $smarty->_realpath($_i_path . DS . $_d_path, true); } } } } return false; }
/** * Check if include path was updated * * @return bool * */ public static function isNewIncludePath(Smarty $smarty) { if (!isset(self::$_has_stream_include)) { self::$_has_stream_include = $smarty->use_include_path === 2 && function_exists('stream_resolve_include_path'); } $_i_path = get_include_path(); if (self::$_include_path != $_i_path) { self::$_include_dirs = array(); self::$_include_path = $_i_path; $_dirs = (array) explode(PATH_SEPARATOR, $_i_path); foreach ($_dirs as $_path) { if ($_path[0] != '/' && isset($dir[1]) && $dir[1] != ':') { $_path = $smarty->_realpath($_path . DS, true); } if (is_dir($_path)) { self::$_include_dirs[] = $smarty->_realpath($_path . DS, true); } } return true; } return 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; }