source: branches/2.2/reports/inc/class.functions.inc.php @ 3949

Revision 3949, 50.9 KB checked in by niltonneto, 13 years ago (diff)

Ticket #1692 - Removido verificação e método desnecessários.

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_user_logon'           => True,
27                        'get_list_all'                          => True,
28                        'get_groups_list'                       => True,
29                        'get_list_ou_user_logon'        => True,
30                        'show_access_log'                       => True,
31                        'get_sectors_list'                      => True
32                );
33               
34                var $nextmatchs;
35                var $sectors_list = array();
36                var $current_config;
37               
38                function functions()
39                {
40                        $this->db_functions = new db_functions;
41                        $GLOBALS['phpgw']->db = $this->db_functions->db;
42
43                        $c = new config;
44                        $c->read_repository();
45                        $this->current_config = $c->config_data;
46                }
47
48                // Account and type of access. Return: Have access ? (true/false)
49                function check_acl($account_lid, $access)
50                {
51                        $acl = $this->read_acl($account_lid);
52                        $array_acl = $this->make_array_acl($acl['acl']);
53                       
54                        switch($access)
55                        {
56                                case list_users:
57                                        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] )
58                                                return true;
59                                        break;
60                                case report_users:
61                                        if ($array_acl[acl_change_users_quote] || $array_acl[acl_view_users])
62                                                return true;
63                                        break;
64                                case list_groups:
65                                        if ($array_acl[acl_add_groups] || $array_acl[acl_edit_groups] || $array_acl[acl_delete_groups])
66                                                return true;
67                                        break;
68                                case list_maillists:
69                                        if ($array_acl[acl_add_maillists] || $array_acl[acl_edit_maillists] || $array_acl[acl_delete_maillists])
70                                                return true;
71                                        break;
72                                case list_sectors:
73//                                      if ($array_acl[acl_create_sectors] || $array_acl[acl_edit_sectors] || $array_acl[acl_delete_sectors])
74                                        if ($array_acl[acl_view_users])
75                                                return true;
76                                        break;
77                                case list_computers:
78                                        if ($array_acl[acl_create_computers] || $array_acl[acl_edit_computers] || $array_acl[acl_delete_computers])
79                                                return true;
80                                        break;
81
82                                case display_groups:
83                                        if ( $array_acl[acl_edit_users] || $array_acl[acl_view_users] || ($array_acl[acl_edit_sambausers_attributes] && ($this->current_config['expressoAdmin_samba_support'] == 'true')) )
84                                                return true;
85                                        break;
86                                case display_emailconfig:
87                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
88                                                return true;
89                                        break;
90                                case display_applications:
91                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
92                                                return true;
93                                        break;
94                                case display_emaillists:
95                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
96                                                return true;
97                                        break;
98
99                                case list_institutional_accounts:
100                                        if ($array_acl[acl_add_institutional_accounts] || $array_acl[acl_edit_institutional_accounts] || $array_acl[acl_delete_institutional_accounts])
101                                                return true;
102                                        break;
103
104
105                                default:
106                                        return $array_acl["acl_$access"];
107                        }
108                        return false;
109                }
110
111               
112                // Read acl from db
113                function read_acl($account_lid)
114                {
115                        $acl = $this->db_functions->read_acl($account_lid);
116                       
117                        $result['acl'] = $acl[0]['acl'];
118                        $result['manager_lid'] = $acl[0]['manager_lid'];
119                        $result['raw_context'] = $acl[0]['context'];
120                       
121                        $all_contexts = split("%", $acl[0]['context']);
122                        foreach ($all_contexts as $index=>$context)
123                        {
124                                $result['contexts'][] = $context;
125                                $result['contexts_display'][] = str_replace(", ", ".", ldap_dn2ufn( $context ));
126                        }
127                       
128                        return $result;
129                }
130               
131                function make_array_acl($acl)
132                {
133                        $array_acl_tmp = array();
134                        $tmp = array(           "acl_add_users",
135                                                                "acl_edit_users",
136                                                                "acl_delete_users",
137                                                                "acl_EMPTY1",
138                                                                "acl_add_groups",
139                                                                "acl_edit_groups",
140                                                                "acl_delete_groups",
141                                                                "acl_change_users_password",
142                                                                "acl_add_maillists",
143                                                                "acl_edit_maillists",
144                                                                "acl_delete_maillists",
145                                                                "acl_EMPTY2",
146                                                                "acl_create_sectors",
147                                                                "acl_edit_sectors",
148                                                                "acl_delete_sectors",
149                                                                "acl_edit_sambausers_attributes",
150                                                                "acl_view_global_sessions",
151                                                                "acl_view_logs",
152                                                                "acl_change_users_quote",
153                                                                "acl_set_user_default_password",
154                                                                "acl_create_computers",
155                                                                "acl_edit_computers",
156                                                                "acl_delete_computers",
157                                                                "acl_rename_users",
158                                                                "acl_edit_sambadomains",
159                                                                "acl_view_users",
160                                                                "acl_edit_email_groups",
161                                                                "acl_empty_user_inbox",
162                                                                "acl_manipulate_corporative_information",
163                                                                "acl_edit_users_picture",
164                                                                "acl_edit_scl_email_lists",
165                                                                "acl_edit_users_phonenumber",
166                                                                "acl_add_institutional_accounts",
167                                                                "acl_edit_institutional_accounts",
168                                                                "acl_remove_institutional_accounts"
169                                                                );
170                       
171                        foreach ($tmp as $index => $right)
172                        {
173                                $bin = '';
174                                for ($i=0; $i<$index; $i++)
175                                {
176                                        $bin .= '0';
177                                }
178                                $bin = '1' . $bin;
179                               
180                                $array_acl[$right] = $this->safeBitCheck(bindec($bin), $acl);
181                        }
182                        return $array_acl;
183                }
184               
185                function get_inactive_users($contexts)
186                {
187                        $retorno = array();
188                        $tempUsers = array();
189                        //Pego no LDAP todos os usuários dos contextos em questão.
190                        $usuariosLdap = $this->get_list('accounts','',$contexts);
191                        foreach($usuariosLdap as $usuarioLdap)
192                        {
193                                $tempUsers[$usuarioLdap["account_id"]] = $usuarioLdap["account_lid"];
194                        }
195                        $ids = implode(",",array_keys($tempUsers)); //Consigo a lista de uids daquele contexto para mandar na query para o banco.
196                       
197                        //Pego nas configurações do expresso o número de dias necessários para inatividade.
198                        $timeToExpire = $GLOBALS['phpgw_info']['server']['time_to_account_expires'];
199                       
200                       
201                        $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.
202                        $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)";
203
204                        $GLOBALS['phpgw']->db->query($query);
205                        while($GLOBALS['phpgw']->db->next_record())
206                        {
207                                $result = $GLOBALS['phpgw']->db->row();
208                                array_push($retorno,array("uidNumber"=>$result["account_id"],"login"=> $tempUsers[$result["account_id"]],"li"=>$result["last_login"]));
209                        }
210                       
211                        return $retorno;
212                }
213
214                function safeBitCheck($number,$comparison)
215                {
216                $binNumber = base_convert($number,10,2);
217                $binComparison = strrev(base_convert($comparison,10,2));
218                        $str = strlen($binNumber);
219               
220                if ( ($str <= strlen($binComparison)) && ($binComparison{$str-1}==="1") )
221                {
222                                return '1';
223                }
224                        else
225                {
226                                return '0';
227                        }
228                }
229               
230                function get_list_all($type, $query, $contexts,$sizelimit)
231                {
232                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
233                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
234                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
235                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
236                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
237                        ldap_bind($ldap_conn,$dn,$passwd);
238                       
239                        if ($type == 'accounts')
240                        {
241                                $justthese = array("uidnumber", "uid", "cn", "mail");
242                                $filter="(&(phpgwAccountType=u)(|(ou=*".$query."*)(uid=*".$query."*)(sn=*".$query."*)(cn=*".$query."*)(givenName=*".$query."*)(mail=$query*)(mailAlternateAddress=$query*)))";
243                                                               
244                                $tmp = array();
245                                foreach ($contexts as $index=>$context)
246                                {
247                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
248                                        $info = ldap_get_entries($ldap_conn, $search);
249                                       
250                                        for ($i=0; $i < $info['count']; $i++)
251                                        {
252                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
253                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
254                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
255                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
256                                                $sort[] = $info[$i]['uid'][0];
257                                        }
258                                }
259                                ldap_close($ldap_conn);
260                               
261                                if (count($sort))
262                                {
263                                        natcasesort($sort);
264                                        foreach ($sort as $user_uid)
265                                                $return[$user_uid] = $tmp[$user_uid];
266                                }
267                               
268                                return $return;
269                        }
270                        elseif($type == 'groups')
271                        {
272                                $filter="(&(phpgwAccountType=g)(cn=*$query*))";
273                                $justthese = array("gidnumber", "cn", "description");
274                               
275                                $tmp = array();
276                                foreach ($contexts as $index=>$context)
277                                {
278                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
279                                        $info = ldap_get_entries($ldap_conn, $search);
280                                        for ($i=0; $i < $info['count']; $i++)
281                                        {
282                                                $tmp[$info[$i]['cn'][0]]['cn']= $info[$i]['cn'][0];
283                                                $tmp[$info[$i]['cn'][0]]['description']= $info[$i]['description'][0];
284                                                $tmp[$info[$i]['cn'][0]]['gidnumber']= $info[$i]['gidnumber'][0];
285                                                $sort[] = $info[$i]['cn'][0];
286                                        }
287                                }
288                                ldap_close($ldap_conn);
289                               
290                                natcasesort($sort);
291                                foreach ($sort as $group_cn)
292                                        $return[$group_cn] = $tmp[$group_cn];
293                               
294                                return $return;
295                        }
296                        elseif($type == 'maillists')
297                        {
298                                $filter="(&(phpgwAccountType=l)(|(cn=*".$query."*)(uid=*".$query."*)(mail=*".$query."*)))";
299                                $justthese = array("uidnumber", "cn", "uid", "mail");
300
301                                $tmp = array();
302                                foreach ($contexts as $index=>$context)
303                                {
304                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
305                                        $info = ldap_get_entries($ldap_conn, $search);
306                                       
307                                        for ($i=0; $i < $info['count']; $i++)
308                                        {
309                                                $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
310                                                $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
311                                                $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
312                                                $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
313                                                $sort[] = $info[$i]['uid'][0];
314                                        }
315                                }
316                                ldap_close($ldap_conn);
317                               
318                                natcasesort($sort);
319                                foreach ($sort as $maillist_uid)
320                                        $return[$maillist_uid] = $tmp[$maillist_uid];
321                               
322                                return $return;
323                        }
324                        elseif($type == 'computers')
325                        {
326                                $filter="(&(objectClass=sambaSAMAccount)(|(sambaAcctFlags=[W          ])(sambaAcctFlags=[DW         ])(sambaAcctFlags=[I          ])(sambaAcctFlags=[S          ]))(cn=*".$query."*))";
327                                $justthese = array("cn","uidNumber","description");
328
329                                $tmp = array();
330                                foreach ($contexts as $index=>$context)
331                                {
332                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
333                                        $info = ldap_get_entries($ldap_conn, $search);
334                                        for ($i=0; $i < $info['count']; $i++)
335                                        {
336                                                $tmp[$info[$i]['cn'][0]]['cn']                  = $info[$i]['cn'][0];
337                                                $tmp[$info[$i]['cn'][0]]['uidNumber']   = $info[$i]['uidnumber'][0];
338                                                $tmp[$info[$i]['cn'][0]]['description'] = utf8_decode($info[$i]['description'][0]);
339                                                $sort[] = $info[$i]['cn'][0];
340                                        }
341
342                                }
343                                ldap_close($ldap_conn);
344                               
345                                if (!empty($sort))
346                                {
347                                        natcasesort($sort);
348                                        foreach ($sort as $computer_cn)
349                                                $return[$computer_cn] = $tmp[$computer_cn];
350                                }
351                               
352                                return $return;
353                        }
354                }
355
356                function get_list($type, $query, $contexts)
357                {
358                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
359                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
360                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
361                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
362                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
363                        ldap_bind($ldap_conn,$dn,$passwd);
364                       
365                        if ($type == 'accounts')
366                        {
367                                $justthese = array("uidnumber", "uid", "cn", "mail");
368                                $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)(sn=*".$query."*)(cn=*".$query."*)(givenName=*".$query."*)(mail=$query*)(mailAlternateAddress=$query*)))";
369
370                                $tmp = array();
371                                foreach ($contexts as $index=>$context)
372                                {
373                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
374                                        $info = ldap_get_entries($ldap_conn, $search);
375                                       
376                                        for ($i=0; $i < $info['count']; $i++)
377                                        {
378                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
379                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
380                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
381                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
382                                                $sort[] = $info[$i]['uid'][0];
383                                        }
384                                }
385                                ldap_close($ldap_conn);
386                               
387                                if (count($sort))
388                                {
389                                        natcasesort($sort);
390                                        foreach ($sort as $user_uid)
391                                                $return[$user_uid] = $tmp[$user_uid];
392                                }
393                               
394                                return $return;
395                        }
396                        elseif($type == 'groups')
397                        {
398                                $filter="(&(phpgwAccountType=g)(cn=*$query*))";
399                                $justthese = array("gidnumber", "cn", "description");
400                               
401                                $tmp = array();
402                                foreach ($contexts as $index=>$context)
403                                {
404                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
405                                        $info = ldap_get_entries($ldap_conn, $search);
406                                        for ($i=0; $i < $info['count']; $i++)
407                                        {
408                                                $tmp[$info[$i]['cn'][0]]['cn']= $info[$i]['cn'][0];
409                                                $tmp[$info[$i]['cn'][0]]['description']= $info[$i]['description'][0];
410                                                $tmp[$info[$i]['cn'][0]]['gidnumber']= $info[$i]['gidnumber'][0];
411                                                $sort[] = $info[$i]['cn'][0];
412                                        }
413                                }
414                                ldap_close($ldap_conn);
415                               
416                                natcasesort($sort);
417                                foreach ($sort as $group_cn)
418                                        $return[$group_cn] = $tmp[$group_cn];
419                               
420                                return $return;
421                        }
422                        elseif($type == 'maillists')
423                        {
424                                $filter="(&(phpgwAccountType=l)(|(cn=*".$query."*)(uid=*".$query."*)(mail=*".$query."*)))";
425                                $justthese = array("uidnumber", "cn", "uid", "mail");
426
427                                $tmp = array();
428                                foreach ($contexts as $index=>$context)
429                                {
430                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
431                                        $info = ldap_get_entries($ldap_conn, $search);
432                                       
433                                        for ($i=0; $i < $info['count']; $i++)
434                                        {
435                                                $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
436                                                $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
437                                                $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
438                                                $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
439                                                $sort[] = $info[$i]['uid'][0];
440                                        }
441                                }
442                                ldap_close($ldap_conn);
443                               
444                                natcasesort($sort);
445                                foreach ($sort as $maillist_uid)
446                                        $return[$maillist_uid] = $tmp[$maillist_uid];
447                               
448                                return $return;
449                        }
450                        elseif($type == 'computers')
451                        {
452                                $filter="(&(objectClass=sambaSAMAccount)(|(sambaAcctFlags=[W          ])(sambaAcctFlags=[DW         ])(sambaAcctFlags=[I          ])(sambaAcctFlags=[S          ]))(cn=*".$query."*))";
453                                $justthese = array("cn","uidNumber","description");
454
455                                $tmp = array();
456                                foreach ($contexts as $index=>$context)
457                                {
458                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
459                                        $info = ldap_get_entries($ldap_conn, $search);
460                                        for ($i=0; $i < $info['count']; $i++)
461                                        {
462                                                $tmp[$info[$i]['cn'][0]]['cn']                  = $info[$i]['cn'][0];
463                                                $tmp[$info[$i]['cn'][0]]['uidNumber']   = $info[$i]['uidnumber'][0];
464                                                $tmp[$info[$i]['cn'][0]]['description'] = utf8_decode($info[$i]['description'][0]);
465                                                $sort[] = $info[$i]['cn'][0];
466                                        }
467
468                                }
469
470                                ldap_close($ldap_conn);
471                               
472                                if (!empty($sort))
473                                {
474                                        natcasesort($sort);
475                                        foreach ($sort as $computer_cn)
476                                                $return[$computer_cn] = $tmp[$computer_cn];
477                                }
478                               
479                                return $return;
480                        }
481                }
482
483                function get_organizations($context, $selected='', $referral=false, $show_invisible_ou=true, $master=false)
484                {
485                        $s = CreateObject('phpgwapi.sector_search_ldap');
486                        $sectors_info = $s->get_organizations($context, $selected, $referral, $show_invisible_ou, $master);
487                        return $sectors_info;
488                }               
489                               
490                function get_sectors($selected='', $referral=false, $show_invisible_ou=true)
491                {
492                        $s = CreateObject('phpgwapi.sector_search_ldap');
493                        $sectors_info = $s->get_sectors($selected, $referral, $show_invisible_ou);
494                        return $sectors_info;
495                }               
496 
497                // Get list of levels (0).
498                function get_groups_list($contexts,$filtro)
499                {
500                        $a_sectors = array();
501                       
502                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
503                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
504                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
505                       
506                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
507                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
508                        ldap_bind($ldap_conn,$dn,$passwd);
509                       
510                        $justthese = array("dn");
511                        $filter = "(ou=".$filtro.")";
512                       
513                        $systemName = strtolower($GLOBALS['phpgw_info']['server']['system_name']);
514                        if ($systemName != '')
515                                $filter = "(&$filter(phpgwSystem=$systemName))";
516                       
517                        foreach ($contexts as $context)
518                        {
519                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
520                        $info = ldap_get_entries($ldap_conn, $search);
521
522                        for ($i=0; $i<$info["count"]; $i++)
523                    {
524                        $a_sectors[] = $info[$i]['dn'];
525                    }
526                        }
527               
528                        ldap_close($ldap_conn);
529
530                        // Retiro o count do array info e inverto o array para ordenação.
531                foreach ($a_sectors as $context)
532            {
533                                $array_dn = ldap_explode_dn ( $context, 1 );
534
535                $array_dn_reverse  = array_reverse ( $array_dn, true );
536
537                                // Retirar o indice count do array.
538                                array_pop ( $array_dn_reverse );
539
540                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
541                        }
542
543                        // Ordenação
544                        natcasesort($inverted_dn);
545                       
546                        // Construção do select
547                        $level = 0;
548                        $options = array();
549                        foreach ($inverted_dn as $dn=>$invert_ufn)
550                        {
551                $display = '';
552
553                $array_dn_reverse = explode ( "#", $invert_ufn );
554                $array_dn  = array_reverse ( $array_dn_reverse, true );
555                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
556
557                if ($level == 0)
558                                {
559                    $display.= ' ';
560                        reset ( $array_dn );
561                    $display .= ' ' . (current ( $array_dn ) );
562                                        $dn = trim(strtolower($dn));
563                                        $options[$dn] = $display;
564                }
565                }
566            return $options;
567                }
568               
569                // Get list of levels (0), value DN
570                function get_groups_list_dn($contexts,$filtro)
571                {
572                        $a_sectors = array();
573                       
574                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
575                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
576                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
577                       
578                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
579                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
580                        ldap_bind($ldap_conn,$dn,$passwd);
581                       
582                        $justthese = array("dn");
583                        $filter = "(ou=".$filtro.")";
584                       
585                        $systemName = strtolower($GLOBALS['phpgw_info']['server']['system_name']);
586                        if ($systemName != '')
587                                $filter = "(&$filter(phpgwSystem=$systemName))";
588                       
589                        foreach ($contexts as $context)
590                        {
591                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
592                        $info = ldap_get_entries($ldap_conn, $search);
593                        for ($i=0; $i<$info["count"]; $i++)
594                    {
595                        $a_sectors[] = $info[$i]['dn'];
596                    }
597                        }
598               
599                        ldap_close($ldap_conn);
600
601                        // Retiro o count do array info e inverto o array para ordenação.
602                foreach ($a_sectors as $context)
603            {
604                                $array_dn = ldap_explode_dn ( $context, 1 );
605
606                $array_dn_reverse  = array_reverse ( $array_dn, true );
607
608                                // Retirar o indice count do array.
609                                array_pop ( $array_dn_reverse );
610
611                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
612                        }
613
614                        // Ordenação
615                        natcasesort($inverted_dn);
616                       
617                        // Construção do select
618                        $level = 0;
619                        $options = array();
620                        foreach ($inverted_dn as $dn=>$invert_ufn)
621                        {
622                $display = '';
623
624                $array_dn_reverse = explode ( "#", $invert_ufn );
625                $array_dn  = array_reverse ( $array_dn_reverse, true );
626                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
627
628                if ($level == 0)
629                                {
630                    $display.= ' ';
631                        reset ( $array_dn );
632                    $display .= ' ' . trim(strtolower($dn));
633                                        $dn = trim(strtolower($dn));
634                                        $options[] = $display;
635                }
636                }
637            return $options;
638                }
639
640                // Get list of all levels.
641                function get_sectors_list($contexts,$contextdn)
642                {
643                        $a_sectors = array();
644                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
645                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
646                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
647                       
648                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
649                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
650                        ldap_bind($ldap_conn,$dn,$passwd);
651                       
652                        $justthese = array("ou", "dn");
653                        $filter = "(&(objectClass=organizationalUnit)(|(objectClass=organizationalUnit)))";
654                       
655                        foreach ($contexts as $context)
656                        {
657                                $search=ldap_search($ldap_conn, $contextdn, $filter, $justthese);
658                        $info = ldap_get_entries($ldap_conn, $search);
659                        for ($i=0; $i<$info["count"]; $i++)
660                    {
661                        $a_sectors[] = trim(strtoupper($info[$i]['dn']));
662                    }
663                        }
664
665                        ldap_close($ldap_conn);
666
667                        // Retiro o count do array info e inverto o array para ordenação.
668                foreach ($a_sectors as $context)
669            {
670
671                                $array_dn = ldap_explode_dn ( $context, 1 );
672
673                $array_dn_reverse  = array_reverse ( $array_dn, true );
674
675                                // Retirar o indice count do array.
676                                array_pop ( $array_dn_reverse );
677                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
678
679                        }
680
681                        // Ordenação
682                        natcasesort($inverted_dn);
683
684                        // seleciona os setores do grupo escolhido
685                        $level = 0;
686                        $options = array();
687
688                        foreach ($inverted_dn as $dn=>$invert_ufn)
689                        {
690                                $display = '';
691                                $array_dn_completo = '';
692                                $ii = 0;                       
693                               
694                                $array_dn_reverse = explode ( "#", $invert_ufn );
695                                $array_dn  = array_reverse ( $array_dn_reverse, true );
696
697                                $valorgrupo = (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])));
698                                $valorsubgrupo = (count($array_dn)-1);
699                                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
700
701                                reset ( $array_dn );
702                               
703                                if ($level > 0)
704                                {
705                                        for ($i=0; $i<$level; $i++)
706                                        {
707                                                $ii = $ii +1;
708                                                $display .= ' --';
709
710                                                if ($ii==1)
711                                                {
712                                                        $array_dn_completo .= $array_dn[$valorgrupo + $ii];                                             
713                                                }
714                                                else
715                                                {
716                                                        $array_dn_completo .= " | ".$array_dn[$valorgrupo + $ii];
717                                                }
718
719                                        }
720                                }
721                                else
722                                {
723//                                      $array_dn_completo .= (current($array_dn));                                                                                     
724                                        $array_dn_completo .= ' ';                                                                                     
725                                }
726
727                                $display .= ' '. (current($array_dn)).'#'.trim(strtolower($dn)).'#'.$array_dn_completo;
728                                $options[] = $display;
729
730                }
731            return $options;
732                }
733
734                function get_list_usersgroups_sector($query, $contexts,$sizelimit)
735                {
736                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
737                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
738                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
739                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
740                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
741                        ldap_bind($ldap_conn,$dn,$passwd);
742
743                        $filter = "(&(phpgwAccountType=g)(cn=*))";
744                        $justthese = array("gidnumber", "cn", "description");
745
746                        $tmp = array();
747                        foreach ($contexts as $index=>$context)
748                        {                               
749                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
750                                $info = ldap_get_entries($ldap_conn, $search);                         
751                               
752                                for ($i=0; $i < $info['count']; $i++)
753                                {
754                                        $tmp[$info[$i]['cn'][0]]['id']                          = $info[$i]['gidnumber'][0];
755                                        $tmp[$info[$i]['cn'][0]]['name']                        = $info[$i]['cn'][0];
756                                        $tmp[$info[$i]['cn'][0]]['description']         = $info[$i]['description'][0];
757                                        $sort[] = $info[$i]['cn'][0];
758                                }
759                        }
760                        ldap_close($ldap_conn);
761                       
762                        if (count($sort))
763                        {                       
764                                natcasesort($sort);
765                                foreach ($sort as $usersgroups_gid)
766                                        $return[$usersgroups_gid] = $tmp[$usersgroups_gid];
767                        }
768                       
769                        return $return;
770                }               
771               
772                function get_list_maillists_sector($query, $contexts,$sizelimit)
773                {
774                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
775                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
776                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
777                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
778                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
779                        ldap_bind($ldap_conn,$dn,$passwd);
780
781                        $filter="(&(phpgwAccountType=l)(|(uid=*)))";
782                        $justthese = array("uidnumber", "cn", "uid", "mail");
783
784                        $tmp = array();
785                        foreach ($contexts as $index=>$context)
786                        {
787                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
788                                $info = ldap_get_entries($ldap_conn, $search);                         
789                               
790                                for ($i=0; $i < $info['count']; $i++)
791                                {
792                                        $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
793                                        $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
794                                        $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
795                                        $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
796                                        $sort[] = $info[$i]['uid'][0];
797                                }
798                        }
799                        ldap_close($ldap_conn);
800                       
801                        if (count($sort))
802                        {                       
803                                natcasesort($sort);
804                                foreach ($sort as $maillist_uid)
805                                        $return[$maillist_uid] = $tmp[$maillist_uid];
806                        }
807                       
808                        return $return;
809                }               
810               
811                function get_list_user_sector($query, $contexts,$sizelimit)
812                {
813                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
814                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
815                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
816                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
817                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
818                        ldap_bind($ldap_conn,$dn,$passwd);
819
820                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
821                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");
822                       
823                        $tmp = array();
824               
825                        foreach ($contexts as $index=>$context)
826                        {
827
828                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
829                                $info = ldap_get_entries($ldap_conn, $search);
830
831                                for ($i=0; $i < $info['count']; $i++)
832                                {
833                                        $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
834                                        $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
835                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
836                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
837                                        $tmp[$info[$i]['uid'][0]]['account_phone']= $info[$i]['telephonenumber'][0];
838                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
839                                        $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];                                 
840                                        $sort[] = $info[$i]['uid'][0];
841                                }
842                        }
843                       
844                        ldap_close($ldap_conn);
845                               
846                        if (count($sort))
847                        {
848                                natcasesort($sort);
849                                foreach ($sort as $user_uid)
850                                        $return[$user_uid] = $tmp[$user_uid];
851                        }
852                               
853                        return $return;
854                }
855
856                function get_list_user_sector_logon($query, $contexts,$sizelimit,$numacesso)
857                {
858                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
859                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
860                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
861                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
862                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
863                        ldap_bind($ldap_conn,$dn,$passwd);
864
865                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
866                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");
867                       
868                        $tmp = array();
869               
870                        foreach ($contexts as $index=>$context)
871                        {
872
873                                $field = 'cn';
874                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
875                                $info = ldap_get_entries($ldap_conn, $search);
876
877                                for ($i=0; $i < $info['count']; $i++)
878                                {
879                                        $access_log =  $this->show_access_log($info[$i]['uidnumber'][0]);
880
881                                        $access_log_array = explode("#",$access_log);
882
883                                        if ($access_log_array[1] >= $numacesso or $numacesso == $access_log_array[0]) {
884
885                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
886                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
887                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
888                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
889                                                $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
890                                                $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];                                 
891                                                $sort[] = $info[$i]['uid'][0];
892                                        }
893                                }
894                        }
895
896                        ldap_close($ldap_conn);
897                               
898                        if (count($sort))
899                        {
900                                natcasesort($sort);
901                                foreach ($sort as $user_uid)
902                                        $return[$user_uid] = $tmp[$user_uid];
903                        }
904                               
905                        return $return;
906                }
907               
908                function exist_account_lid($account_lid)
909                {
910                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
911                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "uid=" . $account_lid);
912                        $result = ldap_get_entries($conection, $sri);
913                        return $result['count'];
914                }
915               
916                function exist_email($mail)
917                {
918                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
919                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "mail=" . $mail);
920                        $result = ldap_get_entries($conection, $sri);
921                        ldap_close($conection);
922                       
923                        if ($result['count'] == 0)
924                                return false;
925                        else
926                                return true;
927                }
928               
929                function array_invert($array)
930                {
931                        $result[] = end($array);
932                        while ($item = prev($array))
933                                $result[] = $item;
934                        return $result;
935                }
936
937                function make_list_app($account_lid, $user_applications='', $disabled='')
938                {
939                        // create list of ALL available apps
940                        $availableAppsGLOBALS = $GLOBALS['phpgw_info']['apps'];
941                       
942                        // create list of available apps for the user
943                        $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."'";
944                        $GLOBALS['phpgw']->db->query($query);
945                        while($GLOBALS['phpgw']->db->next_record())
946                        {
947                                $availableApps[] = $GLOBALS['phpgw']->db->row();
948                        }
949                       
950                        // Retira alguns modulos
951                        if (count($availableApps))
952                        {
953                                foreach ($availableApps as $key => $value)
954                                {
955                                        if ($value['app'] != 'phpgwapi')
956                                                $tmp[] = $availableApps[$key];
957                                }
958                        }
959                        $availableApps = $tmp;
960                       
961                        // Cria um array com as aplicacoes disponiveis para o manager, com as atributos das aplicacoes.
962                        $availableAppsUser = array();
963                        if (count($availableApps))
964                        {
965                                foreach($availableApps as $app => $title)
966                                {
967                                        if ($availableAppsGLOBALS[$title['app']])
968                                                $availableAppsUser[$title['app']] = $availableAppsGLOBALS[$title['app']];
969                                }
970                        }
971                       
972                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
973                        $applications_list = '';
974                        $app_col1 = '';
975                        $app_col2 = '';
976                        $app_col3 = '';
977                        $total_apps = count($availableAppsUser);
978                        $i = 0;
979                        foreach($availableAppsUser as $app => $data)
980                        {
981                                // 1 coluna
982                                if (($i +1) % 3 == 1)
983                                {
984                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
985                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
986                                        $data['title'],$app,$checked, $disabled);
987                                        if ($i == ($total_apps-1))
988                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>','#DDDDDD', $app_col1);
989                                }
990                               
991                                // 2 coluna
992                                if (($i +1) % 3 == 2)
993                                {
994                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
995                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
996                                        $data['title'],$app,$checked, $disabled);
997                                       
998                                        if ($i == ($total_apps-1))
999                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>','#DDDDDD', $app_col1,$app_col2);
1000                                }
1001                                // 3 coluna
1002                                if (($i +1) % 3 == 0)
1003                                {
1004                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1005                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1006                                        $data['title'],$app,$checked, $disabled);
1007                                       
1008                                        // Cria nova linha
1009                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>','#DDDDDD', $app_col1, $app_col2, $app_col3);                                       
1010                                }
1011                                $i++;
1012                        }
1013                        return $applications_list;
1014                }
1015               
1016                function exist_attribute_in_ldap($dn, $attribute, $value)
1017                {
1018                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
1019                        $search = ldap_search($connection, $dn, $attribute. "=" . $value);
1020                        $result = ldap_get_entries($connection, $search);
1021                        ldap_close($connection);
1022                        //_debug_array($result);
1023                        if ($result['count'] == 0)
1024                                return false;
1025                        else
1026                                return true;   
1027                }
1028               
1029                function lang($key)
1030                {
1031                        if ($_SESSION['phpgw_info']['expressoAdmin']['lang'][$key])
1032                                return $_SESSION['phpgw_info']['expressoAdmin']['lang'][$key];
1033                        else
1034                                return $key . '*';
1035                }
1036               
1037                function make_lang($ram_lang)
1038                {
1039                        $a_lang = split("_", $ram_lang);
1040                        $a_lang_reverse  = array_reverse ( $a_lang, true );
1041                        array_pop ( $a_lang_reverse );
1042                        $a_lang  = array_reverse ( $a_lang_reverse, true );
1043                        $a_new_lang = implode ( " ", $a_lang );
1044                        return lang($a_new_lang);
1045                }
1046
1047                function make_dinamic_lang($template_obj, $block)
1048                {
1049                        $tpl_vars = $template_obj->get_undefined($block);
1050                        $array_langs = array();
1051                       
1052                        foreach ($tpl_vars as $atribute)
1053                        {
1054                                $lang = strstr($atribute, 'lang_');
1055                                if($lang !== false)
1056                                {
1057                                        //$template_obj->set_var($atribute, $this->make_lang($atribute));
1058                                        $array_langs[$atribute] = $this->make_lang($atribute);
1059                                }
1060                        }
1061                        return $array_langs;
1062                }
1063
1064                function paginate_usersgroups($query, $contexts, $field, $order = 'asc', $page = null, $perPage = null )
1065                {
1066                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1067                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1068                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1069                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1070                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1071                        ldap_bind($ldap_conn,$dn,$passwd);
1072                       
1073                        $filter="(&(phpgwAccountType=g)(cn=*))";
1074                        $justthese = array("gidnumber", "cn", "description");                                                                           
1075                                                       
1076                        foreach ($contexts as $index=>$context)
1077                        {                               
1078                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1079       
1080                                $rConnection = $ldap_conn;
1081                                $rSearch = $search;
1082                                $sOrder = $order;
1083                                $iPage = $page;
1084                                $iPerPage = $perPage;
1085                                $sField = $field;
1086                                               
1087                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1088                               
1089                                if ( $iPage === null || $iPerPage === null )
1090                                {
1091                                        # fetch all in one page
1092                                        $iStart = 0;
1093                                        $iEnd = $iTotalEntries - 1;
1094                                }
1095                                else
1096                                {
1097                                        # calculate range of page
1098                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1099                                       
1100                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1101                                        $iEnd = $iPage * $iPerPage;
1102
1103                                        if ( $sOrder === "desc" )
1104                                        {
1105                                                # revert range
1106                                                $iStart = $iTotalEntries - 1 - $iEnd;
1107                                                $iEnd = $iStart + $iPerPage - 1;
1108                                        }
1109                                }
1110                               
1111                                /********* Importante Mostra o resultado da paginação **********
1112                                var_dump( $iStart . " " . $iEnd );
1113                                ****************** Só descomentar ******************************/
1114                               
1115                                 # fetch entries
1116                            ldap_sort( $rConnection, $rSearch, $sField );
1117
1118                                $aList = array();
1119                                for (
1120                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1121                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1122                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1123                                        )
1124                                {
1125                                        if ( $iCurrent >= $iStart )
1126                                        {
1127                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1128                                        }
1129                                }
1130                        }
1131
1132                        ldap_close($ldap_conn);
1133
1134                        # if order is desc revert page's entries
1135                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1136                }               
1137               
1138                function paginate_maillists($query, $contexts, $field, $order = 'asc', $page = null, $perPage = null )
1139                {
1140                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1141                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1142                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1143                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1144                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1145                        ldap_bind($ldap_conn,$dn,$passwd);
1146                       
1147                        $filter="(&(phpgwAccountType=l)(|(uid=*)))";
1148                        $justthese = array("uidnumber", "cn", "uid", "mail");                                                                                   
1149                                                       
1150                        foreach ($contexts as $index=>$context)
1151                        {
1152                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1153       
1154                                $rConnection = $ldap_conn;
1155                                $rSearch = $search;
1156                                $sOrder = $order;
1157                                $iPage = $page;
1158                                $iPerPage = $perPage;
1159                                $sField = $field;
1160                                               
1161                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1162                               
1163                                if ( $iPage === null || $iPerPage === null )
1164                                {
1165                                        # fetch all in one page
1166                                        $iStart = 0;
1167                                        $iEnd = $iTotalEntries - 1;
1168                                }
1169                                else
1170                                {
1171                                        # calculate range of page
1172                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1173                                       
1174                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1175                                        $iEnd = $iPage * $iPerPage;
1176
1177                                        if ( $sOrder === "desc" )
1178                                        {
1179                                                # revert range
1180                                                $iStart = $iTotalEntries - 1 - $iEnd;
1181                                                $iEnd = $iStart + $iPerPage - 1;
1182                                        }
1183                                }
1184                               
1185                                /********* Importante Mostra o resultado da paginação **********
1186                                var_dump( $iStart . " " . $iEnd );
1187                                ****************** Só descomentar ******************************/
1188                               
1189                                 # fetch entries
1190                            ldap_sort( $rConnection, $rSearch, $sField );
1191
1192                                $aList = array();
1193                                for (
1194                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1195                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1196                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1197                                        )
1198                                {
1199                                        if ( $iCurrent >= $iStart )
1200                                        {
1201                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1202                                        }
1203                                }
1204                        }
1205
1206                        ldap_close($ldap_conn);
1207
1208                        # if order is desc revert page's entries
1209                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1210                }               
1211               
1212                function Paginate_user($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
1213
1214                {
1215                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1216                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1217                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1218                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1219                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1220                        ldap_bind($ldap_conn,$dn,$passwd);
1221                       
1222                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
1223                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");                                                                                 
1224                                                       
1225                        foreach ($contexts as $index=>$context)
1226                        {
1227                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1228       
1229                                $rConnection = $ldap_conn;
1230                                $rSearch = $search;
1231                                $sOrder = $Order;
1232                                $iPage = $Page;
1233                                $iPerPage = $PerPage;
1234                                $sField = $Field;
1235                                               
1236                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1237
1238                                if ( $iPage === null || $iPerPage === null )
1239                                {
1240                                        # fetch all in one page
1241                                        $iStart = 0;
1242                                        $iEnd = $iTotalEntries - 1;
1243                                }
1244                                else
1245                                {
1246                                        # calculate range of page
1247                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1248                                       
1249                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1250                                        $iEnd = $iPage * $iPerPage;
1251
1252                                        if ( $sOrder === "desc" )
1253                                        {
1254                                                # revert range
1255                                                $iStart = $iTotalEntries - 1 - $iEnd;
1256                                                $iEnd = $iStart + $iPerPage - 1;
1257                                        }
1258                                }
1259                               
1260                                /********* Importante Mostra o resultado da paginação **********
1261                                var_dump( $iStart . " " . $iEnd );
1262                                ****************** Só descomentar ******************************/
1263                               
1264                                 # fetch entries
1265                            ldap_sort( $rConnection, $rSearch, $sField );
1266
1267                                $aList = array();
1268                                for (
1269                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1270                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1271                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1272                                        )
1273                                {
1274                                        if ( $iCurrent >= $iStart )
1275                                        {
1276                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1277                                        }
1278                                }
1279                        }
1280
1281                        ldap_close($ldap_conn);
1282
1283                        # if order is desc revert page's entries
1284                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1285                }
1286
1287                function get_list_ou_user_logon($query,$contexts,$sizelimit)
1288                {
1289                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1290                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1291                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1292                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1293                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1294                        ldap_bind($ldap_conn,$dn,$passwd);
1295
1296                        $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)))";
1297                        $justthese = array("ou");                                                                                       
1298                        $tmp = array();
1299               
1300                        foreach ($contexts as $index=>$context)
1301                        {
1302
1303                                $field = 'cn';
1304                                $search=ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1305                                $info = ldap_get_entries($ldap_conn, $search);
1306
1307                                for ($i=0; $i < $info['count']; $i++)
1308                                {
1309                                        $a_sectors[] = $info[$i]['dn'];
1310                                }
1311                        }
1312                       
1313                        ldap_close($ldap_conn);
1314                               
1315                        // Retiro o count do array info e inverto o array para ordenação.
1316                foreach ($a_sectors as $context)
1317            {
1318                                $array_dn = ldap_explode_dn ( $context, 1 );
1319
1320                $array_dn_reverse  = array_reverse ( $array_dn, true );
1321
1322                                // Retirar o indice count do array.
1323                                array_pop ( $array_dn_reverse );
1324
1325                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
1326                        }
1327
1328                        // Ordenação
1329                        natcasesort($inverted_dn);
1330
1331                        // Construção do select
1332                        $level = 0;
1333                        $options = array();
1334                        foreach ($inverted_dn as $dn=>$invert_ufn)
1335                        {
1336                $display = '';
1337
1338                $array_dn_reverse = explode ( "#", $invert_ufn );
1339                $array_dn  = array_reverse ( $array_dn_reverse, true );
1340
1341                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
1342
1343                                $display.= ' ';
1344                                reset ( $array_dn );
1345                                $display .= ' ' . ($array_dn[$level]);
1346                                $dn = trim(strtolower($dn));
1347                                $options[$dn] = $display;
1348                }
1349
1350            return $options;
1351                }
1352
1353                function get_list_context_logon($query_user,$contexts,$sizelimit)
1354                {
1355                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1356                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1357                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1358                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1359                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1360                        ldap_bind($ldap_conn,$dn,$passwd);
1361
1362                        // Le BD para pegar os administradors.
1363                        $query = "SELECT manager_lid,context FROM phpgw_expressoadmin WHERE manager_lid='".$query_user."' ORDER by manager_lid";
1364                        $GLOBALS['phpgw']->db->query($query);
1365                        while($GLOBALS['phpgw']->db->next_record())
1366                        {
1367                                $managers[] = $GLOBALS['phpgw']->db->row();
1368                        }
1369
1370                        // Loop para listar as dn
1371                        if (count($managers))
1372                        {
1373                                foreach($managers as $array_managers)
1374                                {
1375                                        $display = '';
1376                                        $managers_context = "";
1377                                        $a_managers_context = explode("%", $array_managers['context']);
1378
1379                                        // Ordenação
1380                                        natcasesort($a_managers_context);
1381
1382                                        $options = array();
1383                                        $level = 0;
1384                                                                                               
1385                                        foreach ($a_managers_context as $dn=>$invert_ufn)
1386                                        {
1387                                                $dn_explode = explode (",",$invert_ufn);
1388
1389                                                // Construção do select
1390                                                $array_dn  = array_reverse ( $dn_explode, true );
1391
1392                                                $level = count( $dn_explode ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
1393                                                $display = ' ';
1394                                                reset ( $array_dn );
1395//                                              $display = str_replace("ou=", "",($array_dn[$level]));
1396                                                $display = str_replace("ou=", "",($array_dn[0]));
1397                                                $display = str_replace("dc=", "",($display));
1398                                                $dn = trim(strtolower($dn));
1399                                                $options[$dn] = trim(strtoupper($display));
1400                                        }
1401                                }
1402                        }
1403
1404                return $options;
1405                }
1406
1407                function get_list_groups_dn($query_user,$contexts,$sizelimit)
1408                {
1409                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1410                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1411                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1412                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1413                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1414                        ldap_bind($ldap_conn,$dn,$passwd);
1415
1416                        // Le BD para pegar os administradors.
1417                        $query = "SELECT manager_lid,context FROM phpgw_expressoadmin WHERE manager_lid='".$query_user."' ORDER by manager_lid";
1418                        $GLOBALS['phpgw']->db->query($query);
1419                        while($GLOBALS['phpgw']->db->next_record())
1420                        {
1421                                $managers[] = $GLOBALS['phpgw']->db->row();
1422                        }
1423
1424                        // Loop para listar as dn
1425                        if (count($managers))
1426                        {
1427                                foreach($managers as $array_managers)
1428                                {
1429                                        $display = '';
1430                                        $managers_context = "";
1431                                        $options = explode("%", $array_managers['context']);
1432
1433                                        // Ordenação
1434                                        natcasesort($options);
1435                                }
1436                        }
1437
1438                return $options;
1439                }
1440
1441                function sort_by($field, &$arr, $sorting=SORT_ASC, $case_insensitive=true){
1442                        if(is_array($arr) && (count($arr)>0) && ( ( is_array($arr[0]) && isset($arr[0][$field]) ) || ( is_object($arr[0]) && isset($arr[0]->$field) ) ) ){
1443                                if($case_insensitive==true) $strcmp_fn = "strnatcasecmp";
1444                                else $strcmp_fn = "strnatcmp";
1445               
1446                                if($sorting==SORT_ASC){
1447                                        $fn = create_function('$a,$b', '
1448                                                if(is_object($a) && is_object($b)){
1449                                                        return '.$strcmp_fn.'($a->'.$field.', $b->'.$field.');
1450                                                }else if(is_array($a) && is_array($b)){
1451                                                        return '.$strcmp_fn.'($a["'.$field.'"], $b["'.$field.'"]);
1452                                                }else return 0;
1453                                        ');
1454                                }else{
1455                                        $fn = create_function('$a,$b', '
1456                                                if(is_object($a) && is_object($b)){
1457                                                        return '.$strcmp_fn.'($b->'.$field.', $a->'.$field.');
1458                                                }else if(is_array($a) && is_array($b)){
1459                                                        return '.$strcmp_fn.'($b["'.$field.'"], $a["'.$field.'"]);
1460                                                }else return 0;
1461                                        ');
1462                                }
1463                                usort($arr, $fn);
1464                                return true;
1465                        }else{
1466                                return false;
1467                        }
1468                }       
1469
1470                function show_access_log($account_id)
1471                {       
1472                        $manager_account_lid = $GLOBALS['phpgw']->accounts->data['account_lid'];
1473                        $tmp = $this->read_acl($manager_account_lid);
1474                        $manager_context = $tmp[0]['context'];
1475                       
1476                        // Verifica se tem acesso a este modulo
1477                        if ((!$this->check_acl($manager_account_lid,'edit_users')) && (!$this->check_acl($manager_account_lid,'change_users_password')))
1478                        {
1479                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/reports/inc/access_denied.php'));
1480                        }
1481
1482                        // Le BD para pegar os [li].
1483                        $query = "select li from phpgw_access_log WHERE account_id=".$account_id." order by li desc LIMIT 1 OFFSET 0";
1484                        $GLOBALS['phpgw']->db->query($query);
1485                        while($GLOBALS['phpgw']->db->next_record())
1486                        {
1487                                $managers[] = $GLOBALS['phpgw']->db->row();
1488                        }
1489
1490                        if (count($managers))
1491                        {
1492                                // contar intervalo
1493                                 $data_atual = date("Y/m/d", time());
1494
1495                                 $data_antes = date("Y/m/d",$managers['0']['li']);
1496                               
1497                                 $datainicio=strtotime("$data_antes"); // Data de Hoje
1498                                 $datafim =strtotime("$data_atual"); // Data no próximo ano
1499                               
1500                                 $rdata =($datafim-$datainicio)/86400; //transformação do timestamp em dias
1501
1502                                $access_li = date("d/m/Y",$managers['0']['li'])."#".$rdata;
1503
1504                                return $access_li;
1505
1506                        }else{
1507
1508                                $access_li = "Nunca logou#0";
1509
1510                                return $access_li;
1511                        }
1512                }
1513
1514                function Paginate_user_logon($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null, $numacesso)
1515                {
1516                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1517                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1518                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1519                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1520                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1521                        ldap_bind($ldap_conn,$dn,$passwd);
1522                       
1523                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
1524                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");                                                                                   
1525                                                       
1526                        foreach ($contexts as $index=>$context)
1527                        {
1528                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1529       
1530                                $rConnection = $ldap_conn;
1531                                $rSearch = $search;
1532                                $sOrder = $Order;
1533                                $iPage = $Page;
1534                                $iPerPage = $PerPage;
1535                                $sField = $Field;
1536                                               
1537                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1538
1539                                if ( $iPage === null || $iPerPage === null )
1540                                {
1541                                        # fetch all in one page
1542                                        $iStart = 0;
1543                                        $iEnd = $iTotalEntries - 1;
1544                                }
1545                                else
1546                                {
1547                                        # calculate range of page
1548                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1549                                       
1550                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1551                                        $iEnd = $iPage * $iPerPage;
1552
1553                                        if ( $sOrder === "desc" )
1554                                        {
1555                                                # revert range
1556                                                $iStart = $iTotalEntries - 1 - $iEnd;
1557                                                $iEnd = $iStart + $iPerPage - 1;
1558                                        }
1559                                }
1560                               
1561                                /********* Importante Mostra o resultado da paginação **********
1562                                var_dump( $iStart . " " . $iEnd );
1563                                ****************** Só descomentar ******************************/
1564                               
1565                                 # fetch entries
1566                            ldap_sort( $rConnection, $rSearch, $sField );
1567
1568                                $aList = array();
1569                                for (
1570                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1571                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1572                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1573                                        )
1574                                {
1575                                        if ( $iCurrent >= $iStart )
1576                                        {
1577                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1578                                        }
1579                                }
1580                        }
1581
1582                        ldap_close($ldap_conn);
1583
1584                        # if order is desc revert page's entries
1585                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1586                }
1587        }
1588// ****************** fim classe Functions ***********************
1589?>
Note: See TracBrowser for help on using the repository browser.