Completed
Push — develop ( ed6c74...ceecfe )
by Patrick
04:06
created

Auth/LDAPCachableObject.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Flipside\Auth;
3
4
trait LDAPCachableObject
5
{
6
    protected function initialize($data)
7
    {
8
        if($data === false)
9
        {
10
            return;
11
        }
12
        if(is_string($data))
13
        {
14
            $this->ldapObj = $this->initializeFromDN($data);
15
            return;
16
        }
17
        if($data instanceof \Flipside\LDAP\LDAPObject)
18
        {
19
            $this->ldapObj = $data;
20
            return;
21
        }
22
        $this->ldapObj = $this->initializeFromArray($data);
23
    }
24
25
    /**
26
     * Get the data from the server based on the DN
27
     *
28
     * @param string $dn The DN of the LDAP object
29
     *
30
     * @return array The raw LDAP data
31
     */
32
    private function initializeFromDN($dn)
33
    {
34
        $data = $this->server->read($dn, false, true);
35
        if($data === false || !isset($data[0]))
36
        {
37
            return false;
38
        }
39
        return $data[0];
40
    }
41
42
    private function initializeFromArray($array)
43
    {
44
        if(isset($array['extended']))
45
        {
46
            return $array['extended'];
47
        }
48
        //Generic user object
49
        if(isset($array['mail']))
50
        {
51
            $filter = new \Flipside\Data\Filter('mail eq '.$array['mail']);
52
            $users = $this->server->read($this->server->user_base, $filter);
53
            if($users === false || !isset($users[0]))
54
            {
55
                return false;
56
            }
57
            return $users[0];
58
        }
59
        var_dump($array);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($array); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
60
        die();
61
    }
62
63
    protected function update($obj)
64
    {
65
        try
66
        {
67
            return $this->server->update($obj);
68
        }
69
        catch(\Exception $ex)
70
        {
71
            $auth = \Flipside\AuthProvider::getInstance();
72
            $ldap = $auth->getMethodByName('Auth\LDAPAuthenticator');
73
            if($ldap === false)
74
            {
75
                return false;
76
            }
77
            $this->server = $ldap->getAndBindServer(true);
78
            return $this->server->update($obj);
79
        }
80
    }
81
82
    /**
83
     * Get the specified field from the cached object or LDAPObject
84
     *
85
     * @param string $fieldName The name of the field to retrieve
86
     *
87
     * @return mixed string|array the value of the field
88
     */
89
    protected function getField($fieldName)
90
    {
91
        if(!is_object($this->ldapObj))
92
        {
93
            return $this->getFieldLocal($fieldName);
94
        }
95
        return $this->getFieldServer($fieldName);
96
    }
97
98
    /**
99
     * Get the value of the specified field from the cached object or LDAPObject
100
     *
101
     * @param string $fieldName The name of the field to retrieve
102
     *
103
     * @return mixed string the value of the field
104
     */
105
    protected function getFieldSingleValue($fieldName)
106
    {
107
        if(!is_object($this->ldapObj))
108
        {
109
            return $this->getFieldLocalSingleValue($fieldName);
110
        }
111
        return $this->getFieldServerSingleValue($fieldName);
112
    }
113
114
    /**
115
     * Set the value of the specified field in the cached object or LDAPObject
116
     *
117
     * @param string $fieldName The name of the field to set
118
     * @param mixed $fieldValue The value to set in the field
119
     *
120
     * @return boolean true if the field is set and false otherwise
121
     */
122
    protected function setField($fieldName, $fieldValue)
123
    {
124
        if(!is_object($this->ldapObj))
125
        {
126
            return $this->setFieldLocal($fieldName, $fieldValue);
127
        }
128
        return $this->setFieldServer($fieldName, $fieldValue);
129
    }
130
131
    /**
132
     * Append a value of the specified field in the cached object or LDAPObject
133
     *
134
     * @param string $fieldName The name of the field to set
135
     * @param mixed $fieldValue The value to append to the field
136
     *
137
     * @return  boolean true if the field is set and false otherwise
138
     */
139
    protected function appendField($fieldName, $fieldValue)
140
    {
141
        if(!is_object($this->ldapObj))
142
        {
143
            return $this->appendFieldLocal($fieldName, $fieldValue);
144
        }
145
        return $this->appendFieldServer($fieldName, $fieldValue);
146
    }
147
148
    /**
149
     * Get the value of the field in the local cache
150
     *
151
     * @param string $fieldName The name of the field to retrieve
152
     *
153
     * @return mixed the value of the field
154
     */
155
    private function getFieldLocal($fieldName)
156
    {
157
        if($this->ldapObj === false)
158
        {
159
            return false;
160
        }
161
        if(!isset($this->ldapObj[$fieldName]))
162
        {
163
            return false;
164
        }
165
        return $this->ldapObj[$fieldName];
166
    }
167
168
    /**
169
     * Get the value of the field in the server object
170
     *
171
     * @param string $fieldName The name of the field to retrieve
172
     *
173
     * @return mixed the value of the field
174
     */
175
    private function getFieldServer($fieldName)
176
    {
177
        $lowerName = strtolower($fieldName);
178
        if(!isset($this->ldapObj->{$lowerName}))
179
        {
180
            return false;
181
        }
182
        return $this->ldapObj->{$lowerName};
183
    }
184
185
    /**
186
     * Get the value of the specified field from the local cache
187
     *
188
     * @param string $fieldName The name of the field to retrieve
189
     *
190
     * @return string the value of the field
191
     */
192
    private function getFieldLocalSingleValue($fieldName)
193
    {
194
        if($this->ldapObj === false)
195
        {
196
            return false;
197
        }
198
        if(!isset($this->ldapObj[$fieldName]))
199
        {
200
            return false;
201
        }
202
        if(is_array($this->ldapObj[$fieldName]))
203
        {
204
            return $this->ldapObj[$fieldName][0];
205
        }
206
        return $this->ldapObj[$fieldName];
207
    }
208
209
    /**
210
     * Get the value of the specified field from the server
211
     *
212
     * @param string $fieldName The name of the field to retrieve
213
     *
214
     * @return string the value of the field
215
     */
216
    private function getFieldServerSingleValue($fieldName)
217
    {
218
        $lowerName = strtolower($fieldName);
219
        if(!isset($this->ldapObj->{$lowerName}))
220
        {
221
            return false;
222
        }
223
        $field = $this->ldapObj->{$lowerName};
224
        if(!isset($field[0]))
225
        {
226
            return false;
227
        }
228
        return $field[0];
229
    }
230
231
    /**
232
     * Set the specified field in the server
233
     *
234
     * @param string $fieldName The name of the field to set
235
     * @param mixed $fieldValue The value to write to the field
236
     *
237
     * @return boolean true if the field is set and false otherwise
238
     */
239
    private function setFieldServer($fieldName, $fieldValue)
240
    {
241
        $obj = array('dn'=>$this->ldapObj->dn);
242
        if($fieldValue !== null && is_array($fieldValue))
243
        {
244
            $found = false;
245
            foreach($fieldValue as $value)
246
            {
247
                if($value !== null && strlen($value) > 0)
248
                {
249
                    $found = true;
250
                    break;
251
                }
252
            }
253
            if($found)
254
            {
255
                $obj[$fieldName] = $fieldValue;
256
            }
257
        }
258
        else if($fieldValue !== null && strlen($fieldValue) > 0)
259
        {
260
            $obj[$fieldName] = $fieldValue;
261
        }
262
        else
263
        {
264
            $obj[$fieldName] = null;
265
        }
266
        $lowerName = strtolower($fieldName);
267
        if(!is_array($fieldValue))
268
        {
269
            $this->ldapObj->{$lowerName} = array($fieldValue);
270
        }
271
        else
272
        {
273
            $this->ldapObj->{$lowerName} = $fieldValue;
274
        }
275
        return $this->update($obj);
276
    }
277
278
    /**
279
     * Append a value of the specified field in the server
280
     *
281
     * @param string $fieldName The name of the field to set
282
     * @param mixed $fieldValue The value to append to the field
283
     *
284
     * @return  boolean true if the field is set and false otherwise
285
     */
286
    private function appendFieldServer($fieldName, $fieldValue)
287
    {
288
        $obj = array('dn'=>$this->ldapObj->dn);
289
        if(isset($this->ldapObj->{$fieldName}))
290
        {
291
            $obj[$fieldName] = $this->ldapObj->{$fieldName};
292
            $obj[$fieldName][$obj[$fieldName]['count']] = $fieldValue;
293
            $obj[$fieldName]['count']++;
294
        }
295
        else
296
        {
297
            $obj[$fieldName] = $fieldValue;
298
        }
299
        return $this->update($obj);
300
    }
301
302
    /**
303
     * Set the specified field in the local cache
304
     *
305
     * @param string $fieldName The name of the field to set
306
     * @param mixed $fieldValue The value to write to the field
307
     *
308
     * @return boolean true if the field is set and false otherwise
309
     */
310
    private function setFieldLocal($fieldName, $fieldValue)
311
    {
312
        if($this->ldapObj === false)
313
        {
314
            $this->ldapObj = array();
315
        }
316
        if($fieldValue === null || (is_string($fieldValue) && strlen($fieldValue) === 0))
317
        {
318
            if(isset($this->ldapObj[$fieldName]))
319
            {
320
                unset($this->ldapObj[$fieldName]);
321
            }
322
            return true;
323
        }
324
        $this->ldapObj[$fieldName] = $fieldValue;
325
        return true;
326
    }
327
328
    /**
329
     * Append a value of the specified field in the local cache
330
     *
331
     * @param string $fieldName The name of the field to set
332
     * @param mixed $fieldValue The value to append to the field
333
     *
334
     * @return  boolean true if the field is set and false otherwise
335
     */
336
    private function appendFieldLocal($fieldName, $fieldValue)
337
    {
338
        if($this->ldapObj === false)
339
        {
340
            $this->ldapObj = array();
341
        }
342
        if(!isset($this->ldapObj[$fieldName]))
343
        {
344
            $this->ldapObj[$fieldName] = array();
345
        }
346
        $this->ldapObj[$fieldName][] = $fieldValue;
347
        return true;
348
    }
349
}
350
351
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
352