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

Revision 7655, 8.3 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

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