Code

Added sorting to FAIscripts
[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   ;
39   var $view_logged      = FALSE;
41   function faiVariable ($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
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);
60       $attrs_to_search = $this->subAttributes;
61       $attrs_to_search[] = "FAIstate";
62       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
64       while($object = $ldap->fetch()){
65       
66         /* Skip objects, that are tagged as removed */
67         if(isset($object['FAIstate'][0])){
68           if(preg_match("/removed$/",$object['FAIstate'][0])){
69             continue;
70           }
71         }
73         /* Set status for save management */
74         foreach($this->subAttributes as $attrs){
75           if(!isset($object[$attrs][0])){
76             $this->SubObjects[$object['cn'][0]][$attrs]="";
77           }else{
78             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
79           }
80         }
81      
82         foreach($this->sub64coded as $codeIt){
83           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
84         }
85  
86         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
87         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
88       }
90     }
91     $this->ui = get_userinfo();
92   }
95   function acl_base_for_current_object($dn)
96   {
97     if($dn == "new"){
98       if($this->dn == "new"){
99         $dn= $_SESSION['CurrentMainBase'];
100       }else{
101         $dn = $this->dn;
102       }
103     }
104     return($dn);
105   }
108   function execute()
109   {
110     /* Call parent execute */
111     plugin::execute();
113     if($this->is_account && !$this->view_logged){
114       $this->view_logged = TRUE;
115       new log("view","fai/".get_class($this),$this->dn);
116     }
118     /* Fill templating stuff */
119     $smarty= get_smarty();
120     $display= "";
122     /* Add new sub object */
123     if(isset($_POST['AddSubObject'])){
124       $this->dialog= new $this->subClassName($this->config,"new");
125       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
126       $this->dialog->set_acl_category("fai");
127       $this->dialog->parent = &$this;
128       $this->is_dialog=true;
129     }
131     if($this->dn != "new"){
132       $_SESSION['objectinfo']= $this->dn;
133     }
136     /* Edit selected Sub Object */
137     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
139       $var = $_POST['SubObject'][0];
140       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
141       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
142       $this->dialog->set_acl_category("fai");
143       $this->dialog->set_acl_base($c_dn);
144       $this->dialog->parent = &$this;
145       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
146       $this->is_dialog=true;
147     }
148     
149     /* Remove Sub object */
150     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
151       foreach($_POST['SubObject'] as $var){
153         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
154         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
155         if(preg_match("/d/",$acl)){
156           if($this->SubObjects[$var]['status'] == "edited"){
157             $this->SubObjects[$var]['status']= "delete";
158           }else{
159             unset($this->SubObjects[$var]);
160           }
161         }
162       }
163     }
165     /* Save Dialog */
166     if(isset($_POST['SaveSubObject'])){
167       $this->dialog->save_object();
168       $msgs = $this->dialog->check();
169       if(count($msgs)>0){
170         foreach($msgs as $msg){
171           print_red($msg);
172         }
173       }else{
174         $obj = $this->dialog->save();
175         if(isset($obj['remove'])){
176           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
177             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
178           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
179             unset($this->SubObjects[$obj['remove']['from']]);
180           }
181           $obj['status'] = "new";
182           $this->SubObjects[$obj['remove']['to']] = $obj;
183           unset($this->SubObjects[$obj['remove']['to']]['remove']);
184         }else{
185           $this->SubObjects[$obj['cn']]=$obj;
186         }
187         $this->is_dialog=false;
188         unset($this->dialog);
189         $this->dialog=NULL;
190       }
191     }
193     /* Sort entries */
194     $tmp = $keys = array();
195     foreach($this->SubObjects as $key => $entry){
196       $keys[$key]=$key;
197     }
198     natcasesort($keys);
199     foreach($keys as $key){
200       $tmp[$key]=$this->SubObjects[$key];
201     } 
202     $this->SubObjects = $tmp;
204     /* Cancel Dialog */
205     if(isset($_POST['CancelSubObject'])){
206       $this->is_dialog=false; 
207       unset($this->dialog);
208       $this->dialog=NULL;
209     }
211     /* Print dialog if $this->dialog is set */
212     if($this->dialog){
213       $this->dialog->save_object();
214       $display = $this->dialog->execute();
215       return($display);
216     }
218     $ui = get_userinfo();
219     $ret = $this->getList();
220     $tmp = array();
221     foreach($this->SubObjects as $key => $obj){
222       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
223       if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
224         $tmp[$key] = $ret[$key];
225       } 
226     }
227     $smarty->assign("SubObjects",$tmp);
230     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
231      * If we post the escaped strings they will be escaped again
232      */
233     foreach($this->attributes as $attrs){
234       if(get_magic_quotes_gpc()){
235         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
236       }else{
237         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
238       }
239     }
241     $c_dn = $this->acl_base_for_current_object($this->dn);
242     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
243     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
245     $tmp = $this->plInfo();
246     foreach($tmp['plProvidedAcls'] as $name => $translation) {
247       $smarty->assign($name."ACL",$this->getacl($name));
248     }
249     
251     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
252     return($display);
253   }
255   /* Generate listbox friendly SubObject list
256   */
257   function getList(){
258     $a_return=array();
259     foreach($this->SubObjects as $obj){
260       if($obj['status'] != "delete"){
262         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
263           if (preg_match("/\[\*\]/", $obj['description'])){
264             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
265           } else {
266             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
267           }
268         }else{
269           if (preg_match("/\[\*\]/", $obj['description'])){
270             $a_return[$obj['cn']]= $obj['cn'];
271           } else {
272             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
273           }
274         }
275       }
276     }
277     return($a_return);
278   }
280   /* Delete me, and all my subtrees
281    */
282   function remove_from_parent()
283   {
284     if($this->acl_is_removeable()){
285       $ldap = $this->config->get_ldap_link();
286       $ldap->cd ($this->dn);
288       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
289       if($_SESSION['faifilter']['branch'] == "main"){
290         $use_dn = $this->dn;
291       }
292   
293       prepare_to_save_FAI_object($use_dn,array(),true);
294       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
296       foreach($this->SubObjects as $name => $obj){
297         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
298         if($_SESSION['faifilter']['branch'] == "main"){
299           $use_dn = $obj['dn'];
300         }
301         prepare_to_save_FAI_object($use_dn,array(),true);
302       }
303       $this->handle_post_events("remove");
304     }
305   }
308   /* Save data to object 
309    */
310   function save_object()
311   {
312     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
313       plugin::save_object();
314       foreach($this->attributes as $attrs){
315         if(isset($_POST[$attrs])){
316           $this->$attrs = $_POST[$attrs];
317         }
318       }
319     }
320   }
323   /* Check supplied data */
324   function check()
325   {
326     /* Call common method to give check the hook */
327     $message= plugin::check();
329     return ($message);
330   }
333   /* Save to LDAP */
334   function save()
335   {
336     plugin::save();
337  
338     $ldap = $this->config->get_ldap_link();
339     prepare_to_save_FAI_object($this->dn,$this->attrs);
340     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
342     if($this->initially_was_account){
343       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
344     }else{
345       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
346     }
347  
348     /* Do object tagging */
349     $this->handle_object_tagging();
350  
351     /* Prepare FAIscriptEntry to write it to ldap
352      * First sort array.
353      *  Because we must delete old entries first.
354      * After deletion, we perform add and modify 
355      */
356     $Objects = array();
357     foreach($this->SubObjects as $name => $obj){
358       if($obj['status'] == "delete"){
359         $Objects[$name] = $obj; 
360       }
361     }
362     foreach($this->SubObjects as $name => $obj){
363       if($obj['status'] != "delete"){
364         $Objects[$name] = $obj; 
365       }
366     }
368     foreach($Objects as $name => $obj){
370       foreach($this->sub64coded as $codeIt){
371         $obj[$codeIt]=base64_encode($obj[$codeIt]);
372       }
374       $tmp = array();
375       foreach($this->subAttributes as $attrs){
376         if(empty($obj[$attrs])){
377           $obj[$attrs] = array();
378         }
379         $tmp[$attrs] = $obj[$attrs];
380       }    
381         
382       $tmp['objectClass'] = $this->subClasses;
384       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
386       if($obj['status']=="new"){
387         $ldap->cat($sub_dn,array("objectClass"));
388         if($ldap->count()){
389           $obj['status']="edited";
390         }
391       }
393       /* Check if gosaAdministrativeUnitTag is required as object class */
394       if($obj['status'] == "edited"){
395         $ldap->cat($sub_dn,array("objectClass"));
396         $attrs = $ldap->fetch();
397         if(isset($attrs['objectClass'])){
398           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
399             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
400           }
401         }
402       }
403       
404       if($obj['status'] == "delete"){
405         prepare_to_save_FAI_object($sub_dn,array(),true);
406         $this->handle_post_events("remove");
407       }elseif($obj['status'] == "edited"){
408         prepare_to_save_FAI_object($sub_dn,$tmp);
409         $this->handle_post_events("modify");
410       }elseif($obj['status']=="new"){
411         prepare_to_save_FAI_object($sub_dn,$tmp);
412         $this->handle_post_events("add");
413       }
415       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
416     }
417   }
419   
420   /* Return plugin informations for acl handling */ 
421   function plInfo()
422   {
423     return (array( 
424           "plShortName" => _("Variable"),
425           "plDescription" => _("FAI variable"),
426           "plSelfModify"  => FALSE,
427           "plDepends"     => array(),
428           "plPriority"    => 22,
429           "plSection"     => array("administration"),
430           "plCategory"    => array("fai"),
431           "plProvidedAcls" => array(
432             "cn"                => _("Name")." ("._("Read only").")",
433             "description"       => _("Description"))
434           ));
435   }
438 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
439 ?>