Code

1688637e148cf0adb3d448ea50d77c022855a2e0
[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","section","mirror","release");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIpackageList");
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 $section          = ""; // The selected section
38   var $release          = ""; // selected release
39   var $mirror           = ""; // 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 $mirrors          = array();  // The combination of server/section/release
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     unset($this->attrs['FAIpackage']['count']);
63     foreach($this->attrs['FAIpackage'] as $pkg){
64       $this->usedPackages[] = $pkg;
65     }
66     $this->confDir = CONFIG_DIR."/fai/";
67     $this->FAIpackage = "test";
68   }
70   function execute()
71   {
72     /* Fill templating stuff */
73     $smarty= get_smarty();
74     $display= "";
76     /* Assign variables */
77     foreach($this->attributes as $attrs){
78       $smarty->assign($attrs,$this->$attrs);
79     }
81     $this->genMirror();
83     $availableServer = array();
84     foreach($this->mirrors as $mir){
85       if(($mir['section'] == $this->section)&&($mir['release']==$this->release)){
86         $availableServer[$mir['mirror']]=$mir['mirror'];
87       }
88     }
90     if(isset($_POST['Addpkg'])){
91       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->confDir.$this->mirror."/".$this->section."/".$this->release,$this->usedPackages);
92       $this->is_dialog =true;
93     }
94   
95     if(isset($_POST['CancelSubObject'])){
96       $this->dialog = false;
97       $this->is_dialog=false;
98     }
100     if(isset($_POST['SaveSubObject'])) {
101       $this->dialog->save_object();
102       if(count($this->dialog->check())){
103         foreach($this->dialog->check() as $msgs){
104         print_red($msgs);
105         }
106       }else{
107         $use = $this->dialog->save();
108         $this->usedPackages = $use;
109         $this->dialog = false;
110         $this->is_dialog=false;
111       }
112     }
113  
114     if($this->is_dialog){
115       return $this->dialog->execute();
116     }
117     
119     $smarty->assign("mirrors" ,$availableServer);
120     $smarty->assign("mirror"  ,$this->mirror);
121     $smarty->assign("sections",$this->sections);
122     $smarty->assign("section" ,$this->section);
123     $smarty->assign("releases",$this->releases);
124     $smarty->assign("release" ,$this->release);
125     $smarty->assign("usedPackages",$this->printUsedPackages());
126     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
127     return($display);
128   }
130   function genMirror(){
131     $confDir = $this->confDir; 
132     if(!is_readable($confDir)){
133       print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
134     }else{
135       
136       /* Try to catch all available mirrors 
137          Possibly check if each server is reachable
138        */
139       $this->mirrors= array();
140       $fd = opendir($confDir);
141       while($mirror = readdir($fd)){
142         if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
144           $mirrorDir = $confDir.$mirror."/";
146           $fe = opendir($mirrorDir);
147           while($section = readdir($fe)){
148             if((is_dir($mirrorDir.$section))&&(($section != "." )&&($section!=".."))){
150               $sectionDir = $mirrorDir.$section."/";
152               $ff = opendir($sectionDir);
153               while($release = readdir($ff)){
154                 if((is_file($sectionDir.$release))&&(($release != "." )&&($release!="..")&&(!preg_match("/.*\.in$/",$release)))){
155               
156                   $this->servers[$mirror]=$mirror;
157                   $this->sections[$section]=$section;
158                   $this->releases[$release]=$release;
160                   if(empty($this->mirror)){
161                     $this->mirror= $mirror;
162                   }
163                   if(empty($this->section)){
164                     $this->section = $section;
165                   }
166                   if(empty($this->release)){
167                     $this->release = $release;
168                   }                    
170                   $arr=array();
171                   $arr['mirror'] = $mirror;
172                   $arr['section']= $section; 
173                   $arr['release']= $release;
174  
175                   $this->mirrors[] = $arr ;
176                 }
177               }
178               fclose($ff);
179             }
180           }
181           fclose($fe);
182         }
183       }
184       fclose($fd);
185     }
188   }
190   /* Delete me, and all my subtrees
191    */
192   function remove_from_parent()
193   {
194     $ldap = $this->config->get_ldap_link();
195     $ldap->cd ($this->dn);
196     $ldap->rmdir_recursive($this->dn);
197     $this->handle_post_events("remove");    
198   }
201   /* Save data to object 
202    */
203   function save_object()
204   {
205     plugin::save_object();
206     foreach($this->attributes as $attrs){
207       if(isset($_POST[$attrs])){
208         $this->$attrs = $_POST[$attrs];
209       }
210     }
211   }
214   /* Check supplied data */
215   function check()
216   {
217     $message= array();
218     $str = utf8_encode("üöä");
219     if((preg_match("/[^a-z0-9".$str."\.,;:\-_\? ]/i",$this->description))){
220       $message[]=_("Please enter a valid description.");
221     }
222     return ($message);
223   }
225   function printUsedPackages(){
226     $a_ret=array(); 
227     if(is_array($this->usedPackages)) {
228       foreach($this->usedPackages as $usedName){
229         $a_ret[$usedName] = $usedName;
230       }
231     }
232     return($a_ret);
233   }
235   /* Save to LDAP */
236   function save()
237   {
238     plugin::save();
239  
240     $ldap = $this->config->get_ldap_link();
242     $this->attrs['FAIpackage'] = array();
243     foreach($this->usedPackages as $pkg => $obj){
244       $this->attrs['FAIpackage'][] = $pkg;
245     } 
247     unset($this->attrs['section']);
248     unset($this->attrs['mirror']);
249     unset($this->attrs['release']);
251     $ldap->cat($this->dn);
252     if($ldap->count()!=0){
253       /* Write FAIscript to ldap*/
254       $ldap->cd($this->dn);
255       $ldap->modify($this->attrs);
256     }else{
257       /* Write FAIscript to ldap*/
258       $ldap->cd($this->config->current['BASE']);
259       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
260       $ldap->cd($this->dn);
261       $ldap->add($this->attrs);
262     }
263     show_ldap_error($ldap->get_error());
264   }
267 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
268 ?>