Code

Fixed debconf loading
[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","FAIdebianMirror");
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 $FAIdebianMirror           = ""; // selected mirror
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(!in_array($attr['FAIvariableType'],array("multiselect"))){
90           $tmp['Value'] = $attr['FAIvariableContent'][0];
91         }else{
92           $content = array();        
93           unset($attr['FAIvariableContent']['count']);
94           foreach($attr['FAIvariableContent'] as $attr){
95             $tmp['Value'][] = $attr;
96           }
97         }
98         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
99       }
100     }
102     if (isset($this->attrs['FAIdebianSection']['count'])){
103       unset($this->attrs['FAIdebianSection']['count']);
104     }
105     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
106       $this->FAIdebianSection = array();
107       foreach($this->attrs['FAIdebianSection'] as $sec){
108         $this->FAIdebianSection[$sec]=$sec;
109       }
110     }
112     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
113       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
114     }
115     $this->confDir = CONFIG_DIR."/fai/";
116     $this->FAIpackage = array();
118     /* Extract mirror location */
119     $this->mirror= preg_replace("!^[^/]+//([^/]+).*$!", '$1', $this->FAIdebianMirror);
120   }
122   function execute()
123   {
124         /* Call parent execute */
126         plugin::execute();
128     /* Fill templating stuff */
129     $smarty= get_smarty();
130     $display= "";
132     $this->genMirror();
134     /* Check if we exist already - no need to ask for revisions, etc. then */
135     if ($this->dn != "new"){
136         $this->newDialogShown= true;
137     }
139     if((!$this->is_account)&&(!$this->newDialogShown)){
140       
141       if($this->dialog==NULL){
142         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
143         $this->is_dialog =true;
144       }
146       /* alert possible missconfigurations */ 
147       if((count($this->releases)==0)||(count($this->servers)==0)||(count($this->sections)==0)){
148         print_red(_("There is no useable package list defined."));
149       }
151       /* Assign posible changes, for mirror combinations */
152       $this->dialog->save_object();
153       $this->dialog->releases = $this->releases;
154       $this->dialog->servers  = $this->servers;
155       $this->dialog->sections = $this->sections;
156       $this->dialog->mirrors  = $this->mirrors;
158       /* Assign Repository settings*/ 
159       if(isset($_POST['SaveObjectNew'])){
160         $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
161         $this->FAIdebianSection = $this->dialog->FAIdebianSection;
162         $this->FAIdebianMirror  = $this->dialog->FAIdebianMirror;
163         unset($this->dialog);
164         $this->dialog = false;
165         $this->is_dialog=false;
166         $this->newDialogShown = true;
167       }
169       /* Draw dialog */
170       if($this->dialog){
171         $display=$this->dialog->execute();
172         return($display); 
173       }
174     }
176     /* Assign variables */
177     foreach($this->attributes as $attrs){
178       $smarty->assign($attrs,$this->$attrs);
179     }
180   
181     /* Set mirror if changed */
182     if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
183       $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
184     }
185   
186     /* check servers matching release and section settings */
187     $availableServer = array();
188     foreach($this->mirrors as $mir){
189       if($mir['release'] == $this->FAIdebianRelease){
190         $pass = true;
191         foreach($this->FAIdebianSection as $sec){
192           if($sec != $mir['section']){
193             $pass = false;
194           }
195         if($pass){
196             $availableServer[$mir['mirror']]=$mir['mirror'];
197           }
198         }
199       }
200     }
201  
202     /* Generate package list */
203     $this->list=$this->genPkgs();
205     /* + was pressed to open the package dialog */
206     if(isset($_POST['Addpkg'])){
207       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
208       $this->is_dialog =true;
209     }
211     /* Delte selected package */ 
212     if(isset($_POST['Delpkg'])){
213       unset($this->usedPackages[$_POST['usedPackages']]);
214     }
216     /* Abort package selection dialog */ 
217     if(isset($_POST['CancelSubObject'])){
218       $this->dialog = false;
219       $this->is_dialog=false;
220     }
222     /* attach new packages */
223     if(isset($_POST['SaveSubObject'])) {
224       $this->dialog->save_object();
225       if(count($this->dialog->check())){
226         foreach($this->dialog->check() as $msgs){
227         print_red($msgs);
228         }
229       }else{
230         $use = $this->dialog->save();
231         $this->usedPackages = $use;
232         $this->dialog = false;
233         $this->is_dialog=false;
234         ksort($this->usedPackages);
235       }
236     }
238     /* Configuration dialog open*/
239     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
240       $path = "/etc/gosa/fai/".preg_replace("#^[^/]+//([^/]+).*$#","\\1",$this->FAIdebianMirror)."/".$this->FAIdebianRelease."/debconf.d";
241       $pkg_config = array();
242       if(isset($this->ConfiguredPackages[$_POST['usedPackages']])){
243         $pkg_config = $this->ConfiguredPackages[$_POST['usedPackages']];
244       }
245   
246       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$_POST['usedPackages'], $path, $pkg_config);
247       $this->is_dialog =true;
248     }
249   
250     /* Save Configuration */
251     if(isset($_POST['SaveObjectConfig'])){
252       $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
253       $this->dialog = false;
254       $this->is_dialog=false;
255     }
257     /* cancel configuration */     
258     if(isset($_POST['CancelObjectConfig'])){
259       $this->dialog = false;
260       $this->is_dialog=false;
261     }
263     /* Display dialog */ 
264     if($this->is_dialog){
265       return $this->dialog->execute();
266     }
268     /* Assign section to smarty */
269     $strsec = "";
270     foreach($this->FAIdebianSection as $sec){
271        $strsec .= $sec." ";
272     }
274     $smarty->assign("OptionsACL","");
275     if(empty($this->FAIdebianMirror)){
276       $smarty->assign("OptionsACL"," disabled ");
277     }
279     $smarty->assign("mirrors" ,$availableServer);
280     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
281     $smarty->assign("releases",$this->releases);
282     $smarty->assign("release" ,$this->FAIdebianRelease);
283     $smarty->assign("sections",$this->sections);
284     $smarty->assign("section" ,$strsec);
285     $smarty->assign("usedPackages",$this->printUsedPackages());
286     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
287     return($display);
288   }
290   function genMirror(){
291     $confDir = $this->confDir; 
292     if(!is_readable($confDir)){
293       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
294     }else{
295       
296       /* Try to catch all available mirrors 
297          Possibly check if each server is reachable
298        */
300       $this->servers= array();
301       $this->releases=array();  
302       $this->sections=array(); 
303       $this->mirrors= array();
304       $fd = opendir($confDir);
305       while($mirror = readdir($fd)){
306         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
308           $mirrorDir = $confDir.$mirror."/";
310           $fe = opendir($mirrorDir);
311           while($release = readdir($fe)){
312             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
314               $releaseDir = $mirrorDir.$release."/";
316               $ff = opendir($releaseDir);
317               while($section = readdir($ff)){
318                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
319               
320                   $this->servers[$mirror]=$mirror;
321                   $this->releases[$release]=$release;
322                   $this->sections[$section]=$section;
324                   $arr=array();
325                   $arr['mirror'] = $mirror;
326                   $arr['release']= $release; 
327                   $arr['section']= $section;
328  
329                   $this->mirrors[] = $arr ;
330                 }
331               }
332               fclose($ff);
333             }
334           }
335           fclose($fe);
336         }
337       }
338       fclose($fd);
339     }
342   }
344   /* Delete me, and all my subtrees
345    */
346   function remove_from_parent()
347   {
348     $ldap = $this->config->get_ldap_link();
349     $ldap->cd ($this->dn);
350     $ldap->rmdir_recursive($this->dn);
351     $this->handle_post_events("remove");    
352   }
355   /* Save data to object 
356    */
357   function save_object()
358   {
359     
360     plugin::save_object();
361     foreach($this->attributes as $attrs){
362       if(isset($_POST[$attrs])){
363         $this->$attrs = $_POST[$attrs];
364       }
365     }
366   }
369   /* Check supplied data */
370   function check()
371   {
372     $message= array();
373   
374     if(count($this->usedPackages)==0){
375       $message[]=_("Please select a least one Package.");
376     }
377   
378     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
379       $message[]=_("Please choose a valid combination for your repository setup.");
380     }
382     return ($message);
383   }
385   function printUsedPackages(){
386     $a_ret=array(); 
387     if(is_array($this->usedPackages)) {
388       foreach($this->usedPackages as $usedName){
389       
390         $config = 0;
391    
392         foreach($this->ConfiguredPackages as $name => $value){
393           if($name == $usedName){
394             $config ++;
395           }
396         }
397       
398         $c_str ="";
399         if($config){
400           $c_str = " - "._("package is configured");
401         }
402      
403         if(isset($this->list[$usedName][1])){
404           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
405         }else{
406           $a_ret[$usedName] = $usedName.$c_str;
407         }
408       }
409     }
410     return($a_ret);
411   }
413    function genPkgs(){
414     /* Generate a list off available packages for this mirror, section and release
415      */
416     /* Only read this file if it wasn't read before */
417     if($this->buffer==NULL){
418       $this->buffer=array();
419       $a_ret = array();
420       foreach($this->FAIdebianSection as $sec){
421         $strID= "/etc/gosa/fai/".preg_replace("#^[^/]+//([^/]+).*$#","\\1",$this->FAIdebianMirror)."/".$this->FAIdebianRelease."/".$sec;
422         
423         if(!is_file($strID)){
424           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
425           $this->FAIdebianMirror="";
426           unset($this->buffer);
427           return(array());
428         }
429         $fp = fopen($strID,"r");
431         /* Parse every line and create an array */
432         while(!feof($fp)){
433           $str= fgets($fp,512);
434           $stra= split("\|",$str);
435           if(count($stra)==4){
436             $a_ret[$stra[0]] = $stra;
437           }
438         }
439         fclose($fp);
440           /* Save our Data, to avoid reading it again */
441       }
442       $this->buffer = $a_ret;
443       ksort($a_ret);
444       return($a_ret);
445     }else{
446       return $this->buffer;
447     }
448   }
451   /* Save to LDAP */
452   function save()
453   {
454     plugin::save();
455  
456     $ldap = $this->config->get_ldap_link();
458     $this->attrs['FAIpackage'] = array();
459     foreach($this->usedPackages as $pkg => $obj){
460       $this->attrs['FAIpackage'][] = $pkg;
461     } 
462   
463     $this->attrs['FAIdebianSection'] = array();
464     foreach($this->FAIdebianSection as $sec){
465       $this->attrs['FAIdebianSection'][] = $sec;
466     }
468     $ldap->cat($this->dn);
469     if($ldap->count()!=0){
470       /* Write FAIscript to ldap*/
471       $ldap->cd($this->dn);
472       $ldap->modify($this->attrs);
473     }else{
474       /* Write FAIscript to ldap*/
475       $ldap->cd($this->config->current['BASE']);
476       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
477       $ldap->cd($this->dn);
478       $ldap->add($this->attrs);
479     }
480     show_ldap_error($ldap->get_error());
482       /* Save Package configurations */
483     foreach($this->ConfiguredPackages as $pkgname => $attrs){
484       foreach($attrs as $name => $attr){
486         foreach($attr as $n=>$v){
487           if(empty($v)) $v = array();
488         }
490         /* Set attributes */
491         $pkgattrs['objectClass']          = "FAIdebconfInfo";
492         $pkgattrs['FAIpackage']           = $pkgname;
493         $pkgattrs['FAIvariable']          = $name;
494         $pkgattrs['FAIvariableType']      = $attr['Type'];
495         $pkgattrs['FAIvariableContent']   = $attr['Value'];
496         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
498         /* cehck if object already exists */
499         $ldap->cat($pkgdn);
500         if($ldap->count()!=0){
501           $ldap->cd($pkgdn);
502           $ldap->modify($pkgattrs);
503         }else{
504           $ldap->cd($this->config->current['BASE']);
505           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
506           $ldap->cd($pkgdn);
507           $ldap->add($pkgattrs);
508         }
509         show_ldap_error($ldap->get_error());
510       }
511     }
514     
516   }
519 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
520 ?>