diff --git a/sources/Subs.php b/sources/Subs.php index d31f643b06..09cec750c5 100644 --- a/sources/Subs.php +++ b/sources/Subs.php @@ -3501,7 +3501,7 @@ function getAttachmentFilename($filename, $attachment_id, $dir = null, $new = fa FROM {db_prefix}attachments WHERE id_attach = {int:id_attach}', array( - 'id_attach' => $attachment_id, + 'id_attach' => (int) $attachment_id, )); if ($db->num_rows($request) === 0) @@ -3513,7 +3513,10 @@ function getAttachmentFilename($filename, $attachment_id, $dir = null, $new = fa // In case of files from the old system, do a legacy call. if (empty($file_hash)) + { + require_once(SUBSDIR . '/Attachments.subs.php'); return getLegacyAttachmentFilename($filename, $attachment_id, $dir, $new); + } // Are we using multiple directories? if (!empty($modSettings['currentAttachmentUploadDir'])) @@ -3528,49 +3531,6 @@ function getAttachmentFilename($filename, $attachment_id, $dir = null, $new = fa return $path . '/' . $attachment_id . '_' . $file_hash . '.elk'; } -/** - * Older attachments may still use this function. - * - * @param $filename - * @param $attachment_id - * @param $dir - * @param $new - */ -function getLegacyAttachmentFilename($filename, $attachment_id, $dir = null, $new = false) -{ - global $modSettings; - - $clean_name = $filename; - - // Sorry, no spaces, dots, or anything else but letters allowed. - $clean_name = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $clean_name); - - $enc_name = $attachment_id . '_' . strtr($clean_name, '.', '_') . md5($clean_name); - $clean_name = preg_replace('~\.[\.]+~', '.', $clean_name); - - if ($attachment_id == false || ($new && empty($modSettings['attachmentEncryptFilenames']))) - return $clean_name; - elseif ($new) - return $enc_name; - - // Are we using multiple directories? - if (!empty($modSettings['currentAttachmentUploadDir'])) - { - if (!is_array($modSettings['attachmentUploadDir'])) - $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); - $path = $modSettings['attachmentUploadDir'][$dir]; - } - else - $path = $modSettings['attachmentUploadDir']; - - if (file_exists($path . '/' . $enc_name)) - $filename = $path . '/' . $enc_name; - else - $filename = $path . '/' . $clean_name; - - return $filename; -} - /** * Convert a single IP to a ranged IP. * internal function used to convert a user-readable format to a format suitable for the database. diff --git a/sources/subs/Attachments.subs.php b/sources/subs/Attachments.subs.php index b95e9a927c..2f7ece860b 100644 --- a/sources/subs/Attachments.subs.php +++ b/sources/subs/Attachments.subs.php @@ -2687,6 +2687,7 @@ function loadAttachmentContext($id_msg) 'link' => '' . htmlspecialchars($attachment['filename']) . '', 'is_image' => !empty($attachment['width']) && !empty($attachment['height']) && !empty($modSettings['attachmentShowImages']), 'is_approved' => $attachment['approved'], + 'file_hash' => $attachment['file_hash'], ); // If something is unapproved we'll note it so we can sort them. @@ -2707,7 +2708,7 @@ function loadAttachmentContext($id_msg) // A proper thumb doesn't exist yet? Create one! Or, it needs update. if (empty($attachment['id_thumb']) || $attachment['thumb_width'] > $modSettings['attachmentThumbWidth'] || $attachment['thumb_height'] > $modSettings['attachmentThumbHeight'] || ($attachment['thumb_width'] < $modSettings['attachmentThumbWidth'] && $attachment['thumb_height'] < $modSettings['attachmentThumbHeight'])) { - $filename = getAttachmentFilename($attachment['filename'], $attachment['id_attach'], $attachment['id_folder']); + $filename = getAttachmentFilename($attachment['filename'], $attachment['id_attach'], $attachment['id_folder'], false, $attachment['file_hash']); $attachment = array_merge($attachment, updateAttachmentThumbnail($filename, $attachment['id_attach'], $id_msg, $attachment['id_thumb'])); } @@ -2815,4 +2816,47 @@ function countAttachmentsInFolders($id_folder) $db->free_result($request); return $num_attach; +} + +/** + * Older attachments may still use this function. + * + * @param $filename + * @param $attachment_id + * @param $dir + * @param $new + */ +function getLegacyAttachmentFilename($filename, $attachment_id, $dir = null, $new = false) +{ + global $modSettings; + + $clean_name = $filename; + + // Sorry, no spaces, dots, or anything else but letters allowed. + $clean_name = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $clean_name); + + $enc_name = $attachment_id . '_' . strtr($clean_name, '.', '_') . md5($clean_name); + $clean_name = preg_replace('~\.[\.]+~', '.', $clean_name); + + if ($attachment_id == false || ($new && empty($modSettings['attachmentEncryptFilenames']))) + return $clean_name; + elseif ($new) + return $enc_name; + + // Are we using multiple directories? + if (!empty($modSettings['currentAttachmentUploadDir'])) + { + if (!is_array($modSettings['attachmentUploadDir'])) + $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); + $path = $modSettings['attachmentUploadDir'][$dir]; + } + else + $path = $modSettings['attachmentUploadDir']; + + if (file_exists($path . '/' . $enc_name)) + $filename = $path . '/' . $enc_name; + else + $filename = $path . '/' . $clean_name; + + return $filename; } \ No newline at end of file