1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class IMP_Maillog_Storage_History extends IMP_Maillog_Storage_Base
24: {
25: 26: 27: 28: 29:
30: protected $_history;
31:
32: 33: 34: 35: 36:
37: protected $_user;
38:
39: 40: 41: 42: 43: 44:
45: public function __construct(Horde_History $history, $user)
46: {
47: $this->_history = $history;
48: $this->_user = $user;
49: }
50:
51: 52:
53: public function saveLog(
54: IMP_Maillog_Message $msg, IMP_Maillog_Log_Base $log
55: )
56: {
57: $data = array(
58: 'action' => $log->action,
59: 'ts' => $log->timestamp
60: );
61:
62: switch ($log->action) {
63: case 'forward':
64: case 'redirect':
65: $data['recipients'] = $log->recipients;
66: break;
67: }
68:
69: try {
70: $this->_history->log($this->_getUniqueHistoryId($msg), $data);
71: return true;
72: } catch (RuntimeException $e) {
73:
74: } catch (Exception $e) {
75: 76: 77: 78:
79: Horde::log(
80: sprintf(
81: 'Could not log message details to Horde_History. Error returned: %s',
82: $e->getMessage()
83: ),
84: 'ERR'
85: );
86: }
87:
88: return false;
89: }
90:
91: 92:
93: public function getLog(IMP_Maillog_Message $msg, array $filter = array())
94: {
95: $out = array();
96:
97: try {
98: $history = $this->_history->getHistory(
99: $this->_getUniqueHistoryId($msg)
100: );
101: } catch (Exception $e) {
102: return $out;
103: }
104:
105: foreach ($history as $val) {
106: if (!in_array($val['action'], $filter)) {
107: switch ($val['action']) {
108: case 'forward':
109: $ob = new IMP_Maillog_Log_Forward($val['recipients']);
110: break;
111:
112: case 'mdn':
113: $ob = new IMP_Maillog_Log_Mdn();
114: break;
115:
116: case 'redirect':
117: $ob = new IMP_Maillog_Log_Redirect($val['recipients']);
118: break;
119:
120: case 'reply':
121: $ob = new IMP_Maillog_Log_Reply();
122: break;
123:
124: case 'reply_all':
125: $ob = new IMP_Maillog_Log_Replyall();
126: break;
127:
128: case 'reply_list':
129: $ob = new IMP_Maillog_Log_Replylist();
130: break;
131:
132: default:
133: continue 2;
134: }
135:
136: $ob->timestamp = $val['ts'];
137:
138: $out[] = $ob;
139: }
140: }
141:
142: return $out;
143: }
144:
145: 146:
147: public function deleteLogs(array $msgs)
148: {
149: $ids = array();
150: foreach ($msgs as $val) {
151: try {
152: $ids[] = $this->_getUniqueHistoryId($val);
153: } catch (RuntimeException $e) {
154:
155: }
156: }
157:
158: $this->_history->removeByNames($ids);
159: }
160:
161: 162:
163: public function getChanges($ts)
164: {
165: $msgids = preg_replace(
166: '/^([^:]*:){2}/',
167: '',
168: array_keys($this->_history->getByTimestamp(
169: '>',
170: $ts,
171: array(),
172: $this->_getUniqueHistoryId()
173: ))
174: );
175:
176: $out = array();
177: foreach ($msgids as $val) {
178: $out[] = new IMP_Maillog_Message($val);
179: }
180:
181: return $out;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192:
193: protected function _getUniqueHistoryId($msg = null)
194: {
195: $msgid = $msg ? $msg->msgid : null;
196: if ($msgid === '') {
197: throw new RuntimeException('Message-ID missing.');
198: }
199:
200: return implode(':', array_filter(array(
201: 'imp',
202: str_replace('.', '*', $this->_user),
203: $msgid
204: )));
205: }
206:
207: }
208: