source: sandbox/2.5.1-evolucao/expressoAdmin1_2/inc/class.functions.inc.php @ 8234

Revision 8234, 35.8 KB checked in by angelo, 11 years ago (diff)

Ticket #3491 - Compatibilizar Expresso com novas versoes do PHP

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