Code

* Cosmetic changes
[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   /* Specific attributes */
31   var $cn               = "";       // The class name for this object
32   var $description      = "";       // The description for this set of partitions
33   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
34   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
35   var $SubObjects       = array();  // All leafobjects of this object
37   var $FAIdebianRelease          = ""; // The selected release
38   var $FAIdebianSection          = ""; // selected section
39   var $FAIdebianMirror           = ""; // selected mirror
41   var $servers          = array();  // All available servers
42   var $releases         = array();  // All possible releases 
43   var $sections         = array();  // All section types
45   var $list             = NULL;
47   var $mirrors          = array();  // The combination of server/release/section
48   var $confDir          = "";
49   var $usedPackages     = array();
50   var $buffer           = NULL; 
51   var $strID            ="";
52   var $newDialogShown   =false;
54   function faiPackage ($config, $dn= NULL)
55   {
56     /* Load Attributes */
57     plugin::plugin ($config, $dn);
59     /* If "dn==new" we try to create a new entry
60      * Else we must read all objects from ldap which belong to this entry.
61      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
62      */
63     if($dn != "new"){
64       $this->dn =$dn;
65     }
67     if(isset($this->attrs['FAIpackage'])){
68       unset($this->attrs['FAIpackage']['count']);
69       foreach($this->attrs['FAIpackage'] as $pkg){
70         $this->usedPackages[$pkg] = $pkg;
71       }
72     }else{
73       $this->usedPackages = array();
74     }  
76     unset($this->attrs['FAIdebianSection']['count']);
77     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
78       $this->FAIdebianSection = array();
79       foreach($this->attrs['FAIdebianSection'] as $sec){
80         $this->FAIdebianSection[$sec]=$sec;
81       }
82     }
84     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
85       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
86     }
87     $this->confDir = CONFIG_DIR."/fai/";
88     $this->FAIpackage = array();
89   }
91   function execute()
92   {
93     /* Fill templating stuff */
94     $smarty= get_smarty();
95     $display= "";
97     $this->genMirror();
99     if((!$this->is_account)&&(!$this->newDialogShown)){
100       
101       if($this->dialog==NULL){
102         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
103         $this->is_dialog =true;
104       }
106       /* alert possible missconfigurations */ 
107       if((count($this->releases)==0)||(count($this->servers)==0)||(count($this->sections)==0)){
108         print_red(_("There is no useable package list defined."));
109       }
111       /* Assign posible changes, for mirror combinations */
112       $this->dialog->releases = $this->releases;
113       $this->dialog->servers  = $this->servers;
114       $this->dialog->sections = $this->sections;
115       $this->dialog->mirrors  = $this->mirrors;
117       /* Assign Repository settings*/ 
118       if(isset($_POST['SaveObjectNew'])){
119         $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
120         $this->FAIdebianSection = $this->dialog->FAIdebianSection;
121         $this->FAIdebianMirror  = $this->dialog->FAIdebianMirror;
122         unset($this->dialog);
123         $this->dialog = false;
124         $this->is_dialog=false;
125         $this->newDialogShown = true;
126       }
128       /* Draw dialog */
129       if($this->dialog){
130         $display=$this->dialog->execute();
131         return($display); 
132       }
133     }
135     /* Assign variables */
136     foreach($this->attributes as $attrs){
137       $smarty->assign($attrs,$this->$attrs);
138     }
140     /* Set mirror if changed */
141     if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
142       $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
143     }
145     /* check servers matching release and section settings */
146     $availableServer = array();
147     foreach($this->mirrors as $mir){
148       if($mir['release'] == $this->FAIdebianRelease){
149         $pass = true;
150         foreach($this->FAIdebianSection as $sec){
151           if($sec != $mir['section']){
152             $pass = false;
153           }
154         if($pass){
155             $availableServer[$mir['mirror']]=$mir['mirror'];
156           }
157         }
158       }
159     }
160     
161     /* Generate package list */
162     $this->list=$this->genPkgs();
164     /* + was pressed to open the package dialog */
165     if(isset($_POST['Addpkg'])){
166       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
167       $this->is_dialog =true;
168     }
170     /* Delte selected package */ 
171     if(isset($_POST['Delpkg'])){
172       unset($this->usedPackages[$_POST['usedPackages']]);
173     }
175     /* Abort package selection dialog */ 
176     if(isset($_POST['CancelSubObject'])){
177       $this->dialog = false;
178       $this->is_dialog=false;
179     }
181     /* attach new packages */
182     if(isset($_POST['SaveSubObject'])) {
183       $this->dialog->save_object();
184       if(count($this->dialog->check())){
185         foreach($this->dialog->check() as $msgs){
186         print_red($msgs);
187         }
188       }else{
189         $use = $this->dialog->save();
190         $this->usedPackages = $use;
191         $this->dialog = false;
192         $this->is_dialog=false;
193       }
194     }
196     /* Configuration dialog open*/
197     if(isset($_POST['Conpkg'])){
198       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,"none");
199       $this->is_dialog =true;
200     }
201    
202     /* Save Configuration */
203     if(isset($_POST['SaveObjectConfig'])){
204       $this->dialog = false;
205       $this->is_dialog=false;
206     }
208     /* cancel configuration */     
209     if(isset($_POST['CancelObjectConfig'])){
210       $this->dialog = false;
211       $this->is_dialog=false;
212     }
214     /* Display dialog */ 
215     if($this->is_dialog){
216       return $this->dialog->execute();
217     }
219     /* Assign section to smarty */
220     $strsec = "";
221     foreach($this->FAIdebianSection as $sec){
222        $strsec .= $sec." ";
223     }
225     $smarty->assign("OptionsACL","");
226     if(empty($this->FAIdebianMirror)){
227       $smarty->assign("OptionsACL"," disabled ");
228     }
230     $smarty->assign("mirrors" ,$availableServer);
231     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
232     $smarty->assign("releases",$this->releases);
233     $smarty->assign("release" ,$this->FAIdebianRelease);
234     $smarty->assign("sections",$this->sections);
235     $smarty->assign("section" ,$strsec);
236     $smarty->assign("usedPackages",$this->printUsedPackages());
237     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
238     return($display);
239   }
241   function genMirror(){
242     $confDir = $this->confDir; 
243     if(!is_readable($confDir)){
244       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
245     }else{
246       
247       /* Try to catch all available mirrors 
248          Possibly check if each server is reachable
249        */
251       $this->servers= array();
252       $this->releases=array();  
253       $this->sections=array(); 
254       $this->mirrors= array();
255       $fd = opendir($confDir);
256       while($mirror = readdir($fd)){
257         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
259           $mirrorDir = $confDir.$mirror."/";
261           $fe = opendir($mirrorDir);
262           while($release = readdir($fe)){
263             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
265               $releaseDir = $mirrorDir.$release."/";
267               $ff = opendir($releaseDir);
268               while($section = readdir($ff)){
269                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
270               
271                   $this->servers[$mirror]=$mirror;
272                   $this->releases[$release]=$release;
273                   $this->sections[$section]=$section;
275                   $arr=array();
276                   $arr['mirror'] = $mirror;
277                   $arr['release']= $release; 
278                   $arr['section']= $section;
279  
280                   $this->mirrors[] = $arr ;
281                 }
282               }
283               fclose($ff);
284             }
285           }
286           fclose($fe);
287         }
288       }
289       fclose($fd);
290     }
293   }
295   /* Delete me, and all my subtrees
296    */
297   function remove_from_parent()
298   {
299     $ldap = $this->config->get_ldap_link();
300     $ldap->cd ($this->dn);
301     $ldap->rmdir_recursive($this->dn);
302     $this->handle_post_events("remove");    
303   }
306   /* Save data to object 
307    */
308   function save_object()
309   {
310     
311     plugin::save_object();
312     foreach($this->attributes as $attrs){
313       if(isset($_POST[$attrs])){
314         $this->$attrs = $_POST[$attrs];
315       }
316     }
317   }
320   /* Check supplied data */
321   function check()
322   {
323     $message= array();
324   
325     if(count($this->usedPackages)==0){
326       $message[]=_("Please select a least one Package.");
327     }
328   
329     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
330       $message[]=_("Please choose a valid combination for your repository setup.");
331     }
333     return ($message);
334   }
336   function printUsedPackages(){
337     $a_ret=array(); 
338     if(is_array($this->usedPackages)) {
339       foreach($this->usedPackages as $usedName){
340         if(isset($this->list[$usedName][1])){
341           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
342         }else{
343           $a_ret[$usedName] = $usedName;
344         }
345       }
346     }
347     return($a_ret);
348   }
350    function genPkgs(){
351     /* Generate a list off available packages for this mirror, section and release
352      */
353     /* Only read this file if it wasn't read before */
354     if($this->buffer==NULL){
355       $this->buffer=array();
356       $a_ret = array();
357       foreach($this->FAIdebianSection as $sec){
358         $strID= "/etc/gosa/fai/".$this->FAIdebianMirror."/".$this->FAIdebianRelease."/".$sec;
359         
360         if(!is_file($strID)){
361           print_red(sprintf(_("Package file don't exists '%s'."),$strID));
362           $this->FAIdebianMirror="";
363           unset($this->buffer);
364           return(array());
365         }
366         $fp = fopen($strID,"r");
368         /* Parse every line and create an array */
369         while(!feof($fp)){
370           $str= fgets($fp,512);
371           $stra= split("\|",$str);
372           if(count($stra)==4){
373             $a_ret[$stra[0]] = $stra;
374           }
375         }
376         fclose($fp);
377           /* Save our Data, to avoid reading it again */
378       }
379       $this->buffer = $a_ret;
380       ksort($a_ret);
381       return($a_ret);
382     }else{
383       return $this->buffer;
384     }
385   }
388   /* Save to LDAP */
389   function save()
390   {
391     plugin::save();
392  
393     $ldap = $this->config->get_ldap_link();
395     $this->attrs['FAIpackage'] = array();
396     foreach($this->usedPackages as $pkg => $obj){
397       $this->attrs['FAIpackage'][] = $pkg;
398     } 
399   
400     $this->attrs['FAIdebianSection'] = array();
401     foreach($this->FAIdebianSection as $sec){
402       $this->attrs['FAIdebianSection'][] = $sec;
403     }
405     $ldap->cat($this->dn);
406     if($ldap->count()!=0){
407       /* Write FAIscript to ldap*/
408       $ldap->cd($this->dn);
409       $ldap->modify($this->attrs);
410     }else{
411       /* Write FAIscript to ldap*/
412       $ldap->cd($this->config->current['BASE']);
413       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
414       $ldap->cd($this->dn);
415       $ldap->add($this->attrs);
416     }
417     show_ldap_error($ldap->get_error());
418   }
421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
422 ?>