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

Revision 5075, 36.1 KB checked in by airton, 13 years ago (diff)

Ticket #2267 - Exibir mais detalhes dos contatos retornados na busca rapida de destinatarios

  • 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       
288         /**
289        * Método que faz o roteamento entre os métodos de busca (Catálogo pessoal, global e todos)
290        * @license    http://www.gnu.org/copyleft/gpl.html GPL
291        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
292        * @sponsor    Caixa Econômica Federal
293        * @author     Prognus Software Livre <airton@prognus.com.br | prognus@prognus.com.br>
294        * @param      <array> <$param> <parametros vindos do cliente>
295        */
296        function quicksearchcontact($params)
297        {
298            $modal = false;
299            if($params['catalog'])
300                $modal = true;
301             
302            include_once $_SESSION['rootPath']. '/header.inc.php';
303           
304                     
305            if (stripos( $params['search_for'] , '@' ) !== false)
306                $params['search_for'] = substr($params['search_for'], 0 , stripos( $params['search_for'] , '@' ) );
307           
308           
309            if($modal)
310            {
311                if($params['catalog'] == "global")
312                {       
313                    return $this->quickSearchGlobal($params);
314                }
315                else
316                {
317                    if($params['catalog'] == "personal")
318                        return $this->quickSearchPersonal($params);
319                    else
320                        return $this->quickSearchAll($params);
321                }
322            }
323            else
324            {
325                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['catalog_search'] == "global")
326                {
327                    return $this->quickSearchGlobal($params);
328                }
329                else
330                {   
331                    if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['catalog_search'] == "personal")
332                        return $this->quickSearchPersonal($params);                   
333                    else
334                                                //Veirifica se quem chamou foi o campo de pesquisa rápida Expresso_Mail
335                                                if(($params['field'] == 'null') && ($params['ID'] == 'null'))
336                                                        return $this->quickSearch($params);
337                                                else
338                                                        return $this->quickSearchAll($params);                                                 
339                }
340            }
341        }
342       
343       
344
345        /**
346        * Método que faz a busca de usuários em todos os catálogos
347        * @license    http://www.gnu.org/copyleft/gpl.html GPL
348        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
349        * @sponsor    Caixa Econômica Federal
350        * @author     Prognus Software Livre <airton@prognus.com.br | prognus@prognus.com.br>
351        * @param      <array> <$param> <parametros vindos do cliente>
352        * @return     <array> <$retorno> <Array com os usuários de todos os catálogos, de acordo com o parâmetro>
353        */
354        function quickSearchAll($params)
355        {
356               
357            $retorno_personal = $this->quickSearchPersonal($params);
358            $retorno_global   = $this->quickSearchGlobal($params);
359            //$retorno = $retorno_personal + $retorno_global;
360            $retorno = array_merge($retorno_personal, $retorno_global);
361            $retorno['search_for'] = $params['search_for'];
362                        $retorno['type_catalog'] = "A";
363            return $retorno;
364        }
365       
366       
367        /**
368        * Método que faz a busca de usuários no Catálogo Pessoal
369        * @license    http://www.gnu.org/copyleft/gpl.html GPL
370        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
371        * @sponsor    Caixa Econômica Federal
372        * @author     Prognus Software Livre <airton@prognus.com.br | prognus@prognus.com.br>
373        * @param      <array> <$param> <parametros vindos do cliente>
374        * @return     <array> <$retorno> <Array com os usuários do Catálogo Pessoal, de acordo com o parâmetro>
375        */
376        function quickSearchPersonal($params, $all=false)
377        {
378            $results = array();
379            $DBService = ServiceLocator::getService('db');
380            $results   = $DBService->search_contacts($params['search_for']);
381           
382            if(!$all)
383                $results['type_catalog'] = "P";
384            $results['search_for'] = $params['search_for'];
385                        return $results;
386        }
387       
388       
389        /**
390        * Método que faz a busca de usuários no Catálogo Geral
391        * @license    http://www.gnu.org/copyleft/gpl.html GPL
392        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
393        * @sponsor    Caixa Econômica Federal
394        * @author     Prognus Software Livre <airton@prognus.com.br | prognus@prognus.com.br>
395        * @param      <array> <$param> <parametros vindos do cliente>
396        * @return     <array> <$retorno> <Array com os usuários do Catálogo Global, de acordo com o parâmetro>
397        */
398        function quickSearchGlobal($params, $all=false)
399        {             
400            include_once $_SESSION['rootPath']. '/header.inc.php';
401            $c = CreateObject('phpgwapi.config','contactcenter');
402            $all_data = $c->read_repository();
403           
404            $searchable_fields = array();           
405            foreach($all_data as $index => $value)
406            {
407                $parts = explode('_', $index);
408                if (is_numeric($parts[3]) && $parts[1]=='attribute')
409                {   
410                    if($parts[2] == 'searchable' && $value == 'true')
411                        $searchable_fields[$all_data['cc_attribute_ldapname_' . $parts[3]]] = $all_data['cc_attribute_name_' . $parts[3]];   
412                }
413            }   
414            include_once("class.functions.inc.php");
415            $functions      = new functions;
416            $field          = $params['field'];
417            $ID             = $params['ID'];
418            $ldapService    = ServiceLocator::getService('ldap');
419            $filter         = $ldapService->getSearchFilter($params['search_for']);
420 
421            $contacts_result          = array();
422            $contacts_result['field'] = $field;
423            $contacts_result['ID']    = $ID;
424            $search_for               = utf8_encode($params['search_for']);
425           
426            $this->ldapConnect(true);
427            if ($this->ds)
428            {
429                $ldapService->connection = $this->ds;
430                $campos_extras = '';
431                $justthese = array("cn", "mail", "telephonenumber", "mobile", "phpgwaccountvisible", "uid", "employeenumber", "ou");
432                foreach($searchable_fields as $fields_ldap => $value_field)
433                {   
434                    $campos_extras .= $fields_ldap . "|" . $value_field . "#";
435                    array_push($justthese, $fields_ldap);   
436                }
437                $types = false;
438
439                $campos_extras = substr($campos_extras,0,-1);
440               
441                if($field == 'null' || $ID == 'null')
442                {
443                    $justthese[] = "jpegphoto";
444                    $types = 'u';
445                } 
446                $filter = $ldapService->getSearchFilter($params['search_for'], $types);
447                // Retirei o this->max_results, que limitava a busca. Agora ta retornando tudo amigo.
448                $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0);
449                 
450                if(!$sr)
451                    return null;
452               
453                $count_entries = ldap_count_entries($this->ds,$sr);
454                $info          = ldap_get_entries($this->ds, $sr);
455                $info_return   = $info;
456                //if($campos_extras != '')
457                //array_push($info_return, $campos_extras);
458                        }
459                        ldap_close($this->ds);     
460               
461                        //Busca em Catalagos externos
462                        $catalogsNum=count($this->external_srcs);
463                        for ($i=0; $i<=$catalogsNum; $i++)      {
464                                if ($this->external_srcs[$i]["quicksearch"])
465                                {
466                                        $this->ldapConnect(true,$i);
467                                        $filter="(|(cn=*$search_for*)(mail=*$search_for*))";
468                                        $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid","employeeNumber", "ou");
469                                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result+1);
470                                        if(!$sr)
471                                                return null;
472                                        $count_entries = ldap_count_entries($this->ds,$sr);
473                                        $search = ldap_get_entries($this->ds, $sr);
474                                        for ($j=0; $j<$search["count"]; $j++) {
475                                                $info_return[] = $search[$j];
476                                        }
477                                        $info_return["count"] = count($info_return)-1;
478                                }
479                        }
480                        //---------------------------------------------------------------//
481                       
482            if($all == false)
483                $info_return['type_catalog'] = "G";
484           
485            $info_return['search_for'] = $params['search_for'];
486                        $info_return['extra_ldap_fields'] = $campos_extras;
487            return $info_return;
488        }
489       
490       
491       
492       
493       
494       
495       
496        function make_quicksearch_card($info, $cn)
497        {
498                include_once("class.functions.inc.php");
499                $functions = new functions;
500
501                $contacts_result = array();
502                $contacts_result["cn"] = $cn;
503                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
504                    $extendedinfo=true;
505                else
506                    $extendedinfo=false;
507
508                list ($contacts_result["mail"], $contacts_result["phone"], $contacts_result["mobile"], $contacts_result["uid"], $contacts_result["jpegphoto"], $contacts_result["employeenumber"], $contacts_result["ou"]) = split ('%', $info);
509
510                if ($contacts_result['jpegphoto'])
511                        $photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result['mail'].'">';
512                else
513                        $photo_link = '<img src="./templates/default/images/photo.jpg">';
514
515                $phoneUser = $contacts_result['phone'];
516
517                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
518                        $phoneUser = '<a title="'.$this->functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
519                        if($contacts_result['mobile']){
520                                $phoneUser .= ' / <a title="'.$this->functions->getLang("Call to Mobile Number").'" href="#" onclick="InfoContact.connectVoip(\''.$contacts_result['mobile'].'\',\'mob\')">'.$contacts_result['mobile'].'</a>';
521                        }
522                }
523                //verifica de existe o celular do funcionario ($contacts_result["mobile"]), se nao, $phoneUser fica vazia;
524
525                    $mobileUser = $contacts_result["mobile"];
526                    if($mobileUser && !$voipMobile) {
527                            $phoneUser .= " / $mobileUser";
528                    }
529                    $empNumber = $contacts_result["employeenumber"];
530                    if($empNumber) {
531                            $empNumber = "$empNumber - ";
532                    }
533                    $ou = $contacts_result["ou"];
534                    if($ou) {
535                            $ou = "<br/>$ou" ;
536                    }
537
538                // Begin: nickname, firstname and lastname for QuickAdd.
539                $fn = $contacts_result["cn"];
540                $array_name = explode(" ", $fn);
541                if(count($array_name) > 1){                     
542                        $fn = $array_name[0];
543                        array_shift($array_name);
544                        $sn = implode(" ",$array_name);
545                }
546                // End:
547                $option =
548                        '<tr class="quicksearchcontacts_unselected">' .
549                                '<td class="cc" width="1%">' .
550                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
551                                                $photo_link .
552                                        '</a>' .
553                                '</td>' .
554                                '<td class="cc">' .
555                                        '<span name="cn">' . $empNumber . $contacts_result['cn'] . '</span>' . '<br>' .
556                                        '<a title="'.$functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
557                                                '<font color=blue>' .
558                                                '<span name="mail">' . $contacts_result['mail'] . '</span></a></font>'.
559                                                '<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"].'\')">'.
560                                        '<br>' .
561                                        $phoneUser .
562                                        $ou .
563                                '</td>' .
564                                '</tr>';
565                return $option;
566        }
567
568        function get_catalogs(){
569                $catalogs = array();
570                $catalogs[0] = $this->functions->getLang("Global Catalog");
571                if($this->external_srcs)
572                        foreach ($this->external_srcs as $key => $valor ){
573                        $catalogs[$key] = $valor['name'];
574                }
575                return $catalogs;
576        }
577        function get_organizations($params){
578                $organizations = array();
579                $params['referral']?$referral = $params['referral']:$referral = false;
580                $cat = $params['catalog'];
581
582                $this->ldapConnect($referral,$cat);
583
584                        if($this->branch != '') {
585                                $filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
586                                $justthese = array("$this->branch");
587                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
588                        $info = ldap_get_entries($this->ds, $sr);
589
590                        if($info["count"] == 0)
591                        {
592                            $organizations[0]['ou'] = $this->ldap_context;
593                        }
594
595                        for ($i=0; $i<$info["count"]; $i++)
596                                $organizations[$i] = $info[$i]["ou"][0];
597
598                        ldap_close($this->ds);
599                        sort($organizations);
600                return $organizations;
601                        }else{
602                        return null;
603        }
604        }
605        function get_organizations2($params){
606                $organizations = array();
607                $referral = $params['referral'];
608                $this->ldapRootConnect($referral);
609                if ($this->ds) {
610                        $filter="(&(objectClass=organizationalUnit)(!(phpgwAccountVisible=-1)))";
611                        $justthese = array("ou");
612                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
613                        $info = ldap_get_entries($this->ds, $sr);
614
615
616                        if($info["count"] == 0)
617                        {
618                            $organizations[0]['ou'] = $this->ldap_context;
619                            $organizations[0]['dn'] = $this->ldap_context;
620                        }
621                        else{
622                            for ($i=0; $i<$info["count"]; $i++)
623                            {
624                                    $organizations[$i]['ou'] = $info[$i]["ou"][0];
625                                    $organizations[$i]['dn'] = $info[$i]["dn"];
626                            }
627                        }
628                        ldap_close($this->ds);
629                        sort($organizations);
630                }
631                return $organizations;
632        }
633        //Busca usuarios de um contexto e ja retorna as options do select - usado por template serpro;
634        function search_users($params)
635        {
636                $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
637                        $ldapService = ServiceLocator::getService('ldap');
638                        $ldapService->connect($_SESSION['phpgw_info']['expressomail']['server']['ldap_host'],
639                        $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],
640                        $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);
641
642                        $groups = $ldapService->accountSearch($params['filter'], array("gidNumber","cn"), $params['context'] , 'g', 'cn');
643                        $users = $ldapService->accountSearch($params['filter'], array("uidNumber","cn"), $params['context'] , 'u', 'cn');
644                        $group_options = array();
645                        $user_options  = array();
646                        foreach($groups as $group)
647                {
648                                $group_options[] = '"'.$group['gidNumber'].'U'.'":"'.$group['cn'].'"';
649                }
650
651                foreach($users as $user)
652                    {
653                                if($owner != $user['uidNumber'])
654                                        $user_options[] = '"'.$user['uidNumber'].'U'.'":"'.$user['cn'].'"';
655                    }
656       
657                        $user_options = '{'.implode( ',', $user_options ).'}';
658                        $group_options = '{'.implode( ',', $group_options ).'}';
659
660                return array("users" => $user_options, "groups" => $group_options);
661        }
662
663        function catalogsearch($params)
664        {
665                $ldapService = ServiceLocator::getService('ldap');
666                $filter =  $ldapService->getSearchFilter($params['search_for'],array('u','l','s'));
667               
668                $catalog = $params['catalog'];
669                $error = False;
670
671                //if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
672                    //$extendedinfo=true;
673                //else
674                    //$extendedinfo=false;
675
676
677                $this->ldapConnect(true,$catalog);
678
679                $params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;
680
681                if ($this->ds) {
682                                $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible", "employeeNumber", "ou");
683                        $sr=@ldap_search($this->ds, $user_context, $filter, $justthese, 0, $ldapService->limit+1);
684
685                        if(!$sr)
686                                return null;
687                        $count_entries = ldap_count_entries($this->ds,$sr);
688                        if ($count_entries > $ldapService->limit){
689                                $info = null;
690                                $error = True;
691                        }
692                        else
693                                $info = ldap_get_entries($this->ds, $sr);
694
695                        ldap_close($this->ds);
696
697                        $u_tmp = array();
698                        $g_tmp = array();
699
700                        for ($i=0; $i<$info["count"]; $i++){
701                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
702                                        //aqui eh feita a concatenacao do departamento ao cn;
703                                        $u_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]). '%' . $info[$i]["ou"][0];
704                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'L') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
705                                        $g_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]);
706                        }
707
708                        natcasesort($u_tmp);
709                        natcasesort($g_tmp);
710
711                        $i = 0;
712                        $users = array();
713
714                        foreach ($u_tmp as $mail => $cn){
715
716                                $tmp = explode("%", $cn); //explode o cn pelo caracter "%" e joga em $tmp;
717                                $name = $tmp[0]; //pega o primeiro item (cn) do vetor resultante do explode acima;
718                                $department = $tmp[1]; //pega o segundo item (ou) do vetor resultanto do explode acima;
719                                $users[$i++] = array("name" => $name, "email" => $mail, "department" => $department);
720
721                        }
722                        unset($u_tmp);
723
724                        $i = 0;
725                        $groups = array();
726
727                        foreach ($g_tmp as $mail => $cn){
728                                $groups[$i++] = array("name" => $cn, "email" => $mail);
729                        }
730                        unset($g_tmp);
731
732                        return  array('users' => $users, 'groups' => $groups, 'error' => $error,'maxResult' => $ldapService->limit);
733                }else
734                return null;
735        }
736
737        function get_emails_ldap(){
738
739                $result['mail']= array();
740                $result['mailalter']= array();
741                $user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
742                $this->ldapRootConnect(false);
743                if ($this->ds) {
744                        $filter="uid=".$user;
745                        $justthese = array("mail","mailAlternateAddress");
746                        $sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
747                        $ent = ldap_get_entries($this->ds, $sr);
748                        ldap_close($this->ds);
749
750                        for ($i=0; $i<$ent["count"]; $i++){
751                                $result['mail'][] = $ent[$i]["mail"][0];
752                                $result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];
753                        }
754                }
755                return $result;
756        }
757
758        //Busca usuarios de um contexto e ja retorna as options do select;
759        function get_available_users($params)
760    {
761        $this->ldapRootConnect();
762        //Monta lista de Grupos e Usuarios
763        $users = Array();
764        $groups = Array();
765        $user_context= $params['context'];
766        $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
767
768        if ($this->ds)
769        {
770            $justthese = array("gidNumber","cn");
771            if ($params['type'] == 'search')
772                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
773            else
774                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
775            $info = ldap_get_entries($this->ds, $sr);
776            for ($i=0; $i<$info["count"]; $i++)
777                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
778            $justthese = array("phpgwaccountvisible","uidNumber","cn");
779            if ($params['type'] == 'search')
780                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
781            else
782                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
783
784            $info = ldap_get_entries($this->ds, $sr);
785            for ($i=0; $i<$info["count"]; $i++)
786            {
787                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
788                    continue;
789                $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
790            }
791        }
792        ldap_close($this->ds);
793
794        @asort($users);
795        @reset($users);
796        @asort($groups);
797        @reset($groups);
798        $user_options ='';
799        $group_options ='';
800
801        foreach($groups as $id => $user_array) {
802                $newId = $id.'U';
803                $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
804        }
805        foreach($users as $id => $user_array) {
806            if($owner != $id){
807                $newId = $id.'U';
808                $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
809            }
810        }
811        return array("users" => $user_options, "groups" => $group_options);
812    }
813
814        //Busca usuarios de um contexto e ja retorna as options do select;
815        function get_available_users2($params)
816        {
817                $ldapService = ServiceLocator::getService('ldap');
818                $ldapService->connect($_SESSION['phpgw_info']['expressomail']['server']['ldap_host'],
819                $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],
820                $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);
821
822                $entries = $ldapService->accountSearch($params['sentence'], array('cn', 'uid'), $params['context'], 'u', 'cn');
823
824                $options = array();
825
826                foreach ($entries as $value)
827                        $options[] = '"'.$value['uid'].'"'.':'.'"'.$value['cn'].'"';
828
829                return "{".implode(',',$options)."}";           
830                                }
831
832        function uid2cn($uid)
833        {
834                // do not follow the referral
835                $this->ldapRootConnect(false);
836                if ($this->ds)
837                {
838                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
839                        $justthese = array("cn");
840                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
841                        if(!$sr)
842                                return false;
843                        $info = ldap_get_entries($this->ds, $sr);
844                        return utf8_decode($info[0]["cn"][0]);
845                }
846                return false;
847        }
848        function uidnumber2uid($uidnumber)
849        {
850                // do not follow the referral
851                $this->ldapRootConnect(false);
852                if ($this->ds)
853                {
854                        $filter="(&(phpgwAccountType=u)(uidnumber=$uidnumber))";
855                        $justthese = array("uid");
856                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
857                        if(!$sr)
858                                return false;
859                        $info = ldap_get_entries($this->ds, $sr);
860                        return $info[0]["uid"][0];
861                }
862                return false;
863        }
864        function getSharedUsersFrom($params){
865                $filter = '';
866                $i = 0;         
867                //Added to save if must save sent messages in shared folder
868                $acl_save_sent_in_shared = array();
869               
870                if($params['uids']) {
871                        $uids = explode(";",$params['uids']);
872                        $this->imap = new imap_functions();                     
873                        foreach($uids as $index => $uid){
874                                $params = array();
875                                //Added to save if user has create permission
876                                $acl_create_message = array();
877                                $acl = $this->imap->getacltouser($uid);
878                                if ( preg_match("/a/",$acl )){                         
879                                        $filter .= "(uid=$uid)";                                       
880                                        if ( preg_match("/p/",$acl )){                         
881                                                $acl_save_sent_in_shared[ $i ] =$uid;
882                                                $i++;
883                                        }                                       
884                                }                                                       
885                        }                       
886                }
887               
888                $this->ldapRootConnect(false);
889                if ($this->ds) {
890                        $justthese = array("cn","mail","uid");
891                        if($filter) {
892                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=s))(|$filter))";
893                                $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
894                                ldap_sort($this->ds,$sr,"cn");
895                                $info   =       ldap_get_entries($this->ds, $sr);
896                                $var = print_r($acl_save_sent_in_shared, true);                         
897                                for ($i = 0;$i < $info["count"]; $i++){
898                                        $info[$i]['cn'][0] = utf8_decode($info[$i]['cn'][0]);
899                                        //verify if user has permission to save sent messages in a shared folder
900                                        if ( in_array( $info[$i]['uid'][0],$acl_save_sent_in_shared) ){                                         
901                                                $info[$i]['save_shared'][0] = 'y';
902                                        } else $info[$i]['save_shared'][0] = 'n';
903                                }
904                        }
905               
906                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
907
908                        //Find institucional_account.
909                        $filter="(&(phpgwAccountType=i)(mailForwardingAddress=".$_SESSION['phpgw_info']['expressomail']['user']['email']."))";
910                        $sr     = ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
911                        ##
912                        # @AUTHOR Rodrigo Souza dos Santos
913                        # @DATE 2008/09/17
914                        # @BRIEF Changing to ensure that the variable session is always with due value.
915                        ##
916                        if(ldap_count_entries($this->ds,$sr))
917                        {
918                                ldap_sort($this->ds,$sr,"cn");
919                                $result = ldap_get_entries($this->ds, $sr);
920                                for ($j = 0;$j < $result["count"]; $j++){
921                                        $info[$i]['cn'][0] = utf8_decode($result[$j]['cn'][0]);
922                                        $info[$i]['mail'][0] = $result[$j]['mail'][0];
923                                        $info[$i]['save_shared'][0] = 'n';
924                                        $info[$i++]['uid'][0] = $result[$j]['uid'][0];                                 
925                                }
926                        }
927
928                        $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] = $info;
929                       
930                        return $info;
931                }
932        }
933
934        function getUserByEmail($params)
935        {
936                $expires = 60*60*24*30; /* 30 days */
937                header("Cache-Control: maxage=".$expires);
938                header("Pragma: public");
939                header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires)); 
940                $filter="(&(phpgwAccountType=u)(mail=" . $params['email'] . "))";
941                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
942                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
943                    $extendedinfo=true;
944                else
945                    $extendedinfo=false;
946
947                if($extendedinfo)
948                    $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile","ou","employeeNumber");
949                else
950                    $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile");
951
952                // Follow the referral
953                $ds = $this->ldapConnect2(true);
954                if ($ds)
955                {
956                        $sr=@ldap_search($ds, $ldap_context, $filter, $justthese);
957
958                        if (!$sr)
959                                return null;
960
961                        $entry = ldap_first_entry($ds, $sr);
962
963                        if($entry) {
964                                $obj =  array("cn" => utf8_decode(current(ldap_get_values($ds, $entry, "cn"))),
965                                                  "email" => $params['email'],
966                                                  "uid" => ldap_get_values($ds, $entry, "uid"),
967                                                  "type" => "global",
968                                                  "mobile" =>  @ldap_get_values($ds, $entry, "mobile"),
969                                                  "telefone" =>  @ldap_get_values($ds, $entry, "telephonenumber"),
970                                                  "ou" =>  @ldap_get_values($ds, $entry, "ou"),
971                                                  "employeeNumber" =>  @ldap_get_values($ds, $entry, "employeeNumber")
972                                        );
973
974                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] = @ldap_get_values_len($ds, $entry, "jpegphoto");
975                                ldap_close($ds);
976                                return $obj;
977                        }
978                }
979                return null;
980        }
981       
982        function uid2uidnumber($uid)
983        {
984                // do not follow the referral
985                $this->ldapRootConnect(false);
986                if ($this->ds)
987                {
988                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
989                        $justthese = array("uidnumber");
990                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
991                        if(!$sr)
992                                return false;
993                        $info = ldap_get_entries($this->ds, $sr);
994                        return $info[0]["uidnumber"][0];
995                }
996                return false;
997        }
998}
999?>
Note: See TracBrowser for help on using the repository browser.