source: trunk/phpgwapi/inc/class.sector_search_ldap.inc.php @ 207

Revision 207, 8.2 KB checked in by niltonneto, 16 years ago (diff)
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**************************************************************************\
3        * phpGroupWare API - Accounts manager for LDAP                             *
4        * Written by Joao Alfredo Knopik Junior <jakje@celepar.pr.gov.br>          *
5        * View and manipulate account records using LDAP                           *
6        * Copyright (C) 2000 - 2002 Joseph Engo, Lars Kneschke                     *
7        * Copyright (C) 2003 Lars Kneschke, Bettina Gille                          *
8        * ------------------------------------------------------------------------ *
9        * This library is part of the phpGroupWare API                             *
10        * http://www.phpgroupware.org                                              *
11        * ------------------------------------------------------------------------ *
12        * This library is free software; you can redistribute it and/or modify it  *
13        * under the terms of the GNU Lesser General Public License as published by *
14        * the Free Software Foundation; either version 2.1 of the License,         *
15        * or any later version.                                                    *
16        * This library is distributed in the hope that it will be useful, but      *
17        * WITHOUT ANY WARRANTY; without even the implied warranty of               *
18        * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
19        * See the GNU Lesser General Public License for more details.              *
20        * You should have received a copy of the GNU Lesser General Public License *
21        * along with this library; if not, write to the Free Software Foundation,  *
22        * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
23        \**************************************************************************/
24       
25        include_once('class.common.inc.php');
26       
27        class sector_search_ldap
28        {
29                var $common;
30                var $ldap_connection;
31                var $sector_name;
32                var $sector_context;
33                var $sector_level;
34                var $sector_leaf;
35                       
36                function sector_search_ldap($sector_name="", $sector_context="", $sector_level="", $sector_leaf="", $ldap_conn="")
37                {
38                        if ($ldap_conn == "")
39                        {
40                                $this->common = new common;
41                                $this->ldap_connection = $this->common->ldapConnect();
42                        }
43                        else
44                                $this->ldap_connection = $ldap_conn;
45                               
46                        $this->sector_name = $sector_name;
47                        $this->sector_context = $sector_context;
48                        $this->sector_level = $sector_level;
49                        $this->sector_leaf = $sector_leaf;
50                }
51       
52                // All levels of ouŽs, organizations (first level) and sectors (rest levels)
53                function sector_search($ldap_context, $clear_static_vars=true)
54                {
55                        static $sectors_list = array();
56                        static $level = 0;
57                       
58                        if ($clear_static_vars)
59                        {
60                                $sectors_list = array();
61                                $level = 0;
62                        }
63                       
64                        $filter="ou=*";         
65                        $justthese = array("ou");
66                        $sr=ldap_list($this->ldap_connection, $ldap_context, $filter, $justthese);
67                        ldap_sort($this->ldap_connection ,$sr, "ou");
68                        $info = ldap_get_entries($this->ldap_connection, $sr);
69
70                        for ($i=0; $i<$info["count"]; $i++)
71                        {
72                                $level++;
73                                $next_ldap_context[$i] = $info[$i]["dn"];
74                               
75                                $obj = new sector_search_ldap($info[$i]["ou"][0], $next_ldap_context[$i], $level, 'False', $this->ldap_connection);
76                                array_push($sectors_list, $obj);
77
78                                $this->sector_search($next_ldap_context[$i], false);
79                        }
80                        $level--;
81                        return $sectors_list;
82                }
83               
84                // Just the first level, or the organizations
85                function organization_search($ldap_context)
86                {
87                        $filter="ou=*";         
88                        $justthese = array("ou");
89                       
90                        $sr=ldap_list($this->ldap_connection, $ldap_context, $filter, $justthese);
91                        $info = ldap_get_entries($this->ldap_connection, $sr);
92                       
93                        for ($i=0; $i<$info["count"]; $i++)
94                        {
95                                $sectors_list[$i] = $info[$i]["ou"][0];
96                        }                               
97                        sort($sectors_list);
98                        return $sectors_list;
99                }
100               
101                // Retorna os organizações com os options prontos
102                // Parametro master: realiza ldap_connect utilizando dados do Contact Center, permitindo buscas completas em todos os ldaps.
103                function get_organizations($context, $selected='', $referral=false ,$show_invisible_ou=false, $master=false)
104                {
105                        if ($master)
106                        {
107                                $ldap_cc_info = CreateObject('contactcenter.bo_ldap_manager');
108                                $ldap_cc_info = $ldap_cc_info ? $ldap_cc_info->srcs[1] : null;
109                                $dn                     = $ldap_cc_info['acc'];
110                                $passwd         = $ldap_cc_info['pw'];
111                                $host           = $ldap_cc_info['host'];
112                        }
113                        else
114                        {
115                                $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
116                                $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
117                                $host           = $GLOBALS['phpgw_info']['server']['ldap_host'];
118                        }
119                       
120                        $ldap_conn      = ldap_connect($host);
121                       
122                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
123                       
124                        if ($referral)
125                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 1);
126                        else
127                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
128                       
129                        ldap_bind($ldap_conn,$dn,$passwd);
130                       
131                        $justthese = array("dn");
132                        $filter = $show_invisible_ou ? "(ou=*)" : "(& (ou=*) (!(phpgwAccountVisible=-1)) )";
133                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
134               
135                ldap_sort($ldap_conn, $search, "ou");
136                $info = ldap_get_entries($ldap_conn, $search);
137                        ldap_close($ldap_conn);
138
139                        // Retiro o count do array info e inverto o array para ordenação.
140                for ($i=0; $i<$info["count"]; $i++)
141            {
142                                $dn = $info[$i]["dn"];
143                               
144                                // Necessário, pq em uma busca com ldapsearch ou=*, traz tb o próprio ou.
145                                if (strtolower($dn) == $context)
146                                        continue;
147
148                                $array_dn = ldap_explode_dn ( $dn, 1 );
149
150                $array_dn_reverse  = array_reverse ( $array_dn, true );
151
152                                // Retirar o indice count do array.
153                                array_pop ( $array_dn_reverse );
154
155                                $inverted_dn[$dn] = implode ( "#", $array_dn_reverse );
156                        }
157
158                        if (is_array($inverted_dn))
159                        {
160                                // Ordenação
161                                natcasesort($inverted_dn);
162                       
163                                // Construção do select
164                                $level = 0;
165                                foreach ($inverted_dn as $dn=>$invert_ufn)
166                                {
167                        $display = '';
168
169                    $array_dn_reverse = explode ( "#", $invert_ufn );
170                        $array_dn  = array_reverse ( $array_dn_reverse, true );
171
172                        $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
173
174                    if ($level == 0)
175                                                $display .= '+';
176                        else
177                        {
178                                                for ($i=0; $i<$level; $i++)
179                                                        $display .= '---';
180                        }
181
182                        reset ( $array_dn );
183                        $display .= ' ' . (current ( $array_dn ) );
184                               
185                                        $dn = trim(strtolower($dn));
186                                        if ( $dn == $selected )
187                                                $select = ' selected';
188                                        else
189                                                $select = '';
190                        $options .= "<option value='$dn'$select>$display</option>";
191                        }
192                        }
193
194                        // Cria a primeira entrada na combo
195                        $first_sector_ufn = ldap_dn2ufn($context);
196                        $first_sector_string = split(",", $first_sector_ufn);
197                       
198                        if ($context == $selected)
199                                $select_first_entrie = ' selected';
200                        $options = "<option value='$context'$select_first_entrie>+ " . strtoupper($first_sector_string[0]) . "</option>" . $options;
201
202            return $options;
203                }
204
205                // Retorna os setores (somente OUs de primeiro nivel) com as options prontas
206                function get_sectors($selected='', $referral=false ,$show_invisible_ou=false)
207                {
208                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
209                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
210                        $context        = $GLOBALS['phpgw_info']['server']['ldap_context'];
211                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
212                       
213                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
214                       
215                        if ($referral)
216                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 1);
217                        else
218                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
219                       
220                        ldap_bind($ldap_conn,$dn,$passwd);
221                       
222                        $justthese = array("dn", "ou");
223                        $filter = $show_invisible_ou ? "(ou=*)" : "(&(ou=*)(!(phpgwAccountVisible=-1)))";
224                       
225                        $search=ldap_list($ldap_conn, $context, $filter, $justthese);
226               
227                ldap_sort($ldap_conn, $search, "ou");
228                $info = ldap_get_entries($ldap_conn, $search);
229               
230                        ldap_close($ldap_conn);
231                $options = '';
232                for ($i=0; $i<$info["count"]; $i++)
233            {
234                $dn = trim(strtolower($info[$i]['dn']));
235                                if ( $dn == $selected )
236                                        $select = ' selected';
237                                else
238                                        $select = '';
239                                $display = strtoupper($info[$i]['ou'][0]);
240                                $options .= "<option value='$dn'$select>$display</option>";
241            }
242               
243                        return $options;
244                }
245        }
246?>
Note: See TracBrowser for help on using the repository browser.