Code

dd942cf0bc9133b43309e31fcec36822dfcb97ae
[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
40   var $mirror                    = ""; // selected mirror
42   var $servers          = array();  // All available servers
43   var $releases         = array();  // All possible releases 
44   var $sections         = array();  // All section types
46   var $list             = NULL;
48   var $mirrors          = array();  // The combination of server/release/section
49   var $confDir          = "";
50   var $usedPackages     = array();
51   var $buffer           = NULL; 
52   var $strID            ="";
53   var $newDialogShown   =false;
55   function faiPackage ($config, $dn= NULL)
56   {
57     /* Load Attributes */
58     plugin::plugin ($config, $dn);
60     /* If "dn==new" we try to create a new entry
61      * Else we must read all objects from ldap which belong to this entry.
62      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
63      */
64     if($dn != "new"){
65       $this->dn =$dn;
66     }
68     if(isset($this->attrs['FAIpackage'])){
69       unset($this->attrs['FAIpackage']['count']);
70       foreach($this->attrs['FAIpackage'] as $pkg){
71         $this->usedPackages[$pkg] = $pkg;
72       }
73     }else{
74       $this->usedPackages = array();
75     }  
77     unset($this->attrs['FAIdebianSection']['count']);
78     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
79       $this->FAIdebianSection = array();
80       foreach($this->attrs['FAIdebianSection'] as $sec){
81         $this->FAIdebianSection[$sec]=$sec;
82       }
83     }
85     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
86       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
87     }
88     $this->confDir = CONFIG_DIR."/fai/";
89     $this->FAIpackage = array();
91     /* Extract mirror location */
92     $this->mirror= preg_replace("!^[^/]+//([^/]+).*$!", '$1', $this->FAIdebianMirror);
93   }
95   function execute()
96   {
97     /* Fill templating stuff */
98     $smarty= get_smarty();
99     $display= "";
101     $this->genMirror();
103     /* Check if we exist already - no need to ask for revisions, etc. then */
104     if ($this->dn != "new"){
105         $this->newDialogShown= true;
106     }
108     if((!$this->is_account)&&(!$this->newDialogShown)){
109       
110       if($this->dialog==NULL){
111         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
112         $this->is_dialog =true;
113       }
115       /* alert possible missconfigurations */ 
116       if((count($this->releases)==0)||(count($this->servers)==0)||(count($this->sections)==0)){
117         print_red(_("There is no useable package list defined."));
118       }
120       /* Assign posible changes, for mirror combinations */
121       $this->dialog->save_object();
122       $this->dialog->releases = $this->releases;
123       $this->dialog->servers  = $this->servers;
124       $this->dialog->sections = $this->sections;
125       $this->dialog->mirrors  = $this->mirrors;
127       /* Assign Repository settings*/ 
128       if(isset($_POST['SaveObjectNew'])){
129         $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
130         $this->FAIdebianSection = $this->dialog->FAIdebianSection;
131         $this->FAIdebianMirror  = $this->dialog->FAIdebianMirror;
132         unset($this->dialog);
133         $this->dialog = false;
134         $this->is_dialog=false;
135         //  $this->newDialogShown = true;
136       }
138       /* Draw dialog */
139       if($this->dialog){
140         $display=$this->dialog->execute();
141         return($display); 
142       }
143     }
145     /* Assign variables */
146     foreach($this->attributes as $attrs){
147       $smarty->assign($attrs,$this->$attrs);
148     }
149   
150     /* Set mirror if changed */
151     if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
152       $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
153     }
154   
155     /* check servers matching release and section settings */
156     $availableServer = array();
157     foreach($this->mirrors as $mir){
158       if($mir['release'] == $this->FAIdebianRelease){
159         $pass = true;
160         foreach($this->FAIdebianSection as $sec){
161           if($sec != $mir['section']){
162             $pass = false;
163           }
164         if($pass){
165             $availableServer[$mir['mirror']]=$mir['mirror'];
166           }
167         }
168       }
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       }
204     }
206     /* Configuration dialog open*/
207     if(isset($_POST['Conpkg'])){
208       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,"none");
209       $this->is_dialog =true;
210     }
211    
212     /* Save Configuration */
213     if(isset($_POST['SaveObjectConfig'])){
214       $this->dialog = false;
215       $this->is_dialog=false;
216     }
218     /* cancel configuration */     
219     if(isset($_POST['CancelObjectConfig'])){
220       $this->dialog = false;
221       $this->is_dialog=false;
222     }
224     /* Display dialog */ 
225     if($this->is_dialog){
226       return $this->dialog->execute();
227     }
229     /* Assign section to smarty */
230     $strsec = "";
231     foreach($this->FAIdebianSection as $sec){
232        $strsec .= $sec." ";
233     }
235     $smarty->assign("OptionsACL","");
236     if(empty($this->FAIdebianMirror)){
237       $smarty->assign("OptionsACL"," disabled ");
238     }
240     $smarty->assign("mirrors" ,$availableServer);
241     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
242     $smarty->assign("releases",$this->releases);
243     $smarty->assign("release" ,$this->FAIdebianRelease);
244     $smarty->assign("sections",$this->sections);
245     $smarty->assign("section" ,$strsec);
246     $smarty->assign("usedPackages",$this->printUsedPackages());
247     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
248     return($display);
249   }
251   function genMirror(){
252     $confDir = $this->confDir; 
253     if(!is_readable($confDir)){
254       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
255     }else{
256       
257       /* Try to catch all available mirrors 
258          Possibly check if each server is reachable
259        */
261       $this->servers= array();
262       $this->releases=array();  
263       $this->sections=array(); 
264       $this->mirrors= array();
265       $fd = opendir($confDir);
266       while($mirror = readdir($fd)){
267         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
269           $mirrorDir = $confDir.$mirror."/";
271           $fe = opendir($mirrorDir);
272           while($release = readdir($fe)){
273             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
275               $releaseDir = $mirrorDir.$release."/";
277               $ff = opendir($releaseDir);
278               while($section = readdir($ff)){
279                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
280               
281                   $this->servers[$mirror]=$mirror;
282                   $this->releases[$release]=$release;
283                   $this->sections[$section]=$section;
285                   $arr=array();
286                   $arr['mirror'] = $mirror;
287                   $arr['release']= $release; 
288                   $arr['section']= $section;
289  
290                   $this->mirrors[] = $arr ;
291                 }
292               }
293               fclose($ff);
294             }
295           }
296           fclose($fe);
297         }
298       }
299       fclose($fd);
300     }
303   }
305   /* Delete me, and all my subtrees
306    */
307   function remove_from_parent()
308   {
309     $ldap = $this->config->get_ldap_link();
310     $ldap->cd ($this->dn);
311     $ldap->rmdir_recursive($this->dn);
312     $this->handle_post_events("remove");    
313   }
316   /* Save data to object 
317    */
318   function save_object()
319   {
320     
321     plugin::save_object();
322     foreach($this->attributes as $attrs){
323       if(isset($_POST[$attrs])){
324         $this->$attrs = $_POST[$attrs];
325       }
326     }
327   }
330   /* Check supplied data */
331   function check()
332   {
333     $message= array();
334   
335     if(count($this->usedPackages)==0){
336       $message[]=_("Please select a least one Package.");
337     }
338   
339     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
340       $message[]=_("Please choose a valid combination for your repository setup.");
341     }
343     return ($message);
344   }
346   function printUsedPackages(){
347     $a_ret=array(); 
348     if(is_array($this->usedPackages)) {
349       foreach($this->usedPackages as $usedName){
350         if(isset($this->list[$usedName][1])){
351           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
352         }else{
353           $a_ret[$usedName] = $usedName;
354         }
355       }
356     }
357     return($a_ret);
358   }
360    function genPkgs(){
361     /* Generate a list off available packages for this mirror, section and release
362      */
363     /* Only read this file if it wasn't read before */
364     if($this->buffer==NULL){
365       $this->buffer=array();
366       $a_ret = array();
367       foreach($this->FAIdebianSection as $sec){
368         $strID= "/etc/gosa/fai/".$this->FAIdebianMirror."/".$this->FAIdebianRelease."/".$sec;
369         
370         if(!is_file($strID)){
371           print_red(sprintf(_("Package file don't exists '%s'."),$strID));
372           $this->FAIdebianMirror="";
373           unset($this->buffer);
374           return(array());
375         }
376         $fp = fopen($strID,"r");
378         /* Parse every line and create an array */
379         while(!feof($fp)){
380           $str= fgets($fp,512);
381           $stra= split("\|",$str);
382           if(count($stra)==4){
383             $a_ret[$stra[0]] = $stra;
384           }
385         }
386         fclose($fp);
387           /* Save our Data, to avoid reading it again */
388       }
389       $this->buffer = $a_ret;
390       ksort($a_ret);
391       return($a_ret);
392     }else{
393       return $this->buffer;
394     }
395   }
398   /* Save to LDAP */
399   function save()
400   {
401     plugin::save();
402  
403     $ldap = $this->config->get_ldap_link();
405     $this->attrs['FAIpackage'] = array();
406     foreach($this->usedPackages as $pkg => $obj){
407       $this->attrs['FAIpackage'][] = $pkg;
408     } 
409   
410     $this->attrs['FAIdebianSection'] = array();
411     foreach($this->FAIdebianSection as $sec){
412       $this->attrs['FAIdebianSection'][] = $sec;
413     }
415     $ldap->cat($this->dn);
416     if($ldap->count()!=0){
417       /* Write FAIscript to ldap*/
418       $ldap->cd($this->dn);
419       $ldap->modify($this->attrs);
420     }else{
421       /* Write FAIscript to ldap*/
422       $ldap->cd($this->config->current['BASE']);
423       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
424       $ldap->cd($this->dn);
425       $ldap->add($this->attrs);
426     }
427     show_ldap_error($ldap->get_error());
428   }
431 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
432 ?>