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

Revision 2, 9.4 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • 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                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(cn=*$search_for*)(sn=*$search_for*)(mail=$search_for*)))";             
45                        $justthese = array("cn", "mail", "telephoneNumber", "phpgwAccountVisible");
46                        $sr=ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
47                       
48                        $count_entries = ldap_count_entries($this->ds,$sr);
49                        if ($count_entries > 200){
50                                $return = array();
51                                $return['status'] = false;
52                                $return['error'] = "many results";
53                                return $return;
54                        }
55                       
56                        $info = ldap_get_entries($this->ds, $sr);
57                        $tmp = array();
58                        for ($i=0; $i<$info["count"]; $i++)
59                        {
60                                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
61                                        continue;
62                                $tmp[$info[$i]["mail"][0] . '%' . $info[$i]["telephonenumber"][0]] = $info[$i]["cn"][0];
63                        }
64                        natcasesort($tmp);
65                       
66                        $i = 0;
67                        foreach ($tmp as $mail => $cn)
68                        {
69                                $contacts_result[$i] = array();
70                                $contacts_result[$i]["cn"] = $cn;
71                                list ($contacts_result[$i]["mail"], $contacts_result[$i]["phone"]) = split ('%', $mail);
72                                $i++;
73                        }
74                }
75                ldap_close($this->ds);
76                return $contacts_result;
77        }
78
79        function get_organizations($params){
80                $organizations = array();
81                $referral = $params['referral'];
82                $this->ldapConnect($referral);
83                if ($this->ds) {
84                        $filter="ou=*";         
85                        $justthese = array("ou");
86                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
87                        $info = ldap_get_entries($this->ds, $sr);
88                       
89                        for ($i=0; $i<$info["count"]; $i++)
90                                $organizations[$i] = $info[$i]["ou"][0];
91
92                        ldap_close($this->ds);
93                        sort($organizations);                                   
94                }
95                return $organizations;
96        }
97
98        function get_organizations2($params){
99                $organizations = array();
100                $referral = $params['referral'];
101                $this->ldapRootConnect($referral);
102                if ($this->ds) {
103                        $filter="ou=*";         
104                        $justthese = array("ou");
105                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
106                        $info = ldap_get_entries($this->ds, $sr);
107                       
108                        for ($i=0; $i<$info["count"]; $i++)
109                        {
110                                $organizations[$i]['ou'] = $info[$i]["ou"][0];
111                                $organizations[$i]['dn'] = $info[$i]["dn"];
112                        }
113                       
114                        ldap_close($this->ds);
115                        sort($organizations);                                   
116                }
117                return $organizations;
118        }
119
120        function catalogsearch($params)
121        {       
122                $this->ldapConnect(true);
123                $cn     = $params['search_for'] ? "*".$params['search_for']."*" : "*";
124                $user_context = "ou=".$params['organization'].",".$this->ldap_context;         
125                $max_result       = $params['max_result'] ? $params['max_result'] : '200';             
126                $error = False;
127               
128                if ($this->ds) {                                                                                       
129                        $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible");
130                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(cn=".$cn."))";
131                        $sr=ldap_search($this->ds, $user_context, $filter, $justthese);
132                       
133                        $count_entries = ldap_count_entries($this->ds,$sr);
134                        if ($count_entries > $max_result){
135                                $info = null;
136                                $error = True;                                         
137                        }
138                        else
139                                $info = ldap_get_entries($this->ds, $sr);               
140                       
141                        ldap_close($this->ds);                 
142                       
143                        $u_tmp = array();
144                        $g_tmp = array();
145                       
146                        for ($i=0; $i<$info["count"]; $i++){
147                                if((strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
148                                        $u_tmp[$info[$i]["mail"][0]] = $info[$i]["cn"][0];
149                                else
150                                        $g_tmp[$info[$i]["mail"][0]] = $info[$i]["cn"][0];
151                        }                                                                               
152                       
153                        natcasesort($u_tmp);
154                        natcasesort($g_tmp);
155                       
156                        $i = 0;
157                        $users = array();                       
158                       
159                        foreach ($u_tmp as $mail => $cn){                               
160                                $users[$i++] = array("name" => $cn, "email" => $mail);
161                        }
162                        unset($u_tmp);
163                       
164                        $i = 0;                 
165                        $groups = array();
166                       
167                        foreach ($g_tmp as $mail => $cn){                               
168                                $groups[$i++] = array("name" => $cn, "email" => $mail);
169                        }
170                        unset($g_tmp);
171                       
172                        return  array('users' => $users, 'groups' => $groups, 'error' => $error);
173                }                               
174                return null;
175        }
176       
177        function get_emails_ldap(){
178
179                $result['mail']= array();
180                $result['mailalter']= array();         
181                $user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
182                $this->ldapRootConnect(false);
183                if ($this->ds) {
184                        $filter="uid=".$user;           
185                        $justthese = array("mail","mailAlternateAddress");
186                        $sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
187                        $ent = ldap_get_entries($this->ds, $sr);
188                        ldap_close($this->ds);
189                       
190                        for ($i=0; $i<$ent["count"]; $i++){
191                                $result['mail'][] = $ent[$i]["mail"][0];
192                                $result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];                           
193                        }
194                }
195                return $result;
196        }
197       
198        //Busca usuários de um contexto e já retorna as options do select;
199        function get_available_users($params)
200        {
201                $this->ldapRootConnect();
202                //Monta lista de Grupos e Usuários
203                $users = Array();
204                $groups = Array();
205                $user_context= $params['context'];
206                $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
207
208            if ($this->ds)
209            {
210                $justthese = array("gidNumber","cn");
211                        $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g))"),$justthese);
212                        $info = ldap_get_entries($this->ds, $sr);
213                        for ($i=0; $i<$info["count"]; $i++)
214                                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name' =>      $uids=$info[$i]["cn"][0], 'type'        =>      g);                     
215                        $justthese = array("phpgwaccountvisible","uidNumber","cn");
216                        $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(cn=lista*)))"),$justthese);
217                        $info = ldap_get_entries($this->ds, $sr);
218                        for ($i=0; $i<$info["count"]; $i++)
219                        {
220                                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
221                                        continue;
222                                $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'  =>      $uids=$info[$i]["cn"][0], 'type'        =>      u);
223                        }
224                }
225                ldap_close($this->ds);
226               
227                @asort($users);
228                @reset($users);
229                @asort($groups);
230                @reset($groups);
231                $user_options ='';
232                $group_options ='';
233
234                foreach($groups as $id => $user_array) {
235                                $newId = $id.'U';
236                                $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
237                }
238                foreach($users as $id => $user_array) {
239                        if($owner != $id){
240                                $newId = $id.'U';
241                                $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
242                        }
243                }
244                return array("users" => $user_options, "groups" => $group_options);
245        }
246
247        //Busca usuários de um contexto e já retorna as options do select;
248        function get_available_users2($params)
249        {
250                $this->ldapRootConnect();
251               
252                $context= $params['context'];
253                $justthese = array("cn", "uid", "cn");
254                $filter = "(&(phpgwaccounttype=u)(!(cn=lista*)))";
255
256            if ($this->ds)
257            {
258                        $sr=ldap_search($this->ds, $context, $filter, $justthese);
259                        $entries = ldap_get_entries($this->ds, $sr);
260                       
261                        for ($i=0; $i<$entries["count"]; $i++){
262                                if($_SESSION['phpgw_info']['expressomail']['user']['account_lid'] != $entries[$i]["uid"][0]){
263                                        $u_tmp[$entries[$i]["uid"][0]] = $entries[$i]["cn"][0];
264                                }
265                        }
266                       
267                        natcasesort($u_tmp);
268
269                        $i = 0;
270                        $users = array();
271                       
272                        if (count($u_tmp))
273                        {
274                                foreach ($u_tmp as $uidnumber => $cn)
275                                {
276                                        $options .= "<option value=$uidnumber>$cn</option>";
277                                }
278                                unset($u_tmp);
279                        }
280
281                        ldap_close($this->ds);
282                return $options;
283                }
284        }
285       
286        function uid2cn($uid)
287        {       
288                // do not follow the referral
289                $this->ldapRootConnect(false);
290                if ($this->ds)
291                {
292                        $filter="(&(phpgwAccountType=u)(uid=$uid))";           
293                        $justthese = array("cn");
294                        $sr=ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
295                       
296                        $info = ldap_get_entries($this->ds, $sr);
297                        return $info[0]["cn"][0];
298                }
299                return false;
300        }
301        function getSharedUsersFrom($params){           
302                $uids = explode(";",$params['uids']);
303
304                $this->imap = new imap_functions();
305                $filter = '';
306
307                foreach($uids as $index => $uid){
308                        $params = array();             
309                        $acl = $this->imap->getacltouser($uid);
310                        if(preg_match("/a/",$acl))
311                                $filter .= "(uid=$uid)";
312                }
313
314                $this->ldapRootConnect(false);
315                $filter="(&(phpgwAccountType=u)(|$filter))";
316                if ($this->ds) {                                       
317                        $justthese = array("cn","mail");
318                        $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
319                        ldap_sort($this->ds,$sr,"cn");                 
320                        $info   =       ldap_get_entries($this->ds, $sr);
321                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];                                         
322                        return $info;
323                }
324        }
325}
326?>
Note: See TracBrowser for help on using the repository browser.