Code

Some session fixes
[gosa.git] / gosa-core / plugins / admin / fai / class_faiVariable.inc
1 <?php
3 class faiVariable 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","FAIvariable");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIvariableEntry";
21   var $subClasses       = array("top","FAIclass","FAIvariableEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiVariableEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIvariableContent"); 
28   var $sub64coded       = array();  
30   /* Specific attributes */
31   var $cn               = "";       // The class name for this object
32   var $description      = "";       // The description for this set of partitions
33   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
34   var $SubObjects       = array();  // All leafobjects of this object
36   var $FAIstate         = "";
37   var $ui   ;
38   var $view_logged      = FALSE;
40   function faiVariable (&$config, $dn= NULL)
41   {
42     /* Load Attributes */
43     plugin::plugin ($config, $dn);
45     if($dn != "new"){
46       $this->dn =$dn;
48       /* Get FAIstate
49        */
50       if(isset($this->attrs['FAIstate'][0])){
51         $this->FAIstate = $this->attrs['FAIstate'][0];
52       }
54       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
55        */
56       $ldap     = $this->config->get_ldap_link();
57       $ldap->cd ($this->dn);
59       $attrs_to_search = $this->subAttributes;
60       $attrs_to_search[] = "FAIstate";
61       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
63       while($object = $ldap->fetch()){
64       
65         /* Skip objects, that are tagged as removed */
66         if(isset($object['FAIstate'][0])){
67           if(preg_match("/removed$/",$object['FAIstate'][0])){
68             continue;
69           }
70         }
72         /* Set status for save management */
73         foreach($this->subAttributes as $attrs){
74           if(!isset($object[$attrs][0])){
75             $this->SubObjects[$object['cn'][0]][$attrs]="";
76           }else{
77             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
78           }
79         }
80      
81         foreach($this->sub64coded as $codeIt){
82           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
83         }
84  
85         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
86         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
87       }
89     }
90     $this->ui = get_userinfo();
91   }
94   function acl_base_for_current_object($dn)
95   {
96     if($dn == "new"){
97       if($this->dn == "new"){
98         $dn = session::get('CurrentMainBase');
99       }else{
100         $dn = $this->dn;
101       }
102     }
103     return($dn);
104   }
107   function execute()
108   {
109     /* Call parent execute */
110     plugin::execute();
112     if($this->is_account && !$this->view_logged){
113       $this->view_logged = TRUE;
114       new log("view","fai/".get_class($this),$this->dn);
115     }
117     /* Fill templating stuff */
118     $smarty= get_smarty();
119     $display= "";
121     /* Add new sub object */
122     if(isset($_POST['AddSubObject'])){
123       $this->dialog= new $this->subClassName($this->config,"new");
124       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
125       $this->dialog->set_acl_category("fai");
126       $this->dialog->parent = &$this;
127       $this->is_dialog=true;
128     }
130     if($this->dn != "new"){
131       session::set('objectinfo',$this->dn);
132     }
135     /* Edit selected Sub Object */
136     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
138       $var = $_POST['SubObject'][0];
139       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
140       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
141       $this->dialog->set_acl_category("fai");
142       $this->dialog->set_acl_base($c_dn);
143       $this->dialog->parent = &$this;
144       session::set('objectinfo',$this->SubObjects[$var]['dn']);
145       $this->is_dialog=true;
146     }
147     
148     /* Remove Sub object */
149     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
150       foreach($_POST['SubObject'] as $var){
152         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
153         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
154         if(preg_match("/d/",$acl)){
155           if($this->SubObjects[$var]['status'] == "edited"){
156             $this->SubObjects[$var]['status']= "delete";
157           }else{
158             unset($this->SubObjects[$var]);
159           }
160         }
161       }
162     }
164     /* Save Dialog */
165     if(isset($_POST['SaveSubObject'])){
166       $this->dialog->save_object();
167       $msgs = $this->dialog->check();
168       if(count($msgs)>0){
169         foreach($msgs as $msg){
170           print_red($msg);
171         }
172       }else{
173         $obj = $this->dialog->save();
174         if(isset($obj['remove'])){
175           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
176             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
177           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
178             unset($this->SubObjects[$obj['remove']['from']]);
179           }
180           $obj['status'] = "new";
181           $this->SubObjects[$obj['remove']['to']] = $obj;
182           unset($this->SubObjects[$obj['remove']['to']]['remove']);
183         }else{
184           $this->SubObjects[$obj['cn']]=$obj;
185         }
186         $this->is_dialog=false;
187         unset($this->dialog);
188         $this->dialog=FALSE;
189       }
190     }
192     /* Sort entries */
193     $tmp = $keys = array();
194     foreach($this->SubObjects as $key => $entry){
195       $keys[$key]=$key;
196     }
197     natcasesort($keys);
198     foreach($keys as $key){
199       $tmp[$key]=$this->SubObjects[$key];
200     } 
201     $this->SubObjects = $tmp;
203     /* Cancel Dialog */
204     if(isset($_POST['CancelSubObject'])){
205       $this->is_dialog=false; 
206       unset($this->dialog);
207       $this->dialog=FALSE;
208     }
210     /* Print dialog if $this->dialog is set */
211     if(is_object($this->dialog)){
212       $this->dialog->save_object();
213       $display = $this->dialog->execute();
214       return($display);
215     }
217     $ui = get_userinfo();
218     $ret = $this->getList();
219     $tmp = array();
220     foreach($this->SubObjects as $key => $obj){
221       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
222       if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
223         $tmp[$key] = $ret[$key];
224       } 
225     }
226     $smarty->assign("SubObjects",$tmp);
229     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
230      * If we post the escaped strings they will be escaped again
231      */
232     foreach($this->attributes as $attrs){
233       if(get_magic_quotes_gpc()){
234         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
235       }else{
236         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
237       }
238     }
240     $c_dn = $this->acl_base_for_current_object($this->dn);
241     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
242     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
244     $tmp = $this->plInfo();
245     foreach($tmp['plProvidedAcls'] as $name => $translation) {
246       $smarty->assign($name."ACL",$this->getacl($name));
247     }
248     
250     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
251     return($display);
252   }
254   /* Generate listbox friendly SubObject list
255   */
256   function getList(){
257     $a_return=array();
258     foreach($this->SubObjects as $obj){
259       if($obj['status'] != "delete"){
261         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
262           if (preg_match("/\[\*\]/", $obj['description'])){
263             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
264           } else {
265             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
266           }
267         }else{
268           if (preg_match("/\[\*\]/", $obj['description'])){
269             $a_return[$obj['cn']]= $obj['cn'];
270           } else {
271             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
272           }
273         }
274       }
275     }
276     return($a_return);
277   }
279   /* Delete me, and all my subtrees
280    */
281   function remove_from_parent()
282   {
283     if($this->acl_is_removeable()){
284       $ldap = $this->config->get_ldap_link();
285       $ldap->cd ($this->dn);
287       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
288       if($_SESSION['faifilter']['branch'] == "main"){
289         $use_dn = $this->dn;
290       }
291   
292       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(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
297         if($_SESSION['faifilter']['branch'] == "main"){
298           $use_dn = $obj['dn'];
299         }
300         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'])) && ($this->FAIstate != "freeze") ){
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     return ($message);
329   }
332   /* Save to LDAP */
333   function save()
334   {
335     plugin::save();
336  
337     $ldap = $this->config->get_ldap_link();
338     prepare_to_save_FAI_object($this->dn,$this->attrs);
339     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
341     if($this->initially_was_account){
342       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
343     }else{
344       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
345     }
346  
347     /* Do object tagging */
348     $this->handle_object_tagging();
349  
350     /* Prepare FAIscriptEntry to write it to ldap
351      * First sort array.
352      *  Because we must delete old entries first.
353      * After deletion, we perform add and modify 
354      */
355     $Objects = array();
356     foreach($this->SubObjects as $name => $obj){
357       if($obj['status'] == "delete"){
358         $Objects[$name] = $obj; 
359       }
360     }
361     foreach($this->SubObjects as $name => $obj){
362       if($obj['status'] != "delete"){
363         $Objects[$name] = $obj; 
364       }
365     }
367     foreach($Objects as $name => $obj){
369       foreach($this->sub64coded as $codeIt){
370         $obj[$codeIt]=base64_encode($obj[$codeIt]);
371       }
373       $tmp = array();
374       foreach($this->subAttributes as $attrs){
375         if(empty($obj[$attrs])){
376           $obj[$attrs] = array();
377         }
378         $tmp[$attrs] = $obj[$attrs];
379       }    
380         
381       $tmp['objectClass'] = $this->subClasses;
383       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
385       if($obj['status']=="new"){
386         $ldap->cat($sub_dn,array("objectClass"));
387         if($ldap->count()){
388           $obj['status']="edited";
389         }
390       }
392       /* Check if gosaAdministrativeUnitTag is required as object class */
393       if($obj['status'] == "edited"){
394         $ldap->cat($sub_dn,array("objectClass"));
395         $attrs = $ldap->fetch();
396         if(isset($attrs['objectClass'])){
397           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
398             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
399           }
400         }
401       }
402       
403       if($obj['status'] == "delete"){
404         prepare_to_save_FAI_object($sub_dn,array(),true);
405         $this->handle_post_events("remove");
406       }elseif($obj['status'] == "edited"){
407         prepare_to_save_FAI_object($sub_dn,$tmp);
408         $this->handle_post_events("modify");
409       }elseif($obj['status']=="new"){
410         prepare_to_save_FAI_object($sub_dn,$tmp);
411         $this->handle_post_events("add");
412       }
414       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
415     }
416   }
419   function PrepareForCopyPaste($source)
420   {
421     plugin::PrepareForCopyPaste($source);
423     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
424      */
425     $ldap     = $this->config->get_ldap_link();
426     $ldap->cd ($source['dn']);
427     $attrs_to_search = $this->subAttributes;
428     $attrs_to_search[] = "FAIstate";
429     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
430     while($object = $ldap->fetch()){
432       /* Skip objects, that are tagged as removed */
433       if(isset($object['FAIstate'][0])){
434         if(preg_match("/removed$/",$object['FAIstate'][0])){
435           continue;
436         }
437       }
439       /* Set status for save management */
440       foreach($this->subAttributes as $attrs){
441         if(!isset($object[$attrs][0])){
442           $this->SubObjects[$object['cn'][0]][$attrs]="";
443         }else{
444           $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
445         }
446       }
447       foreach($this->sub64coded as $codeIt){
448         $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
449       }
450       $this->SubObjects[$object['cn'][0]]['status']      = "edited";
451       $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
452     }
453   } 
455  
456   /* Return plugin informations for acl handling */ 
457   static function plInfo()
458   {
459     return (array( 
460           "plShortName" => _("Variable"),
461           "plDescription" => _("FAI variable"),
462           "plSelfModify"  => FALSE,
463           "plDepends"     => array(),
464           "plPriority"    => 22,
465           "plSection"     => array("administration"),
466           "plCategory"    => array("fai"),
467           "plProvidedAcls" => array(
468             "cn"                => _("Name")." ("._("Read only").")",
469             "description"       => _("Description"))
470           ));
471   }
474 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
475 ?>