/
History.php
193 lines (168 loc) · 4.63 KB
/
History.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
<?php
/**
* Copyright 2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Horde_History storage driver for the IMP_Maillog class.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Maillog_Storage_History extends IMP_Maillog_Storage_Base
{
/**
* History object.
*
* @var Horde_History
*/
protected $_history;
/**
* User name.
*
* @var string
*/
protected $_user;
/**
* Constructor.
*
* @param Horde_History $history History object.
* @param string $user User name.
*/
public function __construct(Horde_History $history, $user)
{
$this->_history = $history;
$this->_user = $user;
}
/**
*/
public function saveLog(
IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
)
{
$data = array(
'action' => $log->action,
'ts' => $log->timestamp
);
switch ($log->action) {
case 'forward':
case 'redirect':
$data['recipients'] = $log->recipients;
break;
}
try {
$this->_history->log($this->_getUniqueHistoryId($msg), $data);
return true;
} catch (Exception $e) {
/* On error, log the error message only since informing the user is
* just a waste of time and a potential point of confusion,
* especially since they most likely don't even know the message
* is being logged. */
Horde::log(
sprintf(
'Could not log message details to Horde_History. Error returned: %s',
$e->getMessage()
),
'ERR'
);
}
return false;
}
/**
*/
public function getLog(IMP_Maillog_Message $msg, array $filter = array())
{
$out = array();
try {
$history = $this->_history->getHistory(
$this->_getUniqueHistoryId($msg)
);
} catch (Exception $e) {
return $out;
}
foreach ($history as $val) {
if (!in_array($val['action'], $filter)) {
switch ($val['action']) {
case 'forward':
$ob = new IMP_Maillog_Log_Forward($val['recipients']);
break;
case 'mdn':
$ob = new IMP_Maillog_Log_Mdn();
break;
case 'redirect':
$ob = new IMP_Maillog_Log_Redirect($val['recipients']);
break;
case 'reply':
$ob = new IMP_Maillog_Log_Reply();
break;
case 'reply_all':
$ob = new IMP_Maillog_Log_Replyall();
break;
case 'reply_list':
$ob = new IMP_Maillog_Log_Replylist();
break;
default:
continue 2;
}
$ob->timestamp = $val['ts'];
$out[] = $ob;
}
}
return $out;
}
/**
*/
public function deleteLogs(array $msgs)
{
$this->_history->removeByNames(array_map(
array($this, '_getUniqueHistoryId'),
$msgs
));
}
/**
*/
public function getChanges($ts)
{
$msgids = preg_replace(
'/^([^:]*:){2}/',
'',
array_keys($this->_history->getByTimestamp(
'>',
$ts,
array(),
$this->_getUniqueHistoryId()
))
);
$out = array();
foreach ($msgids as $val) {
$out[] = new IMP_Maillog_Message($val);
}
return $out;
}
/**
* Generate the unique log ID for an event.
*
* @param mixed $msg An IMP_Maillog_Message object or, if null, return
* the parent ID.
*
* @return string The unique log ID.
*/
protected function _getUniqueHistoryId($msg = null)
{
return implode(':', array_filter(array(
'imp',
str_replace('.', '*', $this->_user),
$msg ? $msg->msgid : null
)));
}
}