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

Revision 2802, 28.8 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1058 - Permitindo o envio de formulário por requisição background

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3/***************************************************************************
4        * Expresso Livre                                                           *
5        * http://www.expressolivre.org                                             *
6        * --------------------------------------------                             *
7        *  This program is free software; you can redistribute it and/or modify it *
8        *  under the terms of the GNU General Public License as published by the   *
9        *  Free Software Foundation; either version 2 of the License, or (at your  *
10        *  option) any later version.                                              *
11        \**************************************************************************/
12include_once("class.imap_functions.inc.php");
13include_once("class.functions.inc.php");
14
15function ldapRebind($ldap_connection, $ldap_url)
16{
17        @ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);
18}
19
20class ldap_functions
21{
22        var $ds;
23        var $ldap_host;
24        var $ldap_context;
25        var $imap;
26        var $external_srcs;
27        var $max_result;
28        var $functions;
29        function ldap_functions(){
30        // todo: Page Configuration for External Catalogs.
31                @include("../contactcenter/setup/external_catalogs.inc.php");
32                $this->external_srcs= ( isset( $external_srcs ) ) ? $external_srcs : NULL;
33                $this->max_result       = 200;
34                $this->functions = new functions();
35        }
36        // Using ContactCenter configuration.
37        function ldapConnect($refer = false,$catalog = 0){
38                if ($catalog > 0 && is_array($this->external_srcs)){
39                        $this->ldap_host        = $this->external_srcs[$catalog]['host'];
40                        $this->ldap_context = $this->external_srcs[$catalog]['dn'];
41                        $this->bind_dn          = $this->external_srcs[$catalog]['acc'];
42                        $this->bind_dn_pw       = $this->external_srcs[$catalog]['pw'];
43                        $this->object_class = $this->external_srcs[$catalog]['obj'];
44                        $this->base_dn          = $this->external_srcs[$catalog]['dn'];
45                        $this->branch           = $this->external_srcs[$catalog]['branch'];
46                }else {
47                        $this->ldap_host        = $_SESSION['phpgw_info']['expressomail']['ldap_server']['host'];
48                        $this->ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
49                        $this->bind_dn = $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'];
50                        $this->bind_dn_pw = $_SESSION['phpgw_info']['expressomail']['ldap_server']['pw'];
51                        $this->branch = 'ou';
52                }
53
54                $this->ds = ldap_connect($this->ldap_host);
55                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
56                ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
57                if ($refer)     {
58                        ldap_set_rebind_proc($this->ds, ldapRebind);
59                }
60                @ldap_bind($this->ds,$this->bind_dn,$this->bind_dn_pw );
61        }
62
63        //Teste jakjr retornando o DS
64        function ldapConnect2($refer = false){
65                $ds = ldap_connect($_SESSION['phpgw_info']['expressomail']['ldap_server']['host']);
66
67                if (!$ds)
68                        return false;
69
70                ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
71                ldap_set_option($ds, LDAP_OPT_REFERRALS, $refer);
72                if ($refer)
73                        ldap_set_rebind_proc($ds, ldapRebind);
74                @ldap_bind($ds, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);
75
76                return $ds;
77        }
78
79
80        // usa o host e context do setup.
81        function ldapRootConnect($refer = false){
82                $this->ldap_host        = $_SESSION['phpgw_info']['expressomail']['server']['ldap_host'];
83                $this->ldap_context = $_SESSION['phpgw_info']['expressomail']['server']['ldap_context'];
84                $this->ds                       = ldap_connect($this->ldap_host);
85                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
86                ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
87                ldap_bind($this->ds, $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],$_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);
88
89        }
90
91        function quicksearch($params)
92        {
93                include_once("class.functions.inc.php");
94                $functions = new functions;
95
96                $search_for     = utf8_encode($params['search_for']);
97                $field          = $params['field'];
98                $ID                     = $params['ID'];
99               
100                $search_for     = explode(" ",$search_for);
101                $aux="";
102                foreach ($search_for as $search)
103                {
104                        if(!$aux)
105                        {
106                                $aux=$search;
107                        }
108                        else
109                        {
110                                if (strlen($search) > 2)
111                                {
112                                        $aux=$aux."*".$search;
113                                }
114                                else
115                                {
116                                        $aux=$aux." ".$search;
117                                }
118                        }
119                }
120                $search_for=$aux;
121               
122                $contacts_result = array();
123                $contacts_result['field'] = $field;
124                $contacts_result['ID'] = $ID;
125
126                // follow the referral
127                $this->ldapConnect(true);
128
129                if ($this->ds)
130                {
131                        if (($field != 'null') && ($ID != 'null'))
132                        {
133                                $filter="(& (&(|(phpgwAccountType=u)(phpgwAccountType=g)(phpgwAccountType=l)(phpgwAccountType=i)(phpgwAccountType=s))(mail=*)) (|(cn=*$search_for*)(mail=*$search_for*)) (!(phpgwaccountvisible=-1)) )";
134                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid", "employeeNumber", "ou");
135                        }
136                        else
137                        {
138                                $filter="(& (phpgwAccountType=u)(cn=*$search_for*) (!(phpgwaccountvisible=-1)) )";
139                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible","jpegPhoto", "uid", "employeeNumber", "ou");
140                        }
141                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result + 1);
142                        if(!$sr)
143                                return null;
144                        $count_entries = ldap_count_entries($this->ds,$sr);
145
146                        // Get user org dn.
147                        $user_dn = $_SESSION['phpgw_info']['expressomail']['user']['account_dn'];
148                        $user_sector_dn = ldap_explode_dn ( $user_dn, false );
149                        array_shift($user_sector_dn);
150                        array_shift($user_sector_dn);
151                        $user_sector_dn = implode(",", $user_sector_dn);
152
153                        // New search only on user sector
154                        if ($count_entries > $this->max_result)
155                        {
156                                // Close old ldap conection
157                                ldap_close($this->ds);
158
159                                // Reopen a local ldap connection, following referral
160                                $this->ldapRootConnect(true);
161
162                                $sr= ldap_search($this->ds, $user_sector_dn, $filter, $justthese);
163                                if(!$sr)
164                                        return null;
165                                $count_entries = ldap_count_entries($this->ds,$sr);
166
167                                if ($count_entries > $this->max_result){
168                                        $return = array();
169                                        $return['status'] = false;
170                                        $return['error'] = "many results";
171                                        return $return;
172                                }
173                                else
174                                {
175                                        $quickSearch_only_in_userSector = true;
176                                }
177                        }
178
179                        $info = ldap_get_entries($this->ds, $sr);
180
181                        $tmp = array();
182                        $tmp_users_from_user_org = array();
183
184                        if (!$quickSearch_only_in_userSector) {
185                                $catalogsNum=count($this->external_srcs);
186                                for ($i=0; $i<=count($this->external_srcs); $i++)       {
187                                        if ($this->external_srcs[$i]["quicksearch"]) {
188                                                $this->ldapConnect(true,$i);
189                                                $filter="(|(cn=*$search_for*)(mail=*$search_for*))";
190                                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid","employeeNumber", "ou");
191                                                $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result+1);
192                                                if(!$sr)
193                                                        return null;
194                                                $count_entries = ldap_count_entries($this->ds,$sr);
195                                                $search = ldap_get_entries($this->ds, $sr);
196                                                for ($j=0; $j<$search["count"]; $j++) {
197                                                        $info[] = $search[$j];
198                                                }
199                                                $info["count"] = count($info)-1;
200                                        }
201                                }
202                        }
203
204                        for ($i=0; $i<$info["count"]; $i++)
205                        {
206                                if ($quickSearch_only_in_userSector)
207                                {
208                                        $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]);
209                                }
210                                else
211                                {
212                                        if (preg_match("/$user_sector_dn/i", $info[$i]['dn']))
213                                        {
214                                                $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]);
215                                        }
216                                        else
217                                        {
218                                                $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]);
219                                        }
220                                }
221                        }
222                        natcasesort($tmp_users_from_user_org);
223                        natcasesort($tmp);
224
225                        if (($field != 'null') && ($ID != 'null'))
226                        {
227                                $i = 0;
228
229                                $tmp = array_merge($tmp, $tmp_users_from_user_org);
230                                natcasesort($tmp);
231
232                                foreach ($tmp as $info => $cn)
233                                {
234                                        $contacts_result[$i] = array();
235                                        $contacts_result[$i]["cn"] = $cn;
236                                        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);
237                                        $i++;
238                                }
239                                $contacts_result['quickSearch_only_in_userSector'] = $quickSearch_only_in_userSector;
240                        }
241                        else
242                        {
243                                $options_users_from_user_org = '';
244                                $options = '';
245
246                                /* List of users from user org */
247                                $i = 0;
248                                foreach ($tmp_users_from_user_org as $info => $cn)
249                                {
250                                        $contacts_result[$i] = array();
251                                        $options_users_from_user_org .= $this->make_quicksearch_card($info, $cn);
252                                        $i++;
253                                }
254
255                                /* List of users from others org */
256                                foreach ($tmp as $info => $cn)
257                                {
258                                        $contacts_result[$i] = array();
259                                        $options .= $this->make_quicksearch_card($info, $cn);
260                                        $i++;
261                                }
262
263                                if ($quickSearch_only_in_userSector)
264                                {
265                                        if ($options != '')
266                                        {
267                                                $head_option =
268                                                        '<tr class="quicksearchcontacts_unselected">' .
269                                                                '<td colspan="2" width="100%" align="center">' .
270                                                                        str_replace("%1", $this->max_result,$this->functions->getLang('More than %1 results were found')) . '.<br>' .
271                                                                        $this->functions->getLang('Showing only the results found in your organization') . '.';
272                                                                '</td>' .
273                                                        '</tr>';
274                                                $contacts_result = $head_option . $options_users_from_user_org . $options;
275                                        }
276                                        else
277                                        {
278                                                $return = array();
279                                                $return['status'] = false;
280                                                $return['error'] = "many results";
281                                                return $return;
282                                        }
283                                }
284                                else
285                                {
286                                        if (($options_users_from_user_org != '') && ($options != ''))
287                                        {
288                                                $head_option0 =
289                                                        '<tr class="quicksearchcontacts_unselected">' .
290                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
291                                                                        $this->functions->getLang('Users from your organization') . '</B> ['.count($tmp_users_from_user_org).']';
292                                                                '</td>' .
293                                                        '</tr>';
294
295                                                $head_option1 =
296                                                        '<tr class="quicksearchcontacts_unselected">' .
297                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
298                                                                        $this->functions->getLang('Users from others organizations') . '</B> ['.count($tmp).']';
299                                                                '</td>' .
300                                                        '</tr>';
301                                        }
302                                        $contacts_result = $head_option0 . $options_users_from_user_org . $head_option1 . $options;
303                                }
304                        }
305                }
306                ldap_close($this->ds);
307                return $contacts_result;
308        }
309
310        function make_quicksearch_card($info, $cn)
311        {
312                include_once("class.functions.inc.php");
313                $functions = new functions;
314
315                $contacts_result = array();
316                $contacts_result["cn"] = $cn;
317                list ($contacts_result["mail"], $contacts_result["phone"], $contacts_result["mobile"], $contacts_result["uid"], $contacts_result["jpegphoto"]) = split ('%', $info);
318
319                if ($contacts_result['jpegphoto'])
320                        $photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result['mail'].'">';
321                else
322                        $photo_link = '<img src="./templates/default/images/photo.jpg">';
323
324                $phoneUser = $contacts_result['phone'];
325
326                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
327                        $phoneUser = '<a title="'.$this->functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
328                        if($contacts_result['mobile']){
329                                $phoneUser .= ' / <a title="'.$this->functions->getLang("Call to Mobile Number").'" href="#" onclick="InfoContact.connectVoip(\''.$contacts_result['mobile'].'\',\'mob\')">'.$contacts_result['mobile'].'</a>';
330                        }
331                }
332                // Begin: nickname, firstname and lastname for QuickAdd.
333                $fn = $contacts_result["cn"];
334                $array_name = explode(" ", $fn);
335                if(count($array_name) > 1){                     
336                        $fn = $array_name[0];
337                        array_shift($array_name);
338                        $sn = implode(" ",$array_name);
339                }
340                // End:
341                $option =
342                        '<tr class="quicksearchcontacts_unselected">' .
343                                '<td class="cc" width="1%">' .
344                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
345                                                $photo_link .
346                                        '</a>' .
347                                '</td>' .
348                                '<td class="cc">' .
349                                        '<span name="cn">' . $contacts_result['cn'] . '</span>' . '<br>' .
350                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
351                                                '<font color=blue>' .
352                                                '<span name="mail">' . $contacts_result['mail'] . '</span></a></font>'.
353                                                '<img src="templates/default/images/user_card.png" style="cursor: pointer;" title="'.$this->functions->getLang("Add Contact").'" onclick="javascript:connector.loadScript(\'jscode/ccQuickAdd\');ccQuickAddOne.showList(\''.$fn.','.$fn.','.$sn.','.$contacts_result["mail"].'\')">'.
354                                        '<br>' .
355                                        $phoneUser .
356                                '</td>' .
357                                '</tr>';
358                return $option;
359        }
360
361        function get_catalogs(){
362                $catalogs = array();
363                $catalogs[0] = $this->functions->getLang("Global Catalog");
364                if($this->external_srcs)
365                        foreach ($this->external_srcs as $key => $valor ){
366                        $catalogs[$key] = $valor['name'];
367                }
368                return $catalogs;
369        }
370        function get_organizations($params){
371                $organizations = array();
372                $params['referral']?$referral = $params['referral']:$referral = false;
373                $cat = $params['catalog'];
374
375                $this->ldapConnect($referral,$cat);
376
377                        if($this->branch != '') {
378                                $filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
379                                $justthese = array("$this->branch");
380                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
381                        $info = ldap_get_entries($this->ds, $sr);
382
383                        for ($i=0; $i<$info["count"]; $i++)
384                                $organizations[$i] = $info[$i]["ou"][0];
385
386                        ldap_close($this->ds);
387                        sort($organizations);
388                return $organizations;
389                        }else{
390                        return null;
391        }
392        }
393        function get_organizations2($params){
394                $organizations = array();
395                $referral = $params['referral'];
396                $this->ldapRootConnect($referral);
397                if ($this->ds) {
398                        $filter="(&(ou=*)(!(phpgwAccountVisible=-1)))";
399                        $justthese = array("ou");
400                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
401                        $info = ldap_get_entries($this->ds, $sr);
402
403                        for ($i=0; $i<$info["count"]; $i++)
404                        {
405                                $organizations[$i]['ou'] = $info[$i]["ou"][0];
406                                $organizations[$i]['dn'] = $info[$i]["dn"];
407                        }
408
409                        ldap_close($this->ds);
410                        sort($organizations);
411                }
412                return $organizations;
413        }
414       
415        //Busca usuarios de um contexto e ja retorna as options do select - usado por template serpro;
416        function search_users($params)
417        {
418                $this->ldapConnect(false,0);
419                //Monta lista de Grupos e Usuarios
420                $users = Array();
421                $groups = Array();
422                $user_context= $this->ldap_context;
423                $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
424                $filtro =utf8_encode($params['filter']);
425                $context =utf8_encode($params['context']);//adicionado
426
427                if ($this->ds)
428                {
429                    $justthese = array("gidNumber","cn");
430                    if ($params['type'] == 'search')
431                        $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(cn=*$filtro*))"),$justthese);
432                    else
433                        $sr=ldap_list($this->ds,  $context ? $context : $user_context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(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, $context ? $context : $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 json_encode( 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 json_encode( 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                //Added to save if must save sent messages in shared folder
703                $acl_save_sent_in_shared = array();
704               
705                if($params['uids']) {
706                        $uids = explode(";",$params['uids']);
707                        $this->imap = new imap_functions();                     
708                        foreach($uids as $index => $uid){
709                                $params = array();
710                                //Added to save if user has create permission
711                                $acl_create_message = array();
712                                $acl = $this->imap->getacltouser($uid);
713                                if ( preg_match("/a/",$acl )){                         
714                                        $filter .= "(uid=$uid)";                                       
715                                        if ( preg_match("/p/",$acl )){                         
716                                                $acl_save_sent_in_shared[ $i ] =$uid;
717                                                $i++;
718                                        }                                       
719                                }                                                       
720                        }                       
721                }
722               
723                $this->ldapRootConnect(false);
724                if ($this->ds) {
725                        $justthese = array("cn","mail","uid");
726                        if($filter) {
727                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=s))(|$filter))";
728                                $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
729                                ldap_sort($this->ds,$sr,"cn");
730                                $info   =       ldap_get_entries($this->ds, $sr);
731                                $var = print_r($acl_save_sent_in_shared, true);                         
732                                for ($i = 0;$i < $info["count"]; $i++){
733                                        $info[$i]['cn'][0] = utf8_decode($info[$i]['cn'][0]);
734                                        //verify if user has permission to save sent messages in a shared folder
735                                        if ( in_array( $info[$i]['uid'][0],$acl_save_sent_in_shared) ){                                         
736                                                $info[$i]['save_shared'][0] = 'y';
737                                        } else $info[$i]['save_shared'][0] = 'n';
738                                }
739                        }
740               
741                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
742
743                        //Find institucional_account.
744                        $filter="(&(phpgwAccountType=i)(mailForwardingAddress=".$_SESSION['phpgw_info']['expressomail']['user']['email']."))";
745                        $sr     = ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
746                        ##
747                        # @AUTHOR Rodrigo Souza dos Santos
748                        # @DATE 2008/09/17
749                        # @BRIEF Changing to ensure that the variable session is always with due value.
750                        ##
751                        if(ldap_count_entries($this->ds,$sr))
752                        {
753                                ldap_sort($this->ds,$sr,"cn");
754                                $result = ldap_get_entries($this->ds, $sr);
755                                for ($j = 0;$j < $result["count"]; $j++){
756                                        $info[$i]['cn'][0] = utf8_decode($result[$j]['cn'][0]);
757                                        $info[$i]['mail'][0] = $result[$j]['mail'][0];
758                                        $info[$i]['save_shared'][0] = 'n';
759                                        $info[$i++]['uid'][0] = $result[$j]['uid'][0];                                 
760                                }
761                        }
762
763                        $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] = $info;
764                       
765                        return $info;
766                }
767        }
768
769        function getUserByEmail($params)
770        {
771                $filter="(&(phpgwAccountType=u)(mail=" . $params['email'] . "))";
772                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
773                $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile");
774
775                // Follow the referral
776                $ds = $this->ldapConnect2(true);
777                if ($ds)
778                {
779                        $sr=@ldap_search($ds, $ldap_context, $filter, $justthese);
780
781                        if (!$sr)
782                                return null;
783
784                        $entry = ldap_first_entry($ds, $sr);
785
786                        if($entry) {
787                                $obj =  array("cn" => utf8_decode(current(ldap_get_values($ds, $entry, "cn"))),
788                                                  "email" => $params['email'],
789                                                  "uid" => ldap_get_values($ds, $entry, "uid"),
790                                                  "type" => "global",
791                                                  "mobile" =>  @ldap_get_values($ds, $entry, "mobile"),
792                                                  "telefone" =>  @ldap_get_values($ds, $entry, "telephonenumber")
793                                        );
794
795                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] = @ldap_get_values_len($ds, $entry, "jpegphoto");
796                                ldap_close($ds);
797                                return $obj;
798                        }
799                }
800                return null;
801        }
802       
803        function uid2uidnumber($uid)
804        {
805                // do not follow the referral
806                $this->ldapRootConnect(false);
807                if ($this->ds)
808                {
809                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
810                        $justthese = array("uidnumber");
811                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
812                        if(!$sr)
813                                return false;
814                        $info = ldap_get_entries($this->ds, $sr);
815                        return $info[0]["uidnumber"][0];
816                }
817                return false;
818        }
819}
820?>
Note: See TracBrowser for help on using the repository browser.