Skip to content

Commit

Permalink
- bugfix remove constant DS as shortcut for DIRECTORY_SEPARATOR as th…
Browse files Browse the repository at this point in the history
…e user may have defined it to something else #277
  • Loading branch information
uwetews committed Aug 23, 2016
1 parent be39cc0 commit 2003020
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 48 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
===== 3.1.31-dev ===== (xx.xx.xx)
23.08.2016
- bugfix remove constant DS as shortcut for DIRECTORY_SEPARATOR as the user may have defined it to something else https://github.com/smarty-php/smarty/issues/277

20.08-2016
- bugfix {config_load ... scope="global"} shall not throw an arror but fallback to scope="smarty" https://github.com/smarty-php/smarty/issues/274
- bugfix {make_nocache} failed when using composer autoloader https://github.com/smarty-php/smarty/issues/275
Expand Down
44 changes: 22 additions & 22 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,23 @@
* @version 3.1.31-dev
*/

/**
* define shorthand directory separator constant
*/
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}

/**
* set SMARTY_DIR to absolute path to Smarty library files.
* Sets SMARTY_DIR only if user application has not already defined it.
*/
if (!defined('SMARTY_DIR')) {
define('SMARTY_DIR', dirname(__FILE__) . DS);
define('SMARTY_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
}

/**
* set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
* Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
*/
if (!defined('SMARTY_SYSPLUGINS_DIR')) {
define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DIRECTORY_SEPARATOR);
}
if (!defined('SMARTY_PLUGINS_DIR')) {
define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DIRECTORY_SEPARATOR);
}
if (!defined('SMARTY_MBSTRING')) {
define('SMARTY_MBSTRING', function_exists('mb_get_info'));
Expand Down Expand Up @@ -718,6 +711,13 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public $_debug = null;

/**
* Directory separator
*
* @var string
*/
public $ds = DIRECTORY_SEPARATOR;

/**
* removed properties
*
Expand Down Expand Up @@ -974,7 +974,7 @@ public function getPluginsDir()
$this->plugins_dir = (array) $this->plugins_dir;
}
foreach ($this->plugins_dir as $k => $v) {
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . DS, true);
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
}
$this->_cache[ 'plugin_files' ] = array();
$this->_pluginsDirNormalized = true;
Expand Down Expand Up @@ -1045,7 +1045,7 @@ public function getCacheDir()
*/
private function _normalizeDir($dirName, $dir)
{
$this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . DS, true);
$this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . $this->ds, true);
if (!isset(Smarty::$_muted_directories[ $this->{$dirName} ])) {
Smarty::$_muted_directories[ $this->{$dirName} ] = null;
}
Expand All @@ -1071,7 +1071,7 @@ private function _nomalizeTemplateConfig($isConfig)
}
foreach ($dir as $k => $v) {
if (!isset($processed[ $k ])) {
$dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . DS, true);
$dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . $this->ds, true);
$processed[ $k ] = true;
}
}
Expand Down Expand Up @@ -1200,23 +1200,23 @@ public function _getTemplateId($template_name, $cache_id = null, $compile_id = n
*/
public function _realpath($path, $realpath = null)
{
$nds = DS == '/' ? '\\' : '/';
// normalize DS
$path = str_replace($nds, DS, $path);
$nds = $this->ds == '/' ? '\\' : '/';
// normalize $this->ds
$path = str_replace($nds, $this->ds, $path);
preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(?:[[:print:]]*))$%',
$path, $parts);
$path = $parts[ 'path' ];
if ($parts[ 'root' ] == '\\') {
$parts[ 'root' ] = substr(getcwd(), 0, 2) . $parts[ 'root' ];
} else {
if ($realpath !== null && !$parts[ 'root' ]) {
$path = getcwd() . DS . $path;
$path = getcwd() . $this->ds . $path;
}
}
// remove noop 'DS DS' and 'DS.DS' patterns
$path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#', DS, $path);
// resolve '..DS' pattern, smallest first
if (strpos($path, '..' . DS) != false &&
// remove noop 'DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR' and 'DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR' patterns
$path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#', $this->ds, $path);
// resolve '..DIRECTORY_SEPARATOR' pattern, smallest first
if (strpos($path, '..' . $this->ds) != false &&
preg_match_all('#(([.]?[\\\\/])*([.][.])[\\\\/]([.]?[\\\\/])*)+#', $path, $match)
) {
$counts = array();
Expand All @@ -1227,7 +1227,7 @@ public function _realpath($path, $realpath = null)
foreach ($counts as $count) {
$path = preg_replace('#(([\\\\/]([.]?[\\\\/])*[^\\\\/.]+){' . $count .
'}[\\\\/]([.]?[\\\\/])*([.][.][\\\\/]([.]?[\\\\/])*){' . $count . '})(?=[^.])#',
DS, $path);
$this->ds, $path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion libs/plugins/modifier.date_format.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function smarty_modifier_date_format($string, $format = null, $default_date = ''
return;
}
if ($formatter == 'strftime' || ($formatter == 'auto' && strpos($format, '%') !== false)) {
if (DS == '\\') {
if (DIRECTORY_SEPARATOR == '\\') {
$_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T');
$_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
if (strpos($format, '%e') !== false) {
Expand Down
6 changes: 3 additions & 3 deletions libs/sysplugins/smarty_internal_cacheresource_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Templat
{
$source = &$_template->source;
$smarty = &$_template->smarty;
$_compile_dir_sep = $smarty->use_sub_dirs ? DS : '^';
$_compile_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^';
$_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) {
Expand All @@ -41,8 +41,8 @@ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Templat
}
// if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DS . $_filepath[ 2 ] . $_filepath[ 3 ] . DS .
$_filepath[ 4 ] . $_filepath[ 5 ] . DS;
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] . $smarty->ds .
$_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
}
$cached->filepath .= $_filepath;
$basename = $source->handler->getBasename($source);
Expand Down
2 changes: 1 addition & 1 deletion libs/sysplugins/smarty_internal_compile_include_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
}
if (!empty($_dir)) {
foreach ((array) $_dir as $_script_dir) {
$_path = $compiler->smarty->_realpath($_script_dir . DS . $_file, true);
$_path = $compiler->smarty->_realpath($_script_dir . $compiler->smarty->ds . $_file, true);
if (file_exists($_path)) {
$_filepath = $_path;
break;
Expand Down
2 changes: 1 addition & 1 deletion libs/sysplugins/smarty_internal_compile_insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
}
if (!empty($_dir)) {
foreach ((array) $_dir as $_script_dir) {
$_script_dir = rtrim($_script_dir, '/\\') . DS;
$_script_dir = rtrim($_script_dir, '/\\') . $compiler->smarty->ds;
if (file_exists($_script_dir . $_script)) {
$_filepath = $_script_dir . $_script;
break;
Expand Down
2 changes: 1 addition & 1 deletion libs/sysplugins/smarty_internal_extension_clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function clear(Smarty $smarty, $resource_name, $cache_id, $compile
$_cache_id_parts_count = count($_cache_id_parts);
if ($smarty->use_sub_dirs) {
foreach ($_cache_id_parts as $id_part) {
$_dir .= $id_part . DS;
$_dir .= $id_part . $smarty->ds;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $co
return 0;
}
$_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
$_dir_sep = $smarty->use_sub_dirs ? DS : '^';
$_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^';
if (isset($resource_name)) {
$_save_stat = $smarty->caching;
$smarty->caching = false;
/* @var Smarty_Internal_Template $tpl */
$tpl = new $smarty->template_class($resource_name, $smarty);
$smarty->caching = $_save_stat;
if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
$_resource_part_1 = basename(str_replace('^', DS, $tpl->compiled->filepath));
$_resource_part_1 = basename(str_replace('^', $smarty->ds, $tpl->compiled->filepath));
$_resource_part_1_length = strlen($_resource_part_1);
} else {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_
continue;
}
if ($_fileinfo->getPath() !== substr($_dir, 0, - 1)) {
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
$_file = substr($_fileinfo->getPath(), strlen($_dir)) . $smarty->ds . $_file;
}
echo "\n<br>", $_dir, '---', $_file;
flush();
Expand Down
10 changes: 5 additions & 5 deletions libs/sysplugins/smarty_internal_resource_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal
throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
}
// normalize path
$path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . DS . $file);
$path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . $source->smarty->ds . $file);
// 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);
// normalize $source->smarty->ds
if (strpos($file, $source->smarty->ds == '/' ? '\\' : '/') !== false) {
$file = str_replace($source->smarty->ds == '/' ? '\\' : '/', $source->smarty->ds, $file);
}

$_directories = $source->smarty->getTemplateDir(null, $source->isConfig);
Expand Down Expand Up @@ -90,7 +90,7 @@ protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal
foreach ($_directories as $_directory) {
$path = $_directory . $file;
if (is_file($path)) {
return (strpos($path, '.' . DS) !== false) ? $source->smarty->_realpath($path) : $path;
return (strpos($path, '.' . $source->smarty->ds) !== false) ? $source->smarty->_realpath($path) : $path;
}
}
if (!isset($_index_dirs)) {
Expand Down
4 changes: 2 additions & 2 deletions libs/sysplugins/smarty_internal_runtime_getincludepath.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public function isNewIncludePath(Smarty $smarty)
if ($this->_include_path != $_i_path) {
$this->_include_dirs = array();
$this->_include_path = $_i_path;
$_dirs = (array) explode(PATH_SEPARATOR, $_i_path);
$_dirs = (array) explode($smarty->ds, $_i_path);
foreach ($_dirs as $_path) {
if (is_dir($_path)) {
$this->_include_dirs[] = $smarty->_realpath($_path . DS, true);
$this->_include_dirs[] = $smarty->_realpath($_path . $smarty->ds, true);
}
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion libs/sysplugins/smarty_internal_runtime_writefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function writeFile($_filepath, $_contents, Smarty $smarty)
}

// write to tmp file, then move to overt file lock race condition
$_tmp_file = $_dirpath . DS . str_replace(array('.', ','), '_', uniqid('wrt', true));
$_tmp_file = $_dirpath . $smarty->ds . str_replace(array('.', ','), '_', uniqid('wrt', true));
if (!file_put_contents($_tmp_file, $_contents)) {
error_reporting($_error_reporting);
throw new SmartyException("unable to write file {$_tmp_file}");
Expand Down
2 changes: 1 addition & 1 deletion libs/sysplugins/smarty_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public static function getUniqueTemplateName($obj, $template_resource)
if ($obj->_objType == 2 && $_file_is_dotted &&
($obj->source->type == 'file' || $obj->parent->source->type == 'extends')
) {
$name = $smarty->_realpath(dirname($obj->parent->source->filepath) . DS . $name);
$name = $smarty->_realpath(dirname($obj->parent->source->filepath) . $smarty->ds . $name);
}
return $resource->buildUniqueResourceName($smarty, $name);
}
Expand Down
8 changes: 4 additions & 4 deletions libs/sysplugins/smarty_security.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public function isTrustedResourceDir($filepath, $isConfig = null)
unset($this->_resource_dir[ $directory ]);
}
foreach ((array) $this->secure_dir as $directory) {
$directory = $this->smarty->_realpath($directory . DS, true);
$directory = $this->smarty->_realpath($directory . DIRECTORY_SEPARATOR, true);
$this->_resource_dir[ $directory ] = true;
}
$this->_secure_dir = (array) $this->secure_dir;
Expand Down Expand Up @@ -618,7 +618,7 @@ public function isTrustedPHPDir($filepath)

$this->_trusted_dir = $this->trusted_dir;
foreach ((array) $this->trusted_dir as $directory) {
$directory = $this->smarty->_realpath($directory . DS, true);
$directory = $this->smarty->_realpath($directory . DIRECTORY_SEPARATOR, true);
$this->_php_resource_dir[ $directory ] = true;
}
}
Expand All @@ -639,7 +639,7 @@ public function isTrustedPHPDir($filepath)
*/
private function _checkDir($filepath, $dirs)
{
$directory = dirname($filepath) . DS;
$directory = dirname($filepath) . DIRECTORY_SEPARATOR;
$_directory = array();
while (true) {
// remember the directory to add it to _resource_dir in case we're successful
Expand All @@ -656,7 +656,7 @@ private function _checkDir($filepath, $dirs)
break;
}
// bubble up one level
$directory = preg_replace('#[\\\/][^\\\/]+[\\\/]$#', DS, $directory);
$directory = preg_replace('#[\\\/][^\\\/]+[\\\/]$#', DIRECTORY_SEPARATOR, $directory);
}

// give up
Expand Down
6 changes: 3 additions & 3 deletions libs/sysplugins/smarty_template_compiled.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public function populateCompiledFilepath(Smarty_Internal_Template $_template)
$this->filepath = $smarty->getCompileDir();
if (isset($_template->compile_id)) {
$this->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) .
($smarty->use_sub_dirs ? DS : '^');
($smarty->use_sub_dirs ? $smarty->ds : '^');
}
// if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) {
$this->filepath .= $source->uid[ 0 ] . $source->uid[ 1 ] . DS . $source->uid[ 2 ] . $source->uid[ 3 ] . DS .
$source->uid[ 4 ] . $source->uid[ 5 ] . DS;
$this->filepath .= $source->uid[ 0 ] . $source->uid[ 1 ] . $smarty->ds . $source->uid[ 2 ] . $source->uid[ 3 ] . $smarty->ds .
$source->uid[ 4 ] . $source->uid[ 5 ] . $smarty->ds;
}
$this->filepath .= $source->uid . '_';
if ($source->isConfig) {
Expand Down

0 comments on commit 2003020

Please sign in to comment.