Skip to content

Commit

Permalink
- bugfix a filepath starting with '/' or '\' on windows should normal…
Browse files Browse the repository at this point in the history
…ize to the root dir

    of current working drive #134 (reverted from commit e298343) (reverted from commit 5a418ef)
  • Loading branch information
uwetews committed Dec 21, 2015
1 parent 5a418ef commit 76bb166
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 60 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.30-dev ===== (xx.xx.xx)
21.12.2015
- bugfix a filepath starting with '/' or '\' on windows should normalize to the root dir
of current working drive https://github.com/smarty-php/smarty/issues/134
===== 3.1.29 ===== (21.12.2015)
21.12.2015
- optimization improve speed of filetime checks on extends and extendsall resource
Expand Down
127 changes: 67 additions & 60 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev';
const SMARTY_VERSION = '3.1.30-dev/1';

/**
* define variable scopes
Expand Down Expand Up @@ -705,8 +705,8 @@ public function __construct()
}
$this->start_time = microtime(true);

if (isset($_SERVER['SCRIPT_NAME'])) {
Smarty::$global_tpl_vars['SCRIPT_NAME'] = new Smarty_Variable($_SERVER['SCRIPT_NAME']);
if (isset($_SERVER[ 'SCRIPT_NAME' ])) {
Smarty::$global_tpl_vars[ 'SCRIPT_NAME' ] = new Smarty_Variable($_SERVER[ 'SCRIPT_NAME' ]);
}

// Check if we're running on windows
Expand Down Expand Up @@ -742,15 +742,15 @@ public function templateExists($resource_name)
public function getGlobal($varname = null)
{
if (isset($varname)) {
if (isset(self::$global_tpl_vars[$varname])) {
return self::$global_tpl_vars[$varname]->value;
if (isset(self::$global_tpl_vars[ $varname ])) {
return self::$global_tpl_vars[ $varname ]->value;
} else {
return '';
}
} else {
$_result = array();
foreach (self::$global_tpl_vars AS $key => $var) {
$_result[$key] = $var->value;
$_result[ $key ] = $var->value;
}

return $_result;
Expand Down Expand Up @@ -797,8 +797,8 @@ public function setTemplateDir($template_dir, $isConfig = false)
$joined = '_joined_' . $type;
$this->{$type} = (array) $template_dir;
$this->{$joined} = join(' # ', $this->{$type});
$this->_cache[$type . '_new'] = true;
$this->_cache[$type] = false;
$this->_cache[ $type . '_new' ] = true;
$this->_cache[ $type ] = false;
return $this;
}

Expand All @@ -815,11 +815,11 @@ public function addTemplateDir($template_dir, $key = null, $isConfig = false)
{
$type = $isConfig ? 'config_dir' : 'template_dir';
$joined = '_joined_' . $type;
if (!isset($this->_cache[$type])) {
if (!isset($this->_cache[ $type ])) {
$this->{$type} = (array) $this->{$type};
$this->{$joined} = join(' # ', $this->{$type});
$this->_cache[$type . '_new'] = true;
$this->_cache[$type] = false;
$this->_cache[ $type . '_new' ] = true;
$this->_cache[ $type ] = false;
}
$this->{$joined} .= ' # ' . join(' # ', (array) $template_dir);
$this->_addDir($type, $template_dir, $key);
Expand All @@ -837,21 +837,21 @@ public function addTemplateDir($template_dir, $key = null, $isConfig = false)
public function getTemplateDir($index = null, $isConfig = false)
{
$type = $isConfig ? 'config_dir' : 'template_dir';
if (!isset($this->_cache[$type])) {
if (!isset($this->_cache[ $type ])) {
$joined = '_joined_' . $type;
$this->{$type} = (array) $this->{$type};
$this->{$joined} = join(' # ', $this->{$type});
$this->_cache[$type] = false;
$this->_cache[ $type ] = false;
}
if ($this->_cache[$type] == false) {
if ($this->_cache[ $type ] == false) {
foreach ($this->{$type} as $k => $v) {
$this->{$type}[$k] = $this->_realpath($v . DS, true);
$this->{$type}[ $k ] = $this->_realpath($v . DS, true);
}
$this->_cache[$type . '_new'] = true;
$this->_cache[$type] = true;
$this->_cache[ $type . '_new' ] = true;
$this->_cache[ $type ] = true;
}
if ($index !== null) {
return isset($this->{$type}[$index]) ? $this->{$type}[$index] : null;
return isset($this->{$type}[ $index ]) ? $this->{$type}[ $index ] : null;
}
return $this->{$type};
}
Expand Down Expand Up @@ -903,8 +903,8 @@ public function getConfigDir($index = null)
public function setPluginsDir($plugins_dir)
{
$this->plugins_dir = (array) $plugins_dir;
if (isset($this->_cache['plugins_dir'])) {
unset($this->_cache['plugins_dir']);
if (isset($this->_cache[ 'plugins_dir' ])) {
unset($this->_cache[ 'plugins_dir' ]);
}
return $this;
}
Expand All @@ -922,8 +922,8 @@ public function addPluginsDir($plugins_dir)
$this->plugins_dir = array(SMARTY_PLUGINS_DIR);
}
$this->plugins_dir = array_merge((array) $this->plugins_dir, (array) $plugins_dir);
if (isset($this->_cache['plugins_dir'])) {
unset($this->_cache['plugins_dir']);
if (isset($this->_cache[ 'plugins_dir' ])) {
unset($this->_cache[ 'plugins_dir' ]);
}
return $this;
}
Expand All @@ -935,7 +935,7 @@ public function addPluginsDir($plugins_dir)
*/
public function getPluginsDir()
{
if (!isset($this->_cache['plugins_dir'])) {
if (!isset($this->_cache[ 'plugins_dir' ])) {
if (!isset($this->plugins_dir)) {
$this->plugins_dir = array(SMARTY_PLUGINS_DIR);
} else {
Expand All @@ -946,8 +946,8 @@ public function getPluginsDir()
}
$this->plugins_dir = array_unique($this->plugins_dir);
}
$this->_cache['plugin_files'] = array();
$this->_cache['plugins_dir'] = true;
$this->_cache[ 'plugin_files' ] = array();
$this->_cache[ 'plugins_dir' ] = true;
}
return $this->plugins_dir;
}
Expand All @@ -962,10 +962,10 @@ public function getPluginsDir()
public function setCompileDir($compile_dir)
{
$this->compile_dir = $this->_realpath($compile_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
Smarty::$_muted_directories[$this->compile_dir] = null;
if (!isset(Smarty::$_muted_directories[ $this->compile_dir ])) {
Smarty::$_muted_directories[ $this->compile_dir ] = null;
}
$this->_cache['compile_dir'] = true;
$this->_cache[ 'compile_dir' ] = true;
return $this;
}

Expand All @@ -976,12 +976,12 @@ public function setCompileDir($compile_dir)
*/
public function getCompileDir()
{
if (!isset($this->_cache['compile_dir'])) {
if (!isset($this->_cache[ 'compile_dir' ])) {
$this->compile_dir = $this->_realpath($this->compile_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
Smarty::$_muted_directories[$this->compile_dir] = null;
if (!isset(Smarty::$_muted_directories[ $this->compile_dir ])) {
Smarty::$_muted_directories[ $this->compile_dir ] = null;
}
$this->_cache['compile_dir'] = true;
$this->_cache[ 'compile_dir' ] = true;
}
return $this->compile_dir;
}
Expand All @@ -996,10 +996,10 @@ public function getCompileDir()
public function setCacheDir($cache_dir)
{
$this->cache_dir = $this->_realpath($cache_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
Smarty::$_muted_directories[$this->cache_dir] = null;
if (!isset(Smarty::$_muted_directories[ $this->cache_dir ])) {
Smarty::$_muted_directories[ $this->cache_dir ] = null;
}
$this->_cache['cache_dir'] = true;
$this->_cache[ 'cache_dir' ] = true;
return $this;
}

Expand All @@ -1010,12 +1010,12 @@ public function setCacheDir($cache_dir)
*/
public function getCacheDir()
{
if (!isset($this->_cache['cache_dir'])) {
if (!isset($this->_cache[ 'cache_dir' ])) {
$this->cache_dir = $this->_realpath($this->cache_dir . DS, true);
if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
Smarty::$_muted_directories[$this->cache_dir] = null;
if (!isset(Smarty::$_muted_directories[ $this->cache_dir ])) {
Smarty::$_muted_directories[ $this->cache_dir ] = null;
}
$this->_cache['cache_dir'] = true;
$this->_cache[ 'cache_dir' ] = true;
}
return $this->cache_dir;
}
Expand All @@ -1029,7 +1029,7 @@ public function getCacheDir()
*/
private function _addDir($dirName, $dir, $key = null)
{
$rp = $this->_cache[$dirName];
$rp = $this->_cache[ $dirName ];
if (is_array($dir)) {
foreach ($dir as $k => $v) {
$path = $rp ? $this->_realpath($v . DS, true) : $v;
Expand All @@ -1038,14 +1038,14 @@ private function _addDir($dirName, $dir, $key = null)
$this->{$dirName}[] = $path;
} else {
// string indexes are overridden
$this->{$dirName}[$k] = $path;
$this->{$dirName}[ $k ] = $path;
}
}
} else {
$path = $rp ? $this->_realpath($dir . DS, true) : $dir;
if ($key !== null) {
// override directory at specified index
$this->{$dirName}[$key] = $path;
$this->{$dirName}[ $key ] = $path;
} else {
// append new directory
$this->{$dirName}[] = $path;
Expand Down Expand Up @@ -1076,10 +1076,11 @@ public function createTemplate($template, $cache_id = null, $compile_id = null,
} else {
$data = null;
}
if ($this->caching &&
isset($this->_cache['isCached'][$_templateId = $this->_getTemplateId($template, $cache_id, $compile_id)])
if ($this->caching && isset($this->_cache[ 'isCached' ][ $_templateId =
$this->_getTemplateId($template, $cache_id, $compile_id) ])
) {
$tpl = $do_clone ? clone $this->_cache['isCached'][$_templateId] : $this->_cache['isCached'][$_templateId];
$tpl = $do_clone ? clone $this->_cache[ 'isCached' ][ $_templateId ] :
$this->_cache[ 'isCached' ][ $_templateId ];
$tpl->parent = $parent;
$tpl->tpl_vars = array();
$tpl->config_vars = array();
Expand All @@ -1096,7 +1097,7 @@ public function createTemplate($template, $cache_id = null, $compile_id = null,
if (!empty($data) && is_array($data)) {
// set up variable values
foreach ($data as $_key => $_val) {
$tpl->tpl_vars[$_key] = new Smarty_Variable($_val);
$tpl->tpl_vars[ $_key ] = new Smarty_Variable($_val);
}
}
if ($this->debugging || $this->debugging_ctrl == 'URL') {
Expand Down Expand Up @@ -1147,7 +1148,7 @@ public function _getTemplateId($template_name, $cache_id = null, $compile_id = n
} else {
$_templateId = $this->_joined_template_dir . "#{$template_name}#{$cache_id}#{$compile_id}#{$caching}";
}
if (isset($_templateId[150])) {
if (isset($_templateId[ 150 ])) {
$_templateId = sha1($_templateId);
}
return $_templateId;
Expand All @@ -1158,8 +1159,10 @@ public function _getTemplateId($template_name, $cache_id = null, $compile_id = n
* - remove /./ and /../
* - make it absolute if required
*
* @param string $path file path
* @param bool $realpath leave $path relative
* @param string $path file path
* @param bool $realpath if true - convert to absolute
* false - convert to relative
* null - keep as it is but remove /./ /../
*
* @return string
*/
Expand All @@ -1178,13 +1181,17 @@ public function _realpath($path, $realpath = null)
$path = str_replace($nds, DS, $path);
}

if ($realpath === true && (($path[0] !== '/' && DS == '/') || ($path[1] !== ':' && DS != '/'))) {
$path = getcwd() . DS . $path;
if (DS != '/' && $path[ 0 ] == DS) {
$path = substr(getcwd(), 0, 2) . $path;
} else {
if ($realpath === true && $path[ 0 ] !== '/' && $path[ 1 ] !== ':') {
$path = getcwd() . DS . $path;
}
}
while ((strpos($path, '.' . DS) !== false) || (strpos($path, DS . DS) !== false)) {
$path = preg_replace($pattern, DS, $path);
}
if ($realpath === false && ($path[0] == '/' || $path[1] == ':')) {
if ($realpath === false && ($path[ 0 ] == '/' || $path[ 1 ] == ':')) {
$path = str_ireplace(getcwd(), '.', $path);
}
return $path;
Expand Down Expand Up @@ -1348,8 +1355,8 @@ public function __destruct()
public function __get($name)
{

if (isset(self::$accessMap[$name])) {
$method = 'get' . self::$accessMap[$name];
if (isset(self::$accessMap[ $name ])) {
$method = 'get' . self::$accessMap[ $name ];
return $this->{$method}();
} elseif (in_array($name, self::$obsoleteProperties)) {
return null;
Expand All @@ -1368,8 +1375,8 @@ public function __get($name)
*/
public function __set($name, $value)
{
if (isset(self::$accessMap[$name])) {
$method = 'set' . self::$accessMap[$name];
if (isset(self::$accessMap[ $name ])) {
$method = 'set' . self::$accessMap[ $name ];
$this->{$method}($value);
} elseif (in_array($name, self::$obsoleteProperties)) {
return;
Expand Down Expand Up @@ -1400,10 +1407,10 @@ public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $
$_is_muted_directory = false;

// add the SMARTY_DIR to the list of muted directories
if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) {
if (!isset(Smarty::$_muted_directories[ SMARTY_DIR ])) {
$smarty_dir = realpath(SMARTY_DIR);
if ($smarty_dir !== false) {
Smarty::$_muted_directories[SMARTY_DIR] =
Smarty::$_muted_directories[ SMARTY_DIR ] =
array('file' => $smarty_dir, 'length' => strlen($smarty_dir),);
}
}
Expand All @@ -1415,12 +1422,12 @@ public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $
$file = realpath($key);
if ($file === false) {
// this directory does not exist, remove and skip it
unset(Smarty::$_muted_directories[$key]);
unset(Smarty::$_muted_directories[ $key ]);
continue;
}
$dir = array('file' => $file, 'length' => strlen($file),);
}
if (!strncmp($errfile, $dir['file'], $dir['length'])) {
if (!strncmp($errfile, $dir[ 'file' ], $dir[ 'length' ])) {
$_is_muted_directory = true;
break;
}
Expand Down

0 comments on commit 76bb166

Please sign in to comment.