Calendar/Tasklist: De-duplicate attachments handling code

This commit is contained in:
Aleksander Machniak 2022-12-21 14:03:22 +01:00
parent a0c564f946
commit e5f57f6160
3 changed files with 126 additions and 135 deletions

View file

@ -2359,20 +2359,9 @@ $("#rcmfd_new_category").keypress(function(event) {
$event['valarms'] = libcalendaring::from_client_alarms($event['valarms']);
}
$attachments = [];
$eventid = 'cal-' . (!empty($event['id']) ? $event['id'] : 'new');
if (!empty($_SESSION[self::SESSION_KEY]) && $_SESSION[self::SESSION_KEY]['id'] == $eventid) {
if (!empty($_SESSION[self::SESSION_KEY]['attachments'])) {
foreach ($_SESSION[self::SESSION_KEY]['attachments'] as $id => $attachment) {
if (!empty($event['attachments']) && in_array($id, $event['attachments'])) {
$attachments[$id] = $this->rc->plugins->exec_hook('attachment_get', $attachment);
}
}
}
}
$event['attachments'] = $attachments;
$eventid = 'cal-' . (!empty($event['id']) ? $event['id'] : 'new');
$handler = new kolab_attachments_handler();
$event['attachments'] = $handler->attachments_set(self::SESSION_KEY, $eventid, $event['attachments'] ?? []);
// convert link references into simple URIs
if (array_key_exists('links', $event)) {
@ -2445,11 +2434,8 @@ $("#rcmfd_new_category").keypress(function(event) {
*/
private function cleanup_event(&$event)
{
// remove temp. attachment files
if (!empty($_SESSION[self::SESSION_KEY]) && ($eventid = $_SESSION[self::SESSION_KEY]['id'])) {
$this->rc->plugins->exec_hook('attachments_cleanup', ['group' => $eventid]);
$this->rc->session->remove(self::SESSION_KEY);
}
$handler = new kolab_attachments_handler();
$handler->attachments_cleanup(self::SESSION_KEY);
}
/**
@ -3805,38 +3791,9 @@ $("#rcmfd_new_category").keypress(function(event) {
$event['links'] = [$msgref];
}
// copy mail attachments to event
else if ($message->attachments) {
$eventid = 'cal-';
if (empty($_SESSION[self::SESSION_KEY]) || $_SESSION[self::SESSION_KEY]['id'] != $eventid) {
$_SESSION[self::SESSION_KEY] = [
'id' => $eventid,
'attachments' => [],
];
}
foreach ((array) $message->attachments as $part) {
$attachment = [
'data' => $imap->get_message_part($uid, $part->mime_id, $part),
'size' => $part->size,
'name' => $part->filename,
'mimetype' => $part->mimetype,
'group' => $eventid,
];
$attachment = $this->rc->plugins->exec_hook('attachment_save', $attachment);
if (!empty($attachment['status']) && !$attachment['abort']) {
$id = $attachment['id'];
$attachment['classname'] = rcube_utils::file2class($attachment['mimetype'], $attachment['name']);
// store new attachment in session
unset($attachment['status'], $attachment['abort'], $attachment['data']);
$_SESSION[self::SESSION_KEY]['attachments'][$id] = $attachment;
$attachment['id'] = 'rcmfile' . $attachment['id']; // add prefix to consider it 'new'
$event['attachments'][] = $attachment;
}
}
else if (!empty($message->attachments) && !empty($this->driver->attachments)) {
$handler = new kolab_attachments_handler();
$event['attachments'] = $handler->copy_mail_attachments(self::SESSION_KEY, 'cal-', $message);
}
$this->rc->output->set_env('event_prop', $event);

View file

@ -38,15 +38,15 @@ class kolab_attachments_handler
$rcmail = rcmail::get_instance();
$self = new self;
$rcmail->output->add_handler('plugin.attachments_form', array($self, 'files_form'));
$rcmail->output->add_handler('plugin.attachments_list', array($self, 'files_list'));
$rcmail->output->add_handler('plugin.filedroparea', array($self, 'files_drop_area'));
$rcmail->output->add_handler('plugin.attachments_form', [$self, 'files_form']);
$rcmail->output->add_handler('plugin.attachments_list', [$self, 'files_list']);
$rcmail->output->add_handler('plugin.filedroparea', [$self, 'files_drop_area']);
}
/**
* Generate HTML element for attachments list
*/
public function files_list($attrib = array())
public function files_list($attrib = [])
{
if (empty($attrib['id'])) {
$attrib['id'] = 'kolabattachmentlist';
@ -64,20 +64,20 @@ class kolab_attachments_handler
/**
* Generate the form for event attachments upload
*/
public function files_form($attrib = array())
public function files_form($attrib = [])
{
// add ID if not given
if (empty($attrib['id'])) {
$attrib['id'] = 'kolabuploadform';
}
return $this->rc->upload_form($attrib, 'uploadform', 'upload-file', array('multiple' => true));
return $this->rc->upload_form($attrib, 'uploadform', 'upload-file', ['multiple' => true]);
}
/**
* Register UI object for HTML5 drag & drop file upload
*/
public function files_drop_area($attrib = array())
public function files_drop_area($attrib = [])
{
// add ID if not given
if (empty($attrib['id'])) {
@ -85,7 +85,7 @@ class kolab_attachments_handler
}
$this->rc->output->add_gui_object('filedrop', $attrib['id']);
$this->rc->output->set_env('filedrop', array('action' => 'upload', 'fieldname' => '_attachments'));
$this->rc->output->set_env('filedrop', ['action' => 'upload', 'fieldname' => '_attachments']);
}
/**
@ -97,8 +97,8 @@ class kolab_attachments_handler
$this->rc->plugins->include_script('libkolab/libkolab.js');
$this->rc->output->add_handler('plugin.attachmentframe', array($this, 'attachment_frame'));
$this->rc->output->add_handler('plugin.attachmentcontrols', array($this, 'attachment_header'));
$this->rc->output->add_handler('plugin.attachmentframe', [$this, 'attachment_frame']);
$this->rc->output->add_handler('plugin.attachmentcontrols', [$this, 'attachment_header']);
$this->rc->output->set_env('filename', $attachment['name']);
$this->rc->output->set_env('mimetype', $attachment['mimetype']);
$this->rc->output->send('libkolab.attachment');
@ -119,9 +119,9 @@ class kolab_attachments_handler
$recid = $id_prefix . ($id ?: 'new');
if (empty($_SESSION[$session_key]) || $_SESSION[$session_key]['id'] != $recid) {
$_SESSION[$session_key] = array();
$_SESSION[$session_key] = [];
$_SESSION[$session_key]['id'] = $recid;
$_SESSION[$session_key]['attachments'] = array();
$_SESSION[$session_key]['attachments'] = [];
}
// clear all stored output properties (like scripts and env vars)
@ -134,13 +134,13 @@ class kolab_attachments_handler
if (!$err) {
$filename = $_FILES['_attachments']['name'][$i];
$attachment = array(
$attachment = [
'path' => $filepath,
'size' => $_FILES['_attachments']['size'][$i],
'name' => $filename,
'mimetype' => rcube_mime::file_content_type($filepath, $filename, $_FILES['_attachments']['type'][$i]),
'group' => $recid,
);
];
$attachment = $this->rc->plugins->exec_hook('attachment_upload', $attachment);
}
@ -156,10 +156,10 @@ class kolab_attachments_handler
&& ($icon = $_SESSION[$session_key . '_deleteicon'])
&& is_file($icon)
) {
$button = html::img(array(
$button = html::img([
'src' => $icon,
'alt' => $this->rc->gettext('delete')
));
]);
}
else if (!empty($_SESSION[$session_key . '_textbuttons'])) {
$button = rcube::Q($this->rc->gettext('delete'));
@ -171,35 +171,35 @@ class kolab_attachments_handler
$link_content = sprintf('<span class="attachment-name">%s</span><span class="attachment-size">(%s)</span>',
rcube::Q($attachment['name']), $this->rc->show_bytes($attachment['size']));
$delete_link = html::a(array(
$delete_link = html::a([
'href' => "#delete",
'class' => 'delete',
'onclick' => sprintf("return %s.remove_from_attachment_list('rcmfile%s')", rcmail_output::JS_OBJECT_NAME, $id),
'title' => $this->rc->gettext('delete'),
'aria-label' => $this->rc->gettext('delete') . ' ' . $attachment['name'],
), $button);
], $button);
$content_link = html::a(array(
$content_link = html::a([
'href' => "#load",
'class' => 'filename',
'onclick' => 'return false', // sprintf("return %s.command('load-attachment','rcmfile%s', this, event)", rcmail_output::JS_OBJECT_NAME, $id),
), $link_content);
], $link_content);
$left = !empty($_SESSION[$session_key . '_icon_pos']) && $_SESSION[$session_key . '_icon_pos'] == 'left';
$content = $left ? $delete_link.$content_link : $content_link.$delete_link;
$this->rc->output->command('add2attachment_list', "rcmfile$id", array(
$this->rc->output->command('add2attachment_list', "rcmfile$id", [
'html' => $content,
'name' => $attachment['name'],
'mimetype' => $attachment['mimetype'],
'classname' => 'no-menu ' . rcube_utils::file2class($attachment['mimetype'], $attachment['name']),
'complete' => true
), $uploadid);
], $uploadid);
}
else { // upload failed
if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
$msg = $this->rc->gettext(array('name' => 'filesizeerror', 'vars' => array(
'size' => $this->rc->show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
$msg = $this->rc->gettext(['name' => 'filesizeerror', 'vars' => [
'size' => $this->rc->show_bytes(parse_bytes(ini_get('upload_max_filesize')))]]);
}
else if (!empty($attachment['error'])) {
$msg = $attachment['error'];
@ -217,8 +217,8 @@ class kolab_attachments_handler
// if filesize exceeds post_max_size then $_FILES array is empty,
// show filesizeerror instead of fileuploaderror
if ($maxsize = ini_get('post_max_size')) {
$msg = $this->rc->gettext(array('name' => 'filesizeerror', 'vars' => array(
'size' => $this->rc->show_bytes(parse_bytes($maxsize)))));
$msg = $this->rc->gettext(['name' => 'filesizeerror', 'vars' => [
'size' => $this->rc->show_bytes(parse_bytes($maxsize))]]);
}
else {
$msg = $this->rc->gettext('fileuploaderror');
@ -247,15 +247,16 @@ class kolab_attachments_handler
$part->size = $attachment['size'];
$part->mimetype = $attachment['mimetype'];
$plugin = $this->rc->plugins->exec_hook('message_part_get', array(
$plugin = $this->rc->plugins->exec_hook('message_part_get', [
'body' => $attachment['body'],
'mimetype' => strtolower($attachment['mimetype']),
'download' => !empty($_GET['_download']),
'part' => $part,
));
]);
if ($plugin['abort'])
if (!empty($plugin['abort'])) {
exit;
}
$mimetype = $plugin['mimetype'];
list($ctype_primary, $ctype_secondary) = explode('/', $mimetype);
@ -289,12 +290,12 @@ class kolab_attachments_handler
$filename = $attachment['name'];
$filename = preg_replace('[\r\n]', '', $filename);
if ($browser->ie && $browser->ver < 7)
$filename = rawurlencode(abbreviate_string($filename, 55));
else if ($browser->ie)
if ($browser->ie) {
$filename = rawurlencode($filename);
else
}
else {
$filename = addcslashes($filename, '"');
}
$disposition = !empty($_GET['_download']) ? 'attachment' : 'inline';
header("Content-Disposition: $disposition; filename=\"$filename\"");
@ -342,7 +343,7 @@ class kolab_attachments_handler
}
/**
*
* Template object for attachment metadata
*/
public function attachment_header($attrib = [])
{
@ -374,4 +375,80 @@ class kolab_attachments_handler
return $table->show($attrib);
}
/**
* Remove uploaded attachments
*/
public function attachments_cleanup($session_key)
{
// remove temp. attachments
if (!empty($_SESSION[$session_key]) && ($group = $_SESSION[$session_key]['id'])) {
$this->rc->plugins->exec_hook('attachments_cleanup', ['group' => $group]);
$this->rc->session->remove($session_key);
}
}
/**
* Collect uploaded attachments
*/
public function attachments_set($session_key, $group, $ids = [])
{
$attachments = [];
if (!empty($_SESSION[$session_key]) && $_SESSION[$session_key]['id'] == $group) {
if (!empty($_SESSION[$session_key]['attachments'])) {
foreach ($_SESSION[$session_key]['attachments'] as $id => $attachment) {
if (is_array($ids) && in_array($id, $ids)) {
$attachments[$id] = $this->rc->plugins->exec_hook('attachment_get', $attachment);
unset($attachments[$id]['abort'], $attachments[$id]['group']);
}
}
}
}
return $attachments;
}
/**
* Register mail message attachments
*/
public function copy_mail_attachments($session_key, $group, $message)
{
$result = [];
$imap = $this->rc->get_storage();
if (!empty($_SESSION[$session_key]) || $_SESSION[$session_key]['id'] != $group) {
$_SESSION[$session_key] = [
'id' => $group,
'attachments' => [],
];
}
foreach ((array) $message->attachments as $part) {
$attachment = [
'data' => $imap->get_message_part($message->uid, $part->mime_id, $part),
'size' => $part->size,
'name' => $part->filename,
'mimetype' => $part->mimetype,
'group' => $group,
];
$attachment = $this->rc->plugins->exec_hook('attachment_save', $attachment);
if (!empty($attachment['status']) && empty($attachment['abort'])) {
$id = $attachment['id'];
$attachment['classname'] = rcube_utils::file2class($attachment['mimetype'], $attachment['name']);
// store new attachment in session
unset($attachment['status'], $attachment['abort'], $attachment['data']);
$_SESSION[$session_key]['attachments'][$id] = $attachment;
$attachment['id'] = 'rcmfile' . $attachment['id']; // add prefix to consider it 'new'
$result[] = $attachment;
}
}
return $result;
}
}

View file

@ -717,20 +717,8 @@ class tasklist extends rcube_plugin
}
}
$attachments = array();
$taskid = $rec['id'];
if (!empty($_SESSION[self::SESSION_KEY]) && $_SESSION[self::SESSION_KEY]['id'] == $taskid) {
if (!empty($_SESSION[self::SESSION_KEY]['attachments'])) {
foreach ($_SESSION[self::SESSION_KEY]['attachments'] as $id => $attachment) {
if (is_array($rec['attachments']) && in_array($id, $rec['attachments'])) {
$attachments[$id] = $this->rc->plugins->exec_hook('attachment_get', $attachment);
unset($attachments[$id]['abort'], $attachments[$id]['group']);
}
}
}
}
$rec['attachments'] = $attachments;
$handler = new kolab_attachments_handler();
$rec['attachments'] = $handler->attachments_set(self::SESSION_KEY, $rec['id'], $rec['attachments'] ?? []);
// convert link references into simple URIs
if (array_key_exists('links', $rec)) {
@ -805,11 +793,8 @@ class tasklist extends rcube_plugin
*/
private function cleanup_task(&$rec)
{
// remove temp. attachment files
if (!empty($_SESSION[self::SESSION_KEY]) && ($taskid = $_SESSION[self::SESSION_KEY]['id'])) {
$this->rc->plugins->exec_hook('attachments_cleanup', array('group' => $taskid));
$this->rc->session->remove(self::SESSION_KEY);
}
$handler = new kolab_attachments_handler();
$handler->attachments_cleanup(self::SESSION_KEY);
}
/**
@ -1794,37 +1779,9 @@ class tasklist extends rcube_plugin
$task['links'] = array($msgref);
}
// copy mail attachments to task
else if ($message->attachments && $this->driver->attachments) {
if (!is_array($_SESSION[self::SESSION_KEY]) || $_SESSION[self::SESSION_KEY]['id'] != $task['id']) {
$_SESSION[self::SESSION_KEY] = array(
'id' => $task['id'],
'attachments' => array(),
);
}
foreach ((array)$message->attachments as $part) {
$attachment = array(
'data' => $imap->get_message_part($uid, $part->mime_id, $part),
'size' => $part->size,
'name' => $part->filename,
'mimetype' => $part->mimetype,
'group' => $task['id'],
);
$attachment = $this->rc->plugins->exec_hook('attachment_save', $attachment);
if ($attachment['status'] && !$attachment['abort']) {
$id = $attachment['id'];
$attachment['classname'] = rcube_utils::file2class($attachment['mimetype'], $attachment['name']);
// store new attachment in session
unset($attachment['status'], $attachment['abort'], $attachment['data']);
$_SESSION[self::SESSION_KEY]['attachments'][$id] = $attachment;
$attachment['id'] = 'rcmfile' . $attachment['id']; // add prefix to consider it 'new'
$task['attachments'][] = $attachment;
}
}
else if (!empty($message->attachments) && $this->driver->attachments) {
$handler = new kolab_attachments_handler();
$task['attachments'] = $handler->copy_mail_attachments(self::SESSION_KEY, $task['id'], $message);
}
$this->rc->output->set_env('task_prop', $task);