Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Setting Compile Directory to a non-file system Stream

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Gozzy
Smarty Rookie


Joined: 12 May 2021
Posts: 5

PostPosted: Wed May 12, 2021 3:46 pm    Post subject: Setting Compile Directory to a non-file system Stream Reply with quote

Way back in the dim and distant past (see this thread), there was a request to allow the compile directory to be moved to a stream.

I want to do the same as my application is highly distributed and centralisation is via a database. I have a stream wrapper for the database but I cannot get Smarty to play ball. I can use $smarty->setCompileDir('dbfs://templates_c/') to set the directory to my stream wrapper (protocol registered as 'dbfs'), but when testing on Windows the Smarty::_realpath() method mangles my stream wrapper path and changes all instances of / to \ so my stream wrapper never gets called.

For the cache I have resources which work perfectly, but there is no similar solution for compile.

Are there any solutions I am missing, or is this a bug in the handling of file names which use stream protocols?

Thanks,
G.
Back to top
View user's profile Send private message
Gozzy
Smarty Rookie


Joined: 12 May 2021
Posts: 5

PostPosted: Thu May 13, 2021 2:41 pm    Post subject: Reply with quote

I should probably add that I am running the latest release, Smarty 3.1.39 on top of PHP 8. Smarty was installed via Composer.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat May 15, 2021 11:35 am    Post subject: Reply with quote

How would PHP then include files from this wrapper?
Especially considering that in normal setup, allow_url_include=Off.
Back to top
View user's profile Send private message
Gozzy
Smarty Rookie


Joined: 12 May 2021
Posts: 5

PostPosted: Sat May 15, 2021 3:39 pm    Post subject: Reply with quote

AnrDaemon wrote:
How would PHP then include files from this wrapper?

It is a stream wrapper registered with stream_wrapper_register() so any PHP code - including Smarty - would use the standard file access functions (e.g. fopen, fwrite, file_get_contents, etc), and the wrapper translates that. The precise implementation details of the wrapper are not important because PHP treats it the same as any other stream.

AnrDaemon wrote:
Especially considering that in normal setup, allow_url_include=Off

I am not trying to include template files this way, for that I already have a working custom Resource. My only interest is in pointing the compile directory at my stream.

The Smarty::_realpath() method supports streams, insofar as it uses a regular expression that parses the stream name correctly. The issue is the mishandling of directory separators when running on Windows. This might simply be a Windows only bug, or it might be deliberate, or I might be missing a trick in implementation. Any thoughts welcome!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun May 16, 2021 7:29 pm    Post subject: Reply with quote

Yet again: compiled template is a regular PHP code.
It is not read using file access functions, it is directly executed (see Smarty_Template_Compiled::loadCompiledTemplate).
Back to top
View user's profile Send private message
Gozzy
Smarty Rookie


Joined: 12 May 2021
Posts: 5

PostPosted: Tue May 18, 2021 2:26 pm    Post subject: Reply with quote

AnrDaemon wrote:
Yet again: compiled template is a regular PHP code.
It is not read using file access functions, it is directly executed (see Smarty_Template_Compiled::loadCompiledTemplate).

Understood, thanks.

Looking at Smarty_Template_Compiled::loadCompiledTemplate I see it uses include. By setting allow_url_include = On as you recommended I can get a test include to work just fine with my stream wrapper.

But, as I said originally, this still does not work because the Smarty::_realpath() method messes up the compile directory path on Windows so my stream wrapper never gets called. Indeed I get a fatal error in Smarty_Internal_Runtime_WriteFile->writeFile(), way before the file is read back and executed.

I suspect this is an unintended side-effect of the different directory separator on Windows so will raise it as a bug.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed May 19, 2021 11:52 am    Post subject: Reply with quote

Gozzy wrote:
By setting allow_url_include = On as you recommended I can get a test include to work just fine with my stream wrapper.

And any other stream wrapper including HTTP. Do you realize you just created a backdoor in your system that's waiting to be exploited?
Back to top
View user's profile Send private message
Gozzy
Smarty Rookie


Joined: 12 May 2021
Posts: 5

PostPosted: Wed May 19, 2021 12:05 pm    Post subject: Reply with quote

AnrDaemon wrote:
Gozzy wrote:
By setting allow_url_include = On as you recommended I can get a test include to work just fine with my stream wrapper.

And any other stream wrapper including HTTP. Do you realize you just created a backdoor in your system that's waiting to be exploited?

Indeed. Hence why I would prefer a more considered solution as posited in that thread from 2007. Given your knowledge of Smarty and PHP I would welcome any constructive thoughts you might have on that.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP