source: trunk/expressoMail1_2/inc/class.ldap_functions.inc.php @ 114

Revision 114, 15.1 KB checked in by niltonneto, 16 years ago (diff)

Otimizado filtro ldap para deixar a busca de contatos mais rápida.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php                   
2include_once("class.imap_functions.inc.php");                   
3class ldap_functions
4{
5        var $ds;
6        var $ldap_host;
7        var $ldap_context;
8        var $imap;
9       
10        // usa o host e context do contact center.
11        function ldapConnect($refer = false){
12                $this->ldap_host        = $_SESSION['phpgw_info']['expressomail']['ldap_server']['host'];
13                $this->ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
14                $this->ds                       = ldap_connect($this->ldap_host);
15                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
16                ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
17                ldap_bind($this->ds, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);                               
18        }
19
20        // usa o host e context do setup.
21        function ldapRootConnect($refer = false){
22                $this->ldap_host        = $_SESSION['phpgw_info']['expressomail']['server']['ldap_host'];
23                $this->ldap_context = $_SESSION['phpgw_info']['expressomail']['server']['ldap_context'];
24                $this->ds                       = ldap_connect($this->ldap_host);
25                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
26                ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
27                ldap_bind($this->ds, $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],$_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);                             
28
29        }
30
31        function quicksearch($params)
32        {       
33                include_once("class.functions.inc.php");
34                $functions = new functions;                                             
35
36                $search_for     = $params['search_for'];
37                $field          = $params['field'];
38                $ID                     = $params['ID'];
39               
40                $contacts_result = array();
41                $contacts_result['field'] = $field;
42                $contacts_result['ID'] = $ID;
43               
44                // follow the referral
45                $this->ldapConnect(true);
46               
47                if ($this->ds)
48                {
49                        if (($field != 'null') && ($ID != 'null'))
50                        {
51                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(cn=*$search_for*)(mail=$search_for*)))";
52                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid");
53                        }
54                        else
55                        {
56                                $filter="(& (phpgwAccountType=u)(cn=*$search_for*) )";
57                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "jpegPhoto", "uid");
58                        }
59                       
60                        $sr=ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
61                        $count_entries = ldap_count_entries($this->ds,$sr);
62                       
63                        if ($count_entries > 200)
64                        {
65                                // Close old ldap conection
66                                ldap_close($this->ds);
67                               
68                                // Reopen a local ldap connection, following referral
69                                $this->ldapRootConnect(true);
70                               
71                                $user_dn = $_SESSION['phpgw_info']['expressomail']['user']['account_dn'];
72                                $user_sector_dn = ldap_explode_dn ( $user_dn, false );
73                                array_shift($user_sector_dn);
74                                array_shift($user_sector_dn);
75                                $user_sector_dn = implode(",", $user_sector_dn);
76                               
77                                // New search only on user sector
78                                $sr=ldap_search($this->ds, $user_sector_dn, $filter, $justthese);
79                                $count_entries = ldap_count_entries($this->ds,$sr);
80                               
81                                if ($count_entries > 200){
82                                        $return = array();
83                                        $return['status'] = false;
84                                        $return['error'] = "many results";
85                                        return $return;
86                                }
87                                else
88                                {
89                                        $quickSearch_only_in_userSector = true;
90                                }
91                        }
92                        $info = ldap_get_entries($this->ds, $sr);
93                       
94                        $tmp = array();
95                        for ($i=0; $i<$info["count"]; $i++)
96                        {
97                                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
98                                        continue;
99                                $tmp[$info[$i]["mail"][0] . '%' . $info[$i]["telephonenumber"][0] . '%'. $info[$i]["mobile"][0] . '%' . $info[$i]["uid"][0] . '%' . $info[$i]["jpegphoto"]['count']] = $info[$i]["cn"][0];
100                        }
101                        natcasesort($tmp);
102                       
103                        $i = 0;
104                        if (($field != 'null') && ($ID != 'null'))
105                        {
106                                foreach ($tmp as $info => $cn)
107                                {
108                                        $contacts_result[$i] = array();
109                                        $contacts_result[$i]["cn"] = $cn;
110                                        list ($contacts_result[$i]["mail"], $contacts_result[$i]["phone"], $contacts_result[$i]["mobile"]) = split ('%', $info);
111                                        $i++;
112                                }
113                                $contacts_result['quickSearch_only_in_userSector'] = $quickSearch_only_in_userSector;
114                        }
115                        else
116                        {
117                                $options = '';
118                               
119                                foreach ($tmp as $info => $cn)
120                                {
121                                        $contacts_result[$i] = array();
122                                        $contacts_result[$i]["cn"] = $cn;
123                                        list ($contacts_result[$i]["mail"], $contacts_result[$i]["phone"], $contacts_result[$i]["mobile"], $contacts_result[$i]["uid"], $contacts_result[$i]["jpegphoto"]) = split ('%', $info);
124                                       
125                                        if ($contacts_result[$i]['jpegphoto'])
126                                                $photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result[$i]['mail'].'">';
127                                        else
128                                                $photo_link = '<img src="./templates/default/images/photo.png">';
129                                       
130                                        // '<tr class="quicksearchcontacts_unselected" onClick="javascript:QuickSearchUser.select_cc(this)">' .
131                                        $phoneUser = $contacts_result[$i]['phone'];
132                                       
133                                        if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
134                                                $phoneUser = '<a title="'.$functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
135                                                if($contacts_result[$i]['mobile']){
136                                                $phoneUser .= ' / <a title="'.$functions->getLang("Call to Mobile Number").'" href="#" onclick="InfoContact.connectVoip(\''.$contacts_result[$i]['mobile'].'\',\'mob\')">'.$contacts_result[$i]['mobile'].'</a>';
137                                                }
138                                        }
139                                        $options .=
140                                                '<tr class="quicksearchcontacts_unselected">' .
141                                                        '<td class="cc" width="1%">' .
142                                                                '<a title="'.$functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result[$i]["cn"].'\', \''.$contacts_result[$i]["mail"].'\')">' .
143                                                                        $photo_link .
144                                                                '</a>' .
145                                                        '</td>' .
146                                                        '<td class="cc">' .
147                                                                '<span name="cn">' . $contacts_result[$i]['cn'] . '</span>' . '<br>' .
148                                                                '<a title="'.$functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result[$i]["cn"].'\', \''.$contacts_result[$i]["mail"].'\')">' .
149                                                                        '<font color=blue>' .
150                                                                                '<span name="mail">' . $contacts_result[$i]['mail'] . '</span>' . '<br>' .
151                                                                        '</font>' .
152                                                                '</a>' .
153                                                                $phoneUser .
154                                                        '</td>' .
155                                                '</tr>';
156                                        $i++;
157                                }
158                                $contacts_result = $options;
159                               
160                                if ($quickSearch_only_in_userSector)
161                                {
162                                        $head_option =
163                                                '<tr class="quicksearchcontacts_unselected">' .
164                                                        '<td colspan="2" width="100%" align="center">' .
165                                                                $functions->getLang('More than 200 results were found') . '.<br>' .
166                                                                $functions->getLang('Showing only the results found in your organization') . '.';
167                                                        '</td>' .
168                                                '</tr>';
169                                        $contacts_result = $head_option . $contacts_result;
170                                }
171                        }
172                }
173                ldap_close($this->ds);
174                return $contacts_result;
175        }
176
177        function get_organizations($params){
178                $organizations = array();
179                $referral = $params['referral'];
180                $this->ldapConnect($referral);
181                if ($this->ds) {
182                        $filter="ou=*";         
183                        $justthese = array("ou");
184                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
185                        $info = ldap_get_entries($this->ds, $sr);
186                       
187                        for ($i=0; $i<$info["count"]; $i++)
188                                $organizations[$i] = $info[$i]["ou"][0];
189
190                        ldap_close($this->ds);
191                        sort($organizations);                                   
192                }
193                return $organizations;
194        }
195
196        function get_organizations2($params){
197                $organizations = array();
198                $referral = $params['referral'];
199                $this->ldapRootConnect($referral);
200                if ($this->ds) {
201                        $filter="ou=*";         
202                        $justthese = array("ou");
203                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
204                        $info = ldap_get_entries($this->ds, $sr);
205                       
206                        for ($i=0; $i<$info["count"]; $i++)
207                        {
208                                $organizations[$i]['ou'] = $info[$i]["ou"][0];
209                                $organizations[$i]['dn'] = $info[$i]["dn"];
210                        }
211                       
212                        ldap_close($this->ds);
213                        sort($organizations);                                   
214                }
215                return $organizations;
216        }
217
218        function catalogsearch($params)
219        {       
220                $this->ldapConnect(true);
221                $cn     = $params['search_for'] ? "*".$params['search_for']."*" : "*";
222                $user_context = "ou=".$params['organization'].",".$this->ldap_context;         
223                $max_result       = $params['max_result'] ? $params['max_result'] : '200';             
224                $error = False;
225               
226                if ($this->ds) {                                                                                       
227                        $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible");
228                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(cn=".$cn."))";
229                        $sr=ldap_search($this->ds, $user_context, $filter, $justthese);
230                       
231                        $count_entries = ldap_count_entries($this->ds,$sr);
232                        if ($count_entries > $max_result){
233                                $info = null;
234                                $error = True;                                         
235                        }
236                        else
237                                $info = ldap_get_entries($this->ds, $sr);               
238                       
239                        ldap_close($this->ds);                 
240                       
241                        $u_tmp = array();
242                        $g_tmp = array();
243                       
244                        for ($i=0; $i<$info["count"]; $i++){
245                                if((strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
246                                        $u_tmp[$info[$i]["mail"][0]] = $info[$i]["cn"][0];
247                                else
248                                        $g_tmp[$info[$i]["mail"][0]] = $info[$i]["cn"][0];
249                        }                                                                               
250                       
251                        natcasesort($u_tmp);
252                        natcasesort($g_tmp);
253                       
254                        $i = 0;
255                        $users = array();                       
256                       
257                        foreach ($u_tmp as $mail => $cn){                               
258                                $users[$i++] = array("name" => $cn, "email" => $mail);
259                        }
260                        unset($u_tmp);
261                       
262                        $i = 0;                 
263                        $groups = array();
264                       
265                        foreach ($g_tmp as $mail => $cn){                               
266                                $groups[$i++] = array("name" => $cn, "email" => $mail);
267                        }
268                        unset($g_tmp);
269                       
270                        return  array('users' => $users, 'groups' => $groups, 'error' => $error);
271                }                               
272                return null;
273        }
274       
275        function get_emails_ldap(){
276
277                $result['mail']= array();
278                $result['mailalter']= array();         
279                $user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
280                $this->ldapRootConnect(false);
281                if ($this->ds) {
282                        $filter="uid=".$user;           
283                        $justthese = array("mail","mailAlternateAddress");
284                        $sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
285                        $ent = ldap_get_entries($this->ds, $sr);
286                        ldap_close($this->ds);
287                       
288                        for ($i=0; $i<$ent["count"]; $i++){
289                                $result['mail'][] = $ent[$i]["mail"][0];
290                                $result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];                           
291                        }
292                }
293                return $result;
294        }
295       
296        //Busca usuários de um contexto e já retorna as options do select;
297        function get_available_users($params)
298    {
299        $this->ldapRootConnect();
300        //Monta lista de Grupos e Usuários
301        $users = Array();
302        $groups = Array();
303        $user_context= $params['context'];
304        $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
305
306        if ($this->ds)
307        {
308            $justthese = array("gidNumber","cn");
309            if ($params['type'] == 'search')
310                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
311            else
312                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
313            $info = ldap_get_entries($this->ds, $sr);
314            for ($i=0; $i<$info["count"]; $i++)
315                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);           
316            $justthese = array("phpgwaccountvisible","uidNumber","cn");
317            if ($params['type'] == 'search')
318                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
319            else
320                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
321
322            $info = ldap_get_entries($this->ds, $sr);
323            for ($i=0; $i<$info["count"]; $i++)
324            {
325                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
326                    continue;
327                $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
328            }
329        }
330        ldap_close($this->ds);
331           
332        @asort($users);
333        @reset($users);   
334        @asort($groups);
335        @reset($groups);
336        $user_options ='';
337        $group_options ='';
338
339        foreach($groups as $id => $user_array) {
340                $newId = $id.'U';
341                $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
342        }
343        foreach($users as $id => $user_array) {
344            if($owner != $id){
345                $newId = $id.'U';
346                $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
347            }
348        }
349        return array("users" => $user_options, "groups" => $group_options);
350    }
351
352        //Busca usuários de um contexto e já retorna as options do select;
353        function get_available_users2($params)
354        {
355                $this->ldapRootConnect();
356               
357                $context= $params['context'];
358                $justthese = array("cn", "uid", "cn");
359                $filter = "(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))";
360
361            if ($this->ds)
362            {
363                        $sr=ldap_search($this->ds, $context, $filter, $justthese);
364                        $entries = ldap_get_entries($this->ds, $sr);
365                       
366                        for ($i=0; $i<$entries["count"]; $i++){
367                                if($_SESSION['phpgw_info']['expressomail']['user']['account_lid'] != $entries[$i]["uid"][0]){
368                                        $u_tmp[$entries[$i]["uid"][0]] = $entries[$i]["cn"][0];
369                                }
370                        }
371                       
372                        natcasesort($u_tmp);
373
374                        $i = 0;
375                        $users = array();
376                       
377                        if (count($u_tmp))
378                        {
379                                foreach ($u_tmp as $uidnumber => $cn)
380                                {
381                                        $options .= "<option value=$uidnumber>$cn</option>";
382                                }
383                                unset($u_tmp);
384                        }
385
386                        ldap_close($this->ds);
387                return $options;
388                }
389        }
390       
391        function uid2cn($uid)
392        {       
393                // do not follow the referral
394                $this->ldapRootConnect(false);
395                if ($this->ds)
396                {
397                        $filter="(&(phpgwAccountType=u)(uid=$uid))";           
398                        $justthese = array("cn");
399                        $sr=ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
400                       
401                        $info = ldap_get_entries($this->ds, $sr);
402                        return $info[0]["cn"][0];
403                }
404                return false;
405        }
406        function getSharedUsersFrom($params){           
407                $uids = explode(";",$params['uids']);
408
409                $this->imap = new imap_functions();
410                $filter = '';
411
412                foreach($uids as $index => $uid){
413                        $params = array();             
414                        $acl = $this->imap->getacltouser($uid);
415                        if(preg_match("/a/",$acl))
416                                $filter .= "(uid=$uid)";
417                }
418
419                $this->ldapRootConnect(false);
420                $filter="(&(phpgwAccountType=u)(|$filter))";
421                if ($this->ds) {                                       
422                        $justthese = array("cn","mail");
423                        $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
424                        ldap_sort($this->ds,$sr,"cn");                 
425                        $info   =       ldap_get_entries($this->ds, $sr);
426                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];                                         
427                        return $info;
428                }
429        }
430
431        function getUserByEmail($params){       
432                // Follow the referral
433                $email = $params['email'];
434                $this->ldapConnect(true);
435                if ($this->ds)
436                {
437                        $filter="(&(phpgwAccountType=u)(mail=$email))";         
438                        $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile");
439                        $sr=ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
440                        $entry = ldap_first_entry($this->ds, $sr);                     
441                        if($entry) {                                           
442                                $obj =  array("cn" => @ldap_get_values($this->ds, $entry, "cn"),
443                                                  "email" => $email,
444                                                  "uid" => @ldap_get_values($this->ds, $entry, "uid"),
445                                                  "type" => "global",
446                                                  "mobile" =>  @ldap_get_values($this->ds, $entry, "mobile"),
447                                                  "telefone" =>  @ldap_get_values($this->ds, $entry, "telephonenumber"));
448
449                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] = @ldap_get_values_len($this->ds, $entry, "jpegphoto");
450                                ldap_close($this->ds);                         
451                                return $obj;
452                        }               
453                }
454                return null;
455        }
456}
457?>
Note: See TracBrowser for help on using the repository browser.