00001 <?php
00007 include_once("core/system/JBaseObject.class.php");
00008 include_once("core/utils/JSession.class.php");
00009 include_once("core/config/ConfigPetition.class.php");
00010
00011
00012 define(CONFIG_PATH, "config/");
00013 define(STORAGE_METHOD_FILE, "storage-method.php");
00014 define(CORE_CONFIG_FILE, "core-config.php");
00015 define(CONFIGCACHE_SESSION_VAR, "james-configcache");
00016 define(CONFIG_DEFAULT_BACKEND, "DB");
00017
00041 class ConfigCache extends JBaseObject
00042 {
00043 var $system;
00044 var $systemgroup;
00045
00046 var $module;
00047 var $modulegroup;
00048
00056 function ConfigCache()
00057 {
00058
00059 $this->system = array();
00060 $this->systemgroup = array();
00061 $this->module = array();
00062 $this->modulegroup = array();
00063 }
00064
00076 function &GetInstance()
00077 {
00078 static $ob;
00079
00080 if (! isset($ob))
00081 {
00082
00083 if (JSession::ExistsVar(CONFIGCACHE_SESSION_VAR))
00084 {
00085 $ob = JSession::GetVar(CONFIGCACHE_SESSION_VAR);
00086 }
00087 else
00088 {
00089 $ob = new ConfigCache();
00090 $ob->_UpdateSession();
00091 }
00092
00093
00094
00095
00096
00097 register_shutdown_function(array(&$ob, _UpdateSession));
00098 }
00099 return $ob;
00100 }
00101
00105 function _UpdateSession()
00106 {
00107 JSession::SetVar(CONFIGCACHE_SESSION_VAR, $this);
00108 }
00109
00120 function &_ConfigBackendFactory($petition)
00121 {
00122 $backend = $this->_GetBackendMethod($petition);
00123
00124
00125
00126 if (empty($backend))
00127 $backend = CONFIG_DEFAULT_BACKEND;
00128
00129
00130
00131 $class = "Config" . $backend . "Backend";
00132 @include_once "core/config/backends/" . $class . ".class.php";
00133
00134 if (class_exists($class))
00135 {
00136 return new $class($petition);
00137 }
00138 }
00139
00149 function _GetBackendMethod($petition)
00150 {
00151
00152 include(CONFIG_PATH.STORAGE_METHOD_FILE);
00153
00154 if ($petition->GetUser() != null)
00155 $method = $UserConfig;
00156 else if ($petition->GetGroup() != null)
00157 $method = $SystemConfig["groups"];
00158 else if ($petition->GetModule() != null)
00159 $method = $SystemConfig["modules"];
00160 else
00161 $method = $SystemConfig["system"];
00162
00163 return $method;
00164 }
00165
00174 function GetPetition($petition)
00175 {
00176 $idM = $petition->GetModule();
00177 $idG = $petition->GetGroup();
00178
00179
00180 if ($idM == null)
00181 {
00182 if ($idG == null)
00183 $res = $this->_GetPetitionUser(
00184 $petition, $this->system);
00185 else
00186 {
00187 $res = $this->_GetPetitionGroup(
00188 $petition,
00189 $this->systemgroup);
00190 }
00191 }
00192 else
00193 {
00194 $res = $this->_GetPetitionModule($petition);
00195 }
00196
00197 return $res;
00198 }
00199
00208 function _GetPetitionModule($petition)
00209 {
00210 $idG = $petition->GetGroup();
00211 $idM = $petition->GetModule();
00212
00213 if ($idG == null)
00214 {
00215 if (!isset($this->module[$idM]))
00216 $this->module[$idM] = array();
00217
00218 $res = $this->_GetPetitionUser(
00219 $petition,
00220 $this->module[$idM]);
00221 }
00222 else
00223 {
00224 if (!isset($this->modulegroup[$idM]))
00225 $this->modulegroup[$idM] = array();
00226
00227 $res = $this->_GetPetitionGroup(
00228 $petition,
00229 $this->modulegroup[$idM]);
00230 }
00231
00232 return $res;
00233 }
00234
00244 function _GetPetitionGroup($petition, &$cache)
00245 {
00246 $idG = $petition->GetGroup();
00247
00248 if (!isset($cache[$idG]))
00249 $cache[$idG] = array();
00250
00251 $res = $this->_GetPetitionUser($petition,$cache[$idG]);
00252
00253 return $res;
00254 }
00255
00265 function _GetPetitionUser($petition, &$cache)
00266 {
00267 $idU = $petition->GetUser();
00268
00269 if ($idU == null)
00270 {
00271 if (!isset($cache["system"]))
00272 {
00273 $backend =& $this->_ConfigBackendFactory($petition);
00274 $cache["system"] = $backend->GetConfig();
00275 }
00276
00277 $res = $cache["system"];
00278 }
00279 else
00280 {
00281 if (!isset($cache["user"][$idU]))
00282 {
00283 $backend =& $this->_ConfigBackendFactory($petition);
00284 $cache["user"][$idU] = $backend->GetConfig();
00285 }
00286 $res = $cache["user"][$idU];
00287 }
00288 return $res;
00289 }
00290
00297 function SetPetition($petition, $values)
00298 {
00299
00300 $backend =& $this->_ConfigBackendFactory($petition);
00301
00302
00303 $backend->SetConfig($values);
00304 }
00305 }
00306 ?>