source: trunk/expressoAdmin1_2/inc/class.functions.inc.php @ 13

Revision 13, 17.8 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**********************************************************************************\
3        * Expresso Administração                                                                                              *
4        * by Joao Alfredo Knopik Junior (joao.alfredo@gmail.com, jakjr@celepar.pr.gov.br) *
5        * --------------------------------------------------------------------------------*
6        *  This program is free software; you can redistribute it and/or modify it                *
7        *  under the terms of the GNU General Public License as published by the                  *
8        *  Free Software Foundation; either version 2 of the License, or (at your                 *
9        *  option) any later version.                                                                                                     *
10        \**********************************************************************************/
11       
12        include_once('class.db_functions.inc.php');
13       
14        class functions
15        {
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               
30                function functions()
31                {
32                        $this->db_functions = new db_functions;
33                }
34               
35                // Account and type of access. Return: Have access ? (true/false)
36                function check_acl($account_lid, $access)
37                {
38                        $acl = $this->read_acl($account_lid);
39                        $array_acl = $this->make_array_acl($acl[0][acl]);
40                       
41                        //What access ?? In the IF, verify if have access.
42                        switch($access)
43                        {
44                                case list_users:
45                                        if ($array_acl[add_users] || $array_acl[edit_users] || $array_acl[delete_users] || $array_acl[change_users_password] || $array_acl[change_users_quote] || $array_acl[edit_sambausers_attributes])
46                                                return true;
47                                        break;
48                                case add_users:
49                                        if ($array_acl[add_users])
50                                                return true;
51                                        break;
52                                case edit_users:
53                                        if ($array_acl[edit_users])
54                                                return true;
55                                        break;
56                                case delete_users:
57                                        if ($array_acl[delete_users])
58                                                return true;
59                                        break;
60                                case rename_users:
61                                        if ($array_acl[rename_users])
62                                                return true;
63                                        break;
64                                case change_users_password:
65                                        if ($array_acl[change_users_password])
66                                                return true;
67                                        break;
68                                case change_users_quote:
69                                        if ($array_acl[change_users_quote])
70                                                return true;
71                                        break;
72                                case set_user_default_password:
73                                        if ($array_acl[set_user_default_password])
74                                                return true;
75                                        break;
76                                case edit_sambausers_attributes:
77                                        if ($array_acl[edit_sambausers_attributes])
78                                                return true;
79                                        break;
80                               
81                                case list_groups:
82                                        if ($array_acl[add_groups] || $array_acl[edit_groups] || $array_acl[delete_groups])
83                                                return true;
84                                        break;
85                                case add_groups:
86                                        if ($array_acl[add_groups])
87                                                return true;
88                                        break;
89                                case edit_groups:
90                                        if ($array_acl[edit_groups])
91                                                return true;
92                                        break;
93                                case delete_groups:
94                                        if ($array_acl[delete_groups])
95                                                return true;
96                                        break;
97                               
98                                case list_maillists:
99                                        if ($array_acl[add_maillists] || $array_acl[edit_maillists] || $array_acl[delete_maillists])
100                                                return true;
101                                        break;
102                                case add_maillists:
103                                        if ($array_acl[add_maillists])
104                                                return true;
105                                        break;
106                                case edit_maillists:
107                                        if ($array_acl[edit_maillists])
108                                                return true;
109                                        break;
110                                case delete_maillists:
111                                        if ($array_acl[delete_maillists])
112                                                return true;
113                                        break;
114
115                                case list_sectors:
116                                        if ($array_acl[create_sectors] || $array_acl[edit_sectors] || $array_acl[delete_sectors])
117                                                return true;
118                                        break;
119                                case create_sectors:
120                                        if ($array_acl[create_sectors])
121                                                return true;
122                                        break;
123                                case edit_sectors:
124                                        if ($array_acl[edit_sectors])
125                                                return true;
126                                        break;
127                                case delete_sectors:
128                                        if ($array_acl[delete_sectors])
129                                                return true;
130                                        break;
131
132                                case manage_wf_sectors:
133                                        if ($array_acl[manage_wf_sectors])
134                                                return true;
135                                        break;
136                               
137                                case view_global_sessions:
138                                        if ($array_acl[view_global_sessions])
139                                                return true;
140                                        break;
141
142                                case list_computers:
143                                        if ($array_acl[create_computers] || $array_acl[edit_computers] || $array_acl[delete_computers])
144                                                return true;
145                                        break;
146                                case create_computers:
147                                        if ($array_acl[create_computers])
148                                                return true;
149                                        break;
150                                case edit_computers:
151                                        if ($array_acl[edit_computers])
152                                                return true;
153                                        break;
154                                case delete_computers:
155                                        if ($array_acl[delete_computers])
156                                                return true;
157                                        break;
158
159                                case view_logs:
160                                        if ($array_acl[view_logs])
161                                                return true;
162                                        break;
163
164                                default:
165                                        return false;   
166                        }
167                       
168                        return false;
169                }
170               
171                // Read acl from db
172                function read_acl($account_lid)
173                {
174                        /*
175                        $query = "SELECT * FROM phpgw_expressoadmin WHERE manager_lid = '" . $account_lid . "'";
176                        $GLOBALS['phpgw']->db->query($query);
177                        while($GLOBALS['phpgw']->db->next_record())
178                        {
179                                $result[] = $GLOBALS['phpgw']->db->row();
180                        }
181                        */
182                        $result = $this->db_functions->read_acl($account_lid);
183                        $context_array = ldap_explode_dn($result[0]['context'], 1);
184                        $tmp = array_shift($context_array);
185                        $result[0]['context_display'] = implode(".", $context_array);
186                        return $result;
187                }
188               
189                // Make a array read humam
190                function make_array_acl($acl)
191                {
192                        $array_acl['add_users'] = $acl & 1;
193                        $array_acl['edit_users'] = $acl & 2;
194                        $array_acl['delete_users'] = $acl & 4;
195                        $array_acl['rename_users'] = $acl & 8388608;
196                        $array_acl['add_groups'] = $acl & 16;
197                        $array_acl['edit_groups'] = $acl & 32;
198                        $array_acl['delete_groups'] = $acl & 64;
199                        $array_acl['change_users_password'] = $acl & 128;
200                        $array_acl['change_users_quote'] = $acl & 262144;
201                        $array_acl['set_user_default_password'] = $acl & 524288;
202                        $array_acl['edit_sambausers_attributes'] = $acl & 32768;
203                        $array_acl['add_maillists'] = $acl & 256;
204                        $array_acl['edit_maillists'] = $acl & 512;
205                        $array_acl['delete_maillists'] = $acl & 1024;
206                        $array_acl['create_sectors'] = $acl & 4096;
207                        $array_acl['edit_sectors'] = $acl & 8192;
208                        $array_acl['delete_sectors'] = $acl & 16384;
209                        $array_acl['view_global_sessions'] = $acl & 65536;
210                        $array_acl['view_logs'] = $acl & 131072;
211                        $array_acl['create_computers'] = $acl & 1048576;
212                        $array_acl['edit_computers'] = $acl & 2097152;
213                        $array_acl['delete_computers'] = $acl & 4194304;
214                        return $array_acl;
215                }
216               
217                function get_list($type, $query, $context)
218                {
219                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
220                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
221                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
222                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
223                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
224                        ldap_bind($ldap_conn,$dn,$passwd);
225                       
226                        if ($type == 'accounts')
227                        {
228                               
229                                $justthese = array("uidnumber", "uid", "cn", "mail");
230                                $filter="(&(phpgwAccountType=u)(|(uid=*".$query."*)(sn=*".$query."*)(cn=*".$query."*)(givenName=*".$query."*)(mail=$query*)))";
231                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
232                                ldap_sort($ldap_conn, $search, "uid");
233                                $info = ldap_get_entries($ldap_conn, $search);
234                                ldap_close($ldap_conn);
235                               
236                                $i = 0;
237                                $tmp = array();
238                                for ($i=0; $i < $info['count']; $i++)
239                                {
240                                        $tmp[$i][account_id]                    = $info[$i]['uidnumber'][0];
241                                        $tmp[$i][account_lid]                   = $info[$i]['uid'][0];
242                                        $tmp[$i][account_cn]                    = $info[$i]['cn'][0];
243                                        $tmp[$i][account_mail]                  = $info[$i]['mail'][0];
244                                }                               
245                                return $tmp;
246                        }
247                        elseif($type == 'groups')
248                        {
249                                $filter="(&(phpgwAccountType=g)(cn=*".$query."*))";
250                                $justthese = array("gidnumber", "cn", "description");
251                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
252                                ldap_sort($ldap_conn, $search, "cn");
253                                $info = ldap_get_entries($ldap_conn, $search);                         
254                                ldap_close($ldap_conn);
255                               
256                                $i = 0;
257                                $tmp = array();
258                                for ($i=0; $i < $info['count']; $i++)
259                                {
260                                        $tmp[$i][cn]                    = $info[$i][cn][0];
261                                        $tmp[$i][description]   = $info[$i][description][0];
262                                        $tmp[$i][gidnumber]             = $info[$i][gidnumber][0];
263                                }
264                                return $tmp;
265                        }
266                        elseif($type == 'maillists')
267                        {
268                                $filter="(&(phpgwAccountType=l)(|(cn=*".$query."*)(uid=*".$query."*)(mail=*".$query."*)))";
269                                $justthese = array("uidnumber", "cn", "uid", "mail");
270                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
271                                ldap_sort($ldap_conn, $search, "uid");
272                                $info = ldap_get_entries($ldap_conn, $search);
273                                ldap_close($ldap_conn);
274                               
275                                $i = 0;
276                                $tmp = array();
277                                for ($i=0; $i < $info['count']; $i++)
278                                {
279                                        $tmp[$i]['uid']                 = $info[$i]['uid'][0];
280                                        $tmp[$i]['name']                = $info[$i]['cn'][0];
281                                        $tmp[$i]['uidnumber']   = $info[$i]['uidnumber'][0];
282                                        $tmp[$i]['email']               = $info[$i]['mail'][0];
283                                }
284                                return $tmp;
285                        }
286                        elseif($type == 'computers')
287                        {
288                                $filter="(&(objectClass=sambaSAMAccount)(|(sambaAcctFlags=[W          ])(sambaAcctFlags=[DW         ])(sambaAcctFlags=[I          ])(sambaAcctFlags=[S          ]))(cn=*".$query."*))";
289                                $justthese = array("cn","uidNumber","description");
290                                $search=ldap_search($ldap_conn, $context, $filter, $justthese);
291                                ldap_sort($ldap_conn, $search, "cn");
292                                $info = ldap_get_entries($ldap_conn, $search);
293                                ldap_close($ldap_conn);
294                                $tmp = array();
295                                for ($i=0; $i < $info['count']; $i++)
296                                {
297                                        $tmp[$i]['cn']                          = $info[$i]['cn'][0];
298                                        $tmp[$i]['uidNumber']           = $info[$i]['uidnumber'][0];
299                                        $tmp[$i]['description']         = utf8_decode($info[$i]['description'][0]);
300                                }
301                                return $tmp;
302                        }
303                }
304               
305                function get_organizations($context, $selected='')
306                {
307                        //echo $selected;
308                       
309                        $first_sector_ufn = ldap_dn2ufn($context);
310                        $first_sector_string = split(",", $first_sector_ufn);
311                       
312                        $s = CreateObject('phpgwapi.sector_search_ldap');
313                        $sectors_info = $s->sector_search($context);
314                       
315                        $sector_select = "";
316                        $sector_select .= "<option value='" .$context . "'";
317                        $sector_select .= ">".strtoupper($first_sector_string[0])."</option>\n";
318                       
319                        foreach($sectors_info as $sector)
320                        {
321                                if ($sector->sector_level == 1)
322                                {
323                                        $sector->sector_name = '+ '.$sector->sector_name;
324                                }
325                                else
326                                {
327                                        $sector_space = '';
328                                        for ($i=1; $i < $sector->sector_level; $i++)
329                                                $sector_space = '---'.$sector_space;
330                                        $sector->sector_name = $sector_space.' '.$sector->sector_name;
331                                }
332                                $sector_select .= '<option value="' . strtolower($sector->sector_context) . '"';
333                               
334                                if (trim(strtolower($sector->sector_context)) == $selected)
335                                        $sector_select .= ' selected>' . $sector->sector_name . "</option>\n";
336                                else
337                                        $sector_select .= '>' . $sector->sector_name . "</option>\n";
338                        }
339                        //$sector_select .= "</select>";
340                        return $sector_select; 
341                }
342               
343                function get_sectors($context, $selected='')
344                {
345                        $query = "SELECT sector FROM phpgw_expressoadmin_sectors WHERE organization = '" . $context . "'";
346                        $GLOBALS['phpgw']->db->query($query);
347                        while($GLOBALS['phpgw']->db->next_record())
348                        {
349                                $query_result[] = $GLOBALS['phpgw']->db->row();
350                        }
351                       
352                        if (count($query_result) > 0)
353                        {
354                                foreach ($query_result as $index=>$tmp)
355                                {
356                                        $sector = $tmp['sector'];
357                                        if ($sector == $selected)
358                                                $sector_options .= "<option value='$sector' selected>$sector</option>";
359                                        else
360                                                $sector_options .= "<option value='$sector'>$sector</option>";
361                                }
362                                return $sector_options;
363                        }
364                        else
365                                return false;
366                }
367               
368                // Get list of all levels, this function is used for sectors module.
369                function get_sectors_list($context)
370                {
371                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
372                        ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
373                        ldap_set_option($connection, LDAP_OPT_REFERRALS, 0);
374       
375                        if ($connection)
376                        {
377                                $bind=ldap_bind($connection);
378                                $filter="ou=*";
379                                $justthese = array("ou");
380                                $search=ldap_list($connection, $context, $filter, $justthese);
381                                ldap_sort($connection ,$search, "ou");
382                                $info = ldap_get_entries($connection, $search);
383                                for ($i=0; $i<$info["count"]; $i++)
384                                {
385                                        $this->level++;
386                                        $next_context[$i] = 'ou='.$info[$i]["ou"][0].','.$context;
387                                        $obj = new sectors_object($info[$i]["ou"][0], $next_context[$i], $this->level, 'False');
388                                        array_push($this->sectors_list, $obj);
389                                        $this->get_sectors_list($next_context[$i]);
390                                }
391                        }
392                        ldap_close($connection);
393                        $this->level--;
394                        return $this->sectors_list;
395                }
396               
397                function exist_account_lid($account_lid)
398                {
399                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
400                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "uid=" . $account_lid);
401                        $result = ldap_get_entries($conection, $sri);
402                        return $result['count'];
403                }
404               
405                function exist_email($mail)
406                {
407                        $conection = $GLOBALS['phpgw']->common->ldapConnect();
408                        $sri = ldap_search($conection, $GLOBALS['phpgw_info']['server']['ldap_context'], "mail=" . $mail);
409                        $result = ldap_get_entries($conection, $sri);
410                        ldap_close($conection);
411                       
412                        if ($result['count'] == 0)
413                                return false;
414                        else
415                                return true;
416                }
417               
418                function array_invert($array)
419                {
420                        $result[] = end($array);
421                        while ($item = prev($array))
422                                $result[] = $item;
423                        return $result;
424                }
425               
426                function get_next_id()
427                {
428                        // Busco o ID dos accounts
429                        $query_accounts = "SELECT id FROM phpgw_nextid WHERE appname = 'accounts'";
430                        $GLOBALS['phpgw']->db->query($query_accounts);
431                        while($GLOBALS['phpgw']->db->next_record())
432                        {
433                                $result_accounts[] = $GLOBALS['phpgw']->db->row();
434                        }                       
435                        $accounts_id = $result_accounts[0]['id'];
436                       
437                        // Busco o ID dos groups
438                        $query_groups = "SELECT id FROM phpgw_nextid WHERE appname = 'groups'";
439                        $GLOBALS['phpgw']->db->query($query_groups);
440                        while($GLOBALS['phpgw']->db->next_record())
441                        {
442                                $result_groups[] = $GLOBALS['phpgw']->db->row();
443                        }                       
444                        $groups_id = $result_groups[0]['id'];
445                       
446                        //Retorna o maior dos ID's
447                        if ($accounts_id >= $groups_id)
448                                return $accounts_id;
449                        else
450                                return $groups_id;
451                }
452               
453                function increment_id($id, $type)
454                {
455                        $sql = "UPDATE phpgw_nextid set id = '".$id."' WHERE appname = '" . $type . "'";
456                        $GLOBALS['phpgw']->db->query($sql);
457                }
458               
459                function make_list_app($account_lid, $context, $user_applications, $disabled='')
460                {
461                        // create list of ALL available apps
462                        $availableAppsGLOBALS = $GLOBALS['phpgw_info']['apps'];
463                       
464                        // create list of available apps for the user
465                        $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."' AND context = '".$context."'";
466                        $GLOBALS['phpgw']->db->query($query);
467                        while($GLOBALS['phpgw']->db->next_record())
468                        {
469                                $availableApps[] = $GLOBALS['phpgw']->db->row();
470                        }
471                       
472                        // Retira alguns modulos
473                        if (count($availableApps))
474                        {
475                                foreach ($availableApps as $key => $value)
476                                {
477                                        if ($value['app'] != 'phpgwapi')
478                                                $tmp[] = $availableApps[$key];
479                                }
480                        }
481                        $availableApps = $tmp;
482                       
483                        // Cria um array com as aplicacoes disponiveis para o manager, com as atributos das aplicacoes.
484                        $availableAppsUser = array();
485                        if (count($availableApps))
486                        {
487                                foreach($availableApps as $app => $title)
488                                {
489                                        if ($availableAppsGLOBALS[$title['app']])
490                                                $availableAppsUser[$title['app']] = $availableAppsGLOBALS[$title['app']];
491                                }
492                        }
493                       
494                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
495                        $applications_list = '';
496                        $app_col1 = '';
497                        $app_col2 = '';
498                        $app_col3 = '';
499                        $total_apps = count($availableAppsUser);
500                        $i = 0;
501                        foreach($availableAppsUser as $app => $data)
502                        {
503                                // 1 coluna
504                                if (($i +1) % 3 == 1)
505                                {
506                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
507                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
508                                        $data['title'],$app,$checked, $disabled);
509                                        if ($i == ($total_apps-1))
510                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>','#DDDDDD', $app_col1);
511                                }
512                               
513                                // 2 coluna
514                                if (($i +1) % 3 == 2)
515                                {
516                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
517                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
518                                        $data['title'],$app,$checked, $disabled);
519                                       
520                                        if ($i == ($total_apps-1))
521                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>','#DDDDDD', $app_col1,$app_col2);
522                                }
523                                // 3 coluna
524                                if (($i +1) % 3 == 0)
525                                {
526                                        $checked = $user_applications[$app] ? 'CHECKED' : '';
527                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='apps[%s]' value='1' %s %s></td>\n",
528                                        $data['title'],$app,$checked, $disabled);
529                                       
530                                        // Cria nova linha
531                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>','#DDDDDD', $app_col1, $app_col2, $app_col3);                                       
532                                }
533                                $i++;
534                        }
535                        return $applications_list;
536                }
537               
538                function exist_attribute_in_ldap($dn, $attribute, $value)
539                {
540                        $connection = $GLOBALS['phpgw']->common->ldapConnect();
541                        $search = ldap_search($connection, $dn, $attribute. "=" . $value);
542                        $result = ldap_get_entries($connection, $search);
543                        ldap_close($connection);
544                        //_debug_array($result);
545                        if ($result['count'] == 0)
546                                return false;
547                        else
548                                return true;   
549                }
550               
551                function getReturnExecuteForm(){
552                        $response = $_SESSION['response'];
553                        $_SESSION['response'] = null;
554                        return $response;
555                }
556
557                function write_log2($action, $groupinfo='', $userinfo='', $appinfo='', $msg_log='')
558                {
559                        $sql = "INSERT INTO phpgw_expressoadmin_log (date, manager, action, groupinfo, userinfo, appinfo, msg) "
560                        . "VALUES('now','" . $_SESSION['phpgw_info']['expresso']['user']['account_lid'] . "','" . strtolower($action) . "','" . strtolower($groupinfo) . "','" . strtolower($userinfo) . "','" . strtolower($appinfo) . "','" .strtolower($msg_log) . "')";
561                        $GLOBALS['phpgw']->db->query($sql);
562                        return;
563                }
564               
565        }
566       
567        class sectors_object
568        {
569                var $sector_name;
570                var $sector_context;
571                var $sector_level;
572                var $sector_leaf;
573                var $sectors_list = array();
574                var $level;
575               
576                function sectors_object($sector_name, $sector_context, $sector_level, $sector_leaf)
577                {
578                        $this->sector_name = $sector_name;
579                        $this->sector_context = $sector_context;
580                        $this->sector_level = $sector_level;
581                        $this->sector_leaf = $sector_leaf;
582                }
583        }
Note: See TracBrowser for help on using the repository browser.