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)
28 $force = TRUE;
30 if(!$force && $this->status == 'finished') return;
32 // Reset everything
33 $this->ldapStoredProperties = array();
34 $this->fileStoredProperties = array();
35 $this->properties = array();
36 $this->mapByClass = array();
37 $this->mapPropertyToClass = array();
39 // Search for config flags defined in the config file
40 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
41 foreach($tabdefs as $info){
43 // Check if the info is valid
44 if(isset($info['NAME']) && isset($info['CLASS'])){
46 // Check if there is nore than just the plugin definition
47 if(count($info) > 2){
48 foreach($info as $name => $value){
50 if(!in_array($name, array('CLASS','NAME'))){
51 $class= $info['CLASS'];
52 $this->fileStoredProperties[$class][strtolower($name)] = $value;
53 }
54 }
55 }
56 }
57 }
58 }
60 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
61 if(!empty($this->config->current['CONFIG'])){
62 $ldap = $this->config->get_ldap_link();
63 $ldap->cd($this->config->current['CONFIG']);
64 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
65 while($attrs = $ldap->fetch()){
66 $class = $attrs['cn'][0];
67 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
68 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
69 $this->ldapStoredProperties[$class][$name] = $value;
70 }
71 }
72 $this->status = 'finished';
73 }
75 global $class_mapping;
76 foreach ($class_mapping as $cname => $path){
77 $cmethods = get_class_methods($cname);
78 if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
80 // Get plugin definitions
81 $def = call_user_func(array($cname, 'plInfo'));;
83 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
84 if(isset($def['plShortName'])){
85 $this->classToName[$cname] = $def['plShortName'];
86 $data = array('name' => 'postcreate','type' => 'string');
87 $this->register($cname, $data);
88 $data = array('name' => 'postremove','type' => 'string');
89 $this->register($cname, $data);
90 $data = array('name' => 'postmodify','type' => 'string');
91 $this->register($cname, $data);
92 $data = array('name' => 'checkhook', 'type' => 'string');
93 $this->register($cname, $data);
94 }
96 if(isset($def['plProperties'])){
97 foreach($def['plProperties'] as $property){
98 $this->register($cname, $property);
99 }
100 }
101 }
102 }
103 }
105 function register($class,$data)
106 {
107 $id = count($this->properties);
108 $this->properties[$id] = new gosaProperty($this,$class,$data);
109 $this->mapByName[$class][$data['name']] = $id;
110 $this->mapByClass[$class][] = $id;
111 $this->mapPropertyToClass[$id] = $class;
112 }
114 public function getAllProperties()
115 {
116 return($this->properties);
117 }
119 function propertyExists($class,$name)
120 {
121 return(isset($this->mapByName[$class][$name]));
122 }
124 private function getId($class,$name)
125 {
126 return($this->mapByName[$class][$name]);
127 }
129 function getProperty($class,$name)
130 {
131 if($this->propertyExists($class,$name)){
132 return($this->properties[$this->getId($class,$name)]);
133 }
134 return(NULL);
135 }
137 function getPropertyValue($class,$name)
138 {
139 if($this->propertyExists($class,$name)){
140 $tmp = $this->getProperty($class,$name);
141 return($tmp->getValue());
142 }
143 return("");
144 }
146 function setPropertyValue($class,$name, $value)
147 {
148 if($this->propertyExists($class,$name)){
149 $tmp = $this->getProperty($class,$name);
150 return($tmp->setValue($value));
151 }
152 return("");
153 }
154 }
157 class gosaProperty
158 {
159 protected $name = "";
160 protected $class = "";
161 protected $value = "";
162 protected $type = "string";
163 protected $default = "";
164 protected $description = "";
165 protected $check = "";
166 protected $migrate = "";
167 protected $mandatory = FALSE;
168 protected $parent = NULL;
169 protected $data = array();
171 /*! The current property status
172 * 'ldap' Property is stored in ldap
173 * 'file' Property is stored in the config file
174 * 'undefined' Property is currently not stored anywhere
175 * 'modified' Property has been modified (should be saved)
176 */
177 protected $status = 'undefined';
179 protected $attributes = array('name','type','default','description','check',
180 'migrate','mandatory');
182 function __construct($parent,$classname,$data)
183 {
184 // Set some basic infos
185 $this->parent = &$parent;
186 $this->class = $classname;
187 $this->data = $data;
189 // Get all relevant information from the data array (comes from plInfo)
190 foreach($this->attributes as $aName){
191 if(isset($data[$aName])){
192 $this->$aName = $data[$aName];
193 }
194 }
196 // First check for values in the LDAP Database.
197 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
198 $this->setStatus('ldap');
199 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
200 }
202 // Second check for values in the config file.
203 if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
204 $this->setStatus('file');
205 $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
206 }
208 // If there still wasn't found anything then fallback to the default.
209 if($this->getStatus() == 'undefined'){
210 $this->value = $this->getDefault();
211 }
212 }
214 function getValue() { return($this->value); }
215 function getName() { return($this->name); }
216 function getClass() { return($this->class); }
217 function getType() { return($this->type); }
218 function getDescription() { return($this->description); }
219 function getDefault() { return($this->default); }
220 function getStatus() { return($this->status); }
222 function setValue($str)
223 {
224 $this->setStatus('modified');
225 $this->value = $str;
226 }
228 function save()
229 {
230 if($this->getStatus() == 'modified'){
232 $ldap = $this->parent->config->get_ldap_link();
233 $ldap->cd($this->parent->config->current['BASE']);
234 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
235 $ldap->cat($dn);
236 if(!$ldap->count()){
237 $ldap->cd($dn);
238 $data = array(
239 'cn' => $this->class,
240 'objectClass' => array('top','gosaConfig'),
241 'gosaSetting' => $this->name.":".$this->value);
243 $ldap->add($data);
244 if($ldap->success()){
245 $this->status = 'ldap';
246 }else{
247 echo $ldap->get_error();
248 }
249 }else{
250 $attrs = $ldap->fetch();
251 $data = array();
252 $found = false;
253 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
254 $set = $attrs['gosaSetting'][$i];
255 if(preg_match("/^{$this->name}:/", $set)){
256 $set = "{$this->name}:{$this->value}";
257 $found = true;
258 }
259 $data['gosaSetting'][] = $set;
260 }
261 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
262 $ldap->cd($dn);
263 $ldap->modify($data);
264 if($ldap->success()){
265 $this->status = 'ldap';
266 }else{
267 echo $ldap->get_error();
268 }
269 }
270 }
271 }
273 private function setStatus($state)
274 {
275 if(!in_array($state, array('ldap','file','undefined','modified'))) {
276 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
277 }else{
278 $this->status = $state;
279 }
280 }
282 function isValid()
283 {
284 return(TRUE);
285 }
286 }
288 ?>