source: companies/celepar/expressoAdmin1_2/inc/class.functions.inc.php @ 763

Revision 763, 26.4 KB checked in by niltonneto, 15 years ago (diff)

Importação inicial do Expresso da Celepar

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