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][$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][$name]));
132 }
134 private function getId($class,$name)
135 {
136 return($this->mapByName[$class][$name]);
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 $description = "";
183 protected $check = "";
184 protected $migrate = "";
185 protected $mandatory = FALSE;
186 protected $group = "default";
187 protected $parent = NULL;
188 protected $data = array();
190 /*! The current property status
191 * 'ldap' Property is stored in ldap
192 * 'file' Property is stored in the config file
193 * 'undefined' Property is currently not stored anywhere
194 * 'modified' Property has been modified (should be saved)
195 */
196 protected $status = 'undefined';
198 protected $attributes = array('name','type','default','description','check',
199 'migrate','mandatory','group');
201 function __construct($parent,$classname,$data)
202 {
203 // Set some basic infos
204 $this->parent = &$parent;
205 $this->class = $classname;
206 $this->data = $data;
208 // Get all relevant information from the data array (comes from plInfo)
209 foreach($this->attributes as $aName){
210 if(isset($data[$aName])){
211 $this->$aName = $data[$aName];
212 }
213 }
215 // Initialize with the current value
216 $this->_restoreCurrentValue();
217 }
219 private function _restoreCurrentValue()
220 {
222 // First check for values in the LDAP Database.
223 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
224 $this->setStatus('ldap');
225 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
226 return;
227 }
229 // Second check for values in the config file.
230 if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
231 $this->setStatus('file');
232 $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
233 return;
234 }
236 // If there still wasn't found anything then fallback to the default.
237 if($this->getStatus() == 'undefined'){
238 $this->value = $this->getDefault();
239 }
240 }
242 function getValue() { return($this->value); }
243 function getMigrate() { return($this->migrate); }
244 function getCheck() { return($this->check); }
245 function getName() { return($this->name); }
246 function getClass() { return($this->class); }
247 function getGroup() { return($this->group); }
248 function getType() { return($this->type); }
249 function getDescription() { return($this->description); }
250 function getDefault() { return($this->default); }
251 function getStatus() { return($this->status); }
252 function isMandatory() { return($this->mandatory); }
254 function setValue($str)
255 {
256 if($this->value != $str){
257 $this->setStatus('modified');
258 $this->value = $str;
259 }
260 }
262 function restoreDefault()
263 {
264 if(in_array($this->getStatus(),array('ldap'))){
265 $this->setStatus('removed');
266 }elseif(in_array($this->getStatus(),array('modified'))){
267 $this->_restoreCurrentValue();
268 }
269 }
271 function save()
272 {
273 if($this->getStatus() == 'modified'){
275 $ldap = $this->parent->config->get_ldap_link();
276 $ldap->cd($this->parent->config->current['BASE']);
277 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
278 $ldap->cat($dn);
279 if(!$ldap->count()){
280 $ldap->cd($dn);
281 $data = array(
282 'cn' => $this->class,
283 'objectClass' => array('top','gosaConfig'),
284 'gosaSetting' => $this->name.":".$this->value);
286 $ldap->add($data);
287 if(!$ldap->success()){
288 echo $ldap->get_error();
289 }
290 $this->_restoreCurrentValue();
292 }else{
293 $attrs = $ldap->fetch();
294 $data = array();
295 $found = false;
296 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
297 $set = $attrs['gosaSetting'][$i];
298 if(preg_match("/^{$this->name}:/", $set)){
299 $set = "{$this->name}:{$this->value}";
300 $found = true;
301 }
302 $data['gosaSetting'][] = $set;
303 }
304 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
305 $ldap->cd($dn);
306 $ldap->modify($data);
307 if(!$ldap->success()){
308 echo $ldap->get_error();
309 }
310 $this->_restoreCurrentValue();
311 }
312 }elseif($this->getStatus() == 'removed'){
313 $ldap = $this->parent->config->get_ldap_link();
314 $ldap->cd($this->parent->config->current['BASE']);
315 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
316 $ldap->cat($dn);
317 $attrs = $ldap->fetch();
318 $data = array('gosaSetting' => array());
319 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
320 $set = $attrs['gosaSetting'][$i];
321 if(preg_match("/^{$this->name}:/", $set)){
322 continue;
323 }
324 $data['gosaSetting'][] = $set;
325 }
326 $ldap->cd($dn);
327 $ldap->modify($data);
328 if(!$ldap->success()){
329 echo $ldap->get_error();
330 }
331 $this->_restoreCurrentValue();
332 }
333 }
335 private function setStatus($state)
336 {
337 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
338 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
339 }else{
340 $this->status = $state;
341 }
342 }
344 function isValid()
345 {
346 return(TRUE);
347 }
348 }
350 ?>