Skip to content

Commit

Permalink
! move getLegacyAttachmentFilename to Attachments.subs.php and out of…
Browse files Browse the repository at this point in the history
… Subs.php

! use the filehash in the call, not the legacy call?
! parts of elkarte#403

Signed-off-by: Spuds <spuds@spudsdesign.com>
  • Loading branch information
Spuds committed Nov 2, 2013
1 parent 1e8df13 commit 98a585f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
48 changes: 4 additions & 44 deletions sources/Subs.php
Expand Up @@ -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)
Expand All @@ -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']))
Expand All @@ -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.
Expand Down
46 changes: 45 additions & 1 deletion sources/subs/Attachments.subs.php
Expand Up @@ -2687,6 +2687,7 @@ function loadAttachmentContext($id_msg)
'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $topic . '.0;attach=' . $attachment['id_attach'] . '">' . htmlspecialchars($attachment['filename']) . '</a>',
'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.
Expand All @@ -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']));
}

Expand Down Expand Up @@ -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;
}

0 comments on commit 98a585f

Please sign in to comment.