Code

Added sorting to FAIscripts
[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->dialog->parent = &$this;
190         $this->is_dialog=true;
191       }
192     }
194     if($this->dn != "new"){
195       $_SESSION['objectinfo']= $this->dn;
196     }
198     /* Save Dialog */
199     if(isset($_POST['SaveSubObject'])){
200       $this->dialog->save_object();
201       $msgs = $this->dialog->check();
202       if(count($msgs)>0){
203         foreach($msgs as $msg){
204           print_red($msg);
205         }
206       }else{
207         /* Get return object */
208         $obj = $this->dialog->save();
209         if(isset($obj['remove'])){
211           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
213           /* Depending on status, set new status */
214           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
215             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
216           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
217             unset($this->SubObjects[$obj['remove']['from']]);
218           }
219           $obj['status'] = "new";
220           $this->SubObjects[$obj['remove']['to']] = $obj;
221           unset($this->SubObjects[$obj['remove']['to']]['remove']);
222         }else{
223           if($obj['status'] == "FreshLoaded"){
224             $obj['status'] = "edited";
225           }
226           $this->SubObjects[$obj['cn']]=$obj;
227         }
228         $this->is_dialog=false;
229         unset($this->dialog);
230         $this->dialog=NULL;
231       }
232     }
234     /* Sort entries */
235     $tmp = $keys = array();
236     foreach($this->SubObjects as $key => $entry){
237       $keys[$key]=$key;
238     }
239     natcasesort($keys);
240     foreach($keys as $key){
241       $tmp[$key]=$this->SubObjects[$key];
242     }
243     $this->SubObjects = $tmp;
245     /* Cancel Dialog */
246     if(isset($_POST['CancelSubObject'])){
247       $this->is_dialog=false; 
248       unset($this->dialog);
249       $this->dialog=NULL;
250     }
252     /* Print dialog if $this->dialog is set */
253     if($this->dialog){
254       $this->dialog->save_object();
255       $display = $this->dialog->execute();
256       return($display);
257     }
259      /* Divlist Containing FAItemplates */
260     $divlist = new divSelectBox("FAItemplates");
261     $divlist->setHeight(400);
263     $tmp = $this->getList(true);
264   
265     /* Create div list with all sub entries listed */
266     foreach($this->SubObjects as $key => $name){
268       /* Skip removed entries */ 
269       if($name['status'] == "delete") continue;
271       /* Get permissions */
272       $dn  = $this->acl_base_for_current_object($name['dn']);
273       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
274       $act = "";
276       /* Hide delete icon if this object is freezed */
277       if(preg_match("/freeze/",$this->FAIstate)){
278         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
279       }else{
280         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
281         if(preg_match("/d/",$acl)){
282           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
283         }
284       }
286       /* Check acls for download icon */
287       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
288       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
289         $down = "";
290       }else{
291         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
292           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
293           </a>";
294       }
296       /* Check if we are allowed to view this object */
297       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
298       if(preg_match("/r/",$s_acl)){
299         $divlist->AddEntry(array( array("string"=> $tmp[$key] ),
300               array("string"=>$down , "attach" => "style='width:20px;'"),
301               array("string"=>str_replace("%s",base64_encode($key),$act),
302                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
303       }
304     }
305     $smarty->assign("Entry_divlist",$divlist->DrawList());
306     /* Divlist creation complete
307      */
309      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
310      * If we post the escaped strings they will be escaped again
311      */
312     foreach($this->attributes as $attrs){
313       if(get_magic_quotes_gpc()){
314         $smarty->assign($attrs,stripslashes($this->$attrs));
315       }else{
316         $smarty->assign($attrs,($this->$attrs));
317       }
318     }
320     $dn = $this->acl_base_for_current_object($this->dn);
321     $smarty->assign("sub_object_is_addable", 
322             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
323             !preg_match("/freeze/",$this->FAIstate));
325     foreach($this->attributes as $attr){
326       $smarty->assign($attr."ACL",$this->getacl($attr));
327     }
329     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
330     return($display);
331   }
334   function acl_base_for_current_object($dn)
335   {
336     if($dn == "new"){
337       if($this->dn == "new"){
338         $dn= $_SESSION['CurrentMainBase'];
339       }else{
340         $dn = $this->dn;
341       }
342     }
343     return($dn);
344   }
347   /* Generate listbox friendly SubObject list
348   */
349   function getList(){
350     $a_return=array();
351     foreach($this->SubObjects as $obj){
352       if($obj['status'] != "delete"){
353       
354         if((isset($obj['description']))&&(!empty($obj['description']))){
355           if(strlen($obj['description']) > 40){
356             $obj['description'] = substr($obj['description'],0,40)."...";
357           }
358           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
359         }else{
360           $a_return[$obj['cn']]= $obj['cn'];
361         }
362       }
363     }
364     return($a_return);
365   }
367   /* Delete me, and all my subtrees
368    */
369   function remove_from_parent()
370   {
371     $ldap = $this->config->get_ldap_link();
372     $ldap->cd ($this->dn);
374 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
375     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
376     if($_SESSION['faifilter']['branch'] == "main"){
377       $use_dn = $this->dn;
378     }
380     prepare_to_save_FAI_object($use_dn,array(),true);
381     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
383     foreach($this->SubObjects as $name => $obj){
384 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
385       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
386       if($_SESSION['faifilter']['branch'] == "main"){
387         $use_dn = $obj['dn'];
388       }
389       prepare_to_save_FAI_object($use_dn,array(),true);
390     }
391     $this->handle_post_events("remove");    
392   }
395   /* Save data to object 
396    */
397   function save_object()
398   {
399     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
400       plugin::save_object();
401     }
402   }
405   /* Check supplied data */
406   function check()
407   {
408     /* Call common method to give check the hook */
409     $message= plugin::check();
411     return ($message);
412   }
415   /* Save to LDAP */
416   function save()
417   {
418     plugin::save();
420     $ldap = $this->config->get_ldap_link();
422     prepare_to_save_FAI_object($this->dn,$this->attrs);
423     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
425     if($this->initially_was_account){
426       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
427     }else{
428       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
429     }
431     /* Do object tagging */
432     $this->handle_object_tagging();
434     /* Prepare FAIscriptEntry to write it to ldap
435      * First sort array.
436      *  Because we must delete old entries first.
437      * After deletion, we perform add and modify 
438      */
439     $Objects = array();
440     foreach($this->SubObjects as $name => $obj){
441       if($obj['status'] == "FreshLoaded"){
442         unset($this->SubObjects[$name]);
443       }
444     }
446     foreach($this->SubObjects as $name => $obj){
447       if($obj['status'] == "delete"){
448         $Objects[$name] = $obj; 
449       }
450     }
451     foreach($this->SubObjects as $name => $obj){
452       if($obj['status'] != "delete"){
453         $Objects[$name] = $obj; 
454       }
455     }
457     foreach($Objects as $name => $obj){
459       foreach($this->sub64coded as $codeIt){
460         $obj[$codeIt]=base64_encode($obj[$codeIt]);
461       }
462       $tmp = array();
463       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
464       foreach($attributes as $attrs){
465         if(empty($obj[$attrs])){
466           $obj[$attrs] = array();
467         }
468         $tmp[$attrs] =($obj[$attrs]);
469       }    
470         
471       $tmp['objectClass'] = $this->subClasses;
473       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
475       if($obj['status']=="new"){
476         $ldap->cat($sub_dn,array("objectClass"));
477         if($ldap->count()){
478           $obj['status']="edited";
479         }
480       }
482        /* Check if gosaAdministrativeUnitTag is required as object class */
483       if($obj['status'] == "edited"){
484         $ldap->cat($sub_dn,array("objectClass"));
485         $attrs = $ldap->fetch();
486         if(isset($attrs['objectClass'])){
487           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
488             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
489           }
490         }
491       }
493       if($obj['status'] == "delete"){
494         prepare_to_save_FAI_object($sub_dn,array(),true);
495         $this->handle_post_events("remove");
496       }elseif($obj['status'] == "edited"){
497         prepare_to_save_FAI_object($sub_dn,$tmp);
498         $this->handle_post_events("modify");
499       }elseif($obj['status']=="new"){
500         prepare_to_save_FAI_object($sub_dn,$tmp);
501         $this->handle_post_events("add");
502       }
505       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
506     }
507   }
508   
510   /* Return plugin informations for acl handling */ 
511   function plInfo()
512   {
513     return (array( 
514           "plShortName" => _("Template"),
515           "plDescription" => _("FAI template"),
516           "plSelfModify"  => FALSE,
517           "plDepends"     => array(),
518           "plPriority"    => 24,
519           "plSection"     => array("administration"),
520           "plCategory"    => array("fai"),
521           "plProvidedAcls" => array(
522             "cn"                => _("Name")." ("._("Readonly").")",
523             "description"       => _("Description"))
524           ));
525   }
528 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
529 ?>