Skip to content

Commit

Permalink
Reorder/rename the getAttachmentFilename() parameters
Browse files Browse the repository at this point in the history
Signed-off-by:Joshua Dickerson <joshua.a.dickerson@gmail.com>
  • Loading branch information
joshuaadickerson authored and norv committed May 14, 2013
1 parent bffbfae commit f909d8d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 67 deletions.
63 changes: 10 additions & 53 deletions sources/Subs.php
Expand Up @@ -3300,17 +3300,14 @@ function template_css()

/**
* Get an attachment's encrypted filename. If $new is true, won't check for file existence.
* @todo this currently returns the hash if new, and the full filename otherwise.
* Something messy like that.
* @todo and of course everything relies on this behavior and work around it. :P.
* Converters included.
*
* @param string $filename
* @param int $attachment_id
* @param string $dir
* @param string $file_hash
* @param int $attachment_id
* @param string $filename = ''
* @param int $id_folder = null
* @return string The full path to the attachment
*/
function getAttachmentFilename($filename, $attachment_id, $dir = null, $file_hash = '')
function getAttachmentFilename($file_hash, $attachment_id, $filename, $id_folder = null)
{
global $modSettings, $smcFunc;

Expand All @@ -3323,7 +3320,7 @@ function getAttachmentFilename($filename, $attachment_id, $dir = null, $file_has
FROM {db_prefix}attachments
WHERE id_attach = {int:id_attach}',
array(
'id_attach' => $attachment_id,
'id_attach' => (int) $attachment_id,
));

if ($smcFunc['db_num_rows']($request) === 0)
Expand All @@ -3335,62 +3332,22 @@ function getAttachmentFilename($filename, $attachment_id, $dir = null, $file_has

// In case of files from the old system, do a legacy call.
if (empty($file_hash))
return getLegacyAttachmentFilename($filename, $attachment_id, $dir, $new);

// Are we using multiple directories?
if (!empty($modSettings['currentAttachmentUploadDir']))
{
if (!is_array($modSettings['attachmentUploadDir']))
$modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
$path = isset($modSettings['attachmentUploadDir'][$dir]) ? $modSettings['attachmentUploadDir'][$dir] : $modSettings['attachmentUploadDir'];
require_once(SUBSDIR . '/Attachments.subs.php');
return getLegacyAttachmentFilename($filename, $attachment_id, $id_folder);
}
else
$path = $modSettings['attachmentUploadDir'];

return $path . '/' . $attachment_id . '_' . $file_hash;
}

/**
* 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, $db_character_set;

$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];
$path = isset($modSettings['attachmentUploadDir'][$id_folder]) ? $modSettings['attachmentUploadDir'][$id_folder] : $modSettings['attachmentUploadDir'];
}
else
$path = $modSettings['attachmentUploadDir'];

if (file_exists($path . '/' . $enc_name))
$filename = $path . '/' . $enc_name;
else
$filename = $path . '/' . $clean_name;

return $filename;
return $path . '/' . $attachment_id . '_' . $file_hash;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions sources/admin/ManageAttachments.php
Expand Up @@ -924,7 +924,7 @@ public function action_repair()
// If we are repairing remove the file from disk now.
if ($fix_errors && in_array('missing_thumbnail_parent', $to_fix))
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$filename = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);
@unlink($filename);
}
}
Expand Down Expand Up @@ -1034,7 +1034,7 @@ public function action_repair()
if ($row['attachment_type'] == 1)
$filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
else
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$filename = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);

// File doesn't exist?
if (!file_exists($filename))
Expand Down Expand Up @@ -1153,7 +1153,7 @@ public function action_repair()
if ($row['attachment_type'] == 1)
$filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
else
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$filename = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);
@unlink($filename);
}
}
Expand Down Expand Up @@ -1221,7 +1221,7 @@ public function action_repair()
// If we are repairing remove the file from disk now.
if ($fix_errors && in_array('attachment_no_msg', $to_fix))
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$filename = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);
@unlink($filename);
}
}
Expand Down Expand Up @@ -2030,7 +2030,7 @@ public function action_transfer()
}
}

$source = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$source = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);
$dest = $modSettings['attachmentUploadDir'][$new_dir] . '/' . basename($source);

if (@rename($source, $dest))
Expand Down
2 changes: 1 addition & 1 deletion sources/controllers/Attachment.controller.php
Expand Up @@ -81,7 +81,7 @@ function action_dlattach()
if (empty($is_avatar) || $attachment_type != 3)
increaseDownloadCounter($id_attach);

$filename = getAttachmentFilename($real_filename, $id_attach, $id_folder, $file_hash);
$filename = getAttachmentFilename($file_hash, $id_attach, $real_filename, $id_folder);

// This is done to clear any output that was made before now.
ob_end_clean();
Expand Down
5 changes: 3 additions & 2 deletions sources/controllers/Display.controller.php
Expand Up @@ -1199,6 +1199,7 @@ function loadAttachmentContext($id_msg)
{
$attachmentData[$i] = array(
'id' => $attachment['id_attach'],
'file_hash' => $attachment['file_hash'],
'name' => preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($attachment['filename'])),
'downloads' => $attachment['downloads'],
'size' => ($attachment['filesize'] < 1024000) ? round($attachment['filesize'] / 1024, 2) . ' ' . $txt['kilobyte'] : round($attachment['filesize'] / 1024 / 1024, 2) . ' ' . $txt['megabyte'],
Expand Down Expand Up @@ -1227,7 +1228,7 @@ function loadAttachmentContext($id_msg)
// A proper thumb doesn't exist yet? Create one!
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['file_hash'], $attachment['id_attach'], $attachment['filename'], $attachment['id_folder']);

require_once(SUBSDIR . '/Graphics.subs.php');
if (createThumbnail($filename, $modSettings['attachmentThumbWidth'], $modSettings['attachmentThumbHeight']))
Expand Down Expand Up @@ -1278,7 +1279,7 @@ function loadAttachmentContext($id_msg)
)
);

$thumb_realname = getAttachmentFilename($thumb_filename, $attachment['id_thumb'], $id_folder_thumb, $thumb_hash);
$thumb_realname = getAttachmentFilename($thumb_hash, $attachment['id_thumb'], $thumb_filename, $id_folder_thumb);
rename($filename . '_thumb', $thumb_realname);

// Do we need to remove an old thumbnail?
Expand Down
2 changes: 1 addition & 1 deletion sources/controllers/Topic.controller.php
Expand Up @@ -446,7 +446,7 @@ function action_printpage()
}
}

$row['filename'] = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$row['filename'] = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);

// save for the template
$context['printattach'][$row['id_msg']][] = $row;
Expand Down
53 changes: 48 additions & 5 deletions sources/subs/Attachments.subs.php
Expand Up @@ -761,7 +761,7 @@ function createAttachment(&$attachmentOptions)
return false;

// Now that we have the attach id, let's rename this sucker and finish up.
$attachmentOptions['destination'] = getAttachmentFilename(basename($attachmentOptions['name']), $attachmentOptions['id'], $attachmentOptions['id_folder'], $attachmentOptions['file_hash']);
$attachmentOptions['destination'] = getAttachmentFilename($attachmentOptions['file_hash'], $attachmentOptions['id'], basename($attachmentOptions['name']), $attachmentOptions['id_folder']);
rename($attachmentOptions['tmp_name'], $attachmentOptions['destination']);

// If it's not approved then add to the approval queue.
Expand Down Expand Up @@ -854,7 +854,7 @@ function createAttachment(&$attachmentOptions)
)
);

rename($thumb_path, getAttachmentFilename($thumb_filename, $attachmentOptions['thumb'], $modSettings['currentAttachmentUploadDir'], $thumb_file_hash));
rename($thumb_path, getAttachmentFilename($thumb_file_hash, $attachmentOptions['thumb'], $thumb_filename, $modSettings['currentAttachmentUploadDir']));
}
}
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ function removeAttachments($condition, $query_type = '', $return_affected_messag
}
else
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$filename = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);
@unlink($filename);

// If this was a thumb, the parent attachment should know about it.
Expand All @@ -1135,7 +1135,7 @@ function removeAttachments($condition, $query_type = '', $return_affected_messag
// If this attachments has a thumb, remove it as well.
if (!empty($row['id_thumb']) && $autoThumbRemoval)
{
$thumb_filename = getAttachmentFilename($row['thumb_filename'], $row['id_thumb'], $row['thumb_folder'], $row['thumb_file_hash']);
$thumb_filename = getAttachmentFilename($row['thumb_file_hash'], $row['id_thumb'], $row['thumb_filename'], $row['thumb_folder']);
@unlink($thumb_filename);
$attach[] = $row['id_thumb'];
}
Expand Down Expand Up @@ -2336,7 +2336,7 @@ function moveAvatars()
$updatedAvatars = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], $row['file_hash']);
$filename = getAttachmentFilename($row['file_hash'], $row['id_attach'], $row['filename'], $row['id_folder']);

if (rename($filename, $modSettings['custom_avatar_dir'] . '/' . $row['filename']))
$updatedAvatars[] = $row['id_attach'];
Expand Down Expand Up @@ -2379,4 +2379,47 @@ function setRemovalNotice($messages, $notice)
function getAttachmentFileHash($seed = '')
{
return sha1(md5($seed . time()) . mt_rand());
}

/**
* 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, $db_character_set;

$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 f909d8d

Please sign in to comment.