Code

Same here
[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         = "";
41   function faiTemplate ($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
46     $this->acl="#all#";
48     /* If "dn==new" we try to create a new entry
49      * Else we must read all objects from ldap which belong to this entry.
50      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
51      */
52     if($dn != "new"){
53       $this->dn =$dn;
55       /* Set acls
56        */
57       $ui   = get_userinfo();
58       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
59       $acli = get_module_permission($acl, "FAIclass", $this->dn);
60       $this->acl=$acli;
62       /* Get FAIstate
63        */
64       if(isset($this->attrs['FAIstate'][0])){
65         $this->FAIstate = $this->attrs['FAIstate'][0];
66       }
68       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
69        */
70       $ldap     = $this->config->get_ldap_link();
71       $ldap->cd ($this->dn);
72     
73       $attrs_to_search = $this->subAttributes;
74       $attrs_to_search[] = "FAIstate";
75       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
77       while($object = $ldap->fetch()){
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   }
89   /* Reload some attributes */
90   function get_object_attributes($object,$attributes)
91   {
92     $ldap = $this->config->get_ldap_link();
93     $ldap->cd($this->config->current['BASE']);
94     $ldap->cat($object['dn'],$attributes);
95     $tmp  = $ldap->fetch();
97     foreach($attributes as $attrs){
98       if(isset($tmp[$attrs][0])){
99         $var = $tmp[$attrs][0];
101         /* Check if we must decode some attributes */
102         if(in_array_ics($attrs,$this->sub64coded)){
103           $var = base64_decode($var);
104         }
106         /*  check if this is a binary entry */
107         if(in_array_ics($attrs,$this->subBinary)){
108           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
109         }
111         $var = stripslashes($var);
112   
113         $object[$attrs] = $var;
114       }
115     }
116     return($object);
117   }
120   function execute()
121   {
122         /* Call parent execute */
123         plugin::execute();
125     /* Fill templating stuff */
126     $smarty= get_smarty();
127     $display= "";
129     /* New Listhandling
130      */
131     $once = true;
132     foreach($_POST as $name => $value){
133       if(preg_match("/^editscript_/",$name)&&($once)){
134         $once = false;
135         $entry = preg_replace("/^editscript_/","",$name);
136         $entry = base64_decode(preg_replace("/_.*/","",$entry));
138         $obj  = $this->SubObjects[$entry];
139         if($obj['status'] == "FreshLoaded"){
140           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
141         }
142         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
144         $_SESSION['objectinfo'] = $obj['dn'];
145         $this->dialog->parent = &$this;
146         $this->is_dialog=true;
147       }
148       if(preg_match("/^deletescript_/",$name)&&($once)){
149         $once = false;
150         $entry = preg_replace("/^deletescript_/","",$name);
151         $entry = base64_decode(preg_replace("/_.*/","",$entry));
152         if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
153           $this->SubObjects[$entry]['status']= "delete";
154         }else{
155           unset($this->SubObjects[$entry]);
156         }
157       }
158     }
160     /* Add new sub object */
161     if(isset($_POST['AddSubObject'])){
162       $this->dialog= new $this->subClassName($this->config,"new");
163       $this->dialog->acl = $this->acl;
164       $this->is_dialog=true;
165     }
167     if($this->dn != "new"){
168       $_SESSION['objectinfo']= $this->dn;
169     }
171     /* Save Dialog */
172     if(isset($_POST['SaveSubObject'])){
173       $this->dialog->save_object();
174       $msgs = $this->dialog->check();
175       if(count($msgs)>0){
176         foreach($msgs as $msg){
177           print_red($msg);
178         }
179       }else{
180         /* Get return object */
181         $obj = $this->dialog->save();
182         if(isset($obj['remove'])){
184           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
186           /* Depending on status, set new status */
187           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
188             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
189           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
190             unset($this->SubObjects[$obj['remove']['from']]);
191           }
192           $obj['status'] = "new";
193           $this->SubObjects[$obj['remove']['to']] = $obj;
194           unset($this->SubObjects[$obj['remove']['to']]['remove']);
195         }else{
196           if($obj['status'] == "FreshLoaded"){
197             $obj['status'] = "edited";
198           }
199           $this->SubObjects[$obj['cn']]=$obj;
200         }
201         $this->is_dialog=false;
202         unset($this->dialog);
203         $this->dialog=NULL;
204       }
205     }
207     /* Sort entries */
208     $tmp = $keys = array();
209     foreach($this->SubObjects as $key => $entry){
210       $keys[$key]=$key;
211     }
212     natcasesort($keys);
213     foreach($keys as $key){
214       $tmp[$key]=$this->SubObjects[$key];
215     }
216     $this->SubObjects = $tmp;
218     /* Cancel Dialog */
219     if(isset($_POST['CancelSubObject'])){
220       $this->is_dialog=false; 
221       unset($this->dialog);
222       $this->dialog=NULL;
223     }
225     /* Print dialog if $this->dialog is set */
226     if($this->dialog){
227       $this->dialog->save_object();
228       $display = $this->dialog->execute();
229       return($display);
230     }
232      /* Divlist Containing FAItemplates */
233     $divlist = new divSelectBox("FAItemplates");
234     $divlist->setHeight(400);
235     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
236       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
237       $img_remo = "";
238     }else{
239       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
240       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
241     }
243     foreach($this->getList(true) as $key => $name){
245       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
246         $down = "";
247       }else{
248   
249         $dn = $this->SubObjects[$key]['dn'];       
250  
251         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
252           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
253           </a>";
254       }
256       $divlist->AddEntry(array( array("string"=>$name),
257             array("string"=>$down , "attach" => "style='width:20px;'"),
258             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
259               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
260     }
261     $smarty->assign("Entry_divlist",$divlist->DrawList());
262     /* Divlist creation complete
263      */
265     $smarty->assign("SubObjects",$this->getList());
267      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
268      * If we post the escaped strings they will be escaped again
269      */
270     foreach($this->attributes as $attrs){
271       if(get_magic_quotes_gpc()){
272         $smarty->assign($attrs,stripslashes($this->$attrs));
273       }else{
274         $smarty->assign($attrs,($this->$attrs));
275       }
276     }
278     foreach($this->attributes as $attr){
279       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
280     }
282     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
283     return($display);
284   }
286   /* Generate listbox friendly SubObject list
287   */
288   function getList(){
289     $a_return=array();
290     foreach($this->SubObjects as $obj){
291       if($obj['status'] != "delete"){
292       
293         if((isset($obj['description']))&&(!empty($obj['description']))){
294           if(strlen($obj['description']) > 40){
295             $obj['description'] = substr($obj['description'],0,40)."...";
296           }
297           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
298         }else{
299           $a_return[$obj['cn']]= $obj['cn'];
300         }
301       }
302     }
303     return($a_return);
304   }
306   /* Delete me, and all my subtrees
307    */
308   function remove_from_parent()
309   {
310     $ldap = $this->config->get_ldap_link();
311     $ldap->cd ($this->dn);
313     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
314     if($_SESSION['faifilter']['branch'] == "main"){
315       $use_dn = $this->dn;
316     }
318     prepare_to_save_FAI_object($use_dn,array(),true);
320     foreach($this->SubObjects as $name => $obj){
321       $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
322       if($_SESSION['faifilter']['branch'] == "main"){
323         $use_dn = $obj['dn'];
324       }
325       prepare_to_save_FAI_object($use_dn,array(),true);
326     }
327     $this->handle_post_events("remove");    
328   }
331   /* Save data to object 
332    */
333   function save_object()
334   {
335     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
336       plugin::save_object();
337       foreach($this->attributes as $attrs){
338         if(isset($_POST[$attrs])){
339           $this->$attrs = $_POST[$attrs];
340         } 
341       }
342     }
343   }
346   /* Check supplied data */
347   function check()
348   {
349     /* Call common method to give check the hook */
350     $message= plugin::check();
352     return ($message);
353   }
356   /* Save to LDAP */
357   function save()
358   {
359     plugin::save();
361     $ldap = $this->config->get_ldap_link();
363     prepare_to_save_FAI_object($this->dn,$this->attrs);
364     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
366     /* Do object tagging */
367     $this->handle_object_tagging();
369     /* Prepare FAIscriptEntry to write it to ldap
370      * First sort array.
371      *  Because we must delete old entries first.
372      * After deletion, we perform add and modify 
373      */
374     $Objects = array();
375     foreach($this->SubObjects as $name => $obj){
376       if($obj['status'] == "FreshLoaded"){
377         unset($this->SubObjects[$name]);
378       }
379     }
381     foreach($this->SubObjects as $name => $obj){
382       if($obj['status'] == "delete"){
383         $Objects[$name] = $obj; 
384       }
385     }
386     foreach($this->SubObjects as $name => $obj){
387       if($obj['status'] != "delete"){
388         $Objects[$name] = $obj; 
389       }
390     }
392     foreach($Objects as $name => $obj){
394       foreach($this->sub64coded as $codeIt){
395         $obj[$codeIt]=base64_encode($obj[$codeIt]);
396       }
397       $tmp = array();
398       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
399       foreach($attributes as $attrs){
400         if(empty($obj[$attrs])){
401           $obj[$attrs] = array();
402         }
403         $tmp[$attrs] = stripslashes($obj[$attrs]);
404       }    
405         
406       $tmp['objectClass'] = $this->subClasses;
408       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
410       if($obj['status']=="new"){
411         $ldap->cat($sub_dn,array("objectClass"));
412         if($ldap->count()){
413           $obj['status']="edited";
414         }
415       }
417        /* Check if gosaAdministrativeUnitTag is required as object class */
418       if($obj['status'] == "edited"){
419         $ldap->cat($sub_dn,array("objectClass"));
420         $attrs = $ldap->fetch();
421         if(isset($attrs['objectClass'])){
422           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
423             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
424           }
425         }
426       }
428       if($obj['status'] == "delete"){
429         prepare_to_save_FAI_object($sub_dn,array(),true);
430         $this->handle_post_events("remove");
431       }elseif($obj['status'] == "edited"){
432         prepare_to_save_FAI_object($sub_dn,$tmp);
433         $this->handle_post_events("modify");
434       }elseif($obj['status']=="new"){
435         prepare_to_save_FAI_object($sub_dn,$tmp);
436         $this->handle_post_events("add");
437       }
440       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
441     }
442   }
443   
445   /* Return plugin informations for acl handling */ 
446   function plInfo()
447   {
448     return (array( 
449           "plShortName" => _("Template"),
450           "plDescription" => _("FAI template"),
451           "plSelfModify"  => FALSE,
452           "plDepends"     => array(),
453           "plPriority"    => 0,
454           "plSection"     => array("administration"),
455           "plCategory"    => array("fai"),
456           "plProvidedAcls" => array(
457             "cn"                => _("Name"),
458             "description"       => _("Description"),
459             "FAItemplateFile"   => _("Template file"),
460             "FAItemplatePath"   => _("Template path"),
461             "FAIowner"          => _("File owner"),
462             "FAImode"           => _("File permissions"))
463           ));
464   }
467 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
468 ?>