Code

Initial FAI copy & paste test.
[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;
44   function faiTemplate ($config, $dn= NULL)
45   {
46     /* Load Attributes */
47     plugin::plugin ($config, $dn);
49     $this->acl="#all#";
51     /* If "dn==new" we try to create a new entry
52      * Else we must read all objects from ldap which belong to this entry.
53      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
54      */
55      if($dn != "new"){
56       $this->dn =$dn;
58       /* Set acls
59        */
60       $ui   = get_userinfo();
61       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
62       $acli = get_module_permission($acl, "FAIclass", $this->dn);
63       $this->acl=$acli;
65       /* Get FAIstate
66        */
67       if(isset($this->attrs['FAIstate'][0])){
68         $this->FAIstate = $this->attrs['FAIstate'][0];
69       }
71       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
72        */
73       $ldap     = $this->config->get_ldap_link();
74       $ldap->cd ($this->dn);
75       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
77       while($object = $ldap->fetch()){
79         /* Set status for save management */
80         $objects = array();
81         $objects['status']      = "FreshLoaded";
82         $objects['dn']          = $object['dn'];
83         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
84         $this->SubObjects[$objects['cn']] = $objects;
85       }
86     }
87   }
90   /* Reload some attributes */
91   function get_object_attributes($object,$attributes)
92   {
93     $ldap = $this->config->get_ldap_link();
94     $ldap->cd($this->config->current['BASE']);
95     $ldap->cat($object['dn'],$attributes);
96     $tmp  = $ldap->fetch();
98     foreach($attributes as $attrs){
99       if(isset($tmp[$attrs][0])){
100         $var = $tmp[$attrs][0];
102         /* Check if we must decode some attributes */
103         if(in_array_ics($attrs,$this->sub64coded)){
104           $var = base64_decode($var);
105         }
107         /*  check if this is a binary entry */
108         if(in_array_ics($attrs,$this->subBinary)){
109           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110         }
112         /* Fix slashes */
113         $var = addslashes($var);
115         $object[$attrs] = $var;
116       }
117     }
118     return($object);
119   }
122   function execute()
123   {
124         /* Call parent execute */
125         plugin::execute();
127     /* Fill templating stuff */
128     $smarty= get_smarty();
129     $display= "";
131     /* New Listhandling
132      */
133     $once = true;
134     foreach($_POST as $name => $value){
135       if(preg_match("/^editscript_/",$name)&&($once)){
136         $once = false;
137         $entry = preg_replace("/^editscript_/","",$name);
138         $entry = base64_decode(preg_replace("/_.*/","",$entry));
140         $obj  = $this->SubObjects[$entry];
141         if($obj['status'] == "FreshLoaded"){
142           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
143         }
145         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
146         $this->dialog->acl = $this->acl;
147         $_SESSION['objectinfo'] = $obj['dn'];
148         $this->dialog->parent = &$this;
149         $this->is_dialog=true;
150       }
151       if(preg_match("/^deletescript_/",$name)&&($once)){
152         $once = false;
153         $entry = preg_replace("/^deletescript_/","",$name);
154         $entry = base64_decode(preg_replace("/_.*/","",$entry));
156         $status = $this->SubObjects[$entry]['status'];
157         if($status == "edited" || $status == "FreshLoaded"){
158           $this->SubObjects[$entry]['status']= "delete";
159         }else{
160           unset($this->SubObjects[$entry]);
161         }
162       }
163     }
165     /* Add new sub object */
166     if(isset($_POST['AddSubObject'])){
167       $this->dialog= new $this->subClassName($this->config,"new");
168       $this->dialog->acl = $this->acl;
169       $this->is_dialog=true;
170     }
172     if($this->dn != "new"){
173       $_SESSION['objectinfo']= $this->dn;
174     }
176     /* Save Dialog */
177     if(isset($_POST['SaveSubObject'])){
178       $this->dialog->save_object();
179       $msgs = $this->dialog->check();
180       if(count($msgs)>0){
181         foreach($msgs as $msg){
182           print_red($msg);
183         }
184       }else{
186         /* Get return object */
187         $obj = $this->dialog->save();
188         if(isset($obj['remove'])){
189           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
191           /* Depending on status, set new status */
192           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
193             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
194           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
195             unset($this->SubObjects[$obj['remove']['from']]);
196           }
197           $obj['status'] = "new";
198           $this->SubObjects[$obj['remove']['to']] = $obj;
199           unset($this->SubObjects[$obj['remove']['to']]['remove']);
200         }else{
201           if($obj['status'] == "FreshLoaded"){
202             $obj['status'] = "edited";
203           }
204           $this->SubObjects[$obj['cn']]=$obj;
205         }
207         $this->is_dialog=false;
208         unset($this->dialog);
209         $this->dialog=NULL;
210       }
211     }
213     /* Sort entries */
214     $tmp = $keys = array();
215     foreach($this->SubObjects as $key => $entry){
216       $keys[$key]=$key;
217     }
218     natcasesort($keys);
219     foreach($keys as $key){
220       $tmp[$key]=$this->SubObjects[$key];
221     }
222     $this->SubObjects = $tmp;
224     /* Cancel Dialog */
225     if(isset($_POST['CancelSubObject'])){
226       $this->is_dialog=false; 
227       unset($this->dialog);
228       $this->dialog=NULL;
229     }
231     /* Print dialog if $this->dialog is set */
232     if($this->dialog){
233       $this->dialog->save_object();
234       $display = $this->dialog->execute();
235       return($display);
236     }
238      /* Divlist Containing FAItemplates */
239     $divlist = new divSelectBox("FAItemplates");
240     $divlist->setHeight(400);
241     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
242       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
243       $img_remo = "";
244     }else{
245       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
246       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
247     }
249     foreach($this->getList(true) as $key => $name){
251       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
252         $down = "";
253       }else{
254   
255         $dn = $this->SubObjects[$key]['dn'];       
256  
257         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
258           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
259           </a>";
260       }
262       $divlist->AddEntry(array( array("string"=>$name),
263             array("string"=>$down , "attach" => "style='width:20px;'"),
264             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
265               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
266     }
267     $smarty->assign("Entry_divlist",$divlist->DrawList());
268     /* Divlist creation complete
269      */
271     $smarty->assign("SubObjects",$this->getList());
273      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
274      * If we post the escaped strings they will be escaped again
275      */
276     foreach($this->attributes as $attrs){
277       if(get_magic_quotes_gpc()){
278         $smarty->assign($attrs,stripslashes($this->$attrs));
279       }else{
280         $smarty->assign($attrs,($this->$attrs));
281       }
282     }
284     foreach($this->attributes as $attr){
285       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
286     }
288     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
289     return($display);
290   }
292   /* Generate listbox friendly SubObject list
293   */
294   function getList(){
295     $a_return=array();
296     foreach($this->SubObjects as $obj){
297       if($obj['status'] != "delete"){
298       
299         if((isset($obj['description']))&&(!empty($obj['description']))){
300           if(strlen($obj['description']) > 40){
301             $obj['description'] = substr($obj['description'],0,40)."...";
302           }
303           $a_return[$obj['cn']]= $obj['cn']." [".stripslashes( $obj['description'] )."]";
304         }else{
305           $a_return[$obj['cn']]= $obj['cn'];
306         }
307       }
308     }
309     return($a_return);
310   }
312   /* Delete me, and all my subtrees
313    */
314   function remove_from_parent()
315   {
316     $ldap = $this->config->get_ldap_link();
317     $ldap->cd ($this->dn);
318     $ldap->rmdir_recursive($this->dn);
319     $this->handle_post_events("remove");    
320   }
323   /* Save data to object 
324    */
325   function save_object()
326   {
327     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
328       plugin::save_object();
329       foreach($this->attributes as $attrs){
330         if(isset($_POST[$attrs])){
331           $this->$attrs = $_POST[$attrs];
332         } 
333       }
334     }
335   }
338   /* Check supplied data */
339   function check()
340   {
341     /* Call common method to give check the hook */
342     $message= plugin::check();
344     /* If this is a new script, check if a script with this name already exists */
345     if(!empty($this->release) && $this->copy_paste_mode){
347       /* Check if current name is already used for fai scripts in selected release */
348       $dn = 'cn='.$this->cn.",ou=templates,".$this->release;
349       $ldap = $this->config->get_ldap_link();
350       $ldap->cat($dn);
351       if($ldap->count()){
353         $r =convert_department_dn($this->release);;
354         $message[] = sprintf(_("Can't insert a fai template named '%s' in '%s' there is already a template with the given name."),$this->cn,$r);
355       }
356     }
357     return ($message);
358   }
361   /* Save to LDAP */
362   function save()
363   {
364     plugin::save();
366     $ldap = $this->config->get_ldap_link();
368     $ldap->cat($this->dn,array("objectClass"));
369     if($ldap->count()!=0){
370       /* Write FAIscript to ldap*/
371       $ldap->cd($this->dn);
372       $this->cleanup();
373       $ldap->modify ($this->attrs); 
375     }else{
376       /* Write FAIscript to ldap*/
377       $ldap->cd($this->config->current['BASE']);
378       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
379       $ldap->cd($this->dn);
380       $ldap->add($this->attrs);
381     }
382     show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
384     /* Do object tagging */
385     $this->handle_object_tagging();
387     /* Prepare FAIscriptEntry to write it to ldap
388      * First sort array.
389      *  Because we must delete old entries first.
390      * After deletion, we perform add and modify 
391      */
392     $Objects = array();
394     /* We do not need to save untouched objects */
395     foreach($this->SubObjects as $name => $obj){
396       if($obj['status'] == "FreshLoaded"){
397         unset($this->SubObjects[$name]);
398       }
399     }
401     foreach($this->SubObjects as $name => $obj){
402       if($obj['status'] == "delete"){
403         $Objects[$name] = $obj; 
404       }
405     }
407     foreach($this->SubObjects as $name => $obj){
408       if($obj['status'] != "delete"){
409         $Objects[$name] = $obj; 
410       }
411     }
413     foreach($Objects as $name => $obj){
415       foreach($this->sub64coded as $codeIt){
416         $obj[$codeIt]=base64_encode($obj[$codeIt]);
417       }
418       $tmp = array();
419       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
420       foreach($attributes as $attrs){
421         if(empty($obj[$attrs])){
422           $obj[$attrs] = array();
423         }
424         $tmp[$attrs] = stripslashes ($obj[$attrs]);
425       }    
426         
427       $tmp['objectClass'] = $this->subClasses;
429       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
431       if($obj['status']=="new"){
432         $ldap->cat($sub_dn,array("objectClass"));
433         if($ldap->count()){
434           $obj['status']="edited";
435         }
436       }
438        /* Check if gosaAdministrativeUnitTag is required as object class */
439       if($obj['status'] == "edited"){
440         $ldap->cat($sub_dn,array("objectClass"));
441         $attrs = $ldap->fetch();
442         if(isset($attrs['objectClass'])){
443           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
444             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
445           }
446         }
447       }
449       if($obj['status'] == "delete"){
450         $ldap->cd($sub_dn);
451         $ldap->rmdir_recursive($sub_dn);
452         $this->handle_post_events("remove");
453         show_ldap_error($ldap->get_error(), _("Removing FAI template base failed")); 
454       }elseif($obj['status'] == "edited"){
455         $ldap->cd($sub_dn);
456         $this->cleanup();
457         $ldap->modify ($tmp); 
458         show_ldap_error($ldap->get_error(), _("Saving FAI template failed")); 
460         $this->handle_post_events("modify");
461       }elseif($obj['status']=="new"){
463         if($tmp['description']==array()){
464           unset($tmp['description']);
465         }
466         $ldap->cd($this->config->current['BASE']);
467         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
468         $ldap->cd($sub_dn);
469         $ldap->add($tmp); 
470         $this->handle_post_events("add");
471         show_ldap_error($ldap->get_error(), _("Saving FAI template failed")); 
472       }
474       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
475     }
476   }
479 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
480 ?>