CodeIgniter Forums
index.php - Defined Constants not Absolute [with fix] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: index.php - Defined Constants not Absolute [with fix] (/thread-10113.html)



index.php - Defined Constants not Absolute [with fix] - El Forum - 07-18-2008

[eluser]Pygon[/eluser]
The more I have to come back to this thing, the more annoyed I get.

Code:
/*
|---------------------------------------------------------------
| SET THE SERVER PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full-server path to the "system"
| folder in order to reduce the possibility of path problems.
| Note: We only attempt this if the user hasn't specified a
| full server path.
|
*/
if (strpos($system_folder, '/') === FALSE)
{
    if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE)
    {
        $system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
    }
}
else
{
    // Swap directory separators to Unix style for consistency
    $system_folder = str_replace("\\", "/", $system_folder);
}

/*
|---------------------------------------------------------------
| DEFINE APPLICATION CONSTANTS
|---------------------------------------------------------------
|
| EXT        - The file extension.  Typically ".php"
| FCPATH    - The full server path to THIS file
| SELF        - The name of THIS file (typically "index.php)
| BASEPATH    - The full server path to the "system" folder
| APPPATH    - The full server path to the "application" folder
|
*/
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');

if (is_dir($application_folder))
{
    define('APPPATH', $application_folder.'/');
}
else
{
    if ($application_folder == '')
    {
        $application_folder = 'application';
    }

    define('APPPATH', BASEPATH.$application_folder.'/');
}


First and foremost, the APPPATH is defined as:
" APPPATH - The full server path to the "application" folder "

Unfortunately, this is untrue:
Code:
define('APPPATH', $application_folder.'/'); //relative path
and:
Code:
define('APPPATH', BASEPATH.$application_folder.'/'); //an "almost" absolute path.

Second:
There is some seriously terrible stuff going on with the setting of $system_folder, such as checking if realpath is a function (has been for a LONG time), using strpos('/') to determine if it's an "absolute" path ("./something" is not ABSOLUTE path...) and using realpath(dirname()) (err why?)*.

Look, I'm not saying this code is terrible (I'm atleast not trying to be insulting by saying this), I'm wondering, why is this STILL like this?

I use this for real absolute paths:
Code:
/*
|---------------------------------------------------------------
| SET THE SERVER PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full server path to the "system"
| folder in order to reduce the possibility of path problems.
|
*/
$system_folder = str_replace('\\','/',realpath($system_folder));
if(!$system_folder)
{
    //Try the default
    $system_folder = str_replace('\\','/',realpath(__FILE__).'/system');
}
if(!is_dir($system_folder))
{
    die('Invalid system folder specified. Please check your $system_folder setting.');
}

/*
|---------------------------------------------------------------
| SET THE APPLICATION PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full system path to the "application"
| folder in order to reduce the possibility of path problems.
|
*/
$application_folder = str_replace('\\','/',realpath($application_folder));
if(!$application_folder)
{
    //Try the default
    $application_folder = str_replace('\\','/',realpath(BASEPATH.'application'));
}
if(!is_dir($application_folder))
{
    die('Invalid system folder specified. Please check your $system_folder setting.');
}


/*
|---------------------------------------------------------------
| DEFINE APPLICATION CONSTANTS
|---------------------------------------------------------------
|
| EXT        - The file extension.  Typically ".php"
| FCPATH    - The full server path to THIS file
| SELF        - The name of THIS file (typically "index.php)
| BASEPATH    - The full server path to the "system" folder
| APPPATH    - The full server path to the "application" folder
|
*/
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');
define('APPPATH',$application_folder.'/');

*inparo has pointed this out to be my error.


index.php - Defined Constants not Absolute [with fix] - El Forum - 07-18-2008

[eluser]Pascal Kriete[/eluser]
Hehe, makes me laugh. The only thing you're right about is that application_path is not necessarily absolute, since is_dir handles relative paths. Everything else..

Let's read the comments:
Quote:| Include the path if the folder is not in the same directory
| as this file.

Oh, I get it, so I add a ./ if it's in the same folder. Not.

Bring us straight to using strpos('/'). Oh wait, if we follow directions this does exactly what it's supposed to. It also checks if dirname is available, because some hosts disable it. Bummer.

So far so good, now comes the funny bit:
[quote author="Pygon" date="1216442143"]
using realpath(dirname()) (err why?)

....
Code:
//Try the default
    $system_folder = str_replace('\\','/',realpath(__FILE__).'/system');
[/quote]

realpath(__FILE__) leaves you with: /something/another/index.php
Add /system and we get: /something/another/index.php/system

Well, congratulations.


index.php - Defined Constants not Absolute [with fix] - El Forum - 07-18-2008

[eluser]Pygon[/eluser]
The only thing you correctly identified that I have screwed up is the use of dirname(). You're absolutely right that this is a correct use and I did make a mistake.

Otherwise, "dirname is available, because some hosts disable it". I must be under a misconception from the PHP documentation that this PHP core function, as well as "realpath". In fact, this isn't even possible to disable according to any php configuration I could find. Also, "./" is in no way an absolute path without using realpath, and I'm not even sure where your quote came from since it's not in any of my post.

Look, I'm not trying to be an ass ([redacted]), I'm trying to improve the framework. A simple "realpath would actually include the filename" would have been useful.

Fixed:
Code:
/*
|---------------------------------------------------------------
| SET THE SERVER PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full server path to the "system"
| folder in order to reduce the possibility of path problems.
|
*/
$system_folder = str_replace('\\','/',realpath($system_folder));
if(!$system_folder)
{
    //Try the default
    $system_folder = str_replace('\\','/',realpath(dirname(__FILE__)).'/system');
}
if(!is_dir($system_folder))
{
    die('Invalid system folder specified. Please check your $system_folder setting.');
}

/*
|---------------------------------------------------------------
| SET THE APPLICATION PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full system path to the "application"
| folder in order to reduce the possibility of path problems.
|
*/
$application_folder = str_replace('\\','/',realpath($application_folder));
if(!$application_folder)
{
    //Try the default
    $application_folder = str_replace('\\','/',realpath(BASEPATH.'application'));
}
if(!is_dir($application_folder))
{
    die('Invalid system folder specified. Please check your $system_folder setting.');
}


/*
|---------------------------------------------------------------
| DEFINE APPLICATION CONSTANTS
|---------------------------------------------------------------
|
| EXT        - The file extension.  Typically ".php"
| FCPATH    - The full server path to THIS file
| SELF        - The name of THIS file (typically "index.php)
| BASEPATH    - The full server path to the "system" folder
| APPPATH    - The full server path to the "application" folder
|
*/
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');
define('APPPATH',$application_folder.'/');



index.php - Defined Constants not Absolute [with fix] - El Forum - 07-19-2008

[eluser]Pascal Kriete[/eluser]
Ok, I should apologize, the reply came across too harsh.

A quick search for function_exists('realpath') shows that it's a very commonly used snippet. Of course that doesn't make it right. It's almost always accompanied by something like: "It's broken on my server". If more than one person has that problem, it has to be considered in a framework.

Now to clarify the strpos bit. Here's your quote:
Quote:using strpos(’/’) to determine if it’s an “absolute” path ("./something" is not ABSOLUTE path...)

And the accompanying CI code:
Code:
if (strpos($system_folder, '/') === FALSE)
{
    if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE)
    {
        $system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
    }
}
else
{
    // Swap directory separators to Unix style for consistency
    $system_folder = str_replace("\\", "/", $system_folder);
}

The $system_folder variable either takes a folder name, or a full system path. My server borks when I give it a relative path - that could be a server issue though. So with that prerequisite in mind, the only reason why the else statement would run (and no realpath checking happens) is when a full system path is given.

Quote:The majority of your posts seems to be more about cutting down people than being helpful.
I replied to your post in a tone that I considered to be in line with the way you presented your case at the time. However, not once did I make this personal and I would appreciate if you didn't either. As for cutting people down, if anyone ever feels mistreated by me, they have every right (and I would encourage them) to speak up and put me in my place. We're all equal on this board.

Quote:I’m trying to improve the framework.
So am I. Your last snippet looks like it will work for me, my concern throughout this was compatibility over the wide range of servers that CI is used on.


index.php - Defined Constants not Absolute [with fix] - El Forum - 07-19-2008

[eluser]Pygon[/eluser]
I actually removed the part of my statement that you were more about cutting people down (I was a bit heated and it was an unfair statement so I do appologize).

Quote:A quick search for function_exists(’realpath’) shows that it’s a very commonly used snippet. Of course that doesn’t make it right. It’s almost always accompanied by something like: “It’s broken on my server”.

You're absolutely correct that there are many implementations of this (none of which would ever actually fix the issue of directory permissions on folders higher than the CWD), and this would always pass as true because realpath DOES exist regardless of whether realpath() is working correctly or not. This ofcourse is assuming that PHP doesn't have some undocumented feature to disable(automatically?) realpath that I don't know about and can't find.

Quote:The $system_folder variable either takes a folder name, or a full system path. My server borks when I give it a relative path - that could be a server issue though. So with that prerequisite in mind, the only reason why the else statement would run (and no realpath checking happens) is when a full system path is given.

It absolutely could be a server issue, because "../system" "something/system" are not full system paths even though CI considers them to be using that snippet of code. Added: And it's not whether the system might fail out at some later point without good explaination, that's something we should try to avoid.


index.php - Defined Constants not Absolute [with fix] - El Forum - 07-19-2008

[eluser]Derek Jones[/eluser]
I really don't want to step into the conversation too much, as this boils down primarily to a problem you're having with the wording of a code comment. The point of that variable isn't to always and without reservation be a full server path, but to try to be a reliable means of gaining script access to the folder. It seems like a very modest demand of a developer's ability to provide a good path to the system folder. You seem pretty upset about this, though, but without explanation. Mind if I ask what problems the distributed code has given you that makes you "keep coming back to it"?

And incidentally, as you develop applications with a broad distribution and targeted for all sorts of shared hosting environments, you'll find that things that you would expect to be available based on PHP's documentation cannot be counted on, and that includes having access to functions like realpath(), which certain hosts have been known to disable, along with other functions that expose information about the server to the script.


index.php - Defined Constants not Absolute [with fix] - El Forum - 07-19-2008

[eluser]Pygon[/eluser]
Thanks for the reply Derek. I'm definitely* not upset by this, but I find it more of an annoyance than anything. You are correct that part of this issue lies with the documentation of the code -- when I attempt to use something that is documented to be a "full-system path" to the system directory, I would expect that it actually is a full path, not some random implementation of some sort of path that may or may not be relative or absolute from the front controller.

I can agree, as I review the code, that my solution does not correctly take into account a provider that has set up their system in such a way that realpath always returns false and therefore needs to be revised. However, function_exists('realpath') seems to be quite useless from all my research (and again, I could be wrong and I would be very pleased to be shown how this could be made not to exist).

Honestly, this is an attempt to arrive at some normalized and accurate way to use pathing through-out the system, rather than something that (to me) seems very hack-y in nature.

*corrected just for you, friend.


index.php - Defined Constants not Absolute [with fix] - El Forum - 07-19-2008

[eluser]sophistry[/eluser]
http://www.d-e-f-i-n-i-t-e-l-y.com