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

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

Ticket #559 - Atualização de segurança

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