Code

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