1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * -------------------------------------------- * |
---|
6 | * This program is free software; you can redistribute it and/or modify it * |
---|
7 | * under the terms of the GNU General Public License as published by the * |
---|
8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
9 | * option) any later version. * |
---|
10 | \**************************************************************************/ |
---|
11 | |
---|
12 | /** |
---|
13 | * Implements common LDAP methods |
---|
14 | * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com |
---|
15 | * @version 1.0 |
---|
16 | * @package Workflow |
---|
17 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
18 | */ |
---|
19 | class WorkflowLDAP |
---|
20 | { |
---|
21 | /** |
---|
22 | * @var resource $dataSource Recurso de conexão com o LDAP |
---|
23 | * @access private |
---|
24 | */ |
---|
25 | private $dataSource; |
---|
26 | |
---|
27 | /** |
---|
28 | * @var string $userContext Contexto do usuário |
---|
29 | * @access private |
---|
30 | */ |
---|
31 | private $userContext; |
---|
32 | |
---|
33 | /** |
---|
34 | * @var string $groupContext Contexto do grupo |
---|
35 | * @access private |
---|
36 | */ |
---|
37 | private $groupContext; |
---|
38 | |
---|
39 | /** |
---|
40 | * @var string $ldapContext Contexto do LDAP |
---|
41 | * @access private |
---|
42 | */ |
---|
43 | private $ldapContext; |
---|
44 | |
---|
45 | /** |
---|
46 | * Construtor da classe |
---|
47 | * @return object |
---|
48 | * @access public |
---|
49 | */ |
---|
50 | function WorkflowLDAP($useCCParams = false) |
---|
51 | { |
---|
52 | $this->dataSource =& Factory::getInstance('WorkflowObjects')->getLDAP($useCCParams); |
---|
53 | |
---|
54 | /* get the required parameters */ |
---|
55 | $info = (isset($GLOBALS['phpgw_info']['server']['ldap_context'])) ? |
---|
56 | $GLOBALS['phpgw_info']['server'] : |
---|
57 | $_SESSION['phpgw_info']['workflow']['server']; |
---|
58 | $ldapConfigValues = galaxia_get_config_values(array('ldap_user_context' => '', 'ldap_group_context' => '')); |
---|
59 | if (empty($ldapConfigValues['ldap_user_context'])) |
---|
60 | $ldapConfigValues['ldap_user_context'] = $info['ldap_context']; |
---|
61 | if (empty($ldapConfigValues['ldap_group_context'])) |
---|
62 | $ldapConfigValues['ldap_group_context'] = $info['ldap_group_context']; |
---|
63 | $this->userContext = $ldapConfigValues['ldap_user_context']; |
---|
64 | $this->groupContext = $ldapConfigValues['ldap_group_context']; |
---|
65 | $this->ldapContext = $ldapConfigValues['ldap_user_context']; |
---|
66 | |
---|
67 | $this->cache = array( |
---|
68 | 'getEntities' => array(), |
---|
69 | 'getUserGroups' => array(), |
---|
70 | 'getGroupUsers' => array(), |
---|
71 | 'getOrganizations' => array(), |
---|
72 | 'getNames' => array() |
---|
73 | ); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Checa se um método, e seus parâmetros, já estãoe em cache |
---|
78 | * @param string $methodName O nome do método que se quer verificar |
---|
79 | * @param string $parameters Parâmetros passados para o método (serializados) |
---|
80 | * @return bool true se foi encontrada uma versão em cache dos dados solicitados e false caso contrário |
---|
81 | * @access private |
---|
82 | */ |
---|
83 | private function checkCache($methodName, $parameters) |
---|
84 | { |
---|
85 | return isset($this->cache[$methodName][$parameters]); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Pega a informação, em cache, de um método e seus parâmetros |
---|
90 | * @param string $methodName O nome do método que está em cache |
---|
91 | * @param string $parameters Parâmetros passados para o método (serializados) |
---|
92 | * @return mixed O retorno do método usando os parâmetros passados |
---|
93 | * @access private |
---|
94 | */ |
---|
95 | private function getCache($methodName, $parameters) |
---|
96 | { |
---|
97 | return $this->cache[$methodName][$parameters]; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Coloca em cache um método e seus parâmetros |
---|
102 | * @param string $methodName O nome do método |
---|
103 | * @param string $parameters Parâmetros passados para o método (serializados) |
---|
104 | * @param mixed $output Saída do método usando os parâmetros passados |
---|
105 | * @return mixed A saída igual ao parâmetro $output |
---|
106 | * @access private |
---|
107 | */ |
---|
108 | private function setCache($methodName, $parameters, $output) |
---|
109 | { |
---|
110 | $this->cache[$methodName][$parameters] = $output; |
---|
111 | return $output; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Faz consultas ao LDAP |
---|
116 | * @param string $context Contexto das entidades |
---|
117 | * @param string $filter Filtro utilizado para selecionar as entidades desejadas |
---|
118 | * @param array $elements Array dos campos que se deseja obter |
---|
119 | * @param bool $depthSearch Indica se a busca deve incluir sub-árvores ou não |
---|
120 | * @param string $elementSort O elemento pelo qual se quer ordenar o resultado |
---|
121 | * @return mixed Array do resultado. Ou false em caso de erro |
---|
122 | * @access private |
---|
123 | */ |
---|
124 | private function runLDAP($context, $filter, $elements, $depthSearch = false, $elementSort = null) |
---|
125 | { |
---|
126 | if ($depthSearch) |
---|
127 | $resourceIdentifier = ldap_search($this->dataSource, $context, $filter, $elements); |
---|
128 | else |
---|
129 | $resourceIdentifier = ldap_list($this->dataSource, $context, $filter, $elements); |
---|
130 | |
---|
131 | if (!$resourceIdentifier) |
---|
132 | return false; |
---|
133 | |
---|
134 | if (!is_null($elementSort)) |
---|
135 | ldap_sort($this->dataSource, $resourceIdentifier, $elementSort); |
---|
136 | |
---|
137 | $output = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
138 | return $output; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Faz consultas ao LDAP (inclusive com campos binários) |
---|
143 | * @param string $context Contexto das entidades |
---|
144 | * @param string $filter Filtro utilizado para selecionar as entidades desejadas |
---|
145 | * @param array $elements Array dos campos que se deseja obter |
---|
146 | * @param bool $depthSearch Indica se a busca deve incluir sub-árvores ou não |
---|
147 | * @param string $elementSort O elemento pelo qual se quer ordenar o resultado |
---|
148 | * @return mixed Array do resultado. Ou false em caso de erro |
---|
149 | * @access private |
---|
150 | */ |
---|
151 | private function runBinaryLDAP($context, $filter, $elements, $depthSearch = false, $elementSort = null) |
---|
152 | { |
---|
153 | if ($depthSearch) |
---|
154 | $resourceIdentifier = ldap_search($this->dataSource, $context, $filter, $elements); |
---|
155 | else |
---|
156 | $resourceIdentifier = ldap_list($this->dataSource, $context, $filter, $elements); |
---|
157 | |
---|
158 | if (!$resourceIdentifier) |
---|
159 | return false; |
---|
160 | |
---|
161 | if (!is_null($elementSort)) |
---|
162 | ldap_sort($this->dataSource, $resourceIdentifier, $elementSort); |
---|
163 | |
---|
164 | $entry = ldap_first_entry($this->dataSource, $resourceIdentifier); |
---|
165 | if (!$entry) |
---|
166 | return array(); |
---|
167 | |
---|
168 | $output = array(); |
---|
169 | $counter = 0; |
---|
170 | do |
---|
171 | { |
---|
172 | $attributes = ldap_get_attributes($this->dataSource, $entry); |
---|
173 | for ($i = 0; $i < $attributes['count']; ++$i) |
---|
174 | $output[$counter][$attributes[$i]] = ldap_get_values_len($this->dataSource, $entry, $attributes[$i]); |
---|
175 | |
---|
176 | ++$counter; |
---|
177 | } while ($entry = ldap_next_entry($this->dataSource, $entry)); |
---|
178 | |
---|
179 | return $output; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Busca entidades (usuários, grupos ou listas públicas) do LDAP |
---|
184 | * @param string $context Contexto das entidades |
---|
185 | * @param string $filter Filtro utilizado para selecionar as entidades desejadas |
---|
186 | * @param array $elements Array dos campos que representa, nesta ordem: o ID, o nome e o e-mail da entidade |
---|
187 | * @param char $type O tipo da entidade: 'u' para usuários; 'g' para grupos; e 'l' para listas públicas |
---|
188 | * @param bool $depthSearch Indica se a busca deve incluir sub-árvores ou não |
---|
189 | * @return array Array das entidades |
---|
190 | * @access private |
---|
191 | */ |
---|
192 | private function getEntities($context, $filter, $elements, $type, $depthSearch = false) |
---|
193 | { |
---|
194 | /* check if the required information is in cache */ |
---|
195 | $methodName = 'getEntities'; |
---|
196 | $parameters = serialize(func_get_args()); |
---|
197 | if ($this->checkCache($methodName, $parameters)) |
---|
198 | return $this->getCache($methodName, $parameters); |
---|
199 | |
---|
200 | $result = $this->runLDAP($context, $filter, $elements, $depthSearch, $elements[1]); |
---|
201 | |
---|
202 | $output = array(); |
---|
203 | for ($i = 0; $i < $result['count']; ++$i) |
---|
204 | { |
---|
205 | $output[] = array( |
---|
206 | 'id' => $result[$i][$elements[0]][0], |
---|
207 | 'name' => $result[$i][$elements[1]][0], |
---|
208 | 'type' => $type, |
---|
209 | 'mail' => $result[$i][$elements[2]][0]); |
---|
210 | } |
---|
211 | |
---|
212 | /* store the information in cache and return the output */ |
---|
213 | return $this->setCache($methodName, $parameters, $output); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Retorna os grupos de um usuário |
---|
218 | * @param int $userID O ID do usuário |
---|
219 | * @return array Array dos grupos |
---|
220 | * @access public |
---|
221 | */ |
---|
222 | function getUserGroups($userID) |
---|
223 | { |
---|
224 | /* do not perform any search if the user is '*' */ |
---|
225 | if ($userID == '*') |
---|
226 | return array(); |
---|
227 | |
---|
228 | /* check if the required information is in cache */ |
---|
229 | $methodName = 'getUserGroups'; |
---|
230 | $parameters = serialize(func_get_args()); |
---|
231 | if ($this->checkCache($methodName, $parameters)) |
---|
232 | return $this->getCache($methodName, $parameters); |
---|
233 | |
---|
234 | /* check for error in connection */ |
---|
235 | if (!$this->dataSource) |
---|
236 | return false; |
---|
237 | |
---|
238 | /* first, get the UID */ |
---|
239 | $resourceIdentifier = ldap_search($this->dataSource, $this->userContext, "(&(phpgwaccounttype=u)(uidnumber={$userID}))", array('uid')); |
---|
240 | $result = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
241 | if (!isset($result[0]['uid'][0])) |
---|
242 | return false; |
---|
243 | $userLogin = $result[0]['uid'][0]; |
---|
244 | |
---|
245 | /* initialize some variables */ |
---|
246 | $output = array(); |
---|
247 | |
---|
248 | /* search the LDAP tree */ |
---|
249 | $resourceIdentifier = ldap_search($this->dataSource, $this->groupContext, "(&(phpgwaccounttype=g)(memberuid={$userLogin}))", array('gidnumber')); |
---|
250 | $result = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
251 | for ($i = 0; $i < $result['count']; ++$i) |
---|
252 | $output[] = $result[$i]['gidnumber'][0]; |
---|
253 | |
---|
254 | /* store the information in cache and return the output */ |
---|
255 | return $this->setCache($methodName, $parameters, $output); |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Retorna os usuários de um grupo |
---|
260 | * @param int $groupID O ID do grupo |
---|
261 | * @return array Array dos usuários |
---|
262 | * @access public |
---|
263 | */ |
---|
264 | function getGroupUsers($groupID) |
---|
265 | { |
---|
266 | /* check if the required information is in cache */ |
---|
267 | $methodName = 'getGroupUsers'; |
---|
268 | $parameters = serialize(func_get_args()); |
---|
269 | if ($this->checkCache($methodName, $parameters)) |
---|
270 | return $this->getCache($methodName, $parameters); |
---|
271 | |
---|
272 | /* check for error in connection */ |
---|
273 | if (!$this->dataSource) |
---|
274 | return false; |
---|
275 | |
---|
276 | /* first, we get the UIDs that are members of the group */ |
---|
277 | $resourceIdentifier = ldap_search($this->dataSource, $this->ldapContext, "(&(phpgwaccounttype=g)(gidnumber={$groupID}))", array('memberuid')); |
---|
278 | $result = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
279 | if (!isset($result[0]['memberuid'][0])) |
---|
280 | return false; |
---|
281 | |
---|
282 | $userLogins = $result[0]['memberuid']; |
---|
283 | unset($userLogins['count']); |
---|
284 | |
---|
285 | /* load the user information ten users at a time. This approach was proven to be faster on some systems */ |
---|
286 | $result = array(); |
---|
287 | while (count($userLogins) > 0) |
---|
288 | { |
---|
289 | $selectedUserLogins = array_splice($userLogins, 0, 10); |
---|
290 | $resourceIdentifier = ldap_search($this->dataSource, $this->ldapContext, "(&(phpgwaccounttype=u)(|" . implode('', array_map(create_function('$a', 'return "(uid={$a})";'), $selectedUserLogins)) . "))", array('cn', 'uidnumber')); |
---|
291 | $result = array_merge($result, ldap_get_entries($this->dataSource, $resourceIdentifier)); |
---|
292 | unset($result['count']); |
---|
293 | } |
---|
294 | |
---|
295 | $output = array(); |
---|
296 | $userCount = count($result); |
---|
297 | for ($i = 0; $i < $userCount; ++$i) |
---|
298 | $output[] = array( |
---|
299 | 'account_id' => $result[$i]['uidnumber'][0], |
---|
300 | 'account_name' => $result[$i]['cn'][0]); |
---|
301 | |
---|
302 | /* sort the result */ |
---|
303 | usort($output, create_function('$a,$b', 'return strcasecmp($a[\'account_name\'],$b[\'account_name\']);')); |
---|
304 | |
---|
305 | /* store the information in cache and return the output */ |
---|
306 | return $this->setCache($methodName, $parameters, $output); |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * Retorna as organizações do nível raiz |
---|
311 | * @return array Array de organizações |
---|
312 | * @access public |
---|
313 | */ |
---|
314 | function getOrganizations() |
---|
315 | { |
---|
316 | /* check if the required information is in cache */ |
---|
317 | $methodName = 'getOrganizations'; |
---|
318 | $parameters = serialize(func_get_args()); |
---|
319 | if ($this->checkCache($methodName, $parameters)) |
---|
320 | return $this->getCache($methodName, $parameters); |
---|
321 | |
---|
322 | /* check for error in connection */ |
---|
323 | if (!$this->dataSource) |
---|
324 | return false; |
---|
325 | |
---|
326 | /* load the sectors of level 0 as organizations, then format the data */ |
---|
327 | $output = array_map(create_function('$a', 'return $a[\'ou\'];'), $this->getSectors()); |
---|
328 | |
---|
329 | /* store the information in cache and return the output */ |
---|
330 | return $this->setCache($methodName, $parameters, $output); |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * Retorna os setores de um determinado contexto |
---|
335 | * @param string $organization A organização a partir da qual a busca deve inciar. Utilize null para o nível raiz |
---|
336 | * @param bool $recursive Indica se a busca deve ser recursiva ou não. true indica que deve ser recursiva e false que não deve ser recursiva |
---|
337 | * @param string $contextBase A base do contexto. Utilize null para o contexto padrão |
---|
338 | * @param int $level Indica o nível atual de recursão |
---|
339 | * @return array Array de setores |
---|
340 | * @access public |
---|
341 | */ |
---|
342 | function getSectors($organization = null, $recursive = false, $contextBase = null, $level = 0) |
---|
343 | { |
---|
344 | /* determines the context in which the search will be performed */ |
---|
345 | $context = (!is_null($contextBase) ? $contextBase : $this->ldapContext); |
---|
346 | $context = (!is_null($organization) ? 'ou=' . $organization . ',' : '') . $context; |
---|
347 | |
---|
348 | /* search for the sectors */ |
---|
349 | $resourceIdentifier = @ldap_list($this->dataSource, $context, '(objectClass=organizationalUnit)', array('ou')); |
---|
350 | if ($resourceIdentifier === false) |
---|
351 | return false; |
---|
352 | |
---|
353 | ldap_sort($this->dataSource, $resourceIdentifier, 'ou'); |
---|
354 | $result = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
355 | |
---|
356 | /* collect the data */ |
---|
357 | $output = array(); |
---|
358 | for ($i = 0; $i < $result['count']; ++$i) |
---|
359 | { |
---|
360 | $output[] = array( |
---|
361 | 'dn' => $result[$i]['dn'], |
---|
362 | 'ou' => $result[$i]['ou'][0], |
---|
363 | 'level' => $level); |
---|
364 | |
---|
365 | /* if requested, perform a recursive search */ |
---|
366 | if ($recursive) |
---|
367 | $output = array_merge($output, $this->getSectors($result[$i]['ou'][0], $recursive, $context, $level + 1)); |
---|
368 | } |
---|
369 | |
---|
370 | return $output; |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * Retorna os usuários de um determinado contexto |
---|
375 | * @param string $context O contexto a partir do qual a busca deve começar |
---|
376 | * @return array Array dos usuários encontrados |
---|
377 | * @access public |
---|
378 | */ |
---|
379 | function getUsers($context, $onlyVisibleAccounts = true) |
---|
380 | { |
---|
381 | $filter = '(phpgwaccounttype=u)'; |
---|
382 | if($onlyVisibleAccounts) |
---|
383 | $filter = '(&' . $filter . '(!(phpgwAccountVisible=-1)))'; |
---|
384 | |
---|
385 | $elements = array('uidnumber', 'cn', 'mail'); |
---|
386 | return $this->getEntities($context, $filter, $elements, 'u', false); |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * Retorna os grupos de um determinado contexto |
---|
391 | * @param string $context O contexto a partir do qual a busca deve começar |
---|
392 | * @return array Array dos grupos encontrados |
---|
393 | * @access public |
---|
394 | */ |
---|
395 | function getGroups($context) |
---|
396 | { |
---|
397 | $filter = '(phpgwaccounttype=g)'; |
---|
398 | $elements = array('gidnumber', 'cn', 'mail'); |
---|
399 | return $this->getEntities($context, $filter, $elements, 'g', false); |
---|
400 | } |
---|
401 | |
---|
402 | /** |
---|
403 | * Retorna listas públicas de um determinado contexto |
---|
404 | * @param string $context O contexto a partir do qual a busca deve começar |
---|
405 | * @return array Array das listas públicas encontradas |
---|
406 | * @access public |
---|
407 | */ |
---|
408 | function getPublicLists($context) |
---|
409 | { |
---|
410 | $filter = '(phpgwaccounttype=l)'; |
---|
411 | $elements = array('uidnumber', 'cn', 'mail'); |
---|
412 | return $this->getEntities($context, $filter, $elements, 'l', false); |
---|
413 | } |
---|
414 | |
---|
415 | /** |
---|
416 | * Retornar informação sobre um usuário |
---|
417 | * @param int $userID O ID do usuário |
---|
418 | * @return mixed Array contento informação sobre um usuário ou false se o usuário não for encontrado |
---|
419 | * @access public |
---|
420 | */ |
---|
421 | function getUserInfo($userID) |
---|
422 | { |
---|
423 | $filter = '(&(phpgwaccounttype=u)(uidnumber=' . $userID . '))'; |
---|
424 | $elements = array('uidnumber', 'cn', 'mail'); |
---|
425 | $output = $this->getEntities($this->ldapContext, $filter, $elements, 'u', true); |
---|
426 | if (count($output) == 1) |
---|
427 | return $output[0]; |
---|
428 | else |
---|
429 | return false; |
---|
430 | } |
---|
431 | |
---|
432 | function getUserPicture($userID) |
---|
433 | { |
---|
434 | $userID = (int) $userID; |
---|
435 | $filter = '(&(phpgwaccounttype=u)(uidnumber=' . $userID . '))'; |
---|
436 | $elements = array('jpegPhoto'); |
---|
437 | $result = $this->runBinaryLDAP($this->ldapContext, $filter, $elements, true); |
---|
438 | |
---|
439 | if (isset($result[0]['jpegPhoto'][0])) |
---|
440 | return $result[0]['jpegPhoto'][0]; |
---|
441 | |
---|
442 | return false; |
---|
443 | } |
---|
444 | |
---|
445 | /** |
---|
446 | * Retrieves information about a group |
---|
447 | * @param int $userID The ID of the group |
---|
448 | * @return mixed Array containing information about a group or false if the group is not found |
---|
449 | * @access public |
---|
450 | */ |
---|
451 | function getGroupInfo($groupID) |
---|
452 | { |
---|
453 | $filter = '(&(phpgwaccounttype=g)(gidnumber=' . $groupID . '))'; |
---|
454 | $elements = array('gidnumber', 'cn', 'mail'); |
---|
455 | $output = $this->getEntities($this->ldapContext, $filter, $elements, 'g', true); |
---|
456 | if (count($output) == 1) |
---|
457 | return $output[0]; |
---|
458 | else |
---|
459 | return false; |
---|
460 | } |
---|
461 | |
---|
462 | /** |
---|
463 | * Retorna o tipo de uma entidade |
---|
464 | * @param int $entityID O ID da entidade |
---|
465 | * @return string O tipo da entidade ('g' para grupo e 'u' para usuário) |
---|
466 | * @access public |
---|
467 | */ |
---|
468 | function getEntityType($entityID) |
---|
469 | { |
---|
470 | if ($entityID == '*') |
---|
471 | return false; |
---|
472 | |
---|
473 | $output = $this->getUserInfo($entityID); |
---|
474 | if (is_array($output)) |
---|
475 | return 'u'; |
---|
476 | else |
---|
477 | { |
---|
478 | $output = $this->getGroupInfo($entityID); |
---|
479 | if (is_array($output)) |
---|
480 | return 'g'; |
---|
481 | } |
---|
482 | |
---|
483 | return false; |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | * Retorna o nome de uma entidade |
---|
488 | * @param int $entityID O ID da entidade |
---|
489 | * @return string O nome da entidade |
---|
490 | * @access public |
---|
491 | */ |
---|
492 | function getName($entityID) |
---|
493 | { |
---|
494 | if ($entityID == '*') |
---|
495 | return '*'; |
---|
496 | $output = $this->getUserInfo($entityID); |
---|
497 | if (is_array($output)) |
---|
498 | return $output['name']; |
---|
499 | else |
---|
500 | { |
---|
501 | $output = $this->getGroupInfo($entityID); |
---|
502 | if (is_array($output)) |
---|
503 | return $output['name']; |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | /** |
---|
508 | * Retorna os nomes de uma entidade |
---|
509 | * @param array $entitiesID Os IDs das entidades |
---|
510 | * @return string Os nomes das entidades |
---|
511 | * @access public |
---|
512 | */ |
---|
513 | function getNames($entitiesID) |
---|
514 | { |
---|
515 | |
---|
516 | /* if parameter is not array make a new array with value in entitiesID */ |
---|
517 | if(!is_array($entitiesID)) |
---|
518 | $entitiesID = array($entitiesID); |
---|
519 | |
---|
520 | /* check if the required information is in cache */ |
---|
521 | $methodName = 'getNames'; |
---|
522 | $parameters = serialize(func_get_args()); |
---|
523 | if ($this->checkCache($methodName, $parameters)) |
---|
524 | return $this->getCache($methodName, $parameters); |
---|
525 | |
---|
526 | /* check for error in connection */ |
---|
527 | if (!$this->dataSource) |
---|
528 | return false; |
---|
529 | |
---|
530 | /* check for '*' */ |
---|
531 | $asteriskIndex = array_search('*', $entitiesID); |
---|
532 | if ($asteriskIndex !== false) |
---|
533 | unset($entitiesID[$asteriskIndex]); |
---|
534 | |
---|
535 | /* load the entity information ten entities at a time. This approach was proven to be faster on some systems */ |
---|
536 | $result = array(); |
---|
537 | |
---|
538 | while (count($entitiesID) > 0) |
---|
539 | { |
---|
540 | $selectedEntitiesID = array_splice($entitiesID, 0, 10); |
---|
541 | |
---|
542 | /* if parameter is null make array without values */ |
---|
543 | if(!is_array($entitiesID)) |
---|
544 | $entitiesID = array(); |
---|
545 | |
---|
546 | // Search for all entries that uidnumber or gidnumber matches the arguments |
---|
547 | // and that account type is user, list or group |
---|
548 | $resourceIdentifier = ldap_search( |
---|
549 | $this->dataSource, |
---|
550 | $this->ldapContext, |
---|
551 | sprintf( |
---|
552 | '(&(|%s)(|(phpgwaccounttype=u)(phpgwaccounttype=l)(phpgwaccounttype=g)))', |
---|
553 | implode( |
---|
554 | '', |
---|
555 | array_map( |
---|
556 | create_function('$a', 'return "(uidnumber={$a})(&(gidnumber={$a})(phpgwaccounttype=g))";'), |
---|
557 | array_unique($selectedEntitiesID) |
---|
558 | ) |
---|
559 | ) |
---|
560 | ), |
---|
561 | array('cn', 'uidnumber', 'gidnumber', 'phpgwaccounttype') |
---|
562 | ); |
---|
563 | |
---|
564 | $entries = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
565 | if (is_array($entries)) { |
---|
566 | $result = array_merge($result, $entries); |
---|
567 | } |
---|
568 | |
---|
569 | unset($result['count']); |
---|
570 | } |
---|
571 | |
---|
572 | $output = array(); |
---|
573 | $entityCount = count($result); |
---|
574 | for ($i = 0; $i < $entityCount; ++$i) |
---|
575 | $output[] = array( |
---|
576 | 'id' => ($result[$i]['phpgwaccounttype'][0] == 'g') ? $result[$i]['gidnumber'][0] : $result[$i]['uidnumber'][0], |
---|
577 | 'name' => $result[$i]['cn'][0]); |
---|
578 | |
---|
579 | if ($asteriskIndex !== false) |
---|
580 | $output[] = array('id' => '*', 'name' => '*'); |
---|
581 | |
---|
582 | /* sort the result */ |
---|
583 | usort($output, create_function('$a,$b', 'return strcasecmp($a[\'name\'],$b[\'name\']);')); |
---|
584 | |
---|
585 | /* store the information in cache and return the output */ |
---|
586 | return $this->setCache($methodName, $parameters, $output); |
---|
587 | } |
---|
588 | |
---|
589 | /** |
---|
590 | * Faz uma busca (em todo o catálogo) por um nome |
---|
591 | * @param string $searchTerm O termo que está sendo procurado (pode conter '*') |
---|
592 | * @param bool $includeUsers Indica se na busca devem ser incluídos os registros referentes a usuários (true por padrão) |
---|
593 | * @param bool $includeGroups Indica se na busca devem ser incluídos os registros referentes a grupos (false por padrão) |
---|
594 | * @param bool $includeLists Indica se na busca devem ser incluídos os registros referentes a listas (false por padrão) |
---|
595 | * @return array Uma array contendo os registros encontrados |
---|
596 | * @access public |
---|
597 | */ |
---|
598 | function search($searchTerm, $includeUsers = true, $includeGroups = false, $includeLists = false, $context = null, $onlyVisibleAccounts = true) |
---|
599 | { |
---|
600 | if (!($includeUsers || $includeGroups || $includeLists)) |
---|
601 | return false; |
---|
602 | |
---|
603 | if (is_null($context)) |
---|
604 | $context = $this->ldapContext; |
---|
605 | |
---|
606 | /* check for error in connection */ |
---|
607 | if (!$this->dataSource) |
---|
608 | return false; |
---|
609 | |
---|
610 | $entityFilter = array(); |
---|
611 | if ($includeUsers) |
---|
612 | $entityFilter[] = '(phpgwaccounttype=u)'; |
---|
613 | if ($includeGroups) |
---|
614 | $entityFilter[] = '(phpgwaccounttype=g)'; |
---|
615 | if ($includeLists) |
---|
616 | $entityFilter[] = '(phpgwaccounttype=l)'; |
---|
617 | |
---|
618 | if (count($entityFilter) > 1) |
---|
619 | $entityFilter = '(|' . implode('', $entityFilter) . ')'; |
---|
620 | else |
---|
621 | $entityFilter = $entityFilter[0]; |
---|
622 | |
---|
623 | if($onlyVisibleAccounts) |
---|
624 | $filter = "(&{$entityFilter}(cn={$searchTerm})(!(phpgwAccountVisible=-1)))"; |
---|
625 | else |
---|
626 | $filter = "(&{$entityFilter}(cn={$searchTerm}))"; |
---|
627 | |
---|
628 | $resourceIdentifier = ldap_search($this->dataSource, $context, $filter, array('cn', 'uidnumber', 'gidnumber', 'phpgwaccounttype', 'mail')); |
---|
629 | ldap_sort($this->dataSource, $resourceIdentifier, 'cn'); |
---|
630 | $result = ldap_get_entries($this->dataSource, $resourceIdentifier); |
---|
631 | |
---|
632 | $output = array(); |
---|
633 | for ($i = 0; $i < $result['count']; ++$i) |
---|
634 | $output[] = array( |
---|
635 | 'id' => ($result[$i]['phpgwaccounttype'][0] == 'g') ? $result[$i]['gidnumber'][0] : $result[$i]['uidnumber'][0], |
---|
636 | 'name' => $result[$i]['cn'][0], |
---|
637 | 'mail' => $result[$i]['mail'][0], |
---|
638 | 'type' => $result[$i]['phpgwaccounttype'][0], |
---|
639 | 'dn' => $result[$i]['dn'] |
---|
640 | ); |
---|
641 | |
---|
642 | /* store the information in cache and return the output */ |
---|
643 | return $output; |
---|
644 | } |
---|
645 | |
---|
646 | /** |
---|
647 | * Retorna o contexto de usuários do LDAP |
---|
648 | * @return string O contexto de usuários do LDAP |
---|
649 | * @access public |
---|
650 | */ |
---|
651 | function getUserContext() |
---|
652 | { |
---|
653 | return $this->userContext; |
---|
654 | } |
---|
655 | |
---|
656 | /** |
---|
657 | * Retorna o contexto de grupos do LDAP |
---|
658 | * @return string O contexto de grupos do LDAP |
---|
659 | * @access public |
---|
660 | */ |
---|
661 | function getGroupContext() |
---|
662 | { |
---|
663 | return $this->groupContext; |
---|
664 | } |
---|
665 | |
---|
666 | /** |
---|
667 | * Retorna o contexto do LDAP |
---|
668 | * @return string O contexto do LDAP |
---|
669 | * @access public |
---|
670 | */ |
---|
671 | function getLDAPContext() |
---|
672 | { |
---|
673 | return $this->ldapContext; |
---|
674 | } |
---|
675 | |
---|
676 | /** |
---|
677 | * Retorna a organização de um DN |
---|
678 | * @param mixed $DN O DN do usuário |
---|
679 | * @return mixed Uma string contendo a organização ou false caso não seja achada a organização |
---|
680 | * @access public |
---|
681 | */ |
---|
682 | public function getOrganizationFromDN($DN) |
---|
683 | { |
---|
684 | $userContext = str_replace(' ', '', $this->userContext); |
---|
685 | $DN = str_replace(' ', '', $DN); |
---|
686 | $userInfo = array_reverse(explode(',', substr($DN, 0, - (strlen($userContext) + 1)))); |
---|
687 | foreach ($userInfo as $attributePair) |
---|
688 | { |
---|
689 | list($name, $value) = explode('=', $attributePair, 2); |
---|
690 | if ($name === 'ou') |
---|
691 | return $value; |
---|
692 | } |
---|
693 | |
---|
694 | return false; |
---|
695 | } |
---|
696 | } |
---|
697 | ?> |
---|