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 $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 static function isBool($message,$class,$name,$value, $type)
221 {
222 return(in_array($value,array('true','false','')));
223 }
225 static function isInteger($message,$class,$name,$value, $type)
226 {
227 return(is_numeric($value) && !preg_match("/[^0-9]/", $value));
228 }
230 static function isPath($message,$class,$name,$value, $type)
231 {
232 return(TRUE);
233 }
235 static function isWriteablePath($message,$class,$name,$value, $type)
236 {
237 return(!empty($value)&&is_writeable($value));
238 }
240 static function isCommand($message,$class,$name,$value, $type)
241 {
242 return(TRUE);
243 }
245 static function isDn($message,$class,$name,$value, $type)
246 {
247 return(TRUE);
248 }
250 static function isRdn($message,$class,$name,$value, $type)
251 {
252 return(TRUE);
253 }
255 private function _restoreCurrentValue()
256 {
258 // First check for values in the LDAP Database.
259 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
260 $this->setStatus('ldap');
261 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
262 return;
263 }
265 // Second check for values in the config file.
266 if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
267 $this->setStatus('file');
268 $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
269 return;
270 }
272 // If there still wasn't found anything then fallback to the default.
273 if($this->getStatus() == 'undefined'){
274 $this->value = $this->getDefault();
275 }
276 }
278 function getValue() { return($this->value); }
279 function getMigrate() { return($this->migrate); }
280 function getCheck() { return($this->check); }
281 function getName() { return($this->name); }
282 function getClass() { return($this->class); }
283 function getGroup() { return($this->group); }
284 function getType() { return($this->type); }
285 function getDescription() { return($this->description); }
286 function getDefault() { return($this->default); }
287 function getDefaults() { return($this->defaults); }
288 function getStatus() { return($this->status); }
289 function isMandatory() { return($this->mandatory); }
291 function setValue($str)
292 {
293 if($this->value != $str){
294 $this->setStatus('modified');
295 $this->value = $str;
296 }
297 }
299 function restoreDefault()
300 {
301 if(in_array($this->getStatus(),array('ldap'))){
302 $this->setStatus('removed');
303 }elseif(in_array($this->getStatus(),array('modified'))){
304 $this->_restoreCurrentValue();
305 }
306 }
308 function save()
309 {
310 if($this->getStatus() == 'modified'){
312 $ldap = $this->parent->config->get_ldap_link();
313 $ldap->cd($this->parent->config->current['BASE']);
314 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
315 $ldap->cat($dn);
316 if(!$ldap->count()){
317 $ldap->cd($dn);
318 $data = array(
319 'cn' => $this->class,
320 'objectClass' => array('top','gosaConfig'),
321 'gosaSetting' => $this->name.":".$this->value);
323 $ldap->add($data);
324 if(!$ldap->success()){
325 echo $ldap->get_error();
326 }
327 $this->_restoreCurrentValue();
329 }else{
330 $attrs = $ldap->fetch();
331 $data = array();
332 $found = false;
333 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
334 $set = $attrs['gosaSetting'][$i];
335 if(preg_match("/^{$this->name}:/", $set)){
336 $set = "{$this->name}:{$this->value}";
337 $found = true;
338 }
339 $data['gosaSetting'][] = $set;
340 }
341 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
342 $ldap->cd($dn);
343 $ldap->modify($data);
344 if(!$ldap->success()){
345 echo $ldap->get_error();
346 }
347 $this->_restoreCurrentValue();
348 }
349 }elseif($this->getStatus() == 'removed'){
350 $ldap = $this->parent->config->get_ldap_link();
351 $ldap->cd($this->parent->config->current['BASE']);
352 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
353 $ldap->cat($dn);
354 $attrs = $ldap->fetch();
355 $data = array('gosaSetting' => array());
356 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
357 $set = $attrs['gosaSetting'][$i];
358 if(preg_match("/^{$this->name}:/", $set)){
359 continue;
360 }
361 $data['gosaSetting'][] = $set;
362 }
363 $ldap->cd($dn);
364 $ldap->modify($data);
365 if(!$ldap->success()){
366 echo $ldap->get_error();
367 }
368 $this->_restoreCurrentValue();
369 }
370 }
372 private function setStatus($state)
373 {
374 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
375 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
376 }else{
377 $this->status = $state;
378 }
379 }
381 function isValid()
382 {
383 return(TRUE);
384 }
385 }
387 ?>