source: trunk/services/class.ldap.php @ 5068

Revision 5068, 11.7 KB checked in by airton, 13 years ago (diff)

Ticket #2266 - Atualizar documentacao dos arquivos PHP

  • Property svn:executable set to *
Line 
1<?php
2/**
3*
4* Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
5*
6* This program is free software; you can redistribute it and/or modify it under
7* the terms of the GNU Affero General Public License version 3 as published by
8* the Free Software Foundation with the addition of the following permission
9* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
10* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
11* WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
12*
13* This program is distributed in the hope that it will be useful, but WITHOUT
14* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16* details.
17*
18* You should have received a copy of the GNU Affero General Public License
19* along with this program; if not, see www.gnu.org/licenses or write to
20* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21* MA 02110-1301 USA.
22*
23* This code is based on the OpenXchange Connector and on the Prognus pSync
24* Connector both developed by the community and licensed under the GPL
25* version 2 or above as published by the Free Software Foundation.
26*
27* You can contact Prognus Software Livre headquarters at Av. Tancredo Neves,
28* 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at
29* e-mail address prognus@prognus.com.br.
30*
31* Serviço de LDAP
32*
33* @package    Services
34* @license    http://www.gnu.org/copyleft/gpl.html GPL
35* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
36* @version    1.0
37* @sponsor    Caixa Econômica Federal
38*/
39
40
41
42
43/**
44* Serviço Ldap
45*
46* Classe responsável pelas operações com o LDAP
47*
48* @package    Service
49* @license    http://www.gnu.org/copyleft/gpl.html GPL
50* @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
51* @sponsor    Caixa Econômica Federal
52* @version    1.0
53*/
54class LdapService
55{
56    var $limit = 11;
57
58    var $allTargetTypes = array( 'i', 'g', 'l', 'u', 's' );
59
60    var $connection;
61
62    function LdapService()
63    {
64       
65    }
66
67       
68        /**
69     * Conecta com o LDAP
70     *
71     * @license    http://www.gnu.org/copyleft/gpl.html GPL
72         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
73         * @sponsor    Caixa Econômica Federal
74     * @param   $host
75     * @param   $dn
76     * @param   $passwd
77     * @param   $ldapreferral
78     */
79    function connect($host='', $dn='', $passwd='', $ldapreferral=false)
80    {
81        if(!$host || $host == $GLOBALS['phpgw_info']['server']['ldap_host'])
82        {
83            $dn         = $dn ? $dn : $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
84            $passwd = $passwd ? $passwd : $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
85            $host   = $host ? $host : $GLOBALS['phpgw_info']['server']['ldap_host'];
86        }
87
88        $conection = ldap_connect($host);
89
90        if(!ldap_set_option($conection,LDAP_OPT_PROTOCOL_VERSION,3))
91            $GLOBALS['phpgw_info']['server']['ldap_version3'] = False;
92       
93        ldap_set_option($conection, LDAP_OPT_REFERRALS, $ldapreferral);
94
95        if($ldapreferral)
96        {
97            $GLOBALS['phpgw_info']['server']['user_ldap_referral'] = $dn;
98            $GLOBALS['phpgw_info']['server']['password_ldap_referral'] = $passwd;
99            ldap_set_rebind_proc($conection, ldap_rebind);
100        }
101
102        if($dn && $passwd && !@ldap_bind($conection,$dn,$passwd))
103            @ldap_bind($conection,$dn,$passwd);
104
105        $this->connection = $conection;
106
107        return( $connection );
108    }
109
110    function _or( $toWrap )
111    {
112                if( !is_array( $toWrap ) )
113                        return( $toWrap );
114       
115                //      if( count( $toWrap ) <= 1 )
116                //          return implode( "", $toWrap );
117       
118                return $this->wrap( $toWrap, '|' );
119    }
120
121    function _and( $toWrap )
122    {
123                if( !is_array( $toWrap ) )
124                        return( $toWrap );
125       
126                //      if( count( $toWrap ) <= 1 )
127                //          return implode( "", $toWrap );
128       
129                return $this->wrap( $toWrap, '&' );
130    }
131
132    function _not( $toWrap )
133    {
134                return $this->wrap( $toWrap, '!' );
135    }
136
137    function wrap( $toWrap, $conditional = "" )
138    {
139                if( !is_array( $toWrap ) )
140                        $toWrap = array( $toWrap );
141       
142                $toWrap = array_unique( $toWrap );
143
144                return "(".$conditional.implode( "", $toWrap ).")";
145    }
146
147        /**
148     * Manipula o filtro de busca
149     *
150     * @license    http://www.gnu.org/copyleft/gpl.html GPL
151         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
152         * @sponsor    Caixa Econômica Federal
153     * @param   $search
154     * @param   $targetTypes
155     * @param   $customFilter
156     * @param   $exact
157     */
158    function getSearchFilter( $search, $targetTypes = false, $customFilter = '', $exact = false )
159    {
160        $search = utf8_encode( $search );
161
162                if( !$targetTypes )
163                                $targetTypes = $this->allTargetTypes;
164       
165                if( !is_array( $targetTypes ) )
166                        $targetTypes = array( $targetTypes );
167       
168                $searchFilter = '';
169       
170                foreach( $targetTypes as $targetType )
171                {
172                        switch( $targetType )
173                        {
174                        case 'g':
175                        {
176                                //no caso de grupos, a busca so precisa ser feita em cima do CN
177                                $searchFilter = $this->stemFilter( $search, 'cn' );
178                        }
179                        break;
180       
181                        default :
182                        {
183                        //parametros que a busca tem que ser sintaticas
184                                $searchFilter = /*array( */$this->stemFilter( $search, array(
185                                                        // UID e employeeNumber podem ser iguais ou muito similares, dependendo da organizacao
186                                                        'uid', /*'employeeNumber', */'cn',
187                                                        // givenName e SN sao complementares (nome e sobrenome)
188                                                        'givenName', 'sn', 'displayName' ) //),
189                                                        //parametros que a busca pode ser por aproximacao fonetica
190                                                /*$this->approxFilter( $search, array(
191                                                        // O CN e displayName geralmente sao a mesma coisa
192                                                        'cn', 'displayName' ))*/  );
193       
194                                $searchFilter = $this->stemFilter( $search, array( 'cn', 'givenName', 'uid',
195                                                                                'sn', 'displayName' ) );
196                        }
197                        break;
198                        }
199                }
200
201                $filter = array();
202       
203                if( $customFilter )
204                        $filter[] = $customFilter;
205                if( $search )
206                        $filter[] = $searchFilter;
207       
208                return $this->_and( array(
209                        // Somente objetos relacionados com o Expresso
210                        $this->accountFilter( $targetTypes ),
211                        // Objetos ocultados e/ou desativados pelo Administrador nao podem ser exibidos nas consultas do Expresso
212                        $this->securityFilter( $targetTypes ),
213                        //foco da busca
214                        ( $exact ? $this->_and( $filter ) : $this->_or( $filter ) )
215                        ));
216       
217                // Verificoes extras para validar o resultado
218                //  (&
219                //     // Somente objetos com e-mail no formato da RFC822
220                //    (mail=*@*)
221                //  )
222        }
223
224       
225        /**
226     * Manipula o filtro de segurança
227     *
228     * @license    http://www.gnu.org/copyleft/gpl.html GPL
229         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
230         * @sponsor    Caixa Econômica Federal
231     * @param   $targetTypes
232     */
233        function securityFilter( $targetTypes )
234    {
235                if( !$targetTypes )
236                                $targetTypes = $this->allTargetTypes;
237
238                if( !is_array( $targetTypes ) )
239                        $targetTypes = array( $targetTypes );
240       
241                $typeFilter = array();
242       
243                foreach( $targetTypes as $targetType )
244                {
245                        switch( $targetType )
246                        {
247                        case 'g': $typeFilter[] = "(objectClass=posixGroup)";
248                        break;
249                       
250                        default : $typeFilter[] = "(phpgwAccountStatus=A)";
251                        break;
252                        }
253                }
254
255                return $this->_and( array( '(!(phpgwAccountVisible=-1))', $this->_or( $typeFilter ) ) );
256        }
257
258       
259        /**
260     * Manipula o filtro da conta
261     *
262     * @license    http://www.gnu.org/copyleft/gpl.html GPL
263         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
264         * @sponsor    Caixa Econômica Federal
265     * @param   $targetTypes
266     */
267    function accountFilter( $targetTypes )
268    {
269        if( !$targetTypes )
270            $targetTypes = $this->allTargetTypes;
271
272        if( !is_array( $targetTypes ) )
273           $targetTypes = array( $targetTypes );
274
275        $typeFilter = array();
276
277        foreach( $targetTypes as $targetType )
278            $typeFilter[] = '(phpgwAccountType='.$targetType.')';
279
280        return $this->_and( array( '(objectClass=phpgwAccount)', $this->_or( $typeFilter ) ) );
281    }
282
283   
284       
285        /**
286     * @license    http://www.gnu.org/copyleft/gpl.html GPL
287         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
288         * @sponsor    Caixa Econômica Federal
289     * @param   $search
290     * @param   $params
291     */
292        function stemFilter( $search, $params )
293    {
294                $search = str_replace( ' ', '*', $search );
295       
296                if( !is_array( $params ) )
297                        $params = array( $params );
298       
299                foreach( $params as $i => $param )
300                        $params[$i] = "($param=*$search*)";
301               
302                return $this->_or( $params );
303    }
304
305   
306        /**
307     * Manipula o filtro fonético
308     *
309     * @license    http://www.gnu.org/copyleft/gpl.html GPL
310         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
311         * @sponsor    Caixa Econômica Federal
312     * @param   $search
313     * @param   $params
314     */
315        function phoneticFilter( $search, $params )
316    {
317                if( eregi( "\d", $search ) )
318                        return( "" );
319       
320                if( !is_array( $params ) )
321                        $params = array( $params );
322       
323                foreach( $params as $i => $param )
324                        $params[$i] = "($param~=$search)";
325               
326                return $this->_or( $params );
327    }
328
329       
330       
331       
332    function approxFilter( $search, $params )
333    {
334                return $this->_or( array( $this->stemFilter( $search, $params ),
335                                  $this->phoneticFilter( $search, $params ) ) );
336    }
337
338//     public function search()
339//     {
340//     
341//     }
342
343   
344        /**
345     * Método que realiza a busca no LDAP
346     *
347     * @license    http://www.gnu.org/copyleft/gpl.html GPL
348         * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
349         * @sponsor    Caixa Econômica Federal
350     * @param   $search
351     * @param   $justthese
352     * @param   $context
353     * @param   $accountType
354     * @param   $sort
355     */
356        public function accountSearch($search, $justthese = "*", $context = false , $accountType = false, $sort = false)
357    {
358         if( !$this->connection )
359            $this->connect();
360
361         $filter =  $this->getSearchFilter($search,$accountType);
362
363         if( !$context )
364             $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
365
366         $ls = ldap_search( $this->connection, utf8_encode($context), $filter, $justthese, 0, $this->limit );
367
368         if($sort)
369             ldap_sort( $this->connection, $ls, $sort );
370
371         $entries = ldap_get_entries( $this->connection, $ls );
372
373         if( !$entries ) return( null );
374
375         $return = array();
376
377         for ($i=0; $i < $entries["count"]; $i++)
378         {
379               $entrieTmp = array();
380               foreach ($entries[$i] as $index => $value)
381               {
382                   if(!is_numeric($index) && $index != 'count')
383                   {
384                       if(is_array($value))
385                       {
386                           if(count($value) == 2)
387                               $entrieTmp[$index] = utf8_decode($value['0']);
388                           else
389                           {
390                               foreach ($value as $index2 =>$value2)
391                               {
392                                    if($index != 'count')
393                                        $entrieTmp[$index][$index2] = utf8_decode($value2);
394                               }
395                           }
396                       }
397                       else
398                           $entrieTmp[$index] = utf8_decode($value);
399                   }
400               }
401
402               $return[] = $entrieTmp;
403         }
404
405         return( $return );
406    }
407
408}
409
410ServiceLocator::register( 'ldap', new LdapService() );
411
Note: See TracBrowser for help on using the repository browser.