Code

list base image migration for gosa-plugins
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiTemplate.inc
1 <?php
3 class faiTemplate extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAItemplate");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAItemplateEntry";
16   var $subClasses       = array("top","FAIclass","FAItemplateEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiTemplateEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAItemplatePath","FAIowner","FAImode");
23   var $sub_Load_Later   = array("FAItemplateFile"); 
24   var $sub64coded       = array();
25   var $subBinary        = array("FAItemplateFile");
27   /* Specific attributes */
28   var $cn               = "";       // The class name for this object
29   var $description      = "";       // The description for this set of partitions
30   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
31   var $SubObjects       = array();  // All leafobjects of this object
33   var $FAIstate         = "";
34   var $ui;
35   var $view_logged      = FALSE;
37   function faiTemplate (&$config, $dn= NULL)
38   {
39     /* Load Attributes */
40     plugin::plugin ($config, $dn);
42     /* If "dn==new" we try to create a new entry
43      * Else we must read all objects from ldap which belong to this entry.
44      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45      */
46     if($dn != "new"){
47       $this->dn =$dn;
49       /* Get FAIstate
50        */
51       if(isset($this->attrs['FAIstate'][0])){
52         $this->FAIstate = $this->attrs['FAIstate'][0];
53       }
55       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
56        */
57       $ldap     = $this->config->get_ldap_link();
58       $ldap->cd ($this->dn);
59     
60       $attrs_to_search = $this->subAttributes;
61       $attrs_to_search[] = "FAIstate";
62       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
64       $data = array();
65       while($object = $ldap->fetch()){
66         $data[] = $object;
67       }
68       foreach($data as $object){
70         /* Skip objects, that are tagged as removed */
71         if(isset($object['FAIstate'][0])){
72           if(preg_match("/removed$/",$object['FAIstate'][0])){
73             continue;
74           }
75         }
77         /* Set status for save management */
78         $objects = array();
79         $objects['status']      = "FreshLoaded";
80         $objects['dn']          = $object['dn'];
81         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
82         $this->SubObjects[$objects['cn']] = $objects;
83       }
84     }
85     $this->is_new = FALSE;
86     if($this->dn == "new"){
87       $this->is_new =TRUE;
88     }
89     $this->ui = get_userinfo();
90   }
93   /* Reload some attributes */
94   function get_object_attributes($object,$attributes)
95   {
96     $ldap = $this->config->get_ldap_link();
97     $ldap->cd($this->config->current['BASE']);
98     $ldap->cat($object['dn'],$attributes);
99     $tmp  = $ldap->fetch();
101     foreach($attributes as $attrs){
102       if(isset($tmp[$attrs][0])){
103         $var = $tmp[$attrs][0];
105         /* Check if we must decode some attributes */
106         if(in_array_ics($attrs,$this->sub64coded)){
107           $var = base64_decode($var);
108         }
109         $var = stripslashes($var);
111         /*  check if this is a binary entry */
112         if(in_array_ics($attrs,$this->subBinary)){
113           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
114         }
115   
116         $object[$attrs] = $var;
117       }
118     }
119     return($object);
120   }
123   function execute()
124   {
125     /* Call parent execute */
126     plugin::execute();
128     if($this->is_account && !$this->view_logged){
129       $this->view_logged = TRUE;
130       new log("view","fai/".get_class($this),$this->dn);
131     }
133     /* Fill templating stuff */
134     $smarty= get_smarty();
135     $display= "";
137     /* New Listhandling
138      */
139     $once = true;
140     foreach($_POST as $name => $value){
141       if(preg_match("/^editscript_/",$name)&&($once)){
142         $once = false;
143         $entry = preg_replace("/^editscript_/","",$name);
144         $entry = base64_decode(preg_replace("/_.*/","",$entry));
145         $obj  = $this->SubObjects[$entry];
147         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
148         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
149         if(preg_match("/r/",$acl)){
150           if($obj['status'] == "FreshLoaded"){
151             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
152           }
153           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
154           $this->dialog->set_acl_base($this->acl_base);
155           $this->dialog->set_acl_category("fai");
157           session::set('objectinfo',$obj['dn']);
158           $this->dialog->parent = &$this;
159           $this->is_dialog=true;
160         }
161       }
162       if(preg_match("/^deletescript_/",$name)&&($once)){
163         $once = false;
164         $entry = preg_replace("/^deletescript_/","",$name);
165         $entry = base64_decode(preg_replace("/_.*/","",$entry));
166         $obj  = $this->SubObjects[$entry];
168         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
169         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
170         if(preg_match("/d/",$acl)){
171           $status = $this->SubObjects[$entry]['status'];
172           if($status == "edited" || $status == "FreshLoaded"){
173             $this->SubObjects[$entry]['status']= "delete";
174           }else{
175             unset($this->SubObjects[$entry]);
176           }
177         }
178       }
179     }
181     /* File download requested */
182     if(isset($_GET['getFAItemplate'])){
183       if(isset($this->SubObjects[$_GET['getFAItemplate']])){
184         $obj = $this->SubObjects[$_GET['getFAItemplate']];
185         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
186         send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
187       }
188     }
190     /* Edit entries via GET */
191     if(isset($_GET['act']) && isset($_GET['id'])){
192       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
193         $obj = $this->SubObjects[$_GET['id']];
194           if($obj['status'] == "FreshLoaded"){
195           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
196         }
197         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
198         $this->dialog->acl = $this->acl;
199         session::set('objectinfo',$obj['dn']);
200         $this->dialog->parent = &$this;
201         $this->is_dialog=true;
202       }
203     }
205     /* Add new sub object */
206     if(isset($_POST['AddSubObject'])){
207       $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
208       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
210       if(preg_match("/c/",$acl)){
211         $this->dialog= new $this->subClassName($this->config,"new");
212         $this->dialog->set_acl_base($this->acl_base);
213         $this->dialog->set_acl_category("fai");
214         $this->dialog->parent = &$this;
215         $this->is_dialog=true;
216       }
217     }
219     if($this->dn != "new"){
220       session::set('objectinfo',$this->dn);
221     }
223     /* Save Dialog */
224     if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
225       $this->dialog->save_object();
226       $msgs = $this->dialog->check();
227       if(count($msgs)>0){
228         foreach($msgs as $msg){
229           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
230         }
231       }else{
232         /* Get return object */
233         $obj = $this->dialog->save();
234         if(isset($obj['remove'])){
236           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
238           /* Depending on status, set new status */
239           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
240             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
241           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
242             unset($this->SubObjects[$obj['remove']['from']]);
243           }
244           $obj['status'] = "new";
245           $this->SubObjects[$obj['remove']['to']] = $obj;
246           unset($this->SubObjects[$obj['remove']['to']]['remove']);
247         }else{
248           if($obj['status'] == "FreshLoaded"){
249             $obj['status'] = "edited";
250           }
251           $this->SubObjects[$obj['cn']]=$obj;
252         }
253         $this->is_dialog=false;
254         unset($this->dialog);
255         $this->dialog=FALSE;
256       }
257     }
259     /* Sort entries */
260     $tmp = $keys = array();
261     foreach($this->SubObjects as $key => $entry){
262       $keys[$key]=$key;
263     }
264     natcasesort($keys);
265     foreach($keys as $key){
266       $tmp[$key]=$this->SubObjects[$key];
267     }
268     $this->SubObjects = $tmp;
270     /* Cancel Dialog */
271     if(isset($_POST['CancelSubObject'])){
272       $this->is_dialog=false; 
273       unset($this->dialog);
274       $this->dialog=FALSE;
275     }
277     /* Print dialog if $this->dialog is set */
278     if(is_object($this->dialog)){
279       $this->dialog->save_object();
280       $display = $this->dialog->execute();
281       return($display);
282     }
284      /* Divlist Containing FAItemplates */
285     $divlist = new divSelectBox("FAItemplates");
286     $divlist->setHeight(400);
288     $tmp = $this->getList(true);
289   
290     /* Create div list with all sub entries listed */
291     foreach($this->SubObjects as $key => $name){
293       /* Skip removed entries */ 
294       if($name['status'] == "delete") continue;
296       /* Get permissions */
297       $dn  = $this->acl_base_for_current_object($name['dn']);
298       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
299       $act = "";
301       /* Hide delete icon if this object is freezed */
302       if(preg_match("/freeze/",$this->FAIstate)){
303         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
304       }else{
305         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
306         if(preg_match("/d/",$acl)){
307           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
308         }
309       }
311       /* Check acls for download icon */
312       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
313       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
314         $down = "";
315       }else{
316         $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
317           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
318           </a>";
319       }
321       /* Check if we are allowed to view this object */
322       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
323       if(preg_match("/r/",$s_acl)){
325         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$tmp[$key]."</a>";
326         $divlist->AddEntry(array( array("string"=> $edit_link), 
327               array("string"=>$down , "attach" => "style='width:20px;'"),
328               array("string"=>str_replace("%s",base64_encode($key),$act),
329                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
330       }
331     }
332     $smarty->assign("Entry_divlist",$divlist->DrawList());
333     /* Divlist creation complete
334      */
336      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
337      * If we post the escaped strings they will be escaped again
338      */
339     foreach($this->attributes as $attrs){
340       if(get_magic_quotes_gpc()){
341         $smarty->assign($attrs,stripslashes($this->$attrs));
342       }else{
343         $smarty->assign($attrs,($this->$attrs));
344       }
345     }
347     $dn = $this->acl_base_for_current_object($this->dn);
348     $smarty->assign("sub_object_is_addable", 
349             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
350             !preg_match("/freeze/",$this->FAIstate));
352     foreach($this->attributes as $attr){
353       $smarty->assign($attr."ACL",$this->getacl($attr));
354     }
356     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
357     return($display);
358   }
361   function acl_base_for_current_object($dn)
362   {
363     if($dn == "new"){
364       if($this->dn == "new"){
365         $dn = session::get('CurrentMainBase');
366       }else{
367         $dn = $this->dn;
368       }
369     }
370     return($dn);
371   }
374   /* Generate listbox friendly SubObject list
375   */
376   function getList(){
377     $a_return=array();
378     foreach($this->SubObjects as $obj){
379       if($obj['status'] != "delete"){
380       
381         if((isset($obj['description']))&&(!empty($obj['description']))){
382           if(strlen($obj['description']) > 40){
383             $obj['description'] = substr($obj['description'],0,40)."...";
384           }
385           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
386         }else{
387           $a_return[$obj['cn']]= $obj['cn'];
388         }
389       }
390     }
391     return($a_return);
392   }
394   /* Delete me, and all my subtrees
395    */
396   function remove_from_parent()
397   {
398     $ldap = $this->config->get_ldap_link();
399     $ldap->cd ($this->dn);
401     $faifilter = session::get('faifilter');
402     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
403     if($faifilter['branch'] == "main"){
404       $use_dn = $this->dn;
405     }
407     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
408     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
410     foreach($this->SubObjects as $name => $obj){
411       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
412       if($faifilter['branch'] == "main"){
413         $use_dn = $obj['dn'];
414       }
415       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
416     }
417     $this->handle_post_events("remove");    
418   }
421   /* Save data to object 
422    */
423   function save_object()
424   {
425     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
426       plugin::save_object();
427     }
428   }
431   /* Check supplied data */
432   function check()
433   {
434     /* Call common method to give check the hook */
435     $message= plugin::check();
437     /* Ensure that we do not overwrite an allready existing entry 
438      */
439     if($this->is_new){
440       $new_dn= 'cn='.$this->cn.",".get_ou('faitemplateou').get_ou('faiou').session::get('CurrentMainBase');
441       $faifilter = session::get('faifilter');
442       if($faifilter['branch']!="main"){
443         $new_dn ='cn='.$this->cn.",".get_ou('faitemplateou').$faifilter['branch'];
444       }
446       $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
447       if(isset($res[$this->cn])){
448         $message[] = msgPool::duplicated(_("Name"));
449       }
450     }
452     return ($message);
453   }
456   /* Save to LDAP */
457   function save()
458   {
459     plugin::save();
461     $ldap = $this->config->get_ldap_link();
463     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
465     if($this->initially_was_account){
466       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
467     }else{
468       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
469     }
471     /* Prepare FAIscriptEntry to write it to ldap
472      * First sort array.
473      *  Because we must delete old entries first.
474      * After deletion, we perform add and modify 
475      */
476     $Objects = array();
477     foreach($this->SubObjects as $name => $obj){
478       if($obj['status'] == "FreshLoaded"){
479         unset($this->SubObjects[$name]);
480       }
481     }
483     foreach($this->SubObjects as $name => $obj){
484       if($obj['status'] == "delete"){
485         $Objects[$name] = $obj; 
486       }
487     }
488     foreach($this->SubObjects as $name => $obj){
489       if($obj['status'] != "delete"){
490         $Objects[$name] = $obj; 
491       }
492     }
494     foreach($Objects as $name => $obj){
496       foreach($this->sub64coded as $codeIt){
497         $obj[$codeIt]=base64_encode($obj[$codeIt]);
498       }
499       $tmp = array();
500       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
501       foreach($attributes as $attrs){
502         if(empty($obj[$attrs])){
503           $obj[$attrs] = array();
504         }
505         $tmp[$attrs] =($obj[$attrs]);
506       }    
507         
508       $tmp['objectClass'] = $this->subClasses;
510       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
512       if($obj['status']=="new"){
513         $ldap->cat($sub_dn,array("objectClass"));
514         if($ldap->count()){
515           $obj['status']="edited";
516         }
517       }
519       /* Tag object */
520       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
522       if($obj['status'] == "delete"){
523         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
524         $this->handle_post_events("remove");
525       }elseif($obj['status'] == "edited"){
526         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
527         $this->handle_post_events("modify");
528       }elseif($obj['status']=="new"){
529         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
530         $this->handle_post_events("add");
531       }
533     }
534   }
537   function PrepareForCopyPaste($source)
538   {
539     plugin::PrepareForCopyPaste($source);
541     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
542      */
543     $ldap     = $this->config->get_ldap_link();
544     $ldap->cd ($source['dn']);
546     $attrs_to_search = $this->subAttributes;
547     $attrs_to_search[] = "FAIstate";
548     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
550     while($object = $ldap->fetch()){
552       /* Skip objects, that are tagged as removed */
553       if(isset($object['FAIstate'][0])){
554         if(preg_match("/removed$/",$object['FAIstate'][0])){
555           continue;
556         }
557       }
559       /* Set status for save management */
560       $objects = array();
561       $objects['status']      = "edited";
562       $objects['dn']          = $object['dn'];
563       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
564       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
566       $this->SubObjects[$objects['cn']] = $objects;
567     }
568   }
569   
571   /* Return plugin informations for acl handling */ 
572   static function plInfo()
573   {
574     return (array( 
575           "plShortName" => _("Template"),
576           "plDescription" => _("FAI template"),
577           "plSelfModify"  => FALSE,
578           "plDepends"     => array(),
579           "plPriority"    => 24,
580           "plSection"     => array("administration"),
581           "plCategory"    => array("fai"),
582           "plProvidedAcls" => array(
583             "cn"                => _("Name")." ("._("Readonly").")",
584             "description"       => _("Description"))
585           ));
586   }
589   /*! \brief  Used for copy & paste.
590     Returns a HTML input mask, which allows to change the cn of this entry.
591     @param  Array   Array containing current status && a HTML template.
592    */
593   function getCopyDialog()
594   {
595     $vars = array("cn");
596     $smarty = get_smarty();
597     $smarty->assign("cn", htmlentities($this->cn));
598     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
599     $ret = array();
600     $ret['string'] = $str;
601     $ret['status'] = "";
602     return($ret);
603   }
606   /*! \brief  Used for copy & paste.
607     Some entries must be renamed to avaoid duplicate entries.
608    */
609   function saveCopyDialog()
610   {
611     if(isset($_POST['cn'])){
612       $this->cn = get_post('cn');
613     }
614   }
617 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
618 ?>