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

Revision 1363, 27.0 KB checked in by niltonneto, 15 years ago (diff)

Ticket #565 - Corrigido problema que exibia organizações ocultas.

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