Code

Added \$this->cleanup before all calls of $ldap->modify
[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."))",array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection",""));
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       foreach($_POST['usedPackages'] as $del){
183         if(isset($this->usedPackages[$del])){
184           unset($this->usedPackages[$del]);
185         }
186       }
187     }
189     /* Abort package selection dialog */ 
190     if(isset($_POST['CancelSubObject'])){
191       $this->dialog = false;
192       $this->is_dialog=false;
193     }
195     /* attach new packages */
196     if(isset($_POST['SaveSubObject'])) {
197       $this->dialog->save_object();
198       if(count($this->dialog->check())){
199         foreach($this->dialog->check() as $msgs){
200         print_red($msgs);
201         }
202       }else{
203         $use = $this->dialog->save();
204         $this->usedPackages = $use;
205         $this->dialog = false;
206         $this->is_dialog=false;
207         ksort($this->usedPackages);
208       }
209     }
211     /* Configuration dialog open*/
212     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
213       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
214       $pkg_config = array();
215       $pkg = $_POST['usedPackages'][0];
216       
217       if(isset($this->ConfiguredPackages[$pkg])){
218         $pkg_config = $this->ConfiguredPackages[$pkg];
219       }
220   
221       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
222       $this->is_dialog =true;
223     }
224   
225     /* Save Configuration */
226     if(isset($_POST['SaveObjectConfig'])){
227       $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
228       $this->dialog = false;
229       $this->is_dialog=false;
230     }
232     /* cancel configuration */     
233     if(isset($_POST['CancelObjectConfig'])){
234       $this->dialog = false;
235       $this->is_dialog=false;
236     }
238     /* Display dialog */ 
239     if($this->is_dialog){
240       return $this->dialog->execute();
241     }
243     /* Assign section to smarty */
244     $strsec = "";
245     foreach($this->FAIdebianSection as $sec){
246        $strsec .= $sec." ";
247     }
249     $smarty->assign("OptionsACL","");
251     $smarty->assign("releases",$this->releases);
252     $smarty->assign("release" ,$this->FAIdebianRelease);
253     $smarty->assign("sections",$this->sections);
254     $smarty->assign("section" ,$strsec);
255     $smarty->assign("usedPackages",$this->printUsedPackages());
256     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
257     return($display);
258   }
260   /* Delete me, and all my subtrees
261    */
262   function remove_from_parent()
263   {
264     $ldap = $this->config->get_ldap_link();
265     $ldap->cd ($this->dn);
266     $ldap->rmdir_recursive($this->dn);
267     $this->handle_post_events("remove");    
268   }
271   /* Save data to object 
272    */
273   function save_object()
274   {
275     
276     plugin::save_object();
277     foreach($this->attributes as $attrs){
278       if(isset($_POST[$attrs])){
279         $this->$attrs = $_POST[$attrs];
280       }
281     }
282   }
285   /* Check supplied data */
286   function check()
287   {
288     $message= array();
289   
290     if(count($this->usedPackages)==0){
291       $message[]=_("Please select a least one Package.");
292     }
293   
294     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
295       $message[]=_("Please choose a valid combination for your repository setup.");
296     }
298     return ($message);
299   }
301   function printUsedPackages(){
302     $a_ret=array(); 
303     if(is_array($this->usedPackages)) {
304       foreach($this->usedPackages as $usedName){
305       
306         $config = 0;
307    
308         foreach($this->ConfiguredPackages as $name => $value){
309           if($name == $usedName){
310             $config ++;
311           }
312         }
313       
314         $c_str ="";
315         if($config){
316           $c_str = " - "._("package is configured");
317         }
318      
319         if(isset($this->list[$usedName][1])){
320           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
321         }else{
322           $a_ret[$usedName] = $usedName.$c_str;
323         }
324       }
325     }
326     return($a_ret);
327   }
329    function genPkgs(){
330     /* Generate a list off available packages for this mirror, section and release
331      */
332     /* Only read this file if it wasn't read before */
333     if($this->buffer==NULL){
334       $this->buffer=array();
335       $a_ret = array();
336       foreach($this->FAIdebianSection as $sec){
337         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
338         
339         if(!is_file($strID)){
340           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
341           unset($this->buffer);
342           return(array());
343         }
344         $fp = fopen($strID,"r");
346         /* Parse every line and create an array */
347         while(!feof($fp)){
348           $str= fgets($fp,512);
349           $stra= split("\|",$str);
350           if(count($stra)==4){
351             $a_ret[$stra[0]] = $stra;
352           }
353         }
354         fclose($fp);
355           /* Save our Data, to avoid reading it again */
356       }
357       $this->buffer = $a_ret;
358       ksort($a_ret);
359       return($a_ret);
360     }else{
361       return $this->buffer;
362     }
363   }
366   /* Save to LDAP */
367   function save()
368   {
369     plugin::save();
370  
371     $ldap = $this->config->get_ldap_link();
373     $this->attrs['FAIpackage'] = array();
374     foreach($this->usedPackages as $pkg => $obj){
375       $this->attrs['FAIpackage'][] = $pkg;
376     } 
377   
378     $this->attrs['FAIdebianSection'] = array();
379     foreach($this->FAIdebianSection as $sec){
380       $this->attrs['FAIdebianSection'][] = $sec;
381     }
383     $this->attrs["FAIinstallMethod"]= "aptitude";
385     $ldap->cat($this->dn);
386     if($ldap->count()!=0){
387       /* Write FAIscript to ldap*/
388       $ldap->cd($this->dn);
389       $this->cleanup();
390 $ldap->modify ($this->attrs); 
392     }else{
393       /* Write FAIscript to ldap*/
394       $ldap->cd($this->config->current['BASE']);
395       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
396       $ldap->cd($this->dn);
397       $ldap->add($this->attrs);
398     }
399     show_ldap_error($ldap->get_error());
401       /* Save Package configurations */
402     foreach($this->ConfiguredPackages as $pkgname => $attrs){
403       foreach($attrs as $name => $attr){
405         foreach($attr as $n=>$v){
406           if(empty($v)) $v = array();
407         }
409         /* Set attributes */
410         $pkgattrs['objectClass']          = "FAIdebconfInfo";
411         $pkgattrs['FAIpackage']           = $pkgname;
412         $pkgattrs['FAIvariable']          = $name;
413         $pkgattrs['FAIvariableType']      = $attr['Type'];
414         $pkgattrs['FAIvariableContent']   = $attr['Value'];
415         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
417         /* cehck if object already exists */
418         $ldap->cat($pkgdn);
419         if($ldap->count()!=0){
420           $ldap->cd($pkgdn);
421           $this->cleanup();
422 $ldap->modify ($pkgattrs); 
424         }else{
425           $ldap->cd($this->config->current['BASE']);
426           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
427           $ldap->cd($pkgdn);
428           $ldap->add($pkgattrs);
429         }
430         show_ldap_error($ldap->get_error());
431       }
432     }
435     
437   }
440 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
441 ?>