source: sandbox/expressoMail1_2/MailArchiver/2.2/reports/inc/class.functions.inc.php @ 4644

Revision 4644, 75.7 KB checked in by fernando-alberto, 13 years ago (diff)

Ticket #1269 - Mergiando revisoes do brach22 de rev4447 ate rev4643

Line 
1<?php
2        /**********************************************************************************\
3        *Expresso Relatório                                                                                           *
4        *by Elvio Rufino da Silva (elviosilva@yahoo.com.br, elviosilva@cepromat.mt.gov.br) *
5        * --------------------------------------------------------------------------------*
6        *  This program is free software; you can redistribute it and/or modify it                *
7        *  under the terms of the GNU General Public License as published by the                  *
8        *  Free Software Foundation; either version 2 of the License, or (at your                 *
9        *  option) any later version.                                                                                                    *
10        **********************************************************************************/
11       
12        include_once('class.db_functions.inc.php');
13        include_once(PHPGW_API_INC.'/class.config.inc.php');
14
15        class functions
16        {
17                var $public_functions = array
18                (
19                        'make_array_acl'                        => True,
20                        'check_acl'                                     => True,
21                        'read_acl'                                      => True,
22                        'exist_account_lid'                     => True,
23                        'exist_email'                           => True,
24                        'array_invert'                          => True,
25                        'Paginate_user'                         => True,
26                        'Paginate_cota'                         => True,
27                        'Paginate_shareAccount'                 => True,
28                        'Paginate_institutionalAccount'         => True,
29                        'Paginate_user_logon'           => True,
30                        'get_list_all'                          => True,
31                        'get_groups_list'                       => True,
32                        'get_list_ou_user_logon'        => True,
33                        'show_access_log'                       => True,
34                        'get_sectors_list'                      => True
35                );
36               
37                var $nextmatchs;
38                var $sectors_list = array();
39                var $current_config;
40               
41                function functions()
42                {
43                        $this->db_functions = new db_functions;
44                        $GLOBALS['phpgw']->db = $this->db_functions->db;
45
46                        $c = new config;
47                        $c->read_repository();
48                        $this->current_config = $c->config_data;
49                }
50
51                // Account and type of access. Return: Have access ? (true/false)
52                function check_acl($account_lid, $access)
53                {
54                        $acl = $this->read_acl($account_lid);
55                        $array_acl = $this->make_array_acl($acl['acl']);
56                       
57                        switch($access)
58                        {
59                                case list_users:
60                                        if ($array_acl[acl_add_users] || $array_acl[acl_edit_users] || $array_acl[acl_delete_users] || $array_acl[acl_change_users_password] || $array_acl[acl_change_users_quote] || $array_acl[acl_edit_sambausers_attributes] || $array_acl[acl_view_users] || $array_acl[acl_manipulate_corporative_information] || $array_acl[acl_edit_users_phonenumber] )
61                                                return true;
62                                        break;
63                                case report_users:
64                                        if ($array_acl[acl_change_users_quote] || $array_acl[acl_view_users])
65                                                return true;
66                                        break;
67                                case list_groups:
68                                        if ($array_acl[acl_add_groups] || $array_acl[acl_edit_groups] || $array_acl[acl_delete_groups])
69                                                return true;
70                                        break;
71                                case list_maillists:
72                                        if ($array_acl[acl_add_maillists] || $array_acl[acl_edit_maillists] || $array_acl[acl_delete_maillists])
73                                                return true;
74                                        break;
75                                case list_sectors:
76//                                      if ($array_acl[acl_create_sectors] || $array_acl[acl_edit_sectors] || $array_acl[acl_delete_sectors])
77                                        if ($array_acl[acl_view_users])
78                                                return true;
79                                        break;
80                                case list_computers:
81                                        if ($array_acl[acl_create_computers] || $array_acl[acl_edit_computers] || $array_acl[acl_delete_computers])
82                                                return true;
83                                        break;
84
85                                case display_groups:
86                                        if ( $array_acl[acl_edit_users] || $array_acl[acl_view_users] || ($array_acl[acl_edit_sambausers_attributes] && ($this->current_config['expressoAdmin_samba_support'] == 'true')) )
87                                                return true;
88                                        break;
89                                case display_emailconfig:
90                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
91                                                return true;
92                                        break;
93                                case display_applications:
94                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
95                                                return true;
96                                        break;
97                                case display_emaillists:
98                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
99                                                return true;
100                                        break;
101
102                                case list_institutional_accounts:
103                                        if ($array_acl[acl_add_institutional_accounts] || $array_acl[acl_edit_institutional_accounts] || $array_acl[acl_delete_institutional_accounts])
104                                                return true;
105                                        break;
106
107
108                                default:
109                                        return $array_acl["acl_$access"];
110                        }
111                        return false;
112                }
113
114               
115                // Read acl from db
116                function read_acl($account_lid)
117                {
118                        $acl = $this->db_functions->read_acl($account_lid);
119                       
120                        $result['acl'] = $acl[0]['acl'];
121                        $result['manager_lid'] = $acl[0]['manager_lid'];
122                        $result['raw_context'] = $acl[0]['context'];
123                       
124                        $all_contexts = split("%", $acl[0]['context']);
125                        foreach ($all_contexts as $index=>$context)
126                        {
127                                $result['contexts'][] = $context;
128                                $result['contexts_display'][] = str_replace(", ", ".", ldap_dn2ufn( $context ));
129                        }
130                       
131                        return $result;
132                }
133               
134                function make_array_acl($acl)
135                {
136                        $array_acl_tmp = array();
137                        $tmp = array(           "acl_add_users",
138                                                                "acl_edit_users",
139                                                                "acl_delete_users",
140                                                                "acl_EMPTY1",
141                                                                "acl_add_groups",
142                                                                "acl_edit_groups",
143                                                                "acl_delete_groups",
144                                                                "acl_change_users_password",
145                                                                "acl_add_maillists",
146                                                                "acl_edit_maillists",
147                                                                "acl_delete_maillists",
148                                                                "acl_EMPTY2",
149                                                                "acl_create_sectors",
150                                                                "acl_edit_sectors",
151                                                                "acl_delete_sectors",
152                                                                "acl_edit_sambausers_attributes",
153                                                                "acl_view_global_sessions",
154                                                                "acl_view_logs",
155                                                                "acl_change_users_quote",
156                                                                "acl_set_user_default_password",
157                                                                "acl_create_computers",
158                                                                "acl_edit_computers",
159                                                                "acl_delete_computers",
160                                                                "acl_rename_users",
161                                                                "acl_edit_sambadomains",
162                                                                "acl_view_users",
163                                                                "acl_edit_email_groups",
164                                                                "acl_empty_user_inbox",
165                                                                "acl_manipulate_corporative_information",
166                                                                "acl_edit_users_picture",
167                                                                "acl_edit_scl_email_lists",
168                                                                "acl_edit_users_phonenumber",
169                                                                "acl_add_institutional_accounts",
170                                                                "acl_edit_institutional_accounts",
171                                                                "acl_remove_institutional_accounts"
172                                                                );
173                       
174                        foreach ($tmp as $index => $right)
175                        {
176                                $bin = '';
177                                for ($i=0; $i<$index; $i++)
178                                {
179                                        $bin .= '0';
180                                }
181                                $bin = '1' . $bin;
182                               
183                                $array_acl[$right] = $this->safeBitCheck(bindec($bin), $acl);
184                        }
185                        return $array_acl;
186                }
187               
188                function get_inactive_users($contexts)
189                {
190                        $retorno = array();
191                        $tempUsers = array();
192                        //Pego no LDAP todos os usuários dos contextos em questão.
193                        $usuariosLdap = $this->get_list('accounts','',$contexts);
194                        foreach($usuariosLdap as $usuarioLdap)
195                        {
196                                $tempUsers[$usuarioLdap["account_id"]] = $usuarioLdap["account_lid"];
197                        }
198                        $ids = implode(",",array_keys($tempUsers)); //Consigo a lista de uids daquele contexto para mandar na query para o banco.
199                       
200                        //Pego nas configurações do expresso o número de dias necessários para inatividade.
201                        $timeToExpire = $GLOBALS['phpgw_info']['server']['time_to_account_expires'];
202                       
203                       
204                        $ultimoTsValido = time() - ($timeToExpire * 86400); //O último timestamp válido é dado pelo de agora menos o número de dias para expirar vezes a quantidade de segundos existente em 1 dia.
205                        $query = "select account_id,max(li) as last_login from phpgw_access_log where account_id in (".$ids.") group by account_id having max(li) < ".$ultimoTsValido." order by max(li)";
206
207                        $GLOBALS['phpgw']->db->query($query);
208                        while($GLOBALS['phpgw']->db->next_record())
209                        {
210                                $result = $GLOBALS['phpgw']->db->row();
211                                array_push($retorno,array("uidNumber"=>$result["account_id"],"login"=> $tempUsers[$result["account_id"]],"li"=>$result["last_login"]));
212                        }
213                       
214                        return $retorno;
215                }
216
217                function safeBitCheck($number,$comparison)
218                {
219                $binNumber = base_convert($number,10,2);
220                $binComparison = strrev(base_convert($comparison,10,2));
221                        $str = strlen($binNumber);
222               
223                if ( ($str <= strlen($binComparison)) && ($binComparison{$str-1}==="1") )
224                {
225                                return '1';
226                }
227                        else
228                {
229                                return '0';
230                        }
231                }
232               
233                function get_list_all($type, $query, $contexts,$sizelimit)
234                {
235                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
236                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
237                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
238                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
239                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
240                        ldap_bind($ldap_conn,$dn,$passwd);
241                       
242                        if ($type == 'accounts')
243                        {
244                                $justthese = array("uidnumber", "uid", "cn", "mail");
245                                $filter="(&(phpgwAccountType=u)(|(ou=*".$query."*)(uid=*".$query."*)(sn=*".$query."*)(cn=*".$query."*)(givenName=*".$query."*)(mail=$query*)(mailAlternateAddress=$query*)))";
246                                                               
247                                $tmp = array();
248                                foreach ($contexts as $index=>$context)
249                                {
250                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
251                                        $info = ldap_get_entries($ldap_conn, $search);
252                                       
253                                        for ($i=0; $i < $info['count']; $i++)
254                                        {
255                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
256                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
257                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
258                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
259                                                $sort[] = $info[$i]['uid'][0];
260                                        }
261                                }
262                                ldap_close($ldap_conn);
263                               
264                                if (count($sort))
265                                {
266                                        natcasesort($sort);
267                                        foreach ($sort as $user_uid)
268                                                $return[$user_uid] = $tmp[$user_uid];
269                                }
270                               
271                                return $return;
272                        }
273                        elseif($type == 'groups')
274                        {
275                                $filter="(&(phpgwAccountType=g)(cn=*$query*))";
276                                $justthese = array("gidnumber", "cn", "description");
277                               
278                                $tmp = array();
279                                foreach ($contexts as $index=>$context)
280                                {
281                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
282                                        $info = ldap_get_entries($ldap_conn, $search);
283                                        for ($i=0; $i < $info['count']; $i++)
284                                        {
285                                                $tmp[$info[$i]['cn'][0]]['cn']= $info[$i]['cn'][0];
286                                                $tmp[$info[$i]['cn'][0]]['description']= $info[$i]['description'][0];
287                                                $tmp[$info[$i]['cn'][0]]['gidnumber']= $info[$i]['gidnumber'][0];
288                                                $sort[] = $info[$i]['cn'][0];
289                                        }
290                                }
291                                ldap_close($ldap_conn);
292                               
293                                natcasesort($sort);
294                                foreach ($sort as $group_cn)
295                                        $return[$group_cn] = $tmp[$group_cn];
296                               
297                                return $return;
298                        }
299                        elseif($type == 'maillists')
300                        {
301                                $filter="(&(phpgwAccountType=l)(|(cn=*".$query."*)(uid=*".$query."*)(mail=*".$query."*)))";
302                                $justthese = array("uidnumber", "cn", "uid", "mail");
303
304                                $tmp = array();
305                                foreach ($contexts as $index=>$context)
306                                {
307                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
308                                        $info = ldap_get_entries($ldap_conn, $search);
309                                       
310                                        for ($i=0; $i < $info['count']; $i++)
311                                        {
312                                                $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
313                                                $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
314                                                $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
315                                                $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
316                                                $sort[] = $info[$i]['uid'][0];
317                                        }
318                                }
319                                ldap_close($ldap_conn);
320                               
321                                natcasesort($sort);
322                                foreach ($sort as $maillist_uid)
323                                        $return[$maillist_uid] = $tmp[$maillist_uid];
324                               
325                                return $return;
326                        }
327                        elseif($type == 'computers')
328                        {
329                                $filter="(&(objectClass=sambaSAMAccount)(|(sambaAcctFlags=[W          ])(sambaAcctFlags=[DW         ])(sambaAcctFlags=[I          ])(sambaAcctFlags=[S          ]))(cn=*".$query."*))";
330                                $justthese = array("cn","uidNumber","description");
331
332                                $tmp = array();
333                                foreach ($contexts as $index=>$context)
334                                {
335                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
336                                        $info = ldap_get_entries($ldap_conn, $search);
337                                        for ($i=0; $i < $info['count']; $i++)
338                                        {
339                                                $tmp[$info[$i]['cn'][0]]['cn']                  = $info[$i]['cn'][0];
340                                                $tmp[$info[$i]['cn'][0]]['uidNumber']   = $info[$i]['uidnumber'][0];
341                                                $tmp[$info[$i]['cn'][0]]['description'] = utf8_decode($info[$i]['description'][0]);
342                                                $sort[] = $info[$i]['cn'][0];
343                                        }
344
345                                }
346                                ldap_close($ldap_conn);
347                               
348                                if (!empty($sort))
349                                {
350                                        natcasesort($sort);
351                                        foreach ($sort as $computer_cn)
352                                                $return[$computer_cn] = $tmp[$computer_cn];
353                                }
354                               
355                                return $return;
356                        }
357                }
358
359                function get_list($type, $query, $contexts)
360                {
361                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
362                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
363                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
364                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
365                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
366                        ldap_bind($ldap_conn,$dn,$passwd);
367                       
368                        if ($type == 'accounts')
369                        {
370                                $justthese = array("uidnumber", "uid", "cn", "mail");
371                                $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)(sn=*".$query."*)(cn=*".$query."*)(givenName=*".$query."*)(mail=$query*)(mailAlternateAddress=$query*)))";
372
373                                $tmp = array();
374                                foreach ($contexts as $index=>$context)
375                                {
376                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
377                                        $info = ldap_get_entries($ldap_conn, $search);
378                                       
379                                        for ($i=0; $i < $info['count']; $i++)
380                                        {
381                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
382                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
383                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
384                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
385                                                $sort[] = $info[$i]['uid'][0];
386                                        }
387                                }
388                                ldap_close($ldap_conn);
389                               
390                                if (count($sort))
391                                {
392                                        natcasesort($sort);
393                                        foreach ($sort as $user_uid)
394                                                $return[$user_uid] = $tmp[$user_uid];
395                                }
396                               
397                                return $return;
398                        }
399                        elseif($type == 'groups')
400                        {
401                                $filter="(&(phpgwAccountType=g)(cn=*$query*))";
402                                $justthese = array("gidnumber", "cn", "description");
403                               
404                                $tmp = array();
405                                foreach ($contexts as $index=>$context)
406                                {
407                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
408                                        $info = ldap_get_entries($ldap_conn, $search);
409                                        for ($i=0; $i < $info['count']; $i++)
410                                        {
411                                                $tmp[$info[$i]['cn'][0]]['cn']= $info[$i]['cn'][0];
412                                                $tmp[$info[$i]['cn'][0]]['description']= $info[$i]['description'][0];
413                                                $tmp[$info[$i]['cn'][0]]['gidnumber']= $info[$i]['gidnumber'][0];
414                                                $sort[] = $info[$i]['cn'][0];
415                                        }
416                                }
417                                ldap_close($ldap_conn);
418                               
419                                natcasesort($sort);
420                                foreach ($sort as $group_cn)
421                                        $return[$group_cn] = $tmp[$group_cn];
422                               
423                                return $return;
424                        }
425                        elseif($type == 'maillists')
426                        {
427                                $filter="(&(phpgwAccountType=l)(|(cn=*".$query."*)(uid=*".$query."*)(mail=*".$query."*)))";
428                                $justthese = array("uidnumber", "cn", "uid", "mail");
429
430                                $tmp = array();
431                                foreach ($contexts as $index=>$context)
432                                {
433                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
434                                        $info = ldap_get_entries($ldap_conn, $search);
435                                       
436                                        for ($i=0; $i < $info['count']; $i++)
437                                        {
438                                                $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
439                                                $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
440                                                $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
441                                                $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
442                                                $sort[] = $info[$i]['uid'][0];
443                                        }
444                                }
445                                ldap_close($ldap_conn);
446                               
447                                natcasesort($sort);
448                                foreach ($sort as $maillist_uid)
449                                        $return[$maillist_uid] = $tmp[$maillist_uid];
450                               
451                                return $return;
452                        }
453                        elseif($type == 'computers')
454                        {
455                                $filter="(&(objectClass=sambaSAMAccount)(|(sambaAcctFlags=[W          ])(sambaAcctFlags=[DW         ])(sambaAcctFlags=[I          ])(sambaAcctFlags=[S          ]))(cn=*".$query."*))";
456                                $justthese = array("cn","uidNumber","description");
457
458                                $tmp = array();
459                                foreach ($contexts as $index=>$context)
460                                {
461                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
462                                        $info = ldap_get_entries($ldap_conn, $search);
463                                        for ($i=0; $i < $info['count']; $i++)
464                                        {
465                                                $tmp[$info[$i]['cn'][0]]['cn']                  = $info[$i]['cn'][0];
466                                                $tmp[$info[$i]['cn'][0]]['uidNumber']   = $info[$i]['uidnumber'][0];
467                                                $tmp[$info[$i]['cn'][0]]['description'] = utf8_decode($info[$i]['description'][0]);
468                                                $sort[] = $info[$i]['cn'][0];
469                                        }
470
471                                }
472
473                                ldap_close($ldap_conn);
474                               
475                                if (!empty($sort))
476                                {
477                                        natcasesort($sort);
478                                        foreach ($sort as $computer_cn)
479                                                $return[$computer_cn] = $tmp[$computer_cn];
480                                }
481                               
482                                return $return;
483                        }
484                }
485
486                function get_organizations($context, $selected='', $referral=false, $show_invisible_ou=true, $master=false)
487                {
488                        $s = CreateObject('phpgwapi.sector_search_ldap');
489                        $sectors_info = $s->get_organizations($context, $selected, $referral, $show_invisible_ou, $master);
490                        return $sectors_info;
491                }               
492                               
493                function get_sectors($selected='', $referral=false, $show_invisible_ou=true)
494                {
495                        $s = CreateObject('phpgwapi.sector_search_ldap');
496                        $sectors_info = $s->get_sectors($selected, $referral, $show_invisible_ou);
497                        return $sectors_info;
498                }               
499 
500                // Get list of levels (0).
501                function get_groups_list($contexts,$filtro)
502                {
503                        $a_sectors = array();
504                       
505                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
506                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
507                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
508                       
509                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
510                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
511                        ldap_bind($ldap_conn,$dn,$passwd);
512                       
513                        $justthese = array("dn");
514                        $filter = "(ou=".$filtro.")";
515                       
516                        $systemName = strtolower($GLOBALS['phpgw_info']['server']['system_name']);
517                        if ($systemName != '')
518                                $filter = "(&$filter(phpgwSystem=$systemName))";
519                       
520                        foreach ($contexts as $context)
521                        {
522                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
523                        $info = ldap_get_entries($ldap_conn, $search);
524
525                        for ($i=0; $i<$info["count"]; $i++)
526                    {
527                        $a_sectors[] = $info[$i]['dn'];
528                    }
529                        }
530               
531                        ldap_close($ldap_conn);
532
533                        // Retiro o count do array info e inverto o array para ordenação.
534                foreach ($a_sectors as $context)
535            {
536                                $array_dn = ldap_explode_dn ( $context, 1 );
537
538                $array_dn_reverse  = array_reverse ( $array_dn, true );
539
540                                // Retirar o indice count do array.
541                                array_pop ( $array_dn_reverse );
542
543                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
544                        }
545
546                        // Ordenação
547                        natcasesort($inverted_dn);
548                       
549                        // Construção do select
550                        $level = 0;
551                        $options = array();
552                        foreach ($inverted_dn as $dn=>$invert_ufn)
553                        {
554                $display = '';
555
556                $array_dn_reverse = explode ( "#", $invert_ufn );
557                $array_dn  = array_reverse ( $array_dn_reverse, true );
558                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
559
560                if ($level == 0)
561                                {
562                    $display.= ' ';
563                        reset ( $array_dn );
564                    $display .= ' ' . (current ( $array_dn ) );
565                                        $dn = trim(strtolower($dn));
566                                        $options[$dn] = $display;
567                }
568                }
569            return $options;
570                }
571               
572                // Get list of levels (0), value DN
573                function get_groups_list_dn($contexts,$filtro)
574                {
575                        $a_sectors = array();
576                       
577                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
578                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
579                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
580                       
581                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
582                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
583                        ldap_bind($ldap_conn,$dn,$passwd);
584                       
585                        $justthese = array("dn");
586                        $filter = "(ou=".$filtro.")";
587                       
588                        $systemName = strtolower($GLOBALS['phpgw_info']['server']['system_name']);
589                        if ($systemName != '')
590                                $filter = "(&$filter(phpgwSystem=$systemName))";
591                       
592                        foreach ($contexts as $context)
593                        {
594                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
595                        $info = ldap_get_entries($ldap_conn, $search);
596                        for ($i=0; $i<$info["count"]; $i++)
597                    {
598                        $a_sectors[] = $info[$i]['dn'];
599                    }
600                        }
601               
602                        ldap_close($ldap_conn);
603
604                        // Retiro o count do array info e inverto o array para ordenação.
605                foreach ($a_sectors as $context)
606            {
607                                $array_dn = ldap_explode_dn ( $context, 1 );
608
609                $array_dn_reverse  = array_reverse ( $array_dn, true );
610
611                                // Retirar o indice count do array.
612                                array_pop ( $array_dn_reverse );
613
614                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
615                        }
616
617                        // Ordenação
618                        natcasesort($inverted_dn);
619                       
620                        // Construção do select
621                        $level = 0;
622                        $options = array();
623                        foreach ($inverted_dn as $dn=>$invert_ufn)
624                        {
625                $display = '';
626
627                $array_dn_reverse = explode ( "#", $invert_ufn );
628                $array_dn  = array_reverse ( $array_dn_reverse, true );
629                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
630
631                if ($level == 0)
632                                {
633                    $display.= ' ';
634                        reset ( $array_dn );
635                    $display .= ' ' . trim(strtolower($dn));
636                                        $dn = trim(strtolower($dn));
637                                        $options[] = $display;
638                }
639                }
640            return $options;
641                }
642
643                // Get list of all levels.
644                function get_sectors_list($contexts,$contextdn)
645                {
646                        $a_sectors = array();
647                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
648                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
649                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
650                       
651                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
652                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
653                        ldap_bind($ldap_conn,$dn,$passwd);
654                       
655                        $justthese = array("ou", "dn");
656                        $filter = "(&(objectClass=organizationalUnit)(|(objectClass=organizationalUnit)))";
657                       
658                        foreach ($contexts as $context)
659                        {
660                                $search=ldap_search($ldap_conn, $contextdn, $filter, $justthese);
661                        $info = ldap_get_entries($ldap_conn, $search);
662                        for ($i=0; $i<$info["count"]; $i++)
663                    {
664                        $a_sectors[] = trim(strtoupper($info[$i]['dn']));
665                    }
666                        }
667
668                        ldap_close($ldap_conn);
669
670                        // Retiro o count do array info e inverto o array para ordenação.
671                foreach ($a_sectors as $context)
672            {
673
674                                $array_dn = ldap_explode_dn ( $context, 1 );
675
676                $array_dn_reverse  = array_reverse ( $array_dn, true );
677
678                                // Retirar o indice count do array.
679                                array_pop ( $array_dn_reverse );
680                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
681
682                        }
683
684                        // Ordenação
685                        natcasesort($inverted_dn);
686
687                        // seleciona os setores do grupo escolhido
688                        $level = 0;
689                        $options = array();
690
691                        foreach ($inverted_dn as $dn=>$invert_ufn)
692                        {
693                                $display = '';
694                                $array_dn_completo = '';
695                                $ii = 0;                       
696                               
697                                $array_dn_reverse = explode ( "#", $invert_ufn );
698                                $array_dn  = array_reverse ( $array_dn_reverse, true );
699
700                                $valorgrupo = (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])));
701                                $valorsubgrupo = (count($array_dn)-1);
702                                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
703
704                                reset ( $array_dn );
705                               
706                                if ($level > 0)
707                                {
708                                        for ($i=0; $i<$level; $i++)
709                                        {
710                                                $ii = $ii +1;
711                                                $display .= ' --';
712
713                                                if ($ii==1)
714                                                {
715                                                        $array_dn_completo .= $array_dn[$valorgrupo + $ii];                                             
716                                                }
717                                                else
718                                                {
719                                                        $array_dn_completo .= " | ".$array_dn[$valorgrupo + $ii];
720                                                }
721
722                                        }
723                                }
724                                else
725                                {
726//                                      $array_dn_completo .= (current($array_dn));                                                                                     
727                                        $array_dn_completo .= ' ';                                                                                     
728                                }
729
730                                $display .= ' '. (current($array_dn)).'#'.trim(strtolower($dn)).'#'.$array_dn_completo;
731                                $options[] = $display;
732
733                }
734            return $options;
735                }
736
737                function get_list_usersgroups_sector($query, $contexts,$sizelimit)
738                {
739                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
740                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
741                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
742                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
743                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
744                        ldap_bind($ldap_conn,$dn,$passwd);
745
746                        $filter = "(&(phpgwAccountType=g)(cn=*))";
747                        $justthese = array("gidnumber", "cn", "description");
748
749                        $tmp = array();
750                        foreach ($contexts as $index=>$context)
751                        {                               
752                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
753                                $info = ldap_get_entries($ldap_conn, $search);                         
754                               
755                                for ($i=0; $i < $info['count']; $i++)
756                                {
757                                        $tmp[$info[$i]['cn'][0]]['id']                          = $info[$i]['gidnumber'][0];
758                                        $tmp[$info[$i]['cn'][0]]['name']                        = $info[$i]['cn'][0];
759                                        $tmp[$info[$i]['cn'][0]]['description']         = $info[$i]['description'][0];
760                                        $sort[] = $info[$i]['cn'][0];
761                                }
762                        }
763                        ldap_close($ldap_conn);
764                       
765                        if (count($sort))
766                        {                       
767                                natcasesort($sort);
768                                foreach ($sort as $usersgroups_gid)
769                                        $return[$usersgroups_gid] = $tmp[$usersgroups_gid];
770                        }
771                       
772                        return $return;
773                }               
774               
775                function get_list_maillists_sector($query, $contexts,$sizelimit)
776                {
777                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
778                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
779                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
780                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
781                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
782                        ldap_bind($ldap_conn,$dn,$passwd);
783
784                        $filter="(&(phpgwAccountType=l)(|(uid=*)))";
785                        $justthese = array("uidnumber", "cn", "uid", "mail");
786
787                        $tmp = array();
788                        foreach ($contexts as $index=>$context)
789                        {
790                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
791                                $info = ldap_get_entries($ldap_conn, $search);                         
792                               
793                                for ($i=0; $i < $info['count']; $i++)
794                                {
795                                        $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
796                                        $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
797                                        $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
798                                        $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
799                                        $sort[] = $info[$i]['uid'][0];
800                                }
801                        }
802                        ldap_close($ldap_conn);
803                       
804                        if (count($sort))
805                        {                       
806                                natcasesort($sort);
807                                foreach ($sort as $maillist_uid)
808                                        $return[$maillist_uid] = $tmp[$maillist_uid];
809                        }
810                       
811                        return $return;
812                }               
813               
814                function get_list_user_sector($query, $contexts,$sizelimit)
815                {
816                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
817                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
818                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
819                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
820                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
821                        ldap_bind($ldap_conn,$dn,$passwd);
822
823                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
824                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");
825                       
826                        $tmp = array();
827               
828                        foreach ($contexts as $index=>$context)
829                        {
830
831                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
832                                $info = ldap_get_entries($ldap_conn, $search);
833
834                                for ($i=0; $i < $info['count']; $i++)
835                                {
836                                        $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
837                                        $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
838                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
839                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
840                                        $tmp[$info[$i]['uid'][0]]['account_phone']= $info[$i]['telephonenumber'][0];
841                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
842                                        $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];                                 
843                                        $sort[] = $info[$i]['uid'][0];
844                                }
845                        }
846                       
847                        ldap_close($ldap_conn);
848                               
849                        if (count($sort))
850                        {
851                                natcasesort($sort);
852                                foreach ($sort as $user_uid)
853                                        $return[$user_uid] = $tmp[$user_uid];
854                        }
855                               
856                        return $return;
857                }
858
859                function get_list_cota_sector($query, $contexts,$sizelimit)
860                {
861                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
862                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
863                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
864                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
865                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
866                        ldap_bind($ldap_conn,$dn,$passwd);
867
868                        $filter="(|(phpgwAccountType=u)(|(phpgwAccountType=s)))";
869                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");
870
871                        $tmp = array();
872
873                        foreach ($contexts as $index=>$context)
874                        {
875
876                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
877                                $info = ldap_get_entries($ldap_conn, $search);
878
879                                for ($i=0; $i < $info['count']; $i++)
880                                {
881                                        $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
882                                        $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
883                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
884                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
885                                        $tmp[$info[$i]['uid'][0]]['account_phone']= $info[$i]['telephonenumber'][0];
886                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
887                                        $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];
888                                        $sort[] = $info[$i]['uid'][0];
889                                }
890                        }
891
892                        ldap_close($ldap_conn);
893
894                        if (count($sort))
895                        {
896                                natcasesort($sort);
897                                foreach ($sort as $user_uid)
898                                        $return[$user_uid] = $tmp[$user_uid];
899                        }
900
901                        return $return;
902
903                }
904
905
906                function get_list_shareAccounts_sector($query, $contexts,$sizelimit)
907                {
908                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
909                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
910                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
911                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
912                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
913                        ldap_bind($ldap_conn,$dn,$passwd);
914
915                        $filter="(phpgwAccountType=s)";
916                        $justthese = array("uid", "cn", "mail","accountstatus");
917
918                        $tmp = array();
919
920                        foreach ($contexts as $index=>$context)
921                        {
922
923                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
924                                $info = ldap_get_entries($ldap_conn, $search);
925
926                                for ($i=0; $i < $info['count']; $i++)
927                                {
928                                        $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
929                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
930                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
931                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
932                                        $sort[] = $info[$i]['uid'][0];
933                                }
934                        }
935
936                        ldap_close($ldap_conn);
937
938                        if (count($sort))
939                        {
940                                natcasesort($sort);
941                                foreach ($sort as $user_uid)
942                                        $return[$user_uid] = $tmp[$user_uid];
943                        }
944
945                        return $return;
946                }
947
948                function get_list_institutionalAccounts_sector($query, $contexts,$sizelimit)
949                {
950                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
951                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
952                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
953                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
954                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
955                        ldap_bind($ldap_conn,$dn,$passwd);
956
957                        $filter="(phpgwAccountType=i)";
958                        $justthese = array("uid", "cn", "mail","accountstatus","mailForwardingAddress");
959
960                        $tmp = array();
961
962                        foreach ($contexts as $index=>$context)
963                        {
964
965                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
966                                $info = ldap_get_entries($ldap_conn, $search);
967
968                                for ($i=0; $i < $info['count']; $i++)
969                                {
970                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
971                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
972                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
973                                        $tmp[$info[$i]['uid'][0]]['account_mailforwardingaddress']= $info[$i]['mailforwardingaddress'];
974                                        $sort[] = $info[$i]['uid'][0];
975                                }
976                        }
977
978                        ldap_close($ldap_conn);
979
980                        if (count($sort))
981                        {
982                                natcasesort($sort);
983                                foreach ($sort as $user_uid)
984                                        $return[$user_uid] = $tmp[$user_uid];
985                        }
986
987                        return $return;
988                }
989
990                function get_count_user_sector($query, $contexts,$sizelimit)
991                {
992                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
993                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
994                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
995                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
996                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
997                        ldap_bind($ldap_conn,$dn,$passwd);                     
998                        // counting users by sector.
999                        foreach ($contexts as $index=>$context) {
1000                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
1001                                        $contexts[$index] = null;
1002                                        $justthese = array("dn");
1003                                        $filter="(objectClass=OrganizationalUnit)";
1004                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
1005                                        $entries = ldap_get_entries($ldap_conn, $search);
1006                                        $contexts = array();
1007                                        }
1008                                }
1009
1010                        $filter="(phpgwAccountType=u)";
1011                        $justthese = array("dn");
1012                        $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1013                        $entries = ldap_get_entries($ldap_conn, $search);
1014                        $total_count = $entries["count"];
1015
1016                        ldap_close($ldap_conn);
1017
1018                        return $total_count;
1019                        }       
1020                       
1021                function get_count_cota_sector($query, $contexts,$sizelimit)
1022                {
1023                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1024                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1025                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1026                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1027                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1028                        ldap_bind($ldap_conn,$dn,$passwd);
1029                        // counting users by sector.
1030                        foreach ($contexts as $index=>$context) {                               
1031                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
1032                                        $contexts[$index] = null;
1033                                        $justthese = array("dn");
1034                                        $filter="(objectClass=OrganizationalUnit)";
1035                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
1036                                        $entries = ldap_get_entries($ldap_conn, $search);
1037                                        $contexts = array();
1038                                }
1039                        }
1040
1041                        $filter="(|(phpgwAccountType=u)(|(phpgwAccountType=s)))";
1042                        $justthese = array("dn");
1043                                $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1044                        $entries = ldap_get_entries($ldap_conn, $search);
1045                        $total_count = $entries["count"];
1046
1047                        ldap_close($ldap_conn);
1048
1049                        return $total_count;
1050                        }
1051
1052                function get_count_shareAccount_sector($query, $contexts,$sizelimit)
1053                {
1054                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1055                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1056                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1057                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1058                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1059                        ldap_bind($ldap_conn,$dn,$passwd);
1060                        // counting users by sector.
1061                        foreach ($contexts as $index=>$context) {
1062                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
1063                                        $contexts[$index] = null;
1064                                        $justthese = array("dn");
1065                                        $filter="(objectClass=OrganizationalUnit)";
1066                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
1067                                        $entries = ldap_get_entries($ldap_conn, $search);
1068                                        $contexts = array();
1069                                }
1070                        }
1071
1072                        $filter="(phpgwAccountType=s)";
1073                        $justthese = array("dn");
1074                        $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1075                        $entries = ldap_get_entries($ldap_conn, $search);
1076                        $total_count = $entries["count"];
1077
1078                        ldap_close($ldap_conn);
1079                       
1080                        return $total_count;
1081                }
1082
1083                function get_count_institutionalAccount_sector($query, $contexts,$sizelimit)
1084                {
1085                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1086                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1087                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1088                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1089                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1090                        ldap_bind($ldap_conn,$dn,$passwd);
1091                        // counting users by sector.
1092                        foreach ($contexts as $index=>$context) {
1093                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
1094                                        $contexts[$index] = null;
1095                                        $justthese = array("dn");
1096                                        $filter="(objectClass=OrganizationalUnit)";
1097                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
1098                                        $entries = ldap_get_entries($ldap_conn, $search);
1099                                        $contexts = array();
1100                                }
1101                        }
1102                       
1103                        $filter="(phpgwAccountType=i)";
1104                        $justthese = array("dn");
1105                        $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1106                        $entries = ldap_get_entries($ldap_conn, $search);
1107                        $total_count = $entries["count"];
1108
1109                        ldap_close($ldap_conn);
1110
1111                        return $total_count;
1112                }
1113
1114                function get_list_user_sector_logon($query, $contexts,$sizelimit,$numacesso)
1115                {
1116                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1117                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1118                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1119                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1120                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1121                        ldap_bind($ldap_conn,$dn,$passwd);
1122
1123                        $filter="(phpgwAccountType=u)";
1124                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");
1125                       
1126                        $tmp = array();
1127               
1128                        foreach ($contexts as $index=>$context)
1129                        {
1130
1131                                $field = 'cn';
1132                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
1133                                $info = ldap_get_entries($ldap_conn, $search);
1134
1135                                for ($i=0; $i < $info['count']; $i++)
1136                                {
1137                                        $access_log =  $this->show_access_log($info[$i]['uidnumber'][0]);
1138
1139                                        $access_log_array = explode("#",$access_log);
1140
1141                                        if ($access_log_array[1] >= $numacesso or $numacesso == $access_log_array[0]) {
1142
1143                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
1144                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
1145                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
1146                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
1147                                                $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
1148                                                $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];                                 
1149                                                $sort[] = $info[$i]['uid'][0];
1150                                        }
1151                                }
1152                        }
1153
1154                        ldap_close($ldap_conn);
1155                               
1156                        if (count($sort))
1157                        {
1158                                natcasesort($sort);
1159                                foreach ($sort as $user_uid)
1160                                        $return[$user_uid] = $tmp[$user_uid];
1161                        }
1162                               
1163                        return $return;
1164                }
1165               
1166                function exist_account_lid($account_lid)
1167                {
1168                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
1169                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "uid=" . $account_lid);
1170                        $result = ldap_get_entries($conection, $sri);
1171                        return $result['count'];
1172                }
1173               
1174                function exist_email($mail)
1175                {
1176                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
1177                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "mail=" . $mail);
1178                        $result = ldap_get_entries($conection, $sri);
1179                        ldap_close($conection);
1180                       
1181                        if ($result['count'] == 0)
1182                                return false;
1183                        else
1184                                return true;
1185                }
1186               
1187                function array_invert($array)
1188                {
1189                        $result[] = end($array);
1190                        while ($item = prev($array))
1191                                $result[] = $item;
1192                        return $result;
1193                }
1194
1195                function make_list_app($account_lid, $user_applications='', $disabled='')
1196                {
1197                        // create list of ALL available apps
1198                        $availableAppsGLOBALS = $GLOBALS['phpgw_info']['apps'];
1199                       
1200                        // create list of available apps for the user
1201                        $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."'";
1202                        $GLOBALS['phpgw']->db->query($query);
1203                        while($GLOBALS['phpgw']->db->next_record())
1204                        {
1205                                $availableApps[] = $GLOBALS['phpgw']->db->row();
1206                        }
1207                       
1208                        // Retira alguns modulos
1209                        if (count($availableApps))
1210                        {
1211                                foreach ($availableApps as $key => $value)
1212                                {
1213                                        if ($value['app'] != 'phpgwapi')
1214                                                $tmp[] = $availableApps[$key];
1215                                }
1216                        }
1217                        $availableApps = $tmp;
1218                       
1219                        // Cria um array com as aplicacoes disponiveis para o manager, com as atributos das aplicacoes.
1220                        $availableAppsUser = array();
1221                        if (count($availableApps))
1222                        {
1223                                foreach($availableApps as $app => $title)
1224                                {
1225                                        if ($availableAppsGLOBALS[$title['app']])
1226                                                $availableAppsUser[$title['app']] = $availableAppsGLOBALS[$title['app']];
1227                                }
1228                        }
1229                       
1230                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
1231                        $applications_list = '';
1232                        $app_col1 = '';
1233                        $app_col2 = '';
1234                        $app_col3 = '';
1235                        $total_apps = count($availableAppsUser);
1236                        $i = 0;
1237                        foreach($availableAppsUser as $app => $data)
1238                        {
1239                                // 1 coluna
1240                                if (($i +1) % 3 == 1)
1241                                {
1242                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1243                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1244                                        $data['title'],$app,$checked, $disabled);
1245                                        if ($i == ($total_apps-1))
1246                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>','#DDDDDD', $app_col1);
1247                                }
1248                               
1249                                // 2 coluna
1250                                if (($i +1) % 3 == 2)
1251                                {
1252                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1253                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1254                                        $data['title'],$app,$checked, $disabled);
1255                                       
1256                                        if ($i == ($total_apps-1))
1257                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>','#DDDDDD', $app_col1,$app_col2);
1258                                }
1259                                // 3 coluna
1260                                if (($i +1) % 3 == 0)
1261                                {
1262                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1263                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1264                                        $data['title'],$app,$checked, $disabled);
1265                                       
1266                                        // Cria nova linha
1267                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>','#DDDDDD', $app_col1, $app_col2, $app_col3);                                       
1268                                }
1269                                $i++;
1270                        }
1271                        return $applications_list;
1272                }
1273               
1274                function exist_attribute_in_ldap($dn, $attribute, $value)
1275                {
1276                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
1277                        $search = ldap_search($connection, $dn, $attribute. "=" . $value);
1278                        $result = ldap_get_entries($connection, $search);
1279                        ldap_close($connection);
1280                        //_debug_array($result);
1281                        if ($result['count'] == 0)
1282                                return false;
1283                        else
1284                                return true;   
1285                }
1286               
1287                function lang($key)
1288                {
1289                        if ($_SESSION['phpgw_info']['expressoAdmin']['lang'][$key])
1290                                return $_SESSION['phpgw_info']['expressoAdmin']['lang'][$key];
1291                        else
1292                                return $key . '*';
1293                }
1294               
1295                function make_lang($ram_lang)
1296                {
1297                        $a_lang = split("_", $ram_lang);
1298                        $a_lang_reverse  = array_reverse ( $a_lang, true );
1299                        array_pop ( $a_lang_reverse );
1300                        $a_lang  = array_reverse ( $a_lang_reverse, true );
1301                        $a_new_lang = implode ( " ", $a_lang );
1302                        return lang($a_new_lang);
1303                }
1304
1305                function make_dinamic_lang($template_obj, $block)
1306                {
1307                        $tpl_vars = $template_obj->get_undefined($block);
1308                        $array_langs = array();
1309                       
1310                        foreach ($tpl_vars as $atribute)
1311                        {
1312                                $lang = strstr($atribute, 'lang_');
1313                                if($lang !== false)
1314                                {
1315                                        //$template_obj->set_var($atribute, $this->make_lang($atribute));
1316                                        $array_langs[$atribute] = $this->make_lang($atribute);
1317                                }
1318                        }
1319                        return $array_langs;
1320                }
1321
1322                function paginate_usersgroups($query, $contexts, $field, $order = 'asc', $page = null, $perPage = null )
1323                {
1324                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1325                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1326                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1327                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1328                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1329                        ldap_bind($ldap_conn,$dn,$passwd);
1330                       
1331                        $filter="(&(phpgwAccountType=g)(cn=*))";
1332                        $justthese = array("gidnumber", "cn", "description");                                                                           
1333                                                       
1334                        foreach ($contexts as $index=>$context)
1335                        {                               
1336                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1337       
1338                                $rConnection = $ldap_conn;
1339                                $rSearch = $search;
1340                                $sOrder = $order;
1341                                $iPage = $page;
1342                                $iPerPage = $perPage;
1343                                $sField = $field;
1344                                               
1345                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1346                               
1347                                if ( $iPage === null || $iPerPage === null )
1348                                {
1349                                        # fetch all in one page
1350                                        $iStart = 0;
1351                                        $iEnd = $iTotalEntries - 1;
1352                                }
1353                                else
1354                                {
1355                                        # calculate range of page
1356                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1357                                       
1358                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1359                                        $iEnd = $iPage * $iPerPage;
1360
1361                                        if ( $sOrder === "desc" )
1362                                        {
1363                                                # revert range
1364                                                $iStart = $iTotalEntries - 1 - $iEnd;
1365                                                $iEnd = $iStart + $iPerPage - 1;
1366                                        }
1367                                }
1368                               
1369                                /********* Importante Mostra o resultado da paginação **********
1370                                var_dump( $iStart . " " . $iEnd );
1371                                ****************** Só descomentar ******************************/
1372                               
1373                                 # fetch entries
1374                            ldap_sort( $rConnection, $rSearch, $sField );
1375
1376                                $aList = array();
1377                                for (
1378                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1379                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1380                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1381                                        )
1382                                {
1383                                        if ( $iCurrent >= $iStart )
1384                                        {
1385                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1386                                        }
1387                                }
1388                        }
1389
1390                        ldap_close($ldap_conn);
1391
1392                        # if order is desc revert page's entries
1393                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1394                }               
1395               
1396                function paginate_maillists($query, $contexts, $field, $order = 'asc', $page = null, $perPage = null )
1397                {
1398                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1399                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1400                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1401                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1402                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1403                        ldap_bind($ldap_conn,$dn,$passwd);
1404                       
1405                        $filter="(&(phpgwAccountType=l)(|(uid=*)))";
1406                        $justthese = array("uidnumber", "cn", "uid", "mail");                                                                                   
1407                                                       
1408                        foreach ($contexts as $index=>$context)
1409                        {
1410                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1411       
1412                                $rConnection = $ldap_conn;
1413                                $rSearch = $search;
1414                                $sOrder = $order;
1415                                $iPage = $page;
1416                                $iPerPage = $perPage;
1417                                $sField = $field;
1418                                               
1419                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1420                               
1421                                if ( $iPage === null || $iPerPage === null )
1422                                {
1423                                        # fetch all in one page
1424                                        $iStart = 0;
1425                                        $iEnd = $iTotalEntries - 1;
1426                                }
1427                                else
1428                                {
1429                                        # calculate range of page
1430                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1431                                       
1432                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1433                                        $iEnd = $iPage * $iPerPage;
1434
1435                                        if ( $sOrder === "desc" )
1436                                        {
1437                                                # revert range
1438                                                $iStart = $iTotalEntries - 1 - $iEnd;
1439                                                $iEnd = $iStart + $iPerPage - 1;
1440                                        }
1441                                }
1442                               
1443                                /********* Importante Mostra o resultado da paginação **********
1444                                var_dump( $iStart . " " . $iEnd );
1445                                ****************** Só descomentar ******************************/
1446                               
1447                                 # fetch entries
1448                            ldap_sort( $rConnection, $rSearch, $sField );
1449
1450                                $aList = array();
1451                                for (
1452                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1453                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1454                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1455                                        )
1456                                {
1457                                        if ( $iCurrent >= $iStart )
1458                                        {
1459                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1460                                        }
1461                                }
1462                        }
1463
1464                        ldap_close($ldap_conn);
1465
1466                        # if order is desc revert page's entries
1467                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1468                }               
1469               
1470                function Paginate_user($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
1471
1472                {
1473                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1474                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1475                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1476                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1477                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1478                        ldap_bind($ldap_conn,$dn,$passwd);
1479                       
1480                        $filter="(&(phpgwAccountType=u))";
1481                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");                                                                                 
1482                                                       
1483                        foreach ($contexts as $index=>$context)
1484                        {
1485                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1486       
1487                                $rConnection = $ldap_conn;
1488                                $rSearch = $search;
1489                                $sOrder = $Order;
1490                                $iPage = $Page;
1491                                $iPerPage = $PerPage;
1492                                $sField = $Field;
1493                                               
1494                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1495
1496                                if ( $iPage === null || $iPerPage === null )
1497                                {
1498                                        # fetch all in one page
1499                                        $iStart = 0;
1500                                        $iEnd = $iTotalEntries - 1;
1501                                }
1502                                else
1503                                {
1504                                        # calculate range of page
1505                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1506                                       
1507                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1508                                        $iEnd = $iPage * $iPerPage;
1509
1510                                        if ( $sOrder === "desc" )
1511                                        {
1512                                                # revert range
1513                                                $iStart = $iTotalEntries - 1 - $iEnd;
1514                                                $iEnd = $iStart + $iPerPage - 1;
1515                                        }
1516                                }
1517                               
1518                                /********* Importante Mostra o resultado da paginação **********
1519                                var_dump( $iStart . " " . $iEnd );
1520                                ****************** Só descomentar ******************************/
1521                               
1522                                 # fetch entries
1523                            ldap_sort( $rConnection, $rSearch, $sField );
1524
1525                                $aList = array();
1526                                for (
1527                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1528                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1529                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1530                                        )
1531                                {
1532                                        if ( $iCurrent >= $iStart )
1533                                        {
1534                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1535                                        }
1536                                }
1537                        }
1538
1539                        ldap_close($ldap_conn);
1540
1541                        # if order is desc revert page's entries
1542                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1543                }
1544
1545                function Paginate_cota($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
1546                {
1547                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1548                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1549                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1550                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1551                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1552                        ldap_bind($ldap_conn,$dn,$passwd);
1553
1554                        $filter="(|(phpgwAccountType=u)(|(phpgwAccountType=s)))";
1555                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");                                 
1556
1557                        foreach ($contexts as $index=>$context)
1558                        {
1559                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1560
1561                                $rConnection = $ldap_conn;
1562                                $rSearch = $search;
1563                                $sOrder = $Order;
1564                                $iPage = $Page;
1565                                $iPerPage = $PerPage;
1566                                $sField = $Field;
1567
1568                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1569
1570                                if ( $iPage === null || $iPerPage === null )
1571                                {
1572                                        # fetch all in one page
1573                                        $iStart = 0;
1574                                        $iEnd = $iTotalEntries - 1;
1575                                }
1576                                else
1577                                {
1578                                        # calculate range of page
1579                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1580
1581                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1582                                        $iEnd = $iPage * $iPerPage;
1583
1584
1585                                        if ( $sOrder === "desc" )
1586                                        {
1587                                                # revert range
1588                                                $iStart = $iTotalEntries - 1 - $iEnd;
1589                                                $iEnd = $iStart + $iPerPage - 1;
1590                                        }
1591                                }
1592
1593                                /********* Importante Mostra o resultado da paginação **********
1594                                var_dump( $iStart . " " . $iEnd );
1595                                ****************** Só descomentar ******************************/
1596
1597                                 # fetch entries
1598                                ldap_sort( $rConnection, $rSearch, $sField );
1599
1600                                $aList = array();
1601                                for (
1602                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1603                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1604                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1605                                        )
1606                                {
1607                                        if ( $iCurrent >= $iStart )
1608                                        {
1609                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1610                                        }
1611                                }
1612                        }
1613
1614                        ldap_close($ldap_conn);
1615
1616                        # if order is desc revert page's entries
1617                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1618                }
1619
1620                function Paginate_shareAccount($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
1621
1622                {
1623                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1624                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1625                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1626                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1627                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1628                        ldap_bind($ldap_conn,$dn,$passwd);
1629
1630                        $filter="(phpgwAccountType=s)";
1631                        $justthese = array("uid", "cn", "mail","accountstatus");                                 
1632
1633                        foreach ($contexts as $index=>$context)
1634                        {
1635                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1636
1637                                $rConnection = $ldap_conn;
1638                                $rSearch = $search;
1639                                $sOrder = $Order;
1640                                $iPage = $Page;
1641                                $iPerPage = $PerPage;
1642                                $sField = $Field;
1643
1644                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1645
1646                                if ( $iPage === null || $iPerPage === null )
1647                                {
1648                                        # fetch all in one page
1649                                        $iStart = 0;
1650                                        $iEnd = $iTotalEntries - 1;
1651                                }
1652                                else
1653                                {
1654                                        # calculate range of page
1655                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1656
1657                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1658                                        $iEnd = $iPage * $iPerPage;
1659
1660                                        if ( $sOrder === "desc" )
1661                                        {
1662                                                # revert range
1663                                                $iStart = $iTotalEntries - 1 - $iEnd;
1664                                                $iEnd = $iStart + $iPerPage - 1;
1665                                        }
1666                                }
1667
1668                                 # fetch entries
1669                                ldap_sort( $rConnection, $rSearch, $sField );
1670
1671                                $aList = array();
1672                                for (
1673                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1674                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1675                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1676                                        )
1677                                {
1678                                        if ( $iCurrent >= $iStart )
1679                                        {
1680                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1681                                        }
1682                                }
1683                        }
1684
1685                        ldap_close($ldap_conn);
1686
1687                        # if order is desc revert page's entries
1688                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1689                }
1690
1691                function Paginate_institutionalAccount($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
1692                {
1693                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1694                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1695                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1696                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1697                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1698                        ldap_bind($ldap_conn,$dn,$passwd);
1699
1700                        $filter="(phpgwAccountType=i)";
1701                        $justthese = array("uid", "cn", "mail","accountstatus","mailforwardingaddress");
1702
1703                        foreach ($contexts as $index=>$context)
1704                        {
1705                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1706
1707                                $rConnection = $ldap_conn;
1708                                $rSearch = $search;
1709                                $sOrder = $Order;
1710                                $iPage = $Page;
1711                                $iPerPage = $PerPage;
1712                                $sField = $Field;
1713
1714                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1715
1716                                if ( $iPage === null || $iPerPage === null )
1717                                {
1718                                        # fetch all in one page
1719                                        $iStart = 0;
1720                                        $iEnd = $iTotalEntries - 1;
1721                                }
1722                                else
1723                                {
1724                                        # calculate range of page
1725                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1726
1727                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1728                                        $iEnd = $iPage * $iPerPage;
1729
1730                                        if ( $sOrder === "desc" )
1731                                        {
1732                                                # revert range
1733                                                $iStart = $iTotalEntries - 1 - $iEnd;
1734                                                $iEnd = $iStart + $iPerPage - 1;
1735                                        }
1736                                }
1737
1738                                 # fetch entries
1739                                ldap_sort( $rConnection, $rSearch, $sField );
1740
1741                                $aList = array();
1742                                for (
1743                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1744                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1745                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1746                                        )
1747                                {
1748                                        if ( $iCurrent >= $iStart )
1749                                        {
1750                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1751                                        }
1752                                }
1753                        }
1754
1755                        ldap_close($ldap_conn);
1756
1757                        # if order is desc revert page's entries
1758                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1759                }
1760
1761
1762                function get_list_ou_user_logon($query,$contexts,$sizelimit)
1763                {
1764                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1765                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1766                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1767                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1768                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1769                        ldap_bind($ldap_conn,$dn,$passwd);
1770
1771                        $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)))";
1772                        $justthese = array("ou");                                                                                       
1773                        $tmp = array();
1774               
1775                        foreach ($contexts as $index=>$context)
1776                        {
1777
1778                                $field = 'cn';
1779                                $search=ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1780                                $info = ldap_get_entries($ldap_conn, $search);
1781
1782                                for ($i=0; $i < $info['count']; $i++)
1783                                {
1784                                        $a_sectors[] = $info[$i]['dn'];
1785                                }
1786                        }
1787                       
1788                        ldap_close($ldap_conn);
1789                               
1790                        // Retiro o count do array info e inverto o array para ordenação.
1791                foreach ($a_sectors as $context)
1792            {
1793                                $array_dn = ldap_explode_dn ( $context, 1 );
1794
1795                $array_dn_reverse  = array_reverse ( $array_dn, true );
1796
1797                                // Retirar o indice count do array.
1798                                array_pop ( $array_dn_reverse );
1799
1800                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
1801                        }
1802
1803                        // Ordenação
1804                        natcasesort($inverted_dn);
1805
1806                        // Construção do select
1807                        $level = 0;
1808                        $options = array();
1809                        foreach ($inverted_dn as $dn=>$invert_ufn)
1810                        {
1811                $display = '';
1812
1813                $array_dn_reverse = explode ( "#", $invert_ufn );
1814                $array_dn  = array_reverse ( $array_dn_reverse, true );
1815
1816                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
1817
1818                                $display.= ' ';
1819                                reset ( $array_dn );
1820                                $display .= ' ' . ($array_dn[$level]);
1821                                $dn = trim(strtolower($dn));
1822                                $options[$dn] = $display;
1823                }
1824
1825            return $options;
1826                }
1827
1828                function get_list_context_logon($query_user,$contexts,$sizelimit)
1829                {
1830                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1831                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1832                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1833                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1834                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1835                        ldap_bind($ldap_conn,$dn,$passwd);
1836
1837                        // Le BD para pegar os administradors.
1838                        $query = "SELECT manager_lid,context FROM phpgw_expressoadmin WHERE manager_lid='".$query_user."' ORDER by manager_lid";
1839                        $GLOBALS['phpgw']->db->query($query);
1840                        while($GLOBALS['phpgw']->db->next_record())
1841                        {
1842                                $managers[] = $GLOBALS['phpgw']->db->row();
1843                        }
1844
1845                        // Loop para listar as dn
1846                        if (count($managers))
1847                        {
1848                                foreach($managers as $array_managers)
1849                                {
1850                                        $display = '';
1851                                        $managers_context = "";
1852                                        $a_managers_context = explode("%", $array_managers['context']);
1853
1854                                        // Ordenação
1855                                        natcasesort($a_managers_context);
1856
1857                                        $options = array();
1858                                        $level = 0;
1859                                                                                               
1860                                        foreach ($a_managers_context as $dn=>$invert_ufn)
1861                                        {
1862                                                $dn_explode = explode (",",$invert_ufn);
1863
1864                                                // Construção do select
1865                                                $array_dn  = array_reverse ( $dn_explode, true );
1866
1867                                                $level = count( $dn_explode ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
1868                                                $display = ' ';
1869                                                reset ( $array_dn );
1870//                                              $display = str_replace("ou=", "",($array_dn[$level]));
1871                                                $display = str_replace("ou=", "",($array_dn[0]));
1872                                                $display = str_replace("dc=", "",($display));
1873                                                $dn = trim(strtolower($dn));
1874                                                $options[$dn] = trim(strtoupper($display));
1875                                        }
1876                                }
1877                        }
1878
1879                return $options;
1880                }
1881
1882                function get_list_groups_dn($query_user,$contexts,$sizelimit)
1883                {
1884                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1885                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1886                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1887                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1888                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1889                        ldap_bind($ldap_conn,$dn,$passwd);
1890
1891                        // Le BD para pegar os administradors.
1892                        $query = "SELECT manager_lid,context FROM phpgw_expressoadmin WHERE manager_lid='".$query_user."' ORDER by manager_lid";
1893                        $GLOBALS['phpgw']->db->query($query);
1894                        while($GLOBALS['phpgw']->db->next_record())
1895                        {
1896                                $managers[] = $GLOBALS['phpgw']->db->row();
1897                        }
1898
1899                        // Loop para listar as dn
1900                        if (count($managers))
1901                        {
1902                                foreach($managers as $array_managers)
1903                                {
1904                                        $display = '';
1905                                        $managers_context = "";
1906                                        $options = explode("%", $array_managers['context']);
1907
1908                                        // Ordenação
1909                                        natcasesort($options);
1910                                }
1911                        }
1912
1913                return $options;
1914                }
1915
1916                function sort_by($field, &$arr, $sorting=SORT_ASC, $case_insensitive=true){
1917                        if(is_array($arr) && (count($arr)>0) && ( ( is_array($arr[0]) && isset($arr[0][$field]) ) || ( is_object($arr[0]) && isset($arr[0]->$field) ) ) ){
1918                                if($case_insensitive==true) $strcmp_fn = "strnatcasecmp";
1919                                else $strcmp_fn = "strnatcmp";
1920               
1921                                if($sorting==SORT_ASC){
1922                                        $fn = create_function('$a,$b', '
1923                                                if(is_object($a) && is_object($b)){
1924                                                        return '.$strcmp_fn.'($a->'.$field.', $b->'.$field.');
1925                                                }else if(is_array($a) && is_array($b)){
1926                                                        return '.$strcmp_fn.'($a["'.$field.'"], $b["'.$field.'"]);
1927                                                }else return 0;
1928                                        ');
1929                                }else{
1930                                        $fn = create_function('$a,$b', '
1931                                                if(is_object($a) && is_object($b)){
1932                                                        return '.$strcmp_fn.'($b->'.$field.', $a->'.$field.');
1933                                                }else if(is_array($a) && is_array($b)){
1934                                                        return '.$strcmp_fn.'($b["'.$field.'"], $a["'.$field.'"]);
1935                                                }else return 0;
1936                                        ');
1937                                }
1938                                usort($arr, $fn);
1939                                return true;
1940                        }else{
1941                                return false;
1942                        }
1943                }       
1944
1945                function show_access_log($account_id)
1946                {       
1947                        $manager_account_lid = $GLOBALS['phpgw']->accounts->data['account_lid'];
1948                        $tmp = $this->read_acl($manager_account_lid);
1949                        $manager_context = $tmp[0]['context'];
1950                       
1951                        // Verifica se tem acesso a este modulo
1952                        if ((!$this->check_acl($manager_account_lid,'edit_users')) && (!$this->check_acl($manager_account_lid,'change_users_password')))
1953                        {
1954                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/reports/inc/access_denied.php'));
1955                        }
1956
1957                        // Le BD para pegar os [li].
1958                        $query = "select li from phpgw_access_log WHERE account_id=".$account_id." order by li desc LIMIT 1 OFFSET 0";
1959                        $GLOBALS['phpgw']->db->query($query);
1960                        while($GLOBALS['phpgw']->db->next_record())
1961                        {
1962                                $managers[] = $GLOBALS['phpgw']->db->row();
1963                        }
1964
1965                        if (count($managers))
1966                        {
1967                                // contar intervalo
1968                                 $data_atual = date("Y/m/d", time());
1969
1970                                 $data_antes = date("Y/m/d",$managers['0']['li']);
1971                               
1972                                 $datainicio=strtotime("$data_antes"); // Data de Hoje
1973                                 $datafim =strtotime("$data_atual"); // Data no próximo ano
1974                               
1975                                 $rdata =($datafim-$datainicio)/86400; //transformação do timestamp em dias
1976
1977                                $access_li = date("d/m/Y",$managers['0']['li'])."#".$rdata;
1978
1979                                return $access_li;
1980
1981                        }else{
1982
1983                                $access_li = "Nunca logou#0";
1984
1985                                return $access_li;
1986                        }
1987                }
1988
1989                function Paginate_user_logon($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null, $numacesso)
1990                {
1991                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1992                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1993                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1994                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1995                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1996                        ldap_bind($ldap_conn,$dn,$passwd);
1997                       
1998                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
1999                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");                                                                                   
2000                                                       
2001                        foreach ($contexts as $index=>$context)
2002                        {
2003                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
2004       
2005                                $rConnection = $ldap_conn;
2006                                $rSearch = $search;
2007                                $sOrder = $Order;
2008                                $iPage = $Page;
2009                                $iPerPage = $PerPage;
2010                                $sField = $Field;
2011                                               
2012                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
2013
2014                                if ( $iPage === null || $iPerPage === null )
2015                                {
2016                                        # fetch all in one page
2017                                        $iStart = 0;
2018                                        $iEnd = $iTotalEntries - 1;
2019                                }
2020                                else
2021                                {
2022                                        # calculate range of page
2023                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
2024                                       
2025                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
2026                                        $iEnd = $iPage * $iPerPage;
2027
2028                                        if ( $sOrder === "desc" )
2029                                        {
2030                                                # revert range
2031                                                $iStart = $iTotalEntries - 1 - $iEnd;
2032                                                $iEnd = $iStart + $iPerPage - 1;
2033                                        }
2034                                }
2035                               
2036                                /********* Importante Mostra o resultado da paginação **********
2037                                var_dump( $iStart . " " . $iEnd );
2038                                ****************** Só descomentar ******************************/
2039                               
2040                                 # fetch entries
2041                            ldap_sort( $rConnection, $rSearch, $sField );
2042
2043                                $aList = array();
2044                                for (
2045                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
2046                                        $iCurrent <= $iEnd && is_resource( $rEntry );
2047                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
2048                                        )
2049                                {
2050                                        if ( $iCurrent >= $iStart )
2051                                        {
2052                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
2053                                        }
2054                                }
2055                        }
2056
2057                        ldap_close($ldap_conn);
2058
2059                        # if order is desc revert page's entries
2060                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
2061                }
2062        }
2063// ****************** fim classe Functions ***********************
2064?>
Note: See TracBrowser for help on using the repository browser.