Code

b346ed4442de2a5b5bfe4d67f81a1a6429b53e43
[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->releases = $this->releases;
122       $this->dialog->servers  = $this->servers;
123       $this->dialog->sections = $this->sections;
124       $this->dialog->mirrors  = $this->mirrors;
126       /* Assign Repository settings*/ 
127       if(isset($_POST['SaveObjectNew'])){
128         $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
129         $this->FAIdebianSection = $this->dialog->FAIdebianSection;
130         $this->FAIdebianMirror  = $this->dialog->FAIdebianMirror;
131         unset($this->dialog);
132         $this->dialog = false;
133         $this->is_dialog=false;
134         $this->newDialogShown = true;
135       }
137       /* Draw dialog */
138       if($this->dialog){
139         $display=$this->dialog->execute();
140         return($display); 
141       }
142     }
144     /* Assign variables */
145     foreach($this->attributes as $attrs){
146       $smarty->assign($attrs,$this->$attrs);
147     }
149     /* Set mirror if changed */
150     if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
151       $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
152     }
154     /* check servers matching release and section settings */
155     $availableServer = array();
156     foreach($this->mirrors as $mir){
157       if($mir['release'] == $this->FAIdebianRelease){
158         $pass = true;
159         foreach($this->FAIdebianSection as $sec){
160           if($sec != $mir['section']){
161             $pass = false;
162           }
163         if($pass){
164             $availableServer[$mir['mirror']]=$mir['mirror'];
165           }
166         }
167       }
168     }
169     
170     /* Generate package list */
171     $this->list=$this->genPkgs();
173     /* + was pressed to open the package dialog */
174     if(isset($_POST['Addpkg'])){
175       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
176       $this->is_dialog =true;
177     }
179     /* Delte selected package */ 
180     if(isset($_POST['Delpkg'])){
181       unset($this->usedPackages[$_POST['usedPackages']]);
182     }
184     /* Abort package selection dialog */ 
185     if(isset($_POST['CancelSubObject'])){
186       $this->dialog = false;
187       $this->is_dialog=false;
188     }
190     /* attach new packages */
191     if(isset($_POST['SaveSubObject'])) {
192       $this->dialog->save_object();
193       if(count($this->dialog->check())){
194         foreach($this->dialog->check() as $msgs){
195         print_red($msgs);
196         }
197       }else{
198         $use = $this->dialog->save();
199         $this->usedPackages = $use;
200         $this->dialog = false;
201         $this->is_dialog=false;
202       }
203     }
205     /* Configuration dialog open*/
206     if(isset($_POST['Conpkg'])){
207       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,"none");
208       $this->is_dialog =true;
209     }
210    
211     /* Save Configuration */
212     if(isset($_POST['SaveObjectConfig'])){
213       $this->dialog = false;
214       $this->is_dialog=false;
215     }
217     /* cancel configuration */     
218     if(isset($_POST['CancelObjectConfig'])){
219       $this->dialog = false;
220       $this->is_dialog=false;
221     }
223     /* Display dialog */ 
224     if($this->is_dialog){
225       return $this->dialog->execute();
226     }
228     /* Assign section to smarty */
229     $strsec = "";
230     foreach($this->FAIdebianSection as $sec){
231        $strsec .= $sec." ";
232     }
234     $smarty->assign("OptionsACL","");
235     if(empty($this->FAIdebianMirror)){
236       $smarty->assign("OptionsACL"," disabled ");
237     }
239     $smarty->assign("mirrors" ,$availableServer);
240     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
241     $smarty->assign("releases",$this->releases);
242     $smarty->assign("release" ,$this->FAIdebianRelease);
243     $smarty->assign("sections",$this->sections);
244     $smarty->assign("section" ,$strsec);
245     $smarty->assign("usedPackages",$this->printUsedPackages());
246     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
247     return($display);
248   }
250   function genMirror(){
251     $confDir = $this->confDir; 
252     if(!is_readable($confDir)){
253       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
254     }else{
255       
256       /* Try to catch all available mirrors 
257          Possibly check if each server is reachable
258        */
260       $this->servers= array();
261       $this->releases=array();  
262       $this->sections=array(); 
263       $this->mirrors= array();
264       $fd = opendir($confDir);
265       while($mirror = readdir($fd)){
266         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
268           $mirrorDir = $confDir.$mirror."/";
270           $fe = opendir($mirrorDir);
271           while($release = readdir($fe)){
272             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
274               $releaseDir = $mirrorDir.$release."/";
276               $ff = opendir($releaseDir);
277               while($section = readdir($ff)){
278                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
279               
280                   $this->servers[$mirror]=$mirror;
281                   $this->releases[$release]=$release;
282                   $this->sections[$section]=$section;
284                   $arr=array();
285                   $arr['mirror'] = $mirror;
286                   $arr['release']= $release; 
287                   $arr['section']= $section;
288  
289                   $this->mirrors[] = $arr ;
290                 }
291               }
292               fclose($ff);
293             }
294           }
295           fclose($fe);
296         }
297       }
298       fclose($fd);
299     }
302   }
304   /* Delete me, and all my subtrees
305    */
306   function remove_from_parent()
307   {
308     $ldap = $this->config->get_ldap_link();
309     $ldap->cd ($this->dn);
310     $ldap->rmdir_recursive($this->dn);
311     $this->handle_post_events("remove");    
312   }
315   /* Save data to object 
316    */
317   function save_object()
318   {
319     
320     plugin::save_object();
321     foreach($this->attributes as $attrs){
322       if(isset($_POST[$attrs])){
323         $this->$attrs = $_POST[$attrs];
324       }
325     }
326   }
329   /* Check supplied data */
330   function check()
331   {
332     $message= array();
333   
334     if(count($this->usedPackages)==0){
335       $message[]=_("Please select a least one Package.");
336     }
337   
338     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
339       $message[]=_("Please choose a valid combination for your repository setup.");
340     }
342     return ($message);
343   }
345   function printUsedPackages(){
346     $a_ret=array(); 
347     if(is_array($this->usedPackages)) {
348       foreach($this->usedPackages as $usedName){
349         if(isset($this->list[$usedName][1])){
350           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
351         }else{
352           $a_ret[$usedName] = $usedName;
353         }
354       }
355     }
356     return($a_ret);
357   }
359    function genPkgs(){
360     /* Generate a list off available packages for this mirror, section and release
361      */
362     /* Only read this file if it wasn't read before */
363     if($this->buffer==NULL){
364       $this->buffer=array();
365       $a_ret = array();
366       foreach($this->FAIdebianSection as $sec){
367         $strID= "/etc/gosa/fai/".$this->mirror."/".$this->FAIdebianRelease."/".$sec;
368         
369         if(!is_file($strID)){
370           print_red(sprintf(_("Package file don't exists '%s'."),$strID));
371           $this->FAIdebianMirror="";
372           unset($this->buffer);
373           return(array());
374         }
375         $fp = fopen($strID,"r");
377         /* Parse every line and create an array */
378         while(!feof($fp)){
379           $str= fgets($fp,512);
380           $stra= split("\|",$str);
381           if(count($stra)==4){
382             $a_ret[$stra[0]] = $stra;
383           }
384         }
385         fclose($fp);
386           /* Save our Data, to avoid reading it again */
387       }
388       $this->buffer = $a_ret;
389       ksort($a_ret);
390       return($a_ret);
391     }else{
392       return $this->buffer;
393     }
394   }
397   /* Save to LDAP */
398   function save()
399   {
400     plugin::save();
401  
402     $ldap = $this->config->get_ldap_link();
404     $this->attrs['FAIpackage'] = array();
405     foreach($this->usedPackages as $pkg => $obj){
406       $this->attrs['FAIpackage'][] = $pkg;
407     } 
408   
409     $this->attrs['FAIdebianSection'] = array();
410     foreach($this->FAIdebianSection as $sec){
411       $this->attrs['FAIdebianSection'][] = $sec;
412     }
414     $ldap->cat($this->dn);
415     if($ldap->count()!=0){
416       /* Write FAIscript to ldap*/
417       $ldap->cd($this->dn);
418       $ldap->modify($this->attrs);
419     }else{
420       /* Write FAIscript to ldap*/
421       $ldap->cd($this->config->current['BASE']);
422       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
423       $ldap->cd($this->dn);
424       $ldap->add($this->attrs);
425     }
426     show_ldap_error($ldap->get_error());
427   }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>