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 | require_once 'common.inc.php'; |
---|
13 | require_once 'class.bo_ajaxinterface.inc.php'; |
---|
14 | |
---|
15 | /** |
---|
16 | * @package Workflow |
---|
17 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
18 | * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com |
---|
19 | * @author Rodrigo Daniel C de Lira - rodrigo.lira@gmail.com |
---|
20 | */ |
---|
21 | class bo_participants extends bo_ajaxinterface |
---|
22 | { |
---|
23 | /** |
---|
24 | * @var resource $ldap Conexão com o LDAP |
---|
25 | * @access public |
---|
26 | */ |
---|
27 | var $ldap; |
---|
28 | |
---|
29 | /** |
---|
30 | * Construtor da classe bo_participants |
---|
31 | * @return object |
---|
32 | * @access public |
---|
33 | */ |
---|
34 | function bo_participants() |
---|
35 | { |
---|
36 | $useCCParams = (isset($_REQUEST['useCCParams']) && $_REQUEST['useCCParams'] !== 'false') ? (bool)$_REQUEST['useCCParams'] : false; |
---|
37 | |
---|
38 | $this->ldap = &Factory::getInstance('WorkflowLDAP', $useCCParams); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Busca as organizações do LDAP |
---|
43 | * @return array A lista de organizações |
---|
44 | * @access public |
---|
45 | */ |
---|
46 | function getOrganizations() |
---|
47 | { |
---|
48 | return $this->ldap->getOrganizations(); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Busca as entidades (usuários, grupos ou listas públicas) de uma organização |
---|
53 | * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax) |
---|
54 | * @param bool $raw Se false, indica que os dados devem ser retornados em código HTML (via Ajax), se true indica que os dados devem ser retornados em forma de array |
---|
55 | * @return mixed A lista de entidades em formato de array ou em formato de string HTML para construção de combo box |
---|
56 | * @access public |
---|
57 | */ |
---|
58 | function getEntities($params, $raw = false) |
---|
59 | { |
---|
60 | if (preg_match('/^[a-z0-9_\- =,]+$/i', $params['context']) < 1) |
---|
61 | { |
---|
62 | if ($raw) |
---|
63 | return array(); |
---|
64 | else |
---|
65 | return ''; |
---|
66 | } |
---|
67 | |
---|
68 | if($params['id'] === 'mail') |
---|
69 | { |
---|
70 | $id = 'mail'; |
---|
71 | $usePreffix = false; |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | $id = 'id'; |
---|
76 | $usePreffix = ($params['usePreffix'] == true) ? true : false; |
---|
77 | } |
---|
78 | |
---|
79 | $output = array(); |
---|
80 | $entities = $params['entities']; |
---|
81 | |
---|
82 | /* se requisitado, carrega os usuários */ |
---|
83 | if (strpos($entities, 'u') !== false) |
---|
84 | { |
---|
85 | $preffix = ($usePreffix) ? 'u' : ''; |
---|
86 | $ents = $this->ldap->getUsers($params['context'], $params['onlyVisibleAccounts']); |
---|
87 | foreach ($ents as $ent) |
---|
88 | $output[$preffix . $ent[$id]] = $ent['name']; |
---|
89 | } |
---|
90 | |
---|
91 | /* se requisitado, carrega os grupos */ |
---|
92 | if (strpos($entities, 'g') !== false) |
---|
93 | { |
---|
94 | $preffix = ($usePreffix) ? 'g' : ''; |
---|
95 | $ents = $this->ldap->getGroups($params['context']); |
---|
96 | foreach ($ents as $ent) |
---|
97 | $output[$preffix . $ent[$id]] = $ent['name']; |
---|
98 | } |
---|
99 | |
---|
100 | /* se requisitado, carrega as listas públicas */ |
---|
101 | if (strpos($entities, 'l') !== false) |
---|
102 | { |
---|
103 | $preffix = ($usePreffix) ? 'l' : ''; |
---|
104 | $ents = $this->ldap->getPublicLists($params['context']); |
---|
105 | foreach ($ents as $ent) |
---|
106 | $output[$preffix . $ent[$id]] = $ent['name']; |
---|
107 | } |
---|
108 | |
---|
109 | if (!$raw) |
---|
110 | $output = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($output), array_values($output))); |
---|
111 | |
---|
112 | return $output; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Busca os setores de um dado contexto |
---|
117 | * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax) |
---|
118 | * @param bool $raw Se false, indica que os dados devem ser retornados em código HTML (via Ajax), se true indica que os dados devem ser retornados em forma de array |
---|
119 | * @return mixed A lista de setores em formato de array ou em formato de string HTML para construção de combo box |
---|
120 | * @access public |
---|
121 | */ |
---|
122 | function getSectors($params, $raw = false) |
---|
123 | { |
---|
124 | if (preg_match('/^[a-z0-9_\- ]+$/i', $params['organization']) < 1) |
---|
125 | { |
---|
126 | if ($raw) |
---|
127 | return array(); |
---|
128 | else |
---|
129 | return array('sectors' => '', 'participants' => ''); |
---|
130 | } |
---|
131 | |
---|
132 | $output = array('ou=' . $params['organization'] . ',' . $this->ldap->getLDAPContext() => $params['organization']); |
---|
133 | $sectorList = $this->ldap->getSectors($params['organization'], true); |
---|
134 | foreach ($sectorList as $sector) |
---|
135 | $output[$sector['dn']] = str_repeat(' ', 3 * $sector['level']) . $sector['ou']; |
---|
136 | |
---|
137 | if (!$raw) |
---|
138 | { |
---|
139 | $newOutput['sectors'] = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($output), array_values($output))); |
---|
140 | reset($output); |
---|
141 | $params['context'] = key($output); |
---|
142 | $newOutput['participants'] = $this->getEntities($params, false); |
---|
143 | $output = $newOutput; |
---|
144 | } |
---|
145 | |
---|
146 | return $output; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Efetua uma busca no catálogo LDAP (completo) |
---|
151 | * @param array $params Array que contém parâmetros necessários para fazer a busca (podem ser advindos de Ajax) |
---|
152 | * @param bool $raw Se false, indica que os dados devem ser retornados em código HTML (via Ajax), se true indica que os dados devem ser retornados em forma de array |
---|
153 | * @return mixed Os registros encontrados, em formato de array ou em formato de string HTML para construção de combo box |
---|
154 | * @access public |
---|
155 | */ |
---|
156 | function globalSearch($params, $raw = false) |
---|
157 | { |
---|
158 | $entities = $params['entities']; |
---|
159 | |
---|
160 | $searchTerm = $params['searchTerm']; |
---|
161 | |
---|
162 | if (strlen(str_replace(' ', '', $searchTerm)) < 3) |
---|
163 | return array('warnings' => array('Utilize ao menos três caracteres em sua busca.')); |
---|
164 | |
---|
165 | if (preg_match('/^[a-z0-9_\- =,]+$/i', $searchTerm) < 1) |
---|
166 | return array('warnings' => array('O parâmetro de busca é inválido.')); |
---|
167 | |
---|
168 | $searchTerm = '*' . str_replace(' ', '*', trim($searchTerm)) . '*'; |
---|
169 | |
---|
170 | $searchUsers = (strpos($entities, 'u') !== false); |
---|
171 | $searchGroups = (strpos($entities, 'g') !== false); |
---|
172 | $searchLists = (strpos($entities, 'l') !== false); |
---|
173 | |
---|
174 | $onlyVisibleAccounts = $params['onlyVisibleAccounts']; |
---|
175 | |
---|
176 | /* faz a busca */ |
---|
177 | $output = array(); |
---|
178 | $output['participants'] = $this->ldap->search($searchTerm, $searchUsers, $searchGroups, $searchLists, null, $onlyVisibleAccounts); |
---|
179 | |
---|
180 | /* limita os resultados e define uma mensagem que será exibida */ |
---|
181 | $participantsCount = count($output['participants']); |
---|
182 | if ($participantsCount > 200) |
---|
183 | { |
---|
184 | $participantsCount = 200; |
---|
185 | $output['participants'] = array_slice($output['participants'], 0, 200); |
---|
186 | $output['warnings'][] = 'Sua busca foi limitada a 200 registros.'; |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | if ($participantsCount === 0) |
---|
191 | $output['warnings'][] = 'Nenhum registro encontrado.'; |
---|
192 | else |
---|
193 | if ($participantsCount > 1) |
---|
194 | $output['warnings'][] = $participantsCount . ' registros encontrados'; |
---|
195 | |
---|
196 | } |
---|
197 | |
---|
198 | /* se necessário, gera a saída em formato HTML */ |
---|
199 | if (!$raw) |
---|
200 | { |
---|
201 | if($params['id'] === 'mail') |
---|
202 | { |
---|
203 | $id = 'mail'; |
---|
204 | $usePreffix = false; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | $id = 'id'; |
---|
209 | $usePreffix = ($params['usePreffix'] == true) ? true : false; |
---|
210 | } |
---|
211 | |
---|
212 | $newOutput = array(); |
---|
213 | foreach ($output['participants'] as $row) |
---|
214 | { |
---|
215 | $organization = str_replace('ou=', '', implode('/', array_reverse(array_filter(explode(',', $row['dn']), create_function('$a', 'return (substr($a, 0, 2) == "ou");'))))); |
---|
216 | $key = ($usePreffix ? $row['type'] : '') . $row[$id]; |
---|
217 | $value = "{$row['name']} ({$organization})"; |
---|
218 | $newOutput[$key] = $value; |
---|
219 | } |
---|
220 | |
---|
221 | $newOutput = implode("\n", array_map(create_function('$a,$b', 'return \'<option value="\' . $a . \'">\' . $b . \'</option>\';'), array_keys($newOutput), array_values($newOutput))); |
---|
222 | $output['participants'] = $newOutput; |
---|
223 | } |
---|
224 | |
---|
225 | return $output; |
---|
226 | } |
---|
227 | } |
---|