Skip to content

Commit cb38eca

Browse files
authored
fix: file uploader not always working (#94)
1 parent 3fd89da commit cb38eca

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

source/php/Submission.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ public static function uploadFiles($fileslist, $formId)
270270
$fileslist = self::sanitizeFilesList($fileslist);
271271

272272
foreach ($fileslist as $key => $files) {
273+
// Sanitizes the key to the field name (ex. removes id-1-)
274+
$sanitizedKey = preg_replace('/id-\d+-/', '', $key);
275+
273276
if (!is_array($files['name'])) {
274277
$files = self::convertItemToArray($files);
275278
}
@@ -278,31 +281,29 @@ public static function uploadFiles($fileslist, $formId)
278281
$fileName = pathinfo((string)$files['name'][$i], PATHINFO_FILENAME);
279282
$fileext = strtolower(pathinfo((string)$files['name'][$i], PATHINFO_EXTENSION));
280283

281-
282284
//Validate that image is in correct format
283-
if (in_array('image/*', $fields[$key]['filetypes'])) {
284-
$fields[$key]['filetypes'] = array_unique(array_merge($fields[$key]['filetypes'], $allowedImageTypes));
285+
if (in_array('image/*', $fields[$sanitizedKey]['filetypes'])) {
286+
$fields[$sanitizedKey]['filetypes'] = array_unique(array_merge($fields[$sanitizedKey]['filetypes'], $allowedImageTypes));
285287
}
286288

287289
//Validate that video is in correct format
288-
if (in_array('video/*', $fields[$key]['filetypes'])) {
289-
$fields[$key]['filetypes'] = array_unique(array_merge($fields[$key]['filetypes'], $allowedVideoTypes));
290+
if (in_array('video/*', $fields[$sanitizedKey]['filetypes'])) {
291+
$fields[$sanitizedKey]['filetypes'] = array_unique(array_merge($fields[$sanitizedKey]['filetypes'], $allowedVideoTypes));
290292
}
291293

292294

293295
//Not a valid filetype at all
294-
if (!in_array('.' . $fileext, $fields[$key]['filetypes'])) {
296+
if (!in_array('.' . $fileext, $fields[$sanitizedKey]['filetypes'])) {
295297
error_log('Filetype not allowed');
296298
$uploaded['error'] = true;
297299
$uploaded['errorData'] = new \WP_Error('filetype-not-allowed', __('Filetype not allowed', 'modularity-form-builder'));
298300
continue;
299301
}
300302

301-
302303
$encryptionConfigDefined = defined('ENCRYPT_SECRET_VI') && defined('ENCRYPT_SECRET_KEY') && defined('ENCRYPT_METHOD');
303304

304305
//Encrypt file if encryption is enabled
305-
if (get_option('options_mod_form_crypt') && empty($fields[$key]['upload_videos_external']) && $encryptionConfigDefined) {
306+
if (get_option('options_mod_form_crypt') && empty($fields[$sanitizedKey]['upload_videos_external']) && $encryptionConfigDefined) {
306307
$encrypted = file_put_contents(
307308
$files['tmp_name'][$i],
308309
\ModularityFormBuilder\App::encryptDecryptFile(
@@ -397,7 +398,7 @@ public static function maybeCreateFolder(string $path): string
397398
public function getMailDownloadLink($filePath)
398399
{
399400
//Check if encrypted
400-
if (strpos($filePath, sanitize_file_name("-enc-" . ENCRYPT_METHOD)) !== false) {
401+
if (defined('ENCRYPT_METHOD') && strpos($filePath, sanitize_file_name("-enc-" . ENCRYPT_METHOD)) !== false) {
401402
return home_url("/") . '?modFormDownloadEncFilePublic=' . urlencode(basename($filePath)) . '&token=' . $this->createToken($filePath);
402403
}
403404

0 commit comments

Comments
 (0)