Code

39565634ae3260dfb449e4314574ff1c68899805
[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 $base             = "";
39   var $release          = "";
40   var $copy_paste_mode  = false;
42   var $CopyPasteVars  = array("SubObjects","FAIstate");
44   function faiVariable ($config, $dn= NULL)
45   {
46     /* Load Attributes */
47     plugin::plugin ($config, $dn);
49     $this->acl ="#all#";
51     /* If "dn==new" we try to create a new entry
52      * Else we must read all objects from ldap which belong to this entry.
53      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
54      */
56     if($dn != "new"){
57       $this->dn =$dn;
59       /* Set acls
60        */
61       $ui   = get_userinfo();
62       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
63       $acli = get_module_permission($acl, "FAIclass", $this->dn);
64       $this->acl=$acli;
66       /* Get FAIstate
67        */
68       if(isset($this->attrs['FAIstate'][0])){
69         $this->FAIstate = $this->attrs['FAIstate'][0];
70       }
72       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
73        */
74       $ldap     = $this->config->get_ldap_link();
75       $ldap->cd ($this->dn);
76       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
78       while($object = $ldap->fetch()){
79         /* Set status for save management */
80   
81         foreach($this->subAttributes as $attrs){
82           if(!isset($object[$attrs][0])){
83             $this->SubObjects[$object['cn'][0]][$attrs]="";
84           }else{
85             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
86           }
87         }
88      
89         foreach($this->sub64coded as $codeIt){
90           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
91         }
92  
93         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
94         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
95       }
96     }
97   }
99   function execute()
100   {
101         /* Call parent execute */
102         plugin::execute();
104     /* Fill templating stuff */
105     $smarty= get_smarty();
106     $display= "";
108     /* Add new sub object */
109     if(isset($_POST['AddSubObject'])){
110       $this->dialog= new $this->subClassName($this->config,"new");
111       $this->dialog->acl = $this->acl;
112       $this->is_dialog=true;
113     }
115     if($this->dn != "new"){
116       $_SESSION['objectinfo']= $this->dn;
117     }
120     /* Edit selected Sub Object */
121     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
123       $var = $_POST['SubObject'][0];
124       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
125       $this->dialog->acl = $this->acl;
126       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
127       $this->is_dialog=true;
128     }
129     
130     /* Remove Sub object */
131     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
132       foreach($_POST['SubObject'] as $var){
133         if($this->SubObjects[$var]['status'] == "edited"){
134           $this->SubObjects[$var]['status']= "delete";
135         }else{
136           unset($this->SubObjects[$var]);
137         }
138       }
139     }
141     /* Save Dialog */
142     if(isset($_POST['SaveSubObject'])){
143       $this->dialog->save_object();
144       $msgs = $this->dialog->check();
145       if(count($msgs)>0){
146         foreach($msgs as $msg){
147           print_red($msg);
148         }
149       }else{
150         $obj = $this->dialog->save();
151         if(isset($obj['remove'])){
152           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
153             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
154           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
155             unset($this->SubObjects[$obj['remove']['from']]);
156           }
157           $obj['status'] = "new";
158           $this->SubObjects[$obj['remove']['to']] = $obj;
159           unset($this->SubObjects[$obj['remove']['to']]['remove']);
160         }else{
161           $this->SubObjects[$obj['cn']]=$obj;
162         }
163         $this->is_dialog=false;
164         unset($this->dialog);
165         $this->dialog=NULL;
166       }
167     }
169     /* Sort entries */
170     $tmp = $keys = array();
171     foreach($this->SubObjects as $key => $entry){
172       $keys[$key]=$key;
173     }
174     natcasesort($keys);
175     foreach($keys as $key){
176       $tmp[$key]=$this->SubObjects[$key];
177     } 
178     $this->SubObjects = $tmp;
180     /* Cancel Dialog */
181     if(isset($_POST['CancelSubObject'])){
182       $this->is_dialog=false; 
183       unset($this->dialog);
184       $this->dialog=NULL;
185     }
187     /* Print dialog if $this->dialog is set */
188     if($this->dialog){
189       $this->dialog->save_object();
190       $display = $this->dialog->execute();
191       return($display);
192     }
194     $smarty->assign("SubObjects",$this->getList());
196       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
197      * If we post the escaped strings they will be escaped again
198      */
200     foreach($this->attributes as $attrs){
201       if(get_magic_quotes_gpc()){
202         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
203       }else{
204         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
205       }
206     }
208       foreach($this->attributes as $attr){
209       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
210     }
213     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
214     return($display);
215   }
217   /* Generate listbox friendly SubObject list
218   */
219   function getList(){
220     $a_return=array();
221     foreach($this->SubObjects as $obj){
222       if($obj['status'] != "delete"){
224         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
225           if (preg_match("/\[\*\]/", $obj['description'])){
226             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
227           } else {
228             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
229           }
230         }else{
231           if (preg_match("/\[\*\]/", $obj['description'])){
232             $a_return[$obj['cn']]= $obj['cn'];
233           } else {
234             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
235           }
236         }
237       }
238     }
239     return($a_return);
240   }
242   /* Delete me, and all my subtrees
243    */
244   function remove_from_parent()
245   {
246     $ldap = $this->config->get_ldap_link();
247     $ldap->cd ($this->dn);
248     $ldap->rmdir_recursive($this->dn);
249     $this->handle_post_events("remove");    
250   }
253   /* Save data to object 
254    */
255   function save_object()
256   {
257     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
258       plugin::save_object();
259       foreach($this->attributes as $attrs){
260         if(isset($_POST[$attrs])){
261           $this->$attrs = $_POST[$attrs];
262         }
263       }
264     }
265   }
268   /* Check supplied data */
269   function check()
270   {
271     /* Call common method to give check the hook */
272     $message= plugin::check();
274     /* If this is a new script, check if a script with this name already exists */
275     if(!empty($this->release) && $this->copy_paste_mode){
277       /* Check if current name is already used for fai scripts in selected release */
278       $dn = 'cn='.$this->cn.",ou=variables,".$this->release;
279       $ldap = $this->config->get_ldap_link();
280       $ldap->cat($dn);
281       if($ldap->count()){
283         $r =convert_department_dn($this->release);;
284         $message[] = sprintf(_("Can't insert a fai variable named '%s' in '%s' there is already a variable with the given name."),$this->cn,$r);
285       }
286     }
287     return ($message);
288   }
291   /* Save to LDAP */
292   function save()
293   {
294     plugin::save();
295  
296     $ldap = $this->config->get_ldap_link();
297  
298     /* Copy & Paste : Ensure that FAIstate is copied too */
299     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
300       $this->attrs['FAIstate'] = $this->FAIstate;
301     }
302  
303     $ldap->cat($this->dn,array("objectClass"));
304     if($ldap->count()!=0){
305       /* Write FAIscript to ldap*/
306       $ldap->cd($this->dn);
307       $this->cleanup();
308       $ldap->modify ($this->attrs); 
310     }else{
311       /* Write FAIscript to ldap*/
312       $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
313       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
314       $ldap->cd($this->dn);
315       $ldap->add($this->attrs);
316     }
317     show_ldap_error($ldap->get_error(), _("Saving FAI variable base failed"));
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($this->copy_paste_mode){
358         $obj['status'] = "new";
359       }
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         $ldap->cd($sub_dn);
381         $ldap->rmdir_recursive($sub_dn);
382         $this->handle_post_events("remove");
383         show_ldap_error($ldap->get_error(), _("Removing FAI variable failed")); 
384       }elseif($obj['status'] == "edited"){
385         $ldap->cd($sub_dn);
386         $this->cleanup();
387         $ldap->modify ($tmp); 
389         $this->handle_post_events("modify");
390         show_ldap_error($ldap->get_error(), _("Saving FAI variable failed")); 
391       }elseif($obj['status']=="new"){
393         if($tmp['description'] == array()){
394           unset($tmp['description']);
395         }
397         $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
398         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
399         $ldap->cd($sub_dn);
400         $ldap->add($tmp); 
401         $this->handle_post_events("add");
402         show_ldap_error($ldap->get_error(), _("Saving FAI variable failed")); 
403       }
405       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
406     }
407   }
408   
409   /* return copy & paste dialog
410    */
411   function getCopyDialog()
412   {
413     /* Ask for cn */
414     $smarty = get_smarty();
415     $smarty->assign("cn" ,$this->cn);
416     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
417     $ret = array();
418     $ret['string'] = $str;
419     $ret['status'] = "";
420     return($ret);
421   }
423   /* Get posted cn */
424   function saveCopyDialog()
425   {
426     if(isset($_POST['cn'])){
427       $this->cn = $_POST['cn'];
428     }
429   }
432 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
433 ?>