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

Revision 1040, 27.1 KB checked in by amuller, 15 years ago (diff)

Ticket #559 - Atualização de download de arquivos e sessão

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