Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Failing to understand Smarty::_realpath()

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1314

PostPosted: Tue Dec 05, 2017 2:08 pm    Post subject: Failing to understand Smarty::_realpath() Reply with quote

Can somebody please explain to me, what it does?
The code is so disconnected that I'm failing to make any sense of it. Consequently, unable to debug it, as it is failing in very simple cases of passing it a plain path with no tricks.
Back to top
View user's profile Send private message
carpii
Smarty Rookie


Joined: 06 Sep 2008
Posts: 14

PostPosted: Sat Dec 30, 2017 12:02 am    Post subject: Reply with quote

I don't know. It looks like very strange code personally.

My guess is it is used by Smarty to grab an absolute path to a script or base template, when there are subtemplates included.

Try adding some debug output to it, showing the inputs and then the returned path. It might help you to understand why and when its being used
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1314

PostPosted: Sat Dec 30, 2017 1:11 am    Post subject: Reply with quote

I know, why and when it is used, I don't understand, what it is supposed to do, as it seems to be working purely by accident.
Back to top
View user's profile Send private message
carpii
Smarty Rookie


Joined: 06 Sep 2008
Posts: 14

PostPosted: Tue Jan 02, 2018 2:37 am    Post subject: Reply with quote

The more I look at it the less I understand it Razz

From what I can see though, realpath is never called with false (only true or null).

There are some tests in libs/sysplugins/smarty_internal_testinstall.php which might shed some light on it too.

Code like this makes me think it should just be rewritten from scratch (even if it works, it's probably not optimal performance-wise)..

Quote:

$counts[] = (int)((strlen($m) - 1) / 3);
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1314

PostPosted: Wed Jan 03, 2018 12:17 am    Post subject: Reply with quote

carpii wrote:
Code like this makes me think it should just be rewritten from scratch

That's my full intention.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7364
Location: Lincoln Nebraska, USA

PostPosted: Thu Jan 04, 2018 7:08 pm    Post subject: Reply with quote

What part of the header description is confusing?

Code:
* Normalize path
     *  - remove /./ and /../
     *  - make it absolute if required
     *
     * @param string $path      file path
     * @param bool   $realpath  if true - convert to absolute
     *                          false - convert to relative
     *                          null - keep as it is but remove /./ /../


So it removes "." and ".." from the path, and if 2nd param is true, return absolute (system) path, otherwise return relative path. It is compatible with different directory separator types.

If you are testing it directly, make sure DIRECTORY_SEPARATOR is defined the way you want it. ie. use an instance of smarty. It is also going to use your cwd as the relative path location.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1314

PostPosted: Sun Jan 07, 2018 12:17 am    Post subject: Reply with quote

The problem is, the function code is a long mile away from the function description.
I'm going to take another look at it, and rewrite if I don't understand it again.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7364
Location: Lincoln Nebraska, USA

PostPosted: Tue Jan 09, 2018 1:01 am    Post subject: Reply with quote

It could probably be rewritten, at the risk of breaking something you don't realize you are breaking. I think this was written quite some time ago, when PHP and the OS's it ran on were quite a different animal. Think directory separators. If you do rewrite it, it could use ample unit testing both positive and negative.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1314

PostPosted: Tue Jan 09, 2018 11:00 pm    Post subject: Reply with quote

That's why I'm asking. I want to be 100% sure I fully understand the intention of this function before fixing it.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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