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

Revision 1500, 27.4 KB checked in by eduardoalex, 15 years ago (diff)

Ticket #681 - Criada a função para delegar evento

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2include_once("class.imap_functions.inc.php");
3include_once("class.functions.inc.php");
4
5function ldapRebind($ldap_connection, $ldap_url)
6{
7        @ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);
8}
9
10class ldap_functions
11{
12        var $ds;
13        var $ldap_host;
14        var $ldap_context;
15        var $imap;
16        var $external_srcs;
17        var $max_result;
18        var $functions;
19        function ldap_functions(){
20        // todo: Page Configuration for External Catalogs.
21                @include("../contactcenter/setup/external_catalogs.inc.php");
22                $this->external_srcs= $external_srcs;
23                $this->max_result       = 200;
24                $this->functions = new functions();
25        }
26        // Using ContactCenter configuration.
27        function ldapConnect($refer = false,$catalog = 0){
28                if ($catalog > 0 && is_array($this->external_srcs)){
29                        $this->ldap_host        = $this->external_srcs[$catalog]['host'];
30                        $this->ldap_context = $this->external_srcs[$catalog]['dn'];
31                        $this->bind_dn          = $this->external_srcs[$catalog]['acc'];
32                        $this->bind_dn_pw       = $this->external_srcs[$catalog]['pw'];
33                        $this->object_class = $this->external_srcs[$catalog]['obj'];
34                        $this->base_dn          = $this->external_srcs[$catalog]['dn'];
35                        $this->branch           = $this->external_srcs[$catalog]['branch'];
36                }else {
37                        $this->ldap_host        = $_SESSION['phpgw_info']['expressomail']['ldap_server']['host'];
38                        $this->ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
39                        $this->bind_dn = $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'];
40                        $this->bind_dn_pw = $_SESSION['phpgw_info']['expressomail']['ldap_server']['pw'];
41                        $this->branch = 'ou';
42                }
43
44                $this->ds = ldap_connect($this->ldap_host);
45                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
46                ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
47                if ($refer)     {
48                        ldap_set_rebind_proc($this->ds, ldapRebind);
49                }
50                @ldap_bind($this->ds,$this->bind_dn,$this->bind_dn_pw );
51        }
52
53        //Teste jakjr retornando o DS
54        function ldapConnect2($refer = false){
55                $ds = ldap_connect($_SESSION['phpgw_info']['expressomail']['ldap_server']['host']);
56
57                if (!$ds)
58                        return false;
59
60                ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
61                ldap_set_option($ds, LDAP_OPT_REFERRALS, $refer);
62                if ($refer)
63                        ldap_set_rebind_proc($ds, ldapRebind);
64                @ldap_bind($ds, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);
65
66                return $ds;
67        }
68
69
70        // usa o host e context do setup.
71        function ldapRootConnect($refer = false){
72                $this->ldap_host        = $_SESSION['phpgw_info']['expressomail']['server']['ldap_host'];
73                $this->ldap_context = $_SESSION['phpgw_info']['expressomail']['server']['ldap_context'];
74                $this->ds                       = ldap_connect($this->ldap_host);
75                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
76                ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
77                ldap_bind($this->ds, $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],$_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);
78
79        }
80
81        function quicksearch($params)
82        {
83                include_once("class.functions.inc.php");
84                $functions = new functions;
85
86                $search_for     = utf8_encode($params['search_for']);
87                $field          = $params['field'];
88                $ID                     = $params['ID'];
89               
90                $search_for     = explode(" ",$search_for);
91                $aux="";
92                foreach ($search_for as $search)
93                {
94                        if(!$aux)
95                        {
96                                $aux=$search;
97                        }
98                        else
99                        {
100                                if (strlen($search) > 2)
101                                {
102                                        $aux=$aux."*".$search;
103                                }
104                                else
105                                {
106                                        $aux=$aux." ".$search;
107                                }
108                        }
109                }
110                $search_for=$aux;
111               
112                $contacts_result = array();
113                $contacts_result['field'] = $field;
114                $contacts_result['ID'] = $ID;
115
116                // follow the referral
117                $this->ldapConnect(true);
118
119                if ($this->ds)
120                {
121                        if (($field != 'null') && ($ID != 'null'))
122                        {
123                                $filter="(& (&(|(phpgwAccountType=u)(phpgwAccountType=g)(phpgwAccountType=l))(mail=*)) (|(cn=*$search_for*)(mail=*$search_for*)) (!(phpgwaccountvisible=-1)) )";
124                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid", "employeeNumber", "ou");
125                        }
126                        else
127                        {
128                                $filter="(& (phpgwAccountType=u)(cn=*$search_for*) (!(phpgwaccountvisible=-1)) )";
129                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible","jpegPhoto", "uid", "employeeNumber", "ou");
130                        }
131                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result + 1);
132                        if(!$sr)
133                                return null;
134                        $count_entries = ldap_count_entries($this->ds,$sr);
135
136                        // Get user org dn.
137                        $user_dn = $_SESSION['phpgw_info']['expressomail']['user']['account_dn'];
138                        $user_sector_dn = ldap_explode_dn ( $user_dn, false );
139                        array_shift($user_sector_dn);
140                        array_shift($user_sector_dn);
141                        $user_sector_dn = implode(",", $user_sector_dn);
142
143                        // New search only on user sector
144                        if ($count_entries > $this->max_result)
145                        {
146                                // Close old ldap conection
147                                ldap_close($this->ds);
148
149                                // Reopen a local ldap connection, following referral
150                                $this->ldapRootConnect(true);
151
152                                $sr= ldap_search($this->ds, $user_sector_dn, $filter, $justthese);
153                                if(!$sr)
154                                        return null;
155                                $count_entries = ldap_count_entries($this->ds,$sr);
156
157                                if ($count_entries > $this->max_result){
158                                        $return = array();
159                                        $return['status'] = false;
160                                        $return['error'] = "many results";
161                                        return $return;
162                                }
163                                else
164                                {
165                                        $quickSearch_only_in_userSector = true;
166                                }
167                        }
168
169                        $info = ldap_get_entries($this->ds, $sr);
170
171                        $tmp = array();
172                        $tmp_users_from_user_org = array();
173
174                        if (!$quickSearch_only_in_userSector) {
175                                $catalogsNum=count($this->external_srcs);
176                                for ($i=0; $i<=count($this->external_srcs); $i++)       {
177                                        if ($this->external_srcs[$i]["quicksearch"]) {
178                                                $this->ldapConnect(true,$i);
179                                                $filter="(|(cn=*$search_for*)(mail=*$search_for*))";
180                                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid","employeeNumber", "ou");
181                                                $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result+1);
182                                                if(!$sr)
183                                                        return null;
184                                                $count_entries = ldap_count_entries($this->ds,$sr);
185                                                $search = ldap_get_entries($this->ds, $sr);
186                                                for ($j=0; $j<$search["count"]; $j++) {
187                                                        $info[] = $search[$j];
188                                                }
189                                                $info["count"] = count($info)-1;
190                                        }
191                                }
192                        }
193
194                        for ($i=0; $i<$info["count"]; $i++)
195                        {
196                                if ($quickSearch_only_in_userSector)
197                                {
198                                        $tmp[$info[$i]["mail"][0] . '%' . $info[$i]["telephonenumber"][0] . '%'. $info[$i]["mobile"][0] . '%' . $info[$i]["uid"][0] . '%' . $info[$i]["jpegphoto"]['count'] . '%' . $info[$i]["employeenumber"][0] . '%' . $info[$i]["ou"][0]] = utf8_decode($info[$i]["cn"][0]);
199                                }
200                                else
201                                {
202                                        if (preg_match("/$user_sector_dn/i", $info[$i]['dn']))
203                                        {
204                                                $tmp_users_from_user_org[$info[$i]["mail"][0] . '%' . $info[$i]["telephonenumber"][0] . '%'. $info[$i]["mobile"][0] . '%' . $info[$i]["uid"][0] . '%' . $info[$i]["jpegphoto"]['count'] . '%' . $info[$i]["employeenumber"][0] . '%' . $info[$i]["ou"][0]] = utf8_decode($info[$i]["cn"][0]);
205                                        }
206                                        else
207                                        {
208                                                $tmp[$info[$i]["mail"][0] . '%' . $info[$i]["telephonenumber"][0] . '%'. $info[$i]["mobile"][0] . '%' . $info[$i]["uid"][0] . '%' . $info[$i]["jpegphoto"]['count'] . '%' . $info[$i]["employeenumber"][0] . '%' . $info[$i]["ou"][0]] = utf8_decode($info[$i]["cn"][0]);
209                                        }
210                                }
211                        }
212                        natcasesort($tmp_users_from_user_org);
213                        natcasesort($tmp);
214
215                        if (($field != 'null') && ($ID != 'null'))
216                        {
217                                $i = 0;
218
219                                $tmp = array_merge($tmp, $tmp_users_from_user_org);
220                                natcasesort($tmp);
221
222                                foreach ($tmp as $info => $cn)
223                                {
224                                        $contacts_result[$i] = array();
225                                        $contacts_result[$i]["cn"] = $cn;
226                                        list ($contacts_result[$i]["mail"], $contacts_result[$i]["phone"], $contacts_result[$i]["mobile"], $contacts_result[$i]["uid"], $contacts_result[$i]["jpegphoto"], $contacts_result[$i]["employeenumber"], $contacts_result[$i]["ou"]) = split ('%', $info);
227                                        $i++;
228                                }
229                                $contacts_result['quickSearch_only_in_userSector'] = $quickSearch_only_in_userSector;
230                        }
231                        else
232                        {
233                                $options_users_from_user_org = '';
234                                $options = '';
235
236                                /* List of users from user org */
237                                $i = 0;
238                                foreach ($tmp_users_from_user_org as $info => $cn)
239                                {
240                                        $contacts_result[$i] = array();
241                                        $options_users_from_user_org .= $this->make_quicksearch_card($info, $cn);
242                                        $i++;
243                                }
244
245                                /* List of users from others org */
246                                foreach ($tmp as $info => $cn)
247                                {
248                                        $contacts_result[$i] = array();
249                                        $options .= $this->make_quicksearch_card($info, $cn);
250                                        $i++;
251                                }
252
253                                if ($quickSearch_only_in_userSector)
254                                {
255                                        if ($options != '')
256                                        {
257                                                $head_option =
258                                                        '<tr class="quicksearchcontacts_unselected">' .
259                                                                '<td colspan="2" width="100%" align="center">' .
260                                                                        str_replace("%1", $this->max_result,$this->functions->getLang('More than %1 results were found')) . '.<br>' .
261                                                                        $this->functions->getLang('Showing only the results found in your organization') . '.';
262                                                                '</td>' .
263                                                        '</tr>';
264                                                $contacts_result = $head_option . $options_users_from_user_org . $options;
265                                        }
266                                        else
267                                        {
268                                                $return = array();
269                                                $return['status'] = false;
270                                                $return['error'] = "many results";
271                                                return $return;
272                                        }
273                                }
274                                else
275                                {
276                                        if (($options_users_from_user_org != '') && ($options != ''))
277                                        {
278                                                $head_option0 =
279                                                        '<tr class="quicksearchcontacts_unselected">' .
280                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
281                                                                        $this->functions->getLang('Users from your organization') . '</B> ['.count($tmp_users_from_user_org).']';
282                                                                '</td>' .
283                                                        '</tr>';
284
285                                                $head_option1 =
286                                                        '<tr class="quicksearchcontacts_unselected">' .
287                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
288                                                                        $this->functions->getLang('Users from others organizations') . '</B> ['.count($tmp).']';
289                                                                '</td>' .
290                                                        '</tr>';
291                                        }
292                                        $contacts_result = $head_option0 . $options_users_from_user_org . $head_option1 . $options;
293                                }
294                        }
295                }
296                ldap_close($this->ds);
297                return $contacts_result;
298        }
299
300        function make_quicksearch_card($info, $cn)
301        {
302                include_once("class.functions.inc.php");
303                $functions = new functions;
304
305                $contacts_result = array();
306                $contacts_result["cn"] = $cn;
307                list ($contacts_result["mail"], $contacts_result["phone"], $contacts_result["mobile"], $contacts_result["uid"], $contacts_result["jpegphoto"]) = split ('%', $info);
308
309                if ($contacts_result['jpegphoto'])
310                        $photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result['mail'].'">';
311                else
312                        $photo_link = '<img src="./templates/default/images/photo.jpg">';
313
314                $phoneUser = $contacts_result['phone'];
315
316                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
317                        $phoneUser = '<a title="'.$this->functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
318                        if($contacts_result['mobile']){
319                                $phoneUser .= ' / <a title="'.$this->functions->getLang("Call to Mobile Number").'" href="#" onclick="InfoContact.connectVoip(\''.$contacts_result['mobile'].'\',\'mob\')">'.$contacts_result['mobile'].'</a>';
320                        }
321                }
322                // Begin: nickname, firstname and lastname for QuickAdd.
323                $fn = $contacts_result["cn"];
324                $array_name = explode(" ", $fn);
325                if(count($array_name) > 1){                     
326                        $fn = $array_name[0];
327                        array_shift($array_name);
328                        $sn = implode(" ",$array_name);
329                }
330                // End:
331                $option =
332                        '<tr class="quicksearchcontacts_unselected">' .
333                                '<td class="cc" width="1%">' .
334                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
335                                                $photo_link .
336                                        '</a>' .
337                                '</td>' .
338                                '<td class="cc">' .
339                                        '<span name="cn">' . $contacts_result['cn'] . '</span>' . '<br>' .
340                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
341                                                '<font color=blue>' .
342                                                '<span name="mail">' . $contacts_result['mail'] . '</span></a></font>'.
343                                                '<img src="templates/default/images/user_card.png" style="cursor: pointer;" title="'.$this->functions->getLang("Add Contact").'" onclick="javascript:connector.loadScript(\'ccQuickAdd\');ccQuickAddOne.showList(\''.$fn.','.$fn.','.$sn.','.$contacts_result["mail"].'\')">'.
344                                        '<br>' .
345                                        $phoneUser .
346                                '</td>' .
347                                '</tr>';
348                return $option;
349        }
350
351        function get_catalogs(){
352                $catalogs = array();
353                $catalogs[0] = $this->functions->getLang("Global Catalog");
354                if($this->external_srcs)
355                        foreach ($this->external_srcs as $key => $valor ){
356                        $catalogs[$key] = $valor['name'];
357                }
358                return $catalogs;
359        }
360        function get_organizations($params){
361                $organizations = array();
362                $params['referral']?$referral = $params['referral']:$referral = false;
363                $cat = $params['catalog'];
364
365                $this->ldapConnect($referral,$cat);
366
367                        if($this->branch != '') {
368                                $filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
369                                $justthese = array("$this->branch");
370                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
371                        $info = ldap_get_entries($this->ds, $sr);
372
373                        for ($i=0; $i<$info["count"]; $i++)
374                                $organizations[$i] = $info[$i]["ou"][0];
375
376                        ldap_close($this->ds);
377                        sort($organizations);
378                return $organizations;
379                        }else{
380                        return null;
381        }
382        }
383        function get_organizations2($params){
384                $organizations = array();
385                $referral = $params['referral'];
386                $this->ldapRootConnect($referral);
387                if ($this->ds) {
388                        $filter="(&(ou=*)(!(phpgwAccountVisible=-1)))";
389                        $justthese = array("ou");
390                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
391                        $info = ldap_get_entries($this->ds, $sr);
392
393                        for ($i=0; $i<$info["count"]; $i++)
394                        {
395                                $organizations[$i]['ou'] = $info[$i]["ou"][0];
396                                $organizations[$i]['dn'] = $info[$i]["dn"];
397                        }
398
399                        ldap_close($this->ds);
400                        sort($organizations);
401                }
402                return $organizations;
403        }
404       
405        //Busca usuarios de um contexto e ja retorna as options do select - usado por template serpro;
406        function search_users($params)
407        {
408                $this->ldapConnect(false,0);
409                //Monta lista de Grupos e Usuarios
410                $users = Array();
411                $groups = Array();
412                $user_context= $this->ldap_context;
413                $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
414                $filtro =utf8_encode($params['filter']);
415                $context =utf8_encode($params['context']);//adicionado
416
417                if ($this->ds)
418                {
419                    $justthese = array("gidNumber","cn");
420                    if ($params['type'] == 'search')
421                        //$sr=ldap_search($this->ds, $user_context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(cn=*$filtro*))"),$justthese);
422                        $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(cn=*$filtro*))"),$justthese);
423                    else
424                        $sr=ldap_list($this->ds, $user_context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(cn=*".$filtro."*))"),$justthese);
425                    $info = ldap_get_entries($this->ds, $sr);
426                    for ($i=0; $i<$info["count"]; $i++)
427                        $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
428                    $justthese = array("phpgwaccountvisible","uidNumber","cn");
429                    if ($params['type'] == 'search')
430                       // $sr=ldap_search($this->ds, $user_context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
431                       $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
432                    else
433                        $sr=ldap_list($this->ds, $user_context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
434
435                    $info = ldap_get_entries($this->ds, $sr);
436                    for ($i=0; $i<$info["count"]; $i++)
437                    {
438                        if ($info[$i]["phpgwaccountvisible"][0] == '-1')
439                      continue;
440                        $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
441                    }
442                }
443                ldap_close($this->ds);
444       
445                @asort($users);
446                @reset($users);
447                @asort($groups);
448                @reset($groups);
449                $user_options ='';
450                $group_options ='';
451
452                foreach($groups as $id => $user_array) {
453                        $newId = $id.'U';
454                        $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
455                }
456                foreach($users as $id => $user_array) {
457                    if($owner != $id){
458                        $newId = $id.'U';
459                        $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
460                    }
461                }
462                return array("users" => $user_options, "groups" => $group_options);
463        }
464
465        function catalogsearch($params)
466        {
467                $cn     = $params['search_for'] ? "*".utf8_encode($params['search_for'])."*" : "*";
468                $max_result       = $params['max_result'] ? $params['max_result'] : $this->max_result;
469                $catalog = $params['catalog'];
470                $error = False;
471
472                $this->ldapConnect(true,$catalog);
473
474                $params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;
475
476                if ($this->ds) {
477                        if ($catalog == 0){
478                                //os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
479                                $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible", "employeeNumber", "ou");
480                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(cn=".$cn."))";
481                                //$user_context = "ou=".$params['organization'].",".$this->ldap_context;
482                        }else {
483                                //os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
484                                $justthese = array("cn", "mail", "employeeNumber", "ou");
485                                $filter="(&(objectClass=".$this->object_class.")(cn=".$cn."))";
486                                //$user_context = $this->branch."=".$params['organization'].",".$external_srcs[$catalog]['dn'];
487                        }
488
489                        $sr=@ldap_search($this->ds, $user_context, $filter, $justthese, 0, $max_result+1);
490                        if(!$sr)
491                                return null;
492                        $count_entries = ldap_count_entries($this->ds,$sr);
493                        if ($count_entries > $max_result){
494                                $info = null;
495                                $error = True;
496                        }
497                        else
498                                $info = ldap_get_entries($this->ds, $sr);
499
500                        ldap_close($this->ds);
501
502                        $u_tmp = array();
503                        $g_tmp = array();
504
505                        for ($i=0; $i<$info["count"]; $i++){
506                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
507                                        //aqui eh feita a concatenacao do departamento ao cn;
508                                        $u_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]). '%' . $info[$i]["ou"][0];
509                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'L') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
510                                        $g_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]);
511                        }
512
513                        natcasesort($u_tmp);
514                        natcasesort($g_tmp);
515
516                        $i = 0;
517                        $users = array();
518
519                        foreach ($u_tmp as $mail => $cn){
520
521                                $tmp = explode("%", $cn); //explode o cn pelo caracter "%" e joga em $tmp;
522                                $name = $tmp[0]; //pega o primeiro item (cn) do vetor resultante do explode acima;
523                                $department = $tmp[1]; //pega o segundo item (ou) do vetor resultanto do explode acima;
524                                $users[$i++] = array("name" => $name, "email" => $mail, "department" => $department);
525
526                        }
527                        unset($u_tmp);
528
529                        $i = 0;
530                        $groups = array();
531
532                        foreach ($g_tmp as $mail => $cn){
533                                $groups[$i++] = array("name" => $cn, "email" => $mail);
534                        }
535                        unset($g_tmp);
536
537                        return  array('users' => $users, 'groups' => $groups, 'error' => $error);
538                }else
539                return null;
540        }
541
542        function get_emails_ldap(){
543
544                $result['mail']= array();
545                $result['mailalter']= array();
546                $user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
547                $this->ldapRootConnect(false);
548                if ($this->ds) {
549                        $filter="uid=".$user;
550                        $justthese = array("mail","mailAlternateAddress");
551                        $sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
552                        $ent = ldap_get_entries($this->ds, $sr);
553                        ldap_close($this->ds);
554
555                        for ($i=0; $i<$ent["count"]; $i++){
556                                $result['mail'][] = $ent[$i]["mail"][0];
557                                $result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];
558                        }
559                }
560                return $result;
561        }
562
563        //Busca usuarios de um contexto e ja retorna as options do select;
564        function get_available_users($params)
565    {
566        $this->ldapRootConnect();
567        //Monta lista de Grupos e Usuarios
568        $users = Array();
569        $groups = Array();
570        $user_context= $params['context'];
571        $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
572
573        if ($this->ds)
574        {
575            $justthese = array("gidNumber","cn");
576            if ($params['type'] == 'search')
577                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
578            else
579                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
580            $info = ldap_get_entries($this->ds, $sr);
581            for ($i=0; $i<$info["count"]; $i++)
582                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
583            $justthese = array("phpgwaccountvisible","uidNumber","cn");
584            if ($params['type'] == 'search')
585                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
586            else
587                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
588
589            $info = ldap_get_entries($this->ds, $sr);
590            for ($i=0; $i<$info["count"]; $i++)
591            {
592                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
593                    continue;
594                $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
595            }
596        }
597        ldap_close($this->ds);
598
599        @asort($users);
600        @reset($users);
601        @asort($groups);
602        @reset($groups);
603        $user_options ='';
604        $group_options ='';
605
606        foreach($groups as $id => $user_array) {
607                $newId = $id.'U';
608                $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
609        }
610        foreach($users as $id => $user_array) {
611            if($owner != $id){
612                $newId = $id.'U';
613                $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
614            }
615        }
616        return array("users" => $user_options, "groups" => $group_options);
617    }
618
619        //Busca usuarios de um contexto e ja retorna as options do select;
620        function get_available_users2($params)
621        {
622                $this->ldapRootConnect();
623
624                $context= $params['context'];
625                $justthese = array("cn", "uid", "cn");
626                $filter = "(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))";
627
628            if ($this->ds)
629            {
630                        $sr=ldap_search($this->ds, $context, $filter, $justthese);
631                        $entries = ldap_get_entries($this->ds, $sr);
632
633                        for ($i=0; $i<$entries["count"]; $i++){
634                                if($_SESSION['phpgw_info']['expressomail']['user']['account_lid'] != $entries[$i]["uid"][0]){
635                                        $u_tmp[$entries[$i]["uid"][0]] = $entries[$i]["cn"][0];
636                                }
637                        }
638
639                        natcasesort($u_tmp);
640
641                        $i = 0;
642                        $users = array();
643
644                        if (count($u_tmp))
645                        {
646                                foreach ($u_tmp as $uidnumber => $cn)
647                                {
648                                        $options .= "<option value=$uidnumber>$cn</option>";
649                                }
650                                unset($u_tmp);
651                        }
652
653                        ldap_close($this->ds);
654                return $options;
655                }
656        }
657
658        function uid2cn($uid)
659        {
660                // do not follow the referral
661                $this->ldapRootConnect(false);
662                if ($this->ds)
663                {
664                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
665                        $justthese = array("cn");
666                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
667                        if(!$sr)
668                                return false;
669                        $info = ldap_get_entries($this->ds, $sr);
670                        return utf8_decode($info[0]["cn"][0]);
671                }
672                return false;
673        }
674        function uidnumber2uid($uidnumber)
675        {
676                // do not follow the referral
677                $this->ldapRootConnect(false);
678                if ($this->ds)
679                {
680                        $filter="(&(phpgwAccountType=u)(uidnumber=$uidnumber))";
681                        $justthese = array("uid");
682                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
683                        if(!$sr)
684                                return false;
685                        $info = ldap_get_entries($this->ds, $sr);
686                        return $info[0]["uid"][0];
687                }
688                return false;
689        }
690        function getSharedUsersFrom($params){
691                $filter = '';
692                $i = 0;
693                if($params['uids']) {
694                        $uids = explode(";",$params['uids']);
695                        $this->imap = new imap_functions();
696                        foreach($uids as $index => $uid){
697                                $params = array();
698                                $acl = $this->imap->getacltouser($uid);
699                                if(preg_match("/a/",$acl))
700                                        $filter .= "(uid=$uid)";
701                        }
702                }
703
704                $this->ldapRootConnect(false);
705                if ($this->ds) {
706                        $justthese = array("cn","mail");
707                        if($filter) {
708                                $filter="(&(phpgwAccountType=u)(|$filter))";
709                                $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
710                                ldap_sort($this->ds,$sr,"cn");
711                                $info   =       ldap_get_entries($this->ds, $sr);
712                                for ($i = 0;$i < $info["count"]; $i++)
713                                        $info[$i]['cn'][0] = utf8_decode($info[$i]['cn'][0]);
714                        }
715                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
716
717                        //Find institucional_account.
718                        $filter="(&(phpgwAccountType=i)(mailForwardingAddress=".$_SESSION['phpgw_info']['expressomail']['user']['email']."))";
719                        $sr     = ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
720                        ##
721                        # @AUTHOR Rodrigo Souza dos Santos
722                        # @DATE 2008/09/17
723                        # @BRIEF Changing to ensure that the variable session is always with due value.
724                        ##
725                        if(ldap_count_entries($this->ds,$sr))
726                        {
727                                ldap_sort($this->ds,$sr,"cn");
728                                $result = ldap_get_entries($this->ds, $sr);
729                                for ($j = 0;$j < $result["count"]; $j++){
730                                        $info[$i]['cn'][0] = utf8_decode($result[$j]['cn'][0]);
731                                        $info[$i++]['mail'][0] = $result[$j]['mail'][0];
732                                }
733                        }
734
735                        $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] = $info;
736
737                        return $info;
738                }
739        }
740
741        function getUserByEmail($params)
742        {
743                $filter="(&(phpgwAccountType=u)(mail=" . $params['email'] . "))";
744                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
745                $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile");
746
747                // Follow the referral
748                $ds = $this->ldapConnect2(true);
749                if ($ds)
750                {
751                        $sr=@ldap_search($ds, $ldap_context, $filter, $justthese);
752
753                        if (!$sr)
754                                return null;
755
756                        $entry = ldap_first_entry($ds, $sr);
757
758                        if($entry) {
759                                $obj =  array("cn" => utf8_decode(current(ldap_get_values($ds, $entry, "cn"))),
760                                                  "email" => $params['email'],
761                                                  "uid" => ldap_get_values($ds, $entry, "uid"),
762                                                  "type" => "global",
763                                                  "mobile" =>  @ldap_get_values($ds, $entry, "mobile"),
764                                                  "telefone" =>  @ldap_get_values($ds, $entry, "telephonenumber")
765                                        );
766
767                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] = @ldap_get_values_len($ds, $entry, "jpegphoto");
768                                ldap_close($ds);
769                                return $obj;
770                        }
771                }
772                return null;
773        }
774       
775        function uid2uidnumber($uid)
776        {
777                // do not follow the referral
778                $this->ldapRootConnect(false);
779                if ($this->ds)
780                {
781                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
782                        $justthese = array("uidnumber");
783                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
784                        if(!$sr)
785                                return false;
786                        $info = ldap_get_entries($this->ds, $sr);
787                        return $info[0]["uidnumber"][0];
788                }
789                return false;
790        }
791}
792?>
Note: See TracBrowser for help on using the repository browser.