Code

Updated fai copy&paste stuff
[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         = "";
40   var $base             = "";
41   var $release          = "";
42   var $copy_paste_mode  = false;
43   var $cut_paste_mode   = false;
45   var $CopyPasteVars  = array("SubObjects","FAIstate");
47   function faiTemplate ($config, $dn= NULL)
48   {
49     /* Load Attributes */
50     plugin::plugin ($config, $dn);
52     $this->acl="#all#";
54     /* If "dn==new" we try to create a new entry
55      * Else we must read all objects from ldap which belong to this entry.
56      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
57      */
58      if($dn != "new"){
59       $this->dn =$dn;
61       /* Set acls
62        */
63       $ui   = get_userinfo();
64       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
65       $acli = get_module_permission($acl, "FAIclass", $this->dn);
66       $this->acl=$acli;
68       /* Get FAIstate
69        */
70       if(isset($this->attrs['FAIstate'][0])){
71         $this->FAIstate = $this->attrs['FAIstate'][0];
72       }
74       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
75        */
76       $ldap     = $this->config->get_ldap_link();
77       $ldap->cd ($this->dn);
78       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
80       while($object = $ldap->fetch()){
82         /* Set status for save management */
83         $objects = array();
84         $objects['status']      = "FreshLoaded";
85         $objects['dn']          = $object['dn'];
86         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
87         $this->SubObjects[$objects['cn']] = $objects;
88       }
89     }
90   }
93   /* Reload some attributes */
94   function get_object_attributes($object,$attributes)
95   {
96     $ldap = $this->config->get_ldap_link();
97     $ldap->cd($this->config->current['BASE']);
98     $ldap->cat($object['dn'],$attributes);
99     $tmp  = $ldap->fetch();
101     foreach($attributes as $attrs){
102       if(isset($tmp[$attrs][0])){
103         $var = $tmp[$attrs][0];
105         /* Check if we must decode some attributes */
106         if(in_array_ics($attrs,$this->sub64coded)){
107           $var = base64_decode($var);
108         }
110         /*  check if this is a binary entry */
111         if(in_array_ics($attrs,$this->subBinary)){
112           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
113         }
115         /* Fix slashes */
116         $var = addslashes($var);
118         $object[$attrs] = $var;
119       }
120     }
121     return($object);
122   }
125   function execute()
126   {
127         /* Call parent execute */
128         plugin::execute();
130     /* Fill templating stuff */
131     $smarty= get_smarty();
132     $display= "";
134     /* New Listhandling
135      */
136     $once = true;
137     foreach($_POST as $name => $value){
138       if(preg_match("/^editscript_/",$name)&&($once)){
139         $once = false;
140         $entry = preg_replace("/^editscript_/","",$name);
141         $entry = base64_decode(preg_replace("/_.*/","",$entry));
143         $obj  = $this->SubObjects[$entry];
144         if($obj['status'] == "FreshLoaded"){
145           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
146         }
148         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
149         $this->dialog->acl = $this->acl;
150         $_SESSION['objectinfo'] = $obj['dn'];
151         $this->dialog->parent = &$this;
152         $this->is_dialog=true;
153       }
154       if(preg_match("/^deletescript_/",$name)&&($once)){
155         $once = false;
156         $entry = preg_replace("/^deletescript_/","",$name);
157         $entry = base64_decode(preg_replace("/_.*/","",$entry));
159         $status = $this->SubObjects[$entry]['status'];
160         if($status == "edited" || $status == "FreshLoaded"){
161           $this->SubObjects[$entry]['status']= "delete";
162         }else{
163           unset($this->SubObjects[$entry]);
164         }
165       }
166     }
168     /* Add new sub object */
169     if(isset($_POST['AddSubObject'])){
170       $this->dialog= new $this->subClassName($this->config,"new");
171       $this->dialog->acl = $this->acl;
172       $this->is_dialog=true;
173     }
175     if($this->dn != "new"){
176       $_SESSION['objectinfo']= $this->dn;
177     }
179     /* Save Dialog */
180     if(isset($_POST['SaveSubObject'])){
181       $this->dialog->save_object();
182       $msgs = $this->dialog->check();
183       if(count($msgs)>0){
184         foreach($msgs as $msg){
185           print_red($msg);
186         }
187       }else{
189         /* Get return object */
190         $obj = $this->dialog->save();
191         if(isset($obj['remove'])){
192           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
194           /* Depending on status, set new status */
195           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
196             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
197           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
198             unset($this->SubObjects[$obj['remove']['from']]);
199           }
200           $obj['status'] = "new";
201           $this->SubObjects[$obj['remove']['to']] = $obj;
202           unset($this->SubObjects[$obj['remove']['to']]['remove']);
203         }else{
204           if($obj['status'] == "FreshLoaded"){
205             $obj['status'] = "edited";
206           }
207           $this->SubObjects[$obj['cn']]=$obj;
208         }
210         $this->is_dialog=false;
211         unset($this->dialog);
212         $this->dialog=NULL;
213       }
214     }
216     /* Sort entries */
217     $tmp = $keys = array();
218     foreach($this->SubObjects as $key => $entry){
219       $keys[$key]=$key;
220     }
221     natcasesort($keys);
222     foreach($keys as $key){
223       $tmp[$key]=$this->SubObjects[$key];
224     }
225     $this->SubObjects = $tmp;
227     /* Cancel Dialog */
228     if(isset($_POST['CancelSubObject'])){
229       $this->is_dialog=false; 
230       unset($this->dialog);
231       $this->dialog=NULL;
232     }
234     /* Print dialog if $this->dialog is set */
235     if($this->dialog){
236       $this->dialog->save_object();
237       $display = $this->dialog->execute();
238       return($display);
239     }
241      /* Divlist Containing FAItemplates */
242     $divlist = new divSelectBox("FAItemplates");
243     $divlist->setHeight(400);
244     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
245       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
246       $img_remo = "";
247     }else{
248       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
249       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
250     }
252     foreach($this->getList(true) as $key => $name){
254       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
255         $down = "";
256       }else{
257   
258         $dn = $this->SubObjects[$key]['dn'];       
259  
260         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
261           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
262           </a>";
263       }
265       $divlist->AddEntry(array( array("string"=>$name),
266             array("string"=>$down , "attach" => "style='width:20px;'"),
267             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
268               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
269     }
270     $smarty->assign("Entry_divlist",$divlist->DrawList());
271     /* Divlist creation complete
272      */
274     $smarty->assign("SubObjects",$this->getList());
276      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
277      * If we post the escaped strings they will be escaped again
278      */
279     foreach($this->attributes as $attrs){
280       if(get_magic_quotes_gpc()){
281         $smarty->assign($attrs,stripslashes($this->$attrs));
282       }else{
283         $smarty->assign($attrs,($this->$attrs));
284       }
285     }
287     foreach($this->attributes as $attr){
288       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
289     }
291     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
292     return($display);
293   }
295   /* Generate listbox friendly SubObject list
296   */
297   function getList(){
298     $a_return=array();
299     foreach($this->SubObjects as $obj){
300       if($obj['status'] != "delete"){
301       
302         if((isset($obj['description']))&&(!empty($obj['description']))){
303           if(strlen($obj['description']) > 40){
304             $obj['description'] = substr($obj['description'],0,40)."...";
305           }
306           $a_return[$obj['cn']]= $obj['cn']." [".stripslashes( $obj['description'] )."]";
307         }else{
308           $a_return[$obj['cn']]= $obj['cn'];
309         }
310       }
311     }
312     return($a_return);
313   }
315   /* Delete me, and all my subtrees
316    */
317   function remove_from_parent()
318   {
319     $ldap = $this->config->get_ldap_link();
320     $ldap->cd ($this->dn);
321     $ldap->rmdir_recursive($this->dn);
322     $this->handle_post_events("remove");    
323   }
326   /* Save data to object 
327    */
328   function save_object()
329   {
330     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
331       plugin::save_object();
332       foreach($this->attributes as $attrs){
333         if(isset($_POST[$attrs])){
334           $this->$attrs = $_POST[$attrs];
335         } 
336       }
337     }
338   }
341   /* Check supplied data */
342   function check()
343   {
344     /* Call common method to give check the hook */
345     $message= plugin::check();
347     /* If this is a new script, check if a script with this name already exists */
348     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
350       /* Check if current name is already used for fai scripts in selected release */
351       $dn = 'cn='.$this->cn.",ou=templates,".$this->release;
352       $ldap = $this->config->get_ldap_link();
353       $ldap->cat($dn);
354       if($ldap->count()){
356         $r =convert_department_dn($this->release);;
357         $message[] = sprintf(_("Can't insert a fai template named '%s' in '%s' there is already a template with the given name."),$this->cn,$r);
358       }
359     }
360     return ($message);
361   }
364   /* Save to LDAP */
365   function save()
366   {
367     plugin::save();
369     $ldap = $this->config->get_ldap_link();
371     /* Copy & Paste : Ensure that FAIstate is copied too */
372     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
373       $this->attrs['FAIstate'] = $this->FAIstate;
374     }
376     $ldap->cat($this->dn,array("objectClass"));
377     if($ldap->count()!=0){
378       /* Write FAIscript to ldap*/
379       $ldap->cd($this->dn);
380       $this->cleanup();
381       $ldap->modify ($this->attrs); 
383     }else{
384       /* Write FAIscript to ldap*/
385       $ldap->cd($this->config->current['BASE']);
386       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
387       $ldap->cd($this->dn);
388       $ldap->add($this->attrs);
389     }
390     show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
392     /* Do object tagging */
393     $this->handle_object_tagging();
395     /* Prepare FAIscriptEntry to write it to ldap
396      * First sort array.
397      *  Because we must delete old entries first.
398      * After deletion, we perform add and modify 
399      */
400     $Objects = array();
402     /* We do not need to save untouched objects */
403     foreach($this->SubObjects as $name => $obj){
404       if($obj['status'] == "FreshLoaded"){
405         if($this->copy_paste_mode){
406           $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
407           $this->SubObjects[$name]['status'] = "new";
408         }else{
409           unset($this->SubObjects[$name]);
410         }
411       }
412     }
414     foreach($this->SubObjects as $name => $obj){
415       if($obj['status'] == "delete"){
416         $Objects[$name] = $obj; 
417       }
418     }
420     foreach($this->SubObjects as $name => $obj){
421       if($obj['status'] != "delete"){
422         $Objects[$name] = $obj; 
423       }
424     }
426     foreach($Objects as $name => $obj){
428       foreach($this->sub64coded as $codeIt){
429         $obj[$codeIt]=base64_encode($obj[$codeIt]);
430       }
431       $tmp = array();
432       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
433       foreach($attributes as $attrs){
434         if(empty($obj[$attrs])){
435           $obj[$attrs] = array();
436           $tmp[$attrs] = $obj[$attrs];
437         }else{
438           $tmp[$attrs] = stripslashes ($obj[$attrs]);
439         }
440       }    
441         
442       $tmp['objectClass'] = $this->subClasses;
444       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
446       if($obj['status']=="new"){
447         $ldap->cat($sub_dn,array("objectClass"));
448         if($ldap->count()){
449           $obj['status']="edited";
450         }
451       }
453        /* Check if gosaAdministrativeUnitTag is required as object class */
454       if($obj['status'] == "edited"){
455         $ldap->cat($sub_dn,array("objectClass"));
456         $attrs = $ldap->fetch();
457         if(isset($attrs['objectClass'])){
458           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
459             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
460           }
461         }
462       }
464       if($obj['status'] == "delete"){
465         $ldap->cd($sub_dn);
466         $ldap->rmdir_recursive($sub_dn);
467         $this->handle_post_events("remove");
468         show_ldap_error($ldap->get_error(), _("Removing FAI template entry failed")); 
469       }elseif($obj['status'] == "edited"){
470         $ldap->cd($sub_dn);
471         $this->cleanup();
472         $ldap->modify ($tmp); 
473         show_ldap_error($ldap->get_error(), _("Modifying FAI template entry failed")); 
475         $this->handle_post_events("modify");
476       }elseif($obj['status']=="new"){
478         if($tmp['description']==array()){
479           unset($tmp['description']);
480         }
481         $ldap->cd($this->config->current['BASE']);
482         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
483         $ldap->cd($sub_dn);
484         $ldap->add($tmp); 
485         $this->handle_post_events("add");
486         show_ldap_error($ldap->get_error(), _("Creating FAI template entry failed")); 
487       }
489       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
490     }
491   }
492   
493   /* return copy & paste dialog
494    */
495   function getCopyDialog()
496   {
497     /* Ask for cn */
498     $smarty = get_smarty();
499     $smarty->assign("cn" ,$this->cn);
500     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
501     $ret = array();
502     $ret['string'] = $str;
503     $ret['status'] = "";
504     return($ret);
505   }
507   /* Get posted cn */
508   function saveCopyDialog()
509   {
510     if(isset($_POST['cn'])){
511       $this->cn = $_POST['cn'];
512     }
513   }
516 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
517 ?>