22d84c49bf17ec1c87c5462ad288352857c86179
1 <?php
3 class configRegistry{
5 public $config = NULL;
6 public $properties = array();
7 public $mapByClass = array();
8 public $mapPropertyToClass = array();
9 public $ldapStoredProperties = array();
10 public $fileStoredProperties = array();
11 public $classToName = array();
13 public $status = 'none';
15 function __construct($config)
16 {
17 restore_error_handler();
18 $this->config = &$config;
19 $this->reload();
20 }
22 function reload($force = FALSE)
23 {
24 // Do not reload the properties everytime, once we have
25 // everything loaded and registrered skip the reload.
26 // Status is 'finished' once we had a ldap connection (logged in)
27 if(!$force && $this->status == 'finished') return;
29 // Reset everything
30 $this->ldapStoredProperties = array();
31 $this->fileStoredProperties = array();
32 $this->properties = array();
33 $this->mapByClass = array();
34 $this->mapByName = array();
35 $this->mapPropertyToClass = array();
37 // Search for config flags defined in the config file (TAB section)
38 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
39 foreach($tabdefs as $info){
41 // Check if the info is valid
42 if(isset($info['NAME']) && isset($info['CLASS'])){
44 // Check if there is nore than just the plugin definition
45 if(count($info) > 2){
46 foreach($info as $name => $value){
48 if(!in_array($name, array('CLASS','NAME'))){
49 $class= $info['CLASS'];
50 $this->fileStoredProperties[$class][strtolower($name)] = $value;
51 }
52 }
53 }
54 }
55 }
56 }
58 // Search for config flags defined in the config file (MAIN section)
59 foreach($this->config->data['MAIN'] as $name => $value){
60 $this->fileStoredProperties['core'][strtolower($name)] = $value;
61 }
63 // Search for config flags defined in the config file (Current LOCATION section)
64 if(isset($this->config->current)){
65 foreach($this->config->current as $name => $value){
66 $this->fileStoredProperties['core'][strtolower($name)] = $value;
67 }
68 }
70 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
71 if(!empty($this->config->current['CONFIG'])){
72 $ldap = $this->config->get_ldap_link();
73 $ldap->cd($this->config->current['CONFIG']);
74 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
75 while($attrs = $ldap->fetch()){
76 $class = $attrs['cn'][0];
77 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
78 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
79 $this->ldapStoredProperties[$class][$name] = $value;
80 }
81 }
82 $this->status = 'finished';
83 }
85 global $class_mapping;
86 foreach ($class_mapping as $cname => $path){
87 $cmethods = get_class_methods($cname);
88 if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
90 // Get plugin definitions
91 $def = call_user_func(array($cname, 'plInfo'));;
93 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
94 if(isset($def['plShortName'])){
95 $this->classToName[$cname] = $def['plShortName'];
96 $data = array('name' => 'postcreate','type' => 'string');
97 $this->register($cname, $data);
98 $data = array('name' => 'postremove','type' => 'string');
99 $this->register($cname, $data);
100 $data = array('name' => 'postmodify','type' => 'string');
101 $this->register($cname, $data);
102 $data = array('name' => 'checkhook', 'type' => 'string');
103 $this->register($cname, $data);
104 }
106 if(isset($def['plProperties'])){
107 foreach($def['plProperties'] as $property){
108 $this->register($cname, $property);
109 }
110 }
111 }
112 }
113 }
115 function register($class,$data)
116 {
117 $id = count($this->properties);
118 $this->properties[$id] = new gosaProperty($this,$class,$data);
119 $this->mapByName[$class][strtolower($data['name'])] = $id;
120 $this->mapByClass[$class][] = $id;
121 $this->mapPropertyToClass[$id] = $class;
122 }
124 public function getAllProperties()
125 {
126 return($this->properties);
127 }
129 function propertyExists($class,$name)
130 {
131 return(isset($this->mapByName[$class][strtolower($name)]));
132 }
134 private function getId($class,$name)
135 {
136 if(!isset($this->mapByName[$class][strtolower($name)])){
137 return(-1);
138 }
139 return($this->mapByName[$class][strtolower($name)]);
140 }
142 function getProperty($class,$name)
143 {
144 if($this->propertyExists($class,$name)){
145 return($this->properties[$this->getId($class,$name)]);
146 }
147 return(NULL);
148 }
150 function getPropertyValue($class,$name)
151 {
152 if($this->propertyExists($class,$name)){
153 $tmp = $this->getProperty($class,$name);
154 return($tmp->getValue());
155 }
156 return("");
157 }
159 function setPropertyValue($class,$name, $value)
160 {
161 if($this->propertyExists($class,$name)){
162 $tmp = $this->getProperty($class,$name);
163 return($tmp->setValue($value));
164 }
165 return("");
166 }
168 function saveChanges()
169 {
170 foreach($this->properties as $prop){
171 $prop->save();
172 }
173 $this->reload(TRUE);
174 }
175 }
178 class gosaProperty
179 {
180 protected $name = "";
181 protected $class = "";
182 protected $value = "";
183 protected $type = "string";
184 protected $default = "";
185 protected $description = "";
186 protected $check = "";
187 protected $migrate = "";
188 protected $mandatory = FALSE;
189 protected $group = "default";
190 protected $parent = NULL;
191 protected $data = array();
193 /*! The current property status
194 * 'ldap' Property is stored in ldap
195 * 'file' Property is stored in the config file
196 * 'undefined' Property is currently not stored anywhere
197 * 'modified' Property has been modified (should be saved)
198 */
199 protected $status = 'undefined';
201 protected $attributes = array('name','type','default','description','check',
202 'migrate','mandatory','group');
204 function __construct($parent,$classname,$data)
205 {
206 // Set some basic infos
207 $this->parent = &$parent;
208 $this->class = $classname;
209 $this->data = $data;
211 // Get all relevant information from the data array (comes from plInfo)
212 foreach($this->attributes as $aName){
213 if(isset($data[$aName])){
214 $this->$aName = $data[$aName];
215 }
216 }
218 // Initialize with the current value
219 $this->_restoreCurrentValue();
220 }
222 private function _restoreCurrentValue()
223 {
225 // First check for values in the LDAP Database.
226 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
227 $this->setStatus('ldap');
228 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
229 return;
230 }
232 // Second check for values in the config file.
233 if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
234 $this->setStatus('file');
235 $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
236 return;
237 }
239 // If there still wasn't found anything then fallback to the default.
240 if($this->getStatus() == 'undefined'){
241 $this->value = $this->getDefault();
242 }
243 }
245 function getValue() { return($this->value); }
246 function getMigrate() { return($this->migrate); }
247 function getCheck() { return($this->check); }
248 function getName() { return($this->name); }
249 function getClass() { return($this->class); }
250 function getGroup() { return($this->group); }
251 function getType() { return($this->type); }
252 function getDescription() { return($this->description); }
253 function getDefault() { return($this->default); }
254 function getStatus() { return($this->status); }
255 function isMandatory() { return($this->mandatory); }
257 function setValue($str)
258 {
259 if($this->value != $str){
260 $this->setStatus('modified');
261 $this->value = $str;
262 }
263 }
265 function restoreDefault()
266 {
267 if(in_array($this->getStatus(),array('ldap'))){
268 $this->setStatus('removed');
269 }elseif(in_array($this->getStatus(),array('modified'))){
270 $this->_restoreCurrentValue();
271 }
272 }
274 function save()
275 {
276 if($this->getStatus() == 'modified'){
278 $ldap = $this->parent->config->get_ldap_link();
279 $ldap->cd($this->parent->config->current['BASE']);
280 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
281 $ldap->cat($dn);
282 if(!$ldap->count()){
283 $ldap->cd($dn);
284 $data = array(
285 'cn' => $this->class,
286 'objectClass' => array('top','gosaConfig'),
287 'gosaSetting' => $this->name.":".$this->value);
289 $ldap->add($data);
290 if(!$ldap->success()){
291 echo $ldap->get_error();
292 }
293 $this->_restoreCurrentValue();
295 }else{
296 $attrs = $ldap->fetch();
297 $data = array();
298 $found = false;
299 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
300 $set = $attrs['gosaSetting'][$i];
301 if(preg_match("/^{$this->name}:/", $set)){
302 $set = "{$this->name}:{$this->value}";
303 $found = true;
304 }
305 $data['gosaSetting'][] = $set;
306 }
307 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
308 $ldap->cd($dn);
309 $ldap->modify($data);
310 if(!$ldap->success()){
311 echo $ldap->get_error();
312 }
313 $this->_restoreCurrentValue();
314 }
315 }elseif($this->getStatus() == 'removed'){
316 $ldap = $this->parent->config->get_ldap_link();
317 $ldap->cd($this->parent->config->current['BASE']);
318 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
319 $ldap->cat($dn);
320 $attrs = $ldap->fetch();
321 $data = array('gosaSetting' => array());
322 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
323 $set = $attrs['gosaSetting'][$i];
324 if(preg_match("/^{$this->name}:/", $set)){
325 continue;
326 }
327 $data['gosaSetting'][] = $set;
328 }
329 $ldap->cd($dn);
330 $ldap->modify($data);
331 if(!$ldap->success()){
332 echo $ldap->get_error();
333 }
334 $this->_restoreCurrentValue();
335 }
336 }
338 private function setStatus($state)
339 {
340 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
341 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
342 }else{
343 $this->status = $state;
344 }
345 }
347 function isValid()
348 {
349 return(TRUE);
350 }
351 }
353 ?>