source: trunk/phpgwapi/inc/class.config.inc.php @ 370

Revision 370, 6.0 KB checked in by niltonneto, 16 years ago (diff)

Ver ocorrência #270 do Trac.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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                                /*jakjr: ExpressoLivre does not use this.
77                                if($this->appname == 'phpgwapi')
78                                {
79                                        $this->db->query("delete from phpgw_app_sessions where sessionid = '0' and loginid = '0' and app = '".$this->appname."' and location = 'config'",__LINE__,__FILE__);
80                                }*/
81                                foreach($this->config_data as $name => $value)
82                                {
83                                        $this->save_value($name,$value);
84                                }
85                                foreach($this->read_data as $name => $value)
86                                {
87                                        if (!isset($this->config_data[$name]))  // has been deleted
88                                        {
89                                                $this->db->query("DELETE FROM phpgw_config WHERE config_app='$this->appname' AND config_name='$name'",__LINE__,__FILE__);
90                                        }
91                                }
92                                $this->db->unlock();
93                        }
94                        $this->read_data = $this->config_data;
95                }
96
97                /*!
98                @function save_value
99                @abstract updates or insert a single config-value
100                @param $name string name of the config-value
101                @param $value mixed content
102                @param $app string app-name, defaults to $this->appname set via the constructor
103                */
104                function save_value($name,$value,$app=False)
105                {
106                        //echo "<p>config::save_value('$name','".print_r($value,True)."','$app')</p>\n";
107                        if (!$app || $app == $this->appname)
108                        {
109                                $app = $this->appname;
110                                $this->config_data[$name] = $value;
111                        }
112                        $name = $this->db->db_addslashes($name);
113
114                        if ($app == $this->appname && $this->read_data[$name] == $value)
115                        {
116                                return True;    // no change ==> exit
117                        }
118                        $this->db->query($sql="select * from phpgw_config where config_app='$app' AND config_name='$name'",__LINE__,__FILE__);
119                        if ($this->db->next_record())
120                        {
121                                $value_read = @unserialize($this->db->f('config_value'));
122                                if (!$value_read)
123                                {
124                                        $value_read = $this->db->f('config_value');
125                                }
126                                if ($value_read == $value)
127                                {
128                                        return True;    // no change ==> exit
129                                }
130                                $update = True;
131                        }
132                        //echo "<p>config::save_value('$name','".print_r($value,True)."','$app')</p>\n";
133
134                        if(is_array($value))
135                        {
136                                $value = serialize($value);
137                        }
138                        $value = $this->db->db_addslashes($value);
139
140                        $query = $update ? "UPDATE phpgw_config SET config_value='$value' WHERE config_app='$app' AND config_name='$name'" :
141                                "INSERT INTO phpgw_config (config_app,config_name,config_value) VALUES ('$app','$name','$value')";
142
143                        return $this->db->query($query,__LINE__,__FILE__);
144                }
145
146                /*!
147                @function delete_repository
148                @abstract deletes the whole repository for $this->appname, appname has to be set via the constructor
149                */
150                function delete_repository()
151                {
152                        $this->db->query("delete from phpgw_config where config_app='" . $this->appname . "'",__LINE__,__FILE__);
153                }
154
155                /*!
156                @function delete_value
157                @abstract deletes a single value from the repository, you need to call save_repository after
158                @param $variable_name string name of the config
159                */
160                function delete_value($variable_name)
161                {
162                        unset($this->config_data[$variable_name]);
163                }
164                /*!
165                @function value
166                @abstract sets a single value in the repositry, you need to call save_repository after
167                @param $variable_name string name of the config
168                @param $variable_data mixed the content
169                */
170                function value($variable_name,$variable_data)
171                {
172                        $this->config_data[$variable_name] = $variable_data;
173                }
174        }
175?>
Note: See TracBrowser for help on using the repository browser.