1 | <?php |
---|
2 | /***************************************************************************\ |
---|
3 | * eGroupWare - Contacts Center * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * Written by: * |
---|
6 | * - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net> * |
---|
7 | * ------------------------------------------------------------------------- * |
---|
8 | * This program is free software; you can redistribute it and/or modify it * |
---|
9 | * under the terms of the GNU General Public License as published by the * |
---|
10 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
11 | * option) any later version. * |
---|
12 | \***************************************************************************/ |
---|
13 | |
---|
14 | |
---|
15 | /* |
---|
16 | This class is responsible for the LDAP control/generic functions and for |
---|
17 | configuration gathering |
---|
18 | */ |
---|
19 | |
---|
20 | class bo_ldap_manager |
---|
21 | { |
---|
22 | |
---|
23 | var $srcs; |
---|
24 | |
---|
25 | |
---|
26 | function bo_ldap_manager () |
---|
27 | { |
---|
28 | if (!($this->srcs = $GLOBALS['phpgw']->session->appsession('bo_ldap_manager.srcs','contactcenter'))) |
---|
29 | { |
---|
30 | $c = CreateObject('phpgwapi.config','contactcenter'); |
---|
31 | $data = $c->read_repository(); |
---|
32 | |
---|
33 | if (!$data or $data['cc_global_source0'] !== 'ldap') |
---|
34 | { |
---|
35 | $this->srcs = null; |
---|
36 | return; |
---|
37 | } |
---|
38 | |
---|
39 | $this->srcs = array( |
---|
40 | 1 => array( |
---|
41 | 'name' => $data['cc_catalog_name'], |
---|
42 | 'host' => $data['cc_ldap_host0'], |
---|
43 | 'dn' => $data['cc_ldap_context0'], |
---|
44 | 'acc' => $data['cc_ldap_browse_dn0'], |
---|
45 | 'pw' => $data['cc_ldap_pw0'], |
---|
46 | 'obj' => 'inetOrgPerson', |
---|
47 | 'branch' => strtolower('ou') |
---|
48 | ) |
---|
49 | ); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | function new_ldap_source ( $source_name, $charset, $host, $port, $dn_root, $dn_admin, $admin_pass, $contact_objectclass ) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | /* |
---|
58 | |
---|
59 | @function get_all_ldap_sources |
---|
60 | @abstract Returns an array containing all LDAP sources informations |
---|
61 | @author Raphael Derosso Pereira |
---|
62 | |
---|
63 | @return array All LDAP information |
---|
64 | $return = array( |
---|
65 | <id_source> => array( |
---|
66 | 'host' => (string), |
---|
67 | 'dn' => (string), |
---|
68 | 'acc' => (string), |
---|
69 | 'pw' => (string) |
---|
70 | ), |
---|
71 | ... |
---|
72 | ) |
---|
73 | |
---|
74 | TODO: Return multiple sources... |
---|
75 | */ |
---|
76 | function get_all_ldap_sources ( ) |
---|
77 | { |
---|
78 | return $this->srcs; |
---|
79 | } |
---|
80 | |
---|
81 | function get_ldap_fields_association ( $id_source ) |
---|
82 | { |
---|
83 | |
---|
84 | $op_iop = array( |
---|
85 | 'contact.id_contact' => array('dn'), |
---|
86 | 'contact.photo' => array('jpegPhoto'), |
---|
87 | 'contact.prefixes.prefix' => false, |
---|
88 | 'contact.alias' => array('alias'), |
---|
89 | 'contact.given_names' => array('givenName'), |
---|
90 | 'contact.family_names' => array('sn'), |
---|
91 | 'contact.names_ordered' => array('cn'),//,'displayName'), |
---|
92 | 'contact.suffixes.suffix' => false, |
---|
93 | 'contact.birthdate' => false, |
---|
94 | 'contact.sex' => false, |
---|
95 | 'contact.pgp_key' => false, |
---|
96 | 'contact.notes' => false, |
---|
97 | 'contact.mail_forwarding_address' => array('mailForwardingAddress'), |
---|
98 | 'contact.account_type' => array('phpgwAccountType'), |
---|
99 | 'contact.business_info.title' => array('title'), |
---|
100 | 'contact.business_info.department' => array('ou'), |
---|
101 | 'contact.company.company_name' => array('o'), |
---|
102 | 'contact.company.company_notes' => array('businessCategory'), |
---|
103 | |
---|
104 | 'contact.contact_related.names_ordered' => 'contact.contact_related.typeof_relation.contact_relation_name', |
---|
105 | 'contact.contact_related.typeof_relation.contact_relation_name' => array( |
---|
106 | 'manager' => array('manager'), |
---|
107 | 'secretary' => array('secretary') |
---|
108 | ), |
---|
109 | |
---|
110 | 'contact.address.address1' => 'contact.address.typeof_address.contact_address_type_name', |
---|
111 | 'contact.address.typeof_address.contact_address_type_name' => array( |
---|
112 | 'home' => array('street', 'st', 'postalAddress', 'homePostalAddress'), |
---|
113 | ), |
---|
114 | |
---|
115 | 'contact.address.postal_code' => 'contact.address.typeof_address.contact_address_type_name', |
---|
116 | 'contact.address.typeof_address.contact_address_type_name' => array( |
---|
117 | 'home' => array('PostalCode'), |
---|
118 | ), |
---|
119 | |
---|
120 | 'contact.address.city.city_name' => 'contact.address.typeof_address.contact_address_type_name', |
---|
121 | 'contact.address.typeof_address.contact_address_type_name' => array( |
---|
122 | 'home' => array('l'), |
---|
123 | ), |
---|
124 | |
---|
125 | 'contact.address.city.state.state_name' => 'contact.address.typeof_address.contact_address_type_name', |
---|
126 | 'contact.address.typeof_address.contact_address_type_name' => array( |
---|
127 | 'home' => false, |
---|
128 | ), |
---|
129 | |
---|
130 | 'contact.address.city.country.id_country' => 'contact.address.typeof_address.contact_address_type_name', |
---|
131 | 'contact.address.typeof_address.contact_address_type_name' => array( |
---|
132 | 'home' => array('c') |
---|
133 | ), |
---|
134 | |
---|
135 | 'contact.connection.connection_value' => 'contact.connection.typeof_connection.contact_connection_type_name', |
---|
136 | 'contact.connection.typeof_connection.contact_connection_type_name' => array ( |
---|
137 | 'email' => array('mail'), |
---|
138 | 'phone' => array('telephoneNumber'), |
---|
139 | 'mobile' => array('mobile'), |
---|
140 | 'pager' => array('pager'), |
---|
141 | 'fax' => array('facsimileTelephoneNumber'), |
---|
142 | 'telex' => array('telexNumber') |
---|
143 | ), |
---|
144 | ); |
---|
145 | |
---|
146 | return $op_iop; |
---|
147 | } |
---|
148 | |
---|
149 | /*! |
---|
150 | |
---|
151 | @function get_ldap_tree |
---|
152 | @abstract Returns the LDAP tree corresponding to the specified level |
---|
153 | @author Raphael Derosso Pereira |
---|
154 | |
---|
155 | @param (integer) $id_source The ID of the LDAP source |
---|
156 | |
---|
157 | @param (string) $context The context to be used as root branch |
---|
158 | |
---|
159 | @param (boolean) $recursive Make it a recursive construction. |
---|
160 | CAUTION! This is EXTREMELY SLOW on large LDAP databases, |
---|
161 | specially when they're not indexed |
---|
162 | */ |
---|
163 | function get_ldap_tree($id_source, $context = false, $recursive = false) |
---|
164 | { |
---|
165 | if (!$this->srcs[$id_source]) |
---|
166 | { |
---|
167 | return null; |
---|
168 | } |
---|
169 | |
---|
170 | $ldap = $GLOBALS['phpgw']->common->ldapConnect($this->srcs[$id_source]['host'], $this->srcs[$id_source]['acc'],$this->srcs[$id_source]['pw'], false); |
---|
171 | if (!$ldap) |
---|
172 | { |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | if ($recursive) |
---|
177 | { |
---|
178 | $tree = $this->get_ldap_tree_recursive($ldap, $context, $this->srcs[$id_source]['obj'],$this->srcs[$id_source]['branch']); |
---|
179 | $tree['recursive'] = true; |
---|
180 | |
---|
181 | return $tree; |
---|
182 | } |
---|
183 | |
---|
184 | return $this->get_ldap_tree_level($id_source, $ldap, $context, $this->srcs[$id_source]['obj'],$this->srcs[$id_source]['branch']); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /*! |
---|
189 | |
---|
190 | THIS FUNCTION IS NOT TESTED AND IS PROBABLY BROKEN! |
---|
191 | I WILL CORRECT IT IN THE NEAR FUTURE |
---|
192 | |
---|
193 | */ |
---|
194 | function get_ldap_tree_recursive($resource, $context, $objectClass) |
---|
195 | { |
---|
196 | $filter = '(!(objectClass='.$objectClass.'))'; |
---|
197 | $result_res = ldap_list($resource, $context, $filter); |
---|
198 | |
---|
199 | if ($result_res === false) |
---|
200 | { |
---|
201 | return null; |
---|
202 | } |
---|
203 | |
---|
204 | $count = ldap_count_entries($resource,$result_res); |
---|
205 | if ( $count == 0 ) |
---|
206 | { |
---|
207 | $filter = 'objectClass='.$objectClass; |
---|
208 | $result_res2 = ldap_list($resource, $context, $filter); |
---|
209 | $entries_count = ldap_count_entries($resource, $result_res2); |
---|
210 | |
---|
211 | if ($result_res2 !== false && $entries_count > 0) |
---|
212 | { |
---|
213 | return $entries_count; |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | return null; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | $entries = ldap_get_entries($resource, $result_res); |
---|
222 | |
---|
223 | for ($i = 0; $i < $entries['count']; $i++) |
---|
224 | { |
---|
225 | $subtree = $this->get_ldap_tree_recursive($resource, $entries[$i]['dn'], $objectClass); |
---|
226 | |
---|
227 | $dn_parts=ldap_explode_dn($entries[$i]['dn'],1); |
---|
228 | |
---|
229 | if ($subtree !== null and is_array($subtree)) |
---|
230 | { |
---|
231 | $tree[$i]['name'] = $dn_parts[0]; |
---|
232 | $tree[$i]['type'] = 'catalog_group'; |
---|
233 | $tree[$i]['recursive'] = true; |
---|
234 | $tree[$i]['sub_branch'] = $subtree; |
---|
235 | } |
---|
236 | else if (is_int($subtree) and $subtree !== null) |
---|
237 | { |
---|
238 | $tree[$i] = array( |
---|
239 | 'name' => $dn_parts[0], |
---|
240 | 'type' => 'catalog', |
---|
241 | 'class' => 'global_contact_manager', |
---|
242 | 'icon' => 'share-mini.png', |
---|
243 | 'value' => $entries[$i]['dn'], |
---|
244 | 'sub_branch' => false |
---|
245 | ); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | if (is_array($tree)) |
---|
250 | { |
---|
251 | return $tree; |
---|
252 | } |
---|
253 | else |
---|
254 | { |
---|
255 | return null; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | function get_ldap_referrals($ds, $dn, $filter) { |
---|
260 | |
---|
261 | ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); |
---|
262 | ldap_set_option($ds,LDAP_OPT_PROTOCOL_VERSION,3); |
---|
263 | |
---|
264 | if ($ds) { |
---|
265 | ldap_bind($ds); |
---|
266 | $sr=ldap_list($ds,$dn, $filter); |
---|
267 | $ref = ldap_first_reference($ds, $sr); |
---|
268 | $array_referral = array(); |
---|
269 | $idx = 0; |
---|
270 | |
---|
271 | while ($ref) { |
---|
272 | $array_referral[$idx++] = ldap_get_dn($ds, $ref); |
---|
273 | $ref = ldap_next_reference($ds, $ref); |
---|
274 | } |
---|
275 | return $array_referral; |
---|
276 | } |
---|
277 | else |
---|
278 | return false; |
---|
279 | } |
---|
280 | |
---|
281 | function get_ldap_sub_branches_referrals($ds, $dn, $filter) { |
---|
282 | |
---|
283 | $referral = $this -> get_ldap_referrals($ds, $dn, $filter); |
---|
284 | $sub_branches = array(); |
---|
285 | |
---|
286 | for($i = 0; $i <count($referral); $i++) { |
---|
287 | $dn = str_replace("??base","",preg_replace('!^(ldap://[^/]+)/(.*$)!', '\\2', $referral[$i])); |
---|
288 | $dn = explode(",",$dn); |
---|
289 | $dn = strtoupper(str_replace("ou=", "",$dn[0])); |
---|
290 | $dn = str_replace("DC=", "",$dn); |
---|
291 | |
---|
292 | $sub_branch = array( |
---|
293 | 'name' => $dn, |
---|
294 | 'type' => 'unknown', |
---|
295 | 'value' => $referral[$i], |
---|
296 | 'sub_branch' => false |
---|
297 | ); |
---|
298 | $sub_branches[$i] = $sub_branch; |
---|
299 | } |
---|
300 | return $sub_branches; |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | function get_ldap_tree_level($id_source, $resource, $context, $objectClass, $branch_dn) |
---|
305 | { |
---|
306 | if(strstr($context, "ldap://")){ |
---|
307 | $refer_context = str_replace("??base","",preg_replace('!^(ldap://[^/]+)/(.*$)!', '\\2', $context)); |
---|
308 | $host = preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $context); |
---|
309 | $resource = ldap_connect($host); |
---|
310 | ldap_bind($resource); |
---|
311 | } |
---|
312 | $dn_parts = ldap_explode_dn(($refer_context ? $refer_context : $context),1); |
---|
313 | //$filter = '(!(objectClass='.$objectClass.'))'; |
---|
314 | // Don't show OU's whith phpgwAccountVisible equal to '-1' |
---|
315 | $filter = '(&(!(objectClass='.$objectClass.')) (!(phpgwAccountVisible=-1)))'; |
---|
316 | $result_res = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter); |
---|
317 | @ldap_sort($resource, $result_res, 'ou'); |
---|
318 | |
---|
319 | if ($result_res === false) |
---|
320 | { |
---|
321 | return null; |
---|
322 | } |
---|
323 | |
---|
324 | $count = ldap_count_entries($resource,$result_res); |
---|
325 | |
---|
326 | if ( $count == 0 ) |
---|
327 | { |
---|
328 | $filter = 'objectClass='.$objectClass; |
---|
329 | $result_res2 = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter); |
---|
330 | $entries_count = ldap_count_entries($resource, $result_res2); |
---|
331 | |
---|
332 | if ($result_res2 !== false && $entries_count > 0) |
---|
333 | { |
---|
334 | return array( |
---|
335 | 'name' => $dn_parts[0], |
---|
336 | 'type' => 'catalog', |
---|
337 | 'class' => 'bo_global_ldap_catalog', |
---|
338 | 'class_args' => array($id_source, $context), |
---|
339 | 'icon' => 'globalcatalog-mini.png', |
---|
340 | 'value' => $context, |
---|
341 | 'sub_branch' => false |
---|
342 | ); |
---|
343 | } |
---|
344 | else |
---|
345 | { |
---|
346 | return array( |
---|
347 | 'name' => $dn_parts[0], |
---|
348 | 'type' => 'empty' |
---|
349 | ); |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | $sub_branch_found = false; |
---|
354 | $i = 0; |
---|
355 | for ($entry = ldap_first_entry($resource, $result_res); |
---|
356 | $entry != false; |
---|
357 | $entry = ldap_next_entry($resource, $entry)) |
---|
358 | { |
---|
359 | $dn = ldap_get_dn($resource, $entry); |
---|
360 | $dn_parts_1 = ldap_explode_dn($dn,1); |
---|
361 | $dn_parts_full = ldap_explode_dn($dn,0); |
---|
362 | list($group) = explode('=',$dn_parts_full[0]); |
---|
363 | |
---|
364 | if ($group == $branch_dn or $branch_dn === 'all') |
---|
365 | { |
---|
366 | $tree['sub_branch'][$i] = array( |
---|
367 | 'name' => $dn_parts_1[0], |
---|
368 | 'type' => 'unknown', |
---|
369 | 'value' => ($refer_context ? $host."/" : "").$dn, |
---|
370 | 'sub_branch' => false |
---|
371 | ); |
---|
372 | $sub_branch_found = true; |
---|
373 | } |
---|
374 | $i++; |
---|
375 | } |
---|
376 | |
---|
377 | if(! $refer_context) { |
---|
378 | $array_referral = $this -> get_ldap_sub_branches_referrals($resource, $context,'(objectClass=organizationalUnit)'); |
---|
379 | for($z = 0; $z < count($array_referral); $z++) { |
---|
380 | $tree['sub_branch'][$i++] = $array_referral[$z]; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | $filter = 'objectClass='.$objectClass; |
---|
385 | $result_res2 = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter); |
---|
386 | $entries_count = ldap_count_entries($resource, $result_res2); |
---|
387 | |
---|
388 | if ($result_res2 !== false && $entries_count > 0 && $sub_branch_found) |
---|
389 | { |
---|
390 | $tree['name'] = $dn_parts[0]; |
---|
391 | $tree['type'] = 'mixed_catalog_group'; |
---|
392 | $tree['class'] = 'bo_global_ldap_catalog'; |
---|
393 | $tree['class_args'] = array($id_source,$context); |
---|
394 | $tree['icon'] = 'globalcatalog-mini.png'; |
---|
395 | $tree['value'] = $context; |
---|
396 | } |
---|
397 | elseif ($result_res2 !== false && $entries_count > 0 && !$sub_branch_found) |
---|
398 | { |
---|
399 | return array( |
---|
400 | 'name' => $dn_parts[0], |
---|
401 | 'type' => 'catalog', |
---|
402 | 'class' => 'bo_global_ldap_catalog', |
---|
403 | 'class_args' => array($id_source, $context), |
---|
404 | 'icon' => 'globalcatalog-mini.png', |
---|
405 | 'value' => $context, |
---|
406 | 'sub_branch' => false |
---|
407 | ); |
---|
408 | } |
---|
409 | else |
---|
410 | { |
---|
411 | $tree['name'] = $dn_parts[0]; |
---|
412 | $tree['type'] = 'catalog_group'; |
---|
413 | $tree['class'] = 'bo_catalog_group_catalog'; |
---|
414 | $tree['class_args'] = array('$this', '$this->get_branch_by_level($this->catalog_level[0])'); |
---|
415 | $tree['value'] = $context; |
---|
416 | $tree['ldap'] = array('id_source' => $id_source, 'context' => $context); |
---|
417 | } |
---|
418 | |
---|
419 | usort($tree['sub_branch'], array($this, "compareTreeNodes")); |
---|
420 | return $tree; |
---|
421 | } |
---|
422 | |
---|
423 | function compareTreeNodes($a, $b) { |
---|
424 | |
---|
425 | return strnatcasecmp($a['name'], $b['name']); |
---|
426 | } |
---|
427 | |
---|
428 | } |
---|
429 | ?> |
---|