1 <?php
3 class faiVariable extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIvariable");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIvariableEntry";
21 var $subClasses = array("top","FAIclass","FAIvariableEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiVariableEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIvariableContent");
28 var $sub64coded = array();
30 /* Specific attributes */
31 var $cn = ""; // The class name for this object
32 var $description = ""; // The description for this set of partitions
33 var $is_dialog = false; // specifies which buttons will be shown to save or abort
34 var $SubObjects = array(); // All leafobjects of this object
36 var $FAIstate = "";
37 var $ui ;
38 var $view_logged = FALSE;
40 function faiVariable (&$config, $dn= NULL)
41 {
42 /* Load Attributes */
43 plugin::plugin ($config, $dn);
45 if($dn != "new"){
46 $this->dn =$dn;
48 /* Get FAIstate
49 */
50 if(isset($this->attrs['FAIstate'][0])){
51 $this->FAIstate = $this->attrs['FAIstate'][0];
52 }
54 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
55 */
56 $ldap = $this->config->get_ldap_link();
57 $ldap->cd ($this->dn);
59 $attrs_to_search = $this->subAttributes;
60 $attrs_to_search[] = "FAIstate";
61 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
63 while($object = $ldap->fetch()){
65 /* Skip objects, that are tagged as removed */
66 if(isset($object['FAIstate'][0])){
67 if(preg_match("/removed$/",$object['FAIstate'][0])){
68 continue;
69 }
70 }
72 /* Set status for save management */
73 foreach($this->subAttributes as $attrs){
74 if(!isset($object[$attrs][0])){
75 $this->SubObjects[$object['cn'][0]][$attrs]="";
76 }else{
77 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
78 }
79 }
81 foreach($this->sub64coded as $codeIt){
82 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
83 }
85 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
86 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
87 }
89 }
90 $this->ui = get_userinfo();
91 }
94 function acl_base_for_current_object($dn)
95 {
96 if($dn == "new"){
97 if($this->dn == "new"){
98 $dn = session::get('CurrentMainBase');
99 }else{
100 $dn = $this->dn;
101 }
102 }
103 return($dn);
104 }
107 function execute()
108 {
109 /* Call parent execute */
110 plugin::execute();
112 if($this->is_account && !$this->view_logged){
113 $this->view_logged = TRUE;
114 new log("view","fai/".get_class($this),$this->dn);
115 }
117 /* Fill templating stuff */
118 $smarty= get_smarty();
119 $display= "";
121 /* Add new sub object */
122 if(isset($_POST['AddSubObject'])){
123 $this->dialog= new $this->subClassName($this->config,"new");
124 $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
125 $this->dialog->set_acl_category("fai");
126 $this->dialog->parent = &$this;
127 $this->is_dialog=true;
128 }
130 if($this->dn != "new"){
131 session::set('objectinfo',$this->dn);
132 }
135 /* Edit selected Sub Object */
136 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
138 $var = $_POST['SubObject'][0];
139 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
140 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
141 $this->dialog->set_acl_category("fai");
142 $this->dialog->set_acl_base($c_dn);
143 $this->dialog->parent = &$this;
144 session::set('objectinfo',$this->SubObjects[$var]['dn']);
145 $this->is_dialog=true;
146 }
148 /* Remove Sub object */
149 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
150 foreach($_POST['SubObject'] as $var){
152 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
153 $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
154 if(preg_match("/d/",$acl)){
155 if($this->SubObjects[$var]['status'] == "edited"){
156 $this->SubObjects[$var]['status']= "delete";
157 }else{
158 unset($this->SubObjects[$var]);
159 }
160 }
161 }
162 }
164 /* Save Dialog */
165 if(isset($_POST['SaveSubObject'])){
166 $this->dialog->save_object();
167 $msgs = $this->dialog->check();
168 if(count($msgs)>0){
169 foreach($msgs as $msg){
170 print_red($msg);
171 }
172 }else{
173 $obj = $this->dialog->save();
174 if(isset($obj['remove'])){
175 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
176 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
177 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
178 unset($this->SubObjects[$obj['remove']['from']]);
179 }
180 $obj['status'] = "new";
181 $this->SubObjects[$obj['remove']['to']] = $obj;
182 unset($this->SubObjects[$obj['remove']['to']]['remove']);
183 }else{
184 $this->SubObjects[$obj['cn']]=$obj;
185 }
186 $this->is_dialog=false;
187 unset($this->dialog);
188 $this->dialog=FALSE;
189 }
190 }
192 /* Sort entries */
193 $tmp = $keys = array();
194 foreach($this->SubObjects as $key => $entry){
195 $keys[$key]=$key;
196 }
197 natcasesort($keys);
198 foreach($keys as $key){
199 $tmp[$key]=$this->SubObjects[$key];
200 }
201 $this->SubObjects = $tmp;
203 /* Cancel Dialog */
204 if(isset($_POST['CancelSubObject'])){
205 $this->is_dialog=false;
206 unset($this->dialog);
207 $this->dialog=FALSE;
208 }
210 /* Print dialog if $this->dialog is set */
211 if(is_object($this->dialog)){
212 $this->dialog->save_object();
213 $display = $this->dialog->execute();
214 return($display);
215 }
217 $ui = get_userinfo();
218 $ret = $this->getList();
219 $tmp = array();
220 foreach($this->SubObjects as $key => $obj){
221 $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
222 if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
223 $tmp[$key] = $ret[$key];
224 }
225 }
226 $smarty->assign("SubObjects",$tmp);
229 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
230 * If we post the escaped strings they will be escaped again
231 */
232 foreach($this->attributes as $attrs){
233 if(get_magic_quotes_gpc()){
234 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
235 }else{
236 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
237 }
238 }
240 $c_dn = $this->acl_base_for_current_object($this->dn);
241 $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
242 $smarty->assign("is_removeable", preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
244 $tmp = $this->plInfo();
245 foreach($tmp['plProvidedAcls'] as $name => $translation) {
246 $smarty->assign($name."ACL",$this->getacl($name));
247 }
250 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
251 return($display);
252 }
254 /* Generate listbox friendly SubObject list
255 */
256 function getList(){
257 $a_return=array();
258 foreach($this->SubObjects as $obj){
259 if($obj['status'] != "delete"){
261 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
262 if (preg_match("/\[\*\]/", $obj['description'])){
263 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
264 } else {
265 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
266 }
267 }else{
268 if (preg_match("/\[\*\]/", $obj['description'])){
269 $a_return[$obj['cn']]= $obj['cn'];
270 } else {
271 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
272 }
273 }
274 }
275 }
276 return($a_return);
277 }
279 /* Delete me, and all my subtrees
280 */
281 function remove_from_parent()
282 {
283 if($this->acl_is_removeable()){
284 $ldap = $this->config->get_ldap_link();
285 $ldap->cd ($this->dn);
287 $faifilter = session::get('faifilter');
288 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
289 if($faifilter['branch'] == "main"){
290 $use_dn = $this->dn;
291 }
293 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
294 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
296 foreach($this->SubObjects as $name => $obj){
297 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
298 if($faifilter['branch'] == "main"){
299 $use_dn = $obj['dn'];
300 }
301 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
302 }
303 $this->handle_post_events("remove");
304 }
305 }
308 /* Save data to object
309 */
310 function save_object()
311 {
312 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
313 plugin::save_object();
314 foreach($this->attributes as $attrs){
315 if(isset($_POST[$attrs])){
316 $this->$attrs = $_POST[$attrs];
317 }
318 }
319 }
320 }
323 /* Check supplied data */
324 function check()
325 {
326 /* Call common method to give check the hook */
327 $message= plugin::check();
329 return ($message);
330 }
333 /* Save to LDAP */
334 function save()
335 {
336 plugin::save();
338 $ldap = $this->config->get_ldap_link();
339 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
340 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
342 if($this->initially_was_account){
343 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
344 }else{
345 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
346 }
348 /* Prepare FAIscriptEntry to write it to ldap
349 * First sort array.
350 * Because we must delete old entries first.
351 * After deletion, we perform add and modify
352 */
353 $Objects = array();
354 foreach($this->SubObjects as $name => $obj){
355 if($obj['status'] == "delete"){
356 $Objects[$name] = $obj;
357 }
358 }
359 foreach($this->SubObjects as $name => $obj){
360 if($obj['status'] != "delete"){
361 $Objects[$name] = $obj;
362 }
363 }
365 foreach($Objects as $name => $obj){
367 foreach($this->sub64coded as $codeIt){
368 $obj[$codeIt]=base64_encode($obj[$codeIt]);
369 }
371 $tmp = array();
372 foreach($this->subAttributes as $attrs){
373 if(empty($obj[$attrs])){
374 $obj[$attrs] = array();
375 }
376 $tmp[$attrs] = $obj[$attrs];
377 }
379 $tmp['objectClass'] = $this->subClasses;
381 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
383 if($obj['status']=="new"){
384 $ldap->cat($sub_dn,array("objectClass"));
385 if($ldap->count()){
386 $obj['status']="edited";
387 }
388 }
390 /* Tag object */
391 $this->tag_attrs(&$tmp, $sub_dn, $this->gosaUnitTag);
393 if($obj['status'] == "delete"){
394 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
395 $this->handle_post_events("remove");
396 }elseif($obj['status'] == "edited"){
397 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
398 $this->handle_post_events("modify");
399 }elseif($obj['status']=="new"){
400 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
401 $this->handle_post_events("add");
402 }
404 }
405 }
408 function PrepareForCopyPaste($source)
409 {
410 plugin::PrepareForCopyPaste($source);
412 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
413 */
414 $ldap = $this->config->get_ldap_link();
415 $ldap->cd ($source['dn']);
416 $attrs_to_search = $this->subAttributes;
417 $attrs_to_search[] = "FAIstate";
418 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
419 while($object = $ldap->fetch()){
421 /* Skip objects, that are tagged as removed */
422 if(isset($object['FAIstate'][0])){
423 if(preg_match("/removed$/",$object['FAIstate'][0])){
424 continue;
425 }
426 }
428 /* Set status for save management */
429 foreach($this->subAttributes as $attrs){
430 if(!isset($object[$attrs][0])){
431 $this->SubObjects[$object['cn'][0]][$attrs]="";
432 }else{
433 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
434 }
435 }
436 foreach($this->sub64coded as $codeIt){
437 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
438 }
439 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
440 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
441 }
442 }
445 /* Return plugin informations for acl handling */
446 static function plInfo()
447 {
448 return (array(
449 "plShortName" => _("Variable"),
450 "plDescription" => _("FAI variable"),
451 "plSelfModify" => FALSE,
452 "plDepends" => array(),
453 "plPriority" => 22,
454 "plSection" => array("administration"),
455 "plCategory" => array("fai"),
456 "plProvidedAcls" => array(
457 "cn" => _("Name")." ("._("Read only").")",
458 "description" => _("Description"))
459 ));
460 }
461 }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>