/
Prefs.php
189 lines (170 loc) · 5.84 KB
/
Prefs.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
<?php
/**
* Copyright 2012-2015 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 Michael Slusarz <slusarz@horde.org>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/apache ASL
* @package Ingo
*/
/**
* Ingo_Storage_Prefs implements the Ingo_Storage API to save Ingo data via the
* Horde preferences system.
*
* @author Michael Slusarz <slusarz@horde.org>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/apache ASL
* @package Ingo
*/
class Ingo_Storage_Prefs 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 The specified data.
*/
protected function _retrieve($field, $readonly = false)
{
switch ($field) {
case self::ACTION_BLACKLIST:
$ob = new Ingo_Storage_Blacklist();
if ($data = @unserialize($this->_prefs()->getValue('blacklist'))) {
$ob->setBlacklist($data['a']);
$ob->setBlacklistFolder($data['f']);
}
break;
case self::ACTION_WHITELIST:
$ob = new Ingo_Storage_Whitelist();
if ($data = @unserialize($this->_prefs()->getValue('whitelist'))) {
$ob->setWhitelist($data);
}
break;
case self::ACTION_FILTERS:
$ob = new Ingo_Storage_Filters_Prefs($this->_prefs());
break;
case self::ACTION_FORWARD:
$ob = new Ingo_Storage_Forward();
if ($data = @unserialize($this->_prefs()->getValue('forward'))) {
$ob->setForwardAddresses($data['a']);
$ob->setForwardKeep($data['k']);
}
break;
case self::ACTION_VACATION:
$ob = new Ingo_Storage_Vacation();
if ($data = @unserialize($this->_prefs()->getValue('vacation'))) {
$ob->setVacationAddresses($data['addresses']);
$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:
$ob = new Ingo_Storage_Spam();
if ($data = @unserialize($this->_prefs()->getValue('spam'))) {
$ob->setSpamFolder($data['folder']);
$ob->setSpamLevel($data['level']);
}
break;
default:
$ob = false;
break;
}
return $ob;
}
/**
* Stores the specified data in the storage backend.
*
* @param Ingo_Storage_Rule $ob The object to store.
*/
protected function _store($ob)
{
switch ($ob->obType()) {
case self::ACTION_BLACKLIST:
$this->_prefs()->setValue('blacklist', serialize(array(
'a' => $ob->getBlacklist(),
'f' => $ob->getBlacklistFolder()
)));
break;
case self::ACTION_FILTERS:
$this->_prefs()->setValue(
'rules',
serialize($ob->getFilterList())
);
break;
case self::ACTION_FORWARD:
$this->_prefs()->setValue('forward', serialize(array(
'a' => $ob->getForwardAddresses(),
'k' => $ob->getForwardKeep()
)));
break;
case self::ACTION_VACATION:
$this->_prefs()->setValue('vacation', serialize(array(
'addresses' => $ob->getVacationAddresses(),
'days' => $ob->getVacationDays(),
'excludes' => $ob->getVacationExcludes(),
'ignorelist' => $ob->getVacationIgnorelist(),
'reason' => $ob->getVacationReason(),
'subject' => $ob->getVacationSubject(),
'start' => $ob->getVacationStart(),
'end' => $ob->getVacationEnd()
)));
break;
case self::ACTION_WHITELIST:
$this->_prefs()->setValue(
'whitelist',
serialize($ob->getWhitelist())
);
break;
case self::ACTION_SPAM:
$this->_prefs()->setValue('spam', serialize(array(
'folder' => $ob->getSpamFolder(),
'level' => $ob->getSpamLevel()
)));
break;
}
}
/**
*/
protected function _removeUserData($user)
{
$p = $this->_prefs($user);
$p->remove('blacklist');
$p->remove('forward');
$p->remove('rules');
$p->remove('spam');
$p->remove('vacation');
$p->remove('whitelist');
}
/**
* Get prefs object to use for storage.
*
* @param string $user Username to use (if not default).
*
* @return Horde_Prefs Prefs object.
*/
protected function _prefs($user = null)
{
global $injector;
return $injector->getInstance('Horde_Core_Factory_Prefs')->create('ingo', array(
'cache' => false,
'user' => is_null($user) ? Ingo::getUser() : $user
));
}
}