Code

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