Code

Bloody fucking stuff
[gosa.git] / 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 $dialog           = NULL;     // a dialog, e.g. new disk dialog
35   var $SubObjects       = array();  // All leafobjects of this object
37   var $FAIstate         = "";
38   var $ui   ;
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       }
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['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     /* Fill templating stuff */
112     $smarty= get_smarty();
113     $display= "";
115     /* Add new sub object */
116     if(isset($_POST['AddSubObject'])){
117       $this->dialog= new $this->subClassName($this->config,"new");
118       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
119       $this->dialog->set_acl_category("fai");
120       $this->is_dialog=true;
121     }
123     if($this->dn != "new"){
124       $_SESSION['objectinfo']= $this->dn;
125     }
128     /* Edit selected Sub Object */
129     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
131       $var = $_POST['SubObject'][0];
132       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
133       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
134       $this->dialog->set_acl_category("fai");
135       $this->dialog->set_acl_base($c_dn);
136       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
137       $this->is_dialog=true;
138     }
139     
140     /* Remove Sub object */
141     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
142       foreach($_POST['SubObject'] as $var){
144         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
145         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
146         if(preg_match("/d/",$acl)){
147           if($this->SubObjects[$var]['status'] == "edited"){
148             $this->SubObjects[$var]['status']= "delete";
149           }else{
150             unset($this->SubObjects[$var]);
151           }
152         }
153       }
154     }
156     /* Save Dialog */
157     if(isset($_POST['SaveSubObject'])){
158       $this->dialog->save_object();
159       $msgs = $this->dialog->check();
160       if(count($msgs)>0){
161         foreach($msgs as $msg){
162           print_red($msg);
163         }
164       }else{
165         $obj = $this->dialog->save();
166         if(isset($obj['remove'])){
167           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
168             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
169           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
170             unset($this->SubObjects[$obj['remove']['from']]);
171           }
172           $obj['status'] = "new";
173           $this->SubObjects[$obj['remove']['to']] = $obj;
174           unset($this->SubObjects[$obj['remove']['to']]['remove']);
175         }else{
176           $this->SubObjects[$obj['cn']]=$obj;
177         }
178         $this->is_dialog=false;
179         unset($this->dialog);
180         $this->dialog=NULL;
181       }
182     }
184     /* Sort entries */
185     $tmp = $keys = array();
186     foreach($this->SubObjects as $key => $entry){
187       $keys[$key]=$key;
188     }
189     natcasesort($keys);
190     foreach($keys as $key){
191       $tmp[$key]=$this->SubObjects[$key];
192     } 
193     $this->SubObjects = $tmp;
195     /* Cancel Dialog */
196     if(isset($_POST['CancelSubObject'])){
197       $this->is_dialog=false; 
198       unset($this->dialog);
199       $this->dialog=NULL;
200     }
202     /* Print dialog if $this->dialog is set */
203     if($this->dialog){
204       $this->dialog->save_object();
205       $display = $this->dialog->execute();
206       return($display);
207     }
209     $smarty->assign("SubObjects",$this->getList());
211       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
212      * If we post the escaped strings they will be escaped again
213      */
215     foreach($this->attributes as $attrs){
216       if(get_magic_quotes_gpc()){
217         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
218       }else{
219         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
220       }
221     }
223     $c_dn = $this->acl_base_for_current_object($this->dn);
224     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
225     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
227     $tmp = $this->plInfo();
228     foreach($tmp['plProvidedAcls'] as $name => $translation) {
229       $smarty->assign($name."ACL",$this->getacl($name));
230     }
231     
233     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
234     return($display);
235   }
237   /* Generate listbox friendly SubObject list
238   */
239   function getList(){
240     $a_return=array();
241     foreach($this->SubObjects as $obj){
242       if($obj['status'] != "delete"){
244         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
245           if (preg_match("/\[\*\]/", $obj['description'])){
246             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
247           } else {
248             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
249           }
250         }else{
251           if (preg_match("/\[\*\]/", $obj['description'])){
252             $a_return[$obj['cn']]= $obj['cn'];
253           } else {
254             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
255           }
256         }
257       }
258     }
259     return($a_return);
260   }
262   /* Delete me, and all my subtrees
263    */
264   function remove_from_parent()
265   {
266     if($this->acl_is_removeable()){
267       $ldap = $this->config->get_ldap_link();
268       $ldap->cd ($this->dn);
270       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
271       if($_SESSION['faifilter']['branch'] == "main"){
272         $use_dn = $this->dn;
273       }
275       prepare_to_save_FAI_object($use_dn,array(),true);
277       foreach($this->SubObjects as $name => $obj){
278         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
279         if($_SESSION['faifilter']['branch'] == "main"){
280           $use_dn = $obj['dn'];
281         }
282         prepare_to_save_FAI_object($use_dn,array(),true);
283       }
284       $this->handle_post_events("remove");
285     }
286   }
289   /* Save data to object 
290    */
291   function save_object()
292   {
293     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
294       plugin::save_object();
295       foreach($this->attributes as $attrs){
296         if(isset($_POST[$attrs])){
297           $this->$attrs = $_POST[$attrs];
298         }
299       }
300     }
301   }
304   /* Check supplied data */
305   function check()
306   {
307     /* Call common method to give check the hook */
308     $message= plugin::check();
310     return ($message);
311   }
314   /* Save to LDAP */
315   function save()
316   {
317     plugin::save();
318  
319     $ldap = $this->config->get_ldap_link();
320     prepare_to_save_FAI_object($this->dn,$this->attrs);
321     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
322  
323     /* Do object tagging */
324     $this->handle_object_tagging();
325  
326     /* Prepare FAIscriptEntry to write it to ldap
327      * First sort array.
328      *  Because we must delete old entries first.
329      * After deletion, we perform add and modify 
330      */
331     $Objects = array();
332     foreach($this->SubObjects as $name => $obj){
333       if($obj['status'] == "delete"){
334         $Objects[$name] = $obj; 
335       }
336     }
337     foreach($this->SubObjects as $name => $obj){
338       if($obj['status'] != "delete"){
339         $Objects[$name] = $obj; 
340       }
341     }
343     foreach($Objects as $name => $obj){
345       foreach($this->sub64coded as $codeIt){
346         $obj[$codeIt]=base64_encode($obj[$codeIt]);
347       }
349       $tmp = array();
350       foreach($this->subAttributes as $attrs){
351         if(empty($obj[$attrs])){
352           $obj[$attrs] = array();
353         }
354         $tmp[$attrs] = $obj[$attrs];
355       }    
356         
357       $tmp['objectClass'] = $this->subClasses;
359       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
361       if($obj['status']=="new"){
362         $ldap->cat($sub_dn,array("objectClass"));
363         if($ldap->count()){
364           $obj['status']="edited";
365         }
366       }
368       /* Check if gosaAdministrativeUnitTag is required as object class */
369       if($obj['status'] == "edited"){
370         $ldap->cat($sub_dn,array("objectClass"));
371         $attrs = $ldap->fetch();
372         if(isset($attrs['objectClass'])){
373           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
374             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
375           }
376         }
377       }
378       
379       if($obj['status'] == "delete"){
380         prepare_to_save_FAI_object($sub_dn,array(),true);
381         $this->handle_post_events("remove");
382       }elseif($obj['status'] == "edited"){
383         prepare_to_save_FAI_object($sub_dn,$tmp);
384         $this->handle_post_events("modify");
385       }elseif($obj['status']=="new"){
386         prepare_to_save_FAI_object($sub_dn,$tmp);
387         $this->handle_post_events("add");
388       }
390       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
391     }
392   }
394   
395   /* Return plugin informations for acl handling */ 
396   function plInfo()
397   {
398     return (array( 
399           "plShortName" => _("Variable"),
400           "plDescription" => _("FAI variable"),
401           "plSelfModify"  => FALSE,
402           "plDepends"     => array(),
403           "plPriority"    => 22,
404           "plSection"     => array("administration"),
405           "plCategory"    => array("fai"),
406           "plProvidedAcls" => array(
407             "cn"                => _("Name")." ("._("Read only").")",
408             "description"       => _("Description"))
409           ));
410   }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>