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

Revision 95, 14.9 KB checked in by niltonneto, 16 years ago (diff)

Implementada funcionalidade na busca rápida no Catálogo Geral.
Quando o limite de resultados é excedido, o retorno da busca traz somente os usuários da mesma organização do usuário logado.

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