Add support for SMF legacy attachment filenames
authorTim Düsterhus <tim@bastelstu.be>
Mon, 9 Sep 2019 18:34:42 +0000 (20:34 +0200)
committerTim Düsterhus <tim@bastelstu.be>
Tue, 10 Sep 2019 10:15:32 +0000 (12:15 +0200)
files/lib/system/exporter/SMF2xExporter.class.php

index e4609a7d6248d8ff3c987b59d54992c9e802bad6..cbed0e9f82a07fa4692f721235296a8775cd0e3c 100644 (file)
@@ -542,7 +542,7 @@ class SMF2xExporter extends AbstractExporter {
                while ($row = $statement->fetchArray()) {
                        switch ($row['type']) {
                                case 'attachment':
-                                       $fileLocation = $this->getAttachmentFilename($row['id_attach'], $row['id_folder'], $row['file_hash']);
+                                       $fileLocation = $this->getAttachmentFilename($row['id_attach'], $row['id_folder'], $row['file_hash'], $row['filename']);
                                break;
                                case 'user':
                                        if (FileUtil::isURL($row['filename'])) return;
@@ -941,7 +941,7 @@ class SMF2xExporter extends AbstractExporter {
                $statement = $this->database->prepareStatement($sql, $limit, $offset);
                $statement->execute([0, 0]);
                while ($row = $statement->fetchArray()) {
-                       $fileLocation = $this->getAttachmentFilename($row['id_attach'], $row['id_folder'], $row['file_hash']);
+                       $fileLocation = $this->getAttachmentFilename($row['id_attach'], $row['id_folder'], $row['file_hash'], $row['filename']);
                        
                        if ($imageSize = @getimagesize($fileLocation)) {
                                $row['isImage'] = 1;
@@ -1428,7 +1428,7 @@ class SMF2xExporter extends AbstractExporter {
                return $message;
        }
        
-       private function getAttachmentFilename($id, $dir, $hash) {
+       private function getAttachmentFilename($id, $dir, $hash, $filename) {
                if (!empty($this->readOption('currentAttachmentUploadDir'))) {
                        // multiple attachments dir
                        static $dirs;
@@ -1447,6 +1447,24 @@ class SMF2xExporter extends AbstractExporter {
                        $path = $this->readOption('attachmentUploadDir');
                }
                
-               return $path . '/' . $id . '_' . $hash;
+               if ($hash) {
+                       return $path . '/' . $id . '_' . $hash;
+               }
+               else {
+                       // sanitize spaces
+                       $filename = preg_replace('/\s/', '_', $filename);
+                       // strip special characters
+                       $filename = preg_replace('/[^\w_\.\-]/', '', $filename);
+                       
+                       $scrambled = $id . '_' . str_replace('.', '_', $filename) . md5($filename);
+                       if (file_exists($path . '/' . $scrambled)) {
+                               return $path . '/' . $scrambled;
+                       }
+                       
+                       // collapsed consecutive dots
+                       $filename = preg_replace('/\.{2,}/', '.', $filename);
+                       
+                       return $path . '/' . $filename;
+               }
        }
 }