source: companies/serpro/contactcenter/inc/class.bo_ldap_manager.inc.php @ 903

Revision 903, 19.4 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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        include_once('class.Thread.inc.php');
20
21        class bo_ldap_manager
22        {
23               
24                var $srcs;
25
26               
27                function bo_ldap_manager ()
28                {
29                        if (!($this->srcs = $GLOBALS['phpgw']->session->appsession('bo_ldap_manager.srcs','contactcenter')))
30                        {
31                                $c = CreateObject('phpgwapi.config','contactcenter');
32                                $data = $c->read_repository();
33                               
34                                if (!$data or $data['cc_global_source0'] !== 'ldap')
35                                {
36                                        $this->srcs = null;
37                                        return;
38                                }
39                               
40                                $this->srcs = array(
41                                        1 => array(
42                                                'name'   => $data['cc_catalog_name'],
43                                                'host'   => $data['cc_ldap_host0'],
44                                                'dn'     => $data['cc_ldap_context0'],
45                                                'acc'    => $data['cc_ldap_browse_dn0'],
46                                                'pw'     => $data['cc_ldap_pw0'],
47                                                'obj'    => 'inetOrgPerson',
48                                                'branch' => strtolower('ou'),
49                                                'visible' => $data['cc_ldap_query_automatic']
50                                        )
51                                );
52                        }
53                }
54
55                function new_ldap_source ( $source_name, $charset, $host, $port, $dn_root, $dn_admin, $admin_pass, $contact_objectclass )
56                {
57                }
58       
59                /*
60               
61                        @function get_all_ldap_sources
62                        @abstract Returns an array containing all LDAP sources informations
63                        @author Raphael Derosso Pereira
64               
65                        @return array All LDAP information
66                                $return = array(
67                                        <id_source> => array(
68                                                'host' => (string),
69                                                'dn'   => (string),
70                                                'acc'  => (string),
71                                                'pw'   => (string)   
72                                        ),
73                                        ...
74                                )
75                               
76                        TODO: Return multiple sources...
77                */
78                function get_all_ldap_sources (  )
79                {
80                        return $this->srcs;
81                }
82               
83                /*
84                * @function get_external_ldap_sources
85                * @author Mï¿œrio Cï¿œsar Kolling <mario.kolling@serpro.gov.br>
86                * @abstract returns an array with the external sources
87                * @return (array) the external sources
88                */
89
90                function get_external_ldap_sources()
91                {
92                        include(PHPGW_INCLUDE_ROOT . '/contactcenter/setup/external_catalogs.inc.php' );
93                        //include('external_catalogs.inc.php' );
94                        return $external_srcs;
95                }
96
97                /*
98                 * @function get_ldap_fields_association
99                 * @abstract get the fields associantion for ldap source
100                 * @return an array with attribute mappings
101                 */
102                function get_ldap_fields_association ( $id_source )
103                {
104                       
105                        $op_iop = array(
106                                'contact.id_contact'               => array('dn'),
107                                'contact.photo'                    => array('jpegPhoto'),
108                                'contact.prefixes.prefix'          => false,
109                                'contact.alias'                    => array('alias'),
110                                'contact.given_names'              => array('givenName'),
111                                'contact.family_names'             => array('sn'),
112                                'contact.names_ordered'            => array('cn'),//,'displayName'),
113                                'contact.suffixes.suffix'          => false,
114                                'contact.birthdate'                => false,
115                                'contact.sex'                      => false,
116                                'contact.pgp_key'                  => false,
117                                'contact.notes'                    => false,
118                                'contact.mail_forwarding_address' => array('mailForwardingAddress'),
119                                'contact.account_type' => array('phpgwAccountType'),
120                'contact.account_status'           => array('phpgwAccountStatus'),
121                'contact.account_visible'          => array('phpgwAccountVisible'),
122                                'contact.object_class'             => array('objectClass'),
123                                'contact.business_info.title'      => array('title'),
124                                'contact.business_info.department' => array('ou'), // Setor do empregado...
125                                'contact.business_info.empNumber'  => array('employeeNumber'), // Matricula do empregado
126
127                                'contact.business_info.celPhone'   => array('mobile'), // Celular empresarial do empregado
128
129                                'contact.company.company_name'     => array('o'),
130                                'contact.company.company_notes'    => array('businessCategory'),
131                               
132                                'contact.contact_related.names_ordered' => 'contact.contact_related.typeof_relation.contact_relation_name',
133                                'contact.contact_related.typeof_relation.contact_relation_name' =>  array(
134                                        'manager'   => array('manager'),
135                                        'secretary' => array('secretary')
136                                ),
137                               
138                                'contact.address.address1'         => 'contact.address.typeof_address.contact_address_type_name',
139                                'contact.address.typeof_address.contact_address_type_name' => array(
140                                        'home' => array('street', 'st', 'postalAddress', 'homePostalAddress'),
141                                ),
142                               
143                                'contact.address.postal_code'      => 'contact.address.typeof_address.contact_address_type_name',
144                                'contact.address.typeof_address.contact_address_type_name' => array(
145                                        'home' => array('PostalCode'),
146                                ),
147                               
148                                'contact.address.city.city_name'   => 'contact.address.typeof_address.contact_address_type_name',
149                                'contact.address.typeof_address.contact_address_type_name' => array(
150                                        'home' => array('l'),
151                                ),
152                               
153                                'contact.address.city.state.state_name'       => 'contact.address.typeof_address.contact_address_type_name',
154                                'contact.address.typeof_address.contact_address_type_name' => array(
155                                        'home' => false,
156                                ),
157                               
158                                'contact.address.city.country.id_country'     => 'contact.address.typeof_address.contact_address_type_name',
159                                'contact.address.typeof_address.contact_address_type_name' => array(
160                                        'home' => array('c')
161                                ),
162                               
163                                'contact.connection.connection_value'         => 'contact.connection.typeof_connection.contact_connection_type_name',
164                                'contact.connection.typeof_connection.contact_connection_type_name' => array (
165                                        'email'  => array('mail'),
166                                        'phone'  => array('telephoneNumber'),
167                                //      'mobile' => array('mobile'),
168                                //      'pager'  => array('pager'), // idem ao comentario abaixo, do atributo fax;
169                                //      'fax'    => array('facsimileTelephoneNumber'), //linha comentada para nao trazer
170                                // o atributo fax do Ldap; correcao temporaria para nao exibir o fax no ContactCenter
171                                //(estava sobrepondo o telefone do usuario)
172
173                                        'telex'  => array('telexNumber')
174                                ),
175                        );
176                       
177                        return $op_iop;
178                }
179
180                /*
181                 * @function get_external_ldap_fields_association
182                 * @author Mï¿œrio Cï¿œsar Kolling <mario.kolling@serpro.gov.br>
183                 * @abstract get the fields association for an external ldap_source
184                 * @return an array with attribute mappings
185                 */
186                function get_external_ldap_fields_association ( $id_source )
187                {
188                        include(PHPGW_INCLUDE_ROOT . '/contactcenter/setup/external_catalogs.inc.php' );
189                        //include('external_catalogs.inc.php' );
190                        return $external_mappings[$id_source];
191                }
192
193                /*
194                 * @function test_connection
195                 * @author Mï¿œrio Cï¿œsar Kolling <mario.kolling@serpro.gov.br>
196                 * @abstract Test if we can bind to a ldap server in a reasonable time
197                 * @param (string) $host ldap server's hostname
198                 * @param (string) $account ldap bind dn
199                 * @param (string) $password a bind dn's password
200                 * @return (array) an array with the answer from the subprocess, null otherwise
201                 */
202                function test_connection($host, $account, $password, $timeout = 5)
203                {
204                        //opens a subprocess for nonblocking bind
205                        $tsearch = Thread::Create('class.ldap_assync.inc.php', array('host'     => $host,
206                                                                                                                                'account'       => $account,
207                                                                                                                                'password'      => $password
208                                                                                                                                )
209                        );
210
211                        // It's problably more efficient to let method readResponse control the timeout through
212                        // stream_select native timeout.
213                        $response = NULL;
214                        for ($i = 0; $i < $timeout; $i++)
215                        {
216                                if ($tsearch->isActive())
217                                {
218                                        sleep(1);
219                                        if (($response = $tsearch->readResponse()) !== NULL)
220                                        {
221                                                $tsearch->close();
222                                                return $response;
223                                        }
224
225                                }
226                                else
227                                {
228                                        $response = $tsearch->readResponse();
229                                        break;
230                                }
231                        }
232
233                        $tsearch->close();
234                        return null;
235                }
236
237                /*!
238               
239                        @function get_ldap_tree
240                        @abstract Returns the LDAP tree corresponding to the specified level
241                        @author Raphael Derosso Pereira
242                       
243                        @param (integer) $id_source The ID of the LDAP source
244                       
245                        @param (string)  $context The context to be used as root branch
246                               
247                        @param (boolean) $recursive Make it a recursive construction.
248                                CAUTION! This is EXTREMELY SLOW on large LDAP databases,
249                                specially when they're not indexed
250                */             
251                function get_ldap_tree($id_source, $context = false, $recursive = false)
252                {
253                        if (!$this->srcs[$id_source])
254                        {
255                                return null;
256                        }
257                       
258                        $ldap = $GLOBALS['phpgw']->common->ldapConnect($this->srcs[$id_source]['host'], $this->srcs[$id_source]['acc'],$this->srcs[$id_source]['pw'], false);
259                        if (!$ldap)
260                        {
261                                return false;
262                        }
263                       
264                        if ($recursive)
265                        {
266                                $tree = $this->get_ldap_tree_recursive($ldap, $context, $this->srcs[$id_source]['obj'],$this->srcs[$id_source]['branch']);
267                                $tree['recursive'] = true;
268
269                                return $tree;
270                        }
271                       
272                        return $this->get_ldap_tree_level($id_source, $ldap, $context, $this->srcs[$id_source]['obj'],$this->srcs[$id_source]['branch']);
273                }
274                // SERPRO
275
276                /*!
277
278                        @function get_external_ldap_tree
279                        @abstract Returns the LDAP external tree corresponding to the specified level
280                        @author Mï¿œrio Cï¿œsar Kolling <mario.kolling@serpro.gov.br>
281                        @param (integer) $id_source The ID of the external LDAP source
282                        @param (string)  $context The context to be used as root branch
283                        @param (boolean) $recursive Make it a recursive construction.
284                                CAUTION! This is EXTREMELY SLOW on large LDAP databases,
285                                specially when they're not indexed
286                */
287                function get_external_ldap_tree($id_source, $context = false, $recursive = false)
288                {
289
290
291                        include(PHPGW_INCLUDE_ROOT . '/contactcenter/setup/external_catalogs.inc.php' );
292                        //include('external_catalogs.inc.php' );
293
294                        if (!$external_srcs[$id_source])
295                        {
296                                return null;
297                        }
298
299                        // calls test_connection first. If succeeded continue, return error message otherwise.
300                        if (!($response = $this->test_connection($external_srcs[$id_source]['host'], $external_srcs[$id_source]['acc'], $external_srcs[$id_source]['pw'], 10)))
301                        {
302                                return array(
303                                        'msg'           =>      lang("Catalog %1 temporarily unavailable. Please try again later!", $external_srcs[$id_source]['name']),
304                                        'timeout'       =>      'true'
305                                );
306                        }
307
308                        $ldap = $GLOBALS['phpgw']->common->ldapConnect($external_srcs[$id_source]['host'], $external_srcs[$id_source]['acc'],$external_srcs[$id_source]['pw'], false);
309                        if (!$ldap)
310                        {
311                                return false;
312                        }
313
314                        // Option recursive commented out
315                        /*
316                        if ($recursive)
317                        {
318                                $tree = $this->get_ldap_tree_recursive($ldap, $context, $this->srcs[$id_source]['obj'],$this->srcs[$id_source]['branch']);
319                                $tree['recursive'] = true;
320
321                                return $tree;
322                        }
323                        */
324
325                        return $this->get_ldap_tree_level($id_source, $ldap, $context, $external_srcs[$id_source]['obj'],$external_srcs[$id_source]['branch'], 1);
326                }
327
328                /*!
329
330                        THIS FUNCTION IS NOT TESTED AND IS PROBABLY BROKEN!
331                        I WILL CORRECT IT IN THE NEAR FUTURE
332
333                */
334                function get_ldap_tree_recursive($resource, $context, $objectClass)
335                {
336                        $filter = '(!(objectClass='.$objectClass.'))';
337                        $result_res = ldap_list($resource, $context, $filter);
338
339                        if ($result_res === false)
340                        {
341                                return null;
342                        }
343                       
344                        $count = ldap_count_entries($resource,$result_res);
345                        if ( $count == 0 )
346                        {
347                                $filter = 'objectClass='.$objectClass;
348                                $result_res2 = ldap_list($resource, $context, $filter);
349                                $entries_count = ldap_count_entries($resource, $result_res2);
350
351                                if ($result_res2 !== false && $entries_count > 0)
352                                {
353                                        return $entries_count;
354                                }
355                                else
356                                {
357                                        return null;
358                                }
359                        }
360                       
361                        $entries = ldap_get_entries($resource, $result_res);
362                       
363                        for ($i = 0; $i < $entries['count']; $i++)
364                        {
365                                $subtree = $this->get_ldap_tree_recursive($resource, $entries[$i]['dn'], $objectClass);
366                               
367                                $dn_parts=ldap_explode_dn($entries[$i]['dn'],1);
368                               
369                                if ($subtree !== null and is_array($subtree))
370                                {
371                                        $tree[$i]['name'] = $dn_parts[0];
372                                        $tree[$i]['type'] = 'catalog_group';
373                                        $tree[$i]['recursive'] = true;
374                                        $tree[$i]['sub_branch'] = $subtree;
375                                }
376                                else if (is_int($subtree) and $subtree !== null)
377                                {
378                                        $tree[$i] = array(
379                                                'name'       => $dn_parts[0],
380                                                'type'       => 'catalog',
381                                                'class'      => 'global_contact_manager',
382                                                'icon'       => 'share-mini.png',
383                                                'value'      => $entries[$i]['dn'],
384                                                'sub_branch' => false
385                                        );
386                                }
387                        }
388
389                        if (is_array($tree))
390                        {
391                                return $tree;
392                        }
393                        else
394                        {
395                                return null;
396                        }
397                }
398               
399                function get_ldap_referrals($ds, $dn, $filter) {
400                       
401                        ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
402                        ldap_set_option($ds,LDAP_OPT_PROTOCOL_VERSION,3);
403                       
404                        if ($ds) {
405                            ldap_bind($ds);
406                                $sr=ldap_list($ds,$dn, $filter);                   
407                                $ref = ldap_first_reference($ds, $sr);
408                                $array_referral = array();
409                                $idx = 0;
410                               
411                                 while ($ref) {
412                                        $array_referral[$idx++] = ldap_get_dn($ds, $ref);
413                                        $ref = ldap_next_reference($ds, $ref);
414                                }
415                                return $array_referral;
416                        }
417                        else
418                                return false;
419                }
420
421                function get_ldap_sub_branches_referrals($ds, $dn, $filter) {
422                       
423                        $referral = $this -> get_ldap_referrals($ds, $dn, $filter);
424                        $sub_branches = array();
425                       
426                        for($i = 0; $i <count($referral); $i++) {
427                                $dn = str_replace("??base","",preg_replace('!^(ldap://[^/]+)/(.*$)!', '\\2', $referral[$i]));
428                                $dn = explode(",",$dn);                         
429                                $dn = strtoupper(str_replace("ou=", "",$dn[0]));
430                                $dn = str_replace("DC=", "",$dn);
431                                                                                                                                                                               
432                                $sub_branch = array(
433                                                                                        'name' => $dn,
434                                                                'type' => 'unknown',
435                                                                'value' => $referral[$i],
436                                                                'sub_branch' => false           
437                                                                                );                                                                                                                     
438                                $sub_branches[$i] = $sub_branch;                       
439                        }
440                        return $sub_branches;
441                }
442               
443
444                function translate_accentuation($text)
445                {
446                        /*
447                         * Esta operaᅵᅵo resolve o problema causado pela conversï¿œo de caracteres acentuados realizada
448                         * pela funᅵᅵo ldap_explode_dn().
449                         */
450
451                        return utf8_decode(preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $text));
452                }
453
454                function get_ldap_tree_level($id_source, $resource, $context, $objectClass, $branch_dn, $external = 0)
455                {
456                        if(strstr($context, "ldap://")){
457                                $refer_context = str_replace("??base","",preg_replace('!^(ldap://[^/]+)/(.*$)!', '\\2', $context));
458                                $host   = preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $context);
459                                $resource = ldap_connect($host);
460                                ldap_bind($resource);
461                        }
462
463                        /*
464                         * TODO: Search timeouts
465                         */
466
467                        $dn_parts = ldap_explode_dn(($refer_context ? $refer_context : $context),1);
468                        //$filter = '(!(objectClass='.$objectClass.'))';
469                        // Don't show OU's whith phpgwAccountVisible equal to '-1'
470                        if ($external)
471                        {
472                                // external source: get all organizationalUnits
473                                $filter = '(objectClass=organizationalUnit)';
474                        }
475                        else
476                        {
477                                // get any objectClass except the objectClass used for the source
478                                // and whose attribute phpgwAccountVisible value is different from -1
479                                $filter = '(&(!(objectClass='.$objectClass.')) (!(phpgwAccountVisible=-1)))';
480                        }
481                        $result_res = @ldap_list($resource,  ($refer_context ? $refer_context : $context), $filter, array(), 0, 0);
482                        @ldap_sort($resource, $result_res, 'ou');
483
484                        // Timeouts commented out
485                        /*
486                        if ($result_res === false)
487                        {
488                                return null;
489                        }
490                        */
491
492                        $count = ldap_count_entries($resource,$result_res);
493
494                        if ( $count == 0 )
495                        {
496                                $filter = '(objectClass='.$objectClass.')';
497                                // Get only one attribute of the source's objectClass
498                                $result_res2 = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter, Array('cn'), 0, 1);
499                                $entries_count = ldap_count_entries($resource, $result_res2);
500
501                                if ($result_res2 !== false && $entries_count > 0)
502                                {
503                                        return array(
504                                                'name'       => $this->translate_accentuation($dn_parts[0]),
505                                                'type'       => 'catalog',
506                                                'class'      => 'bo_global_ldap_catalog',
507                                                // Pass the variable $external as a parameter to the constructor
508                                                'class_args' => array($id_source, $context, $external),
509                                                'icon'       => 'globalcatalog-mini.png',
510                                                'value'      => $context,
511                                                'sub_branch' => false
512                                        );
513                                }
514                                else
515                                {
516                                        return array(
517                                                'name' => $this->translate_accentuation($dn_parts[0]),
518                                                'type' => 'empty'
519                                        );
520                                }
521                        }
522
523                        $sub_branch_found = false;
524                        $i = 0;
525                        for ($entry = ldap_first_entry($resource, $result_res);
526                             $entry != false;
527                             $entry = ldap_next_entry($resource, $entry))
528                        {
529                                $dn = ldap_get_dn($resource, $entry);
530                                $dn_parts_1 = ldap_explode_dn($dn,1);
531                                $dn_parts_full = ldap_explode_dn($dn,0);
532                                list($group) = explode('=',$dn_parts_full[0]);
533
534                                //Faz a comparaᅵᅵo do branch como case insensitive
535                                if (strtolower($group) == strtolower($branch_dn) or $branch_dn === 'all')
536                                {
537                                        $tree['sub_branch'][$i] = array(
538                                                'name'  => $this->translate_accentuation($dn_parts_1[0]),
539                                                'type'  => 'unknown',
540                                                'value' =>  ($refer_context ? $host."/" : "").$dn,
541                                                'sub_branch' => false
542                                        );
543                                        $sub_branch_found = true;
544                                }
545                                $i++;
546                        }
547
548                        if(! $refer_context) {
549                                $array_referral = $this -> get_ldap_sub_branches_referrals($resource, $context,'(objectClass=organizationalUnit)');
550                                for($z = 0; $z < count($array_referral); $z++) {
551                                        $tree['sub_branch'][$i++] = $array_referral[$z];
552                                }
553                        }
554
555                        $filter = 'objectClass='.$objectClass;
556                        $result_res2 = @ldap_list($resource, ($refer_context ? $refer_context : $context), $filter, Array('cn'), 0, 1);
557                        $entries_count = ldap_count_entries($resource, $result_res2);
558
559                        if ($result_res2 !== false && $entries_count > 0 && $sub_branch_found)
560                        {
561                                $tree['name']       = $this->translate_accentuation($dn_parts[0]);
562                                $tree['type']       = 'mixed_catalog_group';
563                                $tree['class']      = 'bo_global_ldap_catalog';
564                                // Pass the variable $external as a parameter to the constructor
565                                $tree['class_args'] = array($id_source,$context,$external);
566                                $tree['icon']       = 'globalcatalog-mini.png';
567                                $tree['value']      = $context;
568                        }
569                        elseif ($result_res2 !== false && $entries_count > 0 && !$sub_branch_found)
570                        {
571                                return array(
572                                        'name'       => $this->translate_accentuation($dn_parts[0]),
573                                        'type'       => 'catalog',
574                                        'class'      => 'bo_global_ldap_catalog',
575                                        // Pass the variable $external as a parameter to the constructor
576                                        'class_args' => array($id_source, $context,$external),
577                                        'icon'       => 'globalcatalog-mini.png',
578                                        'value'      => $context,
579                                        'sub_branch' => false
580                                );
581                        }
582                        else
583                        {
584                                $tree['name']       = $this->translate_accentuation($dn_parts[0]);
585                                $tree['type']       = 'catalog_group';
586                                $tree['class']      = 'bo_catalog_group_catalog';
587                                // Pass the variable $external as a parameter to the constructor
588                                $tree['class_args'] = array('$this', '$this->get_branch_by_level($this->catalog_level[0])', $external);
589                                $tree['value']      = $context;
590                                $tree['ldap']       = array('id_source' => $id_source, 'context' => $context);
591                        }
592                       
593                        usort($tree['sub_branch'], array($this, "compareTreeNodes"));
594                        return $tree;
595                }
596
597                function compareTreeNodes($a, $b)       {
598                                               
599                        return strnatcasecmp($a['name'], $b['name']);
600                }       
601
602        }
603?>
Note: See TracBrowser for help on using the repository browser.