Code

Added several comments
[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         = "";
40   var $ui;
42   function faiTemplate ($config, $dn= NULL)
43   {
44     /* Load Attributes */
45     plugin::plugin ($config, $dn);
47     /* If "dn==new" we try to create a new entry
48      * Else we must read all objects from ldap which belong to this entry.
49      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
50      */
51     if($dn != "new"){
52       $this->dn =$dn;
54       /* Get FAIstate
55        */
56       if(isset($this->attrs['FAIstate'][0])){
57         $this->FAIstate = $this->attrs['FAIstate'][0];
58       }
60       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
61        */
62       $ldap     = $this->config->get_ldap_link();
63       $ldap->cd ($this->dn);
64     
65       $attrs_to_search = $this->subAttributes;
66       $attrs_to_search[] = "FAIstate";
67       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
69       while($object = $ldap->fetch()){
70         /* Set status for save management */
71         $objects = array();
72         $objects['status']      = "FreshLoaded";
73         $objects['dn']          = $object['dn'];
74         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
75         $this->SubObjects[$objects['cn']] = $objects;
76       }
77     }
78     $this->ui = get_userinfo();
79   }
82   /* Reload some attributes */
83   function get_object_attributes($object,$attributes)
84   {
85     $ldap = $this->config->get_ldap_link();
86     $ldap->cd($this->config->current['BASE']);
87     $ldap->cat($object['dn'],$attributes);
88     $tmp  = $ldap->fetch();
90     foreach($attributes as $attrs){
91       if(isset($tmp[$attrs][0])){
92         $var = $tmp[$attrs][0];
94         /* Check if we must decode some attributes */
95         if(in_array_ics($attrs,$this->sub64coded)){
96           $var = base64_decode($var);
97         }
99         /*  check if this is a binary entry */
100         if(in_array_ics($attrs,$this->subBinary)){
101           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
102         }
104         $var = stripslashes($var);
105   
106         $object[$attrs] = $var;
107       }
108     }
109     return($object);
110   }
113   function execute()
114   {
115     /* Call parent execute */
116     plugin::execute();
118     /* Fill templating stuff */
119     $smarty= get_smarty();
120     $display= "";
122     /* New Listhandling
123      */
124     $once = true;
125     foreach($_POST as $name => $value){
126       if(preg_match("/^editscript_/",$name)&&($once)){
127         $once = false;
128         $entry = preg_replace("/^editscript_/","",$name);
129         $entry = base64_decode(preg_replace("/_.*/","",$entry));
130         $obj  = $this->SubObjects[$entry];
132         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
133         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
134         if(preg_match("/r/",$acl)){
135           if($obj['status'] == "FreshLoaded"){
136             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
137           }
138           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
139           $this->dialog->set_acl_base($this->acl_base);
140           $this->dialog->set_acl_category("fai");
142           $_SESSION['objectinfo'] = $obj['dn'];
143           $this->dialog->parent = &$this;
144           $this->is_dialog=true;
145         }
146       }
147       if(preg_match("/^deletescript_/",$name)&&($once)){
148         $once = false;
149         $entry = preg_replace("/^deletescript_/","",$name);
150         $entry = base64_decode(preg_replace("/_.*/","",$entry));
151         $obj  = $this->SubObjects[$entry];
153         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
154         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
155         if(preg_match("/d/",$acl)){
156           if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
157             $this->SubObjects[$entry]['status']= "delete";
158           }else{
159             unset($this->SubObjects[$entry]);
160           }
161         }
162       }
163     }
165     /* Add new sub object */
166     if(isset($_POST['AddSubObject'])){
167       $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
168       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
170       if(preg_match("/c/",$acl)){
171         $this->dialog= new $this->subClassName($this->config,"new");
172         $this->dialog->set_acl_base($this->acl_base);
173         $this->dialog->set_acl_category("fai");
174         $this->is_dialog=true;
175       }
176     }
178     if($this->dn != "new"){
179       $_SESSION['objectinfo']= $this->dn;
180     }
182     /* Save Dialog */
183     if(isset($_POST['SaveSubObject'])){
184       $this->dialog->save_object();
185       $msgs = $this->dialog->check();
186       if(count($msgs)>0){
187         foreach($msgs as $msg){
188           print_red($msg);
189         }
190       }else{
191         /* Get return object */
192         $obj = $this->dialog->save();
193         if(isset($obj['remove'])){
195           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
197           /* Depending on status, set new status */
198           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
199             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
200           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
201             unset($this->SubObjects[$obj['remove']['from']]);
202           }
203           $obj['status'] = "new";
204           $this->SubObjects[$obj['remove']['to']] = $obj;
205           unset($this->SubObjects[$obj['remove']['to']]['remove']);
206         }else{
207           if($obj['status'] == "FreshLoaded"){
208             $obj['status'] = "edited";
209           }
210           $this->SubObjects[$obj['cn']]=$obj;
211         }
212         $this->is_dialog=false;
213         unset($this->dialog);
214         $this->dialog=NULL;
215       }
216     }
218     /* Sort entries */
219     $tmp = $keys = array();
220     foreach($this->SubObjects as $key => $entry){
221       $keys[$key]=$key;
222     }
223     natcasesort($keys);
224     foreach($keys as $key){
225       $tmp[$key]=$this->SubObjects[$key];
226     }
227     $this->SubObjects = $tmp;
229     /* Cancel Dialog */
230     if(isset($_POST['CancelSubObject'])){
231       $this->is_dialog=false; 
232       unset($this->dialog);
233       $this->dialog=NULL;
234     }
236     /* Print dialog if $this->dialog is set */
237     if($this->dialog){
238       $this->dialog->save_object();
239       $display = $this->dialog->execute();
240       return($display);
241     }
243      /* Divlist Containing FAItemplates */
244     $divlist = new divSelectBox("FAItemplates");
245     $divlist->setHeight(400);
247     $tmp = $this->getList(true);
248   
249     foreach($this->SubObjects as $key => $name){
251       $dn  = $this->acl_base_for_current_object($name['dn']);
252       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
253       $act = "";
255       /* Hide delete icon if this object is freezed */
256       if(preg_match("/freeze/",$this->FAIstate)){
257         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
258       }else{
259         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
260         if(preg_match("/d/",$acl)){
261           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
262         }
263       }
265       /* Check acls for download icon */
266       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
267       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
268         $down = "";
269       }else{
270         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
271           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
272           </a>";
273       }
275       /* Check if we are allowed to view this object */
276       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
277       if(preg_match("/r/",$s_acl)){
278         $divlist->AddEntry(array( array("string"=> $tmp[$key] ),
279               array("string"=>$down , "attach" => "style='width:20px;'"),
280               array("string"=>str_replace("%s",base64_encode($key),$act),
281                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
282       }
283     }
284     $smarty->assign("Entry_divlist",$divlist->DrawList());
285     /* Divlist creation complete
286      */
288      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
289      * If we post the escaped strings they will be escaped again
290      */
291     foreach($this->attributes as $attrs){
292       if(get_magic_quotes_gpc()){
293         $smarty->assign($attrs,stripslashes($this->$attrs));
294       }else{
295         $smarty->assign($attrs,($this->$attrs));
296       }
297     }
299     $dn = $this->acl_base_for_current_object($this->dn);
300     $smarty->assign("sub_object_is_addable", 
301             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
302             !preg_match("/freeze/",$this->FAIstate));
304     foreach($this->attributes as $attr){
305       $smarty->assign($attr."ACL",$this->getacl($attr));
306     }
308     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
309     return($display);
310   }
313   function acl_base_for_current_object($dn)
314   {
315     if($dn == "new"){
316       if($this->dn == "new"){
317         $dn= $_SESSION['CurrentMainBase'];
318       }else{
319         $dn = $this->dn;
320       }
321     }
322     return($dn);
323   }
326   /* Generate listbox friendly SubObject list
327   */
328   function getList(){
329     $a_return=array();
330     foreach($this->SubObjects as $obj){
331       if($obj['status'] != "delete"){
332       
333         if((isset($obj['description']))&&(!empty($obj['description']))){
334           if(strlen($obj['description']) > 40){
335             $obj['description'] = substr($obj['description'],0,40)."...";
336           }
337           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
338         }else{
339           $a_return[$obj['cn']]= $obj['cn'];
340         }
341       }
342     }
343     return($a_return);
344   }
346   /* Delete me, and all my subtrees
347    */
348   function remove_from_parent()
349   {
350     $ldap = $this->config->get_ldap_link();
351     $ldap->cd ($this->dn);
353 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
354     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
355     if($_SESSION['faifilter']['branch'] == "main"){
356       $use_dn = $this->dn;
357     }
359     prepare_to_save_FAI_object($use_dn,array(),true);
361     foreach($this->SubObjects as $name => $obj){
362 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
363       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
364       if($_SESSION['faifilter']['branch'] == "main"){
365         $use_dn = $obj['dn'];
366       }
367       prepare_to_save_FAI_object($use_dn,array(),true);
368     }
369     $this->handle_post_events("remove");    
370   }
373   /* Save data to object 
374    */
375   function save_object()
376   {
377     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
378       plugin::save_object();
379     }
380   }
383   /* Check supplied data */
384   function check()
385   {
386     /* Call common method to give check the hook */
387     $message= plugin::check();
389     return ($message);
390   }
393   /* Save to LDAP */
394   function save()
395   {
396     plugin::save();
398     $ldap = $this->config->get_ldap_link();
400     prepare_to_save_FAI_object($this->dn,$this->attrs);
401     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
403     /* Do object tagging */
404     $this->handle_object_tagging();
406     /* Prepare FAIscriptEntry to write it to ldap
407      * First sort array.
408      *  Because we must delete old entries first.
409      * After deletion, we perform add and modify 
410      */
411     $Objects = array();
412     foreach($this->SubObjects as $name => $obj){
413       if($obj['status'] == "FreshLoaded"){
414         unset($this->SubObjects[$name]);
415       }
416     }
418     foreach($this->SubObjects as $name => $obj){
419       if($obj['status'] == "delete"){
420         $Objects[$name] = $obj; 
421       }
422     }
423     foreach($this->SubObjects as $name => $obj){
424       if($obj['status'] != "delete"){
425         $Objects[$name] = $obj; 
426       }
427     }
429     foreach($Objects as $name => $obj){
431       foreach($this->sub64coded as $codeIt){
432         $obj[$codeIt]=base64_encode($obj[$codeIt]);
433       }
434       $tmp = array();
435       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
436       foreach($attributes as $attrs){
437         if(empty($obj[$attrs])){
438           $obj[$attrs] = array();
439         }
440         $tmp[$attrs] =($obj[$attrs]);
441       }    
442         
443       $tmp['objectClass'] = $this->subClasses;
445       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
447       if($obj['status']=="new"){
448         $ldap->cat($sub_dn,array("objectClass"));
449         if($ldap->count()){
450           $obj['status']="edited";
451         }
452       }
454        /* Check if gosaAdministrativeUnitTag is required as object class */
455       if($obj['status'] == "edited"){
456         $ldap->cat($sub_dn,array("objectClass"));
457         $attrs = $ldap->fetch();
458         if(isset($attrs['objectClass'])){
459           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
460             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
461           }
462         }
463       }
465       if($obj['status'] == "delete"){
466         prepare_to_save_FAI_object($sub_dn,array(),true);
467         $this->handle_post_events("remove");
468       }elseif($obj['status'] == "edited"){
469         prepare_to_save_FAI_object($sub_dn,$tmp);
470         $this->handle_post_events("modify");
471       }elseif($obj['status']=="new"){
472         prepare_to_save_FAI_object($sub_dn,$tmp);
473         $this->handle_post_events("add");
474       }
477       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
478     }
479   }
480   
482   /* Return plugin informations for acl handling */ 
483   function plInfo()
484   {
485     return (array( 
486           "plShortName" => _("Template"),
487           "plDescription" => _("FAI template"),
488           "plSelfModify"  => FALSE,
489           "plDepends"     => array(),
490           "plPriority"    => 24,
491           "plSection"     => array("administration"),
492           "plCategory"    => array("fai"),
493           "plProvidedAcls" => array(
494             "cn"                => _("Name")." ("._("Readonly").")",
495             "description"       => _("Description"))
496           ));
497   }
500 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
501 ?>