News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Problem with Attachements

Started by Martin12, November 18, 2009, 02:28:27 AM

Previous topic - Next topic

Martin12

There seems to be a problem with downloading attachments on my forum after the upgrade.  All of the correct paths and settings have been made and SMF sees all of the attachments since I can browse them in the Admin center. However nobody can seem to use the download links in the forum for the attachments. Am I doing something wrong? Any help would be much appreciated. Thank you  :)

Xylar2

#1
How did you upgrade, with the big upgrade package or the small one?

Arantor

What happens with the download links? Is it that people get an error, or they can't see them or what?

Martin12

Quote from: Xylar2 on November 18, 2009, 03:13:48 AM
How did you upgrade, with the big upgrade package or the small one?
Big.


When someone clicks the download link it just takes them to a 404 page. I am thinking it must be how my forum is linking the link to the downloads. Here is an example /forums/index.php?action=dlattach;topic=2499.0;attach=5602

Arantor

Do uploaded avatars work, out of interest?

Martin12

Quote from: Arantor on November 18, 2009, 03:41:39 PM
Do uploaded avatars work, out of interest?
Yes they work. Also attachments uploaded after the update work just fine

Arantor

So, the update was done in place, meaning you overwrite all the existing files and updated the database... were backups done and did you restore the backup at any point?

Martin12

Quote from: Arantor on November 18, 2009, 05:57:04 PM
So, the update was done in place, meaning you overwrite all the existing files and updated the database... were backups done and did you restore the backup at any point?
[/quote
Yes there are backups and no there was no restore done. There have been a lot of posts made since the backup and I was hoping to fix the problem without doing that if possible.

Arantor

I actually meant just of the files themselves; there are instances where using a backup can damage the files.

Seems to be not uncommon though, and unfortunately I don't have an answer for it :(

Martin12

Quote from: Arantor on November 18, 2009, 06:28:49 PM
I actually meant just of the files themselves; there are instances where using a backup can damage the files.

Seems to be not uncommon though, and unfortunately I don't have an answer for it :(

Thanks for all of your help!!!!


Found a solution....seems to be a problem with the subs.php in the sources folder

The pre update sub had this
Quote// Get an attachment's encrypted filename.  If $new is true, won't check for file existence.
function getAttachmentFilename($filename, $attachment_id, $dir = null, $new = false)
{
   global $modSettings;

   // Remove special accented characters - ie. sí.
   $clean_name = strtr($filename, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
   $clean_name = strtr($clean_name, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));

   // 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;
}

The new sub after the upgrade had this
Quote
// Get an attachment's encrypted filename.  If $new is true, won't check for file existence.
function getAttachmentFilename($filename, $attachment_id, $dir = null, $new = false, $file_hash = '')
{
   global $modSettings, $smcFunc;

   // Just make up a nice hash...
   if ($new)
      return sha1(md5($filename . time()) . mt_rand());

   // Grab the file hash if it wasn't added.
   if ($file_hash === '')
   {
      $request = $smcFunc['db_query']('', '
         SELECT file_hash
         FROM {db_prefix}attachments
         WHERE id_attach = {int:id_attach}',
         array(
            'id_attach' => $attachment_id,
      ));

      if ($smcFunc['db_num_rows']($request) === 0)
         return false;

      list ($file_hash) = $smcFunc['db_fetch_row']($request);
      $smcFunc['db_free_result']($request);
   }

   // 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 = $modSettings['attachmentUploadDir'][$dir];
   }
   else
      $path = $modSettings['attachmentUploadDir'];

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

// Older attachments may still use this function.
function getLegacyAttachmentFilename($filename, $attachment_id, $dir = null, $new = false)
{
   global $modSettings;

   // Remove special accented characters - ie. sí.
   $clean_name = strtr($filename, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
   $clean_name = strtr($clean_name, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));

   // 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;
}

Removing the new code and replacing it with the old code fixed everything. New and old attachments are working

Arantor

Hmm.

Did you have encrypted filenames turned on before or not?

Advertisement: