Code

aef188eb915c056fe31950fffd2c5ce386a0a8c7
[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 $mirrors          = array();  // The combination of server/release/section
46   var $confDir          = "";
47   var $usedPackages      = array();
49   function faiPackage ($config, $dn= NULL)
50   {
51     /* Load Attributes */
52     plugin::plugin ($config, $dn);
54     /* If "dn==new" we try to create a new entry
55      * Else we must read all objects from ldap which belong to this entry.
56      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
57      */
58     if($dn != "new"){
59       $this->dn =$dn;
60     }
62     if(isset($this->attrs['FAIpackage'])){
63       unset($this->attrs['FAIpackage']['count']);
64       foreach($this->attrs['FAIpackage'] as $pkg){
65         $this->usedPackages[$pkg] = $pkg;
66       }
67     }else{
68       $this->usedPackages = array();;
69     }  
70     $this->confDir = CONFIG_DIR."/fai/";
71     $this->FAIpackage = array();
72   }
74   function execute()
75   {
76     /* Fill templating stuff */
77     $smarty= get_smarty();
78     $display= "";
80     /* Assign variables */
81     foreach($this->attributes as $attrs){
82       $smarty->assign($attrs,$this->$attrs);
83     }
85     $disableRelease = false;
86     $disableMirror  = false;
87     $disableOptions = false;
88     /* Gray out handling */
89     if(empty($this->FAIdebianSection)){
90       $disableRelease = $disableMirror = $disableOptions = true;
91     }else
92     if(empty($this->FAIdebianRelease)){
93       $disableMirror = $disableOptions = true;
94     }else
95     if(empty($this->FAIdebianMirror)){
96       $disableOptions = true;
97     }
98    
99     $smarty->assign("ReleaseACL", "");
100     $smarty->assign("MirrorACL", "");
101     if($disableOptions){
102       $smarty->assign("OptionACL"," disabled ");
103     }else{
104       $smarty->assign("OptionACL", "");
105     } 
106     if($disableRelease){
107       $smarty->assign("ReleaseACL"," disabled ");
108     } 
109     if($disableMirror){
110       $smarty->assign("MirrorACL"," disabled ");
111     } 
112     $this->genMirror();
114     $availableServer = array();
115     foreach($this->mirrors as $mir){
116       if(($mir['release'] == $this->FAIdebianRelease)&&($mir['section']==$this->FAIdebianSection)){
117         $availableServer[$mir['mirror']]=$mir['mirror'];
118       }
119     }
121     if($disableMirror){
122       $availableServer[$mir['mirror']]=array("---");
123     }
125     if(isset($_POST['Addpkg'])){
126       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->confDir.$this->FAIdebianMirror."/".$this->FAIdebianRelease."/".$this->FAIdebianSection,$this->usedPackages);
127       $this->is_dialog =true;
128     }
129  
130     if(isset($_POST['Delpkg'])){
131       unset($this->usedPackages[$_POST['usedPackages']]);
132     }
133  
134     if(isset($_POST['CancelSubObject'])){
135       $this->dialog = false;
136       $this->is_dialog=false;
137     }
139     if(isset($_POST['SaveSubObject'])) {
140       $this->dialog->save_object();
141       if(count($this->dialog->check())){
142         foreach($this->dialog->check() as $msgs){
143         print_red($msgs);
144         }
145       }else{
146         $use = $this->dialog->save();
147         $this->usedPackages = $use;
148         $this->dialog = false;
149         $this->is_dialog=false;
150       }
151     }
152  
153     if($this->is_dialog){
154       return $this->dialog->execute();
155     }
157     $smarty->assign("mirrors" ,$availableServer);
158     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
159     $smarty->assign("releases",$this->releases);
160     $smarty->assign("release" ,$this->FAIdebianRelease);
161     $smarty->assign("sections",$this->sections);
162     $smarty->assign("section" ,$this->FAIdebianSection);
163     $smarty->assign("usedPackages",$this->printUsedPackages());
164     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
165     return($display);
166   }
168   function genMirror(){
169     $confDir = $this->confDir; 
170     if(!is_readable($confDir)){
171       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
172     }else{
173       
174       /* Try to catch all available mirrors 
175          Possibly check if each server is reachable
176        */
178       $this->servers= array();
179       $this->releases=array();  
180       $this->sections=array(); 
181       $this->mirrors= array();
182       $fd = opendir($confDir);
183       while($mirror = readdir($fd)){
184         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
186           $mirrorDir = $confDir.$mirror."/";
188           $fe = opendir($mirrorDir);
189           while($release = readdir($fe)){
190             if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
192               $releaseDir = $mirrorDir.$release."/";
194               $ff = opendir($releaseDir);
195               while($section = readdir($ff)){
196                 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
197               
198                   $this->servers[$mirror]=$mirror;
199                   $this->releases[$release]=$release;
200                   $this->sections[$section]=$section;
202                   if(empty($this->FAIdebianRelease)){
203                     $this->servers= array("---");
204                   }
205                   if(empty($this->FAIdebianSection)){
206                     $this->releases= array("---");
207                   }
208                   if(empty($this->FAIdebianSection)){
209                     $this->FAIdebianSection = $section;
210                   }                    
212                   $arr=array();
213                   $arr['mirror'] = $mirror;
214                   $arr['release']= $release; 
215                   $arr['section']= $section;
216  
217                   $this->mirrors[] = $arr ;
218                 }
219               }
220               fclose($ff);
221             }
222           }
223           fclose($fe);
224         }
225       }
226       fclose($fd);
227     }
230   }
232   /* Delete me, and all my subtrees
233    */
234   function remove_from_parent()
235   {
236     $ldap = $this->config->get_ldap_link();
237     $ldap->cd ($this->dn);
238     $ldap->rmdir_recursive($this->dn);
239     $this->handle_post_events("remove");    
240   }
243   /* Save data to object 
244    */
245   function save_object()
246   {
247     plugin::save_object();
248     foreach($this->attributes as $attrs){
249       if(isset($_POST[$attrs])){
250         $this->$attrs = $_POST[$attrs];
251       }
252     }
253   }
256   /* Check supplied data */
257   function check()
258   {
259     $message= array();
260   
261     if(count($this->usedPackages)==0){
262       $message[]=_("Please select a least one Package.");
263     }
264   
265     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
266       $message[]=_("Please choose a valid combination for your repository setup.");
267     }
269     return ($message);
270   }
272   function printUsedPackages(){
273     $a_ret=array(); 
274     if(is_array($this->usedPackages)) {
275       foreach($this->usedPackages as $usedName){
276         $a_ret[$usedName] = $usedName;
277       }
278     }
279     return($a_ret);
280   }
282   /* Save to LDAP */
283   function save()
284   {
285     plugin::save();
286  
287     $ldap = $this->config->get_ldap_link();
289     $this->attrs['FAIpackage'] = array();
290     foreach($this->usedPackages as $pkg => $obj){
291       $this->attrs['FAIpackage'][] = $pkg;
292     } 
294     $ldap->cat($this->dn);
295     if($ldap->count()!=0){
296       /* Write FAIscript to ldap*/
297       $ldap->cd($this->dn);
298       $ldap->modify($this->attrs);
299     }else{
300       /* Write FAIscript to ldap*/
301       $ldap->cd($this->config->current['BASE']);
302       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
303       $ldap->cd($this->dn);
304       $ldap->add($this->attrs);
305     }
306     show_ldap_error($ldap->get_error());
307   }
310 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
311 ?>