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

Revision 4929, 27.3 KB checked in by roberto.santosjunior, 13 years ago (diff)

Ticket #1820 - Pesquisa rápida de contatos com aspas.r4906

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