File: Identity.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (239 lines) | stat: -rw-r--r-- 7,554 bytes parent folder | download
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
<?php
/*
 * $Horde: horde/lib/Identity.php,v 1.4.2.13 2003/04/28 19:59:07 jan Exp $
 *
 * Copyright 2001-2003 Jan Schneider <jan@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * This class provides an interface to all identities a user might
 * have. Its methods take care of any site-specific restrictions
 * configured in prefs.php and conf.php.
 *
 * @author  Jan Schneider <jan@horde.org>
 * @version $Revision: 1.4.2.13 $
 * @since   Horde 1.3.5
 * @package horde.identity
 */
class Identity {

    /**
     * Array containing all the user's identities.
     * @var array $identities
     */
    var $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 $default
     */
    var $default = 0;

    /**
     * Array containing all of the properties in this identity,
     * excluding the id.
     * @var array $properties
     */
    var $properties = array();

    /**
     * Reference to the prefs object that this Identity points to.
     * @var object Prefs $prefs
     */
    var $prefs;

    /**
     * Reads all the user's identities from the prefs object or builds
     * a new identity from the standard values given in prefs.php.
     *
     * @param optional string $user  If specified, we read another user's
     *                               identities instead of the current user.
     */
    function Identity($user = null)
    {
        if (is_null($user)) {
            $this->prefs = &$GLOBALS['prefs'];
            $this->default = $this->prefs->getValue('default_identity');
            $this->identities = @unserialize($this->prefs->getValue('identities'));
        } else {
            global $registry;
            $this->prefs = &Prefs::singleton($GLOBALS['conf']['prefs']['driver'], $registry->getApp(), '', '', $GLOBALS['conf']['prefs']['params']);
            $this->prefs->setDefaults($registry->getParam('fileroot', 'horde') . '/config/prefs.php');
            $this->prefs->setDefaults($registry->getParam('fileroot') . '/config/prefs.php');

            $vals = $this->prefs->getPref($user, array('default_identity', 'identities'));
            $this->default = isset($vals['default_identity']) ? $vals['default_identity'] : 0;
            $this->identities = @unserialize($vals['identities']);
        }

        if (!is_array($this->identities) || (count($this->identities) <= 0)) {
            $identity = array('id' => _("Default Identity"));
            foreach ($this->properties as $key) {
                $identity[$key] = $this->prefs->getValue($key);
            }

            $this->identities[] = $identity;
        }
    }

    /**
     * Saves all identities in the prefs backend.
     */
    function save()
    {
        $this->prefs->setValue('identities', serialize($this->identities));
        $this->prefs->setValue('default_identity', $this->default);
        $this->prefs->store();
    }

    /**
     * Adds a new empty identity to the array of identities.
     * @return integer      The pointer to the created identity
     */
    function add()
    {
        $this->identities[] = array();
        return count($this->identities) - 1;
    }

    /**
     * Remove an identity from the array of identities
     * @param integer $identity The pointer to the identity to be removed
     * @return array            The removed identity
     */
    function delete($identity)
    {
        $deleted = array_splice($this->identities, $identity, 1);
        foreach ($this->identities as $id => $null) {
            if ($this->setDefault($id)) {
                break;
            }
        }
        $this->save();
        return $deleted;
    }

    /**
     * Returns a pointer to the current default identity.
     * @return integer      The pointer to the current default identity
     */
    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
     */
    function setDefault($identity)
    {
        if (isset($this->identities[$identity])) {
            $this->default = $identity;
            return true;
        } else {
            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     (optional) The identity to retrieve the property from
     * @return mixed                The value of the property.
     */
    function getValue($key, $identity = null)
    {
        if (!isset($identity) || !isset($this->identities[$identity])) {
            $identity = $this->default;
        }

        if (!isset($this->identities[$identity][$key]) || $this->prefs->isLocked($key)) {
            $val = $this->prefs->getValue($key);
        } else {
            $val = $this->identities[$identity][$key];
        }

        return $val;
    }

    /**
     * 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
     */
    function getAll($key)
    {

        $list = array();
        foreach ($this->identities as $identity => $null) {
            $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 (optional) The identity to set the property in
     * @return boolean          True on success, false on failure (property was locked)
     */
    function setValue($key, $val, $identity = null)
    {
        if (!isset($identity)) {
            $identity = $this->default;
        }

        if (!$this->prefs->isLocked($key)) {
            $this->identities[$identity][$key] = $val;
            return true;
        } else {
            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
     */
    function isLocked()
    {
        foreach ($this->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.
     */
    function hasValue($key, $valueA)
    {
        $list = $this->getAll($key);
        foreach ($list as $valueB) {
            if (strpos(strtolower($valueA), strtolower($valueB)) !== false) {
                return true;
            }
        }
        return false;
    }

}