Code

Global Base
[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", "FAIinstallMethod");
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   var $ConfiguredPackages = array();
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIdebianRelease          = ""; // The selected release
40   var $FAIdebianSection          = ""; // selected section
41   var $FAIinstallMethod          = "aptitude"; // hard coded
42   var $mirror                    = ""; // selected mirror
44   var $servers          = array();  // All available servers
45   var $releases         = array();  // All possible releases 
46   var $sections         = array();  // All section types
48   var $list             = NULL;
50   var $mirrors          = array();  // The combination of server/release/section
51   var $confDir          = "";
52   var $usedPackages     = array();
53   var $buffer           = NULL; 
54   var $strID            ="";
55   var $newDialogShown   =false;
57   var $FAIstate         = "";
59   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
60       "dselect-upgrade", "taskinst", "taskrm",
61       "hold", "clean", "aptitude", "aptitude-r",
62       "pending", "dpkgc" );
65   function faiPackage ($config, $dn= NULL)
66   {
67     /* Load Attributes */
68     plugin::plugin ($config, $dn);
70     $this->acl ="#all#";
72     /* If "dn==new" we try to create a new entry
73      * Else we must read all objects from ldap which belong to this entry.
74      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
75      */
76     if($dn != "new"){
77       $this->dn =$dn;
79       /* Set acls
80        */
81       $ui   = get_userinfo();
82       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
83       $acli = get_module_permission($acl, "FAIclass", $this->dn);
84       $this->acl=$acli;
86       /* Get FAIstate
87        */
88       if(isset($this->attrs['FAIstate'][0])){
89         $this->FAIstate = $this->attrs['FAIstate'][0];
90       }
91     }
93     if(isset($this->attrs['FAIpackage'])){
94       unset($this->attrs['FAIpackage']['count']);
95       foreach($this->attrs['FAIpackage'] as $pkg){
96         $this->usedPackages[$pkg] = $pkg;
97       }
98       ksort($this->usedPackages);
99     }else{
100       $this->usedPackages = array();
101     }  
103     $ldap = $this->config->get_ldap_link();
104     foreach($this->usedPackages as $name){
105       $ldap->search("(&(objectClass=FAIdebconfInfo)(FAIpackage=".$name."))",array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection",""));
106       while($attr = $ldap->fetch()){
108         $tmp =array(); 
109         $tmp['Name']  = $attr['FAIvariable'][0];
110         $tmp['Type']  = $attr['FAIvariableType'][0];
112         if (isset($attr['FAIvariableContent'][0])){
113           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
114             $tmp['Value'] = $attr['FAIvariableContent'][0];
115           }else{
116             $content = array();        
117             unset($attr['FAIvariableContent']['count']);
118             foreach($attr['FAIvariableContent'] as $attr){
119               $tmp['Value'][] = $attr;
120             }
121           }
122           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
123         }
124       }
125     }
127     if (isset($this->attrs['FAIdebianSection']['count'])){
128       unset($this->attrs['FAIdebianSection']['count']);
129     }
130     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
131       $this->FAIdebianSection = array();
132       foreach($this->attrs['FAIdebianSection'] as $sec){
133         $this->FAIdebianSection[$sec]=$sec;
134       }
135     }
137     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
138       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
139     }
140     $this->confDir = CONFIG_DIR."/fai/";
141     $this->FAIpackage = array();
144     $methods = array();
145     foreach($this->FAIinstallMethods as $method){
146       $methods[$method] = $method;
147     }
148     $this->FAIinstallMethods = $methods;
149     /* Check if we exist already - no need to ask for revisions, etc. then */
150     if ($this->dn != "new"){
151       $this->newDialogShown= true;
152     }
154   }
156   function execute()
157   {
158     /* Call parent execute */
160     plugin::execute();
162     /* Fill templating stuff */
163     $smarty= get_smarty();
164     $display= "";
166     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
168     if((!$this->is_account)&&(!$this->newDialogShown)){
170       if($this->dialog==NULL){
171         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
172         $this->is_dialog =true;
173       }
175       /* Assign posible changes, for mirror combinations */
176       $this->dialog->save_object();
178       /* Assign Repository settings*/ 
179       if(isset($_POST['SaveObjectNew'])){
180         $obj = $this->dialog->save();
182         $this->FAIdebianSection = $obj['FAIdebianSection'];
183         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
185         unset($this->dialog);
186         $this->dialog         = false;
187         $this->is_dialog      = false;
188         $this->newDialogShown = true;
189         $this->is_account     = true;
190       }
192       /* Draw dialog */
193       if($this->dialog){
194         $display=$this->dialog->execute();
195         return($display); 
196       }
197     }
199     /* Assign variables */
200     foreach($this->attributes as $attrs){
201       $smarty->assign($attrs,$this->$attrs);
202     }
204     /* Generate package list */
205     $this->list=$this->genPkgs();
207     /* + was pressed to open the package dialog */
208     if(isset($_POST['Addpkg'])){
209       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
210       $this->is_dialog =true;
211     }
213     /* Delte selected package */ 
214     if(isset($_POST['Delpkg'])){
215       if($this->FAIstate != "freeze"){
216         foreach($_POST['usedPackages'] as $del){
217           if(isset($this->usedPackages[$del])){
218             unset($this->usedPackages[$del]);
219           }
220         }
221       }
222     }
224     /* Abort package selection dialog */ 
225     if(isset($_POST['CancelSubObject'])){
226       $this->dialog = false;
227       $this->is_dialog=false;
228     }
230     /* attach new packages */
231     if(isset($_POST['SaveSubObject'])) {
232       if($this->FAIstate != "freeze"){
233         $this->dialog->save_object();
234         if(count($this->dialog->check())){
235           foreach($this->dialog->check() as $msgs){
236             print_red($msgs);
237           }
238         }else{
239           $use = $this->dialog->save();
240           $this->usedPackages = $use;
241           $this->dialog = false;
242           $this->is_dialog=false;
243           ksort($this->usedPackages);
244         }
245       }else{
246         $this->dialog = false;
247         $this->is_dialog=false;
248       }
249     }
251     /* Configuration dialog open*/
252     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
253       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
254       $pkg_config = array();
255       $pkg = $_POST['usedPackages'][0];
257       if(isset($this->ConfiguredPackages[$pkg])){
258         $pkg_config = $this->ConfiguredPackages[$pkg];
259       }
261       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
262       $this->dialog ->acl = $this->acl;
263       $this->is_dialog =true;
264     }
266     /* Save Configuration */
267     if(isset($_POST['SaveObjectConfig'])){
268       if($this->FAIstate != "freeze"){
269         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
270       }
271       $this->dialog = false;
272       $this->is_dialog=false;
273     }
275     /* cancel configuration */     
276     if(isset($_POST['CancelObjectConfig'])){
277       $this->dialog = false;
278       $this->is_dialog=false;
279     }
281     /* Display dialog */ 
282     if($this->is_dialog){
283       return $this->dialog->execute();
284     }
286     /* Assign section to smarty */
287     $strsec = "";
288     foreach($this->FAIdebianSection as $sec){
289       $strsec .= $sec." ";
290     }
292     foreach($this->attributes as $attr){
293       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
294     }
296     $smarty->assign("OptionsACL","");
298     $smarty->assign("releases",$this->releases);
299     $smarty->assign("release" ,$this->FAIdebianRelease);
300     $smarty->assign("sections",$this->sections);
301     $smarty->assign("section" ,$strsec);
302     $smarty->assign("usedPackages",$this->printUsedPackages());
303     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
304     return($display);
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   {
323     if($this->FAIstate == "freeze") return;  
324     plugin::save_object();
327     foreach($this->attributes as $attrs){
328       if(isset($_POST[$attrs])){
329         $this->$attrs = $_POST[$attrs];
330       }
331     }
332   }
335   /* Check supplied data */
336   function check()
337   {
338     /* Call common method to give check the hook */
339     $message= plugin::check();
341     if(count($this->usedPackages)==0){
342       $message[]=_("Please select a least one Package.");
343     }
345     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
346       $message[]=_("Please choose a valid combination for your repository setup.");
347     }
349     return ($message);
350   }
352   function printUsedPackages(){
353     $a_ret=array(); 
354     if(is_array($this->usedPackages)) {
355       foreach($this->usedPackages as $usedName){
357         $config = 0;
359         foreach($this->ConfiguredPackages as $name => $value){
360           if($name == $usedName){
361             $config ++;
362           }
363         }
365         $c_str ="";
366         if($config){
367           $c_str = " - "._("package is configured");
368         }
370         if(isset($this->list[$usedName][1])){
371           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
372         }else{
373           $a_ret[$usedName] = $usedName.$c_str;
374         }
375       }
376     }
377     return($a_ret);
378   }
380   function genPkgs(){
381     /* Generate a list off available packages for this mirror, section and release
382      */
383     /* Only read this file if it wasn't read before */
384     if($this->buffer==NULL){
385       $this->buffer=array();
386       $a_ret = array();
387       foreach($this->FAIdebianSection as $sec){
388         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
390         if(!is_file($strID)){
391           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
392           unset($this->buffer);
393           return(array());
394         }
395         $fp = fopen($strID,"r");
397         /* Parse every line and create an array */
398         while(!feof($fp)){
399           $str= fgets($fp,512);
400           $stra= split("\|",$str);
401           if(count($stra)==4){
402             $a_ret[$stra[0]] = $stra;
403           }
404         }
405         fclose($fp);
406         /* Save our Data, to avoid reading it again */
407       }
408       $this->buffer = $a_ret;
409       ksort($a_ret);
410       return($a_ret);
411     }else{
412       return $this->buffer;
413     }
414   }
417   /* Save to LDAP */
418   function save()
419   {
420     plugin::save();
422     $ldap = $this->config->get_ldap_link();
424     $this->attrs['FAIpackage'] = array();
425     foreach($this->usedPackages as $pkg => $obj){
426       $this->attrs['FAIpackage'][] = $pkg;
427     } 
429     $this->attrs['FAIdebianSection'] = array();
430     foreach($this->FAIdebianSection as $sec){
431       $this->attrs['FAIdebianSection'][] = $sec;
432     }
434     //    $this->attrs["FAIinstallMethod"]= "aptitude";
436     $ldap->cat($this->dn);
437     if($ldap->count()!=0){
438       /* Write FAIscript to ldap*/
439       $ldap->cd($this->dn);
440       $this->cleanup();
441       $ldap->modify ($this->attrs); 
443     }else{
444       /* Write FAIscript to ldap*/
445       $ldap->cd($this->config->current['BASE']);
446       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
447       $ldap->cd($this->dn);
448       $ldap->add($this->attrs);
449     }
450     show_ldap_error($ldap->get_error());
452     /* Save Package configurations */
453     foreach($this->ConfiguredPackages as $pkgname => $attrs){
454       foreach($attrs as $name => $attr){
456         foreach($attr as $n=>$v){
457           if(empty($v)) $v = array();
458         }
460         /* Set attributes */
461         $pkgattrs['objectClass']          = "FAIdebconfInfo";
462         $pkgattrs['FAIpackage']           = $pkgname;
463         $pkgattrs['FAIvariable']          = $name;
464         $pkgattrs['FAIvariableType']      = $attr['Type'];
465         $pkgattrs['FAIvariableContent']   = $attr['Value'];
466         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
468         /* cehck if object already exists */
469         $ldap->cat($pkgdn);
470         if($ldap->count()!=0){
471           $ldap->cd($pkgdn);
472           $this->cleanup();
473           $ldap->modify ($pkgattrs); 
475         }else{
476           $ldap->cd($this->config->current['BASE']);
477           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
478           $ldap->cd($pkgdn);
479           $ldap->add($pkgattrs);
480         }
481         show_ldap_error($ldap->get_error());
482       }
483     }
488   }
491 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
492 ?>