Code

Templates were broken when downloaded
[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 $sub64coded       = array();
29   var $sub_Load_Later   = array("FAItemplateFile");
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       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
74       while($object = $ldap->fetch()){
76         /* Set status for save management */
77         $objects = array();
78         $objects['status']      = "FreshLoaded";
79         $objects['dn']          = $object['dn'];
80         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
81         $this->SubObjects[$objects['cn']] = $objects;
82       }
83     }
84   }
87   /* Reload some attributes */
88   function get_object_attributes($object,$attributes)
89   {
90     $ldap = $this->config->get_ldap_link();
91     $ldap->cd($this->config->current['BASE']);
92     $ldap->cat($object['dn'],$attributes);
93     $tmp  = $ldap->fetch();
95     foreach($attributes as $attrs){
96       if(isset($tmp[$attrs][0])){
97         $var = $tmp[$attrs][0];
99         /* Check if we must decode some attributes */
100         if(in_array_ics($attrs,$this->sub64coded)){
101           $var = base64_decode($var);
102         }
104         /*  check if this is a binary entry */
105         if(in_array_ics($attrs,$this->subBinary)){
106           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
107         }
109         /* Fix slashes */
110         $var = addslashes($var);
112         $object[$attrs] = $var;
113       }
114     }
115     return($object);
116   }
119   function execute()
120   {
121         /* Call parent execute */
122         plugin::execute();
124     /* Fill templating stuff */
125     $smarty= get_smarty();
126     $display= "";
128     /* New Listhandling
129      */
130     $once = true;
131     foreach($_POST as $name => $value){
132       if(preg_match("/^editscript_/",$name)&&($once)){
133         $once = false;
134         $entry = preg_replace("/^editscript_/","",$name);
135         $entry = base64_decode(preg_replace("/_.*/","",$entry));
137         $obj  = $this->SubObjects[$entry];
138         if($obj['status'] == "FreshLoaded"){
139           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
140         }
142         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
143         $this->dialog->acl = $this->acl;
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"){
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{
181         /* Get return object */
182         $obj = $this->dialog->save();
183         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         }
202         $this->is_dialog=false;
203         unset($this->dialog);
204         $this->dialog=NULL;
205       }
206     }
208     /* Sort entries */
209     $tmp = $keys = array();
210     foreach($this->SubObjects as $key => $entry){
211       $keys[$key]=$key;
212     }
213     natcasesort($keys);
214     foreach($keys as $key){
215       $tmp[$key]=$this->SubObjects[$key];
216     }
217     $this->SubObjects = $tmp;
219     /* Cancel Dialog */
220     if(isset($_POST['CancelSubObject'])){
221       $this->is_dialog=false; 
222       unset($this->dialog);
223       $this->dialog=NULL;
224     }
226     /* Print dialog if $this->dialog is set */
227     if($this->dialog){
228       $this->dialog->save_object();
229       $display = $this->dialog->execute();
230       return($display);
231     }
233      /* Divlist Containing FAItemplates */
234     $divlist = new divSelectBox("FAItemplates");
235     $divlist->setHeight(400);
236     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
237       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
238       $img_remo = "";
239     }else{
240       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
241       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
242     }
244     foreach($this->getList(true) as $key => $name){
246       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
247         $down = "";
248       }else{
249   
250         $dn = $this->SubObjects[$key]['dn'];       
251  
252         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
253           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
254           </a>";
255       }
257       $divlist->AddEntry(array( array("string"=>$name),
258             array("string"=>$down , "attach" => "style='width:20px;'"),
259             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
260               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
261     }
262     $smarty->assign("Entry_divlist",$divlist->DrawList());
263     /* Divlist creation complete
264      */
266     $smarty->assign("SubObjects",$this->getList());
268      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
269      * If we post the escaped strings they will be escaped again
270      */
271     foreach($this->attributes as $attrs){
272       if(get_magic_quotes_gpc()){
273         $smarty->assign($attrs,stripslashes($this->$attrs));
274       }else{
275         $smarty->assign($attrs,($this->$attrs));
276       }
277     }
279     foreach($this->attributes as $attr){
280       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
281     }
283     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
284     return($display);
285   }
287   /* Generate listbox friendly SubObject list
288   */
289   function getList(){
290     $a_return=array();
291     foreach($this->SubObjects as $obj){
292       if($obj['status'] != "delete"){
293       
294         if((isset($obj['description']))&&(!empty($obj['description']))){
295           if(strlen($obj['description']) > 40){
296             $obj['description'] = substr($obj['description'],0,40)."...";
297           }
298           $a_return[$obj['cn']]= $obj['cn']." [".stripslashes( $obj['description'] )."]";
299         }else{
300           $a_return[$obj['cn']]= $obj['cn'];
301         }
302       }
303     }
304     return($a_return);
305   }
307   /* Delete me, and all my subtrees
308    */
309   function remove_from_parent()
310   {
311     $ldap = $this->config->get_ldap_link();
312     $ldap->cd ($this->dn);
313     $ldap->rmdir_recursive($this->dn);
314     $this->handle_post_events("remove");    
315   }
318   /* Save data to object 
319    */
320   function save_object()
321   {
322     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
323       plugin::save_object();
324       foreach($this->attributes as $attrs){
325         if(isset($_POST[$attrs])){
326           $this->$attrs = $_POST[$attrs];
327         } 
328       }
329     }
330   }
333   /* Check supplied data */
334   function check()
335   {
336     /* Call common method to give check the hook */
337     $message= plugin::check();
339     return ($message);
340   }
343   /* Save to LDAP */
344   function save()
345   {
346     plugin::save();
348     $ldap = $this->config->get_ldap_link();
350     $ldap->cat($this->dn,array("objectClass"));
351     if($ldap->count()!=0){
352       /* Write FAIscript to ldap*/
353       $ldap->cd($this->dn);
354       $this->cleanup();
355       $ldap->modify ($this->attrs); 
357     }else{
358       /* Write FAIscript to ldap*/
359       $ldap->cd($this->config->current['BASE']);
360       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
361       $ldap->cd($this->dn);
362       $ldap->add($this->attrs);
363     }
364     show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
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();
376     /* We do not need to save untouched objects */
377     foreach($this->SubObjects as $name => $obj){
378       if($obj['status'] == "FreshLoaded"){
379         unset($this->SubObjects[$name]);
380       }
381     }
383     foreach($this->SubObjects as $name => $obj){
384       if($obj['status'] == "delete"){
385         $Objects[$name] = $obj; 
386       }
387     }
389     foreach($this->SubObjects as $name => $obj){
390       if($obj['status'] != "delete"){
391         $Objects[$name] = $obj; 
392       }
393     }
395     foreach($Objects as $name => $obj){
397       foreach($this->sub64coded as $codeIt){
398         $obj[$codeIt]=base64_encode($obj[$codeIt]);
399       }
400       $tmp = array();
401       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
402       foreach($attributes as $attrs){
403         if(empty($obj[$attrs])){
404           $obj[$attrs] = array();
405         }
406         $tmp[$attrs] = stripslashes ($obj[$attrs]);
407       }    
408         
409       $tmp['objectClass'] = $this->subClasses;
411       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
413       if($obj['status']=="new"){
414         $ldap->cat($sub_dn,array("objectClass"));
415         if($ldap->count()){
416           $obj['status']="edited";
417         }
418       }
420        /* Check if gosaAdministrativeUnitTag is required as object class */
421       if($obj['status'] == "edited"){
422         $ldap->cat($sub_dn,array("objectClass"));
423         $attrs = $ldap->fetch();
424         if(isset($attrs['objectClass'])){
425           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
426             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
427           }
428         }
429       }
431       if($obj['status'] == "delete"){
432         $ldap->cd($sub_dn);
433         $ldap->rmdir_recursive($sub_dn);
434         $this->handle_post_events("remove");
435         show_ldap_error($ldap->get_error(), _("Removing FAI template base failed")); 
436       }elseif($obj['status'] == "edited"){
437         $ldap->cd($sub_dn);
438         $this->cleanup();
439         $ldap->modify ($tmp); 
440         show_ldap_error($ldap->get_error(), _("Saving FAI template failed")); 
442         $this->handle_post_events("modify");
443       }elseif($obj['status']=="new"){
445         if($tmp['description']==array()){
446           unset($tmp['description']);
447         }
448         $ldap->cd($this->config->current['BASE']);
449         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
450         $ldap->cd($sub_dn);
451         $ldap->add($tmp); 
452         $this->handle_post_events("add");
453         show_ldap_error($ldap->get_error(), _("Saving FAI template failed")); 
454       }
456       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
457     }
458   }
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>