Code

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