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->mapPropertyToClass = array();
36 // Search for config flags defined in the config file
37 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
38 foreach($tabdefs as $info){
40 // Check if the info is valid
41 if(isset($info['NAME']) && isset($info['CLASS'])){
43 // Check if there is nore than just the plugin definition
44 if(count($info) > 2){
45 foreach($info as $name => $value){
47 if(!in_array($name, array('CLASS','NAME'))){
48 $class= $info['CLASS'];
49 $this->fileStoredProperties[$class][strtolower($name)] = $value;
50 }
51 }
52 }
53 }
54 }
55 }
57 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
58 if(!empty($this->config->current['CONFIG'])){
59 $ldap = $this->config->get_ldap_link();
60 $ldap->cd($this->config->current['CONFIG']);
61 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
62 while($attrs = $ldap->fetch()){
63 $class = $attrs['cn'][0];
64 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
65 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
66 $this->ldapStoredProperties[$class][$name] = $value;
67 }
68 }
69 $this->status = 'finished';
70 }
72 global $class_mapping;
73 foreach ($class_mapping as $cname => $path){
74 $cmethods = get_class_methods($cname);
75 if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
77 // Get plugin definitions
78 $def = call_user_func(array($cname, 'plInfo'));;
80 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
81 if(isset($def['plShortName'])){
82 $this->classToName[$cname] = $def['plShortName'];
83 $data = array('name' => 'postcreate','type' => 'string');
84 $this->register($cname, $data);
85 $data = array('name' => 'postremove','type' => 'string');
86 $this->register($cname, $data);
87 $data = array('name' => 'postmodify','type' => 'string');
88 $this->register($cname, $data);
89 $data = array('name' => 'checkhook', 'type' => 'string');
90 $this->register($cname, $data);
91 }
93 if(isset($def['plProperties'])){
94 foreach($def['plProperties'] as $property){
95 $this->register($cname, $property);
96 }
97 }
98 }
99 }
100 }
102 function register($class,$data)
103 {
104 $id = count($this->properties);
105 $this->properties[$id] = new gosaProperty($this,$class,$data);
106 $this->mapByName[$class][$data['name']] = $id;
107 $this->mapByClass[$class][] = $id;
108 $this->mapPropertyToClass[$id] = $class;
109 }
111 public function getAllProperties()
112 {
113 return($this->properties);
114 }
116 function propertyExists($class,$name)
117 {
118 return(isset($this->mapByName[$class][$name]));
119 }
121 private function getId($class,$name)
122 {
123 return($this->mapByName[$class][$name]);
124 }
126 function getProperty($class,$name)
127 {
128 if($this->propertyExists($class,$name)){
129 return($this->properties[$this->getId($class,$name)]);
130 }
131 return(NULL);
132 }
134 function getPropertyValue($class,$name)
135 {
136 if($this->propertyExists($class,$name)){
137 $tmp = $this->getProperty($class,$name);
138 return($tmp->getValue());
139 }
140 return("");
141 }
143 function setPropertyValue($class,$name, $value)
144 {
145 if($this->propertyExists($class,$name)){
146 $tmp = $this->getProperty($class,$name);
147 return($tmp->setValue($value));
148 }
149 return("");
150 }
152 function saveChanges()
153 {
154 foreach($this->properties as $prop){
155 $prop->save();
156 }
157 }
158 }
161 class gosaProperty
162 {
163 protected $name = "";
164 protected $class = "";
165 protected $value = "";
166 protected $type = "string";
167 protected $default = "";
168 protected $description = "";
169 protected $check = "";
170 protected $migrate = "";
171 protected $mandatory = FALSE;
172 protected $group = "default";
173 protected $parent = NULL;
174 protected $data = array();
176 /*! The current property status
177 * 'ldap' Property is stored in ldap
178 * 'file' Property is stored in the config file
179 * 'undefined' Property is currently not stored anywhere
180 * 'modified' Property has been modified (should be saved)
181 */
182 protected $status = 'undefined';
184 protected $attributes = array('name','type','default','description','check',
185 'migrate','mandatory','group');
187 function __construct($parent,$classname,$data)
188 {
189 // Set some basic infos
190 $this->parent = &$parent;
191 $this->class = $classname;
192 $this->data = $data;
194 // Get all relevant information from the data array (comes from plInfo)
195 foreach($this->attributes as $aName){
196 if(isset($data[$aName])){
197 $this->$aName = $data[$aName];
198 }
199 }
201 // First check for values in the LDAP Database.
202 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
203 $this->setStatus('ldap');
204 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
205 }
207 // Second check for values in the config file.
208 if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
209 $this->setStatus('file');
210 $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
211 }
213 // If there still wasn't found anything then fallback to the default.
214 if($this->getStatus() == 'undefined'){
215 $this->value = $this->getDefault();
216 }
217 }
219 function getValue() { return($this->value); }
220 function getMigrate() { return($this->migrate); }
221 function getCheck() { return($this->check); }
222 function getName() { return($this->name); }
223 function getClass() { return($this->class); }
224 function getGroup() { return($this->group); }
225 function getType() { return($this->type); }
226 function getDescription() { return($this->description); }
227 function getDefault() { return($this->default); }
228 function getStatus() { return($this->status); }
229 function isMandatory() { return($this->mandatory); }
231 function setValue($str)
232 {
233 if($this->value != $str){
234 $this->setStatus('modified');
235 $this->value = $str;
236 }
237 }
239 function restoreDefault()
240 {
241 $this->setStatus('removed');
242 }
244 function save()
245 {
246 if($this->getStatus() == 'modified'){
248 $ldap = $this->parent->config->get_ldap_link();
249 $ldap->cd($this->parent->config->current['BASE']);
250 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
251 $ldap->cat($dn);
252 if(!$ldap->count()){
253 $ldap->cd($dn);
254 $data = array(
255 'cn' => $this->class,
256 'objectClass' => array('top','gosaConfig'),
257 'gosaSetting' => $this->name.":".$this->value);
259 $ldap->add($data);
260 if($ldap->success()){
261 $this->status = 'ldap';
262 }else{
263 echo $ldap->get_error();
264 }
265 }else{
266 $attrs = $ldap->fetch();
267 $data = array();
268 $found = false;
269 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
270 $set = $attrs['gosaSetting'][$i];
271 if(preg_match("/^{$this->name}:/", $set)){
272 $set = "{$this->name}:{$this->value}";
273 $found = true;
274 }
275 $data['gosaSetting'][] = $set;
276 }
277 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
278 $ldap->cd($dn);
279 $ldap->modify($data);
280 if($ldap->success()){
281 $this->status = 'ldap';
282 }else{
283 echo $ldap->get_error();
284 }
285 }
286 }elseif($this->getStatus() == 'removed'){
287 $ldap = $this->parent->config->get_ldap_link();
288 $ldap->cd($this->parent->config->current['BASE']);
289 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
290 $ldap->cat($dn);
291 $attrs = $ldap->fetch();
292 $data = array();
293 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
294 $set = $attrs['gosaSetting'][$i];
295 if(preg_match("/^{$this->name}:/", $set)){
296 continue;
297 }
298 $data['gosaSetting'][] = $set;
299 }
300 $ldap->cd($dn);
301 $ldap->modify($data);
302 if($ldap->success()){
303 $this->status = 'undefined';
305 // Second check for values in the config file.
306 if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
307 $this->setStatus('file');
308 $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
309 }
311 // If there still wasn't found anything then fallback to the default.
312 if($this->getStatus() == 'undefined'){
313 $this->value = $this->getDefault();
314 }
316 }else{
317 echo $ldap->get_error();
318 }
319 }
320 }
322 private function setStatus($state)
323 {
324 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
325 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
326 }else{
327 $this->status = $state;
328 }
329 }
331 function isValid()
332 {
333 return(TRUE);
334 }
335 }
337 ?>