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

Revision 37, 12.9 KB checked in by niltonneto, 17 years ago (diff)

Vide arquivo change_log.txt

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