- improvement check if ini_get() and ini_set() not disabled

https://github.com/smarty-php/smarty/pull/362
This commit is contained in:
uwetews
2017-05-19 22:35:55 +02:00
parent 2782e83a92
commit da22c961bb
8 changed files with 130 additions and 105 deletions

View File

@@ -2,9 +2,11 @@
19.5.2017 19.5.2017
- change properties $accessMap and $obsoleteProperties from private to protected - change properties $accessMap and $obsoleteProperties from private to protected
https://github.com/smarty-php/smarty/issues/351 https://github.com/smarty-php/smarty/issues/351
- improvement The named capture buffers can now be accessed also as array - new feature The named capture buffers can now be accessed also as array
See NEWS_FEATURES.txt https://github.com/smarty-php/smarty/issues/366 See NEWS_FEATURES.txt https://github.com/smarty-php/smarty/issues/366
- improvement check if ini_get() and ini_set() not disabled
https://github.com/smarty-php/smarty/pull/362
24.4.2017 24.4.2017
- fix spelling https://github.com/smarty-php/smarty/commit/e3eda8a5f5653d8abb960eb1bc47e3eca679b1b4#commitcomment-21803095 - fix spelling https://github.com/smarty-php/smarty/commit/e3eda8a5f5653d8abb960eb1bc47e3eca679b1b4#commitcomment-21803095

View File

@@ -33,7 +33,9 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
$_filepath = sha1($source->uid . $smarty->_joined_template_dir); $_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir(); $cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) { if (isset($_template->cache_id)) {
$cached->filepath .= preg_replace(array('![^\w|]+!', '![|]+!'), array('_', $_compile_dir_sep), $cached->filepath .= preg_replace(array('![^\w|]+!',
'![|]+!'), array('_',
$_compile_dir_sep),
$_template->cache_id) . $_compile_dir_sep; $_template->cache_id) . $_compile_dir_sep;
} }
if (isset($_template->compile_id)) { if (isset($_template->compile_id)) {
@@ -41,7 +43,8 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
} }
// if use_sub_dirs, break file into directories // if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) { if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] . $smarty->ds . $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] .
$smarty->ds .
$_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds; $_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
} }
$cached->filepath .= $_filepath; $cached->filepath .= $_filepath;
@@ -108,7 +111,9 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content, if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
$_template->smarty) === true $_template->smarty) === true
) { ) {
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) { if (function_exists('opcache_invalidate') &&
(!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api"))) < 1
) {
opcache_invalidate($_template->cached->filepath, true); opcache_invalidate($_template->cached->filepath, true);
} elseif (function_exists('apc_compile_file')) { } elseif (function_exists('apc_compile_file')) {
apc_compile_file($_template->cached->filepath); apc_compile_file($_template->cached->filepath);

View File

@@ -102,28 +102,32 @@ class Smarty_Internal_Config_File_Compiler
{ {
$this->template = $template; $this->template = $template;
$this->template->compiled->file_dependency[ $this->template->source->uid ] = $this->template->compiled->file_dependency[ $this->template->source->uid ] =
array($this->template->source->filepath, $this->template->source->getTimeStamp(), array($this->template->source->filepath,
$this->template->source->getTimeStamp(),
$this->template->source->type); $this->template->source->type);
if ($this->smarty->debugging) { if ($this->smarty->debugging) {
if (!isset( $this->smarty->_debug)) { if (!isset($this->smarty->_debug)) {
$this->smarty->_debug = new Smarty_Internal_Debug(); $this->smarty->_debug = new Smarty_Internal_Debug();
} }
$this->smarty->_debug->start_compile($this->template); $this->smarty->_debug->start_compile($this->template);
} }
// init the lexer/parser to compile the config file // init the lexer/parser to compile the config file
/* @var Smarty_Internal_ConfigFileLexer $this->lex */ /* @var Smarty_Internal_ConfigFileLexer $this ->lex */
$this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n", $this->lex = new $this->lexer_class(str_replace(array("\r\n",
$this); "\r"), "\n", $template->source->getContent()) . "\n",
/* @var Smarty_Internal_ConfigFileParser $this->parser */ $this);
/* @var Smarty_Internal_ConfigFileParser $this ->parser */
$this->parser = new $this->parser_class($this->lex, $this); $this->parser = new $this->parser_class($this->lex, $this);
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { if (function_exists('mb_internal_encoding')
&& function_exists('ini_get')
&& ((int) ini_get('mbstring.func_overload')) & 2
) {
$mbEncoding = mb_internal_encoding(); $mbEncoding = mb_internal_encoding();
mb_internal_encoding('ASCII'); mb_internal_encoding('ASCII');
} else { } else {
$mbEncoding = null; $mbEncoding = null;
} }
if ($this->smarty->_parserdebug) { if ($this->smarty->_parserdebug) {
$this->parser->PrintTrace(); $this->parser->PrintTrace();
} }

View File

@@ -75,7 +75,7 @@ class Smarty_Internal_Method_ClearCompiledTemplate
} }
$_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($_compile as $_file) { foreach ($_compile as $_file) {
if (substr(basename($_file->getPathname()), 0, 1) == '.') { if (substr(basename($_file->getPathname()), 0, 1) == '.') {
continue; continue;
} }
$_filepath = (string) $_file; $_filepath = (string) $_file;
@@ -86,7 +86,7 @@ class Smarty_Internal_Method_ClearCompiledTemplate
} }
} else { } else {
// delete only php files // delete only php files
if (substr($_filepath, -4) !== '.php') { if (substr($_filepath, - 4) !== '.php') {
continue; continue;
} }
$unlink = false; $unlink = false;
@@ -111,7 +111,9 @@ class Smarty_Internal_Method_ClearCompiledTemplate
if ($unlink && @unlink($_filepath)) { if ($unlink && @unlink($_filepath)) {
$_count ++; $_count ++;
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) { if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($_filepath, true); opcache_invalidate($_filepath, true);
} }
} }

View File

@@ -18,13 +18,6 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
*/ */
public $uncompiled = true; public $uncompiled = true;
/**
* container for short_open_tag directive's value before executing PHP templates
*
* @var string
*/
protected $short_open_tag;
/** /**
* Resource does implement populateCompiledFilepath() method * Resource does implement populateCompiledFilepath() method
* *
@@ -32,12 +25,19 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
*/ */
public $hasCompiledHandler = true; public $hasCompiledHandler = true;
/**
* container for short_open_tag directive's value before executing PHP templates
*
* @var string
*/
protected $short_open_tag;
/** /**
* Create a new PHP Resource * Create a new PHP Resource
*/ */
public function __construct() public function __construct()
{ {
$this->short_open_tag = ini_get('short_open_tag'); $this->short_open_tag = function_exists('ini_get') ? ini_get('short_open_tag') : 1;
} }
/** /**
@@ -56,6 +56,23 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
} }
/**
* populate compiled object with compiled filepath
*
* @param Smarty_Template_Compiled $compiled compiled object
* @param Smarty_Internal_Template $_template template object (is ignored)
*/
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
{
$compiled->filepath = $_template->source->filepath;
$compiled->timestamp = $_template->source->timestamp;
$compiled->exists = $_template->source->exists;
$compiled->file_dependency[ $_template->source->uid ] =
array($compiled->filepath,
$compiled->timestamp,
$_template->source->type,);
}
/** /**
* Render and output the template (without using the compiler) * Render and output the template (without using the compiler)
* *
@@ -79,28 +96,16 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
extract($_template->getTemplateVars()); extract($_template->getTemplateVars());
// include PHP template with short open tags enabled // include PHP template with short open tags enabled
ini_set('short_open_tag', '1'); if (function_exists('ini_set')) {
ini_set('short_open_tag', '1');
}
/** @var Smarty_Internal_Template $_smarty_template /** @var Smarty_Internal_Template $_smarty_template
* used in included file * used in included file
*/ */
$_smarty_template = $_template; $_smarty_template = $_template;
include($source->filepath); include($source->filepath);
ini_set('short_open_tag', $this->short_open_tag); if (function_exists('ini_set')) {
} ini_set('short_open_tag', $this->short_open_tag);
}
/**
* populate compiled object with compiled filepath
*
* @param Smarty_Template_Compiled $compiled compiled object
* @param Smarty_Internal_Template $_template template object (is ignored)
*/
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
{
$compiled->filepath = $_template->source->filepath;
$compiled->timestamp = $_template->source->timestamp;
$compiled->exists = $_template->source->exists;
$compiled->file_dependency[ $_template->source->uid ] =
array($compiled->filepath, $compiled->timestamp,
$_template->source->type,);
} }
} }

View File

@@ -80,7 +80,7 @@ class Smarty_Internal_Runtime_CacheResourceFile
} }
} else { } else {
// delete only php files // delete only php files
if (substr($_filepath, -4) !== '.php') { if (substr($_filepath, - 4) !== '.php') {
continue; continue;
} }
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length))); $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
@@ -125,7 +125,9 @@ class Smarty_Internal_Runtime_CacheResourceFile
} }
} }
$_count += @unlink($_filepath) ? 1 : 0; $_count += @unlink($_filepath) ? 1 : 0;
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) { if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($_filepath, true); opcache_invalidate($_filepath, true);
} }
} }

View File

@@ -89,12 +89,16 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
then written to compiled files. */ then written to compiled files. */
// init the lexer/parser to compile the template // init the lexer/parser to compile the template
$this->parser = $this->parser =
new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $_content), $this), new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n",
"\r"), "\n", $_content), $this),
$this); $this);
if ($isTemplateSource && $this->template->caching) { if ($isTemplateSource && $this->template->caching) {
$this->parser->insertPhpCode("<?php\n\$_smarty_tpl->compiled->nocache_hash = '{$this->nocache_hash}';\n?>\n"); $this->parser->insertPhpCode("<?php\n\$_smarty_tpl->compiled->nocache_hash = '{$this->nocache_hash}';\n?>\n");
} }
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { if (function_exists('mb_internal_encoding')
&& function_exists('ini_get')
&& ((int) ini_get('mbstring.func_overload')) & 2
) {
$mbEncoding = mb_internal_encoding(); $mbEncoding = mb_internal_encoding();
mb_internal_encoding('ASCII'); mb_internal_encoding('ASCII');
} else { } else {

View File

@@ -80,64 +80,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
} }
} }
/**
* load compiled template or compile from source
*
* @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*
* @throws Exception
*/
public function process(Smarty_Internal_Template $_smarty_tpl)
{
$source = &$_smarty_tpl->source;
$smarty = &$_smarty_tpl->smarty;
if ($source->handler->recompiled) {
$source->handler->process($_smarty_tpl);
} elseif (!$source->handler->uncompiled) {
if (!$this->exists || $smarty->force_compile ||
($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp())
) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
} else {
$_smarty_tpl->mustCompile = true;
@include($this->filepath);
if ($_smarty_tpl->mustCompile) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
}
}
$_smarty_tpl->_subTemplateRegister();
$this->processed = true;
}
}
/**
* Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*/
private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl)
{
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
opcache_invalidate($this->filepath, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath);
}
if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath));
} else {
include($this->filepath);
}
}
/** /**
* render compiled template code * render compiled template code
* *
@@ -179,6 +121,44 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
} }
} }
/**
* load compiled template or compile from source
*
* @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*
* @throws Exception
*/
public function process(Smarty_Internal_Template $_smarty_tpl)
{
$source = &$_smarty_tpl->source;
$smarty = &$_smarty_tpl->smarty;
if ($source->handler->recompiled) {
$source->handler->process($_smarty_tpl);
} elseif (!$source->handler->uncompiled) {
if (!$this->exists || $smarty->force_compile ||
($smarty->compile_check && $source->getTimeStamp() > $this->getTimeStamp())
) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
} else {
$_smarty_tpl->mustCompile = true;
@include($this->filepath);
if ($_smarty_tpl->mustCompile) {
$this->compileTemplateSource($_smarty_tpl);
$compileCheck = $smarty->compile_check;
$smarty->compile_check = false;
$this->loadCompiledTemplate($_smarty_tpl);
$smarty->compile_check = $compileCheck;
}
}
$_smarty_tpl->_subTemplateRegister();
$this->processed = true;
}
}
/** /**
* compile template from source * compile template from source
* *
@@ -238,6 +218,27 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
return true; return true;
} }
/**
* Load fresh compiled template by including the PHP file
* HHVM requires a work around because of a PHP incompatibility
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
*/
private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl)
{
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($this->filepath);
}
if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($this->filepath));
} else {
include($this->filepath);
}
}
/** /**
* Read compiled content from handler * Read compiled content from handler
* *