2015-03-24 11:55:32 +01:00
|
|
|
/**
|
|
|
|
* Kolab groupware audit trail utilities
|
|
|
|
*
|
|
|
|
* @author Thomas Bruederli <bruederli@kolabsys.com>
|
|
|
|
*
|
|
|
|
* @licstart The following is the entire license notice for the
|
|
|
|
* JavaScript code in this file.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015, Kolab Systems AG <contact@kolabsys.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @licend The above is the entire license notice
|
|
|
|
* for the JavaScript code in this file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var libkolab_audittrail = {}
|
|
|
|
|
|
|
|
libkolab_audittrail.quote_html = function(str)
|
|
|
|
{
|
|
|
|
return String(str).replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// show object changelog in a dialog
|
|
|
|
libkolab_audittrail.object_history_dialog = function(p)
|
|
|
|
{
|
|
|
|
// render dialog
|
|
|
|
var $dialog = $(p.container);
|
|
|
|
|
|
|
|
// close show dialog first
|
|
|
|
if ($dialog.is(':ui-dialog'))
|
|
|
|
$dialog.dialog('close');
|
|
|
|
|
|
|
|
// hide and reset changelog table
|
|
|
|
$dialog.find('div.notfound-message').remove();
|
|
|
|
$dialog.find('.changelog-table').show().children('tbody')
|
2015-04-20 16:25:48 +02:00
|
|
|
.html('<tr><td colspan="4"><span class="loading">' + rcmail.gettext('loading') + '</span></td></tr>');
|
2015-03-24 11:55:32 +01:00
|
|
|
|
|
|
|
// open jquery UI dialog
|
|
|
|
$dialog.dialog({
|
|
|
|
modal: false,
|
|
|
|
resizable: true,
|
|
|
|
closeOnEscape: true,
|
|
|
|
title: p.title,
|
|
|
|
open: function() {
|
2015-03-26 14:57:16 +01:00
|
|
|
$dialog.attr('aria-hidden', 'false');
|
2015-03-24 11:55:32 +01:00
|
|
|
},
|
|
|
|
close: function() {
|
2015-03-26 14:57:16 +01:00
|
|
|
$dialog.dialog('destroy').attr('aria-hidden', 'true').hide();
|
2015-03-24 11:55:32 +01:00
|
|
|
},
|
2015-03-31 14:53:02 +02:00
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
text: rcmail.gettext('close'),
|
|
|
|
click: function() { $dialog.dialog('close'); },
|
|
|
|
autofocus: true
|
|
|
|
}
|
|
|
|
],
|
2015-03-24 11:55:32 +01:00
|
|
|
minWidth: 450,
|
|
|
|
width: 650,
|
|
|
|
height: 350,
|
2015-04-16 14:50:16 +02:00
|
|
|
minHeight: 200
|
2015-03-24 11:55:32 +01:00
|
|
|
})
|
|
|
|
.show().children('.compare-button').hide();
|
|
|
|
|
|
|
|
// initialize event handlers for history dialog UI elements
|
|
|
|
if (!$dialog.data('initialized')) {
|
|
|
|
// compare button
|
|
|
|
$dialog.find('.compare-button input').click(function(e) {
|
|
|
|
var rev1 = $dialog.find('.changelog-table input.diff-rev1:checked').val(),
|
|
|
|
rev2 = $dialog.find('.changelog-table input.diff-rev2:checked').val();
|
|
|
|
|
|
|
|
if (rev1 && rev2 && rev1 != rev2) {
|
|
|
|
// swap revisions if the user got it wrong
|
|
|
|
if (rev1 > rev2) {
|
|
|
|
var tmp = rev2;
|
|
|
|
rev2 = rev1;
|
|
|
|
rev1 = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.comparefunc) {
|
|
|
|
p.comparefunc(rev1, rev2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert('Invalid selection!')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rcube_event.is_keyboard(e) && this.blur) {
|
|
|
|
this.blur();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// delegate handlers for list actions
|
|
|
|
$dialog.find('.changelog-table tbody').on('click', 'td.actions a', function(e) {
|
|
|
|
var link = $(this),
|
|
|
|
action = link.hasClass('restore') ? 'restore' : 'show',
|
|
|
|
event = $('#eventhistory').data('event'),
|
|
|
|
rev = link.attr('data-rev');
|
|
|
|
|
|
|
|
// ignore clicks on first row (current revision)
|
|
|
|
if (link.closest('tr').hasClass('first')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let the user confirm the restore action
|
|
|
|
if (action == 'restore' && !confirm(rcmail.gettext('revisionrestoreconfirm', p.module).replace('$rev', rev))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.listfunc) {
|
|
|
|
p.listfunc(action, rev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rcube_event.is_keyboard(e) && this.blur) {
|
|
|
|
this.blur();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.on('click', 'input.diff-rev1', function(e) {
|
|
|
|
if (!this.checked) return true;
|
|
|
|
|
|
|
|
var rev1 = this.value, selection_valid = false;
|
|
|
|
$dialog.find('.changelog-table input.diff-rev2').each(function(i, elem) {
|
|
|
|
$(elem).prop('disabled', elem.value <= rev1);
|
|
|
|
if (elem.checked && elem.value > rev1) {
|
|
|
|
selection_valid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!selection_valid) {
|
|
|
|
$dialog.find('.changelog-table input.diff-rev2:not([disabled])').last().prop('checked', true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$dialog.addClass('changelog-dialog').data('initialized', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dialog;
|
|
|
|
};
|
|
|
|
|
|
|
|
// callback from server with changelog data
|
|
|
|
libkolab_audittrail.render_changelog = function(data, object, folder)
|
|
|
|
{
|
|
|
|
var Q = libkolab_audittrail.quote_html;
|
|
|
|
|
|
|
|
var $dialog = $('.changelog-dialog')
|
|
|
|
if (data === false || !data.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i, change, accessible, op_append,
|
|
|
|
first = data.length - 1, last = 0,
|
|
|
|
is_writeable = !!folder.editable,
|
2015-04-20 17:20:39 +02:00
|
|
|
op_labels = {
|
|
|
|
RECEIVE: 'actionreceive',
|
|
|
|
APPEND: 'actionappend',
|
|
|
|
MOVE: 'actionmove',
|
|
|
|
DELETE: 'actiondelete',
|
|
|
|
READ: 'actionread',
|
|
|
|
FLAGSET: 'actionflagset',
|
|
|
|
FLAGCLEAR: 'actionflagclear'
|
|
|
|
},
|
|
|
|
actions = '<a href="#show" class="iconbutton preview" title="'+ rcmail.gettext('showrevision','libkolab') +'" data-rev="{rev}" /> ' +
|
|
|
|
(is_writeable ? '<a href="#restore" class="iconbutton restore" title="'+ rcmail.gettext('restore','libkolab') + '" data-rev="{rev}" />' : ''),
|
2015-03-24 11:55:32 +01:00
|
|
|
tbody = $dialog.find('.changelog-table tbody').html('');
|
|
|
|
|
|
|
|
for (i=first; i >= 0; i--) {
|
|
|
|
change = data[i];
|
|
|
|
accessible = change.date && change.user;
|
|
|
|
|
|
|
|
if (change.op == 'MOVE' && change.mailbox) {
|
|
|
|
op_append = ' ⇢ ' + change.mailbox;
|
|
|
|
}
|
2015-04-20 16:25:48 +02:00
|
|
|
else if ((change.op == 'FLAGSET' || change.op == 'FLAGCLEAR') && change.flags) {
|
|
|
|
op_append = ': ' + change.flags;
|
|
|
|
}
|
2015-03-24 11:55:32 +01:00
|
|
|
else {
|
|
|
|
op_append = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('<tr class="' + (i == first ? 'first' : (i == last ? 'last' : '')) + (accessible ? '' : 'undisclosed') + '">')
|
|
|
|
.append('<td class="diff">' + (accessible && change.op != 'DELETE' ?
|
|
|
|
'<input type="radio" name="rev1" class="diff-rev1" value="' + change.rev + '" title="" '+ (i == last ? 'checked="checked"' : '') +' /> '+
|
|
|
|
'<input type="radio" name="rev2" class="diff-rev2" value="' + change.rev + '" title="" '+ (i == first ? 'checked="checked"' : '') +' /></td>'
|
|
|
|
: ''))
|
|
|
|
.append('<td class="revision">' + Q(i+1) + '</td>')
|
|
|
|
.append('<td class="date">' + Q(change.date || '') + '</td>')
|
|
|
|
.append('<td class="user">' + Q(change.user || 'undisclosed') + '</td>')
|
2015-04-20 17:20:39 +02:00
|
|
|
.append('<td class="operation" title="' + op_append + '">' + Q(rcmail.gettext(op_labels[change.op] || '', 'libkolab') + op_append) + '</td>')
|
2015-03-24 11:55:32 +01:00
|
|
|
.append('<td class="actions">' + (accessible && change.op != 'DELETE' ? actions.replace(/\{rev\}/g, change.rev) : '') + '</td>')
|
|
|
|
.appendTo(tbody);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first > 0) {
|
|
|
|
$dialog.find('.compare-button').fadeIn(200);
|
|
|
|
$dialog.find('.changelog-table tr.last input.diff-rev1').click();
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:05:43 +02:00
|
|
|
// set dialog size according to content
|
|
|
|
libkolab_audittrail.dialog_resize($dialog.get(0), $dialog.height() + 15, 600);
|
|
|
|
|
2015-03-24 11:55:32 +01:00
|
|
|
return $dialog;
|
2015-04-16 19:05:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// resize and reposition (center) the dialog window
|
|
|
|
libkolab_audittrail.dialog_resize = function(id, height, width)
|
|
|
|
{
|
|
|
|
var win = $(window), w = win.width(), h = win.height();
|
2016-08-15 03:11:29 -04:00
|
|
|
$(id).dialog('option', { height: Math.min(h-20, height+130), width: Math.min(w-20, width+50) });
|
2015-04-16 19:05:43 +02:00
|
|
|
};
|
2015-04-20 16:25:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
// register handlers for mail message history
|
|
|
|
window.rcmail && rcmail.addEventListener('init', function(e) {
|
|
|
|
var loading_lock;
|
|
|
|
|
|
|
|
if (rcmail.env.task == 'mail') {
|
|
|
|
rcmail.register_command('kolab-mail-history', function() {
|
|
|
|
var dialog, uid = rcmail.get_single_uid(), rec = { uid: uid, mbox: rcmail.get_message_mailbox(uid) };
|
|
|
|
if (!uid || !window.libkolab_audittrail) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// render dialog
|
|
|
|
$dialog = libkolab_audittrail.object_history_dialog({
|
|
|
|
module: 'libkolab',
|
|
|
|
container: '#mailmessagehistory',
|
|
|
|
title: rcmail.gettext('objectchangelog','libkolab')
|
|
|
|
});
|
|
|
|
|
|
|
|
$dialog.data('rec', rec);
|
|
|
|
|
|
|
|
// fetch changelog data
|
|
|
|
loading_lock = rcmail.set_busy(true, 'loading', loading_lock);
|
|
|
|
rcmail.http_post('plugin.message-changelog', { _uid: rec.uid, _mbox: rec.mbox }, loading_lock);
|
|
|
|
|
|
|
|
}, rcmail.env.action == 'show');
|
|
|
|
|
|
|
|
rcmail.addEventListener('plugin.message_render_changelog', function(data) {
|
|
|
|
var $dialog = $('#mailmessagehistory'),
|
|
|
|
rec = $dialog.data('rec');
|
|
|
|
|
2015-04-21 09:24:20 +02:00
|
|
|
if (data === false || !data.length || !rec) {
|
2015-04-20 16:25:48 +02:00
|
|
|
// display 'unavailable' message
|
|
|
|
$('<div class="notfound-message dialog-message warning">' + rcmail.gettext('objectchangelognotavailable','libkolab') + '</div>')
|
|
|
|
.insertBefore($dialog.find('.changelog-table').hide());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.module = 'libkolab';
|
|
|
|
libkolab_audittrail.render_changelog(data, rec, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
rcmail.env.message_commands.push('kolab-mail-history');
|
|
|
|
}
|
|
|
|
});
|