Skip to content

Commit

Permalink
- improvement check if ini_get() and ini_set() not disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed May 21, 2017
1 parent 2782e83 commit da22c96
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 104 deletions.
6 changes: 4 additions & 2 deletions change_log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
19.5.2017
- change properties $accessMap and $obsoleteProperties from private to protected
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

- improvement check if ini_get() and ini_set() not disabled
https://github.com/smarty-php/smarty/pull/362

24.4.2017
- fix spelling https://github.com/smarty-php/smarty/commit/e3eda8a5f5653d8abb960eb1bc47e3eca679b1b4#commitcomment-21803095

Expand Down
11 changes: 8 additions & 3 deletions libs/sysplugins/smarty_internal_cacheresource_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Templat
$_filepath = sha1($source->uid . $smarty->_joined_template_dir);
$cached->filepath = $smarty->getCacheDir();
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;
}
if (isset($_template->compile_id)) {
$cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
}
// if use_sub_dirs, break file into directories
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;
}
$cached->filepath .= $_filepath;
Expand Down Expand Up @@ -108,7 +111,9 @@ public function writeCachedContent(Smarty_Internal_Template $_template, $content
if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
$_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);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($_template->cached->filepath);
Expand Down
22 changes: 13 additions & 9 deletions libs/sysplugins/smarty_internal_config_file_compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,32 @@ public function compileTemplate(Smarty_Internal_Template $template)
{
$this->template = $template;
$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);
if ($this->smarty->debugging) {
if (!isset( $this->smarty->_debug)) {
$this->smarty->_debug = new Smarty_Internal_Debug();
if (!isset($this->smarty->_debug)) {
$this->smarty->_debug = new Smarty_Internal_Debug();
}
$this->smarty->_debug->start_compile($this->template);
}
// init the lexer/parser to compile the config file
/* @var Smarty_Internal_ConfigFileLexer $this->lex */
$this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n",
$this);
/* @var Smarty_Internal_ConfigFileParser $this->parser */
/* @var Smarty_Internal_ConfigFileLexer $this ->lex */
$this->lex = new $this->lexer_class(str_replace(array("\r\n",
"\r"), "\n", $template->source->getContent()) . "\n",
$this);
/* @var Smarty_Internal_ConfigFileParser $this ->parser */
$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();
mb_internal_encoding('ASCII');
} else {
$mbEncoding = null;
}

if ($this->smarty->_parserdebug) {
$this->parser->PrintTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $co
}
$_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($_compile as $_file) {
if (substr(basename($_file->getPathname()), 0, 1) == '.') {
if (substr(basename($_file->getPathname()), 0, 1) == '.') {
continue;
}
$_filepath = (string) $_file;
Expand All @@ -86,7 +86,7 @@ public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $co
}
} else {
// delete only php files
if (substr($_filepath, -4) !== '.php') {
if (substr($_filepath, - 4) !== '.php') {
continue;
}
$unlink = false;
Expand All @@ -111,7 +111,9 @@ public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $co

if ($unlink && @unlink($_filepath)) {
$_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);
}
}
Expand Down
55 changes: 30 additions & 25 deletions libs/sysplugins/smarty_internal_resource_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
public $uncompiled = true;

/**
* container for short_open_tag directive's value before executing PHP templates
* Resource does implement populateCompiledFilepath() method
*
* @var string
* @var bool
*/
protected $short_open_tag;
public $hasCompiledHandler = true;

/**
* Resource does implement populateCompiledFilepath() method
* container for short_open_tag directive's value before executing PHP templates
*
* @var bool
* @var string
*/
public $hasCompiledHandler = true;
protected $short_open_tag;

/**
* Create a new PHP Resource
*/
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;
}

/**
Expand All @@ -56,6 +56,23 @@ public function getContent(Smarty_Template_Source $source)
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)
*
Expand All @@ -79,28 +96,16 @@ public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal
extract($_template->getTemplateVars());

// 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
* used in included file
*/
$_smarty_template = $_template;
include($source->filepath);
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,);
if (function_exists('ini_set')) {
ini_set('short_open_tag', $this->short_open_tag);
}
}
}
6 changes: 4 additions & 2 deletions libs/sysplugins/smarty_internal_runtime_cacheresourcefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
}
} else {
// delete only php files
if (substr($_filepath, -4) !== '.php') {
if (substr($_filepath, - 4) !== '.php') {
continue;
}
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
Expand Down Expand Up @@ -125,7 +125,9 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e
}
}
$_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);
}
}
Expand Down
8 changes: 6 additions & 2 deletions libs/sysplugins/smarty_internal_smartytemplatecompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ protected function doCompile($_content, $isTemplateSource = false)
then written to compiled files. */
// init the lexer/parser to compile the template
$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);
if ($isTemplateSource && $this->template->caching) {
$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();
mb_internal_encoding('ASCII');
} else {
Expand Down
117 changes: 59 additions & 58 deletions libs/sysplugins/smarty_template_compiled.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,64 +80,6 @@ public function populateCompiledFilepath(Smarty_Internal_Template $_template)
}
}

/**
* 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
*
Expand Down Expand Up @@ -179,6 +121,44 @@ public function render(Smarty_Internal_Template $_template)
}
}

/**
* 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
*
Expand Down Expand Up @@ -238,6 +218,27 @@ public function write(Smarty_Internal_Template $_template, $code)
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
*
Expand Down

0 comments on commit da22c96

Please sign in to comment.