Code

Invalid repository settings fixed.
[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     }
121     /* Assign variables */
122     foreach($this->attributes as $attrs){
123       $smarty->assign($attrs,$this->$attrs);
124     }
126     $this->genMirror();
128     if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
129       $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
130     }
133     /* check servers matching release and section settings */
134     $availableServer = array();
135     foreach($this->mirrors as $mir){
136       if($mir['release'] == $this->FAIdebianRelease){
137         $pass = true;
138         foreach($this->FAIdebianSection as $sec){
139           if($sec != $mir['section']){
140             $pass = false;
141           }
142         if($pass){
143             $availableServer[$mir['mirror']]=$mir['mirror'];
144           }
145         }
146       }
147     }
149     $this->list=$this->genPkgs();
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     
204     $smarty->assign("OptionsACL","");
205     if(empty($this->FAIdebianMirror)){
206       $smarty->assign("OptionsACL"," disabled ");
207     }
209     $smarty->assign("mirrors" ,$availableServer);
210     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
211     $smarty->assign("releases",$this->releases);
212     $smarty->assign("release" ,$this->FAIdebianRelease);
213     $smarty->assign("sections",$this->sections);
214     $smarty->assign("section" ,$strsec);
215     $smarty->assign("usedPackages",$this->printUsedPackages());
216     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
217     return($display);
218   }
220   function genMirror(){
221     $confDir = $this->confDir; 
222     if(!is_readable($confDir)){
223       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
224     }else{
225       
226       /* Try to catch all available mirrors 
227          Possibly check if each server is reachable
228        */
230       $this->servers= array();
231       $this->releases=array();  
232       $this->sections=array(); 
233       $this->mirrors= array();
234       $fd = opendir($confDir);
235       while($mirror = readdir($fd)){
236         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
238           $mirrorDir = $confDir.$mirror."/";
240           $fe = opendir($mirrorDir);
241           while($release = readdir($fe)){
242             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
244               $releaseDir = $mirrorDir.$release."/";
246               $ff = opendir($releaseDir);
247               while($section = readdir($ff)){
248                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
249               
250                   $this->servers[$mirror]=$mirror;
251                   $this->releases[$release]=$release;
252                   $this->sections[$section]=$section;
254                   $arr=array();
255                   $arr['mirror'] = $mirror;
256                   $arr['release']= $release; 
257                   $arr['section']= $section;
258  
259                   $this->mirrors[] = $arr ;
260                 }
261               }
262               fclose($ff);
263             }
264           }
265           fclose($fe);
266         }
267       }
268       fclose($fd);
269     }
272   }
274   /* Delete me, and all my subtrees
275    */
276   function remove_from_parent()
277   {
278     $ldap = $this->config->get_ldap_link();
279     $ldap->cd ($this->dn);
280     $ldap->rmdir_recursive($this->dn);
281     $this->handle_post_events("remove");    
282   }
285   /* Save data to object 
286    */
287   function save_object()
288   {
289     
290     plugin::save_object();
291     foreach($this->attributes as $attrs){
292       if(isset($_POST[$attrs])){
293         $this->$attrs = $_POST[$attrs];
294       }
295     }
296   }
299   /* Check supplied data */
300   function check()
301   {
302     $message= array();
303   
304     if(count($this->usedPackages)==0){
305       $message[]=_("Please select a least one Package.");
306     }
307   
308     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
309       $message[]=_("Please choose a valid combination for your repository setup.");
310     }
312     return ($message);
313   }
315   function printUsedPackages(){
316     $a_ret=array(); 
317     if(is_array($this->usedPackages)) {
318       foreach($this->usedPackages as $usedName){
319         if(isset($this->list[$usedName][1])){
320           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
321         }else{
322           $a_ret[$usedName] = $usedName;
323         }
324       }
325     }
326     return($a_ret);
327   }
329    function genPkgs(){
330     /* Generate a list off available packages for this mirror, section and release
331      */
332     /* Only read this file if it wasn't read before */
333     if($this->buffer==NULL){
334       $this->buffer=array();
335       $a_ret = array();
336       foreach($this->FAIdebianSection as $sec){
337         $strID= "/etc/gosa/fai/".$this->FAIdebianMirror."/".$this->FAIdebianRelease."/".$sec;
338         
339         if(!is_file($strID)){
340           print_red(sprintf(_("Package file don't exists '%s'."),$strID));
341           $this->FAIdebianMirror="";
342           unset($this->buffer);
343           return(array());
344         }
345         $fp = fopen($strID,"r");
347         /* Parse every line and create an array */
348         while(!feof($fp)){
349           $str= fgets($fp,512);
350           $stra= split("\|",$str);
351           if(count($stra)==4){
352             $a_ret[$stra[0]] = $stra;
353           }
354         }
355         fclose($fp);
356           /* Save our Data, to avoid reading it again */
357       }
358       $this->buffer = $a_ret;
359       ksort($a_ret);
360       return($a_ret);
361     }else{
362       return $this->buffer;
363     }
364   }
367   /* Save to LDAP */
368   function save()
369   {
370     plugin::save();
371  
372     $ldap = $this->config->get_ldap_link();
374     $this->attrs['FAIpackage'] = array();
375     foreach($this->usedPackages as $pkg => $obj){
376       $this->attrs['FAIpackage'][] = $pkg;
377     } 
378   
379     $this->attrs['FAIdebianSection'] = array();
380     foreach($this->FAIdebianSection as $sec){
381       $this->attrs['FAIdebianSection'][] = $sec;
382     }
384     $ldap->cat($this->dn);
385     if($ldap->count()!=0){
386       /* Write FAIscript to ldap*/
387       $ldap->cd($this->dn);
388       $ldap->modify($this->attrs);
389     }else{
390       /* Write FAIscript to ldap*/
391       $ldap->cd($this->config->current['BASE']);
392       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
393       $ldap->cd($this->dn);
394       $ldap->add($this->attrs);
395     }
396     show_ldap_error($ldap->get_error());
397   }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>