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.

Comments

dbeall’s picture

I 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

justintime’s picture

pathauto 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?

dbeall’s picture

good point, your most probably right.

kmonty’s picture

Status: Needs review » Closed (duplicate)

This 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.

justintime’s picture

StatusFileSize
new1.11 KB

I'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.

justintime’s picture

StatusFileSize
new1.12 KB

Gah, 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.

justintime’s picture

Status: Closed (duplicate) » Needs review
justintime’s picture

So, 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.

dbeall’s picture

@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.

kmonty’s picture

Status: Needs review » Needs work

I 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?

justintime’s picture

Assigned: Unassigned » justintime
Status: Needs work » Needs review
StatusFileSize
new2.79 KB

I 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.

dbeall’s picture

I 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..

dbeall’s picture

well, 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...

dbeall’s picture

sorry, testing the wrong thing here, it's the gallery name... duuuu..
EDIT: still can't break it.. How to test...

justintime’s picture

@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:

  1. Install the patch, make sure transliteration module isn't enabled.
  2. Create a gallery named "ďßèâĩĩ's Gallery", and add images to it like "ďßèâĩĩ Image 1.jpg".
  3. I don't have the code in front of me, but I think it will strip all those wierd UTF characters, so you'll end up with a directory name of "s_Gallery" for the gallery, and filenames like "Image_1.jpg"
  4. Delete your gallery
  5. Enable transliteration
  6. Repeat step 2
  7. Check your directory structure. You should have something like "dbeall's Gallery" and "dbeall Image 1.jpg"
kmonty’s picture

Status: Needs review » Patch (to be ported)

Patch looks good. I'll commit this to HEAD soon.

dbeall’s picture

without 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..

kmonty’s picture

Status: Patch (to be ported) » Fixed

Committed. Thanks justintime!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.