Code

dc22f0a0fdbab5db7fa7f644e93b530140894608
[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","FAIdebianSection","FAIdebianRelease","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 $FAIdebianSection          = ""; // The selected section
38   var $FAIdebianRelease          = ""; // selected release
39   var $FAIdebianMirror           = ""; // selected mirror
41   var $servers          = array();  // All available servers
42   var $sections         = array();  // All possible sections 
43   var $releases         = array();  // All release types
45   var $disableRepository = false;
47   var $mirrors          = array();  // The combination of server/section/release
48   var $confDir          = "";
49   var $usedPackages      = array();
51   function faiPackage ($config, $dn= NULL)
52   {
53     /* Load Attributes */
54     plugin::plugin ($config, $dn);
56     /* If "dn==new" we try to create a new entry
57      * Else we must read all objects from ldap which belong to this entry.
58      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
59      */
60     if($dn != "new"){
61       $this->dn =$dn;
62     }
64     if(isset($this->attrs['FAIdebianRelease'])){
65       $this->disableRepository=true;
66     }
68     unset($this->attrs['FAIpackage']['count']);
69     foreach($this->attrs['FAIpackage'] as $pkg){
70       $this->usedPackages[$pkg] = $pkg;
71     }
72     $this->confDir = CONFIG_DIR."/fai/";
73     $this->FAIpackage = array();
74   }
76   function execute()
77   {
78     /* Fill templating stuff */
79     $smarty= get_smarty();
80     $display= "";
82     /* Assign variables */
83     foreach($this->attributes as $attrs){
84       $smarty->assign($attrs,$this->$attrs);
85     }
87     $this->genMirror();
89     $availableServer = array();
90     foreach($this->mirrors as $mir){
91       if(($mir['section'] == $this->FAIdebianSection)&&($mir['release']==$this->FAIdebianRelease)){
92         $availableServer[$mir['mirror']]=$mir['mirror'];
93       }
94     }
96     if(isset($_POST['Addpkg'])){
97       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->confDir.$this->mirror."/".$this->section."/".$this->release,$this->usedPackages);
98       $this->is_dialog =true;
99     }
100  
101     if(isset($_POST['Delpkg'])){
102       unset($this->usedPackages[$_POST['usedPackages']]);
103     }
104  
105     if(isset($_POST['CancelSubObject'])){
106       $this->dialog = false;
107       $this->is_dialog=false;
108     }
110     if(isset($_POST['SaveSubObject'])) {
111       $this->dialog->save_object();
112       if(count($this->dialog->check())){
113         foreach($this->dialog->check() as $msgs){
114         print_red($msgs);
115         }
116       }else{
117         $use = $this->dialog->save();
118         $this->usedPackages = $use;
119         $this->dialog = false;
120         $this->is_dialog=false;
121         $this->disableRepository =true;
122       }
123     }
124  
125     if($this->is_dialog){
126       return $this->dialog->execute();
127     }
128     
129     if($this->disableRepository == true){
130       $smarty->assign("mirrorACL"  ," disabled ");
131       $smarty->assign("sectionACL" ," disabled ");
132       $smarty->assign("releaseACL" ," disabled ");
133     }else{
134       $smarty->assign("mirrorACL"  ,"");
135       $smarty->assign("sectionACL" ,"");
136       $smarty->assign("releaseACL" ,"");
137     }
138     $smarty->assign("mirrors" ,$availableServer);
139     $smarty->assign("mirror"  ,$this->FAIdebianMirror);
140     $smarty->assign("sections",$this->sections);
141     $smarty->assign("section" ,$this->FAIdebianSection);
142     $smarty->assign("releases",$this->releases);
143     $smarty->assign("release" ,$this->FAIdebianRelease);
144     $smarty->assign("usedPackages",$this->printUsedPackages());
145     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
146     return($display);
147   }
149   function genMirror(){
150     $confDir = $this->confDir; 
151     if(!is_readable($confDir)){
152       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
153     }else{
154       
155       /* Try to catch all available mirrors 
156          Possibly check if each server is reachable
157        */
158       $this->mirrors= array();
159       $fd = opendir($confDir);
160       while($mirror = readdir($fd)){
161         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
163           $mirrorDir = $confDir.$mirror."/";
165           $fe = opendir($mirrorDir);
166           while($section = readdir($fe)){
167             if((is_dir($mirrorDir.$section))&&(($section != "." )&&($section!=".."))){
169               $sectionDir = $mirrorDir.$section."/";
171               $ff = opendir($sectionDir);
172               while($release = readdir($ff)){
173                 if((is_file($sectionDir.$release))&&(($release != "." )&&($release!="..")&&(!preg_match("/.*\.in$/",$release)))){
174               
175                   $this->servers[$mirror]=$mirror;
176                   $this->sections[$section]=$section;
177                   $this->releases[$release]=$release;
179                   if(empty($this->mirror)){
180                     $this->mirror= $mirror;
181                   }
182                   if(empty($this->section)){
183                     $this->section = $section;
184                   }
185                   if(empty($this->release)){
186                     $this->release = $release;
187                   }                    
189                   $arr=array();
190                   $arr['mirror'] = $mirror;
191                   $arr['section']= $section; 
192                   $arr['release']= $release;
193  
194                   $this->mirrors[] = $arr ;
195                 }
196               }
197               fclose($ff);
198             }
199           }
200           fclose($fe);
201         }
202       }
203       fclose($fd);
204     }
207   }
209   /* Delete me, and all my subtrees
210    */
211   function remove_from_parent()
212   {
213     $ldap = $this->config->get_ldap_link();
214     $ldap->cd ($this->dn);
215     $ldap->rmdir_recursive($this->dn);
216     $this->handle_post_events("remove");    
217   }
220   /* Save data to object 
221    */
222   function save_object()
223   {
224     plugin::save_object();
225     foreach($this->attributes as $attrs){
226       if(isset($_POST[$attrs])){
227         $this->$attrs = $_POST[$attrs];
228       }
229     }
230   }
233   /* Check supplied data */
234   function check()
235   {
236     $message= array();
237     
238     if((empty($this->FAIdebianSection))||(empty($this->FAIdebianRelease))||(empty($this->FAIdebianMirror))){
239       $message[]=_("Please choose a valid combination for your repository setup.");
240     }
242     return ($message);
243   }
245   function printUsedPackages(){
246     $a_ret=array(); 
247     if(is_array($this->usedPackages)) {
248       foreach($this->usedPackages as $usedName){
249         $a_ret[$usedName] = $usedName;
250       }
251     }
252     return($a_ret);
253   }
255   /* Save to LDAP */
256   function save()
257   {
258     plugin::save();
259  
260     $ldap = $this->config->get_ldap_link();
262     $this->attrs['FAIpackage'] = array();
263     foreach($this->usedPackages as $pkg => $obj){
264       $this->attrs['FAIpackage'][] = $pkg;
265     } 
267     $ldap->cat($this->dn);
268     if($ldap->count()!=0){
269       /* Write FAIscript to ldap*/
270       $ldap->cd($this->dn);
271       $ldap->modify($this->attrs);
272     }else{
273       /* Write FAIscript to ldap*/
274       $ldap->cd($this->config->current['BASE']);
275       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
276       $ldap->cd($this->dn);
277       $ldap->add($this->attrs);
278     }
279     show_ldap_error($ldap->get_error());
280   }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>