Code

list base image migration for gosa-plugins
[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);
286       $faifilter = session::get('faifilter');
287       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
288       if($faifilter['branch'] == "main"){
289         $use_dn = $this->dn;
290       }
291   
292       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
293       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
295       foreach($this->SubObjects as $name => $obj){
296         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
297         if($faifilter['branch'] == "main"){
298           $use_dn = $obj['dn'];
299         }
300         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
301       }
302       $this->handle_post_events("remove");
303     }
304   }
307   /* Save data to object 
308    */
309   function save_object()
310   {
311     if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
312       plugin::save_object();
313       foreach($this->attributes as $attrs){
314         if(isset($_POST[$attrs])){
315           $this->$attrs = $_POST[$attrs];
316         }
317       }
318     }
319   }
322   /* Check supplied data */
323   function check()
324   {
325     /* Call common method to give check the hook */
326     $message= plugin::check();
328     /* Ensure that we do not overwrite an allready existing entry 
329      */
330     if($this->is_new){
331       $new_dn= 'cn='.$this->cn.",".get_ou('faivariableou').get_ou('faiou').session::get('CurrentMainBase');
332       $faifilter = session::get('faifilter');
333       if($faifilter['branch']!="main"){
334         $new_dn ='cn='.$this->cn.",".get_ou('faivariableou').$faifilter['branch'];
335       }
337       $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
338       if(isset($res[$this->cn])){
339         $message[] = msgPool::duplicated(_("Name"));
340       }
341     }
343     return ($message);
344   }
347   /* Save to LDAP */
348   function save()
349   {
350     plugin::save();
351  
352     $ldap = $this->config->get_ldap_link();
353     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
355     if($this->initially_was_account){
356       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
357     }else{
358       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
359     }
360  
361     /* Prepare FAIscriptEntry to write it to ldap
362      * First sort array.
363      *  Because we must delete old entries first.
364      * After deletion, we perform add and modify 
365      */
366     $Objects = array();
367     foreach($this->SubObjects as $name => $obj){
368       if($obj['status'] == "delete"){
369         $Objects[$name] = $obj; 
370       }
371     }
372     foreach($this->SubObjects as $name => $obj){
373       if($obj['status'] != "delete"){
374         $Objects[$name] = $obj; 
375       }
376     }
378     foreach($Objects as $name => $obj){
380       foreach($this->sub64coded as $codeIt){
381         $obj[$codeIt]=base64_encode($obj[$codeIt]);
382       }
384       $tmp = array();
385       foreach($this->subAttributes as $attrs){
386         if(empty($obj[$attrs])){
387           $obj[$attrs] = array();
388         }
389         $tmp[$attrs] = $obj[$attrs];
390       }    
391         
392       $tmp['objectClass'] = $this->subClasses;
394       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
396       if($obj['status']=="new"){
397         $ldap->cat($sub_dn,array("objectClass"));
398         if($ldap->count()){
399           $obj['status']="edited";
400         }
401       }
403       /* Tag object */
404       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
405       
406       if($obj['status'] == "delete"){
407         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
408         $this->handle_post_events("remove");
409       }elseif($obj['status'] == "edited"){
410         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
411         $this->handle_post_events("modify");
412       }elseif($obj['status']=="new"){
413         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
414         $this->handle_post_events("add");
415       }
417     }
418   }
421   function PrepareForCopyPaste($source)
422   {
423     plugin::PrepareForCopyPaste($source);
425     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
426      */
427     $ldap     = $this->config->get_ldap_link();
428     $ldap->cd ($source['dn']);
429     $attrs_to_search = $this->subAttributes;
430     $attrs_to_search[] = "FAIstate";
431     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
432     while($object = $ldap->fetch()){
434       /* Skip objects, that are tagged as removed */
435       if(isset($object['FAIstate'][0])){
436         if(preg_match("/removed$/",$object['FAIstate'][0])){
437           continue;
438         }
439       }
441       /* Set status for save management */
442       foreach($this->subAttributes as $attrs){
443         if(!isset($object[$attrs][0])){
444           $this->SubObjects[$object['cn'][0]][$attrs]="";
445         }else{
446           $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
447         }
448       }
449       foreach($this->sub64coded as $codeIt){
450         $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
451       }
452       $this->SubObjects[$object['cn'][0]]['status']      = "edited";
453       $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
454     }
455   } 
457  
458   /* Return plugin informations for acl handling */ 
459   static function plInfo()
460   {
461     return (array( 
462           "plShortName" => _("Variable"),
463           "plDescription" => _("FAI variable"),
464           "plSelfModify"  => FALSE,
465           "plDepends"     => array(),
466           "plPriority"    => 22,
467           "plSection"     => array("administration"),
468           "plCategory"    => array("fai"),
469           "plProvidedAcls" => array(
470             "cn"                => _("Name")." ("._("Read only").")",
471             "description"       => _("Description"))
472           ));
473   }
476   /*! \brief  Used for copy & paste.
477     Returns a HTML input mask, which allows to change the cn of this entry.
478     @param  Array   Array containing current status && a HTML template.
479    */
480   function getCopyDialog()
481   {
482     $vars = array("cn");
483     $smarty = get_smarty();
484     $smarty->assign("cn", htmlentities($this->cn));
485     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
486     $ret = array();
487     $ret['string'] = $str;
488     $ret['status'] = "";
489     return($ret);
490   }
493   /*! \brief  Used for copy & paste.
494     Some entries must be renamed to avaoid duplicate entries.
495    */
496   function saveCopyDialog()
497   {
498     if(isset($_POST['cn'])){
499       $this->cn = get_post('cn');
500     }
501   }
504 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
505 ?>