source: branches/2.3/expressoMail1_2/inc/class.ldap_functions.inc.php @ 4906

Revision 4906, 31.2 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #2186 - Pesquisa rápida de contatos com aspas

  • 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");
[1740]22                $this->external_srcs= ( isset( $external_srcs ) ) ? $external_srcs : NULL;
[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
[4906]86//              $search_for     = utf8_encode($params['search_for']);
87  //Testa se a busca foi realizada com aspas
[325]88                $search_for     = utf8_encode($params['search_for']);
[4906]89                $search_len = (strlen($search_for)) - 1;
90                $quote_status=false;
91
92                if((($search_for{0}) == "\"") && (($search_for{$search_len}) == "\"")){
93                    $search_for{0} = " ";
94                    $search_for{$search_len} = " ";
95                    $quote_status=true;
96                }
97
98
99
[2]100                $field          = $params['field'];
101                $ID                     = $params['ID'];
[644]102               
[3388]103                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
104                    $extendedinfo=true;
105                else
106                    $extendedinfo=false;
107
[325]108                $search_for     = explode(" ",$search_for);
109                $aux="";
110                foreach ($search_for as $search)
[644]111                {
112                        if(!$aux)
[325]113                        {
114                                $aux=$search;
[644]115                        }
116                        else
117                        {
[657]118                                if (strlen($search) > 2)
119                                {
[644]120                                        $aux=$aux."*".$search;
[325]121                                }
[657]122                                else
123                                {
124                                        $aux=$aux." ".$search;
125                                }
126                        }
[644]127                }
[325]128                $search_for=$aux;
[644]129               
[2]130                $contacts_result = array();
131                $contacts_result['field'] = $field;
132                $contacts_result['ID'] = $ID;
133                // follow the referral
134                $this->ldapConnect(true);
[387]135
[2]136                if ($this->ds)
[342]137                {
138                        if (($field != 'null') && ($ID != 'null'))
[325]139                        {
[3388]140                                $filter="(& (&(|(phpgwAccountType=u)(phpgwAccountType=g)(phpgwAccountType=l))(mail=*)) (|(cn=*$search_for*)(mail=*$search_for*)) (!(phpgwaccountvisible=-1)) )";
141                                if($extendedinfo)
[325]142                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid", "employeeNumber", "ou");
[3388]143                                else
144                                    $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid");
[342]145                        }
[37]146                        else
[342]147                        {
[4906]148                                if($quote_status){
149                                        $filter="(& (phpgwAccountType=u) (|(cn=* $search_for *)(cn=$search_for *)(cn=* $search_for)) (!(phpgwaccountvisible=-1)) )";
150                                }
151                                else{
152                                        $filter="(& (phpgwAccountType=u)(cn=*$search_for*) (!(phpgwaccountvisible=-1)) )";
153                                }
154                        //      $filter="(& (phpgwAccountType=u)(cn=*$search_for*) (!(phpgwaccountvisible=-1)) )";
[3388]155                                if($extendedinfo)
[342]156                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible","jpegPhoto", "uid", "employeeNumber", "ou");
[3388]157                                else
158                                    $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible","jpegPhoto", "uid");
[342]159                        }
160                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result + 1);
161                        if(!$sr)
162                                return null;
[325]163                        $count_entries = ldap_count_entries($this->ds,$sr);
[342]164
[387]165                        // Get user org dn.
[271]166                        $user_dn = $_SESSION['phpgw_info']['expressomail']['user']['account_dn'];
167                        $user_sector_dn = ldap_explode_dn ( $user_dn, false );
168                        array_shift($user_sector_dn);
169                        array_shift($user_sector_dn);
170                        $user_sector_dn = implode(",", $user_sector_dn);
[387]171
[271]172                        // New search only on user sector
[342]173                        if ($count_entries > $this->max_result)
174                        {
[108]175                                // Close old ldap conection
176                                ldap_close($this->ds);
[387]177
[108]178                                // Reopen a local ldap connection, following referral
179                                $this->ldapRootConnect(true);
[387]180
[342]181                                $sr= ldap_search($this->ds, $user_sector_dn, $filter, $justthese);
182                                if(!$sr)
183                                        return null;
[108]184                                $count_entries = ldap_count_entries($this->ds,$sr);
[387]185
[342]186                                if ($count_entries > $this->max_result){
[108]187                                        $return = array();
188                                        $return['status'] = false;
[387]189                                        $return['error'] = "many results";
[108]190                                        return $return;
191                                }
[95]192                                else
193                                {
194                                        $quickSearch_only_in_userSector = true;
195                                }
196                        }
[387]197
[37]198                        $info = ldap_get_entries($this->ds, $sr);
[387]199
[2]200                        $tmp = array();
[271]201                        $tmp_users_from_user_org = array();
[387]202
[342]203                        if (!$quickSearch_only_in_userSector) {
204                                $catalogsNum=count($this->external_srcs);
205                                for ($i=0; $i<=count($this->external_srcs); $i++)       {
206                                        if ($this->external_srcs[$i]["quicksearch"]) {
[325]207                                                $this->ldapConnect(true,$i);
208                                                $filter="(|(cn=*$search_for*)(mail=*$search_for*))";
[3388]209                                                if($extendedinfo)
[342]210                                                $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid","employeeNumber", "ou");
[3388]211                                                else
212                                                    $justthese = array("cn", "mail", "telephoneNumber", "mobile", "phpgwAccountVisible", "uid");
[342]213                                                $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese, 0, $this->max_result+1);
214                                                if(!$sr)
215                                                        return null;
[325]216                                                $count_entries = ldap_count_entries($this->ds,$sr);
217                                                $search = ldap_get_entries($this->ds, $sr);
[387]218                                                for ($j=0; $j<$search["count"]; $j++) {
219                                                        $info[] = $search[$j];
[342]220                                                }
[325]221                                                $info["count"] = count($info)-1;
222                                        }
[271]223                                }
[342]224                        }
225
[325]226                        for ($i=0; $i<$info["count"]; $i++)
[342]227                        {
228                                if ($quickSearch_only_in_userSector)
[271]229                                {
[325]230                                        $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]231                                }
[325]232                                else
[342]233                                {
234                                        if (preg_match("/$user_sector_dn/i", $info[$i]['dn']))
[325]235                                        {
236                                                $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]237                                        }
[271]238                                        else
[342]239                                        {
[325]240                                                $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]241                                        }
242                                }
[342]243                        }
[271]244                        natcasesort($tmp_users_from_user_org);
[2]245                        natcasesort($tmp);
[387]246
[37]247                        if (($field != 'null') && ($ID != 'null'))
[2]248                        {
[271]249                                $i = 0;
[387]250
[271]251                                $tmp = array_merge($tmp, $tmp_users_from_user_org);
252                                natcasesort($tmp);
[387]253
[37]254                                foreach ($tmp as $info => $cn)
255                                {
256                                        $contacts_result[$i] = array();
257                                        $contacts_result[$i]["cn"] = $cn;
[325]258                                        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]259                                        $i++;
260                                }
[95]261                                $contacts_result['quickSearch_only_in_userSector'] = $quickSearch_only_in_userSector;
[2]262                        }
[37]263                        else
264                        {
[271]265                                $options_users_from_user_org = '';
[37]266                                $options = '';
[271]267
268                                /* List of users from user org */
269                                $i = 0;
270                                foreach ($tmp_users_from_user_org as $info => $cn)
271                                {
272                                        $contacts_result[$i] = array();
273                                        $options_users_from_user_org .= $this->make_quicksearch_card($info, $cn);
274                                        $i++;
275                                }
276
277                                /* List of users from others org */
[37]278                                foreach ($tmp as $info => $cn)
279                                {
280                                        $contacts_result[$i] = array();
[271]281                                        $options .= $this->make_quicksearch_card($info, $cn);
[37]282                                        $i++;
283                                }
[387]284
[95]285                                if ($quickSearch_only_in_userSector)
286                                {
[271]287                                        if ($options != '')
288                                        {
289                                                $head_option =
290                                                        '<tr class="quicksearchcontacts_unselected">' .
291                                                                '<td colspan="2" width="100%" align="center">' .
[342]292                                                                        str_replace("%1", $this->max_result,$this->functions->getLang('More than %1 results were found')) . '.<br>' .
293                                                                        $this->functions->getLang('Showing only the results found in your organization') . '.';
[271]294                                                                '</td>' .
295                                                        '</tr>';
296                                                $contacts_result = $head_option . $options_users_from_user_org . $options;
297                                        }
298                                        else
299                                        {
300                                                $return = array();
301                                                $return['status'] = false;
302                                                $return['error'] = "many results";
303                                                return $return;
304                                        }
[108]305                                }
[271]306                                else
307                                {
308                                        if (($options_users_from_user_org != '') && ($options != ''))
309                                        {
310                                                $head_option0 =
311                                                        '<tr class="quicksearchcontacts_unselected">' .
312                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
[342]313                                                                        $this->functions->getLang('Users from your organization') . '</B> ['.count($tmp_users_from_user_org).']';
314                                                                '</td>' .
[271]315                                                        '</tr>';
316
317                                                $head_option1 =
318                                                        '<tr class="quicksearchcontacts_unselected">' .
319                                                                '<td colspan="2" width="100%" align="center" style="background:#EEEEEE"><B>' .
[342]320                                                                        $this->functions->getLang('Users from others organizations') . '</B> ['.count($tmp).']';
321                                                                '</td>' .
[271]322                                                        '</tr>';
323                                        }
324                                        $contacts_result = $head_option0 . $options_users_from_user_org . $head_option1 . $options;
325                                }
[37]326                        }
[2]327                }
328                ldap_close($this->ds);
329                return $contacts_result;
330        }
331
[271]332        function make_quicksearch_card($info, $cn)
333        {
334                include_once("class.functions.inc.php");
[387]335                $functions = new functions;
336
[271]337                $contacts_result = array();
338                $contacts_result["cn"] = $cn;
[3388]339                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
340                    $extendedinfo=true;
341                else
342                    $extendedinfo=false;
[387]343
[3388]344                list ($contacts_result["mail"], $contacts_result["phone"], $contacts_result["mobile"], $contacts_result["uid"], $contacts_result["jpegphoto"], $contacts_result["employeenumber"], $contacts_result["ou"]) = split ('%', $info);
345
[271]346                if ($contacts_result['jpegphoto'])
347                        $photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result['mail'].'">';
348                else
[1495]349                        $photo_link = '<img src="./templates/default/images/photo.jpg">';
[387]350
[271]351                $phoneUser = $contacts_result['phone'];
[387]352
[271]353                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
[342]354                        $phoneUser = '<a title="'.$this->functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
[271]355                        if($contacts_result['mobile']){
[342]356                                $phoneUser .= ' / <a title="'.$this->functions->getLang("Call to Mobile Number").'" href="#" onclick="InfoContact.connectVoip(\''.$contacts_result['mobile'].'\',\'mob\')">'.$contacts_result['mobile'].'</a>';
[271]357                        }
358                }
[3388]359                //verifica de existe o celular do funcionario ($contacts_result["mobile"]), se nao, $phoneUser fica vazia;
360
361                    $mobileUser = $contacts_result["mobile"];
362                    if($mobileUser && !$voipMobile) {
363                            $phoneUser .= " / $mobileUser";
364                    }
365                    $empNumber = $contacts_result["employeenumber"];
366                    if($empNumber) {
367                            $empNumber = "$empNumber - ";
368                    }
369                    $ou = $contacts_result["ou"];
370                    if($ou) {
371                            $ou = "<br/>$ou" ;
372                    }
373
[644]374                // Begin: nickname, firstname and lastname for QuickAdd.
375                $fn = $contacts_result["cn"];
376                $array_name = explode(" ", $fn);
377                if(count($array_name) > 1){                     
378                        $fn = $array_name[0];
379                        array_shift($array_name);
380                        $sn = implode(" ",$array_name);
381                }
382                // End:
[271]383                $option =
384                        '<tr class="quicksearchcontacts_unselected">' .
385                                '<td class="cc" width="1%">' .
[342]386                                        '<a title="'.$this->functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
[271]387                                                $photo_link .
388                                        '</a>' .
389                                '</td>' .
390                                '<td class="cc">' .
[3388]391                                        '<span name="cn">' . $empNumber . $contacts_result['cn'] . '</span>' . '<br>' .
392                                        '<a title="'.$functions->getLang("Write message").'" onClick="javascript:QuickSearchUser.create_new_message(\''.$contacts_result["cn"].'\', \''.$contacts_result["mail"].'\')">' .
[271]393                                                '<font color=blue>' .
[644]394                                                '<span name="mail">' . $contacts_result['mail'] . '</span></a></font>'.
395                                                '<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"].'\')">'.
396                                        '<br>' .
[271]397                                        $phoneUser .
[3388]398                                        $ou .
[271]399                                '</td>' .
[644]400                                '</tr>';
[271]401                return $option;
402        }
403
[387]404        function get_catalogs(){
[325]405                $catalogs = array();
[387]406                $catalogs[0] = $this->functions->getLang("Global Catalog");
407                if($this->external_srcs)
[342]408                        foreach ($this->external_srcs as $key => $valor ){
[325]409                        $catalogs[$key] = $valor['name'];
410                }
411                return $catalogs;
412        }
[2]413        function get_organizations($params){
414                $organizations = array();
[325]415                $params['referral']?$referral = $params['referral']:$referral = false;
416                $cat = $params['catalog'];
[2]417
[325]418                $this->ldapConnect($referral,$cat);
419
420                        if($this->branch != '') {
[1363]421                                $filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
[325]422                                $justthese = array("$this->branch");
[342]423                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
424                        $info = ldap_get_entries($this->ds, $sr);
[387]425
[3530]426                        if($info["count"] == 0)
427                        {
428                            $organizations[0]['ou'] = $this->ldap_context;
429                        }
430
[342]431                        for ($i=0; $i<$info["count"]; $i++)
432                                $organizations[$i] = $info[$i]["ou"][0];
[325]433
[342]434                        ldap_close($this->ds);
[387]435                        sort($organizations);
[342]436                return $organizations;
[325]437                        }else{
438                        return null;
[2]439        }
[342]440        }
[2]441        function get_organizations2($params){
442                $organizations = array();
443                $referral = $params['referral'];
444                $this->ldapRootConnect($referral);
445                if ($this->ds) {
[3530]446                        $filter="(&(objectClass=organizationalUnit)(!(phpgwAccountVisible=-1)))";
[2]447                        $justthese = array("ou");
448                        $sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
449                        $info = ldap_get_entries($this->ds, $sr);
[387]450
[3530]451
452                        if($info["count"] == 0)
[2]453                        {
[3530]454                            $organizations[0]['ou'] = $this->ldap_context;
455                            $organizations[0]['dn'] = $this->ldap_context;
[2]456                        }
[3530]457                        else{
458                            for ($i=0; $i<$info["count"]; $i++)
459                            {
460                                    $organizations[$i]['ou'] = $info[$i]["ou"][0];
461                                    $organizations[$i]['dn'] = $info[$i]["dn"];
462                            }
463                        }
[2]464                        ldap_close($this->ds);
[387]465                        sort($organizations);
[2]466                }
467                return $organizations;
468        }
[925]469        //Busca usuarios de um contexto e ja retorna as options do select - usado por template serpro;
470        function search_users($params)
471        {
472                $this->ldapConnect(false,0);
473                //Monta lista de Grupos e Usuarios
474                $users = Array();
475                $groups = Array();
476                $user_context= $this->ldap_context;
477                $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
478                $filtro =utf8_encode($params['filter']);
479                $context =utf8_encode($params['context']);//adicionado
[2]480
[925]481                if ($this->ds)
482                {
483                    $justthese = array("gidNumber","cn");
484                    if ($params['type'] == 'search')
[1633]485                        $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(cn=*$filtro*))"),$justthese);
[925]486                    else
[1639]487                        $sr=ldap_list($this->ds,  $context ? $context : $user_context, ("(&(phpgwaccounttype=g)(!(phpgwaccountvisible=-1))(cn=*".$filtro."*))"),$justthese);
[925]488                    $info = ldap_get_entries($this->ds, $sr);
489                    for ($i=0; $i<$info["count"]; $i++)
490                        $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
[3909]491                    $justthese = array("phpgwaccountvisible","uidNumber","cn");
[925]492                    if ($params['type'] == 'search')
493                       // $sr=ldap_search($this->ds, $user_context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
494                       $sr=ldap_search($this->ds, $context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
495                    else
[1639]496                        $sr=ldap_list($this->ds, $context ? $context : $user_context, ("(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1))(phpgwaccountstatus=A)(|(cn=*$filtro*)(mail=$filtro*)))"),$justthese);
[925]497
498                    $info = ldap_get_entries($this->ds, $sr);
499                    for ($i=0; $i<$info["count"]; $i++)
500                    {
501                        if ($info[$i]["phpgwaccountvisible"][0] == '-1')
502                      continue;
[3909]503                        $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
[925]504                    }
505                }
506                ldap_close($this->ds);
507       
508                @asort($users);
509                @reset($users);
510                @asort($groups);
511                @reset($groups);
512                $user_options ='';
513                $group_options ='';
514
[3909]515                foreach($groups as $id => $user_array) {
[925]516                        $newId = $id.'U';
517                        $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
[3909]518                }
[925]519                foreach($users as $id => $user_array) {
520                    if($owner != $id){
[3909]521                        $newId = $id.'U';
522                        $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
[925]523                    }
524                }
[3909]525                return array("users" => $user_options, "groups" => $group_options);
[925]526        }
527
[2]528        function catalogsearch($params)
[387]529        {
[325]530                $cn     = $params['search_for'] ? "*".utf8_encode($params['search_for'])."*" : "*";
[387]531                $max_result       = $params['max_result'] ? $params['max_result'] : $this->max_result;
[325]532                $catalog = $params['catalog'];
[2]533                $error = False;
[3388]534                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
535                    $extendedinfo=true;
536                else
537                    $extendedinfo=false;
[387]538
[3388]539
[381]540                $this->ldapConnect(true,$catalog);
[387]541
542                $params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;
543
544                if ($this->ds) {
[325]545                        if ($catalog == 0){
546                                //os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
[3388]547                                if($extendedinfo)
[325]548                                $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible", "employeeNumber", "ou");
[3388]549                                else
550                                    $justthese = array("cn", "mail", "phpgwaccounttype", "phpgwAccountVisible");
551
[325]552                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(cn=".$cn."))";
553                                //$user_context = "ou=".$params['organization'].",".$this->ldap_context;
554                        }else {
555                                //os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
[3388]556                                if($extendedinfo)
[325]557                                $justthese = array("cn", "mail", "employeeNumber", "ou");
[3388]558                                else
559                                    $justthese = array("cn", "mail");
[325]560                                $filter="(&(objectClass=".$this->object_class.")(cn=".$cn."))";
561                                //$user_context = $this->branch."=".$params['organization'].",".$external_srcs[$catalog]['dn'];
562                        }
[387]563
[381]564                        $sr=@ldap_search($this->ds, $user_context, $filter, $justthese, 0, $max_result+1);
[342]565                        if(!$sr)
566                                return null;
[2]567                        $count_entries = ldap_count_entries($this->ds,$sr);
[381]568                        if ($count_entries > $max_result){
[2]569                                $info = null;
[387]570                                $error = True;
[2]571                        }
572                        else
[387]573                                $info = ldap_get_entries($this->ds, $sr);
574
575                        ldap_close($this->ds);
576
[2]577                        $u_tmp = array();
578                        $g_tmp = array();
[387]579
[2]580                        for ($i=0; $i<$info["count"]; $i++){
[325]581                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
582                                        //aqui eh feita a concatenacao do departamento ao cn;
583                                        $u_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]). '%' . $info[$i]["ou"][0];
[387]584                                if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'L') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
[325]585                                        $g_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]);
[387]586                        }
587
[2]588                        natcasesort($u_tmp);
589                        natcasesort($g_tmp);
[387]590
[2]591                        $i = 0;
[387]592                        $users = array();
[325]593
[387]594                        foreach ($u_tmp as $mail => $cn){
595
[325]596                                $tmp = explode("%", $cn); //explode o cn pelo caracter "%" e joga em $tmp;
597                                $name = $tmp[0]; //pega o primeiro item (cn) do vetor resultante do explode acima;
598                                $department = $tmp[1]; //pega o segundo item (ou) do vetor resultanto do explode acima;
599                                $users[$i++] = array("name" => $name, "email" => $mail, "department" => $department);
600
[2]601                        }
602                        unset($u_tmp);
[387]603
604                        $i = 0;
[2]605                        $groups = array();
[387]606
607                        foreach ($g_tmp as $mail => $cn){
[2]608                                $groups[$i++] = array("name" => $cn, "email" => $mail);
609                        }
610                        unset($g_tmp);
[387]611
[2]612                        return  array('users' => $users, 'groups' => $groups, 'error' => $error);
[387]613                }else
[2]614                return null;
615        }
[387]616
[2]617        function get_emails_ldap(){
618
619                $result['mail']= array();
[387]620                $result['mailalter']= array();
[2]621                $user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
622                $this->ldapRootConnect(false);
623                if ($this->ds) {
[387]624                        $filter="uid=".$user;
[2]625                        $justthese = array("mail","mailAlternateAddress");
626                        $sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
627                        $ent = ldap_get_entries($this->ds, $sr);
628                        ldap_close($this->ds);
[387]629
[2]630                        for ($i=0; $i<$ent["count"]; $i++){
631                                $result['mail'][] = $ent[$i]["mail"][0];
[387]632                                $result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];
[2]633                        }
634                }
635                return $result;
636        }
[387]637
[342]638        //Busca usuarios de um contexto e ja retorna as options do select;
[2]639        function get_available_users($params)
[37]640    {
641        $this->ldapRootConnect();
[342]642        //Monta lista de Grupos e Usuarios
[37]643        $users = Array();
644        $groups = Array();
645        $user_context= $params['context'];
646        $owner = $_SESSION['phpgw_info']['expressomail']['user']['owner'];
[2]647
[37]648        if ($this->ds)
649        {
650            $justthese = array("gidNumber","cn");
651            if ($params['type'] == 'search')
652                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
653            else
654                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=g)(!(phpgwaccountvisible=-1)))"),$justthese);
655            $info = ldap_get_entries($this->ds, $sr);
656            for ($i=0; $i<$info["count"]; $i++)
[387]657                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
[37]658            $justthese = array("phpgwaccountvisible","uidNumber","cn");
659            if ($params['type'] == 'search')
660                $sr=ldap_search($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
661            else
662                $sr=ldap_list($this->ds, $user_context, ("(&(cn=*)(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))"),$justthese);
[2]663
[37]664            $info = ldap_get_entries($this->ds, $sr);
665            for ($i=0; $i<$info["count"]; $i++)
666            {
667                if ($info[$i]["phpgwaccountvisible"][0] == '-1')
668                    continue;
669                $users[$uids=$info[$i]["uidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    u);
670            }
671        }
672        ldap_close($this->ds);
[387]673
[37]674        @asort($users);
[387]675        @reset($users);
[37]676        @asort($groups);
677        @reset($groups);
678        $user_options ='';
679        $group_options ='';
[2]680
[37]681        foreach($groups as $id => $user_array) {
682                $newId = $id.'U';
683                $group_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
684        }
685        foreach($users as $id => $user_array) {
686            if($owner != $id){
687                $newId = $id.'U';
688                $user_options .= '<option  value="'.$newId.'">'.utf8_decode($user_array['name']).'</option>'."\n";
689            }
690        }
691        return array("users" => $user_options, "groups" => $group_options);
692    }
693
[342]694        //Busca usuarios de um contexto e ja retorna as options do select;
[2]695        function get_available_users2($params)
696        {
697                $this->ldapRootConnect();
[387]698
[2]699                $context= $params['context'];
700                $justthese = array("cn", "uid", "cn");
[21]701                $filter = "(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))";
[2]702
[387]703            if ($this->ds)
[2]704            {
705                        $sr=ldap_search($this->ds, $context, $filter, $justthese);
706                        $entries = ldap_get_entries($this->ds, $sr);
[387]707
[2]708                        for ($i=0; $i<$entries["count"]; $i++){
709                                if($_SESSION['phpgw_info']['expressomail']['user']['account_lid'] != $entries[$i]["uid"][0]){
710                                        $u_tmp[$entries[$i]["uid"][0]] = $entries[$i]["cn"][0];
711                                }
712                        }
[387]713
[2]714                        natcasesort($u_tmp);
715
716                        $i = 0;
717                        $users = array();
[387]718
[2]719                        if (count($u_tmp))
720                        {
721                                foreach ($u_tmp as $uidnumber => $cn)
722                                {
723                                        $options .= "<option value=$uidnumber>$cn</option>";
724                                }
725                                unset($u_tmp);
726                        }
727
728                        ldap_close($this->ds);
729                return $options;
730                }
731        }
[387]732
[2]733        function uid2cn($uid)
[387]734        {
[2]735                // do not follow the referral
736                $this->ldapRootConnect(false);
737                if ($this->ds)
738                {
[387]739                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
[2]740                        $justthese = array("cn");
[344]741                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
742                        if(!$sr)
743                                return false;
[2]744                        $info = ldap_get_entries($this->ds, $sr);
[325]745                        return utf8_decode($info[0]["cn"][0]);
[2]746                }
747                return false;
748        }
[757]749        function uidnumber2uid($uidnumber)
750        {
751                // do not follow the referral
752                $this->ldapRootConnect(false);
753                if ($this->ds)
754                {
755                        $filter="(&(phpgwAccountType=u)(uidnumber=$uidnumber))";
756                        $justthese = array("uid");
757                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
758                        if(!$sr)
759                                return false;
760                        $info = ldap_get_entries($this->ds, $sr);
761                        return $info[0]["uid"][0];
762                }
763                return false;
764        }
[387]765        function getSharedUsersFrom($params){
[2]766                $filter = '';
[1912]767                $i = 0;         
768                //Added to save if must save sent messages in shared folder
769                $acl_save_sent_in_shared = array();
770               
[371]771                if($params['uids']) {
[387]772                        $uids = explode(";",$params['uids']);
[1912]773                        $this->imap = new imap_functions();                     
[371]774                        foreach($uids as $index => $uid){
[387]775                                $params = array();
[1912]776                                //Added to save if user has create permission
777                                $acl_create_message = array();
[371]778                                $acl = $this->imap->getacltouser($uid);
[1912]779                                if ( preg_match("/a/",$acl )){                         
780                                        $filter .= "(uid=$uid)";                                       
781                                        if ( preg_match("/p/",$acl )){                         
782                                                $acl_save_sent_in_shared[ $i ] =$uid;
783                                                $i++;
784                                        }                                       
785                                }                                                       
786                        }                       
[2]787                }
[1912]788               
[387]789                $this->ldapRootConnect(false);
[371]790                if ($this->ds) {
[1912]791                        $justthese = array("cn","mail","uid");
[371]792                        if($filter) {
[1912]793                                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=s))(|$filter))";
[371]794                                $sr             =       ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
[387]795                                ldap_sort($this->ds,$sr,"cn");
796                                $info   =       ldap_get_entries($this->ds, $sr);
[1912]797                                $var = print_r($acl_save_sent_in_shared, true);                         
798                                for ($i = 0;$i < $info["count"]; $i++){
[371]799                                        $info[$i]['cn'][0] = utf8_decode($info[$i]['cn'][0]);
[1912]800                                        //verify if user has permission to save sent messages in a shared folder
801                                        if ( in_array( $info[$i]['uid'][0],$acl_save_sent_in_shared) ){                                         
802                                                $info[$i]['save_shared'][0] = 'y';
803                                        } else $info[$i]['save_shared'][0] = 'n';
804                                }
[371]805                        }
[1912]806               
[371]807                        $info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
[387]808
[371]809                        //Find institucional_account.
810                        $filter="(&(phpgwAccountType=i)(mailForwardingAddress=".$_SESSION['phpgw_info']['expressomail']['user']['email']."))";
[387]811                        $sr     = ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
[448]812                        ##
813                        # @AUTHOR Rodrigo Souza dos Santos
814                        # @DATE 2008/09/17
815                        # @BRIEF Changing to ensure that the variable session is always with due value.
816                        ##
817                        if(ldap_count_entries($this->ds,$sr))
818                        {
819                                ldap_sort($this->ds,$sr,"cn");
820                                $result = ldap_get_entries($this->ds, $sr);
821                                for ($j = 0;$j < $result["count"]; $j++){
822                                        $info[$i]['cn'][0] = utf8_decode($result[$j]['cn'][0]);
[1912]823                                        $info[$i]['mail'][0] = $result[$j]['mail'][0];
824                                        $info[$i]['save_shared'][0] = 'n';
825                                        $info[$i++]['uid'][0] = $result[$j]['uid'][0];                                 
[448]826                                }
[371]827                        }
[448]828
829                        $_SESSION['phpgw_info']['expressomail']['user']['shared_mailboxes'] = $info;
[1912]830                       
[2]831                        return $info;
832                }
833        }
[27]834
[205]835        function getUserByEmail($params)
836        {
[3163]837                $expires = 60*60*24*30; /* 30 days */
838                header("Cache-Control: maxage=".$expires);
839                header("Pragma: public");
840                header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires)); 
[205]841                $filter="(&(phpgwAccountType=u)(mail=" . $params['email'] . "))";
842                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
[3388]843                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['extended_info'])
844                    $extendedinfo=true;
845                else
846                    $extendedinfo=false;
[205]847
[3388]848                if($extendedinfo)
849                    $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile","ou","employeeNumber");
850                else
851                    $justthese = array("cn","uid","telephoneNumber","jpegPhoto","mobile");
852
[27]853                // Follow the referral
[205]854                $ds = $this->ldapConnect2(true);
855                if ($ds)
[387]856                {
[342]857                        $sr=@ldap_search($ds, $ldap_context, $filter, $justthese);
[387]858
[205]859                        if (!$sr)
860                                return null;
[387]861
[205]862                        $entry = ldap_first_entry($ds, $sr);
[387]863
864                        if($entry) {
[381]865                                $obj =  array("cn" => utf8_decode(current(ldap_get_values($ds, $entry, "cn"))),
[205]866                                                  "email" => $params['email'],
867                                                  "uid" => ldap_get_values($ds, $entry, "uid"),
[27]868                                                  "type" => "global",
[205]869                                                  "mobile" =>  @ldap_get_values($ds, $entry, "mobile"),
[3388]870                                                  "telefone" =>  @ldap_get_values($ds, $entry, "telephonenumber"),
871                                                  "ou" =>  @ldap_get_values($ds, $entry, "ou"),
872                                                  "employeeNumber" =>  @ldap_get_values($ds, $entry, "employeeNumber")
[205]873                                        );
[27]874
[320]875                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] = @ldap_get_values_len($ds, $entry, "jpegphoto");
[205]876                                ldap_close($ds);
[27]877                                return $obj;
[205]878                        }
[27]879                }
880                return null;
881        }
[1500]882       
883        function uid2uidnumber($uid)
884        {
885                // do not follow the referral
886                $this->ldapRootConnect(false);
887                if ($this->ds)
888                {
889                        $filter="(&(phpgwAccountType=u)(uid=$uid))";
890                        $justthese = array("uidnumber");
891                        $sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
892                        if(!$sr)
893                                return false;
894                        $info = ldap_get_entries($this->ds, $sr);
895                        return $info[0]["uidnumber"][0];
896                }
897                return false;
898        }
[2]899}
[108]900?>
Note: See TracBrowser for help on using the repository browser.