Code

Fixed some typos
[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   function faiPackage ($config, $dn= NULL)
58   {
59     /* Load Attributes */
60     plugin::plugin ($config, $dn);
62     /* If "dn==new" we try to create a new entry
63      * Else we must read all objects from ldap which belong to this entry.
64      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
65      */
66     if($dn != "new"){
67       $this->dn =$dn;
68     }
70     if(isset($this->attrs['FAIpackage'])){
71       unset($this->attrs['FAIpackage']['count']);
72       foreach($this->attrs['FAIpackage'] as $pkg){
73         $this->usedPackages[$pkg] = $pkg;
74       }
75       ksort($this->usedPackages);
76     }else{
77       $this->usedPackages = array();
78     }  
79   
80     $ldap = $this->config->get_ldap_link();
81     foreach($this->usedPackages as $name){
82       $ldap->search("(&(objectClass=FAIdebconfInfo)(FAIpackage=".$name."))");
83       while($attr = $ldap->fetch()){
84      
85         $tmp =array(); 
86         $tmp['Name']  = $attr['FAIvariable'][0];
87         $tmp['Type']  = $attr['FAIvariableType'][0];
89         if (isset($attr['FAIvariableContent'][0])){
90           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
91             $tmp['Value'] = $attr['FAIvariableContent'][0];
92           }else{
93             $content = array();        
94             unset($attr['FAIvariableContent']['count']);
95             foreach($attr['FAIvariableContent'] as $attr){
96               $tmp['Value'][] = $attr;
97             }
98           }
99           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
100         }
101       }
102     }
104     if (isset($this->attrs['FAIdebianSection']['count'])){
105       unset($this->attrs['FAIdebianSection']['count']);
106     }
107     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
108       $this->FAIdebianSection = array();
109       foreach($this->attrs['FAIdebianSection'] as $sec){
110         $this->FAIdebianSection[$sec]=$sec;
111       }
112     }
114     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
115       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
116     }
117     $this->confDir = CONFIG_DIR."/fai/";
118     $this->FAIpackage = array();
119   }
121   function execute()
122   {
123         /* Call parent execute */
125         plugin::execute();
127     /* Fill templating stuff */
128     $smarty= get_smarty();
129     $display= "";
131     /* Check if we exist already - no need to ask for revisions, etc. then */
132     if ($this->dn != "new"){
133         $this->newDialogShown= true;
134     }
136     if((!$this->is_account)&&(!$this->newDialogShown)){
137       
138       if($this->dialog==NULL){
139         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
140         $this->is_dialog =true;
141       }
143       /* Assign posible changes, for mirror combinations */
144       $this->dialog->save_object();
146       /* Assign Repository settings*/ 
147       if(isset($_POST['SaveObjectNew'])){
148         $obj = $this->dialog->save();
149       
150         $this->FAIdebianSection = $obj['FAIdebianSection'];
151         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
152     
153         unset($this->dialog);
154         $this->dialog = false;
155         $this->is_dialog=false;
156         $this->newDialogShown = true;
157       }
159       /* Draw dialog */
160       if($this->dialog){
161         $display=$this->dialog->execute();
162         return($display); 
163       }
164     }
166     /* Assign variables */
167     foreach($this->attributes as $attrs){
168       $smarty->assign($attrs,$this->$attrs);
169     }
170   
171     /* Generate package list */
172     $this->list=$this->genPkgs();
174     /* + was pressed to open the package dialog */
175     if(isset($_POST['Addpkg'])){
176       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
177       $this->is_dialog =true;
178     }
180     /* Delte selected package */ 
181     if(isset($_POST['Delpkg'])){
182       unset($this->usedPackages[$_POST['usedPackages']]);
183     }
185     /* Abort package selection dialog */ 
186     if(isset($_POST['CancelSubObject'])){
187       $this->dialog = false;
188       $this->is_dialog=false;
189     }
191     /* attach new packages */
192     if(isset($_POST['SaveSubObject'])) {
193       $this->dialog->save_object();
194       if(count($this->dialog->check())){
195         foreach($this->dialog->check() as $msgs){
196         print_red($msgs);
197         }
198       }else{
199         $use = $this->dialog->save();
200         $this->usedPackages = $use;
201         $this->dialog = false;
202         $this->is_dialog=false;
203         ksort($this->usedPackages);
204       }
205     }
207     /* Configuration dialog open*/
208     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
209       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
210       $pkg_config = array();
211       if(isset($this->ConfiguredPackages[$_POST['usedPackages']])){
212         $pkg_config = $this->ConfiguredPackages[$_POST['usedPackages']];
213       }
214   
215       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$_POST['usedPackages'], $path, $pkg_config);
216       $this->is_dialog =true;
217     }
218   
219     /* Save Configuration */
220     if(isset($_POST['SaveObjectConfig'])){
221       $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
222       $this->dialog = false;
223       $this->is_dialog=false;
224     }
226     /* cancel configuration */     
227     if(isset($_POST['CancelObjectConfig'])){
228       $this->dialog = false;
229       $this->is_dialog=false;
230     }
232     /* Display dialog */ 
233     if($this->is_dialog){
234       return $this->dialog->execute();
235     }
237     /* Assign section to smarty */
238     $strsec = "";
239     foreach($this->FAIdebianSection as $sec){
240        $strsec .= $sec." ";
241     }
243     $smarty->assign("OptionsACL","");
245     $smarty->assign("releases",$this->releases);
246     $smarty->assign("release" ,$this->FAIdebianRelease);
247     $smarty->assign("sections",$this->sections);
248     $smarty->assign("section" ,$strsec);
249     $smarty->assign("usedPackages",$this->printUsedPackages());
250     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
251     return($display);
252   }
254   /* Delete me, and all my subtrees
255    */
256   function remove_from_parent()
257   {
258     $ldap = $this->config->get_ldap_link();
259     $ldap->cd ($this->dn);
260     $ldap->rmdir_recursive($this->dn);
261     $this->handle_post_events("remove");    
262   }
265   /* Save data to object 
266    */
267   function save_object()
268   {
269     
270     plugin::save_object();
271     foreach($this->attributes as $attrs){
272       if(isset($_POST[$attrs])){
273         $this->$attrs = $_POST[$attrs];
274       }
275     }
276   }
279   /* Check supplied data */
280   function check()
281   {
282     $message= array();
283   
284     if(count($this->usedPackages)==0){
285       $message[]=_("Please select a least one Package.");
286     }
287   
288     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
289       $message[]=_("Please choose a valid combination for your repository setup.");
290     }
292     return ($message);
293   }
295   function printUsedPackages(){
296     $a_ret=array(); 
297     if(is_array($this->usedPackages)) {
298       foreach($this->usedPackages as $usedName){
299       
300         $config = 0;
301    
302         foreach($this->ConfiguredPackages as $name => $value){
303           if($name == $usedName){
304             $config ++;
305           }
306         }
307       
308         $c_str ="";
309         if($config){
310           $c_str = " - "._("package is configured");
311         }
312      
313         if(isset($this->list[$usedName][1])){
314           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
315         }else{
316           $a_ret[$usedName] = $usedName.$c_str;
317         }
318       }
319     }
320     return($a_ret);
321   }
323    function genPkgs(){
324     /* Generate a list off available packages for this mirror, section and release
325      */
326     /* Only read this file if it wasn't read before */
327     if($this->buffer==NULL){
328       $this->buffer=array();
329       $a_ret = array();
330       foreach($this->FAIdebianSection as $sec){
331         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
332         
333         if(!is_file($strID)){
334           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
335           unset($this->buffer);
336           return(array());
337         }
338         $fp = fopen($strID,"r");
340         /* Parse every line and create an array */
341         while(!feof($fp)){
342           $str= fgets($fp,512);
343           $stra= split("\|",$str);
344           if(count($stra)==4){
345             $a_ret[$stra[0]] = $stra;
346           }
347         }
348         fclose($fp);
349           /* Save our Data, to avoid reading it again */
350       }
351       $this->buffer = $a_ret;
352       ksort($a_ret);
353       return($a_ret);
354     }else{
355       return $this->buffer;
356     }
357   }
360   /* Save to LDAP */
361   function save()
362   {
363     plugin::save();
364  
365     $ldap = $this->config->get_ldap_link();
367     $this->attrs['FAIpackage'] = array();
368     foreach($this->usedPackages as $pkg => $obj){
369       $this->attrs['FAIpackage'][] = $pkg;
370     } 
371   
372     $this->attrs['FAIdebianSection'] = array();
373     foreach($this->FAIdebianSection as $sec){
374       $this->attrs['FAIdebianSection'][] = $sec;
375     }
377     $this->attrs["FAIinstallMethod"]= "aptitude";
379     $ldap->cat($this->dn);
380     if($ldap->count()!=0){
381       /* Write FAIscript to ldap*/
382       $ldap->cd($this->dn);
383       $ldap->modify($this->attrs);
384     }else{
385       /* Write FAIscript to ldap*/
386       $ldap->cd($this->config->current['BASE']);
387       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
388       $ldap->cd($this->dn);
389       $ldap->add($this->attrs);
390     }
391     show_ldap_error($ldap->get_error());
393       /* Save Package configurations */
394     foreach($this->ConfiguredPackages as $pkgname => $attrs){
395       foreach($attrs as $name => $attr){
397         foreach($attr as $n=>$v){
398           if(empty($v)) $v = array();
399         }
401         /* Set attributes */
402         $pkgattrs['objectClass']          = "FAIdebconfInfo";
403         $pkgattrs['FAIpackage']           = $pkgname;
404         $pkgattrs['FAIvariable']          = $name;
405         $pkgattrs['FAIvariableType']      = $attr['Type'];
406         $pkgattrs['FAIvariableContent']   = $attr['Value'];
407         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
409         /* cehck if object already exists */
410         $ldap->cat($pkgdn);
411         if($ldap->count()!=0){
412           $ldap->cd($pkgdn);
413           $ldap->modify($pkgattrs);
414         }else{
415           $ldap->cd($this->config->current['BASE']);
416           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
417           $ldap->cd($pkgdn);
418           $ldap->add($pkgattrs);
419         }
420         show_ldap_error($ldap->get_error());
421       }
422     }
425     
427   }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>