File Upload doesn't handle spaces in filename

dharmatech - May 22, 2008 - 22:56
Project:Webform
Version:5.x-1.10
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

Upload a file with a space in the filename. When the e-mail notice is sent out or in the results table, the space (and any other special characters) are not being properly encoded. This fix is applied against 5.x-1.10 but should also be applied against 2.0 as well.

Here's the fix.

Index: file.inc
===================================================================
--- file.inc (revision 1171)
+++ file.inc (working copy)
@@ -303,7 +303,9 @@
*/
function theme_webform_mail_file($data, $component) {
$file = unserialize($data);
- $output = $component['name'] .": ". (!empty($file['filepath']) ? file_create_url($file['filepath']) : '') ."\n";
+ $cleanurl = cleanurl(file_create_url($filedata['filepath']));
+
+ $output = $component['name'] .": ". (!empty($file['filepath']) ? $cleanurl : '') ."\n";
return $output;
}

@@ -329,7 +331,9 @@
$form_item['#default_value'] = empty($filedata['filepath']) ? $filedata['error'] : $filedata['filepath'];
}
if ($filedata['filename']) {
- $form_item['#suffix'] = ' Download '. $filedata['filename'] .'' . $form_item['#suffix'];
+ $cleanurl = cleanurl(file_create_url($filedata['filepath']));
+
+ $form_item['#suffix'] = ' Download '. $filedata['filename'] .'' . $form_item['#suffix'];
if ($enabled) {
$form_item['#description'] = t('Uploading a new file will replace the current file.');
$form_item['#webform_current_file'] = $filedata;
@@ -446,3 +450,12 @@
return empty($filedata['filename']) ? '\,' : $filedata['filename'] .'\,'. (int)($filedata['filesize']/1024);
}

+function cleanurl($url) {
+ $cleanurl = urlencode($url);
+ $cleanurl = str_replace("+","%20",$cleanurl);
+ $cleanurl = str_replace("%3A",":",$cleanurl);
+ $cleanurl = str_replace("%2F","/",$cleanurl);
+
+ return $cleanurl;
+}
+

#1

dharmatech - May 23, 2008 - 17:53

this...

function theme_webform_mail_file($data, $component) {
$file = unserialize($data);
- $output = $component['name'] .": ". (!empty($file['filepath']) ? file_create_url($file['filepath']) : '') ."\n";
+ $cleanurl = cleanurl(file_create_url($filedata['filepath']));
+
+ $output = $component['name'] .": ". (!empty($file['filepath']) ? $cleanurl : '') ."\n";
return $output;
}

should actually be...

function theme_webform_mail_file($data, $component) {
$file = unserialize($data);
- $output = $component['name'] .": ". (!empty($file['filepath']) ? file_create_url($file['filepath']) : '') ."\n";
+ $cleanurl = cleanurl(file_create_url($file['filepath']));
+
+ $output = $component['name'] .": ". (!empty($file['filepath']) ? $cleanurl : '') ."\n";
return $output;
}

#2

quicksketch - May 27, 2008 - 16:26

Thanks, could you reroll and attach as file?

#3

dharmatech - May 27, 2008 - 17:07

This is the file.inc for 5.x-1.10 that urlencodes the URI, but preserves ://~

AttachmentSize
file.inc_.5.x-1.10.txt18.34 KB

#4

dharmatech - May 27, 2008 - 17:08
Version:5.x-1.10» 5.x-2.0

This is the file.inc for 5.x-2.0 that urlencodes the URI, but preserves ://~

AttachmentSize
file.inc_.5.x-2.0.txt18.54 KB

#5

quicksketch - May 27, 2008 - 19:39

Oh sorry I wasn't clear. Could you attach the patch as a file, not the entire file itself? Sorry for the trouble!

#6

dharmatech - May 28, 2008 - 15:55

Here's the 5.x-2.0 diff.

AttachmentSize
file.inc_.5.x-2.0.diff.txt1.11 KB

#7

dharmatech - May 28, 2008 - 15:58
Version:5.x-2.0» 5.x-1.10

Here's the 5.x-1.10 diff.

AttachmentSize
file.inc_.5.x-1.10.diff.txt986 bytes

#8

quicksketch - May 31, 2008 - 19:48

I see the need for this, especially in the submitted e-mails. The implementation is a little wonky though, using urlencode then a strreplace on the encoding? Seems like we could just use rawurlencode and be done with it. How does this patch look as an alternative approach?

AttachmentSize
webform_file_url.patch1.75 KB

#9

quicksketch - May 31, 2008 - 22:28
Status:patch (code needs review)» fixed

Hm, I accidentally committed the 5.x version with another patch. I'll just leave it in there and commit to the 6.x version also. Please reopen if further correction is necessary. Thanks!

#10

dharmatech - June 2, 2008 - 17:28

Much cleaner than my fix. Thanks.

#11

quicksketch - June 2, 2008 - 19:03

Thanks for your patch also! I don't mean to be troublesome, but patches really help me review and commit changes. The first (inline) patch you posted was great except it was inline without the <code> tags around it. The later patches should have been in the Unified format, but I worked with it alright. Thanks again!

#12

Anonymous (not verified) - June 16, 2008 - 19:11
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.