Code

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