Files
Kelx/grommunio-web-5.2.73.g1be61255c/server/includes/download_attachment.php
T
2026-11-30 16:32:37 +00:00

1116 lines
39 KiB
PHP

<?php
/*
* SPDX-FileCopyrightText: Copyright 2020 - 2026 grommunio GmbH
* SPDX-FileCopyrightText: Copyright 2016 Kopano and its licensors
* SPDX-FileCopyrightText: Copyright 2005 - 2016 Zarafa B.V. and its licensors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
// required to handle php errors
require_once __DIR__ . '/exceptions/class.GrommunioErrorException.php';
require_once __DIR__ . '/download_base.php';
require_once __DIR__ . '/exceptions/class.ImportError.php';
/**
* DownloadAttachment.
*
* A class to manage downloading of attachments from message, additionally
* this class can be used to download inline images from message as well.
*
* Main reason to create this class is to not pollute the global namespace.
*/
class DownloadAttachment extends DownloadBase {
/**
* Content disposition type for the attachment that will be sent with header with the attachment data
* Possible values are 'inline' and 'attachment'. When content-type is application/octet-stream and
* content disposition type is 'attachment' then browser will show dialog to save attachment as instead of
* directly displaying content inline.
*/
private $contentDispositionType;
/**
* Attachment number of the attachment that should be downloaded. For normal attachments this will contain
* a single element array with numeric value as sequential attachment number, for attachments that are not saved
* in AttachmentTable of MAPIMessage yet (recently uploaded attachments) this will give single element array
* having value as a string in form of 'filename randomstring'. When accessing embedded messages this array can contain
* multiple elements indicating attachment numbers at each level, So value [0, 1] will indicate we want to download
* second attachment of first embedded message.
*/
private $attachNum;
/**
* Attachment Content Id is used to download inline images of the MAPIMessage, When requesting inline images only
* content id is passed but if we are accessing inline image from embedded message then besides content id,
* attachment number is also passed to access embedded message.
*/
private $attachCid;
/**
* Attachment numbers a ZIP request is restricted to. Empty means every attachment of the message,
* which is what 'AllAsZip' has always done; a non-empty list narrows the archive to exactly those
* attachments, which is how a selection of several is downloaded as one file.
*/
private $selectedAttachNum;
/**
* A string that will be initialized with grommunio Web-specific and common-for-all file name for ZIP file.
*/
private $zipFileName;
/**
* A random string that will be generated with every MAPIMessage instance to uniquely identify attachments that
* belongs to this MAPIMessage, this is mainly used to get recently uploaded attachments for MAPIMessage.
*/
private $dialogAttachments;
/**
* A boolean value, set to false by default, to define if the message, of which the attachments are required to be wrapped in ZIP,
* is a sub message of other webapp item or not.
*/
private $isSubMessage;
/**
* Entryid of the MAPIFolder to which the given attachment needs to be imported as webapp item.
*/
private $destinationFolderId;
/**
* Resource of the MAPIFolder to which the given attachment needs to be imported as webapp item.
*/
private $destinationFolder;
/**
* A boolean value, set to false by default, to define if the attachment needs to be imported into folder as webapp item.
*/
private $import;
/**
* A boolean value, set to false by default, to define if the embedded attachment needs to be imported into folder.
*/
private $isEmbedded;
/**
* Resource of the shared MAPIStore into which attachments needs to be imported.
*/
private $otherStore;
private $messageSubject;
/**
* Constructor.
*/
public function __construct() {
$this->contentDispositionType = 'attachment';
$this->attachNum = [];
$this->selectedAttachNum = [];
$this->attachCid = false;
$this->zipFileName = _('Attachments') . '%s.zip';
$this->messageSubject = '';
$this->isSubMessage = false;
$this->destinationFolderId = false;
$this->destinationFolder = false;
$this->import = false;
$this->isEmbedded = false;
$this->otherStore = false;
parent::__construct();
}
/**
* Function will initialize data for this class object. it will also sanitize data
* for possible XSS attack because data is received in $_GET.
*
* @param mixed $data
*/
#[Override]
public function init($data) {
if (isset($data['store'])) {
$this->store = sanitizeValue($data['store'], '', ID_REGEX);
}
if (isset($data['entryid'])) {
$this->entryId = sanitizeValue($data['entryid'], '', ID_REGEX);
}
if (isset($data['contentDispositionType'])) {
$this->contentDispositionType = in_array($data['contentDispositionType'], ['inline', 'attachment'], true)
? $data['contentDispositionType']
: 'attachment';
}
if (!empty($data['attachNum'])) {
/**
* if you are opening an already saved attachment then $data["attachNum"]
* will contain array of numeric index for that attachment (like 0 or 1 or 2).
*
* if you are opening a recently uploaded attachment then $data["attachNum"]
* will be a one element array and it will contain a string in "filename.randomstring" format
* like README.txtu6K6AH
*/
foreach ($data['attachNum'] as $attachNum) {
$num = sanitizeValue($attachNum, false, NUMERIC_REGEX);
if ($num === false) {
// string is passed in attachNum so get it
$num = sanitizeValue($attachNum, '', FILENAME_REGEX);
if (!empty($num)) {
array_push($this->attachNum, $num);
}
}
else {
array_push($this->attachNum, (int) $num);
}
}
}
if (!empty($data['selectedAttachNum']) && is_array($data['selectedAttachNum'])) {
/*
* Restricts an 'AllAsZip' request to the named attachments. Only numeric attachment
* numbers are accepted: an unsaved attachment of a draft is identified by a temporary
* name rather than a number and cannot be part of such a selection.
*/
foreach ($data['selectedAttachNum'] as $attachNum) {
$num = sanitizeValue($attachNum, false, NUMERIC_REGEX);
if ($num !== false) {
array_push($this->selectedAttachNum, (int) $num);
}
}
}
if (isset($data['attachCid'])) {
$this->attachCid = (string) $data['attachCid'];
}
if (isset($data['AllAsZip'])) {
$this->allAsZip = sanitizeValue($data['AllAsZip'], '', STRING_REGEX);
}
if (isset($data['subject'])) {
// Remove characters that we cannot use in a filename
$data['subject'] = preg_replace('/[^a-z0-9 ()]/mi', '_', $data['subject']);
$this->messageSubject = sanitizeValue($data['subject'], '', FILENAME_REGEX);
}
if ($this->allAsZip && isset($data['isSubMessage'])) {
$this->isSubMessage = sanitizeValue($data['isSubMessage'], '', STRING_REGEX);
}
if (isset($data['dialog_attachments'])) {
$this->dialogAttachments = sanitizeValue($data['dialog_attachments'], '', STRING_REGEX);
}
if ($this->store && $this->entryId) {
$this->store = $GLOBALS['mapisession']->openMessageStore(hex2bin((string) $this->store));
$this->message = mapi_msgstore_openentry($this->store, hex2bin((string) $this->entryId));
// Decode smime signed messages on this message
parse_smime($this->store, $this->message);
}
if (isset($data['destination_folder'])) {
$this->destinationFolderId = sanitizeValue($data['destination_folder'], '', ID_REGEX);
if ($this->destinationFolder === false) {
try {
$this->destinationFolder = mapi_msgstore_openentry($this->store, hex2bin((string) $this->destinationFolderId));
}
catch (Exception) {
// Try to find the folder from shared stores in case if it is not found in current user's store
$this->otherStore = $GLOBALS['operations']->getOtherStoreFromEntryid($this->destinationFolderId);
if ($this->otherStore !== false) {
$this->destinationFolder = mapi_msgstore_openentry($this->otherStore, hex2bin((string) $this->destinationFolderId));
}
else {
$this->destinationFolder = mapi_msgstore_openentry($GLOBALS["mapisession"]->getPublicMessageStore(), hex2bin((string) $this->destinationFolderId));
if (!$this->destinationFolder) {
throw new GrommunioException(_("Destination folder not found."));
}
}
}
}
}
if (isset($data['import'])) {
$this->import = sanitizeValue($data['import'], '', STRING_REGEX);
}
if (isset($data['is_embedded'])) {
$this->isEmbedded = sanitizeValue($data['is_embedded'], '', STRING_REGEX);
}
}
/**
* Returns inline image attachment based on specified attachCid, To get inline image attachment
* we need to compare passed attachCid with PR_ATTACH_CONTENT_ID, PR_ATTACH_CONTENT_LOCATION or
* PR_ATTACH_FILENAME and if that matches then we can get that attachment.
*
* @param false|resource $attachment embedded message attachment from which to get the inline image
*
* @return false|resource requested attachment, or false when it cannot be opened
*/
public function getAttachmentByAttachCid($attachment = false) {
// If the inline image was in a submessage, we have to open that first
if ($attachment !== false) {
$this->message = openAttachedMessage($attachment);
if ($this->message === false) {
return false;
}
}
$attachment = false;
// the body may reference a content id percent-encoded (RFC 2392) or literally
$cids = [$this->attachCid];
if (rawurldecode((string) $this->attachCid) !== $this->attachCid) {
$cids[] = rawurldecode((string) $this->attachCid);
}
$clauses = [];
foreach ($cids as $cid) {
foreach ([PR_ATTACH_CONTENT_ID, PR_ATTACH_CONTENT_LOCATION, PR_ATTACH_FILENAME, PR_ATTACH_LONG_FILENAME] as $tag) {
$clauses[] = [RES_CONTENT,
[
FUZZYLEVEL => FL_FULLSTRING | FL_IGNORECASE,
ULPROPTAG => $tag,
VALUE => [$tag => $cid],
],
];
}
}
$restriction = [RES_OR, $clauses];
// Get the attachment table
$attachTable = mapi_message_getattachmenttable($this->message);
$attachments = mapi_table_queryallrows($attachTable, [PR_ATTACH_NUM], $restriction);
if (count($attachments) > 0) {
// there should be only one attachment
$attachment = mapi_message_openattach($this->message, $attachments[0][PR_ATTACH_NUM]);
}
return $attachment;
}
/**
* Returns attachment based on specified attachNum, additionally it will also get embedded message
* if we want to get the inline image attachment.
*
* @return false|resource embedded message attachment or requested attachment, or false on failure
*/
public function getAttachmentByAttachNum() {
$len = count($this->attachNum);
// Loop through the attachNums, message in message in message ...
for ($index = 0; $index < $len - 1; ++$index) {
// Open the attachment
$tempattach = mapi_message_openattach($this->message, $this->attachNum[$index]);
if ($tempattach === false) {
return false;
}
// Open the object in the attachment
$this->message = openAttachedMessage($tempattach);
if ($this->message === false) {
return false;
}
}
// open the attachment
return mapi_message_openattach($this->message, $this->attachNum[$len - 1]);
}
/**
* Function will set the first and last bytes of a range, with a range
* specified as string and size of the attachment.
*
* If $first is greater than $last, the request returns 416 (not satisfiable)
* If no end of range is specified or larger than the length, $last is set as end
* If no beginning of range is specified, get last x bytes of attachment
*
* @param mixed $range
* @param mixed $filesize
* @param mixed $first
* @param mixed $last
*/
public function downloadSetRange($range, $filesize, &$first, &$last) {
$dash = strpos((string) $range, '-');
$first = trim(substr((string) $range, 0, $dash));
$last = trim(substr((string) $range, $dash + 1));
if ($first == '') {
// suffix byte range: gets last x bytes
$suffix = $last;
$last = $filesize - 1;
$first = $filesize - $suffix;
if ($first < 0) {
$first = 0;
}
}
elseif ($last == '' || $last > $filesize - 1) {
$last = $filesize - 1;
}
if ($first > $last) {
http_response_code(416);
header("Status: 416 Requested range not satisfiable");
header("Content-Range: */{$filesize}");
}
}
/**
* Function will output $bytes of attachment stream with $buffer_size read ahead.
*
* @param mixed $stream
* @param mixed $bytes
* @param mixed $buffer_size
*/
public function downloadBufferedRead($stream, $bytes, $buffer_size = 1024) {
$bytes_left = $bytes;
while ($bytes_left > 0) {
$bytes_to_read = min($buffer_size, $bytes_left);
$bytes_left -= $bytes_to_read;
$contents = mapi_stream_read($stream, $bytes_to_read);
if ($contents === false) {
error_log('Unable to read attachment stream');
break;
}
echo $contents;
flush();
}
}
/**
* Function will open passed attachment and generate response for that attachment to send it to client.
* This should only be used to download attachment that is already saved in MAPIMessage.
*
* @param false|resource $attachment attachment to send to the client, or false if unavailable
* @param bool $inline inline attachment or not
*/
public function downloadSavedAttachment($attachment, $inline = false) {
// Check if the attachment is opened
if ($attachment !== false) {
// Get the props of the attachment
$props = mapi_attach_getprops($attachment, [PR_ATTACH_FILENAME, PR_ATTACH_LONG_FILENAME, PR_ATTACH_MIME_TAG, PR_DISPLAY_NAME, PR_ATTACH_METHOD, PR_ATTACH_CONTENT_ID]);
// Content Type
$contentType = 'application/octet-stream';
// Filename
$filename = 'ERROR';
// Set filename
if ($inline) {
/*
* Gromox preserves the original filename of inline
* attachments, so prefer it. KGWC stores the placeholder
* "inline.txt" instead (see inetmapi/VMIMEToMAPI.cpp); in
* that case, or without any stored filename, fall back to
* CONTENT_ID plus mime tag.
*/
$filename = $props[PR_ATTACH_LONG_FILENAME] ?? $props[PR_ATTACH_FILENAME] ?? '';
if ($filename === '' || strcasecmp($filename, 'inline.txt') === 0) {
$tags = explode('/', (string) ($props[PR_ATTACH_MIME_TAG] ?? ''));
// IE 11 is weird, when a user renames the file it's not saved as in image, when
// the filename is "test.jpeg", but it works when it's "test.jpg".
$filename = (($props[PR_ATTACH_CONTENT_ID] ?? '') ?: 'inline') . '.' . str_replace('jpeg', 'jpg', $tags[1] ?? 'bin');
}
}
elseif (isset($props[PR_ATTACH_LONG_FILENAME])) {
$filename = $props[PR_ATTACH_LONG_FILENAME];
}
elseif (isset($props[PR_ATTACH_FILENAME])) {
$filename = $props[PR_ATTACH_FILENAME];
}
elseif (isset($props[PR_DISPLAY_NAME])) {
$filename = $props[PR_DISPLAY_NAME];
}
// Set content type if available, otherwise it will be default to application/octet-stream
if (isset($props[PR_ATTACH_MIME_TAG])) {
$contentType = normalizeHTTPContentType($props[PR_ATTACH_MIME_TAG]);
}
$embedded = ($props[PR_ATTACH_METHOD] ?? null) == ATTACH_EMBEDDED_MSG;
if ($embedded) {
$filename = self::emlFileName($filename);
$contentType = 'message/rfc822';
}
// Open the stream before sending headers so a missing
// or empty PR_ATTACH_DATA_BIN can be handled cleanly
// without leaving the browser connection hanging.
try {
$stream = $embedded ? $this->openEmbeddedAsEml($attachment) : mapi_openproperty($attachment, PR_ATTACH_DATA_BIN, IID_IStream, 0, 0);
$stat = mapi_stream_stat($stream);
$bodysize = $stat['cb'] ?? 0;
}
catch (MAPIException $e) {
$e->setHandled();
$stream = false;
$bodysize = 0;
}
// Set the headers
header('Pragma: public');
if ($inline) {
// The URL embeds message entryid and content id; an hour bounds
// the exposure of a cached copy after logout or a draft edit.
header('Cache-Control: private, max-age=3600');
}
else {
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
}
$contentDisposition = getDownloadContentDisposition($this->contentDispositionType, $contentType);
sendDownloadSecurityHeaders();
header('Content-Disposition: ' . $contentDisposition . '; filename="' . addslashes(browserDependingHTTPHeaderEncode($filename)) . '"');
header('Content-Type: ' . $contentType);
header('Content-Transfer-Encoding: binary');
$bodyoffset = 0;
$ranges = null;
$first = 0;
$last = 0;
if ($stream !== false && $bodysize > 0 && $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_SERVER['HTTP_RANGE']) && $range = stristr(trim((string) $_SERVER['HTTP_RANGE']), 'bytes=')) {
$range = substr($range, 6);
$ranges = explode(',', $range);
}
if ($ranges && count($ranges)) {
http_response_code(206);
header("Accept-Ranges: bytes");
if (count($ranges) > 1) {
// More than one range specified
$boundary = bin2hex(random_bytes(48));
$content_length = 0;
foreach ($ranges as $range) {
$this->downloadSetRange($range, $bodysize, $first, $last);
if ($first > $last) {
return;
}
$content_length += strlen("\r\n--{$boundary}\r\n");
$content_length += strlen("Content-Type: {$contentType}\r\n");
$content_length += strlen("Content-Range: bytes {$first}-{$last}/{$bodysize}\r\n\r\n");
$content_length += $last - $first + 1;
}
$content_length += strlen("\r\n--{$boundary}--\r\n");
// Header output
header("Content-Length: {$content_length}");
header("Content-Type: multipart/x-byteranges; boundary={$boundary}");
// Content output
foreach ($ranges as $range) {
$this->downloadSetRange($range, $bodysize, $first, $last);
echo "\r\n--{$boundary}\r\n";
echo "Content-Type: {$contentType}\r\n";
echo "Content-Range: bytes {$first}-{$last}/{$bodysize}\r\n\r\n";
mapi_stream_seek($stream, $first + $bodyoffset);
$this->downloadBufferedRead($stream, $last - $first + 1);
}
echo "\r\n--{$boundary}--\r\n";
}
else {
// Single range specified
$range = $ranges[0];
$this->downloadSetRange($range, $bodysize, $first, $last);
if ($first > $last) {
return;
}
header("Content-Length: " . ($last - $first + 1));
header("Content-Range: bytes {$first}-{$last}/{$bodysize}");
header("Content-Type: {$contentType}");
mapi_stream_seek($stream, $first + $bodyoffset);
$this->downloadBufferedRead($stream, $last - $first + 1);
}
}
else {
header('Content-Length: ' . $bodysize);
if ($stream !== false && $bodysize > 0) {
// Read the attachment content from the stream
$body = '';
for ($i = 0; $i < $bodysize; $i += BLOCK_SIZE) {
$body .= mapi_stream_read($stream, BLOCK_SIZE);
}
echo $body;
}
}
}
}
/**
* An embedded message as RFC822 mail, its attachment has no data of its own.
*
* @param resource $attachment
*
* @return resource
*/
private function openEmbeddedAsEml($attachment) {
return mapi_inetmapi_imtoinet($GLOBALS['mapisession']->getSession(), $GLOBALS['mapisession']->getAddressbook(), mapi_attach_openobj($attachment), []);
}
/**
* @param string $name
*
* @return string the name with an .eml extension
*/
private static function emlFileName($name) {
// a subject may hold path separators that would nest the archive entry
$name = strtr($name, '/\\', '__');
return str_ends_with(strtolower($name), '.eml') ? $name : $name . '.eml';
}
/**
* Helper function to configure header information which is required to send response as a ZIP archive
* containing all the attachments.
*
* @param string $randomZipName a random zip archive name
*/
public function sendZipResponse($randomZipName) {
$subject = isset($this->messageSubject) ? ' ' . $this->messageSubject : '';
// Set the headers
$contentType = 'application/zip';
$contentDisposition = getDownloadContentDisposition($this->contentDispositionType, $contentType);
sendDownloadSecurityHeaders();
header('Pragma: public');
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Disposition: ' . $contentDisposition . '; filename="' . addslashes(browserDependingHTTPHeaderEncode(sprintf($this->zipFileName, $subject))) . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Type: ' . $contentType);
header('Content-Length: ' . filesize($randomZipName));
// Send the actual response as ZIP file
readfile($randomZipName);
// Remove the zip file to avoid unnecessary disk-space consumption
unlink($randomZipName);
}
/**
* Function will open all attachments of message and prepare a ZIP file response for that attachment to send it to client.
* This should only be used to download attachment that is already saved in MAPIMessage.
*
* @param AttachmentState $attachment_state object of AttachmentState class
* @param ZipArchive $zip zipArchive object
*/
public function addAttachmentsToZipArchive($attachment_state, $zip) {
// Get all the attachments from message
$attachmentTable = mapi_message_getattachmenttable($this->message);
$attachments = mapi_table_queryallrows($attachmentTable, [PR_ATTACH_NUM, PR_ATTACH_METHOD, PR_ATTACHMENT_HIDDEN]);
$isSelection = !empty($this->selectedAttachNum);
foreach ($attachments as $attachmentRow) {
$embedded = ($attachmentRow[PR_ATTACH_METHOD] ?? null) == ATTACH_EMBEDDED_MSG;
// a hidden embedded message is a recurrence exception, not a file
if ($embedded && !empty($attachmentRow[PR_ATTACHMENT_HIDDEN])) {
continue;
}
// A selection narrows the archive to the attachments it names; without one
// every attachment of the message goes in, as it always has.
if ($isSelection &&
!in_array((int) $attachmentRow[PR_ATTACH_NUM], $this->selectedAttachNum, true)) {
continue;
}
$attachment = mapi_message_openattach($this->message, $attachmentRow[PR_ATTACH_NUM]);
// Keep inline attachments and contact photos out of an archive of everything
// only: this test is wider than the one the attachment list hides by.
if ($isSelection || (!$attachment_state->isInlineAttachment($attachment) && !$attachment_state->isContactPhoto($attachment))) {
$props = mapi_attach_getprops($attachment, [PR_ATTACH_LONG_FILENAME, PR_DISPLAY_NAME]);
$name = $props[PR_ATTACH_LONG_FILENAME] ?? $props[PR_DISPLAY_NAME] ?? _('Untitled');
// Open a stream to get the attachment data
if ($embedded) {
$stream = $this->openEmbeddedAsEml($attachment);
$name = self::emlFileName($name);
}
else {
$stream = mapi_openproperty($attachment, PR_ATTACH_DATA_BIN, IID_IStream, 0, 0);
}
$stat = mapi_stream_stat($stream);
// Get the stream
$datastring = '';
for ($i = 0; $i < $stat['cb']; $i += BLOCK_SIZE) {
$datastring .= mapi_stream_read($stream, BLOCK_SIZE);
}
// Add file into zip by stream
$fileDownloadName = $this->handleDuplicateFileNames($name);
$zip->addFromString($fileDownloadName, $datastring);
}
}
// Go for adding unsaved attachments in ZIP, if any.
// This situation arise while user upload attachments in draft.
// A selection names attachments by number, which an unsaved one does not have,
// so adding them would put files into the archive that were never selected.
if (!$isSelection) {
$attachmentFiles = $attachment_state->getAttachmentFiles($this->dialogAttachments);
if ($attachmentFiles) {
$this->addUnsavedAttachmentsToZipArchive($attachment_state, $zip);
}
}
}
/**
* Function will send attachment data to client side.
* This should only be used to download attachment that is recently uploaded and not saved in MAPIMessage.
*/
public function downloadUnsavedAttachment() {
// return recently uploaded file
$attachment_state = new AttachmentState();
$attachment_state->open();
// there will be only one value in attachNum so directly access 0th element of it
$tmpname = $attachment_state->getAttachmentPath($this->attachNum[0]);
$fileinfo = $attachment_state->getAttachmentFile($this->dialogAttachments, $this->attachNum[0]);
// Check if the file still exists
if (is_file($tmpname)) {
$contentType = function_exists('mime_content_type') ? mime_content_type($tmpname) : false;
$contentType = normalizeHTTPContentType($contentType);
$contentDisposition = getDownloadContentDisposition($this->contentDispositionType, $contentType);
// Set the headers
sendDownloadSecurityHeaders();
header('Pragma: public');
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Disposition: ' . $contentDisposition . '; filename="' . addslashes(browserDependingHTTPHeaderEncode($fileinfo['name'])) . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Type: ' . $contentType);
header('Content-Length: ' . filesize($tmpname));
// Open the uploaded file and print it
$file = fopen($tmpname, 'r');
fpassthru($file);
fclose($file);
}
elseif ($fileinfo['sourcetype'] === 'icsfile') {
// When "Send to" option used with calendar item. which create the new mail with
// ics file as an attachment and now user try to download the ics attachment before saving
// mail at that time this code is used to download the ics file successfully.
$messageStore = $GLOBALS['mapisession']->openMessageStore(hex2bin((string) $fileinfo['store_entryid']));
$message = mapi_msgstore_openentry($messageStore, hex2bin((string) $fileinfo['entryid']));
// Get address book for current session
$addrBook = $GLOBALS['mapisession']->getAddressbook();
// get message properties.
$messageProps = mapi_getprops($message, [PR_SUBJECT]);
// Read the appointment as RFC2445-formatted ics stream.
$appointmentStream = mapi_mapitoical($GLOBALS['mapisession']->getSession(), $addrBook, $message, []);
if ($appointmentStream === false) {
throw new RuntimeException('Unable to convert appointment');
}
$filename = (!empty($messageProps[PR_SUBJECT])) ? $messageProps[PR_SUBJECT] : _('Untitled');
$filename .= '.ics';
$contentType = 'application/octet-stream';
$contentDisposition = getDownloadContentDisposition($this->contentDispositionType, $contentType);
// Set the headers
sendDownloadSecurityHeaders();
header('Pragma: public');
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
// Set Content Disposition header
header('Content-Disposition: ' . $contentDisposition . '; filename="' . addslashes(browserDependingHTTPHeaderEncode($filename)) . '"');
// Set content type header
header('Content-Type: ' . $contentType);
// Set the file length
header('Content-Length: ' . strlen($appointmentStream));
$split = str_split($appointmentStream, BLOCK_SIZE);
foreach ($split as $s) {
echo $s;
}
}
$attachment_state->close();
}
/**
* Function will send all the attachments to client side wrapped in a ZIP file.
* This should only be used to download all the attachments that are recently uploaded and not saved in MAPIMessage.
*
* @param AttachmentState $attachment_state object of AttachmentState class
* @param ZipArchive $zip zipArchive object
*/
public function addUnsavedAttachmentsToZipArchive($attachment_state, $zip) {
// Get recently uploaded attachment files
$attachmentFiles = $attachment_state->getAttachmentFiles($this->dialogAttachments);
foreach ($attachmentFiles as $fileName => $fileInfo) {
$filePath = $attachment_state->getAttachmentPath($fileName);
// Avoid including contact photo and embedded messages in ZIP
if ($fileInfo['sourcetype'] !== 'embedded' && $fileInfo['sourcetype'] !== 'contactphoto') {
$fileDownloadName = $this->handleDuplicateFileNames($fileInfo['name']);
$zip->addFile($filePath, $fileDownloadName);
}
}
}
/**
* Function will get the attachment and import it to the given MAPIFolder as webapp item.
*/
public function importAttachment() {
$attachment = $this->getAttachmentByAttachNum();
if ($attachment === false) {
throw new GrommunioException(_("Could not find attachment."));
}
$addrBook = $GLOBALS['mapisession']->getAddressbook();
$newMessage = null;
$attachmentProps = mapi_attach_getprops($attachment, [PR_ATTACH_LONG_FILENAME]);
$attachmentStream = readMapiPropStream($attachment, PR_ATTACH_DATA_BIN);
$extension = strtolower((string) pathinfo((string) $attachmentProps[PR_ATTACH_LONG_FILENAME], PATHINFO_EXTENSION));
$ok = false;
switch ($extension) {
case 'eml':
if (isBrokenEml($attachmentStream)) {
throw new GrommunioException(_("Eml is corrupted"));
}
$newMessage = mapi_folder_createmessage($this->destinationFolder);
try {
// Convert an RFC822-formatted e-mail to a MAPI Message
$ok = mapi_inetmapi_imtomapi($GLOBALS['mapisession']->getSession(), $this->store, $addrBook, $newMessage, $attachmentStream, []);
}
catch (Exception) {
throw new GrommunioException(_("The eml Attachment is not imported successfully"));
}
break;
case 'vcf':
try {
if (function_exists('mapi_vcftomapi2')) {
// a file may hold several vCards
$contacts = mapi_vcftomapi2($this->destinationFolder, $attachmentStream) ?: [];
foreach ($contacts as $contact) {
mapi_savechanges($contact);
}
$ok = !empty($contacts);
}
else {
$newMessage = mapi_folder_createmessage($this->destinationFolder);
$ok = mapi_vcftomapi($GLOBALS['mapisession']->getSession(), $this->store, $newMessage, $attachmentStream);
}
}
catch (Exception) {
throw new GrommunioException(_("The vcf attachment is not imported successfully"));
}
break;
case 'vcs':
case 'ics':
$ok = $this->importEvents($addrBook, $attachmentStream, $attachmentProps[PR_ATTACH_LONG_FILENAME]);
break;
}
if ($ok === true) {
if ($newMessage !== null) {
mapi_savechanges($newMessage);
}
$storeProps = mapi_getprops($this->store, [PR_ENTRYID]);
$destinationFolderProps = mapi_getprops($this->destinationFolder, [PR_PARENT_ENTRYID, PR_CONTENT_UNREAD]);
$return = [
// 'success' property is needed for Extjs Ext.form.Action.Submit#success handler
'success' => true,
'grommunio' => [
sanitizeGetValue('module', '', STRING_REGEX) => [
sanitizeGetValue('moduleid', '', STRING_REGEX) => [
'update' => [
'success' => true,
],
],
],
],
];
// send hierarchy notification only in case of 'eml'
if ($extension === 'eml') {
$hierarchynotifier = [
'hierarchynotifier1' => [
'folders' => [
'item' => [
0 => [
'entryid' => $this->destinationFolderId,
'parent_entryid' => bin2hex((string) $destinationFolderProps[PR_PARENT_ENTRYID]),
'store_entryid' => bin2hex((string) $storeProps[PR_ENTRYID]),
'props' => [
'content_unread' => $destinationFolderProps[PR_CONTENT_UNREAD] + 1,
],
],
],
],
],
];
$return['grommunio']['hierarchynotifier'] = $hierarchynotifier;
}
echo json_encode($return);
}
else {
throw new GrommunioException(_("Attachment is not imported successfully"));
}
}
/**
* Saves every event of a calendar file into the destination folder,
* turning meeting requests into appointments.
*
* @param mixed $addrBook
* @param string $stream the calendar file
* @param string $filename
*
* @return bool true when at least one event was imported
*/
private function importEvents($addrBook, $stream, $filename) {
try {
// the same check the upload import makes before sending a file
if (!preg_match('/VCALENDAR(\r\n|\n|\r)/i', $stream)) {
throw new Exception('', MAPI_E_INVALID_PARAMETER);
}
// Convert vCalendar 1.0 or iCalendar to MAPI Appointments
$events = mapi_icaltomapi2($addrBook, $this->destinationFolder, $stream) ?: [];
}
catch (Exception $e) {
throw ImportError::fromException($e, $filename, $this->destinationFolder, fn () => $this->otherStore, _("There is no appointment found in this file."));
}
foreach ($events as $event) {
mapi_savechanges($event);
$props = mapi_getprops($event, [PR_MESSAGE_CLASS]);
if (str_starts_with((string) ($props[PR_MESSAGE_CLASS] ?? ''), 'IPM.Schedule.Meeting.')) {
$req = new Meetingrequest($this->store, $event, $GLOBALS['mapisession']->getSession(), ENABLE_DIRECT_BOOKING);
$req->doAccept(true, false, false, false, false, false, false, false, false, true);
}
}
return !empty($events);
}
/**
* Function will get the embedded attachment and import it to the given MAPIFolder as webapp item.
*/
public function importEmbeddedAttachment() {
// get message props of sub message
$copyFromMessage = $GLOBALS['operations']->openMessage($this->store, hex2bin((string) $this->entryId), $this->attachNum, true);
if (empty($copyFromMessage)) {
throw new GrommunioException(_("Embedded attachment not found."));
}
$newMessage = mapi_folder_createmessage($this->destinationFolder);
// Copy the entire message
mapi_copyto($copyFromMessage, [], [], $newMessage);
mapi_savechanges($newMessage);
$storeProps = mapi_getprops($this->store, [PR_ENTRYID]);
$destinationFolderProps = mapi_getprops($this->destinationFolder, [PR_PARENT_ENTRYID, PR_CONTENT_UNREAD]);
$return = [
// 'success' property is needed for Extjs Ext.form.Action.Submit#success handler
'success' => true,
'grommunio' => [
sanitizeGetValue('module', '', STRING_REGEX) => [
sanitizeGetValue('moduleid', '', STRING_REGEX) => [
'update' => [
'success' => true,
],
],
],
'hierarchynotifier' => [
'hierarchynotifier1' => [
'folders' => [
'item' => [
0 => [
'entryid' => $this->destinationFolderId,
'parent_entryid' => bin2hex((string) $destinationFolderProps[PR_PARENT_ENTRYID]),
'store_entryid' => bin2hex((string) $storeProps[PR_ENTRYID]),
'props' => [
'content_unread' => $destinationFolderProps[PR_CONTENT_UNREAD] + 1,
],
],
],
],
],
],
],
];
echo json_encode($return);
}
/**
* Check if the attached eml is corrupted or not.
*
* @param string $attachment content fetched from PR_ATTACH_DATA_BIN property of an attachment
*
* @return bool true if eml is broken, false otherwise
*/
public function isBroken($attachment) {
// Get header part to process further
$splittedContent = preg_split("/\r?\n\r?\n/", $attachment);
// Fetch raw header
if (preg_match_all('/([^:]+): ?.*\n/', $splittedContent[0], $matches)) {
$rawHeaders = $matches[1];
}
// Compare if necessary headers are present or not
if (isset($rawHeaders) && in_array('From', $rawHeaders) && in_array('Date', $rawHeaders)) {
return false;
}
return true;
}
/**
* Generic function to check passed data and decide which type of attachment is requested.
*/
public function download() {
$attachment = false;
// Check if all attachments are requested to be downloaded as ZIP
if ($this->allAsZip) {
$attachment_state = new AttachmentState();
$attachment_state->open();
// Generate random ZIP file name at default temporary path of PHP
$randomZipName = tempnam(sys_get_temp_dir(), 'zip');
// Create an open zip archive.
$zip = new ZipArchive();
$result = $zip->open($randomZipName, ZipArchive::OVERWRITE);
if ($result === true) {
// Check if attachments are of saved message.
// Only saved message has the entryid configured.
if ($this->entryId) {
// Check if the requested attachment(s) are of an embedded message
if ($this->isSubMessage) {
// Loop through the attachNums, message in message in message ...
for ($index = 0, $len = count($this->attachNum); $index < $len - 1; ++$index) {
// Open the attachment
$tempattach = mapi_message_openattach($this->message, $this->attachNum[$index]);
if ($tempattach) {
// Open the object in the attachment
$this->message = openAttachedMessage($tempattach);
}
}
}
$this->addAttachmentsToZipArchive($attachment_state, $zip);
}
else {
$this->addUnsavedAttachmentsToZipArchive($attachment_state, $zip);
}
}
else {
// Throw exception if ZIP is not created successfully
throw new GrommunioException(_("ZIP is not created successfully"));
}
// A selection that matched nothing must not become an empty archive on disk.
if (!empty($this->selectedAttachNum) && $zip->numFiles === 0) {
$zip->close();
if (is_file($randomZipName) && !@unlink($randomZipName)) {
error_log('Unable to remove empty attachment archive: ' . $randomZipName);
}
throw new GrommunioException(_("ZIP is not created successfully"));
}
$zip->close();
$attachment_state->close();
$this->sendZipResponse($randomZipName);
// check if inline image is requested
}
elseif ($this->attachCid) {
$attachment = false;
// check if the inline image is in a embedded message
if (count($this->attachNum) > 0) {
// get the embedded message attachment
$attachment = $this->getAttachmentByAttachNum();
if ($attachment === false) {
return;
}
}
// now get the actual attachment object that should be sent back to client
$attachment = $this->getAttachmentByAttachCid($attachment);
// no need to return anything here function will echo all the output
$this->downloadSavedAttachment($attachment, true);
}
elseif (count($this->attachNum) > 0) {
// check if the attachment needs to be imported
if ($this->import) {
if ($this->isEmbedded) {
$this->importEmbeddedAttachment();
}
else {
$this->importAttachment();
}
return;
}
// check if temporary unsaved attachment is requested
if (is_string($this->attachNum[0])) {
$this->downloadUnsavedAttachment();
}
else {
// normal saved attachment is requested, so get it
$attachment = $this->getAttachmentByAttachNum();
if ($attachment === false) {
// something terrible happened and we can't continue
return;
}
// no need to return anything here function will echo all the output
$this->downloadSavedAttachment($attachment);
}
}
else {
throw new GrommunioException(_("Attachments can not be downloaded"));
}
}
#[Override]
protected function notFoundMessage() {
return _('Could not find attachment.');
}
}
// create instance of class to download attachment
$attachInstance = new DownloadAttachment();
try {
// initialize variables
$attachInstance->init($_GET);
// download attachment
$attachInstance->download();
}
catch (Exception $e) {
$attachInstance->handleSaveMessageException($e);
}