cms_join_path - backslash considered harmful

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
rickb
Forum Members
Forum Members
Posts: 14
Joined: Mon Feb 23, 2009 12:55 pm

cms_join_path - backslash considered harmful

Post 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
Dee
Power Poster
Power Poster
Posts: 1197
Joined: Sun Mar 19, 2006 8:46 pm
Location: the Netherlands

Re: cms_join_path - backslash considered harmful

Post 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
Post Reply

Return to “Developers Discussion”