_mimemail_url bug
dlr - April 2, 2007 - 11:35
| Project: | Mime Mail |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | critical |
| Assigned: | Unassigned |
| Status: | closed |
Description
(406) $url = str_replace('?q=', '', ltrim($url, base_path())); sometimes breaks urls (if url begins with one of the base_path()'s char) , i think something like $url = str_replace('?q=', '',ereg_replace(base_path(),$url)); will be better.
Hope it helps
David

#1
This bug bit me today and in looking through the process I found that your suspicion is correct.
The problem is that when using ltrim() it will continue its replacement until it encounters a character that is not in the $charlist of the second parameter. http://us2.php.net/ltrim This is not a substring replacement function it is a "trim" using any character in the supplied list.
I suggest using preg_replace() instead of ereg_replace() and adding the limit to equal 1 so that it only removes the beginning of the URL.
//echo preg_replace( "!".base_path()."!", "", $url,1 );$url = str_replace('?q=', '', preg_replace( "!".base_path()."!", "", $url,1 ));
I think that this line should be refactored to unravel its complexity. I'll try to submit a patch later.
#2
... here's a patch.
#3
Issues with patches should be flagged accordingly to avoid falling under the radar.
I can't test this, but both the problem and the fix look fairly straightforward. ltrim() is really just wrong for this purpose, as mentioned - ltrim()'s second parameter is not a string to remove, it's simply a blacklist of trimmable characters. That it succeeds is a matter of luck.
Analogy: ltrim("apples/bananas","apples/") -> "bananas" works (by chance!), but
ltrim("bakery/bananas","bakery/") -> "nanas" simply gets nonsensical results.
#4
Patch tested and works correctly.
#5
One additional observation. One of my sites on a Windows box running IIS does not use the RewriteEngine and does not always have clean URLs, so the URLs submitted to _mimemail_url may look like "/?q=services" or "/?q=node/23". What I have found properly adjusts these is to use:
$url = preg_replace( "!".base_path()."(index.php)?!", "", $url,1 );#6
Fixed the problem I was having with this on a client site. Thanks!
Michelle
#7
I wasn't able to reproduce the issue on my test site, as I wasn't sure how to configure to reproduce it. I also had a problem applying the patch linked above and as such, i've re-factored it a bit. The included patch file should work. Testing it, I did not see any issues.
#8
Uploading an updates patch, this patch takes into account the "Code Cleanup" patch from: http://drupal.org/node/220432
This update requires that patch prior to being applied.
#9
Committed to HEAD, will roll a D5 release soon. Thanks!
#10
Automatically closed -- issue fixed for two weeks with no activity.