source: branches/2.4/expressoAdmin1_2/inc/class.functions.inc.php @ 6754

Revision 6754, 35.5 KB checked in by niltonneto, 12 years ago (diff)

Ticket #0000 - Copiadas as alterações do Trunk. Versão final da 2.4.1.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**********************************************************************************\
3        * Expresso Administração                                                                                              *
4        * by Joao Alfredo Knopik Junior (joao.alfredo@gmail.com, jakjr@celepar.pr.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                );
26               
27                var $nextmatchs;
28                var $sectors_list = array();
29                var $current_config;
30               
31                function functions()
32                {
33                        $this->db_functions = new db_functions;
34                        $GLOBALS['phpgw']->db = $this->db_functions->db;
35                       
36                        //$c = CreateObject('phpgwapi.config','expressoAdmin1_2');
37                        $c = new config;
38                        $c->read_repository();
39                        $this->current_config = $c->config_data;
40                }
41               
42                // Account and type of access. Return: Have access ? (true/false)
43                function check_acl($account_lid, $access)
44                {
45                       
46
47                        $array_acl =  $this->db_functions->read_acl($account_lid);
48                       
49                        switch($access)
50                        {
51                                case list_users:
52                                        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'] )
53                                                return true;
54                                        break;
55                                case list_groups:
56                                        if ($array_acl['acl_add_groups'] || $array_acl['acl_edit_groups'] || $array_acl['acl_delete_groups'])
57                                                return true;
58                                        break;
59                                case list_maillists:
60                                        if ($array_acl['acl_add_maillists'] || $array_acl['acl_edit_maillists'] || $array_acl['acl_delete_maillists'])
61                                                return true;
62                                        break;
63                                case list_sectors:
64                                        if ($array_acl['acl_create_sectors'] || $array_acl['acl_edit_sectors'] || $array_acl['acl_delete_sectors'])
65                                                return true;
66                                        break;
67                                case list_computers:
68                                        if ($array_acl['acl_create_computers'] || $array_acl['acl_edit_computers'] || $array_acl['acl_delete_computers'])
69                                                return true;
70                                        break;
71
72                                case display_groups:
73                                        if ( $array_acl['acl_edit_users'] || $array_acl['acl_view_users'] || ($array_acl['acl_edit_sambausers_attributes'] && ($this->current_config['expressoAdmin_samba_support'] == 'true')) )
74                                                return true;
75                                        break;
76                                case display_emailconfig:
77                                        if ($array_acl['acl_edit_users'] || $array_acl['acl_view_users'])
78                                                return true;
79                                        break;
80                                case display_applications:
81                                        if ($array_acl['acl_edit_users'] || $array_acl['acl_view_users'])
82                                                return true;
83                                        break;
84                                case display_emaillists:
85                                        if ($array_acl['acl_edit_users'] || $array_acl['acl_view_users'])
86                                                return true;
87                                        break;
88
89                                case list_institutional_accounts:
90                                        if ($array_acl['acl_add_institutional_accounts'] || $array_acl['acl_edit_institutional_accounts'] || $array_acl['acl_delete_institutional_accounts'])
91                                                return true;
92                                        break;
93                                case list_shared_accounts:
94                                        if ($array_acl['acl_add_shared_accounts'] || $array_acl['acl_edit_shared_accounts'] || $array_acl['acl_delete_shared_accounts'])
95                                                return true;
96                                        break;
97                case configurations:
98                                        if ($array_acl['acl_active_blocking_sending_email_to_shared_accounts'] || $array_acl['acl_add_blocking_sending_email_to_shared_accounts_exception'] || $array_acl['acl_edit_and_remove_blocking_sending_email_to_shared_accounts_exception'] || $array_acl['acl_edit_maximum_number_of_recipients_generally'] || $array_acl['acl_add_maximum_number_of_recipients_by_user'] || $array_acl['acl_edit_and_remove_maximum_number_of_recipients_by_user'] || $array_acl['acl_add_maximum_number_of_recipients_by_group'] || $array_acl['acl_edit_and_remove_maximum_number_of_recipients_by_group'])
99                                                return true;
100                                        break;
101                                case messages_size:
102                                        if($array_acl['acl_add_messages_size_rule'] || $array_acl['acl_edit_messages_size_rule'] || $array_acl['acl_remove_messages_size_rule'])
103                                                return true;
104                                        break;
105
106                                default:
107                                        return $array_acl["acl_$access"];
108                        }
109                        return false;
110                }
111
112                /* OLD FUNCTION
113                function check_acl($account_lid, $access)
114                {
115                        $acl = $this->read_acl($account_lid);
116                        $array_acl = $this->make_array_acl($acl['acl']);
117                       
118                        //What access ?? In the IF, verify if have access.
119                        switch($access)
120                        {
121                                case list_users:
122                                        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])
123                                                return true;
124                                        break;
125                                case add_users:
126                                        if ($array_acl[acl_add_users])
127                                                return true;
128                                        break;
129                                case edit_users:
130                                        if ($array_acl[acl_edit_users])
131                                                return true;
132                                        break;
133                                case delete_users:
134                                        if ($array_acl[acl_delete_users])
135                                                return true;
136                                        break;
137                                case rename_users:
138                                        if ($array_acl[acl_rename_users])
139                                                return true;
140                                        break;
141                                case view_users:
142                                        if ($array_acl[acl_view_users])
143                                                return true;
144                                        break;
145                                case edit_users_picture:
146                                        if ($array_acl[acl_edit_users_picture])
147                                                return true;
148                                        break;
149                                case manipulate_corporative_information:
150                                        if ($array_acl[acl_manipulate_corporative_information])
151                                                return true;
152                                        break;
153                                case change_users_password:
154                                        if ($array_acl[acl_change_users_password])
155                                                return true;
156                                        break;
157                                case change_users_quote:
158                                        if ($array_acl[acl_change_users_quote])
159                                                return true;
160                                        break;
161                                case set_user_default_password:
162                                        if ($array_acl[acl_set_user_default_password])
163                                                return true;
164                                        break;
165                                case empty_user_inbox:
166                                        if (($array_acl[acl_empty_user_inbox]) && ($array_acl[acl_edit_users]))
167                                                return true;
168                                        break;
169                                case edit_sambausers_attributes:                                case list_maillists:
170                                        if ($array_acl[acl_add_maillists] || $array_acl[acl_edit_maillists] || $array_acl[acl_delete_maillists])
171                                                return true;
172                                        break;
173
174                                        if ($array_acl[acl_edit_sambausers_attributes])
175                                                return true;
176                                        break;
177                                case edit_sambadomains:
178                                        if ($array_acl[acl_edit_sambadomains])
179                                                return true;
180                                        break;
181                               
182                                case list_groups:
183                                        if ($array_acl[acl_add_groups] || $array_acl[acl_edit_groups] || $array_acl[acl_delete_groups])
184                                                return true;
185                                        break;
186                                case add_groups:
187                                        if ($array_acl[acl_add_groups])
188                                                return true;
189                                        break;
190                                case edit_groups:
191                                        if ($array_acl[acl_edit_groups])
192                                                return true;
193                                        break;
194                                case delete_groups:
195                                        if ($array_acl[acl_delete_groups])
196                                                return true;
197                                        break;
198                                case edit_email_groups:
199                                        if ($array_acl[acl_edit_email_groups])
200                                                return true;
201                                        break;
202                               
203                                case list_maillists:
204                                        if ($array_acl[acl_add_maillists] || $array_acl[acl_edit_maillists] || $array_acl[acl_delete_maillists])
205                                                return true;
206                                        break;
207                                case add_maillists:
208                                        if ($array_acl[acl_add_maillists])
209                                                return true;
210                                        break;
211                                case edit_maillists:
212                                        if ($array_acl[acl_edit_maillists])
213                                                return true;
214                                        break;
215                                case delete_maillists:
216                                        if ($array_acl[acl_delete_maillists])
217                                                return true;
218                                        break;
219
220                                case list_sectors:
221                                        if ($array_acl[acl_create_sectors] || $array_acl[acl_edit_sectors] || $array_acl[acl_delete_sectors])
222                                                return true;
223                                        break;
224                                case create_sectors:
225                                        if ($array_acl[acl_create_sectors])
226                                                return true;
227                                        break;
228                                case edit_sectors:
229                                        if ($array_acl[acl_edit_sectors])
230                                                return true;
231                                        break;
232                                case delete_sectors:
233                                        if ($array_acl[acl_delete_sectors])
234                                                return true;
235                                        break;
236
237                                case view_global_sessions:
238                                        if ($array_acl[acl_view_global_sessions])
239                                                return true;
240                                        break;
241
242                                case list_computers:
243                                        if ($array_acl[acl_create_computers] || $array_acl[acl_edit_computers] || $array_acl[acl_delete_computers])
244                                                return true;
245                                        break;
246                                case create_computers:
247                                        if ($array_acl[acl_create_computers])
248                                                return true;
249                                        break;
250                                case edit_computers:
251                                        if ($array_acl[acl_edit_computers])
252                                                return true;
253                                        break;
254                                case delete_computers:
255                                        if ($array_acl[acl_delete_computers])
256                                                return true;
257                                        break;
258
259                                case view_logs:
260                                        if ($array_acl[acl_view_logs])
261                                                return true;
262                                        break;
263                       
264                                case display_groups:
265                                        if ( $array_acl[acl_edit_users] || $array_acl[acl_view_users] || ($array_acl[acl_edit_sambausers_attributes] && ($this->current_config['expressoAdmin_samba_support'] == 'true')) )
266                                                return true;
267                                        break;
268                                case display_emailconfig:
269                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
270                                                return true;
271                                        break;
272                                case display_applications:
273                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
274                                                return true;
275                                        break;
276                                case display_emaillists:
277                                        if ($array_acl[acl_edit_users] || $array_acl[acl_view_users])
278                                                return true;
279                                        break;
280
281                                default:
282                                        return $array_acl["acl_$access"];
283                        }
284                        return false;
285                }
286                */
287               
288                // Read acl from db
289                function read_acl($account_lid)
290                {
291                        $acl = $this->db_functions->read_acl($account_lid);
292                        return $acl;
293                        }
294                       
295               
296                function get_inactive_users($contexts) {
297                        $retorno = array();
298                        $tempUsers = array();
299                        //Pego no LDAP todos os usuários dos contextos em questão.
300                        $usuariosLdap = $this->get_list('accounts','',$contexts);
301                        foreach($usuariosLdap as $usuarioLdap) {
302                                $tempUsers[$usuarioLdap["account_id"]] = $usuarioLdap["account_lid"];
303                        }
304                        $ids = implode(",",array_keys($tempUsers)); //Consigo a lista de uids daquele contexto para mandar na query para o banco.
305                       
306                        //Pego nas configurações do expresso o número de dias necessários para inatividade.
307                        $timeToExpire = $GLOBALS['phpgw_info']['server']['time_to_account_expires'];
308                       
309                       
310                        $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.
311                        $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)";
312
313                        $GLOBALS['phpgw']->db->query($query);
314                        while($GLOBALS['phpgw']->db->next_record())
315                        {
316                                $result = $GLOBALS['phpgw']->db->row();
317                                array_push($retorno,array("uidNumber"=>$result["account_id"],"login"=> $tempUsers[$result["account_id"]],"li"=>$result["last_login"]));
318                        }
319                       
320                        return $retorno;
321                }
322
323                function safeBitCheck($number,$comparison)
324                {
325                $binNumber = base_convert($number,10,2);
326                $binComparison = strrev(base_convert($comparison,10,2));
327                        $str = strlen($binNumber);
328               
329                if ( ($str <= strlen($binComparison)) && ($binComparison{$str-1}==="1") )
330                        return '1';
331                else
332                        return '0';
333                }
334               
335                function get_list($type, $query, $contexts)
336                {
337                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
338                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
339                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
340                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
341                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
342                        ldap_bind($ldap_conn,$dn,$passwd);
343                       
344                        if ($type == 'accounts')
345                        {
346                                $justthese = array("uidnumber", "uid", "cn", "mail");
347                                $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)(sn=*".$query."*)(cn=*".$query."*)(givenName=*".$query."*)(mail=$query*)(mailAlternateAddress=$query*)))";
348
349                                $tmp = array();
350                                foreach ($contexts as $index=>$context)
351                                {
352                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
353                                        $info = ldap_get_entries($ldap_conn, $search);
354                                       
355                                        for ($i=0; $i < $info['count']; $i++)
356                                        {
357                                                $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
358                                                $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
359                                                $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
360                                                $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
361                                                $sort[] = $info[$i]['uid'][0];
362                                        }
363                                }
364                                ldap_close($ldap_conn);
365                               
366                                if (count($sort))
367                                {
368                                        natcasesort($sort);
369                                        foreach ($sort as $user_uid)
370                                                $return[$user_uid] = $tmp[$user_uid];
371                                }
372                               
373                                return $return;
374                        }
375                        elseif($type == 'groups')
376                        {
377                                $filter="(&(phpgwAccountType=g)(cn=*$query*))";
378                                $justthese = array("gidnumber", "cn", "description");
379                               
380                                $tmp = array();
381                                foreach ($contexts as $index=>$context)
382                                {
383                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
384                                        $info = ldap_get_entries($ldap_conn, $search);
385                                        for ($i=0; $i < $info['count']; $i++)
386                                        {
387                                                $tmp[$info[$i]['cn'][0]]['cn']= $info[$i]['cn'][0];
388                                                $tmp[$info[$i]['cn'][0]]['description']= $info[$i]['description'][0];
389                                                $tmp[$info[$i]['cn'][0]]['gidnumber']= $info[$i]['gidnumber'][0];
390                                                $sort[] = $info[$i]['cn'][0];
391                                        }
392                                }
393                                ldap_close($ldap_conn);
394                               
395                                natcasesort($sort);
396                                foreach ($sort as $group_cn)
397                                        $return[$group_cn] = $tmp[$group_cn];
398                               
399                                return $return;
400                        }
401                        elseif($type == 'maillists')
402                        {
403                                $filter="(&(phpgwAccountType=l)(|(cn=*".$query."*)(uid=*".$query."*)(mail=*".$query."*)))";
404                                $justthese = array("uidnumber", "cn", "uid", "mail");
405
406                                $tmp = array();
407                                foreach ($contexts as $index=>$context)
408                                {
409                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
410                                        $info = ldap_get_entries($ldap_conn, $search);
411                                       
412                                        for ($i=0; $i < $info['count']; $i++)
413                                        {
414                                                $tmp[$info[$i]['uid'][0]]['uid']                = $info[$i]['uid'][0];
415                                                $tmp[$info[$i]['uid'][0]]['name']               = $info[$i]['cn'][0];
416                                                $tmp[$info[$i]['uid'][0]]['uidnumber']  = $info[$i]['uidnumber'][0];
417                                                $tmp[$info[$i]['uid'][0]]['email']              = $info[$i]['mail'][0];
418                                                $sort[] = $info[$i]['uid'][0];
419                                        }
420                                }
421                                ldap_close($ldap_conn);
422                               
423                                natcasesort($sort);
424                                foreach ($sort as $maillist_uid)
425                                        $return[$maillist_uid] = $tmp[$maillist_uid];
426                               
427                                return $return;
428                        }
429                        elseif($type == 'computers')
430                        {
431                                $filter="(&(objectClass=sambaSAMAccount)(|(sambaAcctFlags=[W          ])(sambaAcctFlags=[DW         ])(sambaAcctFlags=[I          ])(sambaAcctFlags=[S          ]))(cn=*".$query."*))";
432                                $justthese = array("cn","uidNumber","description");
433
434                                $tmp = array();
435                                foreach ($contexts as $index=>$context)
436                                {
437                                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
438                                        $info = ldap_get_entries($ldap_conn, $search);
439                                        for ($i=0; $i < $info['count']; $i++)
440                                        {
441                                                $tmp[$info[$i]['cn'][0]]['cn']                  = $info[$i]['cn'][0];
442                                                $tmp[$info[$i]['cn'][0]]['uidNumber']   = $info[$i]['uidnumber'][0];
443                                                $tmp[$info[$i]['cn'][0]]['description'] = utf8_decode($info[$i]['description'][0]);
444                                                $sort[] = $info[$i]['cn'][0];
445                                        }
446
447                                }
448                                ldap_close($ldap_conn);
449                               
450                                if (!empty($sort))
451                                {
452                                        natcasesort($sort);
453                                        foreach ($sort as $computer_cn)
454                                                $return[$computer_cn] = $tmp[$computer_cn];
455                                }
456                               
457                                return $return;
458                        }
459                }
460               
461                function get_organizations($context, $selected='', $referral=false, $show_invisible_ou=true, $master=false)
462                {
463                        $s = CreateObject('phpgwapi.sector_search_ldap');
464                        $sectors_info = $s->get_organizations($context, $selected, $referral, $show_invisible_ou, $master);
465                        return $sectors_info;
466                }
467                                 
468                /* 
469                        Funciona de maneira similar ao get_sectors_list, porém retorna a propria OU do contexto
470                        e monta o array de retorno de forma diferente, necessário para algumas mudanças implementadas
471                        no método admin.uisectors.list_sectors.
472                */
473                function get_organizations2($contexts, $selected='', $referral=false, $show_invisible_ou=true) {                 
474 
475                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
476                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
477                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
478                                                 
479                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
480                         
481                        if ($referral)
482                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 1);
483                        else
484                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
485                         
486                        ldap_bind($ldap_conn,$dn,$passwd);
487                         
488                        $justthese = array("dn","diskQuota","usersQuota","actualDiskQuota");
489                        $filter = "(ou=*)";
490                        foreach ($contexts as $context) {
491                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
492                                 
493                                ldap_sort($ldap_conn, $search, "ou");
494                                $info = ldap_get_entries($ldap_conn, $search);
495                                ldap_close($ldap_conn);
496         
497                                // Retiro o count do array info e inverto o array para ordenaçãoo.
498                                for ($i=0; $i<$info["count"]; $i++)
499                                {
500                                        $dn = $info[$i]["dn"];
501                                         
502                                        // Necessário, pq em uma busca com ldapsearch ou=*, traz tb o próprio ou. 
503                                        //if (strtolower($dn) == $context)
504                                                //continue;
505         
506                                        $array_dn = ldap_explode_dn ( $dn, 1 );
507         
508                                        $array_dn_reverse  = array_reverse ( $array_dn, true );
509         
510                                        // Retirar o indice count do array.
511                                        array_pop ( $array_dn_reverse );
512         
513                                        $inverted_dn[implode ( "#", $array_dn_reverse )] = $info[$i];
514                                }
515                        }
516                        // Ordenação por chave
517                        ksort($inverted_dn);                     
518                         
519                        // Construção do select
520                        $level = 0;
521                        $options = array();
522                        foreach ($inverted_dn as $dn=>$info_ou)
523                        {
524                $display = '';
525                                $info_retorno = array();
526                $array_dn_reverse = explode ( "#", $dn );
527                $array_dn  = array_reverse ( $array_dn_reverse, true );
528 
529                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
530 
531                if ($level == 0)
532                        $display .= '+';
533                else 
534                {
535                                        for ($i=0; $i<$level; $i++)
536                                                $display .= '---';
537                }
538 
539                reset ( $array_dn );
540                $display .= ' ' . urldecode( str_replace('\\', '%', current ( $array_dn )));
541                                 
542                                $info_retorno['display'] = $display;
543                                $info_retorno['dn'] = $info_ou['dn'];
544                                $info_retorno['diskquota'] = $info_ou['diskquota'][0];
545                                $info_retorno['usersquota'] = $info_ou['usersquota'][0];
546//                              $info_retorno['actualdiskquota'] = round($this->get_actual_disk_usage($info_ou['dn']),2);//$info_ou['actualdiskquota'][0];
547//                              $info_retorno['actualnumusers'] = $this->get_num_users($info_ou['dn']);
548                                 
549                                array_push($options,$info_retorno);
550                                 
551                }
552                        return $options;
553                }       
554                 
555                function get_info($context, $referral = false) {
556                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
557                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
558                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
559                         
560                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
561                         
562                        if ($referral)
563                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 1);
564                        else
565                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
566                         
567                        ldap_bind($ldap_conn,$dn,$passwd);
568                         
569                        $filter="(objectClass=organizationalUnit)";
570                        $search=ldap_search($ldap_conn, $context, $filter);
571                        $result = ldap_get_entries($ldap_conn, $search);
572                        return $result;
573                                 
574                }
575 
576                function get_num_users($context,$selected='', $referral=false, $show_invisible_ou=true) {
577                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
578                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
579                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
580                         
581                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
582                         
583                        if ($referral)
584                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 1);
585                        else
586                                ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
587                         
588                        ldap_bind($ldap_conn,$dn,$passwd);
589                         
590                        $justthese = array("dn");
591                        $filter = "(objectClass=inetOrgPerson)";
592                        $search=ldap_search($ldap_conn, $context, $filter, $justthese);
593                 
594                $retorno = ldap_count_entries($ldap_conn, $search);
595                        ldap_close($ldap_conn);
596                         
597                        return $retorno;
598                }
599                 
600                //Checa se existe quota para mais um usuï¿œrio no setor... se existir retorna true, senï¿œo false.               
601                function existe_quota_usuario($setor) {
602                        $num_users = $this->get_num_users($setor['dn']);
603                        //return $num_users . " --- " . $setor['usersquota'][0]
604                        if(($num_users>=$setor['usersquota'][0]) && ($setor['usersquota'][0]!=-1)) {
605                                return false;
606                        }
607                        return true;
608                }
609                 
610                //Checa se existe quota em disco para mais um usuï¿œrio no setor... se existir retorna true, senï¿œo false.
611                function existe_quota_disco($setor,$quota_novo_usuario) {
612                        settype($quota_novo_usuario,"float");           
613                        $quota_novo_usuario /= 1024; //A quota vï¿œm da interface em megabytes, deve se tornar gigabyte.
614 
615                        $nova_quota = $this->get_actual_disk_usage($setor['dn']) + $quota_novo_usuario;
616                        if(( $nova_quota >= $setor['diskquota'][0] ) && ($setor['diskquota'][0] != -1)) {
617                                return false;
618                        }
619                        return true;
620                }
621                 
622                // Soma as quotas de todos os usuï¿œrios daquele contexto.
623                function get_actual_disk_usage($context) {
624                        $quota_usada=0;
625                        $contexts = array($context);
626                        $usuarios = $this->get_list('accounts', '', $contexts);
627 
628                        $imap_functions = new imap_functions();
629                        foreach($usuarios as $usuario) {
630                                $temp = $imap_functions->get_user_info($usuario['account_lid']);
631                                if($temp['mailquota'] != -1) //Usuï¿œrio sem cota nï¿œo conta...
632                                        $quota_usada += ($temp['mailquota'] / 1024);
633                        }
634                        return $quota_usada;
635                }
636
637
638                function get_sectors($selected='', $referral=false, $show_invisible_ou=true)
639                {
640                        $s = CreateObject('phpgwapi.sector_search_ldap');
641                        $sectors_info = $s->get_sectors($selected, $referral, $show_invisible_ou);
642                        return $sectors_info;
643                }               
644 
645                // Get list of all levels, this function is used for sectors module.
646                function get_sectors_list($contexts)
647                {
648                        $a_sectors = array();
649                       
650                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
651                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
652                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
653                       
654                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
655                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
656                        ldap_bind($ldap_conn,$dn,$passwd);
657                       
658                        $justthese = array("dn");
659                        $filter = "(ou=*)";
660                       
661                        $systemName = strtolower($GLOBALS['phpgw_info']['server']['system_name']);
662                        if ($systemName != '')
663                                $filter = "(&$filter(phpgwSystem=$systemName))";
664                       
665                        foreach ($contexts as $context)
666                        {
667                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
668                        $info = ldap_get_entries($ldap_conn, $search);
669                        for ($i=0; $i<$info["count"]; $i++)
670                    {
671                        $a_sectors[] = $info[$i]['dn'];
672                    }
673                        }
674               
675                        ldap_close($ldap_conn);
676
677                        // Retiro o count do array info e inverto o array para ordenação.
678                foreach ($a_sectors as $context)
679            {
680
681
682                                $array_dn = ldap_explode_dn($context, 1 );
683                                foreach($array_dn as $key=>$value){
684                                        $array_dn[$key]=preg_replace('/\\\([0-9A-Fa-f]{2})/e', "''.chr(hexdec('\\1')).''",$value);
685                                }       
686
687                $array_dn_reverse  = array_reverse ( $array_dn, true );
688
689                                // Retirar o indice count do array.
690                                array_pop ( $array_dn_reverse );
691
692                                $inverted_dn[$context] = implode ( "#", $array_dn_reverse );
693                        }
694
695                        // Ordenação
696                        natcasesort($inverted_dn);
697                       
698                        // Construção do select
699                        $level = 0;
700                        $options = array();
701                        foreach ($inverted_dn as $dn=>$invert_ufn)
702                        {
703                $display = '';
704
705                $array_dn_reverse = explode ( "#", $invert_ufn );
706                $array_dn  = array_reverse ( $array_dn_reverse, true );
707
708                $level = count( $array_dn ) - (int)(count(explode(",", $GLOBALS['phpgw_info']['server']['ldap_context'])) + 1);
709
710                if ($level == 0)
711                        $display .= '+';
712                else
713                {
714                                        for ($i=0; $i<$level; $i++)
715                                                $display .= '---';
716                }
717
718                reset ( $array_dn );
719                $display .= ' ' . (current ( $array_dn ));
720                               
721                                $dn = trim(strtolower($dn));
722                                $options[$dn] = $display;
723                }
724            return $options;
725                }
726               
727                function exist_account_lid($account_lid)
728                {
729                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
730                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "uid=" . $account_lid);
731                        $result = ldap_get_entries($conection, $sri);
732                        return $result['count'];
733                }
734               
735                function exist_email($mail)
736                {
737                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
738                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "mail=" . $mail);
739                        $result = ldap_get_entries($conection, $sri);
740                        ldap_close($conection);
741                       
742                        if ($result['count'] == 0)
743                                return false;
744                        else
745                                return true;
746                }
747               
748                function array_invert($array)
749                {
750                        $result[] = end($array);
751                        while ($item = prev($array))
752                                $result[] = $item;
753                        return $result;
754                }
755               
756                function get_next_id()
757                {
758                        // Busco o ID dos accounts
759                        $query_accounts = "SELECT id FROM phpgw_nextid WHERE appname = 'accounts'";
760                        $GLOBALS['phpgw']->db->query($query_accounts);
761                        while($GLOBALS['phpgw']->db->next_record())
762                        {
763                                $result_accounts[] = $GLOBALS['phpgw']->db->row();
764                        }                       
765                        $accounts_id = $result_accounts[0]['id'];
766                       
767                        // Busco o ID dos groups
768                        $query_groups = "SELECT id FROM phpgw_nextid WHERE appname = 'groups'";
769                        $GLOBALS['phpgw']->db->query($query_groups);
770                        while($GLOBALS['phpgw']->db->next_record())
771                        {
772                                $result_groups[] = $GLOBALS['phpgw']->db->row();
773                        }                       
774                        $groups_id = $result_groups[0]['id'];
775                       
776                        //Retorna o maior dos ID's
777                        if ($accounts_id >= $groups_id)
778                                return $accounts_id;
779                        else
780                                return $groups_id;
781                }
782               
783                function make_list_app($account_lid, $user_applications='', $disabled='')
784                {
785                        // create list of ALL available apps
786                        $availableAppsGLOBALS = $GLOBALS['phpgw_info']['apps'];
787                       
788                        // create list of available apps for the user
789                        $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."'";
790                        $GLOBALS['phpgw']->db->query($query);
791                        while($GLOBALS['phpgw']->db->next_record())
792                        {
793                                $availableApps[] = $GLOBALS['phpgw']->db->row();
794                        }
795                       
796                        // Retira alguns modulos
797                        if (count($availableApps))
798                        {
799                                foreach ($availableApps as $key => $value)
800                                {
801                                        if ($value['app'] != 'phpgwapi')
802                                                $tmp[] = $availableApps[$key];
803                                }
804                        }
805                        $availableApps = $tmp;
806                       
807                        // Cria um array com as aplicacoes disponiveis para o manager, com as atributos das aplicacoes.
808                        $availableAppsUser = array();
809                        if (count($availableApps))
810                        {
811                                foreach($availableApps as $app => $title)
812                                {
813                                        if ($availableAppsGLOBALS[$title['app']])
814                                                $availableAppsUser[$title['app']] = $availableAppsGLOBALS[$title['app']];
815                                }
816                        }
817                       
818                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
819                        $applications_list = '';
820                        $app_col1 = '';
821                        $app_col2 = '';
822                        $app_col3 = '';
823                        $total_apps = count($availableAppsUser);
824                        $i = 0;
825                        foreach($availableAppsUser as $app => $data)
826                        {
827                                // 1 coluna
828                                if (($i +1) % 3 == 1)
829                                {
830                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
831                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
832                                        $data['title'],$app,$checked, $disabled);
833                                        if ($i == ($total_apps-1))
834                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>','#DDDDDD', $app_col1);
835                                }
836                               
837                                // 2 coluna
838                                if (($i +1) % 3 == 2)
839                                {
840                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
841                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
842                                        $data['title'],$app,$checked, $disabled);
843                                       
844                                        if ($i == ($total_apps-1))
845                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>','#DDDDDD', $app_col1,$app_col2);
846                                }
847                                // 3 coluna
848                                if (($i +1) % 3 == 0)
849                                {
850                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
851                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
852                                        $data['title'],$app,$checked, $disabled);
853                                       
854                                        // Cria nova linha
855                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>','#DDDDDD', $app_col1, $app_col2, $app_col3);                                       
856                                }
857                                $i++;
858                        }
859                        return $applications_list;
860                }
861               
862                function exist_attribute_in_ldap($dn, $attribute, $value)
863                {
864                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
865                        $search = ldap_search($connection, $dn, $attribute. "=" . $value);
866                        $result = ldap_get_entries($connection, $search);
867                        ldap_close($connection);
868                        //_debug_array($result);
869                        if ($result['count'] == 0)
870                                return false;
871                        else
872                                return true;   
873                }
874               
875                function getReturnExecuteForm(){
876                        $response = $_SESSION['response'];
877                        $_SESSION['response'] = null;
878                        return $response;
879                }
880
881                function lang($key)
882                {
883                        if ($_SESSION['phpgw_info']['expressoAdmin']['lang'][$key])
884                                return $_SESSION['phpgw_info']['expressoAdmin']['lang'][$key];
885                        else
886                                return $key . '*';
887                }
888               
889               
890                function checkCPF($cpf)
891                {
892                        $nulos = array("12345678909","11111111111","22222222222","33333333333",
893                               "44444444444","55555555555","66666666666","77777777777",
894                           "88888888888","99999999999","00000000000");
895
896                        /* formato do CPF */
897                        if (!(preg_match('/^[0-9]{3}[.][0-9]{3}[.][0-9]{3}[-][0-9]{2}$/',$cpf)))
898                                return false;
899
900                        /* Retira todos os caracteres que nao sejam 0-9 */
901                        $cpf = preg_replace('/[^0-9]/', '', $cpf);
902
903                        /*Retorna falso se houver letras no cpf */
904                        if (!(preg_match('/[0-9]/',$cpf)))
905                        return false;
906
907                        /* Retorna falso se o cpf for nulo */
908                        if( in_array($cpf, $nulos) )
909                        return false;
910
911                        /*Calcula o penúltimo dígito verificador*/
912                        $acum=0;
913                        for($i=0; $i<9; $i++)
914                        {
915                                $acum+= $cpf[$i]*(10-$i);
916                        }
917
918                        $x=$acum % 11;
919                        $acum = ($x>1) ? (11 - $x) : 0;
920                        /* Retorna falso se o digito calculado eh diferente do passado na string */
921                        if ($acum != $cpf[9]){
922                                return false;
923                        }
924                        /*Calcula o último dígito verificador*/
925                        $acum=0;
926                        for ($i=0; $i<10; $i++)
927                        {
928                                $acum+= $cpf[$i]*(11-$i);
929                        }
930
931                        $x=$acum % 11;
932                        $acum = ($x > 1) ? (11-$x) : 0;
933                        /* Retorna falso se o digito calculado eh diferente do passado na string */
934                        if ( $acum != $cpf[10])
935                        {
936                                return false;
937                        }
938                        /* Retorna verdadeiro se o cpf eh valido */
939                        return true;
940                }
941               
942                function make_lang($ram_lang)
943                {
944                        $a_lang = preg_split('/_/', $ram_lang);
945                        $a_lang_reverse  = array_reverse ( $a_lang, true );
946                        array_pop ( $a_lang_reverse );
947                        $a_lang  = array_reverse ( $a_lang_reverse, true );
948                        $a_new_lang = implode ( " ", $a_lang );
949                        return lang($a_new_lang);
950                }
951
952                function make_dinamic_lang($template_obj, $block)
953                {
954                        $tpl_vars = $template_obj->get_undefined($block);
955                        $array_langs = array();
956                       
957                        foreach ($tpl_vars as $atribute)
958                        {
959                                $lang = strstr($atribute, 'lang_');
960                                if($lang !== false)
961                                {
962                                        //$template_obj->set_var($atribute, $this->make_lang($atribute));
963                                        $array_langs[$atribute] = $this->make_lang($atribute);
964                                }
965                        }
966                        return $array_langs;
967                }
968
969     
970         
971         function normalize_calendar_acl($acl)
972         {
973             $return = '';
974
975             if($this->safeBitCheck(1, $acl))
976                $return .= '1-';
977             if($this->safeBitCheck(2, $acl))
978                $return .= '2-';
979             if($this->safeBitCheck(4, $acl))
980                $return .= '4-';
981             if($this->safeBitCheck(8, $acl))
982                $return .= '8-';
983             if($this->safeBitCheck(16, $acl))
984                $return .= '16-';
985             
986             return $return;
987        }
988}
989       
990        class sectors_object
991        {
992                var $sector_name;
993                var $sector_context;
994                var $sector_level;
995                var $sector_leaf;
996                var $sectors_list = array();
997                var $level;
998               
999                function sectors_object($sector_name, $sector_context, $sector_level, $sector_leaf)
1000                {
1001                        $this->sector_name = $sector_name;
1002                        $this->sector_context = $sector_context;
1003                        $this->sector_level = $sector_level;
1004                        $this->sector_leaf = $sector_leaf;
1005                }
1006        }       
Note: See TracBrowser for help on using the repository browser.