-
Notifications
You must be signed in to change notification settings - Fork 127
/
Identity.php
468 lines (415 loc) · 12.9 KB
/
Identity.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
/**
* Copyright 2001-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2001-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Prefs
*/
/**
* This class provides an interface to all identities a user might have.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2001-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Prefs
*/
class Horde_Prefs_Identity
implements ArrayAccess, Countable, IteratorAggregate
{
/**
* Array containing all the user's identities.
*
* @var array
*/
protected $_identities = array();
/**
* A pointer to the user's standard identity.
* This one is used by the methods returning values if no other one is
* specified.
*
* @var integer
*/
protected $_default = 0;
/**
* The user whose identities these are.
*
* @var string
*/
protected $_user = null;
/**
* Preference names.
*
* @var array
*/
protected $_prefnames = array(
'default_identity' => 'default_identity',
'from_addr' => 'from_addr',
'fullname' => 'fullname',
'id' => 'id',
'identities' => 'identities',
'properties' => array('id', 'fullname', 'from_addr')
);
/**
* The prefs object that this Identity points to.
*
* @var Horde_Prefs
*/
protected $_prefs;
/**
* Constructor.
*
* @param array $params Parameters:
* - default_identity: (string) The preference name for the default
* identity.
* DEFAULT: 'default_identity'
* - from_addr: (string) The preference name for the user's from e-mail
* address.
* DEFAULT: 'from_addr'
* - fullname: (string) The preference name for the user's full name.
* DEFAULT: 'fullname'
* - id: (string) The preference name for the identity name.
* DEFAULT: 'id'
* - identities: (string) The preference name for the identity store.
* DEFAULT: 'identities'
* - prefs: (Horde_Prefs) [REQUIRED] The prefs object to use.
* - properties: (array) The list of properties for the identity.
* DEFAULT: array('from_addr', 'fullname', 'id')
* - user: (string) [REQUIRED] The user whose prefs we are handling.
*/
public function __construct($params = array())
{
foreach (array_keys($this->_prefnames) as $val) {
if (isset($params[$val])) {
$this->_prefnames[$val] = $params[$val];
}
}
$this->_prefs = $params['prefs'];
$this->_user = $params['user'];
if (!($this->_identities = @unserialize($this->_prefs->getValue($this->_prefnames['identities'])))) {
$this->_identities = $this->_prefs->getDefault($this->_prefnames['identities']);
}
$this->setDefault($this->_prefs->getValue($this->_prefnames['default_identity']));
}
/**
* Creates a default identity if none exists yet and sets the preferences
* up if the identities are locked.
*/
public function init()
{
if (!is_array($this->_identities) || (count($this->_identities) <= 0)) {
foreach (array_keys($this->_prefnames) as $key) {
$identity[$key] = $this->_prefs->getValue($key);
}
if (empty($identity['id'])) {
$identity['id'] = Horde_Prefs_Translation::t("Default Identity");
}
$this->_identities = array($identity);
$this->verify(0);
}
}
/**
* Saves all identities in the prefs backend.
*/
public function save()
{
$this->_prefs->setValue($this->_prefnames['identities'], serialize($this->_identities));
$this->_prefs->setValue($this->_prefnames['default_identity'], $this->_default);
}
/**
* Adds a new identity to the array of identities.
*
* @param array $identity An identity hash to add.
*
* @return integer The pointer to the created identity
*/
public function add($identity = array())
{
$this->_identities[] = $identity;
return count($this->_identities) - 1;
}
/**
* Returns a complete identity hash.
*
* @param integer $identity The identity to retrieve.
*
* @return array An identity hash. Returns null if the identity does not
* exist.
*/
public function get($identity = null)
{
if (is_null($identity)) {
$identity = $this->_default;
}
return isset($this->_identities[$identity])
? $this->_identities[$identity]
: null;
}
/**
* Removes an identity from the array of identities.
*
* @param integer $identity The pointer to the identity to be removed
*
* @return array The removed identity.
*/
public function delete($identity)
{
$deleted = array_splice($this->_identities, $identity, 1);
if (!empty($deleted)) {
foreach (array_keys($this->_identities) as $id) {
if ($this->setDefault($id)) {
break;
}
}
$this->save();
}
return reset($deleted);
}
/**
* Returns a pointer to the current default identity.
*
* @return integer The pointer to the current default identity.
*/
public function getDefault()
{
return $this->_default;
}
/**
* Sets the current default identity.
* If the identity doesn't exist, the old default identity stays the same.
*
* @param integer $identity The pointer to the new default identity.
*
* @return boolean True on success, false on failure.
*/
public function setDefault($identity)
{
if (isset($this->_identities[$identity])) {
$this->_default = $identity;
return true;
}
return false;
}
/**
* Returns a property from one of the identities. If this value doesn't
* exist or is locked, the property is retrieved from the prefs backend.
*
* @param string $key The property to retrieve.
* @param integer $identity The identity to retrieve the property from.
*
* @return mixed The value of the property.
*/
public function getValue($key, $identity = null)
{
if (is_null($identity) || !isset($this->_identities[$identity])) {
$identity = $this->_default;
}
return (!isset($this->_identities[$identity][$key]) || $this->_prefs->isLocked($key))
? $this->_prefs->getValue($key)
: $this->_identities[$identity][$key];
}
/**
* Returns an array with the specified property from all existing
* identities.
*
* @param string $key The property to retrieve.
*
* @return array The array with the values from all identities.
*/
public function getAll($key)
{
$list = array();
foreach (array_keys($this->_identities) as $identity) {
$list[$identity] = $this->getValue($key, $identity);
}
return $list;
}
/**
* Sets a property with a specified value.
*
* @param string $key The property to set.
* @param mixed $val The value to which the property should be
* set.
* @param integer $identity The identity to set the property in.
*
* @return boolean True on success, false on failure (property was
* locked).
*/
public function setValue($key, $val, $identity = null)
{
if (is_null($identity)) {
$identity = $this->_default;
}
if (!$this->_prefs->isLocked($key)) {
$this->_identities[$identity][$key] = $val;
return true;
}
return false;
}
/**
* Returns true if all properties are locked and therefore nothing in the
* identities can be changed.
*
* @return boolean True if all properties are locked, false otherwise.
*/
public function isLocked()
{
foreach ($this->_prefnames['properties'] as $key) {
if (!$this->_prefs->isLocked($key)) {
return false;
}
}
return true;
}
/**
* Returns true if the given address belongs to one of the identities.
*
* @param string $key The identity key to search.
* @param string $value The value to search for in $key.
*
* @return boolean True if the $value was found in $key.
*/
public function hasValue($key, $value)
{
$list = $this->getAll($key);
foreach ($list as $valueB) {
if (!empty($valueB) &&
strpos(Horde_String::lower($value), Horde_String::lower($valueB)) !== false) {
return true;
}
}
return false;
}
/**
* Verifies and sanitizes all identity properties.
*
* @param integer $identity The identity to verify.
*
* @throws Horde_Prefs_Exception
*/
public function verify($identity = null)
{
if (is_null($identity)) {
$identity = $this->_default;
}
if (!$this->getValue('id', $identity)) {
$this->setValue('id', Horde_Prefs_Translation::t("Unnamed"), $identity);
}
// To verify e-mail, first parse input, than re-parse in verify mode.
$ob = new Horde_Mail_Rfc822_Address($this->getValue($this->_prefnames['from_addr'], $identity));
try {
$rfc822 = new Horde_Mail_Rfc822();
$rfc822->parseAddressList($ob, array(
'validate' => true
));
} catch (Horde_Mail_Exception $e) {
throw new Horde_Prefs_Exception(sprintf(Horde_Prefs_Translation::t("\"%s\" is not a valid email address."), strval($ob)));
}
$this->setValue('from_addr', strval($ob), $identity);
}
/**
* Returns the user's full name.
*
* @param integer $ident The identity to retrieve the name from.
*
* @return string The user's full name, or the user name if it doesn't
* exist.
*/
public function getName($ident = null)
{
if (isset($this->_names[$ident])) {
return $this->_names[$ident];
}
$this->_names[$ident] = $this->getValue($this->_prefnames['fullname'], $ident);
if (!strlen($this->_names[$ident])) {
$this->_names[$ident] = $this->_user;
}
return $this->_names[$ident];
}
/**
* Returns the from address based on the chosen identity.
*
* If no address can be found it is built from the current user.
*
* @since Horde_Prefs 2.3.0
*
* @param integer $ident The identity to retrieve the address from.
*
* @return Horde_Mail_Rfc822_Address A valid from address.
*/
public function getFromAddress($ident = null)
{
$val = $this->getValue($this->_prefnames['from_addr'], $ident);
if (!strlen($val)) {
$val = $this->_user;
}
return new Horde_Mail_Rfc822_Address($val);
}
/**
* Generates the from address to use for the default identity.
*
* @param boolean $fullname Include the fullname information.
*
* @return Horde_Mail_Rfc822_Address The default from address (object
* returned since 2.2.0).
*/
public function getDefaultFromAddress($fullname = false)
{
$ob = new Horde_Mail_Rfc822_Address($this->getFromAddress());
$ob->personal = $fullname
? $this->getValue($this->_prefnames['fullname'])
: null;
return $ob;
}
/* ArrayAccess methods. */
/**
* @since 2.7.0
*/
public function offsetExists($offset)
{
return isset($this->_identities[$offset]);
}
/**
* @since 2.7.0
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* @since 2.7.0
*/
public function offsetSet($offset, $value)
{
// $value is ignored.
$this->set($offset);
}
/**
* @since 2.7.0
*/
public function offsetUnset($offset)
{
$this->delete($offset);
}
/* Countable method. */
/**
* @since 2.7.0
*/
public function count()
{
return count($this->_identities);
}
/* IteratorAggregate method. */
/**
* @since 2.7.0
*/
public function getIterator()
{
return new ArrayIterator($this->_identities);
}
}