Code

Fixed FAIclass selection
[gosa.git] / plugins / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage 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","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description"); 
28   var $sub64coded       = array();
30   var $ConfiguredPackages = array();
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIdebianRelease          = ""; // The selected release
40   var $FAIdebianSection          = ""; // selected section
41   var $FAIinstallMethod          = "aptitude"; // hard coded
42   var $mirror                    = ""; // selected mirror
44   var $servers          = array();  // All available servers
45   var $releases         = array();  // All possible releases 
46   var $sections         = array();  // All section types
48   var $list             = NULL;
50   var $mirrors          = array();  // The combination of server/release/section
51   var $confDir          = "";
52   var $usedPackages     = array();
53   var $buffer           = NULL; 
54   var $strID            ="";
55   var $newDialogShown   =false;
57   var $FAIstate         = "";
59   function faiPackage ($config, $dn= NULL)
60   {
61     /* Load Attributes */
62     plugin::plugin ($config, $dn);
64     $this->acl ="#all#";
66     /* If "dn==new" we try to create a new entry
67      * Else we must read all objects from ldap which belong to this entry.
68      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
69      */
70     if($dn != "new"){
71       $this->dn =$dn;
73       /* Set acls
74        */
75       $ui   = get_userinfo();
76       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
77       $acli = get_module_permission($acl, "FAIclass", $this->dn);
78       $this->acl=$acli;
80       /* Get FAIstate
81        */
82       if(isset($this->attrs['FAIstate'][0])){
83         $this->FAIstate = $this->attrs['FAIstate'][0];
84       }
85     }
87     if(isset($this->attrs['FAIpackage'])){
88       unset($this->attrs['FAIpackage']['count']);
89       foreach($this->attrs['FAIpackage'] as $pkg){
90         $this->usedPackages[$pkg] = $pkg;
91       }
92       ksort($this->usedPackages);
93     }else{
94       $this->usedPackages = array();
95     }  
96   
97     $ldap = $this->config->get_ldap_link();
98     foreach($this->usedPackages as $name){
99       $ldap->search("(&(objectClass=FAIdebconfInfo)(FAIpackage=".$name."))",array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection",""));
100       while($attr = $ldap->fetch()){
101      
102         $tmp =array(); 
103         $tmp['Name']  = $attr['FAIvariable'][0];
104         $tmp['Type']  = $attr['FAIvariableType'][0];
106         if (isset($attr['FAIvariableContent'][0])){
107           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
108             $tmp['Value'] = $attr['FAIvariableContent'][0];
109           }else{
110             $content = array();        
111             unset($attr['FAIvariableContent']['count']);
112             foreach($attr['FAIvariableContent'] as $attr){
113               $tmp['Value'][] = $attr;
114             }
115           }
116           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
117         }
118       }
119     }
121     if (isset($this->attrs['FAIdebianSection']['count'])){
122       unset($this->attrs['FAIdebianSection']['count']);
123     }
124     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
125       $this->FAIdebianSection = array();
126       foreach($this->attrs['FAIdebianSection'] as $sec){
127         $this->FAIdebianSection[$sec]=$sec;
128       }
129     }
131     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
132       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
133     }
134     $this->confDir = CONFIG_DIR."/fai/";
135     $this->FAIpackage = array();
136   }
138   function execute()
139   {
140         /* Call parent execute */
142         plugin::execute();
144     /* Fill templating stuff */
145     $smarty= get_smarty();
146     $display= "";
148     /* Check if we exist already - no need to ask for revisions, etc. then */
149     if ($this->dn != "new"){
150         $this->newDialogShown= true;
151     }
153     if((!$this->is_account)&&(!$this->newDialogShown)){
154       
155       if($this->dialog==NULL){
156         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
157         $this->is_dialog =true;
158       }
160       /* Assign posible changes, for mirror combinations */
161       $this->dialog->save_object();
163       /* Assign Repository settings*/ 
164       if(isset($_POST['SaveObjectNew'])){
165         $obj = $this->dialog->save();
166       
167         $this->FAIdebianSection = $obj['FAIdebianSection'];
168         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
169     
170         unset($this->dialog);
171         $this->dialog = false;
172         $this->is_dialog=false;
173         $this->newDialogShown = true;
174       }
176       /* Draw dialog */
177       if($this->dialog){
178         $display=$this->dialog->execute();
179         return($display); 
180       }
181     }
183     /* Assign variables */
184     foreach($this->attributes as $attrs){
185       $smarty->assign($attrs,$this->$attrs);
186     }
187   
188     /* Generate package list */
189     $this->list=$this->genPkgs();
191     /* + was pressed to open the package dialog */
192     if(isset($_POST['Addpkg'])){
193       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
194       $this->is_dialog =true;
195     }
197     /* Delte selected package */ 
198     if(isset($_POST['Delpkg'])){
199       if($this->FAIstate != "freeze"){
200         foreach($_POST['usedPackages'] as $del){
201           if(isset($this->usedPackages[$del])){
202             unset($this->usedPackages[$del]);
203           }
204         }
205       }
206     }
208     /* Abort package selection dialog */ 
209     if(isset($_POST['CancelSubObject'])){
210       $this->dialog = false;
211       $this->is_dialog=false;
212     }
214     /* attach new packages */
215     if(isset($_POST['SaveSubObject'])) {
216       if($this->FAIstate != "freeze"){
217         $this->dialog->save_object();
218         if(count($this->dialog->check())){
219           foreach($this->dialog->check() as $msgs){
220             print_red($msgs);
221           }
222         }else{
223           $use = $this->dialog->save();
224           $this->usedPackages = $use;
225           $this->dialog = false;
226           $this->is_dialog=false;
227           ksort($this->usedPackages);
228         }
229       }else{
230         $this->dialog = false;
231         $this->is_dialog=false;
232       }
233     }
235     /* Configuration dialog open*/
236     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
237       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
238       $pkg_config = array();
239       $pkg = $_POST['usedPackages'][0];
240       
241       if(isset($this->ConfiguredPackages[$pkg])){
242         $pkg_config = $this->ConfiguredPackages[$pkg];
243       }
244   
245       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
246       $this->dialog ->acl = $this->acl;
247       $this->is_dialog =true;
248     }
249   
250     /* Save Configuration */
251     if(isset($_POST['SaveObjectConfig'])){
252       if($this->FAIstate != "freeze"){
253         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
254       }
255       $this->dialog = false;
256       $this->is_dialog=false;
257     }
259     /* cancel configuration */     
260     if(isset($_POST['CancelObjectConfig'])){
261       $this->dialog = false;
262       $this->is_dialog=false;
263     }
265     /* Display dialog */ 
266     if($this->is_dialog){
267       return $this->dialog->execute();
268     }
270     /* Assign section to smarty */
271     $strsec = "";
272     foreach($this->FAIdebianSection as $sec){
273        $strsec .= $sec." ";
274     }
276     foreach($this->attributes as $attr){
277       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
278     }
280     $smarty->assign("OptionsACL","");
282     $smarty->assign("releases",$this->releases);
283     $smarty->assign("release" ,$this->FAIdebianRelease);
284     $smarty->assign("sections",$this->sections);
285     $smarty->assign("section" ,$strsec);
286     $smarty->assign("usedPackages",$this->printUsedPackages());
287     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
288     return($display);
289   }
291   /* Delete me, and all my subtrees
292    */
293   function remove_from_parent()
294   {
295     $ldap = $this->config->get_ldap_link();
296     $ldap->cd ($this->dn);
297     $ldap->rmdir_recursive($this->dn);
298     $this->handle_post_events("remove");    
299   }
302   /* Save data to object 
303    */
304   function save_object()
305   {
306   
307     if($this->FAIstate == "freeze") return;  
308     plugin::save_object();
309   
311     foreach($this->attributes as $attrs){
312       if(isset($_POST[$attrs])){
313         $this->$attrs = $_POST[$attrs];
314       }
315     }
316   }
319   /* Check supplied data */
320   function check()
321   {
322     $message= array();
323   
324     if(count($this->usedPackages)==0){
325       $message[]=_("Please select a least one Package.");
326     }
327   
328     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
329       $message[]=_("Please choose a valid combination for your repository setup.");
330     }
332     return ($message);
333   }
335   function printUsedPackages(){
336     $a_ret=array(); 
337     if(is_array($this->usedPackages)) {
338       foreach($this->usedPackages as $usedName){
339       
340         $config = 0;
341    
342         foreach($this->ConfiguredPackages as $name => $value){
343           if($name == $usedName){
344             $config ++;
345           }
346         }
347       
348         $c_str ="";
349         if($config){
350           $c_str = " - "._("package is configured");
351         }
352      
353         if(isset($this->list[$usedName][1])){
354           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
355         }else{
356           $a_ret[$usedName] = $usedName.$c_str;
357         }
358       }
359     }
360     return($a_ret);
361   }
363    function genPkgs(){
364     /* Generate a list off available packages for this mirror, section and release
365      */
366     /* Only read this file if it wasn't read before */
367     if($this->buffer==NULL){
368       $this->buffer=array();
369       $a_ret = array();
370       foreach($this->FAIdebianSection as $sec){
371         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
372         
373         if(!is_file($strID)){
374           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
375           unset($this->buffer);
376           return(array());
377         }
378         $fp = fopen($strID,"r");
380         /* Parse every line and create an array */
381         while(!feof($fp)){
382           $str= fgets($fp,512);
383           $stra= split("\|",$str);
384           if(count($stra)==4){
385             $a_ret[$stra[0]] = $stra;
386           }
387         }
388         fclose($fp);
389           /* Save our Data, to avoid reading it again */
390       }
391       $this->buffer = $a_ret;
392       ksort($a_ret);
393       return($a_ret);
394     }else{
395       return $this->buffer;
396     }
397   }
400   /* Save to LDAP */
401   function save()
402   {
403     plugin::save();
404  
405     $ldap = $this->config->get_ldap_link();
407     $this->attrs['FAIpackage'] = array();
408     foreach($this->usedPackages as $pkg => $obj){
409       $this->attrs['FAIpackage'][] = $pkg;
410     } 
411   
412     $this->attrs['FAIdebianSection'] = array();
413     foreach($this->FAIdebianSection as $sec){
414       $this->attrs['FAIdebianSection'][] = $sec;
415     }
417     $this->attrs["FAIinstallMethod"]= "aptitude";
419     $ldap->cat($this->dn);
420     if($ldap->count()!=0){
421       /* Write FAIscript to ldap*/
422       $ldap->cd($this->dn);
423       $this->cleanup();
424 $ldap->modify ($this->attrs); 
426     }else{
427       /* Write FAIscript to ldap*/
428       $ldap->cd($this->config->current['BASE']);
429       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
430       $ldap->cd($this->dn);
431       $ldap->add($this->attrs);
432     }
433     show_ldap_error($ldap->get_error());
435       /* Save Package configurations */
436     foreach($this->ConfiguredPackages as $pkgname => $attrs){
437       foreach($attrs as $name => $attr){
439         foreach($attr as $n=>$v){
440           if(empty($v)) $v = array();
441         }
443         /* Set attributes */
444         $pkgattrs['objectClass']          = "FAIdebconfInfo";
445         $pkgattrs['FAIpackage']           = $pkgname;
446         $pkgattrs['FAIvariable']          = $name;
447         $pkgattrs['FAIvariableType']      = $attr['Type'];
448         $pkgattrs['FAIvariableContent']   = $attr['Value'];
449         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
451         /* cehck if object already exists */
452         $ldap->cat($pkgdn);
453         if($ldap->count()!=0){
454           $ldap->cd($pkgdn);
455           $this->cleanup();
456 $ldap->modify ($pkgattrs); 
458         }else{
459           $ldap->cd($this->config->current['BASE']);
460           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
461           $ldap->cd($pkgdn);
462           $ldap->add($pkgattrs);
463         }
464         show_ldap_error($ldap->get_error());
465       }
466     }
469     
471   }
474 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
475 ?>