/
Ingo.php
306 lines (266 loc) · 9.05 KB
/
Ingo.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
<?php
/**
* Copyright 2002-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @category Horde
* @copyright 2002-2014 Horde LLC
* @license http://www.horde.org/licenses/apache ASL
* @package Ingo
*/
/**
* Ingo base class.
*
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2002-2014 Horde LLC
* @license http://www.horde.org/licenses/apache ASL
* @package Ingo
*/
class Ingo
{
/**
* String that can't be a valid folder name used to mark blacklisted email
* as deleted.
*/
const BLACKLIST_MARKER = '++DELETE++';
/**
* Define the key to use to indicate a user-defined header is requested.
*/
const USER_HEADER = '++USER_HEADER++';
/**
* Only filter unseen messages.
*/
const FILTER_UNSEEN = 1;
/**
* Only filter seen messages.
*/
const FILTER_SEEN = 2;
/**
* Constants for rule types.
*/
const RULE_ALL = 0;
const RULE_FILTER = 1;
const RULE_BLACKLIST = 2;
const RULE_WHITELIST = 3;
const RULE_VACATION = 4;
const RULE_FORWARD = 5;
const RULE_SPAM = 6;
/**
* Returns the user whose rules are currently being edited.
*
* @param boolean $full Always return the full user name with realm?
*
* @return string The current user.
*/
static public function getUser($full = true)
{
global $injector, $registry, $session;
if (!$injector->getInstance('Ingo_Shares')) {
return $registry->getAuth($full ? null : 'bare');
}
list(, $user) = explode(':', $session->get('ingo', 'current_share'), 2);
return $user;
}
/**
* Returns the domain name, if any of the user whose rules are currently
* being edited.
*
* @return string The current user's domain name.
*/
static public function getDomain()
{
$user = self::getUser(true);
$pos = strpos($user, '@');
return ($pos !== false)
? substr($user, $pos + 1)
: false;
}
/**
* Connects to the backend, uploads the scripts and sets them active.
*
* @param array $scripts A list of scripts to set active.
* @param boolean $deactivate If true, notification will identify the
* script as deactivated instead of activated.
*
* @throws Ingo_Exception
*/
static public function activateScripts($scripts, $deactivate = false)
{
foreach ($scripts as $script) {
try {
$GLOBALS['injector']
->getInstance('Ingo_Factory_Transport')
->create($script['transport'])
->setScriptActive($script);
} catch (Ingo_Exception $e) {
$msg = $deactivate
? _("There was an error deactivating the script.")
: _("There was an error activating the script.");
throw new Ingo_Exception(sprintf(_("%s The driver said: %s"), $msg, $e->getMessage()));
}
}
$msg = ($deactivate)
? _("Script successfully deactivated.")
: _("Script successfully activated.");
$GLOBALS['notification']->push($msg, 'horde.success');
}
/**
* Does all the work in updating the script on the server.
*
* @throws Ingo_Exception
*/
static public function updateScript()
{
foreach ($GLOBALS['injector']->getInstance('Ingo_Factory_Script')->createAll() as $script) {
if ($script->hasFeature('script_file')) {
try {
/* Generate and activate the script. */
self::activateScripts($script->generate());
} catch (Ingo_Exception $e) {
throw new Ingo_Exception(sprintf(_("Script not updated: %s"), $e->getMessage()));
}
}
}
}
/**
* Check share permissions.
*
* @param $integer $mask Permission mask.
*
* @return boolean True if user has permission.
*/
static public function hasSharePermission($mask = null)
{
global $injector, $registry, $session;
return ($share = $injector->getInstance('Ingo_Shares'))
? ($share->getPermissions(
$session->get('ingo', 'current_share'),
$registry->getAuth()
) & $mask)
: true;
}
/**
* Returns the vacation reason with all placeholder replaced.
*
* @param string $reason The vacation reason including placeholders.
* @param integer $start The vacation start timestamp.
* @param integer $end The vacation end timestamp.
*
* @return string The vacation reason suitable for usage in the filter
* scripts.
*/
static public function getReason($reason, $start, $end)
{
$identity = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_Identity')
->create(Ingo::getUser());
$format = $GLOBALS['prefs']->getValue('date_format');
return str_replace(array('%NAME%',
'%EMAIL%',
'%SIGNATURE%',
'%STARTDATE%',
'%ENDDATE%'),
array($identity->getName(),
$identity->getDefaultFromAddress(),
$identity->getValue('signature'),
$start ? strftime($format, $start) : '',
$end ? strftime($format, $end) : ''),
$reason);
}
/**
* Updates a list (blacklist/whitelist) filter.
*
* @param mixed $addresses Addresses of the filter.
* @param integer $type Type of filter.
*
* @return Horde_Storage_Rule The filter object.
*/
static public function updateListFilter($addresses, $type)
{
global $injector;
$storage = $injector->getInstance('Ingo_Factory_Storage')->create();
$rule = $storage->retrieve($type);
switch ($type) {
case $storage::ACTION_BLACKLIST:
$rule->setBlacklist($addresses);
$addr = $rule->getBlacklist();
$rule2 = $storage->retrieve($storage::ACTION_WHITELIST);
$addr2 = $rule2->getWhitelist();
break;
case $storage::ACTION_WHITELIST:
$rule->setWhitelist($addresses);
$addr = $rule->getWhitelist();
$rule2 = $storage->retrieve($storage::ACTION_BLACKLIST);
$addr2 = $rule2->getBlacklist();
break;
}
/* Filter out the rule's addresses in the opposite filter. */
$ob = new Horde_Mail_Rfc822_List($addr2);
$ob->setIteratorFilter(0, $addr);
switch ($type) {
case $storage::ACTION_BLACKLIST:
$rule2->setWhitelist($ob->bare_addresses);
break;
case $storage::ACTION_WHITELIST:
$rule2->setBlacklist($ob->bare_addresses);
break;
}
$storage->store($rule);
$storage->store($rule2);
return $rule;
}
/**
* Output description for a rule.
*
* @param array $rule Rule.
*
* @return string Text description.
*/
static public function ruleDescription($rule)
{
global $injector;
$condition_size = count($rule['conditions']) - 1;
$descrip = '';
$storage = $injector->getInstance('Ingo_Factory_Storage')->create();
foreach ($rule['conditions'] as $key => $val) {
$info = $storage->getTestInfo($val['match']);
$descrip .= sprintf("%s %s \"%s\"", _($val['field']), $info->label, $val['value']);
if (!empty($val['case'])) {
$descrip .= ' [' . _("Case Sensitive") . ']';
}
if ($key < $condition_size) {
$descrip .= ($rule['combine'] == Ingo_Storage::COMBINE_ALL)
? _(" and")
: _(" or");
$descrip .= "\n ";
}
}
$descrip .= "\n" . $storage->getActionInfo($rule['action'])->label;
if ($rule['action-value']) {
$descrip .= ': ' . $rule['action-value'];
}
if ($rule['stop']) {
$descrip .= "\n[stop]";
}
return $descrip;
}
/**
* Loads the backends.php configuration file.
*
* @return array Configuration data.
* @throws Horde_Exception
*/
static public function loadBackends()
{
$config = new Horde_Registry_Loadconfig('ingo', 'backends.php', 'backends');
if (empty($config->config['backends']) ||
!is_array($config->config['backends'])) {
throw new Ingo_Exception(_("No backends configured in backends.php"));
}
return $config->config['backends'];
}
}