When forwarding a message, also forward attachment files see BT#14332

pull/2525/head
jmontoyaa 8 years ago
parent 6fbc7b1d00
commit 4bf22d97ff
  1. 159
      main/inc/lib/message.lib.php
  2. 13
      main/messages/new_message.php

@ -325,7 +325,9 @@ class MessageManager
$totalFileSize = 0; $totalFileSize = 0;
if (is_array($file_attachments)) { 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; $fileSize = isset($file_attach['size']) ? $file_attach['size'] : 0;
if (is_array($fileSize)) { if (is_array($fileSize)) {
foreach ($fileSize as $size) { foreach ($fileSize as $size) {
@ -334,6 +336,7 @@ class MessageManager
} else { } else {
$totalFileSize += $fileSize; $totalFileSize += $fileSize;
} }
$counter++;
} }
} }
@ -386,21 +389,39 @@ class MessageManager
$messageId = Database::insert($table, $params); $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 // Save attachment file for inbox messages
if (is_array($file_attachments)) { if (is_array($file_attachments)) {
$i = 0; foreach ($file_attachments as &$file_attach) {
foreach ($file_attachments as $file_attach) {
if ($file_attach['error'] == 0) { if ($file_attach['error'] == 0) {
$comment = $file_attach['comment'];
self::saveMessageAttachmentFile( self::saveMessageAttachmentFile(
$file_attach, $file_attach,
isset($file_comments[$i]) ? $file_comments[$i] : null, $comment,
$messageId, $messageId,
null, null,
$receiver_user_id, $receiver_user_id,
$group_id $group_id
); );
} }
$i++;
} }
} }
@ -421,10 +442,10 @@ class MessageManager
// save attachment file for outbox messages // save attachment file for outbox messages
if (is_array($file_attachments)) { if (is_array($file_attachments)) {
$o = 0; foreach ($file_attachments as &$file_attach) {
foreach ($file_attachments as $file_attach) {
if ($file_attach['error'] == 0) { if ($file_attach['error'] == 0) {
$comment = isset($file_comments[$o]) ? $file_comments[$o] : ''; $comment = $file_attach['comment'];
self::saveMessageAttachmentFile( self::saveMessageAttachmentFile(
$file_attach, $file_attach,
$comment, $comment,
@ -432,7 +453,6 @@ class MessageManager
$user_sender_id $user_sender_id
); );
} }
$o++;
} }
} }
} }
@ -710,7 +730,7 @@ class MessageManager
$sender_user_id = 0, $sender_user_id = 0,
$group_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 // Try to add an extension to the file if it hasn't one
$type = isset($file_attach['type']) ? $file_attach['type'] : ''; $type = isset($file_attach['type']) ? $file_attach['type'] : '';
@ -750,25 +770,31 @@ class MessageManager
} }
$new_path = $path_message_attach.$new_file_name; $new_path = $path_message_attach.$new_file_name;
$fileCopied = false;
if (is_uploaded_file($file_attach['tmp_name'])) { if (isset($file_attach['tmp_name']) && !empty($file_attach['tmp_name'])) {
@copy($file_attach['tmp_name'], $new_path); if (is_uploaded_file($file_attach['tmp_name'])) {
} else {
// 'tmp_name' can be set by the ticket
if (file_exists($file_attach['tmp_name'])) {
@copy($file_attach['tmp_name'], $new_path); @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 if ($fileCopied) {
$params = [ // Storing the attachments if any
'filename' => $file_name, $params = [
'comment' => $file_comment, 'filename' => $file_name,
'path' => $new_file_name, 'comment' => $file_comment,
'message_id' => $message_id, 'path' => $new_file_name,
'size' => $file_attach['size'], 'message_id' => $message_id,
]; 'size' => $file_attach['size'],
Database::insert($tbl_message_attach, $params); ];
Database::insert($table, $params);
}
} }
} }
@ -1183,7 +1209,7 @@ class MessageManager
$user_sender_id = $row['user_sender_id']; $user_sender_id = $row['user_sender_id'];
// get file attachments by message id // get file attachments by message id
$files_attachments = self::get_links_message_attachment_files( $files_attachments = self::getAttachmentLinkList(
$messageId, $messageId,
$source $source
); );
@ -1474,7 +1500,7 @@ class MessageManager
$items_page_nr = null; $items_page_nr = null;
$user_sender_info = api_get_user_info($main_message['user_sender_id']); $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']; $name = $user_sender_info['complete_name'];
$topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null; $topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null;
@ -1614,7 +1640,7 @@ class MessageManager
$links .= '<div class="pull-right">'; $links .= '<div class="pull-right">';
$html_items = ''; $html_items = '';
$user_sender_info = api_get_user_info($topic['user_sender_id']); $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']; $name = $user_sender_info['complete_name'];
$links .= '<div class="btn-group btn-group-sm">'; $links .= '<div class="btn-group btn-group-sm">';
@ -1778,6 +1804,44 @@ class MessageManager
return strcmp($array2['send_date'], $array1['send_date']); 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. * Get array of links (download) for message attachment files.
* *
@ -1786,34 +1850,27 @@ class MessageManager
* *
* @return array * @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); $files = self::getAttachmentList($messageId);
$messageId = intval($messageId);
// get file attachments by message id // get file attachments by message id
$links_attach_file = []; $list = [];
if (!empty($messageId)) { if ($files) {
$sql = "SELECT * FROM $table $attach_icon = Display::return_icon('attachment.gif', '');
WHERE message_id = '$messageId'"; $archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file=';
foreach ($files as $row_file) {
$rs_file = Database::query($sql); $archiveFile = $row_file['path'];
if (Database::num_rows($rs_file) > 0) { $filename = $row_file['filename'];
$attach_icon = Display::return_icon('attachment.gif', ''); $filesize = format_file_size($row_file['size']);
$archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file='; $filecomment = Security::remove_XSS($row_file['comment']);
while ($row_file = Database::fetch_array($rs_file)) { $filename = Security::remove_XSS($filename);
$archiveFile = $row_file['path']; $list[] = $attach_icon.'&nbsp;<a href="'.$archiveURL.$archiveFile.'">'.$filename.'</a>
$filename = $row_file['filename']; &nbsp;('.$filesize.')'.(!empty($filecomment) ? '&nbsp;-&nbsp;<i>'.$filecomment.'</i>' : '');
$filesize = format_file_size($row_file['size']);
$filecomment = Security::remove_XSS($row_file['comment']);
$filename = Security::remove_XSS($filename);
$links_attach_file[] = $attach_icon.'&nbsp;<a href="'.$archiveURL.$archiveFile.'">'.$filename.'</a>
&nbsp;('.$filesize.')'.(!empty($filecomment) ? '&nbsp;-&nbsp;<i>'.$filecomment.'</i>' : '');
}
} }
} }
return $links_attach_file;
return $list;
} }
/** /**

@ -186,6 +186,11 @@ function manageForm($default, $select_from_user_list = null, $sent_to = '')
if (isset($_GET['forward_id'])) { if (isset($_GET['forward_id'])) {
$forwardId = (int) $_GET['forward_id']; $forwardId = (int) $_GET['forward_id'];
$message_reply_info = MessageManager::get_message_by_id($forwardId); $message_reply_info = MessageManager::get_message_by_id($forwardId);
$attachments = MessageManager::getAttachmentLinkList($forwardId);
if (!empty($attachments)) {
$fileListToString = !empty($attachments) ? implode('<br />', $attachments) : '';
$form->addLabel('', $fileListToString);
}
$default['title'] = '['.get_lang('MailSubjectForwardShort').": ".$message_reply_info['title'].']'; $default['title'] = '['.get_lang('MailSubjectForwardShort').": ".$message_reply_info['title'].']';
$form->addHidden('forward_id', $forwardId); $form->addHidden('forward_id', $forwardId);
$form->addHidden('save_form', 'save_form'); $form->addHidden('save_form', 'save_form');
@ -222,7 +227,12 @@ function manageForm($default, $select_from_user_list = null, $sent_to = '')
$form->addLabel( $form->addLabel(
'', '',
'<span id="link-more-attach"><a href="javascript://" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</a></span>&nbsp;('.sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message_max_upload_filesize'))).')' '<span id="link-more-attach"><a href="javascript://" onclick="return add_image_form()">'.
get_lang('AddOneMoreFile').'</a></span>&nbsp;('.
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, false,
$forwardId $forwardId
); );
if ($res) { if ($res) {
$userInfo = api_get_user_info($userId); $userInfo = api_get_user_info($userId);
Display::addFlash(Display::return_message( Display::addFlash(Display::return_message(

Loading…
Cancel
Save