Code

Fixed missing images problem in group headpage
[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       ksort($this->usedPackages);
74     }else{
75       $this->usedPackages = array();
76     }  
78     unset($this->attrs['FAIdebianSection']['count']);
79     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
80       $this->FAIdebianSection = array();
81       foreach($this->attrs['FAIdebianSection'] as $sec){
82         $this->FAIdebianSection[$sec]=$sec;
83       }
84     }
86     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
87       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
88     }
89     $this->confDir = CONFIG_DIR."/fai/";
90     $this->FAIpackage = array();
92     /* Extract mirror location */
93     $this->mirror= preg_replace("!^[^/]+//([^/]+).*$!", '$1', $this->FAIdebianMirror);
94   }
96   function execute()
97   {
98     /* Fill templating stuff */
99     $smarty= get_smarty();
100     $display= "";
102     $this->genMirror();
104     /* Check if we exist already - no need to ask for revisions, etc. then */
105     if ($this->dn != "new"){
106         $this->newDialogShown= true;
107     }
109     if((!$this->is_account)&&(!$this->newDialogShown)){
110       
111       if($this->dialog==NULL){
112         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
113         $this->is_dialog =true;
114       }
116       /* alert possible missconfigurations */ 
117       if((count($this->releases)==0)||(count($this->servers)==0)||(count($this->sections)==0)){
118         print_red(_("There is no useable package list defined."));
119       }
121       /* Assign posible changes, for mirror combinations */
122       $this->dialog->save_object();
123       $this->dialog->releases = $this->releases;
124       $this->dialog->servers  = $this->servers;
125       $this->dialog->sections = $this->sections;
126       $this->dialog->mirrors  = $this->mirrors;
128       /* Assign Repository settings*/ 
129       if(isset($_POST['SaveObjectNew'])){
130         $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
131         $this->FAIdebianSection = $this->dialog->FAIdebianSection;
132         $this->FAIdebianMirror  = $this->dialog->FAIdebianMirror;
133         unset($this->dialog);
134         $this->dialog = false;
135         $this->is_dialog=false;
136         $this->newDialogShown = true;
137       }
139       /* Draw dialog */
140       if($this->dialog){
141         $display=$this->dialog->execute();
142         return($display); 
143       }
144     }
146     /* Assign variables */
147     foreach($this->attributes as $attrs){
148       $smarty->assign($attrs,$this->$attrs);
149     }
150   
151     /* Set mirror if changed */
152     if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
153       $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
154     }
155   
156     /* check servers matching release and section settings */
157     $availableServer = array();
158     foreach($this->mirrors as $mir){
159       if($mir['release'] == $this->FAIdebianRelease){
160         $pass = true;
161         foreach($this->FAIdebianSection as $sec){
162           if($sec != $mir['section']){
163             $pass = false;
164           }
165         if($pass){
166             $availableServer[$mir['mirror']]=$mir['mirror'];
167           }
168         }
169       }
170     }
171  
172     /* Generate package list */
173     $this->list=$this->genPkgs();
175     /* + was pressed to open the package dialog */
176     if(isset($_POST['Addpkg'])){
177       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
178       $this->is_dialog =true;
179     }
181     /* Delte selected package */ 
182     if(isset($_POST['Delpkg'])){
183       unset($this->usedPackages[$_POST['usedPackages']]);
184     }
186     /* Abort package selection dialog */ 
187     if(isset($_POST['CancelSubObject'])){
188       $this->dialog = false;
189       $this->is_dialog=false;
190     }
192     /* attach new packages */
193     if(isset($_POST['SaveSubObject'])) {
194       $this->dialog->save_object();
195       if(count($this->dialog->check())){
196         foreach($this->dialog->check() as $msgs){
197         print_red($msgs);
198         }
199       }else{
200         $use = $this->dialog->save();
201         $this->usedPackages = $use;
202         $this->dialog = false;
203         $this->is_dialog=false;
204         ksort($this->usedPackages);
205       }
206     }
208     /* Configuration dialog open*/
209     if(isset($_POST['Conpkg'])){
210       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,"none");
211       $this->is_dialog =true;
212     }
213    
214     /* Save Configuration */
215     if(isset($_POST['SaveObjectConfig'])){
216       $this->dialog = false;
217       $this->is_dialog=false;
218     }
220     /* cancel configuration */     
221     if(isset($_POST['CancelObjectConfig'])){
222       $this->dialog = false;
223       $this->is_dialog=false;
224     }
226     /* Display dialog */ 
227     if($this->is_dialog){
228       return $this->dialog->execute();
229     }
231     /* Assign section to smarty */
232     $strsec = "";
233     foreach($this->FAIdebianSection as $sec){
234        $strsec .= $sec." ";
235     }
237     $smarty->assign("OptionsACL","");
238     if(empty($this->FAIdebianMirror)){
239       $smarty->assign("OptionsACL"," disabled ");
240     }
242     $smarty->assign("mirrors" ,$availableServer);
243     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
244     $smarty->assign("releases",$this->releases);
245     $smarty->assign("release" ,$this->FAIdebianRelease);
246     $smarty->assign("sections",$this->sections);
247     $smarty->assign("section" ,$strsec);
248     $smarty->assign("usedPackages",$this->printUsedPackages());
249     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
250     return($display);
251   }
253   function genMirror(){
254     $confDir = $this->confDir; 
255     if(!is_readable($confDir)){
256       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
257     }else{
258       
259       /* Try to catch all available mirrors 
260          Possibly check if each server is reachable
261        */
263       $this->servers= array();
264       $this->releases=array();  
265       $this->sections=array(); 
266       $this->mirrors= array();
267       $fd = opendir($confDir);
268       while($mirror = readdir($fd)){
269         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
271           $mirrorDir = $confDir.$mirror."/";
273           $fe = opendir($mirrorDir);
274           while($release = readdir($fe)){
275             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
277               $releaseDir = $mirrorDir.$release."/";
279               $ff = opendir($releaseDir);
280               while($section = readdir($ff)){
281                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
282               
283                   $this->servers[$mirror]=$mirror;
284                   $this->releases[$release]=$release;
285                   $this->sections[$section]=$section;
287                   $arr=array();
288                   $arr['mirror'] = $mirror;
289                   $arr['release']= $release; 
290                   $arr['section']= $section;
291  
292                   $this->mirrors[] = $arr ;
293                 }
294               }
295               fclose($ff);
296             }
297           }
298           fclose($fe);
299         }
300       }
301       fclose($fd);
302     }
305   }
307   /* Delete me, and all my subtrees
308    */
309   function remove_from_parent()
310   {
311     $ldap = $this->config->get_ldap_link();
312     $ldap->cd ($this->dn);
313     $ldap->rmdir_recursive($this->dn);
314     $this->handle_post_events("remove");    
315   }
318   /* Save data to object 
319    */
320   function save_object()
321   {
322     
323     plugin::save_object();
324     foreach($this->attributes as $attrs){
325       if(isset($_POST[$attrs])){
326         $this->$attrs = $_POST[$attrs];
327       }
328     }
329   }
332   /* Check supplied data */
333   function check()
334   {
335     $message= array();
336   
337     if(count($this->usedPackages)==0){
338       $message[]=_("Please select a least one Package.");
339     }
340   
341     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
342       $message[]=_("Please choose a valid combination for your repository setup.");
343     }
345     return ($message);
346   }
348   function printUsedPackages(){
349     $a_ret=array(); 
350     if(is_array($this->usedPackages)) {
351       foreach($this->usedPackages as $usedName){
352         if(isset($this->list[$usedName][1])){
353           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
354         }else{
355           $a_ret[$usedName] = $usedName;
356         }
357       }
358     }
359     return($a_ret);
360   }
362    function genPkgs(){
363     /* Generate a list off available packages for this mirror, section and release
364      */
365     /* Only read this file if it wasn't read before */
366     if($this->buffer==NULL){
367       $this->buffer=array();
368       $a_ret = array();
369       foreach($this->FAIdebianSection as $sec){
370         $strID= "/etc/gosa/fai/".preg_replace("#^[^/]+//([^/]+).*$#","\\1",$this->FAIdebianMirror)."/".$this->FAIdebianRelease."/".$sec;
371         
372         if(!is_file($strID)){
373           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
374           $this->FAIdebianMirror="";
375           unset($this->buffer);
376           return(array());
377         }
378         $fp = fopen($strID,"r");
380         /* Parse every line and create an array */
381         while(!feof($fp)){
382           $str= fgets($fp,512);
383           $stra= split("\|",$str);
384           if(count($stra)==4){
385             $a_ret[$stra[0]] = $stra;
386           }
387         }
388         fclose($fp);
389           /* Save our Data, to avoid reading it again */
390       }
391       $this->buffer = $a_ret;
392       ksort($a_ret);
393       return($a_ret);
394     }else{
395       return $this->buffer;
396     }
397   }
400   /* Save to LDAP */
401   function save()
402   {
403     plugin::save();
404  
405     $ldap = $this->config->get_ldap_link();
407     $this->attrs['FAIpackage'] = array();
408     foreach($this->usedPackages as $pkg => $obj){
409       $this->attrs['FAIpackage'][] = $pkg;
410     } 
411   
412     $this->attrs['FAIdebianSection'] = array();
413     foreach($this->FAIdebianSection as $sec){
414       $this->attrs['FAIdebianSection'][] = $sec;
415     }
417     $ldap->cat($this->dn);
418     if($ldap->count()!=0){
419       /* Write FAIscript to ldap*/
420       $ldap->cd($this->dn);
421       $ldap->modify($this->attrs);
422     }else{
423       /* Write FAIscript to ldap*/
424       $ldap->cd($this->config->current['BASE']);
425       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
426       $ldap->cd($this->dn);
427       $ldap->add($this->attrs);
428     }
429     show_ldap_error($ldap->get_error());
430   }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>