Code

Updated object movement.
[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()){
71         /* Skip objects, that are tagged as removed */
72         if(isset($object['FAIstate'][0])){
73           if(preg_match("/removed$/",$object['FAIstate'][0])){
74             continue;
75           }
76         }
78         /* Set status for save management */
79         $objects = array();
80         $objects['status']      = "FreshLoaded";
81         $objects['dn']          = $object['dn'];
82         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
83         $this->SubObjects[$objects['cn']] = $objects;
84       }
85     }
86     $this->ui = get_userinfo();
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         $var = stripslashes($var);
113   
114         $object[$attrs] = $var;
115       }
116     }
117     return($object);
118   }
121   function execute()
122   {
123     /* Call parent execute */
124     plugin::execute();
126     /* Fill templating stuff */
127     $smarty= get_smarty();
128     $display= "";
130     /* New Listhandling
131      */
132     $once = true;
133     foreach($_POST as $name => $value){
134       if(preg_match("/^editscript_/",$name)&&($once)){
135         $once = false;
136         $entry = preg_replace("/^editscript_/","",$name);
137         $entry = base64_decode(preg_replace("/_.*/","",$entry));
138         $obj  = $this->SubObjects[$entry];
140         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
141         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
142         if(preg_match("/r/",$acl)){
143           if($obj['status'] == "FreshLoaded"){
144             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
145           }
146           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
147           $this->dialog->set_acl_base($this->acl_base);
148           $this->dialog->set_acl_category("fai");
150           $_SESSION['objectinfo'] = $obj['dn'];
151           $this->dialog->parent = &$this;
152           $this->is_dialog=true;
153         }
154       }
155       if(preg_match("/^deletescript_/",$name)&&($once)){
156         $once = false;
157         $entry = preg_replace("/^deletescript_/","",$name);
158         $entry = base64_decode(preg_replace("/_.*/","",$entry));
159         $obj  = $this->SubObjects[$entry];
161         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
162         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
163         if(preg_match("/d/",$acl)){
164           $status = $this->SubObjects[$entry]['status'];
165           if($status == "edited" || $status == "FreshLoaded"){
166             $this->SubObjects[$entry]['status']= "delete";
167           }else{
168             unset($this->SubObjects[$entry]);
169           }
170         }
171       }
172     }
174     /* Add new sub object */
175     if(isset($_POST['AddSubObject'])){
176       $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
177       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
179       if(preg_match("/c/",$acl)){
180         $this->dialog= new $this->subClassName($this->config,"new");
181         $this->dialog->set_acl_base($this->acl_base);
182         $this->dialog->set_acl_category("fai");
183         $this->is_dialog=true;
184       }
185     }
187     if($this->dn != "new"){
188       $_SESSION['objectinfo']= $this->dn;
189     }
191     /* Save Dialog */
192     if(isset($_POST['SaveSubObject'])){
193       $this->dialog->save_object();
194       $msgs = $this->dialog->check();
195       if(count($msgs)>0){
196         foreach($msgs as $msg){
197           print_red($msg);
198         }
199       }else{
200         /* Get return object */
201         $obj = $this->dialog->save();
202         if(isset($obj['remove'])){
204           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
206           /* Depending on status, set new status */
207           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
208             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
209           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
210             unset($this->SubObjects[$obj['remove']['from']]);
211           }
212           $obj['status'] = "new";
213           $this->SubObjects[$obj['remove']['to']] = $obj;
214           unset($this->SubObjects[$obj['remove']['to']]['remove']);
215         }else{
216           if($obj['status'] == "FreshLoaded"){
217             $obj['status'] = "edited";
218           }
219           $this->SubObjects[$obj['cn']]=$obj;
220         }
221         $this->is_dialog=false;
222         unset($this->dialog);
223         $this->dialog=NULL;
224       }
225     }
227     /* Sort entries */
228     $tmp = $keys = array();
229     foreach($this->SubObjects as $key => $entry){
230       $keys[$key]=$key;
231     }
232     natcasesort($keys);
233     foreach($keys as $key){
234       $tmp[$key]=$this->SubObjects[$key];
235     }
236     $this->SubObjects = $tmp;
238     /* Cancel Dialog */
239     if(isset($_POST['CancelSubObject'])){
240       $this->is_dialog=false; 
241       unset($this->dialog);
242       $this->dialog=NULL;
243     }
245     /* Print dialog if $this->dialog is set */
246     if($this->dialog){
247       $this->dialog->save_object();
248       $display = $this->dialog->execute();
249       return($display);
250     }
252      /* Divlist Containing FAItemplates */
253     $divlist = new divSelectBox("FAItemplates");
254     $divlist->setHeight(400);
256     $tmp = $this->getList(true);
257   
258     /* Create div list with all sub entries listed */
259     foreach($this->SubObjects as $key => $name){
261       /* Skip removed entries */ 
262       if($name['status'] == "delete") continue;
264       /* Get permissions */
265       $dn  = $this->acl_base_for_current_object($name['dn']);
266       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
267       $act = "";
269       /* Hide delete icon if this object is freezed */
270       if(preg_match("/freeze/",$this->FAIstate)){
271         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
272       }else{
273         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
274         if(preg_match("/d/",$acl)){
275           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
276         }
277       }
279       /* Check acls for download icon */
280       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
281       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
282         $down = "";
283       }else{
284         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
285           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
286           </a>";
287       }
289       /* Check if we are allowed to view this object */
290       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
291       if(preg_match("/r/",$s_acl)){
292         $divlist->AddEntry(array( array("string"=> $tmp[$key] ),
293               array("string"=>$down , "attach" => "style='width:20px;'"),
294               array("string"=>str_replace("%s",base64_encode($key),$act),
295                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
296       }
297     }
298     $smarty->assign("Entry_divlist",$divlist->DrawList());
299     /* Divlist creation complete
300      */
302      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
303      * If we post the escaped strings they will be escaped again
304      */
305     foreach($this->attributes as $attrs){
306       if(get_magic_quotes_gpc()){
307         $smarty->assign($attrs,stripslashes($this->$attrs));
308       }else{
309         $smarty->assign($attrs,($this->$attrs));
310       }
311     }
313     $dn = $this->acl_base_for_current_object($this->dn);
314     $smarty->assign("sub_object_is_addable", 
315             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
316             !preg_match("/freeze/",$this->FAIstate));
318     foreach($this->attributes as $attr){
319       $smarty->assign($attr."ACL",$this->getacl($attr));
320     }
322     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
323     return($display);
324   }
327   function acl_base_for_current_object($dn)
328   {
329     if($dn == "new"){
330       if($this->dn == "new"){
331         $dn= $_SESSION['CurrentMainBase'];
332       }else{
333         $dn = $this->dn;
334       }
335     }
336     return($dn);
337   }
340   /* Generate listbox friendly SubObject list
341   */
342   function getList(){
343     $a_return=array();
344     foreach($this->SubObjects as $obj){
345       if($obj['status'] != "delete"){
346       
347         if((isset($obj['description']))&&(!empty($obj['description']))){
348           if(strlen($obj['description']) > 40){
349             $obj['description'] = substr($obj['description'],0,40)."...";
350           }
351           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
352         }else{
353           $a_return[$obj['cn']]= $obj['cn'];
354         }
355       }
356     }
357     return($a_return);
358   }
360   /* Delete me, and all my subtrees
361    */
362   function remove_from_parent()
363   {
364     $ldap = $this->config->get_ldap_link();
365     $ldap->cd ($this->dn);
367 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
368     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
369     if($_SESSION['faifilter']['branch'] == "main"){
370       $use_dn = $this->dn;
371     }
373     prepare_to_save_FAI_object($use_dn,array(),true);
375     foreach($this->SubObjects as $name => $obj){
376 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
377       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
378       if($_SESSION['faifilter']['branch'] == "main"){
379         $use_dn = $obj['dn'];
380       }
381       prepare_to_save_FAI_object($use_dn,array(),true);
382     }
383     $this->handle_post_events("remove");    
384   }
387   /* Save data to object 
388    */
389   function save_object()
390   {
391     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
392       plugin::save_object();
393     }
394   }
397   /* Check supplied data */
398   function check()
399   {
400     /* Call common method to give check the hook */
401     $message= plugin::check();
403     return ($message);
404   }
407   /* Save to LDAP */
408   function save()
409   {
410     plugin::save();
412     $ldap = $this->config->get_ldap_link();
414     prepare_to_save_FAI_object($this->dn,$this->attrs);
415     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
417     /* Do object tagging */
418     $this->handle_object_tagging();
420     /* Prepare FAIscriptEntry to write it to ldap
421      * First sort array.
422      *  Because we must delete old entries first.
423      * After deletion, we perform add and modify 
424      */
425     $Objects = array();
426     foreach($this->SubObjects as $name => $obj){
427       if($obj['status'] == "FreshLoaded"){
428         unset($this->SubObjects[$name]);
429       }
430     }
432     foreach($this->SubObjects as $name => $obj){
433       if($obj['status'] == "delete"){
434         $Objects[$name] = $obj; 
435       }
436     }
437     foreach($this->SubObjects as $name => $obj){
438       if($obj['status'] != "delete"){
439         $Objects[$name] = $obj; 
440       }
441     }
443     foreach($Objects as $name => $obj){
445       foreach($this->sub64coded as $codeIt){
446         $obj[$codeIt]=base64_encode($obj[$codeIt]);
447       }
448       $tmp = array();
449       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
450       foreach($attributes as $attrs){
451         if(empty($obj[$attrs])){
452           $obj[$attrs] = array();
453         }
454         $tmp[$attrs] =($obj[$attrs]);
455       }    
456         
457       $tmp['objectClass'] = $this->subClasses;
459       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
461       if($obj['status']=="new"){
462         $ldap->cat($sub_dn,array("objectClass"));
463         if($ldap->count()){
464           $obj['status']="edited";
465         }
466       }
468        /* Check if gosaAdministrativeUnitTag is required as object class */
469       if($obj['status'] == "edited"){
470         $ldap->cat($sub_dn,array("objectClass"));
471         $attrs = $ldap->fetch();
472         if(isset($attrs['objectClass'])){
473           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
474             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
475           }
476         }
477       }
479       if($obj['status'] == "delete"){
480         prepare_to_save_FAI_object($sub_dn,array(),true);
481         $this->handle_post_events("remove");
482       }elseif($obj['status'] == "edited"){
483         prepare_to_save_FAI_object($sub_dn,$tmp);
484         $this->handle_post_events("modify");
485       }elseif($obj['status']=="new"){
486         prepare_to_save_FAI_object($sub_dn,$tmp);
487         $this->handle_post_events("add");
488       }
491       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
492     }
493   }
494   
496   /* Return plugin informations for acl handling */ 
497   function plInfo()
498   {
499     return (array( 
500           "plShortName" => _("Template"),
501           "plDescription" => _("FAI template"),
502           "plSelfModify"  => FALSE,
503           "plDepends"     => array(),
504           "plPriority"    => 24,
505           "plSection"     => array("administration"),
506           "plCategory"    => array("fai"),
507           "plProvidedAcls" => array(
508             "cn"                => _("Name")." ("._("Readonly").")",
509             "description"       => _("Description"))
510           ));
511   }
514 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
515 ?>