diff --git a/main/inc/lib/message.lib.php b/main/inc/lib/message.lib.php index d2ebf25088..270b133157 100755 --- a/main/inc/lib/message.lib.php +++ b/main/inc/lib/message.lib.php @@ -325,7 +325,9 @@ class MessageManager $totalFileSize = 0; if (is_array($file_attachments)) { - foreach ($file_attachments as $file_attach) { + $counter = 0; + foreach ($file_attachments as &$file_attach) { + $file_attach['comment'] = isset($file_comments[$counter]) ? $file_comments[$counter] : ''; $fileSize = isset($file_attach['size']) ? $file_attach['size'] : 0; if (is_array($fileSize)) { foreach ($fileSize as $size) { @@ -334,6 +336,7 @@ class MessageManager } else { $totalFileSize += $fileSize; } + $counter++; } } @@ -386,21 +389,39 @@ class MessageManager $messageId = Database::insert($table, $params); } + // Forward also message attachments + if (!empty($forwardId)) { + $attachments = MessageManager::getAttachmentList($forwardId); + foreach ($attachments as $attachment) { + if (!empty($attachment['file_source'])) { + $file = [ + 'name' => $attachment['filename'], + 'tmp_name' => $attachment['file_source'], + 'size' => $attachment['size'], + 'error' => 0, + 'comment' => $attachment['comment'] + ]; + + // Inject this array so files can be added when sending and email with the mailer + $file_attachments[] = $file; + } + } + } + // Save attachment file for inbox messages if (is_array($file_attachments)) { - $i = 0; - foreach ($file_attachments as $file_attach) { + foreach ($file_attachments as &$file_attach) { if ($file_attach['error'] == 0) { + $comment = $file_attach['comment']; self::saveMessageAttachmentFile( $file_attach, - isset($file_comments[$i]) ? $file_comments[$i] : null, + $comment, $messageId, null, $receiver_user_id, $group_id ); } - $i++; } } @@ -421,10 +442,10 @@ class MessageManager // save attachment file for outbox messages if (is_array($file_attachments)) { - $o = 0; - foreach ($file_attachments as $file_attach) { + foreach ($file_attachments as &$file_attach) { if ($file_attach['error'] == 0) { - $comment = isset($file_comments[$o]) ? $file_comments[$o] : ''; + $comment = $file_attach['comment']; + self::saveMessageAttachmentFile( $file_attach, $comment, @@ -432,7 +453,6 @@ class MessageManager $user_sender_id ); } - $o++; } } } @@ -710,7 +730,7 @@ class MessageManager $sender_user_id = 0, $group_id = 0 ) { - $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT); + $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT); // Try to add an extension to the file if it hasn't one $type = isset($file_attach['type']) ? $file_attach['type'] : ''; @@ -750,25 +770,31 @@ class MessageManager } $new_path = $path_message_attach.$new_file_name; - - if (is_uploaded_file($file_attach['tmp_name'])) { - @copy($file_attach['tmp_name'], $new_path); - } else { - // 'tmp_name' can be set by the ticket - if (file_exists($file_attach['tmp_name'])) { + $fileCopied = false; + if (isset($file_attach['tmp_name']) && !empty($file_attach['tmp_name'])) { + if (is_uploaded_file($file_attach['tmp_name'])) { @copy($file_attach['tmp_name'], $new_path); + $fileCopied = true; + } else { + // 'tmp_name' can be set by the ticket or when forwarding a message + if (file_exists($file_attach['tmp_name'])) { + @copy($file_attach['tmp_name'], $new_path); + $fileCopied = true; + } } } - // Storing the attachments if any - $params = [ - 'filename' => $file_name, - 'comment' => $file_comment, - 'path' => $new_file_name, - 'message_id' => $message_id, - 'size' => $file_attach['size'], - ]; - Database::insert($tbl_message_attach, $params); + if ($fileCopied) { + // Storing the attachments if any + $params = [ + 'filename' => $file_name, + 'comment' => $file_comment, + 'path' => $new_file_name, + 'message_id' => $message_id, + 'size' => $file_attach['size'], + ]; + Database::insert($table, $params); + } } } @@ -1183,7 +1209,7 @@ class MessageManager $user_sender_id = $row['user_sender_id']; // get file attachments by message id - $files_attachments = self::get_links_message_attachment_files( + $files_attachments = self::getAttachmentLinkList( $messageId, $source ); @@ -1474,7 +1500,7 @@ class MessageManager $items_page_nr = null; $user_sender_info = api_get_user_info($main_message['user_sender_id']); - $files_attachments = self::get_links_message_attachment_files($main_message['id']); + $files_attachments = self::getAttachmentLinkList($main_message['id']); $name = $user_sender_info['complete_name']; $topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null; @@ -1614,7 +1640,7 @@ class MessageManager $links .= '