forked from horde/horde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sql.php
317 lines (297 loc) · 13.3 KB
/
Sql.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
<?php
/**
* Copyright 2012-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.
*
* @author Jan Schneider <jan@horde.org>
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @category Horde
* @license http://www.horde.org/licenses/apache ASL
* @package Ingo
*/
/**
* Ingo_Storage_Sql implements the Ingo_Storage API to save Ingo data via
* Horde's Horde_Db database abstraction layer.
*
* @author Jan Schneider <jan@horde.org>
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @category Horde
* @license http://www.horde.org/licenses/apache ASL
* @package Ingo
*/
class Ingo_Storage_Sql extends Ingo_Storage
{
/**
* Retrieves the specified data from the storage backend.
*
* @param integer $field The field name of the desired data.
* See lib/Storage.php for the available fields.
* @param boolean $readonly Whether to disable any write operations.
*
* @return Ingo_Storage_Rule|Ingo_Storage_Filters The specified data.
* @throws Ingo_Exception
*/
protected function _retrieve($field, $readonly = false)
{
switch ($field) {
case self::ACTION_BLACKLIST:
case self::ACTION_WHITELIST:
if ($field == self::ACTION_BLACKLIST) {
$ob = new Ingo_Storage_Blacklist();
$filters = $this->retrieve(self::ACTION_FILTERS);
$rule = $filters->findRule($field);
if (isset($rule['action-value'])) {
$ob->setBlacklistFolder($rule['action-value']);
}
} else {
$ob = new Ingo_Storage_Whitelist();
}
$query = sprintf('SELECT list_address FROM %s WHERE list_owner = ? AND list_blacklist = ?',
$this->_params['table_lists']);
$values = array(Ingo::getUser(),
(int)($field == self::ACTION_BLACKLIST));
try {
$addresses = $this->_params['db']->selectValues($query, $values);
} catch (Horde_Db_Exception $e) {
Horde::log($e->getMessage(), 'ERR');
throw new Ingo_Exception($e);
}
if ($field == self::ACTION_BLACKLIST) {
$ob->setBlacklist($addresses);
} else {
$ob->setWhitelist($addresses);
}
break;
case self::ACTION_FILTERS:
$ob = new Ingo_Storage_Filters_Sql($this->_params['db'], $this->_params);
$ob->init($readonly);
break;
case self::ACTION_FORWARD:
$query = sprintf('SELECT * FROM %s WHERE forward_owner = ?',
$this->_params['table_forwards']);
try {
$data = $this->_params['db']->selectOne($query, array(Ingo::getUser()));
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
$ob = new Ingo_Storage_Forward();
if (!empty($data)) {
$ob->setForwardAddresses(explode("\n", $data['forward_addresses']));
$ob->setForwardKeep((bool)$data['forward_keep']);
$ob->setSaved(true);
} elseif ($data = @unserialize($GLOBALS['prefs']->getDefault('forward'))) {
$ob->setForwardAddresses($data['a']);
$ob->setForwardKeep($data['k']);
}
break;
case self::ACTION_VACATION:
$query = sprintf('SELECT * FROM %s WHERE vacation_owner = ?',
$this->_params['table_vacations']);
try {
$data = $this->_params['db']->selectOne($query, array(Ingo::getUser()));
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
$ob = new Ingo_Storage_Vacation();
if (!empty($data)) {
$ob->setVacationAddresses(explode("\n", $data['vacation_addresses']));
$ob->setVacationDays((int)$data['vacation_days']);
$ob->setVacationStart((int)$data['vacation_start']);
$ob->setVacationEnd((int)$data['vacation_end']);
$ob->setVacationExcludes(explode("\n", $data['vacation_excludes']));
$ob->setVacationIgnorelist((bool)$data['vacation_ignorelists']);
$ob->setVacationReason(Horde_String::convertCharset($data['vacation_reason'], $this->_params['charset'], 'UTF-8'));
$ob->setVacationSubject(Horde_String::convertCharset($data['vacation_subject'], $this->_params['charset'], 'UTF-8'));
$ob->setSaved(true);
} elseif ($data = @unserialize($GLOBALS['prefs']->getDefault('vacation'))) {
$ob->setVacationAddresses($data['addresses'], false);
$ob->setVacationDays($data['days']);
$ob->setVacationExcludes($data['excludes']);
$ob->setVacationIgnorelist($data['ignorelist']);
$ob->setVacationReason($data['reason']);
$ob->setVacationSubject($data['subject']);
if (isset($data['start'])) {
$ob->setVacationStart($data['start']);
}
if (isset($data['end'])) {
$ob->setVacationEnd($data['end']);
}
}
break;
case self::ACTION_SPAM:
$query = sprintf('SELECT * FROM %s WHERE spam_owner = ?',
$this->_params['table_spam']);
try {
$data = $this->_params['db']->selectOne($query, array(Ingo::getUser()));
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
$ob = new Ingo_Storage_Spam();
if (!empty($data)) {
$ob->setSpamFolder($data['spam_folder']);
$ob->setSpamLevel((int)$data['spam_level']);
$ob->setSaved(true);
} elseif ($data = @unserialize($GLOBALS['prefs']->getDefault('spam'))) {
$ob->setSpamFolder($data['folder']);
$ob->setSpamLevel($data['level']);
}
break;
default:
$ob = false;
}
return $ob;
}
/**
* Stores the specified data in the storage backend.
*
* @access private
*
* @param Ingo_Storage_Rule|Ingo_Storage_Filters $ob The object to store.
*/
protected function _store($ob)
{
switch ($ob->obType()) {
case self::ACTION_BLACKLIST:
case self::ACTION_WHITELIST:
$is_blacklist = (int)($ob->obType() == self::ACTION_BLACKLIST);
if ($is_blacklist) {
$filters = $this->retrieve(self::ACTION_FILTERS);
$id = $filters->findRuleId(self::ACTION_BLACKLIST);
if ($id !== null) {
$rule = $filters->getRule($id);
if (!isset($rule['action-value']) ||
$rule['action-value'] != $ob->getBlacklistFolder()) {
$rule['action-value'] = $ob->getBlacklistFolder();
$filters->updateRule($rule, $id);
}
}
}
$query = sprintf('DELETE FROM %s WHERE list_owner = ? AND list_blacklist = ?',
$this->_params['table_lists']);
$values = array(Ingo::getUser(), $is_blacklist);
try {
$this->_params['db']->delete($query, $values);
} catch (Horde_Db_Exception $e) {
Horde::log($e, 'ERR');
throw new Ingo_Exception($e);
}
$query = sprintf('INSERT INTO %s (list_owner, list_blacklist, list_address) VALUES (?, ?, ?)',
$this->_params['table_lists']);
$addresses = $is_blacklist ? $ob->getBlacklist() : $ob->getWhitelist();
foreach ($addresses as $address) {
try {
$result = $this->_params['db']->insert(
$query,
array(Ingo::getUser(),
$is_blacklist,
$address));
} catch (Horde_Db_Exception $e) {
Horde::log($result, 'ERR');
throw new Ingo_Exception($e);
}
}
$ob->setSaved(true);
break;
case self::ACTION_FORWARD:
$values = array(
implode("\n", $ob->getForwardAddresses()),
(int)(bool)$ob->getForwardKeep(),
Ingo::getUser());
try {
if ($ob->isSaved()) {
$query = sprintf('UPDATE %s SET forward_addresses = ?, forward_keep = ? WHERE forward_owner = ?', $this->_params['table_forwards']);
$this->_params['db']->update($query, $values);
} else {
$query = sprintf('INSERT INTO %s (forward_addresses, forward_keep, forward_owner) VALUES (?, ?, ?)', $this->_params['table_forwards']);
$this->_params['db']->insert($query, $values);
}
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
$ob->setSaved(true);
break;
case self::ACTION_VACATION:
$values = array(
implode("\n", $ob->getVacationAddresses()),
Horde_String::convertCharset($ob->getVacationSubject(),
'UTF-8',
$this->_params['charset']),
Horde_String::convertCharset($ob->getVacationReason(),
'UTF-8',
$this->_params['charset']),
(int)$ob->getVacationDays(),
(int)$ob->getVacationStart(),
(int)$ob->getVacationEnd(),
implode("\n", $ob->getVacationExcludes()),
(int)(bool)$ob->getVacationIgnorelist(),
Ingo::getUser()
);
try {
if ($ob->isSaved()) {
$query = sprintf('UPDATE %s SET vacation_addresses = ?, vacation_subject = ?, vacation_reason = ?, vacation_days = ?, vacation_start = ?, vacation_end = ?, vacation_excludes = ?, vacation_ignorelists = ? WHERE vacation_owner = ?', $this->_params['table_vacations']);
$this->_params['db']->update($query, $values);
} else {
$query = sprintf('INSERT INTO %s (vacation_addresses, vacation_subject, vacation_reason, vacation_days, vacation_start, vacation_end, vacation_excludes, vacation_ignorelists, vacation_owner) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->_params['table_vacations']);
$this->_params['db']->insert($query, $values);
}
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
$ob->setSaved(true);
break;
case self::ACTION_SPAM:
$values = array(
(int)$ob->getSpamLevel(),
$ob->getSpamFolder(),
Ingo::getUser());
try {
if ($ob->isSaved()) {
$query = sprintf('UPDATE %s SET spam_level = ?, spam_folder = ? WHERE spam_owner = ?', $this->_params['table_spam']);
$this->_params['db']->update($query, $values);
} else {
$query = sprintf('INSERT INTO %s (spam_level, spam_folder, spam_owner) VALUES (?, ?, ?)', $this->_params['table_spam']);
$this->_params['db']->insert($query, $values);
}
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
$ob->setSaved(true);
break;
}
}
/**
* Removes the data of the specified user from the storage backend.
*
* @param string $user The user name to delete filters for.
*
* @throws Ingo_Exception
*/
public function removeUserData($user)
{
if (!$GLOBALS['registry']->isAdmin() &&
$user != $GLOBALS['registry']->getAuth()) {
throw new Ingo_Exception(_("Permission Denied"));
}
$queries = array(sprintf('DELETE FROM %s WHERE rule_owner = ?',
$this->_params['table_rules']),
sprintf('DELETE FROM %s WHERE list_owner = ?',
$this->_params['table_lists']),
sprintf('DELETE FROM %s WHERE vacation_owner = ?',
$this->_params['table_vacations']),
sprintf('DELETE FROM %s WHERE forward_owner = ?',
$this->_params['table_forwards']),
sprintf('DELETE FROM %s WHERE spam_owner = ?',
$this->_params['table_spam']));
$values = array($user);
foreach ($queries as $query) {
try {
$this->_params['db']->delete($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Ingo_Exception($e);
}
}
return true;
}
}