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