Page 1 of 1

cms_join_path - backslash considered harmful

Posted: Sat Jul 18, 2009 3:30 pm
by rickb
A widely supported feature of modern desktop & server operating system is  for directory names to consist of words separated by '/' (forward slash).  This includes Microsoft Windows (except old versions and special situations such as the cmd.exe shell).  There is more on this topic on Wikipedia http://en.wikipedia.org/wiki/Path_(computing).  A common misunderstanding is that Windows does not allow '/' in file paths; however in fact within PHP programs it is OK to use '/' in file paths even on Windows.

Because URLs always require forward slashes, it is risky to use code that may produce backslashes in some circumstances.  Unfortunately, cms_join_path probably encourages developers to produce strings used both for file paths and URLs which can then cause errors if the code is ported to a Windows server.  I found some examples by inspection of the TrueTypeText module, which may produce invalid URLs when running on a Windows server (more details here http://dev.cmsmadesimple.org/feature_request/view/3749)

A simple solution would be to change the function to this:

Code: Select all

function cms_join_path()
{
	$args = func_get_args();
	return implode('/',$args);
}
Rick

Re: cms_join_path - backslash considered harmful

Posted: Sat Jul 18, 2009 5:32 pm
by Dee
rickb wrote: Unfortunately, cms_join_path probably encourages developers to produce strings used both for file paths and URLs which can then cause errors
cms_join_path is to create file paths, not URL's.
It uses the system directory separator (PHP's DIRECTORY_SEPARATOR constant) so paths will work on systems where paths with (forward) slashes won't.

Regards,
D