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

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