1 <?php
3 class configRegistry{
5 public $config = NULL;
6 public $properties = array();
7 public $ldapStoredProperties = array();
8 public $fileStoredProperties = array();
9 public $classToName = array();
11 public $status = 'none';
13 function __construct($config)
14 {
15 $this->config = &$config;
16 $this->reload();
17 }
19 function reload($force = FALSE)
20 {
21 // Do not reload the properties everytime, once we have
22 // everything loaded and registrered skip the reload.
23 // Status is 'finished' once we had a ldap connection (logged in)
24 if(!$force && $this->status == 'finished') return;
26 // Reset everything
27 $this->ldapStoredProperties = array();
28 $this->fileStoredProperties = array();
29 $this->properties = array();
30 $this->mapByName = array();
32 // Search for config flags defined in the config file (TAB section)
33 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
34 foreach($tabdefs as $info){
36 // Check if the info is valid
37 if(isset($info['NAME']) && isset($info['CLASS'])){
39 // Check if there is nore than just the plugin definition
40 if(count($info) > 2){
41 foreach($info as $name => $value){
43 if(!in_array($name, array('CLASS','NAME'))){
44 $class= $info['CLASS'];
45 $this->fileStoredProperties[$class][strtolower($name)] = $value;
46 }
47 }
48 }
49 }
50 }
51 }
53 // Search for config flags defined in the config file (MAIN section)
54 foreach($this->config->data['MAIN'] as $name => $value){
55 $this->fileStoredProperties['core'][strtolower($name)] = $value;
56 }
58 // Search for config flags defined in the config file (Current LOCATION section)
59 if(isset($this->config->current)){
60 foreach($this->config->current as $name => $value){
61 $this->fileStoredProperties['core'][strtolower($name)] = $value;
62 }
63 }
65 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
66 if(!empty($this->config->current['CONFIG'])){
67 $ldap = $this->config->get_ldap_link();
68 $ldap->cd($this->config->current['CONFIG']);
69 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
70 while($attrs = $ldap->fetch()){
71 $class = $attrs['cn'][0];
72 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
73 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
74 $this->ldapStoredProperties[$class][$name] = $value;
75 }
76 }
77 $this->status = 'finished';
78 }
80 global $class_mapping;
81 foreach ($class_mapping as $cname => $path){
82 $cmethods = get_class_methods($cname);
83 if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
85 // Get plugin definitions
86 $def = call_user_func(array($cname, 'plInfo'));;
88 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
89 if(isset($def['plShortName'])){
90 $this->classToName[$cname] = $def['plShortName'];
91 $data = array('name' => 'postcreate','type' => 'command');
92 $this->register($cname, $data);
93 $data = array('name' => 'postremove','type' => 'command');
94 $this->register($cname, $data);
95 $data = array('name' => 'postmodify','type' => 'command');
96 $this->register($cname, $data);
97 $data = array('name' => 'check', 'type' => 'command');
98 $this->register($cname, $data);
99 }
101 if(isset($def['plProperties'])){
102 foreach($def['plProperties'] as $property){
103 $this->register($cname, $property);
104 }
105 }
106 }
107 }
108 }
110 function register($class,$data)
111 {
112 $id = count($this->properties);
113 $this->properties[$id] = new gosaProperty($this,$class,$data);
114 $p = strtolower("{$class}::{$data['name']}");
115 $this->mapByName[$p] = $id;
116 }
118 public function getAllProperties()
119 {
120 return($this->properties);
121 }
123 function propertyExists($class,$name)
124 {
125 $p = strtolower("{$class}::{$name}");
126 return(isset($this->mapByName[$p]));
127 }
129 private function getId($class,$name)
130 {
131 $p = strtolower("{$class}::{$name}");
132 if(!isset($this->mapByName[$p])){
133 return(-1);
134 }
135 return($this->mapByName[$p]);
136 }
138 function getProperty($class,$name)
139 {
140 if($this->propertyExists($class,$name)){
141 return($this->properties[$this->getId($class,$name)]);
142 }
143 return(NULL);
144 }
146 function getPropertyValue($class,$name)
147 {
148 if($this->propertyExists($class,$name)){
149 $tmp = $this->getProperty($class,$name);
150 return($tmp->getValue());
151 }
152 return("");
153 }
155 function setPropertyValue($class,$name, $value)
156 {
157 if($this->propertyExists($class,$name)){
158 $tmp = $this->getProperty($class,$name);
159 return($tmp->setValue($value));
160 }
161 return("");
162 }
164 function saveChanges()
165 {
166 foreach($this->properties as $prop){
167 $prop->save();
168 }
169 $this->reload(TRUE);
170 }
171 }
174 class gosaProperty
175 {
176 protected $name = "";
177 protected $class = "";
178 protected $value = "";
179 protected $tmp_value = ""; // Used when modified but not saved
180 protected $type = "string";
181 protected $default = "";
182 protected $defaults = "";
183 protected $description = "";
184 protected $check = "";
185 protected $migrate = "";
186 protected $mandatory = FALSE;
187 protected $group = "default";
188 protected $parent = NULL;
189 protected $data = array();
191 /*! The current property status
192 * 'ldap' Property is stored in ldap
193 * 'file' Property is stored in the config file
194 * 'undefined' Property is currently not stored anywhere
195 * 'modified' Property has been modified (should be saved)
196 */
197 protected $status = 'undefined';
199 protected $attributes = array('name','type','default','description','check',
200 'migrate','mandatory','group','defaults');
202 function __construct($parent,$classname,$data)
203 {
204 // Set some basic infos
205 $this->parent = &$parent;
206 $this->class = $classname;
207 $this->data = $data;
209 // Get all relevant information from the data array (comes from plInfo)
210 foreach($this->attributes as $aName){
211 if(isset($data[$aName])){
212 $this->$aName = $data[$aName];
213 }
214 }
216 // Initialize with the current value
217 $this->_restoreCurrentValue();
218 }
220 function check()
221 {
222 $val = $this->getValue(TRUE);
223 $return = TRUE;
224 if($this->mandatory && empty($val)){
225 msg_dialog::display(_("Error"), msgPool::required(_($this->name)), ERROR_DIALOG);
226 $return = FALSE;
227 }
229 $check = $this->getCheck();
230 if(!empty($val) && !empty($check)){
231 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
232 if(!$res){
233 msg_dialog::display(_("Error"), msgPool::invalid(_($this->name)), ERROR_DIALOG);
234 $return = FALSE;
235 }
236 }
237 return($return);
238 }
240 static function isBool($message,$class,$name,$value, $type)
241 {
242 return(in_array($value,array('true','false','')));
243 }
245 static function isInteger($message,$class,$name,$value, $type)
246 {
247 return(is_numeric($value) && !preg_match("/[^0-9]/", $value));
248 }
250 static function isPath($message,$class,$name,$value, $type)
251 {
252 return(TRUE);
253 }
255 static function isWriteablePath($message,$class,$name,$value, $type)
256 {
257 return(!empty($value)&&is_writeable($value));
258 }
260 static function isCommand($message,$class,$name,$value, $type)
261 {
262 return(TRUE);
263 }
265 static function isDn($message,$class,$name,$value, $type)
266 {
267 return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
268 }
270 static function isRdn($message,$class,$name,$value, $type)
271 {
272 return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
273 }
275 private function _restoreCurrentValue()
276 {
278 // First check for values in the LDAP Database.
279 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
280 $this->setStatus('ldap');
281 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
282 return;
283 }
285 // Second check for values in the config file.
286 if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
287 $this->setStatus('file');
288 $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
289 return;
290 }
292 // If there still wasn't found anything then fallback to the default.
293 if($this->getStatus() == 'undefined'){
294 $this->value = $this->getDefault();
295 }
296 }
298 function getMigrate() { return($this->migrate); }
299 function getCheck() { return($this->check); }
300 function getName() { return($this->name); }
301 function getClass() { return($this->class); }
302 function getGroup() { return($this->group); }
303 function getType() { return($this->type); }
304 function getDescription() { return($this->description); }
305 function getDefault() { return($this->default); }
306 function getDefaults() { return($this->defaults); }
307 function getStatus() { return($this->status); }
308 function isMandatory() { return($this->mandatory); }
310 function setValue($str)
311 {
312 if(in_array($this->getStatus(), array('modified'))){
313 $this->tmp_value = $str;
314 }elseif($this->value != $str){
315 $this->setStatus('modified');
316 $this->tmp_value = $str;
317 }
318 }
320 function getValue($temporary = FALSE)
321 {
322 if($temporary && in_array($this->getStatus(), array('modified'))){
323 return($this->tmp_value);
324 }else{
325 return($this->value);
326 }
327 }
329 function restoreDefault()
330 {
331 if(in_array($this->getStatus(),array('ldap'))){
332 $this->setStatus('removed');
333 }elseif(in_array($this->getStatus(),array('modified'))){
334 $this->_restoreCurrentValue();
335 }
336 }
338 function save()
339 {
340 if($this->getStatus() == 'modified'){
341 $ldap = $this->parent->config->get_ldap_link();
342 $ldap->cd($this->parent->config->current['BASE']);
343 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
344 $ldap->cat($dn);
345 if(!$ldap->count()){
346 $ldap->cd($dn);
347 $data = array(
348 'cn' => $this->class,
349 'objectClass' => array('top','gosaConfig'),
350 'gosaSetting' => $this->name.":".$this->tmp_value);
352 $ldap->add($data);
353 if(!$ldap->success()){
354 echo $ldap->get_error();
355 }
357 }else{
358 $attrs = $ldap->fetch();
359 $data = array();
360 $found = false;
361 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
362 $set = $attrs['gosaSetting'][$i];
363 if(preg_match("/^{$this->name}:/", $set)){
364 $set = "{$this->name}:{$this->tmp_value}";
365 $found = true;
366 }
367 $data['gosaSetting'][] = $set;
368 }
369 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
370 $ldap->cd($dn);
371 $ldap->modify($data);
372 if(!$ldap->success()){
373 echo $ldap->get_error();
374 }
375 }
376 $this->_restoreCurrentValue();
377 }elseif($this->getStatus() == 'removed'){
378 $ldap = $this->parent->config->get_ldap_link();
379 $ldap->cd($this->parent->config->current['BASE']);
380 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
381 $ldap->cat($dn);
382 $attrs = $ldap->fetch();
383 $data = array('gosaSetting' => array());
384 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
385 $set = $attrs['gosaSetting'][$i];
386 if(preg_match("/^{$this->name}:/", $set)){
387 continue;
388 }
389 $data['gosaSetting'][] = $set;
390 }
391 $ldap->cd($dn);
392 $ldap->modify($data);
393 if(!$ldap->success()){
394 echo $ldap->get_error();
395 }
396 $this->_restoreCurrentValue();
397 }
398 }
400 private function setStatus($state)
401 {
402 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
403 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
404 }else{
405 $this->status = $state;
406 }
407 }
409 function isValid()
410 {
411 return(TRUE);
412 }
413 }
415 ?>