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

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