forked from Combodo/combodo-email-synchro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
319 lines (300 loc) · 10.9 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
// Copyright (C) 2013 Combodo SARL
//
// This file is part of iTop.
//
// iTop 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.
//
// iTop 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 iTop. If not, see <http://www.gnu.org/licenses/>
/**
* Processing of AJAX calls for the CalendarView
*
* @copyright Copyright (C) 2013 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
require_once('../../approot.inc.php');
require_once(APPROOT.'/application/application.inc.php');
require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
/**
* @param \ajax_page $oPage
* @param \MailInboxBase $oInbox
*
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MissingQueryArgument
* @throws \MySQLException
* @throws \MySQLHasGoneAwayException
* @throws \OQLException
*/
function GetMailboxContent($oPage, $oInbox)
{
if(is_object($oInbox))
{
$iStartIndex = utils::ReadParam('start', 0);
$iMaxCount = utils::ReadParam('count', 10);
$iMsgCount = 0;
try
{
/** @var \EmailSource $oSource */
$oSource = $oInbox->GetEmailSource();
$iTotalMsgCount = $oSource->GetMessagesCount();
$iStart = min($iStartIndex, $iTotalMsgCount);
$iEnd = min($iStart + $iMaxCount, $iTotalMsgCount);
$iMsgCount = $iEnd - $iStart;
$aMessages = $oSource->GetListing();
}
catch(Exception $e)
{
$oPage->p("Failed to initialize the mailbox: ".$oInbox->GetName().". Reason: ".$e->getMessage());
return;
}
if ($iMsgCount > 0)
{
// Get the corresponding EmailReplica object for each message
$aUIDLs = array();
for($iMessage = 0; $iMessage < $iTotalMsgCount; $iMessage++)
{
// Assume that EmailBackgroundProcess::IsMultiSourceMode() is always set to true
$aUIDLs[] = $oSource->GetName().'_'.$aMessages[$iMessage]['uidl'];
}
$sOQL = 'SELECT EmailReplica WHERE uidl IN ('.implode(',', CMDBSource::Quote($aUIDLs)).') AND mailbox_path = ' . CMDBSource::Quote($oInbox->Get('mailbox'));
$oReplicaSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
$oReplicaSet->OptimizeColumnLoad(array('EmailReplica' => array('uidl', 'ticket_id', 'status', 'error_message')));
$iProcessedCount = $oReplicaSet->Count();
$aProcessed = array();
while($oReplica = $oReplicaSet->Fetch())
{
$aProcessed[$oReplica->Get('uidl')] = array(
'status' => $oReplica->Get('status'),
'ticket_id' => $oReplica->Get('ticket_id'),
'error_message' => $oReplica->Get('error_message'),
'id' => $oReplica->GetKey(),
);
}
$aTableConfig = array(
'checkbox' => array('label' => '<input type="checkbox" id="mailbox_checkall"/>', 'description' => ''),
'status' => array('label' => Dict::S('MailInbox:Status'), 'description' => ''),
'date' => array('label' => Dict::S('MailInbox:Date'), 'description' => ''),
'from' => array('label' => Dict::S('MailInbox:From'), 'description' => ''),
'subject' => array('label' => Dict::S('MailInbox:Subject'), 'description' => ''),
'ticket' => array('label' => Dict::S('MailInbox:RelatedTicket'), 'description' => ''),
'error' => array('label' => Dict::S('MailInbox:ErrorMessage'), 'description' => ''),
'details' => array('label' => Dict::S('MailInbox:MessageDetails'), 'description' => ''),
);
$aData = array();
for($iMessage = $iStart; $iMessage < $iStart+$iMsgCount; $iMessage++)
{
$oRawEmail = $oSource->GetMessage($iMessage);
$oEmail = $oRawEmail->Decode($oSource->GetPartsOrder());
// Assume that EmailBackgroundProcess::IsMultiSourceMode() is always set to true
$sUIDLs = $oSource->GetName().'_'.$aMessages[$iMessage]['uidl'];
$sStatus = Dict::S('MailInbox:Status/New');
$sLink = '';
$sErrorMsg = '';
$sDetailsLink = '';
if (array_key_exists($sUIDLs, $aProcessed))
{
switch ($aProcessed[$sUIDLs]['status'])
{
case 'ok':
$sStatus = Dict::S('MailInbox:Status/Processed');
break;
case 'error':
$sStatus = Dict::S('MailInbox:Status/Error');
break;
case 'undesired':
$sStatus = Dict::S('MailInbox:Status/Undesired');
break;
case 'ignored':
$sStatus = Dict::S('MailInbox:Status/Ignored');
break;
}
$sErrorMsg = $aProcessed[$sUIDLs]['error_message'];
if ($aProcessed[$sUIDLs]['ticket_id'] != '')
{
$sTicketUrl = ApplicationContext::MakeObjectUrl($oInbox->Get('target_class'), $aProcessed[$sUIDLs]['ticket_id']);
$sLink = '<a href="'.$sTicketUrl.'">'.$oInbox->Get('target_class').'::'.$aProcessed[$sUIDLs]['ticket_id'].'</a>';
}
$aArgs = array('operation' => 'message_details', 'sUIDL' => $sUIDLs);
$sDetailsURL = utils::GetAbsoluteUrlModulePage(basename(dirname(__FILE__)), 'details.php', $aArgs);
$sDetailsLink = '<a href="'.$sDetailsURL.'">'.Dict::S('MailInbox:MessageDetails').'</a>';
}
$aData[] = array(
'checkbox' => '<input type="checkbox" class="mailbox_item" value="'.htmlentities($sUIDLs, ENT_QUOTES, 'UTF-8').'"/>',
'status' => $sStatus,
'date' => $oEmail->sDate,
'from' => $oEmail->sCallerEmail,
'subject' => $oEmail->sSubject,
'ticket' => $sLink,
'error' => $sErrorMsg,
'details' => $sDetailsLink,
);
}
$oPage->p(Dict::Format('MailInbox:Z_DisplayedThereAre_X_Msg_Y_NewInTheMailbox', $iMsgCount, $iTotalMsgCount, ($iTotalMsgCount - $iProcessedCount)));
$oPage->table($aTableConfig, $aData);
$oPage->add('<div><img alt="" src="../images/tv-item-last.gif" style="vertical-align:bottom;margin-left:10px;"/> '.Dict::S('MailInbox:WithSelectedDo').'  <button class="mailbox_button ibo-button ibo-is-regular ibo-is-neutral" id="mailbox_reset_status">'.Dict::S('MailInbox:ResetStatus').'</button> <button class="mailbox_button ibo-button ibo-is-regular ibo-is-danger" id="mailbox_delete_messages">'.Dict::S('MailInbox:DeleteMessage').'</button> <button class="mailbox_button ibo-button ibo-is-regular ibo-is-neutral" id="mailbox_ignore_messages">'.Dict::S('MailInbox:IgnoreMessage').'</button></div>');
}
else
{
$oPage->p(Dict::Format('MailInbox:EmptyMailbox'));
}
}
else
{
$oPage->P(Dict::S('UI:ObjectDoesNotExist'));
}
}
/**
* Finds the index of the message with the given UIDL identifier
* @param array $aMessages The array returned by $oSource->GetListing()
* @param string $sUIDL The UIDL to find
* @param EmailSource $oSource
* @return integer The index of the message or false if not found
*/
function FindMessageIDFromUIDL($aMessages, $sUIDL, EmailSource $oSource)
{
$sKey = $sUIDL;
$sMultiSourceKey = substr($sUIDL, 1+strlen($oSource->GetName())); // in Multisource mode the name of the source plus _ are prepended to the UIDL
foreach($aMessages as $aData)
{
if ((strcmp($sKey, $aData['uidl']) == 0) || (strcmp($sMultiSourceKey, $aData['uidl']) == 0))
{
return $aData['msg_id'] - 1; // return a zero based index
}
}
return false;
}
try
{
require_once(APPROOT.'/application/cmdbabstract.class.inc.php');
require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
$oPage = new ajax_page('');
} else {
$oPage = new AjaxPage('');
}
$sOperation = utils::ReadParam('operation', '');
$iMailInboxId = utils::ReadParam('id', 0, false, 'raw_data');
if (empty($iMailInboxId))
{
$oInbox = null;
}
else
{
/** @var MailInboxBase $oInbox */
$oInbox = MetaModel::GetObject('MailInboxBase', $iMailInboxId, false);
}
switch($sOperation)
{
case 'mailbox_content':
GetMailboxContent($oPage, $oInbox);
break;
case 'mailbox_reset_status':
case 'mailbox_delete_messages':
$aUIDLs = utils::ReadParam('aUIDLs', array(), false, 'raw_data');
if (count($aUIDLs) > 0)
{
$sOQL = 'SELECT EmailReplica WHERE uidl IN ('.implode(',', CMDBSource::Quote($aUIDLs)).')';
$oReplicaSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
$oReplicaSet->OptimizeColumnLoad(array('EmailReplica' => array('uidl')));
$aReplicas = array();
while($oReplica = $oReplicaSet->Fetch())
{
$aReplicas[$oReplica->Get('uidl')] = $oReplica;
}
if ($sOperation == 'mailbox_delete_messages')
{
// Delete the actual email from the mailbox
$oSource = $oInbox->GetEmailSource();
$aMessages = $oSource->GetListing();
}
foreach($aUIDLs as $sUIDL)
{
if(array_key_exists($sUIDL, $aReplicas))
{
// A replica exists for the given email, let's remove it
$aReplicas[$sUIDL]->DBDelete();
}
if ($sOperation == 'mailbox_delete_messages')
{
$idx = FindMessageIDFromUIDL($aMessages, $sUIDL, $oSource);
if ($idx !== false)
{
// Delete the actual email from the mailbox
$oSource->DeleteMessage($idx);
}
}
}
if ($sOperation == 'mailbox_delete_messages')
{
$oSource->Disconnect();
}
}
GetMailboxContent($oPage, $oInbox);
break;
case 'mailbox_ignore_messages':
$aUIDLs = utils::ReadParam('aUIDLs', array(), false, 'raw_data');
if (count($aUIDLs) > 0)
{
$sOQL = 'SELECT EmailReplica WHERE uidl IN ('.implode(',', CMDBSource::Quote($aUIDLs)).')';
$oReplicaSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL));
$aReplicas = array();
/** @var \DBObject $oReplica */
while ($oReplica = $oReplicaSet->Fetch())
{
$oReplica->Set('status', 'ignored');
$oReplica->DBUpdate();
$aReplicas[$oReplica->Get('uidl')] = true;
}
}
if (count($aReplicas) < count($aUIDLs))
{
// Some "New" messages are marked as ignore
// Add them to the database
$oSource = $oInbox->GetEmailSource();
$aMessages = $oSource->GetListing();
foreach ($aUIDLs as $sUIDL)
{
if (isset($aReplicas[$sUIDL]))
{
// Already processed
continue;
}
$oEmailReplica = new EmailReplica();
$oEmailReplica->Set('uidl', $sUIDL);
$oEmailReplica->Set('status', 'ignored');
$oEmailReplica->Set('mailbox_path', $oSource->GetMailbox());
foreach ($aMessages as $iMessage => $aMessage)
{
if ($oSource->GetName().'_'.$aMessage['uidl'] == $sUIDL)
{
$oEmailReplica->Set('message_id', $iMessage);
$oEmailReplica->DBInsert();
break;
}
}
}
}
GetMailboxContent($oPage, $oInbox);
break;
}
$oPage->output();
}
catch(Exception $e)
{
$oPage->SetContentType('text/html');
$oPage->add($e->getMessage());
$oPage->output();
}