Code

Updated copy & paste acls, centralized checks
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiVariable.inc
1 <?php
3 class faiVariable 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","FAIvariable");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAIvariableEntry";
16   var $subClasses       = array("top","FAIclass","FAIvariableEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiVariableEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAIvariableContent"); 
23   var $sub64coded       = array();  
25   /* Specific attributes */
26   var $cn               = "";       // The class name for this object
27   var $description      = "";       // The description for this set of partitions
28   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
29   var $SubObjects       = array();  // All leafobjects of this object
31   var $FAIstate         = "";
32   var $ui   ;
33   var $view_logged      = FALSE;
35   function faiVariable (&$config, $dn= NULL)
36   {
37     /* Load Attributes */
38     plugin::plugin ($config, $dn);
40     if($dn != "new"){
41       $this->dn =$dn;
43       /* Get FAIstate
44        */
45       if(isset($this->attrs['FAIstate'][0])){
46         $this->FAIstate = $this->attrs['FAIstate'][0];
47       }
49       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50        */
51       $ldap     = $this->config->get_ldap_link();
52       $ldap->cd ($this->dn);
54       $attrs_to_search = $this->subAttributes;
55       $attrs_to_search[] = "FAIstate";
56       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
58       while($object = $ldap->fetch()){
59       
60         /* Skip objects, that are tagged as removed */
61         if(isset($object['FAIstate'][0])){
62           if(preg_match("/removed$/",$object['FAIstate'][0])){
63             continue;
64           }
65         }
67         /* Set status for save management */
68         foreach($this->subAttributes as $attrs){
69           if(!isset($object[$attrs][0])){
70             $this->SubObjects[$object['cn'][0]][$attrs]="";
71           }else{
72             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
73           }
74         }
75      
76         foreach($this->sub64coded as $codeIt){
77           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
78         }
79  
80         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
81         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
82       }
84     }
85     $this->is_new = FALSE;
86     if($this->dn == "new"){
87       $this->is_new =TRUE;
88     }
89     $this->ui = get_userinfo();
90   }
93   function acl_base_for_current_object($dn)
94   {
95     if($dn == "new"){
96       if($this->dn == "new"){
97         $dn = session::get('CurrentMainBase');
98       }else{
99         $dn = $this->dn;
100       }
101     }
102     return($dn);
103   }
106   function execute()
107   {
108     /* Call parent execute */
109     plugin::execute();
111     if($this->is_account && !$this->view_logged){
112       $this->view_logged = TRUE;
113       new log("view","fai/".get_class($this),$this->dn);
114     }
116     /* Fill templating stuff */
117     $smarty= get_smarty();
118     $display= "";
120     /* Add new sub object */
121     if(isset($_POST['AddSubObject'])){
122       $this->dialog= new $this->subClassName($this->config,"new");
123       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
124       $this->dialog->set_acl_category("fai");
125       $this->dialog->parent = &$this;
126       $this->is_dialog=true;
127     }
129     if($this->dn != "new"){
130       session::set('objectinfo',$this->dn);
131     }
134     /* Edit selected Sub Object */
135     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
137       $var = $_POST['SubObject'][0];
138       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
139       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
140       $this->dialog->set_acl_category("fai");
141       $this->dialog->set_acl_base($c_dn);
142       $this->dialog->parent = &$this;
143       session::set('objectinfo',$this->SubObjects[$var]['dn']);
144       $this->is_dialog=true;
145     }
146     
147     /* Remove Sub object */
148     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
149       foreach($_POST['SubObject'] as $var){
151         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
152         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
153         if(preg_match("/d/",$acl)){
154           if($this->SubObjects[$var]['status'] == "edited"){
155             $this->SubObjects[$var]['status']= "delete";
156           }else{
157             unset($this->SubObjects[$var]);
158           }
159         }
160       }
161     }
163     /* Save Dialog */
164     if(isset($_POST['SaveSubObject'])){
165       $this->dialog->save_object();
166       $msgs = $this->dialog->check();
167       if(count($msgs)>0){
168         foreach($msgs as $msg){
169           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
170         }
171       }else{
172         $obj = $this->dialog->save();
173         if(isset($obj['remove'])){
174           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
175             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
176           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
177             unset($this->SubObjects[$obj['remove']['from']]);
178           }
179           $obj['status'] = "new";
180           $this->SubObjects[$obj['remove']['to']] = $obj;
181           unset($this->SubObjects[$obj['remove']['to']]['remove']);
182         }else{
183           $this->SubObjects[$obj['cn']]=$obj;
184         }
185         $this->is_dialog=false;
186         unset($this->dialog);
187         $this->dialog=FALSE;
188       }
189     }
191     /* Sort entries */
192     $tmp = $keys = array();
193     foreach($this->SubObjects as $key => $entry){
194       $keys[$key]=$key;
195     }
196     natcasesort($keys);
197     foreach($keys as $key){
198       $tmp[$key]=$this->SubObjects[$key];
199     } 
200     $this->SubObjects = $tmp;
202     /* Cancel Dialog */
203     if(isset($_POST['CancelSubObject'])){
204       $this->is_dialog=false; 
205       unset($this->dialog);
206       $this->dialog=FALSE;
207     }
209     /* Print dialog if $this->dialog is set */
210     if(is_object($this->dialog)){
211       $this->dialog->save_object();
212       $display = $this->dialog->execute();
213       return($display);
214     }
216     $ui = get_userinfo();
217     $ret = $this->getList();
218     $tmp = array();
219     foreach($this->SubObjects as $key => $obj){
220       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
221       if((preg_match("/r/",$acl) || $obj['dn'] == "new") && isset($ret[$key])){
222         $tmp[$key] = $ret[$key];
223       } 
224     }
225     $smarty->assign("SubObjects",$tmp);
228     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
229      * If we post the escaped strings they will be escaped again
230      */
231     foreach($this->attributes as $attrs){
232       if(get_magic_quotes_gpc()){
233         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
234       }else{
235         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
236       }
237     }
239     $c_dn = $this->acl_base_for_current_object($this->dn);
240     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
241     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
243     $tmp = $this->plInfo();
244     foreach($tmp['plProvidedAcls'] as $name => $translation) {
245       $smarty->assign($name."ACL",$this->getacl($name));
246     }
247     
249     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
250     return($display);
251   }
253   /* Generate listbox friendly SubObject list
254   */
255   function getList(){
256     $a_return=array();
257     foreach($this->SubObjects as $obj){
258       if($obj['status'] != "delete"){
260         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
261           if (preg_match("/\[\*\]/", $obj['description'])){
262             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
263           } else {
264             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
265           }
266         }else{
267           if (preg_match("/\[\*\]/", $obj['description'])){
268             $a_return[$obj['cn']]= $obj['cn'];
269           } else {
270             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
271           }
272         }
273       }
274     }
275     return($a_return);
276   }
278   /* Delete me, and all my subtrees
279    */
280   function remove_from_parent()
281   {
282     if($this->acl_is_removeable()){
283       $ldap = $this->config->get_ldap_link();
284       $ldap->cd ($this->dn);
285       $release = $this->parent->parent->fai_release;
286       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
287       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
288       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
290       foreach($this->SubObjects as $name => $obj){
291         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
292         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
293       }
294       $this->handle_post_events("remove");
295     }
296   }
299   /* Save data to object 
300    */
301   function save_object()
302   {
303     if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
304       plugin::save_object();
305       foreach($this->attributes as $attrs){
306         if(isset($_POST[$attrs])){
307           $this->$attrs = $_POST[$attrs];
308         }
309       }
310     }
311   }
314   /* Check supplied data */
315   function check()
316   {
317     /* Call common method to give check the hook */
318     $message= plugin::check();
320     /* Ensure that we do not overwrite an allready existing entry 
321      */
322     if($this->is_new){
323       $release = $this->parent->parent->fai_release;
324       $new_dn= 'cn='.$this->cn.",".get_ou('faivariableou').get_ou('faiou').$release;
325       $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
326       if(isset($res[$this->cn])){
327         $message[] = msgPool::duplicated(_("Name"));
328       }
329     }
331     return ($message);
332   }
335   /* Save to LDAP */
336   function save()
337   {
338     plugin::save();
339  
340     $ldap = $this->config->get_ldap_link();
341     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
343     if($this->initially_was_account){
344       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
345     }else{
346       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
347     }
348  
349     /* Prepare FAIscriptEntry to write it to ldap
350      * First sort array.
351      *  Because we must delete old entries first.
352      * After deletion, we perform add and modify 
353      */
354     $Objects = array();
355     foreach($this->SubObjects as $name => $obj){
356       if($obj['status'] == "delete"){
357         $Objects[$name] = $obj; 
358       }
359     }
360     foreach($this->SubObjects as $name => $obj){
361       if($obj['status'] != "delete"){
362         $Objects[$name] = $obj; 
363       }
364     }
366     foreach($Objects as $name => $obj){
368       foreach($this->sub64coded as $codeIt){
369         $obj[$codeIt]=base64_encode($obj[$codeIt]);
370       }
372       $tmp = array();
373       foreach($this->subAttributes as $attrs){
374         if(empty($obj[$attrs])){
375           $obj[$attrs] = array();
376         }
377         $tmp[$attrs] = $obj[$attrs];
378       }    
379         
380       $tmp['objectClass'] = $this->subClasses;
382       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
384       if($obj['status']=="new"){
385         $ldap->cat($sub_dn,array("objectClass"));
386         if($ldap->count()){
387           $obj['status']="edited";
388         }
389       }
391       /* Tag object */
392       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
393       
394       if($obj['status'] == "delete"){
395         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
396         $this->handle_post_events("remove");
397       }elseif($obj['status'] == "edited"){
398         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
399         $this->handle_post_events("modify");
400       }elseif($obj['status']=="new"){
401         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
402         $this->handle_post_events("add");
403       }
405     }
406   }
409   function PrepareForCopyPaste($source)
410   {
411     plugin::PrepareForCopyPaste($source);
413     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
414      */
415     $ldap     = $this->config->get_ldap_link();
416     $ldap->cd ($source['dn']);
417     $attrs_to_search = $this->subAttributes;
418     $attrs_to_search[] = "FAIstate";
419     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
420     while($object = $ldap->fetch()){
422       /* Skip objects, that are tagged as removed */
423       if(isset($object['FAIstate'][0])){
424         if(preg_match("/removed$/",$object['FAIstate'][0])){
425           continue;
426         }
427       }
429       /* Set status for save management */
430       foreach($this->subAttributes as $attrs){
431         if(!isset($object[$attrs][0])){
432           $this->SubObjects[$object['cn'][0]][$attrs]="";
433         }else{
434           $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
435         }
436       }
437       foreach($this->sub64coded as $codeIt){
438         $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
439       }
440       $this->SubObjects[$object['cn'][0]]['status']      = "edited";
441       $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
442     }
443   } 
445  
446   /* Return plugin informations for acl handling */ 
447   static function plInfo()
448   {
449     return (array( 
450           "plShortName" => _("Variable"),
451           "plDescription" => _("FAI variable"),
452           "plSelfModify"  => FALSE,
453           "plDepends"     => array(),
454           "plPriority"    => 22,
455           "plSection"     => array("administration"),
456           "plCategory"    => array("fai"),
457           "plProvidedAcls" => array(
458             "cn"                => _("Name")." ("._("Read only").")",
459             "description"       => _("Description"))
460           ));
461   }
464   /*! \brief  Used for copy & paste.
465     Returns a HTML input mask, which allows to change the cn of this entry.
466     @param  Array   Array containing current status && a HTML template.
467    */
468   function getCopyDialog()
469   {
470     $vars = array("cn");
471     $smarty = get_smarty();
472     $smarty->assign("cn", htmlentities($this->cn));
473     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
474     $ret = array();
475     $ret['string'] = $str;
476     $ret['status'] = "";
477     return($ret);
478   }
481   /*! \brief  Used for copy & paste.
482     Some entries must be renamed to avaoid duplicate entries.
483    */
484   function saveCopyDialog()
485   {
486     if(isset($_POST['cn'])){
487       $this->cn = get_post('cn');
488     }
489   }
492 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
493 ?>