2001-05-02 10:02:32 +02:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-05-05 14:06:13 +02:00
|
|
|
* eGroupWare API - Application configuration in a centralized location *
|
2001-05-02 10:02:32 +02:00
|
|
|
* This file written by Joseph Engo <jengo@phpgroupware.org> *
|
|
|
|
* Copyright (C) 2000, 2001 Joseph Engo *
|
|
|
|
* -------------------------------------------------------------------------*
|
2004-05-05 14:06:13 +02:00
|
|
|
* This library is part of the eGroupWare API *
|
|
|
|
* http://www.egroupware.org/api *
|
2001-05-02 10:02:32 +02:00
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
class config
|
|
|
|
{
|
|
|
|
var $db;
|
|
|
|
var $appname;
|
2003-11-08 23:30:06 +01:00
|
|
|
var $config_data; // actual config-data
|
|
|
|
var $read_data; // config-data as read from db
|
2005-11-02 12:45:52 +01:00
|
|
|
var $table = 'egw_config';
|
2001-05-02 10:02:32 +02:00
|
|
|
|
|
|
|
function config($appname = '')
|
|
|
|
{
|
|
|
|
if (! $appname)
|
|
|
|
{
|
2005-07-22 11:05:01 +02:00
|
|
|
$appname = $GLOBALS['egw_info']['flags']['currentapp'];
|
|
|
|
}
|
|
|
|
if (is_object($GLOBALS['egw']->db))
|
|
|
|
{
|
|
|
|
$this->db = clone($GLOBALS['egw']->db);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db = clone($GLOBALS['egw_setup']->db);
|
2001-05-02 10:02:32 +02:00
|
|
|
}
|
2004-08-04 00:52:55 +02:00
|
|
|
$this->db->set_app('phpgwapi');
|
2001-05-02 10:02:32 +02:00
|
|
|
$this->appname = $appname;
|
|
|
|
}
|
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
|
|
|
* reads the whole repository for $this->appname, appname has to be set via the constructor
|
|
|
|
*
|
|
|
|
* the whole config-array for that app
|
|
|
|
*/
|
2001-05-02 10:02:32 +02:00
|
|
|
function read_repository()
|
|
|
|
{
|
2003-11-08 23:30:06 +01:00
|
|
|
$this->config_data = array();
|
|
|
|
|
2004-08-04 00:52:55 +02:00
|
|
|
$this->db->select($this->table,'*',array('config_app'=>$this->appname),__LINE__,__FILE__);
|
2001-05-02 10:02:32 +02:00
|
|
|
while ($this->db->next_record())
|
|
|
|
{
|
2002-04-13 05:34:23 +02:00
|
|
|
$test = @unserialize($this->db->f('config_value'));
|
2005-11-08 09:12:30 +01:00
|
|
|
if(is_array($test))
|
2002-04-13 05:34:23 +02:00
|
|
|
{
|
|
|
|
$this->config_data[$this->db->f('config_name')] = $test;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->config_data[$this->db->f('config_name')] = $this->db->f('config_value');
|
|
|
|
}
|
2001-05-02 10:02:32 +02:00
|
|
|
}
|
2004-04-04 12:02:25 +02:00
|
|
|
return $this->read_data = $this->config_data;
|
2001-05-02 10:02:32 +02:00
|
|
|
}
|
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
|
|
|
* updates the whole repository for $this->appname, you have to call read_repository() before (!)
|
|
|
|
*/
|
2001-05-02 10:02:32 +02:00
|
|
|
function save_repository()
|
|
|
|
{
|
2003-11-08 23:30:06 +01:00
|
|
|
if (is_array($this->config_data))
|
2001-05-02 10:02:32 +02:00
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$this->db->lock(array($this->table));
|
2003-11-03 15:25:50 +01:00
|
|
|
foreach($this->config_data as $name => $value)
|
2001-06-13 17:44:56 +02:00
|
|
|
{
|
2003-11-03 15:25:50 +01:00
|
|
|
$this->save_value($name,$value);
|
2001-06-13 17:44:56 +02:00
|
|
|
}
|
2003-11-08 23:30:06 +01:00
|
|
|
foreach($this->read_data as $name => $value)
|
|
|
|
{
|
|
|
|
if (!isset($this->config_data[$name])) // has been deleted
|
|
|
|
{
|
2004-08-04 00:52:55 +02:00
|
|
|
$this->db->delete($this->table,array('config_app'=>$this->appname,'config_name'=>$name),__LINE__,__FILE__);
|
2003-11-08 23:30:06 +01:00
|
|
|
}
|
|
|
|
}
|
2001-06-13 17:44:56 +02:00
|
|
|
$this->db->unlock();
|
2005-07-22 11:05:01 +02:00
|
|
|
|
2005-11-24 21:08:09 +01:00
|
|
|
if ($this->appname == 'phpgwapi' && method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited
|
2005-07-22 11:05:01 +02:00
|
|
|
{
|
|
|
|
$GLOBALS['egw']->invalidate_session_cache(); // in case egw_info is cached in the session (phpgwapi is in egw_info[server])
|
|
|
|
}
|
2001-05-02 10:02:32 +02:00
|
|
|
}
|
2003-11-08 23:30:06 +01:00
|
|
|
$this->read_data = $this->config_data;
|
2001-05-02 10:02:32 +02:00
|
|
|
}
|
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
|
|
|
* updates or insert a single config-value
|
|
|
|
*
|
|
|
|
* @param $name string name of the config-value
|
|
|
|
* @param $value mixed content
|
|
|
|
* @param $app string app-name, defaults to $this->appname set via the constructor
|
|
|
|
*/
|
2003-11-03 15:25:50 +01:00
|
|
|
function save_value($name,$value,$app=False)
|
|
|
|
{
|
|
|
|
//echo "<p>config::save_value('$name','".print_r($value,True)."','$app')</p>\n";
|
|
|
|
if (!$app || $app == $this->appname)
|
|
|
|
{
|
|
|
|
$app = $this->appname;
|
|
|
|
$this->config_data[$name] = $value;
|
|
|
|
}
|
2006-08-17 16:32:25 +02:00
|
|
|
if ($app == $this->appname && isset($this->read_data[$name]) && $this->read_data[$name] == $value)
|
2003-11-08 23:30:06 +01:00
|
|
|
{
|
|
|
|
return True; // no change ==> exit
|
|
|
|
}
|
2003-11-03 15:25:50 +01:00
|
|
|
//echo "<p>config::save_value('$name','".print_r($value,True)."','$app')</p>\n";
|
|
|
|
|
|
|
|
if(is_array($value))
|
|
|
|
{
|
|
|
|
$value = serialize($value);
|
|
|
|
}
|
2004-08-04 00:52:55 +02:00
|
|
|
return $this->db->insert($this->table,array('config_value'=>$value),array('config_app'=>$app,'config_name'=>$name),__LINE__,__FILE__);
|
2003-11-03 15:25:50 +01:00
|
|
|
}
|
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
|
|
|
* deletes the whole repository for $this->appname, appname has to be set via the constructor
|
|
|
|
*
|
|
|
|
*/
|
2001-05-02 10:02:32 +02:00
|
|
|
function delete_repository()
|
|
|
|
{
|
2005-11-02 12:45:52 +01:00
|
|
|
$this->db->delete($this->table,array('config_app' => $this->appname),__LINE__,__FILE__);
|
2001-05-02 10:02:32 +02:00
|
|
|
}
|
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
|
|
|
* deletes a single value from the repository, you need to call save_repository after
|
|
|
|
*
|
|
|
|
* @param $variable_name string name of the config
|
|
|
|
*/
|
2003-11-08 23:30:06 +01:00
|
|
|
function delete_value($variable_name)
|
|
|
|
{
|
|
|
|
unset($this->config_data[$variable_name]);
|
|
|
|
}
|
2005-11-02 12:45:52 +01:00
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
|
|
|
* sets a single value in the repositry, you need to call save_repository after
|
|
|
|
*
|
|
|
|
* @param $variable_name string name of the config
|
|
|
|
* @param $variable_data mixed the content
|
|
|
|
*/
|
2001-05-02 10:02:32 +02:00
|
|
|
function value($variable_name,$variable_data)
|
|
|
|
{
|
|
|
|
$this->config_data[$variable_name] = $variable_data;
|
|
|
|
}
|
2001-05-21 19:18:39 +02:00
|
|
|
}
|