Code

Also display new created variables
[gosa.git] / plugins / admin / fai / class_faiTemplate.inc
1 <?php
3 class faiTemplate 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","FAItemplate");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAItemplateEntry";
21   var $subClasses       = array("top","FAIclass","FAItemplateEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiTemplateEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItemplatePath","FAIowner","FAImode");
28   var $sub_Load_Later   = array("FAItemplateFile"); 
29   var $sub64coded       = array();
30   var $subBinary        = array("FAItemplateFile");
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIstate         = "";
40   var $ui;
42   function faiTemplate ($config, $dn= NULL)
43   {
44     /* Load Attributes */
45     plugin::plugin ($config, $dn);
47     /* If "dn==new" we try to create a new entry
48      * Else we must read all objects from ldap which belong to this entry.
49      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
50      */
51     if($dn != "new"){
52       $this->dn =$dn;
54       /* Get FAIstate
55        */
56       if(isset($this->attrs['FAIstate'][0])){
57         $this->FAIstate = $this->attrs['FAIstate'][0];
58       }
60       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
61        */
62       $ldap     = $this->config->get_ldap_link();
63       $ldap->cd ($this->dn);
64     
65       $attrs_to_search = $this->subAttributes;
66       $attrs_to_search[] = "FAIstate";
67       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
69       while($object = $ldap->fetch()){
71         /* Skip objects, that are tagged as removed */
72         if(isset($object['FAIstate'][0])){
73           if(preg_match("/removed$/",$object['FAIstate'][0])){
74             continue;
75           }
76         }
78         /* Set status for save management */
79         $objects = array();
80         $objects['status']      = "FreshLoaded";
81         $objects['dn']          = $object['dn'];
82         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
83         $this->SubObjects[$objects['cn']] = $objects;
84       }
85   
86       @log::log("view","fai/".get_class($this),$this->dn);
87     }
88     $this->ui = get_userinfo();
89   }
92   /* Reload some attributes */
93   function get_object_attributes($object,$attributes)
94   {
95     $ldap = $this->config->get_ldap_link();
96     $ldap->cd($this->config->current['BASE']);
97     $ldap->cat($object['dn'],$attributes);
98     $tmp  = $ldap->fetch();
100     foreach($attributes as $attrs){
101       if(isset($tmp[$attrs][0])){
102         $var = $tmp[$attrs][0];
104         /* Check if we must decode some attributes */
105         if(in_array_ics($attrs,$this->sub64coded)){
106           $var = base64_decode($var);
107         }
109         /*  check if this is a binary entry */
110         if(in_array_ics($attrs,$this->subBinary)){
111           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
112         }
114         $var = stripslashes($var);
115   
116         $object[$attrs] = $var;
117       }
118     }
119     return($object);
120   }
123   function execute()
124   {
125     /* Call parent execute */
126     plugin::execute();
128     /* Fill templating stuff */
129     $smarty= get_smarty();
130     $display= "";
132     /* New Listhandling
133      */
134     $once = true;
135     foreach($_POST as $name => $value){
136       if(preg_match("/^editscript_/",$name)&&($once)){
137         $once = false;
138         $entry = preg_replace("/^editscript_/","",$name);
139         $entry = base64_decode(preg_replace("/_.*/","",$entry));
140         $obj  = $this->SubObjects[$entry];
142         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
143         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
144         if(preg_match("/r/",$acl)){
145           if($obj['status'] == "FreshLoaded"){
146             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
147           }
148           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
149           $this->dialog->set_acl_base($this->acl_base);
150           $this->dialog->set_acl_category("fai");
152           $_SESSION['objectinfo'] = $obj['dn'];
153           $this->dialog->parent = &$this;
154           $this->is_dialog=true;
155         }
156       }
157       if(preg_match("/^deletescript_/",$name)&&($once)){
158         $once = false;
159         $entry = preg_replace("/^deletescript_/","",$name);
160         $entry = base64_decode(preg_replace("/_.*/","",$entry));
161         $obj  = $this->SubObjects[$entry];
163         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
164         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
165         if(preg_match("/d/",$acl)){
166           $status = $this->SubObjects[$entry]['status'];
167           if($status == "edited" || $status == "FreshLoaded"){
168             $this->SubObjects[$entry]['status']= "delete";
169           }else{
170             unset($this->SubObjects[$entry]);
171           }
172         }
173       }
174     }
176     /* Add new sub object */
177     if(isset($_POST['AddSubObject'])){
178       $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
179       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
181       if(preg_match("/c/",$acl)){
182         $this->dialog= new $this->subClassName($this->config,"new");
183         $this->dialog->set_acl_base($this->acl_base);
184         $this->dialog->set_acl_category("fai");
185         $this->is_dialog=true;
186       }
187     }
189     if($this->dn != "new"){
190       $_SESSION['objectinfo']= $this->dn;
191     }
193     /* Save Dialog */
194     if(isset($_POST['SaveSubObject'])){
195       $this->dialog->save_object();
196       $msgs = $this->dialog->check();
197       if(count($msgs)>0){
198         foreach($msgs as $msg){
199           print_red($msg);
200         }
201       }else{
202         /* Get return object */
203         $obj = $this->dialog->save();
204         if(isset($obj['remove'])){
206           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
208           /* Depending on status, set new status */
209           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
210             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
211           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
212             unset($this->SubObjects[$obj['remove']['from']]);
213           }
214           $obj['status'] = "new";
215           $this->SubObjects[$obj['remove']['to']] = $obj;
216           unset($this->SubObjects[$obj['remove']['to']]['remove']);
217         }else{
218           if($obj['status'] == "FreshLoaded"){
219             $obj['status'] = "edited";
220           }
221           $this->SubObjects[$obj['cn']]=$obj;
222         }
223         $this->is_dialog=false;
224         unset($this->dialog);
225         $this->dialog=NULL;
226       }
227     }
229     /* Sort entries */
230     $tmp = $keys = array();
231     foreach($this->SubObjects as $key => $entry){
232       $keys[$key]=$key;
233     }
234     natcasesort($keys);
235     foreach($keys as $key){
236       $tmp[$key]=$this->SubObjects[$key];
237     }
238     $this->SubObjects = $tmp;
240     /* Cancel Dialog */
241     if(isset($_POST['CancelSubObject'])){
242       $this->is_dialog=false; 
243       unset($this->dialog);
244       $this->dialog=NULL;
245     }
247     /* Print dialog if $this->dialog is set */
248     if($this->dialog){
249       $this->dialog->save_object();
250       $display = $this->dialog->execute();
251       return($display);
252     }
254      /* Divlist Containing FAItemplates */
255     $divlist = new divSelectBox("FAItemplates");
256     $divlist->setHeight(400);
258     $tmp = $this->getList(true);
259   
260     /* Create div list with all sub entries listed */
261     foreach($this->SubObjects as $key => $name){
263       /* Skip removed entries */ 
264       if($name['status'] == "delete") continue;
266       /* Get permissions */
267       $dn  = $this->acl_base_for_current_object($name['dn']);
268       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
269       $act = "";
271       /* Hide delete icon if this object is freezed */
272       if(preg_match("/freeze/",$this->FAIstate)){
273         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
274       }else{
275         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
276         if(preg_match("/d/",$acl)){
277           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
278         }
279       }
281       /* Check acls for download icon */
282       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
283       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
284         $down = "";
285       }else{
286         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
287           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
288           </a>";
289       }
291       /* Check if we are allowed to view this object */
292       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
293       if(preg_match("/r/",$s_acl)){
294         $divlist->AddEntry(array( array("string"=> $tmp[$key] ),
295               array("string"=>$down , "attach" => "style='width:20px;'"),
296               array("string"=>str_replace("%s",base64_encode($key),$act),
297                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
298       }
299     }
300     $smarty->assign("Entry_divlist",$divlist->DrawList());
301     /* Divlist creation complete
302      */
304      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
305      * If we post the escaped strings they will be escaped again
306      */
307     foreach($this->attributes as $attrs){
308       if(get_magic_quotes_gpc()){
309         $smarty->assign($attrs,stripslashes($this->$attrs));
310       }else{
311         $smarty->assign($attrs,($this->$attrs));
312       }
313     }
315     $dn = $this->acl_base_for_current_object($this->dn);
316     $smarty->assign("sub_object_is_addable", 
317             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
318             !preg_match("/freeze/",$this->FAIstate));
320     foreach($this->attributes as $attr){
321       $smarty->assign($attr."ACL",$this->getacl($attr));
322     }
324     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
325     return($display);
326   }
329   function acl_base_for_current_object($dn)
330   {
331     if($dn == "new"){
332       if($this->dn == "new"){
333         $dn= $_SESSION['CurrentMainBase'];
334       }else{
335         $dn = $this->dn;
336       }
337     }
338     return($dn);
339   }
342   /* Generate listbox friendly SubObject list
343   */
344   function getList(){
345     $a_return=array();
346     foreach($this->SubObjects as $obj){
347       if($obj['status'] != "delete"){
348       
349         if((isset($obj['description']))&&(!empty($obj['description']))){
350           if(strlen($obj['description']) > 40){
351             $obj['description'] = substr($obj['description'],0,40)."...";
352           }
353           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
354         }else{
355           $a_return[$obj['cn']]= $obj['cn'];
356         }
357       }
358     }
359     return($a_return);
360   }
362   /* Delete me, and all my subtrees
363    */
364   function remove_from_parent()
365   {
366     $ldap = $this->config->get_ldap_link();
367     $ldap->cd ($this->dn);
369 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
370     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
371     if($_SESSION['faifilter']['branch'] == "main"){
372       $use_dn = $this->dn;
373     }
375     prepare_to_save_FAI_object($use_dn,array(),true);
376     @log::log("remove","fai/".get_class($this),$use_dn,$this->attributes);
378     foreach($this->SubObjects as $name => $obj){
379 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
380       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
381       if($_SESSION['faifilter']['branch'] == "main"){
382         $use_dn = $obj['dn'];
383       }
384       prepare_to_save_FAI_object($use_dn,array(),true);
385     }
386     $this->handle_post_events("remove");    
387   }
390   /* Save data to object 
391    */
392   function save_object()
393   {
394     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
395       plugin::save_object();
396     }
397   }
400   /* Check supplied data */
401   function check()
402   {
403     /* Call common method to give check the hook */
404     $message= plugin::check();
406     return ($message);
407   }
410   /* Save to LDAP */
411   function save()
412   {
413     plugin::save();
415     $ldap = $this->config->get_ldap_link();
417     prepare_to_save_FAI_object($this->dn,$this->attrs);
418     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
420     if($this->initially_was_account){
421       @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
422     }else{
423       @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
424     }
426     /* Do object tagging */
427     $this->handle_object_tagging();
429     /* Prepare FAIscriptEntry to write it to ldap
430      * First sort array.
431      *  Because we must delete old entries first.
432      * After deletion, we perform add and modify 
433      */
434     $Objects = array();
435     foreach($this->SubObjects as $name => $obj){
436       if($obj['status'] == "FreshLoaded"){
437         unset($this->SubObjects[$name]);
438       }
439     }
441     foreach($this->SubObjects as $name => $obj){
442       if($obj['status'] == "delete"){
443         $Objects[$name] = $obj; 
444       }
445     }
446     foreach($this->SubObjects as $name => $obj){
447       if($obj['status'] != "delete"){
448         $Objects[$name] = $obj; 
449       }
450     }
452     foreach($Objects as $name => $obj){
454       foreach($this->sub64coded as $codeIt){
455         $obj[$codeIt]=base64_encode($obj[$codeIt]);
456       }
457       $tmp = array();
458       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
459       foreach($attributes as $attrs){
460         if(empty($obj[$attrs])){
461           $obj[$attrs] = array();
462         }
463         $tmp[$attrs] =($obj[$attrs]);
464       }    
465         
466       $tmp['objectClass'] = $this->subClasses;
468       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
470       if($obj['status']=="new"){
471         $ldap->cat($sub_dn,array("objectClass"));
472         if($ldap->count()){
473           $obj['status']="edited";
474         }
475       }
477        /* Check if gosaAdministrativeUnitTag is required as object class */
478       if($obj['status'] == "edited"){
479         $ldap->cat($sub_dn,array("objectClass"));
480         $attrs = $ldap->fetch();
481         if(isset($attrs['objectClass'])){
482           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
483             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
484           }
485         }
486       }
488       if($obj['status'] == "delete"){
489         prepare_to_save_FAI_object($sub_dn,array(),true);
490         $this->handle_post_events("remove");
491       }elseif($obj['status'] == "edited"){
492         prepare_to_save_FAI_object($sub_dn,$tmp);
493         $this->handle_post_events("modify");
494       }elseif($obj['status']=="new"){
495         prepare_to_save_FAI_object($sub_dn,$tmp);
496         $this->handle_post_events("add");
497       }
500       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
501     }
502   }
503   
505   /* Return plugin informations for acl handling */ 
506   function plInfo()
507   {
508     return (array( 
509           "plShortName" => _("Template"),
510           "plDescription" => _("FAI template"),
511           "plSelfModify"  => FALSE,
512           "plDepends"     => array(),
513           "plPriority"    => 24,
514           "plSection"     => array("administration"),
515           "plCategory"    => array("fai"),
516           "plProvidedAcls" => array(
517             "cn"                => _("Name")." ("._("Readonly").")",
518             "description"       => _("Description"))
519           ));
520   }
523 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
524 ?>