source: trunk/contactcenter/inc/class.bo_ldap_manager.inc.php @ 2

Revision 2, 13.1 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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                        $result_res = @ldap_list($resource,  ($refer_context ? $refer_context : $context), $filter);
315                        @ldap_sort($resource, $result_res, 'ou');
316
317                        if ($result_res === false)
318                        {
319                                return null;
320                        }
321
322                        $count = ldap_count_entries($resource,$result_res);
323
324                        if ( $count == 0 )
325                        {
326                                $filter = 'objectClass='.$objectClass;
327                                $result_res2 = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter);
328                                $entries_count = ldap_count_entries($resource, $result_res2);
329
330                                if ($result_res2 !== false && $entries_count > 0)
331                                {
332                                        return array(
333                                                'name'       => $dn_parts[0],
334                                                'type'       => 'catalog',
335                                                'class'      => 'bo_global_ldap_catalog',
336                                                'class_args' => array($id_source, $context),
337                                                'icon'       => 'globalcatalog-mini.png',
338                                                'value'      => $context,
339                                                'sub_branch' => false
340                                        );
341                                }
342                                else
343                                {
344                                        return array(
345                                                'name' => $dn_parts[0],
346                                                'type' => 'empty'
347                                        );
348                                }
349                        }
350
351                        $sub_branch_found = false;
352                        $i = 0;
353                        for ($entry = ldap_first_entry($resource, $result_res);
354                             $entry != false;
355                             $entry = ldap_next_entry($resource, $entry))
356                        {
357                                $dn = ldap_get_dn($resource, $entry);
358                                $dn_parts_1 = ldap_explode_dn($dn,1);
359                                $dn_parts_full = ldap_explode_dn($dn,0);
360                                list($group) = explode('=',$dn_parts_full[0]);
361                               
362                                if ($group == $branch_dn or $branch_dn === 'all')
363                                {
364                                        $tree['sub_branch'][$i] = array(
365                                                'name'  => $dn_parts_1[0],
366                                                'type'  => 'unknown',
367                                                'value' =>  ($refer_context ? $host."/" : "").$dn,
368                                                'sub_branch' => false                                   
369                                        );
370                                        $sub_branch_found = true;
371                                }
372                                $i++;
373                        }
374                       
375                        if(! $refer_context) {
376                                $array_referral = $this -> get_ldap_sub_branches_referrals($resource, $context,'(objectClass=organizationalUnit)');
377                                for($z = 0; $z < count($array_referral); $z++) {
378                                        $tree['sub_branch'][$i++] = $array_referral[$z];
379                                }
380                        }
381
382                        $filter = 'objectClass='.$objectClass;
383                        $result_res2 = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter);
384                        $entries_count = ldap_count_entries($resource, $result_res2);
385
386                        if ($result_res2 !== false && $entries_count > 0 && $sub_branch_found)
387                        {
388                                $tree['name']       = $dn_parts[0];
389                                $tree['type']       = 'mixed_catalog_group';
390                                $tree['class']      = 'bo_global_ldap_catalog';
391                                $tree['class_args'] = array($id_source,$context);
392                                $tree['icon']       = 'globalcatalog-mini.png';
393                                $tree['value']      = $context;
394                        }
395                        elseif ($result_res2 !== false && $entries_count > 0 && !$sub_branch_found)
396                        {
397                                return array(
398                                        'name'       => $dn_parts[0],
399                                        'type'       => 'catalog',
400                                        'class'      => 'bo_global_ldap_catalog',
401                                        'class_args' => array($id_source, $context),
402                                        'icon'       => 'globalcatalog-mini.png',
403                                        'value'      => $context,
404                                        'sub_branch' => false
405                                );
406                        }
407                        else
408                        {
409                                $tree['name']       = $dn_parts[0];
410                                $tree['type']       = 'catalog_group';
411                                $tree['class']      = 'bo_catalog_group_catalog';
412                                $tree['class_args'] = array('$this', '$this->get_branch_by_level($this->catalog_level[0])');
413                                $tree['value']      = $context;
414                                $tree['ldap']       = array('id_source' => $id_source, 'context' => $context);
415                        }
416                       
417                        usort($tree['sub_branch'], array($this, "compareTreeNodes"));
418                        return $tree;
419                }
420
421                function compareTreeNodes($a, $b)       {
422                                               
423                        return strnatcasecmp($a['name'], $b['name']);
424                }       
425
426        }
427?>
Note: See TracBrowser for help on using the repository browser.