Code

918dab1d892bb07e7dd88ef41fd5cd2ae4684f81
[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     if((!$this->is_account)&&(!$this->newDialogShown)){
98       
99       if($this->dialog==NULL){
100         $this->genMirror();
101         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
102         $this->is_dialog =true;
103       }
104   
105       if(isset($_POST['SaveObjectNew'])){
106         $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
107         $this->FAIdebianSection = $this->dialog->FAIdebianSection;
108         $this->FAIdebianMirror  = $this->dialog->FAIdebianMirror;
109         unset($this->dialog);
110         $this->dialog = false;
111         $this->is_dialog=false;
112         $this->newDialogShown = true;
113       }
115       if($this->dialog){
116         $display=$this->dialog->execute();
117         return($display); 
118       }
119     }
120     $disableRelease   = false;
121     $disableMirror    = false;
122     $disableOptions   = false;
123     $disableSection   = false;
125     /* Assign variables */
126     foreach($this->attributes as $attrs){
127       $smarty->assign($attrs,$this->$attrs);
128     }
130     $this->genMirror();
132     /* check servers matching release and section settings */
133     $availableServer = array();
134     foreach($this->mirrors as $mir){
135       if($mir['release'] == $this->FAIdebianRelease){
136         $pass = true;
137         foreach($this->FAIdebianSection as $sec){
138           if($sec != $mir['section']){
139             $pass = false;
140           }
141         if($pass){
142             $availableServer[$mir['mirror']]=$mir['mirror'];
143           }
144         }
145       }
146     }
147     if($this->list==NULL){
148       $this->list=$this->genPkgs();
149     }
151     if(isset($_POST['Addpkg'])){
152       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
153       $this->is_dialog =true;
154     }
155  
156     if(isset($_POST['Delpkg'])){
157       unset($this->usedPackages[$_POST['usedPackages']]);
158     }
159  
160     if(isset($_POST['CancelSubObject'])){
161       $this->dialog = false;
162       $this->is_dialog=false;
163     }
165     if(isset($_POST['SaveSubObject'])) {
166       $this->dialog->save_object();
167       if(count($this->dialog->check())){
168         foreach($this->dialog->check() as $msgs){
169         print_red($msgs);
170         }
171       }else{
172         $use = $this->dialog->save();
173         $this->usedPackages = $use;
174         $this->dialog = false;
175         $this->is_dialog=false;
176       }
177     }
179     if(isset($_POST['Conpkg'])){
180       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,"none");
181       $this->is_dialog =true;
182     }
183    
184     if(isset($_POST['SaveObjectConfig'])){
185       $this->dialog = false;
186       $this->is_dialog=false;
187     }
188      
189     if(isset($_POST['CancelObjectConfig'])){
190       $this->dialog = false;
191       $this->is_dialog=false;
192     }
193  
194     if($this->is_dialog){
195       return $this->dialog->execute();
196     }
198     $strsec = "";
199     foreach($this->FAIdebianSection as $sec){
200        $strsec .= $sec." ";
201     }
203     $smarty->assign("mirrors" ,$availableServer);
204     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
205     $smarty->assign("releases",$this->releases);
206     $smarty->assign("release" ,$this->FAIdebianRelease);
207     $smarty->assign("sections",$this->sections);
208     $smarty->assign("section" ,$strsec);
209     $smarty->assign("usedPackages",$this->printUsedPackages());
210     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
211     return($display);
212   }
214   function genMirror(){
215     $confDir = $this->confDir; 
216     if(!is_readable($confDir)){
217       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
218     }else{
219       
220       /* Try to catch all available mirrors 
221          Possibly check if each server is reachable
222        */
224       $this->servers= array();
225       $this->releases=array();  
226       $this->sections=array(); 
227       $this->mirrors= array();
228       $fd = opendir($confDir);
229       while($mirror = readdir($fd)){
230         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
232           $mirrorDir = $confDir.$mirror."/";
234           $fe = opendir($mirrorDir);
235           while($release = readdir($fe)){
236             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
238               $releaseDir = $mirrorDir.$release."/";
240               $ff = opendir($releaseDir);
241               while($section = readdir($ff)){
242                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
243               
244                   $this->servers[$mirror]=$mirror;
245                   $this->releases[$release]=$release;
246                   $this->sections[$section]=$section;
248                   $arr=array();
249                   $arr['mirror'] = $mirror;
250                   $arr['release']= $release; 
251                   $arr['section']= $section;
252  
253                   $this->mirrors[] = $arr ;
254                 }
255               }
256               fclose($ff);
257             }
258           }
259           fclose($fe);
260         }
261       }
262       fclose($fd);
263     }
266   }
268   /* Delete me, and all my subtrees
269    */
270   function remove_from_parent()
271   {
272     $ldap = $this->config->get_ldap_link();
273     $ldap->cd ($this->dn);
274     $ldap->rmdir_recursive($this->dn);
275     $this->handle_post_events("remove");    
276   }
279   /* Save data to object 
280    */
281   function save_object()
282   {
283     
284     plugin::save_object();
285     foreach($this->attributes as $attrs){
286       if(isset($_POST[$attrs])){
287         $this->$attrs = $_POST[$attrs];
288       }
289     }
290   }
293   /* Check supplied data */
294   function check()
295   {
296     $message= array();
297   
298     if(count($this->usedPackages)==0){
299       $message[]=_("Please select a least one Package.");
300     }
301   
302     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
303       $message[]=_("Please choose a valid combination for your repository setup.");
304     }
306     return ($message);
307   }
309   function printUsedPackages(){
310     $a_ret=array(); 
311     if(is_array($this->usedPackages)) {
312       foreach($this->usedPackages as $usedName){
313         if(isset($this->list[$usedName][1])){
314           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
315         }else{
316           $a_ret[$usedName] = $usedName;
317         }
318       }
319     }
320     return($a_ret);
321   }
323    function genPkgs(){
324     /* Generate a list off available packages for this mirror, section and release
325      */
326     /* Only read this file if it wasn't read before */
327     if($this->buffer==NULL){
328       $this->buffer=array();
329       $a_ret = array();
330       foreach($this->FAIdebianSection as $sec){
331         $strID= "/etc/gosa/fai/".$this->FAIdebianMirror."/".$this->FAIdebianRelease."/".$sec;
332         
333         if(!is_file($strID)){
334           print_red(sprintf(_("Package file don't exists '%s'."),$strID));
335           return(array());
336         }
337         $fp = fopen($strID,"r");
339         /* Parse every line and create an array */
340         while(!feof($fp)){
341           $str= fgets($fp,512);
342           $stra= split("\|",$str);
343           if(count($stra)==4){
344             $a_ret[$stra[0]] = $stra;
345           }
346         }
347         fclose($fp);
348           /* Save our Data, to avoid reading it again */
349       }
350       $this->buffer = $a_ret;
351       ksort($a_ret);
352       return($a_ret);
353     }else{
354       return $this->buffer[$strID];
355     }
356   }
359   /* Save to LDAP */
360   function save()
361   {
362     plugin::save();
363  
364     $ldap = $this->config->get_ldap_link();
366     $this->attrs['FAIpackage'] = array();
367     foreach($this->usedPackages as $pkg => $obj){
368       $this->attrs['FAIpackage'][] = $pkg;
369     } 
370   
371     $this->attrs['FAIdebianSection'] = array();
372     foreach($this->FAIdebianSection as $sec){
373       $this->attrs['FAIdebianSection'][] = $sec;
374     }
376     $ldap->cat($this->dn);
377     if($ldap->count()!=0){
378       /* Write FAIscript to ldap*/
379       $ldap->cd($this->dn);
380       $ldap->modify($this->attrs);
381     }else{
382       /* Write FAIscript to ldap*/
383       $ldap->cd($this->config->current['BASE']);
384       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
385       $ldap->cd($this->dn);
386       $ldap->add($this->attrs);
387     }
388     show_ldap_error($ldap->get_error());
389   }
392 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
393 ?>