From 4bf22d97ff4519ff0ff7e656294226822f6415bd Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Thu, 17 May 2018 11:20:13 +0200 Subject: [PATCH] When forwarding a message, also forward attachment files see BT#14332 --- main/inc/lib/message.lib.php | 159 +++++++++++++++++++++++----------- main/messages/new_message.php | 13 ++- 2 files changed, 120 insertions(+), 52 deletions(-) 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 .= '
'; $html_items = ''; $user_sender_info = api_get_user_info($topic['user_sender_id']); - $files_attachments = self::get_links_message_attachment_files($topic['id']); + $files_attachments = self::getAttachmentLinkList($topic['id']); $name = $user_sender_info['complete_name']; $links .= '
'; @@ -1778,6 +1804,44 @@ class MessageManager return strcmp($array2['send_date'], $array1['send_date']); } + /** + * @param int $messageId + * + * @return array + */ + public static function getAttachmentList($messageId) + { + $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT); + $messageId = (int) $messageId; + + if (empty($messageId)) { + return []; + } + + $messageInfo = MessageManager::get_message_by_id($messageId); + + if (empty($messageInfo)) { + return []; + } + + $attachmentDir = UserManager::getUserPathById($messageInfo['user_receiver_id'], 'system'); + $attachmentDir .= 'message_attachments/'; + + $sql = "SELECT * FROM $table + WHERE message_id = '$messageId'"; + $result = Database::query($sql); + $files = []; + while ($row = Database::fetch_array($result, 'ASSOC')) { + $row['file_source'] = ''; + if (file_exists($attachmentDir.$row['path'])) { + $row['file_source'] = $attachmentDir.$row['path']; + } + $files[] = $row; + } + + return $files; + } + /** * Get array of links (download) for message attachment files. * @@ -1786,34 +1850,27 @@ class MessageManager * * @return array */ - public static function get_links_message_attachment_files($messageId, $type = '') + public static function getAttachmentLinkList($messageId, $type = '') { - $table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT); - $messageId = intval($messageId); - + $files = self::getAttachmentList($messageId); // get file attachments by message id - $links_attach_file = []; - if (!empty($messageId)) { - $sql = "SELECT * FROM $table - WHERE message_id = '$messageId'"; - - $rs_file = Database::query($sql); - if (Database::num_rows($rs_file) > 0) { - $attach_icon = Display::return_icon('attachment.gif', ''); - $archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file='; - while ($row_file = Database::fetch_array($rs_file)) { - $archiveFile = $row_file['path']; - $filename = $row_file['filename']; - $filesize = format_file_size($row_file['size']); - $filecomment = Security::remove_XSS($row_file['comment']); - $filename = Security::remove_XSS($filename); - $links_attach_file[] = $attach_icon.' '.$filename.' -  ('.$filesize.')'.(!empty($filecomment) ? ' - '.$filecomment.'' : ''); - } + $list = []; + if ($files) { + $attach_icon = Display::return_icon('attachment.gif', ''); + $archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file='; + foreach ($files as $row_file) { + $archiveFile = $row_file['path']; + $filename = $row_file['filename']; + $filesize = format_file_size($row_file['size']); + $filecomment = Security::remove_XSS($row_file['comment']); + $filename = Security::remove_XSS($filename); + $list[] = $attach_icon.' '.$filename.' +  ('.$filesize.')'.(!empty($filecomment) ? ' - '.$filecomment.'' : ''); } } - return $links_attach_file; + + return $list; } /** diff --git a/main/messages/new_message.php b/main/messages/new_message.php index 4cfd86f0ba..d8a179a325 100755 --- a/main/messages/new_message.php +++ b/main/messages/new_message.php @@ -186,6 +186,11 @@ function manageForm($default, $select_from_user_list = null, $sent_to = '') if (isset($_GET['forward_id'])) { $forwardId = (int) $_GET['forward_id']; $message_reply_info = MessageManager::get_message_by_id($forwardId); + $attachments = MessageManager::getAttachmentLinkList($forwardId); + if (!empty($attachments)) { + $fileListToString = !empty($attachments) ? implode('
', $attachments) : ''; + $form->addLabel('', $fileListToString); + } $default['title'] = '['.get_lang('MailSubjectForwardShort').": ".$message_reply_info['title'].']'; $form->addHidden('forward_id', $forwardId); $form->addHidden('save_form', 'save_form'); @@ -222,7 +227,12 @@ function manageForm($default, $select_from_user_list = null, $sent_to = '') $form->addLabel( '', - ''.get_lang('AddOneMoreFile').' ('.sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message_max_upload_filesize'))).')' + ''. + get_lang('AddOneMoreFile').' ('. + sprintf( + get_lang('MaximunFileSizeX'), + format_file_size(api_get_setting('message_max_upload_filesize')) + ).')' ); } @@ -268,6 +278,7 @@ function manageForm($default, $select_from_user_list = null, $sent_to = '') false, $forwardId ); + if ($res) { $userInfo = api_get_user_info($userId); Display::addFlash(Display::return_message(