1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Application configuration in a centralized location * |
---|
4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
5 | * Copyright (C) 2000, 2001 Joseph Engo * |
---|
6 | * -------------------------------------------------------------------------* |
---|
7 | * This library is part of the eGroupWare API * |
---|
8 | * http://www.egroupware.org/api * |
---|
9 | * ------------------------------------------------------------------------ * |
---|
10 | * This library is free software; you can redistribute it and/or modify it * |
---|
11 | * under the terms of the GNU Lesser General Public License as published by * |
---|
12 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
13 | * or any later version. * |
---|
14 | * This library is distributed in the hope that it will be useful, but * |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
17 | * See the GNU Lesser General Public License for more details. * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
21 | \**************************************************************************/ |
---|
22 | |
---|
23 | |
---|
24 | class config |
---|
25 | { |
---|
26 | var $db; |
---|
27 | var $appname; |
---|
28 | var $config_data; // actual config-data |
---|
29 | var $read_data; // config-data as read from db |
---|
30 | |
---|
31 | function config($appname = '') |
---|
32 | { |
---|
33 | if (! $appname) |
---|
34 | { |
---|
35 | $appname = $GLOBALS['phpgw_info']['flags']['currentapp']; |
---|
36 | } |
---|
37 | |
---|
38 | $this->db = is_object($GLOBALS['phpgw']->db) ? $GLOBALS['phpgw']->db : $GLOBALS['phpgw_setup']->db; |
---|
39 | $this->appname = $appname; |
---|
40 | } |
---|
41 | |
---|
42 | /*! |
---|
43 | @function read_repository |
---|
44 | @abstract reads the whole repository for $this->appname, appname has to be set via the constructor |
---|
45 | @returns the whole config-array for that app |
---|
46 | */ |
---|
47 | function read_repository() |
---|
48 | { |
---|
49 | $this->config_data = array(); |
---|
50 | |
---|
51 | $this->db->query("select * from phpgw_config where config_app='" . $this->appname . "'",__LINE__,__FILE__); |
---|
52 | while ($this->db->next_record()) |
---|
53 | { |
---|
54 | $test = @unserialize($this->db->f('config_value')); |
---|
55 | if($test) |
---|
56 | { |
---|
57 | $this->config_data[$this->db->f('config_name')] = $test; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | $this->config_data[$this->db->f('config_name')] = $this->db->f('config_value'); |
---|
62 | } |
---|
63 | } |
---|
64 | return $this->read_data = $this->config_data; |
---|
65 | } |
---|
66 | |
---|
67 | /*! |
---|
68 | @function save_repository |
---|
69 | @abstract updates the whole repository for $this->appname, you have to call read_repository() before (!) |
---|
70 | */ |
---|
71 | function save_repository() |
---|
72 | { |
---|
73 | if (is_array($this->config_data)) |
---|
74 | { |
---|
75 | $this->db->lock(array('phpgw_config','phpgw_app_sessions')); |
---|
76 | if($this->appname == 'phpgwapi') |
---|
77 | { |
---|
78 | $this->db->query("delete from phpgw_app_sessions where sessionid = '0' and loginid = '0' and app = '".$this->appname."' and location = 'config'",__LINE__,__FILE__); |
---|
79 | } |
---|
80 | foreach($this->config_data as $name => $value) |
---|
81 | { |
---|
82 | $this->save_value($name,$value); |
---|
83 | } |
---|
84 | foreach($this->read_data as $name => $value) |
---|
85 | { |
---|
86 | if (!isset($this->config_data[$name])) // has been deleted |
---|
87 | { |
---|
88 | $this->db->query("DELETE FROM phpgw_config WHERE config_app='$this->appname' AND config_name='$name'",__LINE__,__FILE__); |
---|
89 | } |
---|
90 | } |
---|
91 | $this->db->unlock(); |
---|
92 | } |
---|
93 | $this->read_data = $this->config_data; |
---|
94 | } |
---|
95 | |
---|
96 | /*! |
---|
97 | @function save_value |
---|
98 | @abstract updates or insert a single config-value |
---|
99 | @param $name string name of the config-value |
---|
100 | @param $value mixed content |
---|
101 | @param $app string app-name, defaults to $this->appname set via the constructor |
---|
102 | */ |
---|
103 | function save_value($name,$value,$app=False) |
---|
104 | { |
---|
105 | //echo "<p>config::save_value('$name','".print_r($value,True)."','$app')</p>\n"; |
---|
106 | if (!$app || $app == $this->appname) |
---|
107 | { |
---|
108 | $app = $this->appname; |
---|
109 | $this->config_data[$name] = $value; |
---|
110 | } |
---|
111 | $name = $this->db->db_addslashes($name); |
---|
112 | |
---|
113 | if ($app == $this->appname && $this->read_data[$name] == $value) |
---|
114 | { |
---|
115 | return True; // no change ==> exit |
---|
116 | } |
---|
117 | $this->db->query($sql="select * from phpgw_config where config_app='$app' AND config_name='$name'",__LINE__,__FILE__); |
---|
118 | if ($this->db->next_record()) |
---|
119 | { |
---|
120 | $value_read = @unserialize($this->db->f('config_value')); |
---|
121 | if (!$value_read) |
---|
122 | { |
---|
123 | $value_read = $this->db->f('config_value'); |
---|
124 | } |
---|
125 | if ($value_read == $value) |
---|
126 | { |
---|
127 | return True; // no change ==> exit |
---|
128 | } |
---|
129 | $update = True; |
---|
130 | } |
---|
131 | //echo "<p>config::save_value('$name','".print_r($value,True)."','$app')</p>\n"; |
---|
132 | |
---|
133 | if(is_array($value)) |
---|
134 | { |
---|
135 | $value = serialize($value); |
---|
136 | } |
---|
137 | $value = $this->db->db_addslashes($value); |
---|
138 | |
---|
139 | $query = $update ? "UPDATE phpgw_config SET config_value='$value' WHERE config_app='$app' AND config_name='$name'" : |
---|
140 | "INSERT INTO phpgw_config (config_app,config_name,config_value) VALUES ('$app','$name','$value')"; |
---|
141 | |
---|
142 | return $this->db->query($query,__LINE__,__FILE__); |
---|
143 | } |
---|
144 | |
---|
145 | /*! |
---|
146 | @function delete_repository |
---|
147 | @abstract deletes the whole repository for $this->appname, appname has to be set via the constructor |
---|
148 | */ |
---|
149 | function delete_repository() |
---|
150 | { |
---|
151 | $this->db->query("delete from phpgw_config where config_app='" . $this->appname . "'",__LINE__,__FILE__); |
---|
152 | } |
---|
153 | |
---|
154 | /*! |
---|
155 | @function delete_value |
---|
156 | @abstract deletes a single value from the repository, you need to call save_repository after |
---|
157 | @param $variable_name string name of the config |
---|
158 | */ |
---|
159 | function delete_value($variable_name) |
---|
160 | { |
---|
161 | unset($this->config_data[$variable_name]); |
---|
162 | } |
---|
163 | /*! |
---|
164 | @function value |
---|
165 | @abstract sets a single value in the repositry, you need to call save_repository after |
---|
166 | @param $variable_name string name of the config |
---|
167 | @param $variable_data mixed the content |
---|
168 | */ |
---|
169 | function value($variable_name,$variable_data) |
---|
170 | { |
---|
171 | $this->config_data[$variable_name] = $variable_data; |
---|
172 | } |
---|
173 | } |
---|
174 | ?> |
---|