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

Revision 23, 6.2 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

  • 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                function get_organizations($context, $selected='', $referral=false ,$show_invisible_ou=false)
103                {
104                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
105                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
106                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
107                       
108                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
109                       
110                        if ($referral)
111                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 1);
112                        else
113                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
114                       
115                        ldap_bind($ldap_conn,$dn,$passwd);
116                       
117                        $justthese = array("dn");
118                        $filter = $show_invisible_ou ? "(ou=*)" : "(& (ou=*) (!(phpgwAccountVisible=-1)) )";
119                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
120               
121                ldap_sort($ldap_conn, $search, "ou");
122                $info = ldap_get_entries($ldap_conn, $search);
123                        ldap_close($ldap_conn);
124
125                        // Retiro o count do array info e inverto o array para ordenação.
126                for ($i=0; $i<$info["count"]; $i++)
127            {
128                                $dn = $info[$i]["dn"];
129                               
130                                // Necessário, pq em uma busca com ldapsearch ou=*, traz tb o próprio ou.
131                                if (strtolower($dn) == $context)
132                                        continue;
133
134                                $array_dn = ldap_explode_dn ( $dn, 1 );
135
136                $array_dn_reverse  = array_reverse ( $array_dn, true );
137
138                                array_pop ( $array_dn_reverse );
139
140                                $inverted_dn[$dn] = implode ( ".", $array_dn_reverse );
141                        }
142
143                        // Ordenação
144                        natcasesort($inverted_dn);
145                       
146                        // Construção do select
147                        $level = 0;
148                        foreach ($inverted_dn as $dn=>$invert_ufn)
149                        {
150                $display = '';
151
152                $array_dn_reverse = explode ( ".", $invert_ufn );
153                $array_dn  = array_reverse ( $array_dn_reverse, true );
154
155                $level = count( $array_dn ) - 4;
156
157                if ($level == 0)
158                        $display .= '+';
159                else {
160                        for ($i=0; $i<$level; $i++)
161                                $display .= '---';
162                }
163
164                reset ( $array_dn );
165                $display .= ' ' . (current ( $array_dn ) );
166                               
167                                $dn = trim(strtolower($dn));
168                                if ( $dn == $selected )
169                                        $select = ' selected';
170                                else
171                                        $select = '';
172                $options .= "<option value='$dn'$select>$display</option>";
173                }
174
175                        // Cria a primeira entrada na combo
176                        $first_sector_ufn = ldap_dn2ufn($context);
177                        $first_sector_string = split(",", $first_sector_ufn);
178                        $options = "<option value='$context'>" . strtoupper($first_sector_string[0]) . "</option>" . $options;
179
180            return $options;
181                }
182        }
183?>
Note: See TracBrowser for help on using the repository browser.