source: trunk/expressoAdmin1_2/inc/class.db_functions.inc.php @ 9

Revision 9, 14.9 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
2define('PHPGW_INCLUDE_ROOT','../');     
3define('PHPGW_API_INC','../phpgwapi/inc');
4include_once(PHPGW_API_INC.'/class.db.inc.php');
5
6class db_functions
7{       
8        var $db;
9        var $user_id;
10       
11        function db_functions(){
12                $GLOBALS['phpgw_info']['server'] = $_SESSION['phpgw_info']['expresso']['server'];
13                $this->db = new db();
14                $this->db->Halt_On_Error = 'no';
15                $this->db->connect(
16                                $_SESSION['phpgw_info']['expresso']['server']['db_name'],
17                                $_SESSION['phpgw_info']['expresso']['server']['db_host'],
18                                $_SESSION['phpgw_info']['expresso']['server']['db_port'],
19                                $_SESSION['phpgw_info']['expresso']['server']['db_user'],
20                                $_SESSION['phpgw_info']['expresso']['server']['db_pass'],
21                                $_SESSION['phpgw_info']['expresso']['server']['db_type']
22                );             
23                $this->user_id = $_SESSION['phpgw_info']['expresso']['user']['account_id'];     
24        }
25
26        // BEGIN of functions.
27        function read_acl($account_lid)
28        {
29                $query = "SELECT * FROM phpgw_expressoadmin WHERE manager_lid = '" . $account_lid . "'";
30                $this->db->query($query);
31                while($this->db->next_record())
32                        $result[] = $this->db->row();
33                return $result;
34        }
35       
36        function get_sectors($params)
37        {
38                $organization = strtolower($params['organization']);
39               
40                $result = array();
41                // Pesquisa no BD os nomes setores no tabela phpgw_expressoadmin_sectors.
42                $query = "SELECT sector FROM phpgw_expressoadmin_sectors WHERE organization='$organization' ORDER by sector ASC";
43
44        if (!$this->db->query($query))
45                return 'Erro em get_sectors:' . pg_last_error();
46
47                while($this->db->next_record())
48                        $result[] = $this->db->row();
49
50                return $result;
51        }
52
53        function get_next_id()
54        {
55                // Busco o ID dos accounts
56                $query_accounts = "SELECT id FROM phpgw_nextid WHERE appname = 'accounts'";
57        if (!$this->db->query($query_accounts))
58                return 'Erro em get_next_id:' . pg_last_error();
59                while($this->db->next_record())
60                        $result_accounts[] = $this->db->row();
61                $accounts_id = $result_accounts[0]['id'];
62               
63                // Busco o ID dos groups
64                $query_groups = "SELECT id FROM phpgw_nextid WHERE appname = 'groups'";
65        if (!$this->db->query($query_groups))
66                return 'Erro em get_next_id:' . pg_last_error();
67                while($this->db->next_record())
68                        $result_groups[] = $this->db->row();
69                $groups_id = $result_groups[0]['id'];
70               
71                //Retorna o maior dos ID's
72                if ($accounts_id >= $groups_id)
73                        return $accounts_id;
74                else
75                        return $groups_id;
76                }
77       
78        function increment_id($id, $type)
79        {
80                $sql = "UPDATE phpgw_nextid set id = '".$id."' WHERE appname = '" . $type . "'";
81                if (!$this->db->query($sql))
82                        return 'Erro em increment_id:' . pg_last_error();
83                else
84                        return true;
85        }
86       
87        function add_user2group($gidnumber, $uidnumber)
88        {
89                $query = "SELECT acl_location FROM phpgw_acl WHERE acl_appname = 'phpgw_group' AND acl_location = '" . $gidnumber . "' AND acl_account = '" . $uidnumber . "'";
90                if (!$this->db->query($query))
91                {
92                        $result['status'] = false;
93                        $result['msg'] = 'Erro em add_user2group:' . pg_last_error();
94                        return $result;
95                }
96                while($this->db->next_record())
97                        $user_in_group[] = $this->db->row();
98               
99                if (count($user_in_group) == 0)
100                {
101                        $sql = "INSERT INTO phpgw_acl (acl_appname, acl_location, acl_account, acl_rights) "
102                        . "VALUES('phpgw_group','" . $gidnumber . "','" . $uidnumber . "','1')";
103                        if (!$this->db->query($sql))
104                        {
105                                $result['status'] = false;
106                                $result['msg'] = 'Erro em add_user2group:' . pg_last_error();
107                                return $result;
108                        }
109                }
110                $result['status'] = true;
111                return $result;
112        }
113
114        function remove_user2group($gidnumber, $uidnumber)
115        {
116                $query = "SELECT acl_location FROM phpgw_acl WHERE acl_appname = 'phpgw_group' AND acl_location = '" . $gidnumber . "' AND acl_account = '" . $uidnumber . "'";
117                if (!$this->db->query($query))
118                {
119                        $result['status'] = false;
120                        $result['msg'] = 'Erro em add_user2group:' . pg_last_error();
121                        return $result;
122                }
123                while($this->db->next_record())
124                        $user_in_group[] = $this->db->row();
125               
126                if (count($user_in_group) > 0)
127                {
128                        $sql = "DELETE FROM phpgw_acl WHERE acl_appname = 'phpgw_group' AND acl_location = '" . $gidnumber . "' AND acl_account = '".$uidnumber."'";
129                        if (!$this->db->query($sql))
130                        {
131                                $result['status'] = false;
132                                $result['msg'] = 'Erro em add_user2group:' . pg_last_error();
133                                return $result;
134                        }
135                }
136                $result['status'] = true;
137                return $result;
138        }
139
140        function add_pref_changepassword($uidnumber)
141        {
142                $query = "SELECT * FROM phpgw_acl WHERE acl_appname = 'preferences' AND acl_location = 'changepassword' AND acl_account = '" . $uidnumber . "'";
143                if (!$this->db->query($query))
144                {
145                        $result['status'] = false;
146                        $result['msg'] = 'Erro em add_pref_changepassword:' . pg_last_error();
147                        return $result;
148                }
149                while($this->db->next_record())
150                        $user_pref_changepassword[] = $this->db->row();
151               
152                if (count($user_pref_changepassword) == 0)
153                {
154                        $sql = "INSERT INTO phpgw_acl (acl_appname, acl_location, acl_account, acl_rights) "
155                        . "VALUES('preferences','changepassword','" . $uidnumber . "','1')";
156                        if (!$this->db->query($sql))
157                        {
158                                $result['status'] = false;
159                                $result['msg'] = 'Erro em add_pref_changepassword:' . pg_last_error();
160                                return $result;
161                        }
162                }
163                $result['status'] = true;
164                return $result;
165        }       
166
167        function remove_pref_changepassword($uidnumber)
168        {
169                $query = "SELECT * FROM phpgw_acl WHERE acl_appname = 'preferences' AND acl_location = 'changepassword' AND acl_account = '" . $uidnumber . "'";
170                if (!$this->db->query($query))
171                {
172                        $result['status'] = false;
173                        $result['msg'] = 'Erro em add_pref_changepassword:' . pg_last_error();
174                        return $result;
175                }
176                while($this->db->next_record())
177                        $user_pref_changepassword[] = $this->db->row();
178               
179                if (count($user_pref_changepassword) != 0)
180                {
181                        $sql = "DELETE FROM phpgw_acl WHERE acl_appname = 'preferences' AND acl_location = 'changepassword' AND acl_account = '".$uidnumber."'";
182                        if (!$this->db->query($sql))
183                        {
184                                $result['status'] = false;
185                                $result['msg'] = 'Erro em remove_pref_changepassword:' . pg_last_error();
186                                return $result;
187                        }
188                }
189                $result['status'] = true;
190                return $result;
191        }       
192       
193        function add_id2apps($id, $apps)
194        {
195                $result['status'] = true;
196                if ($apps)
197                {
198                        foreach($apps as $app => $value)
199                        {
200                                $query = "SELECT * FROM phpgw_acl WHERE acl_appname = '".$app."' AND acl_location = 'run' AND acl_account = '" . $id . "'";
201                                if (!$this->db->query($query))
202                                {
203                                        $result['status'] = false;
204                                        $result['msg'] = 'Erro em add_id2apps: ' . pg_last_error();
205                                        return $result;
206                                }
207                               
208                                while($this->db->next_record())
209                                        $user_app[] = $this->db->row();
210                                       
211                                if (count($user_app) == 0)
212                                {
213                                        $sql = "INSERT INTO phpgw_acl (acl_appname, acl_location, acl_account, acl_rights) "
214                                        . "VALUES('".$app."','run','" . $id . "','1')";
215                                               
216                                        if (!$this->db->query($sql))
217                                        {
218                                                $result['status'] = false;
219                                                $result['msg'] = 'Erro em add_id2apps: ' . pg_last_error();
220                                                return $result;
221                                        }
222                                        else
223                                        {
224                                                $this->write_log("Adicionado aplicativo $app ao id",$id,'','','');     
225                                        }
226                                }
227                        }
228                }
229                return $result;
230        }
231
232        function remove_id2apps($id, $apps)
233        {
234                $result['status'] = true;
235                if ($apps)
236                {
237                        foreach($apps as $app => $value)
238                        {
239                                $query = "SELECT acl_location FROM phpgw_acl WHERE acl_appname = '" . $app . "' AND acl_location = 'run' AND acl_account = '" . $id . "'";
240                               
241                                if (!$this->db->query($query))
242                                {
243                                        $result['status'] = false;
244                                        $result['msg'] = 'Erro em remove_id2apps:' . pg_last_error();
245                                        return $result;
246                                }
247                                while($this->db->next_record())
248                                        $user_in_group[] = $this->db->row();
249                               
250                                if (count($user_in_group) > 0)
251                                {
252                                        $sql = "DELETE FROM phpgw_acl WHERE acl_appname = '" . $app . "' AND acl_location = 'run' AND acl_account = '".$id."'";
253                                        if (!$this->db->query($sql))
254                                        {
255                                                $result['status'] = false;
256                                                $result['msg'] = 'Erro em remove_id2apps:' . pg_last_error();
257                                                return $result;
258                                        }
259                                        else
260                                        {
261                                                $this->write_log("Removido aplicativo $app do id",$id,'','','');       
262                                        }
263                                }
264                        }
265                }
266                return $result;
267        }
268
269
270        function get_user_info($uidnumber)
271        {
272                // Groups
273                $query = "SELECT acl_location FROM phpgw_acl WHERE acl_appname = 'phpgw_group' AND acl_account = '".$uidnumber."'";
274                $this->db->query($query);
275                while($this->db->next_record())
276                        $user_app[] = $this->db->row();
277               
278                for ($i=0; $i<count($user_app); $i++)
279                        $return['groups'][] = $user_app[$i]['acl_location'];
280               
281                // ChangePassword
282                $query = "SELECT acl_rights FROM phpgw_acl WHERE acl_appname = 'preferences' AND acl_location = 'changepassword' AND acl_account = '".$uidnumber."'";
283                $this->db->query($query);
284                while($this->db->next_record())
285                        $changepassword[] = $this->db->row();
286                $return['changepassword'] = $changepassword[0]['acl_rights'];
287               
288                // Apps
289                $query = "SELECT acl_appname FROM phpgw_acl WHERE acl_account = '".$uidnumber."' AND acl_location = 'run'";
290                $this->db->query($query);
291                while($this->db->next_record())
292                        $user_apps[] = $this->db->row();
293                       
294                if ($user_apps)
295                {                       
296                        foreach ($user_apps as $app)
297                        {
298                                $return['apps'][$app['acl_appname']] = '1';
299                        }
300                }
301               
302                return $return;
303        }
304       
305        function get_group_info($gidnumber)
306        {
307                // Apps
308                $query = "SELECT acl_appname FROM phpgw_acl WHERE acl_account = '".$gidnumber."' AND acl_location = 'run'";
309                $this->db->query($query);
310                while($this->db->next_record())
311                        $group_apps[] = $this->db->row();
312               
313                if ($group_apps)
314                {                       
315                        foreach ($group_apps as $app)
316                        {
317                                $return['apps'][$app['acl_appname']] = '1';
318                        }
319                }
320               
321                // Members
322                $query = "SELECT acl_account FROM phpgw_acl WHERE acl_appname = 'phpgw_group' AND acl_location = '" . $gidnumber . "'";
323               
324                $this->db->query($query);
325                while($this->db->next_record())
326                        $group_members[] = $this->db->row();
327
328                if ($group_members)
329                {
330                        foreach ($group_members as $member)
331                        {
332                                $return['members'][] = $member['acl_account'];
333                        }
334                }
335                else
336                        $return['members'] = array();
337
338                return $return;
339        }
340       
341        function default_user_password_is_set($uid)
342        {
343                $query = "SELECT uid FROM phpgw_expressoadmin_passwords WHERE uid = '" . $uid . "'";
344                $this->db->query($query);
345                while($this->db->next_record())
346                {
347                        $userPassword[] = $this->db->row();
348                }
349                if (count($userPassword) == 0)
350                        return false;
351                else
352                        return true;
353        }
354       
355        function set_user_password($uid, $password)
356        {
357                $query = "SELECT uid FROM phpgw_expressoadmin_passwords WHERE uid = '" . $uid . "'";
358                $this->db->query($query);
359                while($this->db->next_record())
360                {
361                        $user[] = $this->db->row();
362                }
363                if (count($user) == 0)
364                {
365                        $sql = "INSERT INTO phpgw_expressoadmin_passwords (uid, password) VALUES('".$uid."','".$password."')";
366
367                        if (!$this->db->query($sql))
368                        {
369                                $result['status'] = false;
370                                $result['msg'] = 'Erro em set_user_password: ' . pg_last_error();
371                                return $result;
372                        }
373                }
374                return true;
375        }
376       
377        function get_user_password($uid)
378        {
379                $query = "SELECT password FROM phpgw_expressoadmin_passwords WHERE uid = '" . $uid . "'";
380                $this->db->query($query);
381                while($this->db->next_record())
382                {
383                        $userPassword[] = $this->db->row();
384                }
385               
386                if (count($userPassword) == 1)
387                {
388                        $sql = "DELETE FROM phpgw_expressoadmin_passwords WHERE uid = '" . $uid . "'";
389                        $this->db->query($sql);
390                        return $userPassword[0]['password'];
391                }
392                else
393                        return false;
394        }
395       
396        function delete_user($user_info)
397        {
398                // AGENDA
399                $this->db->query('SELECT cal_id FROM phpgw_cal WHERE owner ='.$user_info['uidnumber']);
400                while($this->db->next_record())
401                {
402                        $ids[] = $this->db->row();
403                }
404                if (count($ids))
405                {
406                        foreach($ids as $i => $id)
407                        {
408                                $this->db->query('DELETE FROM phpgw_cal WHERE cal_id='.$id['cal_id']);
409                                $this->db->query('DELETE FROM phpgw_cal_user WHERE cal_id='.$id['cal_id']);
410                                $this->db->query('DELETE FROM phpgw_cal_repeats WHERE cal_id='.$id['cal_id']);
411                                $this->db->query('DELETE FROM phpgw_cal_extra WHERE cal_id='.$id['cal_id']);
412                        }
413                }
414                       
415                // CONATOS pessoais e grupos.
416                $this->db->query('SELECT id_contact FROM phpgw_cc_contact WHERE id_owner ='.$user_info['uidnumber']);
417                while($this->db->next_record())
418                {
419                        $ids[] = $this->db->row();
420                }
421
422                if (count($ids))
423                {
424                        foreach($ids as $i => $id_contact)
425                        {
426                                $this->db->query('SELECT id_connection FROM phpgw_cc_contact_conns WHERE id_contact='.$id_contact['id_contact']);
427                                while($this->db->next_record())
428                                {
429                                        $id_conns[] = $this->db->row();
430                                }
431                                if (count($id_conns))
432                                {
433                                        foreach($id_conns as $j => $id_conn)
434                                        {
435                                                $this->db->query('DELETE FROM phpgw_cc_connections WHERE id_connection='.$id_conn['id_connection']);
436                                                $this->db->query('DELETE FROM phpgw_cc_contact_grps WHERE id_connection='.$id_conn['id_connection']);
437                                        }
438                                }
439                                       
440                                $this->db->query('SELECT id_address FROM phpgw_cc_contact_addrs WHERE id_contact='.$id_contact['id_contact']);
441                                while($this->db->next_record())
442                                {
443                                        $id_addresses[] = $$this->db->row();
444                                }
445                                if (count($id_addresses))
446                                {
447                                        foreach($id_addresses as $j => $id_addrs)
448                                        {
449                                                $this->db->query('DELETE FROM phpgw_cc_addresses WHERE id_address='.$id_addrs['id_address']);
450                                        }
451                                }
452                                $this->db->query('DELETE FROM phpgw_cc_contact WHERE id_contact='.$id_contact['id_contact']);
453                                $this->db->query('DELETE FROM phpgw_cc_contact_conns WHERE id_contact='.$id_contact['id_contact']);
454                                $this->db->query('DELETE FROM phpgw_cc_contact_addrs WHERE id_contact='.$id_contact['id_contact']);
455                        }
456                }
457                $this->db->query('DELETE FROM phpgw_cc_groups WHERE owner='.$user_info['uidnumber']);
458                       
459                // PREFERENCIAS
460                $this->db->query('DELETE FROM phpgw_preferences WHERE preference_owner='.$user_info['uidnumber']);
461                       
462                // ACL
463                $this->db->query('DELETE FROM phpgw_acl WHERE acl_account='.$user_info['uidnumber']);
464               
465                // Corrigir
466                $return['status'] = true;
467                return $return;
468        }
469
470        function delete_group($gidnumber)
471        {
472                // ACL
473                $this->db->query('DELETE FROM phpgw_acl WHERE acl_location='.$gidnumber);
474                $this->db->query('DELETE FROM phpgw_acl WHERE acl_account='.$gidnumber);
475               
476                // Corrigir
477                $return['status'] = true;
478                return $return;
479        }
480       
481        function write_log($action, $groupinfo='', $userinfo='', $appinfo='', $msg_log='')
482        {
483                $sql = "INSERT INTO phpgw_expressoadmin_log (date, manager, action, groupinfo, userinfo, appinfo, msg) "
484                . "VALUES('now','" . $_SESSION['phpgw_info']['expresso']['user']['account_lid'] . "','" . strtolower($action) . "','" . strtolower($groupinfo) . "','" . strtolower($userinfo) . "','" . strtolower($appinfo) . "','" .strtolower($msg_log) . "')";
485                $this->db->query($sql);
486                return;
487        }
488       
489        function get_sieve_info()
490        {
491                $this->db->query('SELECT profileID,imapenablesieve,imapsieveserver,imapsieveport FROM phpgw_emailadmin');
492               
493                $i=0;
494                while($this->db->next_record())
495                {
496                        $serverList[$i]['profileID']            = $this->db->f(0);
497                        $serverList[$i]['imapenablesieve']      = $this->db->f(1);
498                        $serverList[$i]['imapsieveserver']      = $this->db->f(2);
499                        $serverList[$i]['imapsieveport']        = $this->db->f(3);
500                        $i++;
501                }
502               
503                return $serverList;
504        }
505       
506        function get_apps($account_lid)
507        {
508                $this->db->query("SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '".$account_lid."'");
509               
510                while($this->db->next_record())
511                {
512                        $tmp = $this->db->row();
513                        $availableApps[$tmp['app']] = 'run';
514                }
515                       
516                return $availableApps;
517        }
518}
519?>
Note: See TracBrowser for help on using the repository browser.