Bear with me, it's been years since I've written PHP, and this is my first patch for anything Drupal related...
If you create a new Gallery relationship, and set the "Gallery Directory" to "galleries/%gallery_id", everything works great until you start getting creative with your gallery names.
Let's say I name a gallery "NG Gallery #4?; with! bad $characters". The module will create the directory by simply replacing the spaces with underscores, which is probably not good enough when dealing with user supplied data. Using a name with a pound sign like "Gallery #5" appears to work at first, but imagecache will break because of the non-encoded # in the URI.
I looked around Drupal's api, but couldn't find a function that does sanitization of filesystem paths, so I whipped one up for this module. Using the following patch allows me to create a gallery named like the one above, and everything "just works".
This is my first contribution to Drupal, and I'm all ears for suggestions. See the attached patch.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | translit.patch | 2.79 KB | justintime |
| #6 | transliteration-support.patch | 1.12 KB | justintime |
| #5 | transliteration-support.patch | 1.11 KB | justintime |
| ng_gallery_dirname.patch | 1.88 KB | justintime |
Comments
Comment #1
dbeall commentedI may be wrong here and I don't even know how to write PHP...
but, doesn't pathauto take care of this.
Administer > URL aliases > Automated alias settings
field set, Punctuation settings
and transliteration for other weird stuff
Comment #2
justintime commentedpathauto takes care of URL's, but not form data -> directories.
transliteration is kinda overkill for this issue, I think. Generally you want the module to not depend on another module for just a few lines of code, do you?
Comment #3
dbeall commentedgood point, your most probably right.
Comment #4
kmontyThis was fixed two weeks ago. See #584096: 'strange' characters in directory name will cause the image to not display
We will roll out another release soon.
Comment #5
justintime commentedI'm using HEAD, so the problem's actually still there. #'s and &'s aren't accounted for in the preg replace. I asked around on IRC if anyone knew of an API call in core we could use. There isn't one in core, but marcingy brought up transliteration too. He pointed out that filefield is using it, so it must be pretty stable. As it turns out, the code from filefield can be copied and pasted with a couple of changes. See attached patch that uses transliteration if present.
Comment #6
justintime commentedGah, no matter how simple the patch, you have to test!!!!
I misnamed the variable in the previous patch. This one works. Sorry for the mishap.
Comment #7
justintime commentedComment #8
justintime commentedSo, my version was out of date. HEAD does indeed account for &'s and #'s. The patch I attached will still work, and is probably a bit more thorough than the preg_replace (it will work with UTF-8 chars too), but it depends on an external module. It will work fine if transliteration isn't needed. Your call on whether to include it or not.
Comment #9
dbeall commented@justintime, Your site is great, a real good read and I always learn something.. Some people may not like that kind of stuff, but I really enjoyed it.
Comment #10
kmontyI think this patch is solid except you have to have transliteration installed. What happens without it? I think we need a fallback, maybe the preg_replace we are already using?
Comment #11
justintime commentedI added a helper function to do this for us. The function uses transliteration if available, and falls back to preg_replace+str_replace if not. There was one other spot that I patched to use the helper function as well.
Comment #12
dbeall commentedI tested this and it works nice:
with and without transliteration.
I used the following characters in my image names:
! @ # $ % ^ & ( ) + ; }
Both with and without transliteration, it stripped them down to 004.jpg, 4thJuly06.jpg, and 003.jpg
oh,, yeah, image cache worked fine using %gallery_name for directory..
Comment #13
dbeall commentedwell, not so fast. I put the file back without the patch and uninstalled transliteration..
It still strips the characters out. I do not have pathauto installed
Maybe some one can tell me how to test this...
Comment #14
dbeall commentedsorry, testing the wrong thing here, it's the gallery name... duuuu..
EDIT: still can't break it.. How to test...
Comment #15
justintime commented@dbeall
The code from http://drupal.org/node/584096 makes sure that it doesn't break, and that code is still used if transliteration isn't there. However, if transliteration is there, it will try to make sense of what you're trying to do. Also, the patch will run both the image filename and the gallery directory through the filter, so you were testing the right area, just not with the completely correct data. Here's the steps to test:
Comment #16
kmontyPatch looks good. I'll commit this to HEAD soon.
Comment #17
dbeall commentedwithout transliteration
gallery name= ďßèâĩĩ test .. file system produces = _test
image name= ďßèâĩĩ test4thJuly06.JPG .. file system produces = _test4thJuly06.JPG
WITH transliteration
gallery name= ďßèâĩĩ test translit .. file system produces = _test_translit
image name= ďßèâĩĩ test4thJuly06.JPG .. file system produces = dsseaii_test4thJuly06.JPG
I don't if this helps any.. I guess i need to change the key board to something other than english to do it right..
Comment #18
kmontyCommitted. Thanks justintime!