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

Revision 4033, 52.2 KB checked in by niltonneto, 13 years ago (diff)

Ticket #1691 - Corrigido problema de lentidão ao listar organizações.

RevLine 
[3471]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");
[3530]653                        $filter = "(&(objectClass=organizationalUnit)(|(objectClass=organizationalUnit)))";
[3471]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
[4033]856                function get_count_user_sector($query, $contexts,$sizelimit)
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                        // counting users by sector.
865                        foreach ($contexts as $index=>$context) {
866                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
867                                        $contexts[$index] = null;
868                                        $justthese = array("dn");
869                                        $filter="(objectClass=OrganizationalUnit)";
870                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
871                                        $entries = ldap_get_entries($ldap_conn, $search);
872                                        $contexts = array();
873                                        for ($i=0; $i< $entries['count']; $i++){
874                                                $contexts[] = $entries[$i]['dn'];
875                                        }
876                                }
877                        }       
878                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
879                        $justthese = array("uidnumber");
880                        $total_count = 0;
881                       
882                        foreach ($contexts as $index=>$context) {                               
883                                $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
884                                $total_count += ldap_count_entries($ldap_conn, $search);                                                       
885                        }
886                        ldap_close($ldap_conn);
887                        return $total_count;
888                }
889
[3471]890                function get_list_user_sector_logon($query, $contexts,$sizelimit,$numacesso)
891                {
892                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
893                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
894                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
895                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
896                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
897                        ldap_bind($ldap_conn,$dn,$passwd);
898
899                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
900                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");
901                       
902                        $tmp = array();
903               
904                        foreach ($contexts as $index=>$context)
905                        {
906
907                                $field = 'cn';
908                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
909                                $info = ldap_get_entries($ldap_conn, $search);
910
911                                for ($i=0; $i < $info['count']; $i++)
912                                {
913                                        $access_log =  $this->show_access_log($info[$i]['uidnumber'][0]);
914
915                                        $access_log_array = explode("#",$access_log);
916
917                                        if ($access_log_array[1] >= $numacesso or $numacesso == $access_log_array[0]) {
918
919                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
920                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
921                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
922                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
923                                                $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
924                                                $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];                                 
925                                                $sort[] = $info[$i]['uid'][0];
926                                        }
927                                }
928                        }
929
930                        ldap_close($ldap_conn);
931                               
932                        if (count($sort))
933                        {
934                                natcasesort($sort);
935                                foreach ($sort as $user_uid)
936                                        $return[$user_uid] = $tmp[$user_uid];
937                        }
938                               
939                        return $return;
940                }
941               
942                function exist_account_lid($account_lid)
943                {
944                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
945                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "uid=" . $account_lid);
946                        $result = ldap_get_entries($conection, $sri);
947                        return $result['count'];
948                }
949               
950                function exist_email($mail)
951                {
952                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
953                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "mail=" . $mail);
954                        $result = ldap_get_entries($conection, $sri);
955                        ldap_close($conection);
956                       
957                        if ($result['count'] == 0)
958                                return false;
959                        else
960                                return true;
961                }
962               
963                function array_invert($array)
964                {
965                        $result[] = end($array);
966                        while ($item = prev($array))
967                                $result[] = $item;
968                        return $result;
969                }
[3949]970
[3471]971                function make_list_app($account_lid, $user_applications='', $disabled='')
972                {
973                        // create list of ALL available apps
974                        $availableAppsGLOBALS = $GLOBALS['phpgw_info']['apps'];
975                       
976                        // create list of available apps for the user
977                        $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."'";
978                        $GLOBALS['phpgw']->db->query($query);
979                        while($GLOBALS['phpgw']->db->next_record())
980                        {
981                                $availableApps[] = $GLOBALS['phpgw']->db->row();
982                        }
983                       
984                        // Retira alguns modulos
985                        if (count($availableApps))
986                        {
987                                foreach ($availableApps as $key => $value)
988                                {
989                                        if ($value['app'] != 'phpgwapi')
990                                                $tmp[] = $availableApps[$key];
991                                }
992                        }
993                        $availableApps = $tmp;
994                       
995                        // Cria um array com as aplicacoes disponiveis para o manager, com as atributos das aplicacoes.
996                        $availableAppsUser = array();
997                        if (count($availableApps))
998                        {
999                                foreach($availableApps as $app => $title)
1000                                {
1001                                        if ($availableAppsGLOBALS[$title['app']])
1002                                                $availableAppsUser[$title['app']] = $availableAppsGLOBALS[$title['app']];
1003                                }
1004                        }
1005                       
1006                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
1007                        $applications_list = '';
1008                        $app_col1 = '';
1009                        $app_col2 = '';
1010                        $app_col3 = '';
1011                        $total_apps = count($availableAppsUser);
1012                        $i = 0;
1013                        foreach($availableAppsUser as $app => $data)
1014                        {
1015                                // 1 coluna
1016                                if (($i +1) % 3 == 1)
1017                                {
1018                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1019                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1020                                        $data['title'],$app,$checked, $disabled);
1021                                        if ($i == ($total_apps-1))
1022                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>','#DDDDDD', $app_col1);
1023                                }
1024                               
1025                                // 2 coluna
1026                                if (($i +1) % 3 == 2)
1027                                {
1028                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1029                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1030                                        $data['title'],$app,$checked, $disabled);
1031                                       
1032                                        if ($i == ($total_apps-1))
1033                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>','#DDDDDD', $app_col1,$app_col2);
1034                                }
1035                                // 3 coluna
1036                                if (($i +1) % 3 == 0)
1037                                {
1038                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
1039                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
1040                                        $data['title'],$app,$checked, $disabled);
1041                                       
1042                                        // Cria nova linha
1043                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>','#DDDDDD', $app_col1, $app_col2, $app_col3);                                       
1044                                }
1045                                $i++;
1046                        }
1047                        return $applications_list;
1048                }
1049               
1050                function exist_attribute_in_ldap($dn, $attribute, $value)
1051                {
1052                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
1053                        $search = ldap_search($connection, $dn, $attribute. "=" . $value);
1054                        $result = ldap_get_entries($connection, $search);
1055                        ldap_close($connection);
1056                        //_debug_array($result);
1057                        if ($result['count'] == 0)
1058                                return false;
1059                        else
1060                                return true;   
1061                }
1062               
1063                function lang($key)
1064                {
1065                        if ($_SESSION['phpgw_info']['expressoAdmin']['lang'][$key])
1066                                return $_SESSION['phpgw_info']['expressoAdmin']['lang'][$key];
1067                        else
1068                                return $key . '*';
1069                }
1070               
1071                function make_lang($ram_lang)
1072                {
1073                        $a_lang = split("_", $ram_lang);
1074                        $a_lang_reverse  = array_reverse ( $a_lang, true );
1075                        array_pop ( $a_lang_reverse );
1076                        $a_lang  = array_reverse ( $a_lang_reverse, true );
1077                        $a_new_lang = implode ( " ", $a_lang );
1078                        return lang($a_new_lang);
1079                }
1080
1081                function make_dinamic_lang($template_obj, $block)
1082                {
1083                        $tpl_vars = $template_obj->get_undefined($block);
1084                        $array_langs = array();
1085                       
1086                        foreach ($tpl_vars as $atribute)
1087                        {
1088                                $lang = strstr($atribute, 'lang_');
1089                                if($lang !== false)
1090                                {
1091                                        //$template_obj->set_var($atribute, $this->make_lang($atribute));
1092                                        $array_langs[$atribute] = $this->make_lang($atribute);
1093                                }
1094                        }
1095                        return $array_langs;
1096                }
1097
1098                function paginate_usersgroups($query, $contexts, $field, $order = 'asc', $page = null, $perPage = null )
1099                {
1100                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1101                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1102                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1103                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1104                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1105                        ldap_bind($ldap_conn,$dn,$passwd);
1106                       
1107                        $filter="(&(phpgwAccountType=g)(cn=*))";
1108                        $justthese = array("gidnumber", "cn", "description");                                                                           
1109                                                       
1110                        foreach ($contexts as $index=>$context)
1111                        {                               
1112                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1113       
1114                                $rConnection = $ldap_conn;
1115                                $rSearch = $search;
1116                                $sOrder = $order;
1117                                $iPage = $page;
1118                                $iPerPage = $perPage;
1119                                $sField = $field;
1120                                               
1121                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1122                               
1123                                if ( $iPage === null || $iPerPage === null )
1124                                {
1125                                        # fetch all in one page
1126                                        $iStart = 0;
1127                                        $iEnd = $iTotalEntries - 1;
1128                                }
1129                                else
1130                                {
1131                                        # calculate range of page
1132                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1133                                       
1134                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1135                                        $iEnd = $iPage * $iPerPage;
1136
1137                                        if ( $sOrder === "desc" )
1138                                        {
1139                                                # revert range
1140                                                $iStart = $iTotalEntries - 1 - $iEnd;
1141                                                $iEnd = $iStart + $iPerPage - 1;
1142                                        }
1143                                }
1144                               
1145                                /********* Importante Mostra o resultado da paginação **********
1146                                var_dump( $iStart . " " . $iEnd );
1147                                ****************** Só descomentar ******************************/
1148                               
1149                                 # fetch entries
1150                            ldap_sort( $rConnection, $rSearch, $sField );
1151
1152                                $aList = array();
1153                                for (
1154                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1155                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1156                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1157                                        )
1158                                {
1159                                        if ( $iCurrent >= $iStart )
1160                                        {
1161                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1162                                        }
1163                                }
1164                        }
1165
1166                        ldap_close($ldap_conn);
1167
1168                        # if order is desc revert page's entries
1169                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1170                }               
1171               
1172                function paginate_maillists($query, $contexts, $field, $order = 'asc', $page = null, $perPage = null )
1173                {
1174                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1175                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1176                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1177                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1178                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1179                        ldap_bind($ldap_conn,$dn,$passwd);
1180                       
1181                        $filter="(&(phpgwAccountType=l)(|(uid=*)))";
1182                        $justthese = array("uidnumber", "cn", "uid", "mail");                                                                                   
1183                                                       
1184                        foreach ($contexts as $index=>$context)
1185                        {
1186                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1187       
1188                                $rConnection = $ldap_conn;
1189                                $rSearch = $search;
1190                                $sOrder = $order;
1191                                $iPage = $page;
1192                                $iPerPage = $perPage;
1193                                $sField = $field;
1194                                               
1195                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1196                               
1197                                if ( $iPage === null || $iPerPage === null )
1198                                {
1199                                        # fetch all in one page
1200                                        $iStart = 0;
1201                                        $iEnd = $iTotalEntries - 1;
1202                                }
1203                                else
1204                                {
1205                                        # calculate range of page
1206                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1207                                       
1208                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1209                                        $iEnd = $iPage * $iPerPage;
1210
1211                                        if ( $sOrder === "desc" )
1212                                        {
1213                                                # revert range
1214                                                $iStart = $iTotalEntries - 1 - $iEnd;
1215                                                $iEnd = $iStart + $iPerPage - 1;
1216                                        }
1217                                }
1218                               
1219                                /********* Importante Mostra o resultado da paginação **********
1220                                var_dump( $iStart . " " . $iEnd );
1221                                ****************** Só descomentar ******************************/
1222                               
1223                                 # fetch entries
1224                            ldap_sort( $rConnection, $rSearch, $sField );
1225
1226                                $aList = array();
1227                                for (
1228                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1229                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1230                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1231                                        )
1232                                {
1233                                        if ( $iCurrent >= $iStart )
1234                                        {
1235                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1236                                        }
1237                                }
1238                        }
1239
1240                        ldap_close($ldap_conn);
1241
1242                        # if order is desc revert page's entries
1243                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1244                }               
1245               
1246                function Paginate_user($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
1247
1248                {
1249                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1250                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1251                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1252                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1253                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1254                        ldap_bind($ldap_conn,$dn,$passwd);
1255                       
1256                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
1257                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");                                                                                 
1258                                                       
1259                        foreach ($contexts as $index=>$context)
1260                        {
1261                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1262       
1263                                $rConnection = $ldap_conn;
1264                                $rSearch = $search;
1265                                $sOrder = $Order;
1266                                $iPage = $Page;
1267                                $iPerPage = $PerPage;
1268                                $sField = $Field;
1269                                               
1270                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1271
1272                                if ( $iPage === null || $iPerPage === null )
1273                                {
1274                                        # fetch all in one page
1275                                        $iStart = 0;
1276                                        $iEnd = $iTotalEntries - 1;
1277                                }
1278                                else
1279                                {
1280                                        # calculate range of page
1281                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1282                                       
1283                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1284                                        $iEnd = $iPage * $iPerPage;
1285
1286                                        if ( $sOrder === "desc" )
1287                                        {
1288                                                # revert range
1289                                                $iStart = $iTotalEntries - 1 - $iEnd;
1290                                                $iEnd = $iStart + $iPerPage - 1;
1291                                        }
1292                                }
1293                               
1294                                /********* Importante Mostra o resultado da paginação **********
1295                                var_dump( $iStart . " " . $iEnd );
1296                                ****************** Só descomentar ******************************/
1297                               
1298                                 # fetch entries
1299                            ldap_sort( $rConnection, $rSearch, $sField );
1300
1301                                $aList = array();
1302                                for (
1303                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1304                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1305                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1306                                        )
1307                                {
1308                                        if ( $iCurrent >= $iStart )
1309                                        {
1310                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1311                                        }
1312                                }
1313                        }
1314
1315                        ldap_close($ldap_conn);
1316
1317                        # if order is desc revert page's entries
1318                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1319                }
1320
1321                function get_list_ou_user_logon($query,$contexts,$sizelimit)
1322                {
1323                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1324                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1325                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1326                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1327                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1328                        ldap_bind($ldap_conn,$dn,$passwd);
1329
1330                        $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)))";
1331                        $justthese = array("ou");                                                                                       
1332                        $tmp = array();
1333               
1334                        foreach ($contexts as $index=>$context)
1335                        {
1336
1337                                $field = 'cn';
1338                                $search=ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
1339                                $info = ldap_get_entries($ldap_conn, $search);
1340
1341                                for ($i=0; $i < $info['count']; $i++)
1342                                {
1343                                        $a_sectors[] = $info[$i]['dn'];
1344                                }
1345                        }
1346                       
1347                        ldap_close($ldap_conn);
1348                               
1349                        // Retiro o count do array info e inverto o array para ordenação.
1350                foreach ($a_sectors as $context)
1351            {
1352                                $array_dn = ldap_explode_dn ( $context, 1 );
1353
1354                $array_dn_reverse  = array_reverse ( $array_dn, true );
1355
1356                                // Retirar o indice count do array.
1357                                array_pop ( $array_dn_reverse );
1358
1359                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
1360                        }
1361
1362                        // Ordenação
1363                        natcasesort($inverted_dn);
1364
1365                        // Construção do select
1366                        $level = 0;
1367                        $options = array();
1368                        foreach ($inverted_dn as $dn=>$invert_ufn)
1369                        {
1370                $display = '';
1371
1372                $array_dn_reverse = explode ( "#", $invert_ufn );
1373                $array_dn  = array_reverse ( $array_dn_reverse, true );
1374
1375                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
1376
1377                                $display.= ' ';
1378                                reset ( $array_dn );
1379                                $display .= ' ' . ($array_dn[$level]);
1380                                $dn = trim(strtolower($dn));
1381                                $options[$dn] = $display;
1382                }
1383
1384            return $options;
1385                }
1386
1387                function get_list_context_logon($query_user,$contexts,$sizelimit)
1388                {
1389                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1390                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1391                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1392                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1393                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1394                        ldap_bind($ldap_conn,$dn,$passwd);
1395
1396                        // Le BD para pegar os administradors.
1397                        $query = "SELECT manager_lid,context FROM phpgw_expressoadmin WHERE manager_lid='".$query_user."' ORDER by manager_lid";
1398                        $GLOBALS['phpgw']->db->query($query);
1399                        while($GLOBALS['phpgw']->db->next_record())
1400                        {
1401                                $managers[] = $GLOBALS['phpgw']->db->row();
1402                        }
1403
1404                        // Loop para listar as dn
1405                        if (count($managers))
1406                        {
1407                                foreach($managers as $array_managers)
1408                                {
1409                                        $display = '';
1410                                        $managers_context = "";
1411                                        $a_managers_context = explode("%", $array_managers['context']);
1412
1413                                        // Ordenação
1414                                        natcasesort($a_managers_context);
1415
1416                                        $options = array();
1417                                        $level = 0;
1418                                                                                               
1419                                        foreach ($a_managers_context as $dn=>$invert_ufn)
1420                                        {
1421                                                $dn_explode = explode (",",$invert_ufn);
1422
1423                                                // Construção do select
1424                                                $array_dn  = array_reverse ( $dn_explode, true );
1425
1426                                                $level = count( $dn_explode ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
1427                                                $display = ' ';
1428                                                reset ( $array_dn );
1429//                                              $display = str_replace("ou=", "",($array_dn[$level]));
1430                                                $display = str_replace("ou=", "",($array_dn[0]));
1431                                                $display = str_replace("dc=", "",($display));
1432                                                $dn = trim(strtolower($dn));
1433                                                $options[$dn] = trim(strtoupper($display));
1434                                        }
1435                                }
1436                        }
1437
1438                return $options;
1439                }
1440
1441                function get_list_groups_dn($query_user,$contexts,$sizelimit)
1442                {
1443                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1444                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1445                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1446                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1447                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1448                        ldap_bind($ldap_conn,$dn,$passwd);
1449
1450                        // Le BD para pegar os administradors.
1451                        $query = "SELECT manager_lid,context FROM phpgw_expressoadmin WHERE manager_lid='".$query_user."' ORDER by manager_lid";
1452                        $GLOBALS['phpgw']->db->query($query);
1453                        while($GLOBALS['phpgw']->db->next_record())
1454                        {
1455                                $managers[] = $GLOBALS['phpgw']->db->row();
1456                        }
1457
1458                        // Loop para listar as dn
1459                        if (count($managers))
1460                        {
1461                                foreach($managers as $array_managers)
1462                                {
1463                                        $display = '';
1464                                        $managers_context = "";
1465                                        $options = explode("%", $array_managers['context']);
1466
1467                                        // Ordenação
1468                                        natcasesort($options);
1469                                }
1470                        }
1471
1472                return $options;
1473                }
1474
1475                function sort_by($field, &$arr, $sorting=SORT_ASC, $case_insensitive=true){
1476                        if(is_array($arr) && (count($arr)>0) && ( ( is_array($arr[0]) && isset($arr[0][$field]) ) || ( is_object($arr[0]) && isset($arr[0]->$field) ) ) ){
1477                                if($case_insensitive==true) $strcmp_fn = "strnatcasecmp";
1478                                else $strcmp_fn = "strnatcmp";
1479               
1480                                if($sorting==SORT_ASC){
1481                                        $fn = create_function('$a,$b', '
1482                                                if(is_object($a) && is_object($b)){
1483                                                        return '.$strcmp_fn.'($a->'.$field.', $b->'.$field.');
1484                                                }else if(is_array($a) && is_array($b)){
1485                                                        return '.$strcmp_fn.'($a["'.$field.'"], $b["'.$field.'"]);
1486                                                }else return 0;
1487                                        ');
1488                                }else{
1489                                        $fn = create_function('$a,$b', '
1490                                                if(is_object($a) && is_object($b)){
1491                                                        return '.$strcmp_fn.'($b->'.$field.', $a->'.$field.');
1492                                                }else if(is_array($a) && is_array($b)){
1493                                                        return '.$strcmp_fn.'($b["'.$field.'"], $a["'.$field.'"]);
1494                                                }else return 0;
1495                                        ');
1496                                }
1497                                usort($arr, $fn);
1498                                return true;
1499                        }else{
1500                                return false;
1501                        }
1502                }       
1503
1504                function show_access_log($account_id)
1505                {       
1506                        $manager_account_lid = $GLOBALS['phpgw']->accounts->data['account_lid'];
1507                        $tmp = $this->read_acl($manager_account_lid);
1508                        $manager_context = $tmp[0]['context'];
1509                       
1510                        // Verifica se tem acesso a este modulo
1511                        if ((!$this->check_acl($manager_account_lid,'edit_users')) && (!$this->check_acl($manager_account_lid,'change_users_password')))
1512                        {
[3666]1513                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/reports/inc/access_denied.php'));
[3471]1514                        }
1515
1516                        // Le BD para pegar os [li].
1517                        $query = "select li from phpgw_access_log WHERE account_id=".$account_id." order by li desc LIMIT 1 OFFSET 0";
1518                        $GLOBALS['phpgw']->db->query($query);
1519                        while($GLOBALS['phpgw']->db->next_record())
1520                        {
1521                                $managers[] = $GLOBALS['phpgw']->db->row();
1522                        }
1523
1524                        if (count($managers))
1525                        {
1526                                // contar intervalo
1527                                 $data_atual = date("Y/m/d", time());
1528
1529                                 $data_antes = date("Y/m/d",$managers['0']['li']);
1530                               
1531                                 $datainicio=strtotime("$data_antes"); // Data de Hoje
1532                                 $datafim =strtotime("$data_atual"); // Data no próximo ano
1533                               
1534                                 $rdata =($datafim-$datainicio)/86400; //transformação do timestamp em dias
1535
1536                                $access_li = date("d/m/Y",$managers['0']['li'])."#".$rdata;
1537
1538                                return $access_li;
1539
1540                        }else{
1541
1542                                $access_li = "Nunca logou#0";
1543
1544                                return $access_li;
1545                        }
1546                }
1547
1548                function Paginate_user_logon($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null, $numacesso)
1549                {
1550                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
1551                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
1552                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
1553                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
1554                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
1555                        ldap_bind($ldap_conn,$dn,$passwd);
1556                       
1557                        $filter="(&(phpgwAccountType=u)(|(uid=*)))";
1558                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");                                                                                   
1559                                                       
1560                        foreach ($contexts as $index=>$context)
1561                        {
1562                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
1563       
1564                                $rConnection = $ldap_conn;
1565                                $rSearch = $search;
1566                                $sOrder = $Order;
1567                                $iPage = $Page;
1568                                $iPerPage = $PerPage;
1569                                $sField = $Field;
1570                                               
1571                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
1572
1573                                if ( $iPage === null || $iPerPage === null )
1574                                {
1575                                        # fetch all in one page
1576                                        $iStart = 0;
1577                                        $iEnd = $iTotalEntries - 1;
1578                                }
1579                                else
1580                                {
1581                                        # calculate range of page
1582                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
1583                                       
1584                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
1585                                        $iEnd = $iPage * $iPerPage;
1586
1587                                        if ( $sOrder === "desc" )
1588                                        {
1589                                                # revert range
1590                                                $iStart = $iTotalEntries - 1 - $iEnd;
1591                                                $iEnd = $iStart + $iPerPage - 1;
1592                                        }
1593                                }
1594                               
1595                                /********* Importante Mostra o resultado da paginação **********
1596                                var_dump( $iStart . " " . $iEnd );
1597                                ****************** Só descomentar ******************************/
1598                               
1599                                 # fetch entries
1600                            ldap_sort( $rConnection, $rSearch, $sField );
1601
1602                                $aList = array();
1603                                for (
1604                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
1605                                        $iCurrent <= $iEnd && is_resource( $rEntry );
1606                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
1607                                        )
1608                                {
1609                                        if ( $iCurrent >= $iStart )
1610                                        {
1611                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
1612                                        }
1613                                }
1614                        }
1615
1616                        ldap_close($ldap_conn);
1617
1618                        # if order is desc revert page's entries
1619                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
1620                }
1621        }
1622// ****************** fim classe Functions ***********************
1623?>
Note: See TracBrowser for help on using the repository browser.