source: branches/2.3/expressoMail1_2/inc/class.ldap_functions.inc.php @ 4906

Revision 4906, 31.2 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #2186 - Pesquisa rápida de contatos com aspas

  • 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= ( isset( $external_srcs ) ) ? $external_srcs : NULL;
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  //Testa se a busca foi realizada com aspas
88                $search_for     = utf8_encode($params['search_for']);
89                $search_len = (strlen($search_for)) - 1;
90                $quote_status=false;
91
92                if((($search_for{0}) == "\"") && (($search_for{$search_len}) == "\"")){
93                    $search_for{0} = " ";
94                    $search_for{$search_len} = " ";
95                    $quote_status=true;
96                }
97
98
99
100                $field          = $params['field'];
101                $ID                     = $params['ID'];
102               
103                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
104                    $extendedinfo=true;
105                else
106                    $extendedinfo=false;
107
108                $search_for     = explode(" ",$search_for);
109                $aux="";
110                foreach ($search_for as $search)
111                {
112                        if(!$aux)
113                        {
114                                $aux=$search;
115                        }
116                        else
117                        {
118                                if (strlen($search) > 2)
119                                {
120                                        $aux=$aux."*".$search;
121                                }
122                                else
123                                {
124                                        $aux=$aux." ".$search;
125                                }
126                        }
127                }
128                $search_for=$aux;
129               
130                $contacts_result = array();
131                $contacts_result['field'] = $field;
132                $contacts_result['ID'] = $ID;
133                // follow the referral
134                $this->ldapConnect(true);
135
136                if ($this->ds)
137                {
138                        if (($field != 'null') && ($ID != 'null'))
139                        {
140                                $filter="(& (&(|(phpgwAccountType=u)(phpgwAccountType=g)(phpgwAccountType=l))(mail=*)) (|(cn=*$search_for*)(mail=*$search_for*)) (!(phpgwaccountvisible=-1)) )";
141                                if($extendedinfo)
142                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid", "employeeNumber", "ou");
143                                else
144                                    $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid");
145                        }
146                        else
147                        {
148                                if($quote_status){
149                                        $filter="(& (phpgwAccountType=u) (|(cn=* $search_for *)(cn=$search_for *)(cn=* $search_for)) (!(phpgwaccountvisible=-1)) )";
150                                }
151                                else{
152                                        $filter="(& (phpgwAccountType=u)(cn=*$search_for*) (!(phpgwaccountvisible=-1)) )";
153                                }
154                        //      $filter="(& (phpgwAccountType=u)(cn=*$search_for*) (!(phpgwaccountvisible=-1)) )";
155                                if($extendedinfo)
156                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible","jpegPhoto", "uid", "employeeNumber", "ou");
157                                else
158                                    $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible","jpegPhoto", "uid");
159                        }
160                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result + 1);
161                        if(!$sr)
162                                return null;
163                        $count_entries = ldap_count_entries($this->ds,$sr);
164
165                        // Get user org dn.
166                        $user_dn = $_SESSION['phpgw_info']['expressomail']['user']['account_dn'];
167                        $user_sector_dn = ldap_explode_dn ( $user_dn, false );
168                        array_shift($user_sector_dn);
169                        array_shift($user_sector_dn);
170                        $user_sector_dn = implode(",", $user_sector_dn);
171
172                        // New search only on user sector
173                        if ($count_entries > $this->max_result)
174                        {
175                                // Close old ldap conection
176                                ldap_close($this->ds);
177
178                                // Reopen a local ldap connection, following referral
179                                $this->ldapRootConnect(true);
180
181                                $sr= ldap_search($this->ds, $user_sector_dn, $filter, $justthese);
182                                if(!$sr)
183                                        return null;
184                                $count_entries = ldap_count_entries($this->ds,$sr);
185
186                                if ($count_entries > $this->max_result){
187                                        $return = array();
188                                        $return['status'] = false;
189                                        $return['error'] = "many results";
190                                        return $return;
191                                }
192                                else
193                                {
194                                        $quickSearch_only_in_userSector = true;
195                                }
196                        }
197
198                        $info = ldap_get_entries($this->ds, $sr);
199
200                        $tmp = array();
201                        $tmp_users_from_user_org = array();
202
203                        if (!$quickSearch_only_in_userSector) {
204                                $catalogsNum=count($this->external_srcs);
205                                for ($i=0; $i<=count($this->external_srcs); $i++)       {
206                                        if ($this->external_srcs[$i]["quicksearch"]) {
207                                                $this->ldapConnect(true,$i);
208                                                $filter="(|(cn=*$search_for*)(mail=*$search_for*))";
209                                                if($extendedinfo)
210                                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid","employeeNumber", "ou");
211                                                else
212                                                    $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid");
213                                                $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result+1);
214                                                if(!$sr)
215                                                        return null;
216                                                $count_entries = ldap_count_entries($this->ds,$sr);
217                                                $search = ldap_get_entries($this->ds, $sr);
218                                                for ($j=0; $j<$search["count"]; $j++) {
219                                                        $info[] = $search[$j];
220                                                }
221                                                $info["count"] = count($info)-1;
222                                        }
223                                }
224                        }
225
226                        for ($i=0; $i<$info["count"]; $i++)
227                        {
228                                if ($quickSearch_only_in_userSector)
229                                {
230                                        $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]);
231                                }
232                                else
233                                {
234                                        if (preg_match("/$user_sector_dn/i", $info[$i]['dn']))
235                                        {
236                                                $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]);
237                                        }
238                                        else
239                                        {
240                                                $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]);
241                                        }
242                                }
243                        }
244                        natcasesort($tmp_users_from_user_org);
245                        natcasesort($tmp);
246
247                        if (($field != 'null') && ($ID != 'null'))
248                        {
249                                $i = 0;
250
251                                $tmp = array_merge($tmp, $tmp_users_from_user_org);
252                                natcasesort($tmp);
253
254                                foreach ($tmp as $info => $cn)
255                                {
256                                        $contacts_result[$i] = array();
257                                        $contacts_result[$i]["cn"] = $cn;
258                                        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);
259                                        $i++;
260                                }
261                                $contacts_result['quickSearch_only_in_userSector'] = $quickSearch_only_in_userSector;
262                        }
263                        else
264                        {
265                                $options_users_from_user_org = '';
266                                $options = '';
267
268                                /* List of users from user org */
269                                $i = 0;
270                                foreach ($tmp_users_from_user_org as $info => $cn)
271                                {
272                                        $contacts_result[$i] = array();
273                                        $options_users_from_user_org .= $this->make_quicksearch_card($info, $cn);
274                                        $i++;
275                                }
276
277                                /* List of users from others org */
278                                foreach ($tmp as $info => $cn)
279                                {
280                                        $contacts_result[$i] = array();
281                                        $options .= $this->make_quicksearch_card($info, $cn);
282                                        $i++;
283                                }
284
285                                if ($quickSearch_only_in_userSector)
286                                {
287                                        if ($options != '')
288                                        {
289                                                $head_option =
290                                                        '<tr class="quicksearchcontacts_unselected">' .
291                                                                '<td colspan="2" width="100%" align="center">' .
292                                                                        str_replace("%1", $this->max_result,$this->functions->getLang('More than %1 results were found')) . '.<br>' .
293                                                                        $this->functions->getLang('Showing only the results found in your organization') . '.';
294                                                                '</td>' .
295                                                        '</tr>';
296                                                $contacts_result = $head_option . $options_users_from_user_org . $options;
297                                        }
298                                        else
299                                        {
300                                                $return = array();
301                                                $return['status'] = false;
302                                                $return['error'] = "many results";
303                                                return $return;
304                                        }
305                                }
306                                else
307                                {
308                                        if (($options_users_from_user_org != '') && ($options != ''))
309                                        {
310                                                $head_option0 =
311                                                        '<tr class="quicksearchcontacts_unselected">' .
312                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
313                                                                        $this->functions->getLang('Users from your organization') . '</B> ['.count($tmp_users_from_user_org).']';
314                                                                '</td>' .
315                                                        '</tr>';
316
317                                                $head_option1 =
318                                                        '<tr class="quicksearchcontacts_unselected">' .
319                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
320                                                                        $this->functions->getLang('Users from others organizations') . '</B> ['.count($tmp).']';
321                                                                '</td>' .
322                                                        '</tr>';
323                                        }
324                                        $contacts_result = $head_option0 . $options_users_from_user_org . $head_option1 . $options;
325                                }
326                        }
327                }
328                ldap_close($this->ds);
329                return $contacts_result;
330        }
331
332        function make_quicksearch_card($info, $cn)
333        {
334                include_once("class.functions.inc.php");
335                $functions = new functions;
336
337                $contacts_result = array();
338                $contacts_result["cn"] = $cn;
339                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
340                    $extendedinfo=true;
341                else
342                    $extendedinfo=false;
343
344                list ($contacts_result["mail"], $contacts_result["phone"], $contacts_result["mobile"], $contacts_result["uid"], $contacts_result["jpegphoto"], $contacts_result["employeenumber"], $contacts_result["ou"]) = split ('%', $info);
345
346                if ($contacts_result['jpegphoto'])
347                        $photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result['mail'].'">';
348                else
349                        $photo_link = '<img src="./templates/default/images/photo.jpg">';
350
351                $phoneUser = $contacts_result['phone'];
352
353                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
354                        $phoneUser = '<a title="'.$this->functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
355                        if($contacts_result['mobile']){
356                                $phoneUser .= ' / <a title="'.$this->functions->getLang("Call to Mobile Number").'" href="#" onclick="InfoContact.connectVoip(\''.$contacts_result['mobile'].'\',\'mob\')">'.$contacts_result['mobile'].'</a>';
357                        }
358                }
359                //verifica de existe o celular do funcionario ($contacts_result["mobile"]), se nao, $phoneUser fica vazia;
360
361                    $mobileUser = $contacts_result["mobile"];
362                    if($mobileUser && !$voipMobile) {
363                            $phoneUser .= " / $mobileUser";
364                    }
365                    $empNumber = $contacts_result["employeenumber"];
366                    if($empNumber) {
367                            $empNumber = "$empNumber - ";
368                    }
369                    $ou = $contacts_result["ou"];
370                    if($ou) {
371                            $ou = "<br/>$ou" ;
372                    }
373
374                // Begin: nickname, firstname and lastname for QuickAdd.
375                $fn = $contacts_result["cn"];
376                $array_name = explode(" ", $fn);
377                if(count($array_name) > 1){                     
378                        $fn = $array_name[0];
379                        array_shift($array_name);
380                        $sn = implode(" ",$array_name);
381                }
382                // End:
383                $option =
384                        '<tr class="quicksearchcontacts_unselected">' .
385                                '<td class="cc" width="1%">' .
386                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
387                                                $photo_link .
388                                        '</a>' .
389                                '</td>' .
390                                '<td class="cc">' .
391                                        '<span name="cn">' . $empNumber . $contacts_result['cn'] . '</span>' . '<br>' .
392                                        '<a title="'.$functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
393                                                '<font color=blue>' .
394                                                '<span name="mail">' . $contacts_result['mail'] . '</span></a></font>'.
395                                                '<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"].'\')">'.
396                                        '<br>' .
397                                        $phoneUser .
398                                        $ou .
399                                '</td>' .
400                                '</tr>';
401                return $option;
402        }
403
404        function get_catalogs(){
405                $catalogs = array();
406                $catalogs[0] = $this->functions->getLang("Global Catalog");
407                if($this->external_srcs)
408                        foreach ($this->external_srcs as $key => $valor ){
409                        $catalogs[$key] = $valor['name'];
410                }
411                return $catalogs;
412        }
413        function get_organizations($params){
414                $organizations = array();
415                $params['referral']?$referral = $params['referral']:$referral = false;
416                $cat = $params['catalog'];
417
418                $this->ldapConnect($referral,$cat);
419
420                        if($this->branch != '') {
421                                $filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
422                                $justthese = array("$this->branch");
423                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
424                        $info = ldap_get_entries($this->ds, $sr);
425
426                        if($info["count"] == 0)
427                        {
428                            $organizations[0]['ou'] = $this->ldap_context;
429                        }
430
431                        for ($i=0; $i<$info["count"]; $i++)
432                                $organizations[$i] = $info[$i]["ou"][0];
433
434                        ldap_close($this->ds);
435                        sort($organizations);
436                return $organizations;
437                        }else{
438                        return null;
439        }
440        }
441        function get_organizations2($params){
442                $organizations = array();
443                $referral = $params['referral'];
444                $this->ldapRootConnect($referral);
445                if ($this->ds) {
446                        $filter="(&(objectClass=organizationalUnit)(!(phpgwAccountVisible=-1)))";
447                        $justthese = array("ou");
448                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
449                        $info = ldap_get_entries($this->ds, $sr);
450
451
452                        if($info["count"] == 0)
453                        {
454                            $organizations[0]['ou'] = $this->ldap_context;
455                            $organizations[0]['dn'] = $this->ldap_context;
456                        }
457                        else{
458                            for ($i=0; $i<$info["count"]; $i++)
459                            {
460                                    $organizations[$i]['ou'] = $info[$i]["ou"][0];
461                                    $organizations[$i]['dn'] = $info[$i]["dn"];
462                            }
463                        }
464                        ldap_close($this->ds);
465                        sort($organizations);
466                }
467                return $organizations;
468        }
469        //Busca usuarios de um contexto e ja retorna as options do select - usado por template serpro;
470        function search_users($params)
471        {
472                $this->ldapConnect(false,0);
473                //Monta lista de Grupos e Usuarios
474                $users = Array();
475                $groups = Array();
476                $user_context= $this->ldap_context;
477                $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
478                $filtro =utf8_encode($params['filter']);
479                $context =utf8_encode($params['context']);//adicionado
480
481                if ($this->ds)
482                {
483                    $justthese = array("gidNumber","cn");
484                    if ($params['type'] == 'search')
485                        $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(cn=*$filtro*))"),$justthese);
486                    else
487                        $sr=ldap_list($this->ds,  $context ? $context : $user_context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(cn=*".$filtro."*))"),$justthese);
488                    $info = ldap_get_entries($this->ds, $sr);
489                    for ($i=0; $i<$info["count"]; $i++)
490                        $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
491                    $justthese = array("phpgwaccountvisible","uidNumber","cn");
492                    if ($params['type'] == 'search')
493                       // $sr=ldap_search($this->ds, $user_context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
494                       $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
495                    else
496                        $sr=ldap_list($this->ds, $context ? $context : $user_context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
497
498                    $info = ldap_get_entries($this->ds, $sr);
499                    for ($i=0; $i<$info["count"]; $i++)
500                    {
501                        if ($info[$i]["phpgwaccountvisible"][0] == '-1')
502                      continue;
503                        $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
504                    }
505                }
506                ldap_close($this->ds);
507       
508                @asort($users);
509                @reset($users);
510                @asort($groups);
511                @reset($groups);
512                $user_options ='';
513                $group_options ='';
514
515                foreach($groups as $id => $user_array) {
516                        $newId = $id.'U';
517                        $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
518                }
519                foreach($users as $id => $user_array) {
520                    if($owner != $id){
521                        $newId = $id.'U';
522                        $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
523                    }
524                }
525                return array("users" => $user_options, "groups" => $group_options);
526        }
527
528        function catalogsearch($params)
529        {
530                $cn     = $params['search_for'] ? "*".utf8_encode($params['search_for'])."*" : "*";
531                $max_result       = $params['max_result'] ? $params['max_result'] : $this->max_result;
532                $catalog = $params['catalog'];
533                $error = False;
534                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
535                    $extendedinfo=true;
536                else
537                    $extendedinfo=false;
538
539
540                $this->ldapConnect(true,$catalog);
541
542                $params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;
543
544                if ($this->ds) {
545                        if ($catalog == 0){
546                                //os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
547                                if($extendedinfo)
548                                $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible", "employeeNumber", "ou");
549                                else
550                                    $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible");
551
552                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(cn=".$cn."))";
553                                //$user_context = "ou=".$params['organization'].",".$this->ldap_context;
554                        }else {
555                                //os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
556                                if($extendedinfo)
557                                $justthese = array("cn", "mail", "employeeNumber", "ou");
558                                else
559                                    $justthese = array("cn", "mail");
560                                $filter="(&(objectClass=".$this->object_class.")(cn=".$cn."))";
561                                //$user_context = $this->branch."=".$params['organization'].",".$external_srcs[$catalog]['dn'];
562                        }
563
564                        $sr=@ldap_search($this->ds, $user_context, $filter, $justthese, 0, $max_result+1);
565                        if(!$sr)
566                                return null;
567                        $count_entries = ldap_count_entries($this->ds,$sr);
568                        if ($count_entries > $max_result){
569                                $info = null;
570                                $error = True;
571                        }
572                        else
573                                $info = ldap_get_entries($this->ds, $sr);
574
575                        ldap_close($this->ds);
576
577                        $u_tmp = array();
578                        $g_tmp = array();
579
580                        for ($i=0; $i<$info["count"]; $i++){
581                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
582                                        //aqui eh feita a concatenacao do departamento ao cn;
583                                        $u_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]). '%' . $info[$i]["ou"][0];
584                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'L') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
585                                        $g_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]);
586                        }
587
588                        natcasesort($u_tmp);
589                        natcasesort($g_tmp);
590
591                        $i = 0;
592                        $users = array();
593
594                        foreach ($u_tmp as $mail => $cn){
595
596                                $tmp = explode("%", $cn); //explode o cn pelo caracter "%" e joga em $tmp;
597                                $name = $tmp[0]; //pega o primeiro item (cn) do vetor resultante do explode acima;
598                                $department = $tmp[1]; //pega o segundo item (ou) do vetor resultanto do explode acima;
599                                $users[$i++] = array("name" => $name, "email" => $mail, "department" => $department);
600
601                        }
602                        unset($u_tmp);
603
604                        $i = 0;
605                        $groups = array();
606
607                        foreach ($g_tmp as $mail => $cn){
608                                $groups[$i++] = array("name" => $cn, "email" => $mail);
609                        }
610                        unset($g_tmp);
611
612                        return  array('users' => $users, 'groups' => $groups, 'error' => $error);
613                }else
614                return null;
615        }
616
617        function get_emails_ldap(){
618
619                $result['mail']= array();
620                $result['mailalter']= array();
621                $user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
622                $this->ldapRootConnect(false);
623                if ($this->ds) {
624                        $filter="uid=".$user;
625                        $justthese = array("mail","mailAlternateAddress");
626                        $sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
627                        $ent = ldap_get_entries($this->ds, $sr);
628                        ldap_close($this->ds);
629
630                        for ($i=0; $i<$ent["count"]; $i++){
631                                $result['mail'][] = $ent[$i]["mail"][0];
632                                $result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];
633                        }
634                }
635                return $result;
636        }
637
638        //Busca usuarios de um contexto e ja retorna as options do select;
639        function get_available_users($params)
640    {
641        $this->ldapRootConnect();
642        //Monta lista de Grupos e Usuarios
643        $users = Array();
644        $groups = Array();
645        $user_context= $params['context'];
646        $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
647
648        if ($this->ds)
649        {
650            $justthese = array("gidNumber","cn");
651            if ($params['type'] == 'search')
652                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
653            else
654                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
655            $info = ldap_get_entries($this->ds, $sr);
656            for ($i=0; $i<$info["count"]; $i++)
657                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
658            $justthese = array("phpgwaccountvisible","uidNumber","cn");
659            if ($params['type'] == 'search')
660                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
661            else
662                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
663
664            $info = ldap_get_entries($this->ds, $sr);
665            for ($i=0; $i<$info["count"]; $i++)
666            {
667                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
668                    continue;
669                $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
670            }
671        }
672        ldap_close($this->ds);
673
674        @asort($users);
675        @reset($users);
676        @asort($groups);
677        @reset($groups);
678        $user_options ='';
679        $group_options ='';
680
681        foreach($groups as $id => $user_array) {
682                $newId = $id.'U';
683                $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
684        }
685        foreach($users as $id => $user_array) {
686            if($owner != $id){
687                $newId = $id.'U';
688                $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
689            }
690        }
691        return array("users" => $user_options, "groups" => $group_options);
692    }
693
694        //Busca usuarios de um contexto e ja retorna as options do select;
695        function get_available_users2($params)
696        {
697                $this->ldapRootConnect();
698
699                $context= $params['context'];
700                $justthese = array("cn", "uid", "cn");
701                $filter = "(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))";
702
703            if ($this->ds)
704            {
705                        $sr=ldap_search($this->ds, $context, $filter, $justthese);
706                        $entries = ldap_get_entries($this->ds, $sr);
707
708                        for ($i=0; $i<$entries["count"]; $i++){
709                                if($_SESSION['phpgw_info']['expressomail']['user']['account_lid'] != $entries[$i]["uid"][0]){
710                                        $u_tmp[$entries[$i]["uid"][0]] = $entries[$i]["cn"][0];
711                                }
712                        }
713
714                        natcasesort($u_tmp);
715
716                        $i = 0;
717                        $users = array();
718
719                        if (count($u_tmp))
720                        {
721                                foreach ($u_tmp as $uidnumber => $cn)
722                                {
723                                        $options .= "<option value=$uidnumber>$cn</option>";
724                                }
725                                unset($u_tmp);
726                        }
727
728                        ldap_close($this->ds);
729                return $options;
730                }
731        }
732
733        function uid2cn($uid)
734        {
735                // do not follow the referral
736                $this->ldapRootConnect(false);
737                if ($this->ds)
738                {
739                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
740                        $justthese = array("cn");
741                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
742                        if(!$sr)
743                                return false;
744                        $info = ldap_get_entries($this->ds, $sr);
745                        return utf8_decode($info[0]["cn"][0]);
746                }
747                return false;
748        }
749        function uidnumber2uid($uidnumber)
750        {
751                // do not follow the referral
752                $this->ldapRootConnect(false);
753                if ($this->ds)
754                {
755                        $filter="(&(phpgwAccountType=u)(uidnumber=$uidnumber))";
756                        $justthese = array("uid");
757                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
758                        if(!$sr)
759                                return false;
760                        $info = ldap_get_entries($this->ds, $sr);
761                        return $info[0]["uid"][0];
762                }
763                return false;
764        }
765        function getSharedUsersFrom($params){
766                $filter = '';
767                $i = 0;         
768                //Added to save if must save sent messages in shared folder
769                $acl_save_sent_in_shared = array();
770               
771                if($params['uids']) {
772                        $uids = explode(";",$params['uids']);
773                        $this->imap = new imap_functions();                     
774                        foreach($uids as $index => $uid){
775                                $params = array();
776                                //Added to save if user has create permission
777                                $acl_create_message = array();
778                                $acl = $this->imap->getacltouser($uid);
779                                if ( preg_match("/a/",$acl )){                         
780                                        $filter .= "(uid=$uid)";                                       
781                                        if ( preg_match("/p/",$acl )){                         
782                                                $acl_save_sent_in_shared[ $i ] =$uid;
783                                                $i++;
784                                        }                                       
785                                }                                                       
786                        }                       
787                }
788               
789                $this->ldapRootConnect(false);
790                if ($this->ds) {
791                        $justthese = array("cn","mail","uid");
792                        if($filter) {
793                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=s))(|$filter))";
794                                $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
795                                ldap_sort($this->ds,$sr,"cn");
796                                $info   =       ldap_get_entries($this->ds, $sr);
797                                $var = print_r($acl_save_sent_in_shared, true);                         
798                                for ($i = 0;$i < $info["count"]; $i++){
799                                        $info[$i]['cn'][0] = utf8_decode($info[$i]['cn'][0]);
800                                        //verify if user has permission to save sent messages in a shared folder
801                                        if ( in_array( $info[$i]['uid'][0],$acl_save_sent_in_shared) ){                                         
802                                                $info[$i]['save_shared'][0] = 'y';
803                                        } else $info[$i]['save_shared'][0] = 'n';
804                                }
805                        }
806               
807                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
808
809                        //Find institucional_account.
810                        $filter="(&(phpgwAccountType=i)(mailForwardingAddress=".$_SESSION['phpgw_info']['expressomail']['user']['email']."))";
811                        $sr     = ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
812                        ##
813                        # @AUTHOR Rodrigo Souza dos Santos
814                        # @DATE 2008/09/17
815                        # @BRIEF Changing to ensure that the variable session is always with due value.
816                        ##
817                        if(ldap_count_entries($this->ds,$sr))
818                        {
819                                ldap_sort($this->ds,$sr,"cn");
820                                $result = ldap_get_entries($this->ds, $sr);
821                                for ($j = 0;$j < $result["count"]; $j++){
822                                        $info[$i]['cn'][0] = utf8_decode($result[$j]['cn'][0]);
823                                        $info[$i]['mail'][0] = $result[$j]['mail'][0];
824                                        $info[$i]['save_shared'][0] = 'n';
825                                        $info[$i++]['uid'][0] = $result[$j]['uid'][0];                                 
826                                }
827                        }
828
829                        $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] = $info;
830                       
831                        return $info;
832                }
833        }
834
835        function getUserByEmail($params)
836        {
837                $expires = 60*60*24*30; /* 30 days */
838                header("Cache-Control: maxage=".$expires);
839                header("Pragma: public");
840                header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires)); 
841                $filter="(&(phpgwAccountType=u)(mail=" . $params['email'] . "))";
842                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
843                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
844                    $extendedinfo=true;
845                else
846                    $extendedinfo=false;
847
848                if($extendedinfo)
849                    $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile","ou","employeeNumber");
850                else
851                    $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile");
852
853                // Follow the referral
854                $ds = $this->ldapConnect2(true);
855                if ($ds)
856                {
857                        $sr=@ldap_search($ds, $ldap_context, $filter, $justthese);
858
859                        if (!$sr)
860                                return null;
861
862                        $entry = ldap_first_entry($ds, $sr);
863
864                        if($entry) {
865                                $obj =  array("cn" => utf8_decode(current(ldap_get_values($ds, $entry, "cn"))),
866                                                  "email" => $params['email'],
867                                                  "uid" => ldap_get_values($ds, $entry, "uid"),
868                                                  "type" => "global",
869                                                  "mobile" =>  @ldap_get_values($ds, $entry, "mobile"),
870                                                  "telefone" =>  @ldap_get_values($ds, $entry, "telephonenumber"),
871                                                  "ou" =>  @ldap_get_values($ds, $entry, "ou"),
872                                                  "employeeNumber" =>  @ldap_get_values($ds, $entry, "employeeNumber")
873                                        );
874
875                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] = @ldap_get_values_len($ds, $entry, "jpegphoto");
876                                ldap_close($ds);
877                                return $obj;
878                        }
879                }
880                return null;
881        }
882       
883        function uid2uidnumber($uid)
884        {
885                // do not follow the referral
886                $this->ldapRootConnect(false);
887                if ($this->ds)
888                {
889                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
890                        $justthese = array("uidnumber");
891                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
892                        if(!$sr)
893                                return false;
894                        $info = ldap_get_entries($this->ds, $sr);
895                        return $info[0]["uidnumber"][0];
896                }
897                return false;
898        }
899}
900?>
Note: See TracBrowser for help on using the repository browser.