Code

* Fixed typo
[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     }  
104     /* Create one filter with all package names, 
105        instead of calling $ldap->search for every single package 
106      */
107     $PackageFilter = "";
108     foreach($this->usedPackages as $name){
109       $PackageFilter .= "(FAIpackage=".$name.")";
110     }
111     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
112     
113     /* Search for configuration objects */ 
114     $ldap = $this->config->get_ldap_link();
115     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
117     /* Walk through configurations and append them to our list of ConfiguredPackages */
118     while($attr = $ldap->fetch()){
119       $tmp =array(); 
120       $tmp['Name']  = $attr['FAIvariable'][0];
121       $tmp['Type']  = $attr['FAIvariableType'][0];
123       if (isset($attr['FAIvariableContent'][0])){
124         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
125           $tmp['Value'] = $attr['FAIvariableContent'][0];
126         }else{
127           $content = array();        
128           unset($attr['FAIvariableContent']['count']);
129           foreach($attr['FAIvariableContent'] as $attr){
130             $tmp['Value'][] = $attr;
131           }
132         }
133         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
134       }
135     }
137     if (isset($this->attrs['FAIdebianSection']['count'])){
138       unset($this->attrs['FAIdebianSection']['count']);
139     }
140     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
141       $this->FAIdebianSection = array();
142       foreach($this->attrs['FAIdebianSection'] as $sec){
143         $this->FAIdebianSection[$sec]=$sec;
144       }
145     }
147     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
148       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
149     }
150     $this->confDir = CONFIG_DIR."/fai/";
151     $this->FAIpackage = array();
154     $methods = array();
155     foreach($this->FAIinstallMethods as $method){
156       $methods[$method] = $method;
157     }
158     $this->FAIinstallMethods = $methods;
159     /* Check if we exist already - no need to ask for revisions, etc. then */
160     if ($this->dn != "new"){
161       $this->newDialogShown= true;
162     }
164   }
166   function execute()
167   {
168     /* Call parent execute */
170     plugin::execute();
172     /* Fill templating stuff */
173     $smarty= get_smarty();
174     $display= "";
176     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
178     if((!$this->is_account)&&(!$this->newDialogShown)){
180       if($this->dialog==NULL){
181         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
182         $this->is_dialog =true;
183       }
185       /* Assign posible changes, for mirror combinations */
186       $this->dialog->save_object();
188       /* Assign Repository settings*/ 
189       if(isset($_POST['SaveObjectNew'])){
190         $obj = $this->dialog->save();
192         $this->FAIdebianSection = $obj['FAIdebianSection'];
193         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
195         unset($this->dialog);
196         $this->dialog         = false;
197         $this->is_dialog      = false;
198         $this->newDialogShown = true;
199         $this->is_account     = true;
200       }
202       /* Draw dialog */
203       if($this->dialog){
204         $display=$this->dialog->execute();
205         return($display); 
206       }
207     }
209     /* Assign variables */
210     foreach($this->attributes as $attrs){
211       $smarty->assign($attrs,$this->$attrs);
212     }
214     /* Generate package list */
215     $this->list=$this->genPkgs();
217     /* + was pressed to open the package dialog */
218     if(isset($_POST['Addpkg'])){
219       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
220       $this->is_dialog =true;
221     }
223     /* Delte selected package */ 
224     if(isset($_POST['Delpkg'])){
225       if($this->FAIstate != "freeze"){
226         foreach($_POST['usedPackages'] as $del){
227           if(isset($this->usedPackages[$del])){
228             unset($this->usedPackages[$del]);
229           }
230         }
231       }
232     }
234     /* Abort package selection dialog */ 
235     if(isset($_POST['CancelSubObject'])){
236       $this->dialog = false;
237       $this->is_dialog=false;
238     }
240     /* attach new packages */
241     if(isset($_POST['SaveSubObject'])) {
242       if($this->FAIstate != "freeze"){
243         $this->dialog->save_object();
244         if(count($this->dialog->check())){
245           foreach($this->dialog->check() as $msgs){
246             print_red($msgs);
247           }
248         }else{
249           $use = $this->dialog->save();
250           $this->usedPackages = $use;
251           $this->dialog = false;
252           $this->is_dialog=false;
253           ksort($this->usedPackages);
254         }
255       }else{
256         $this->dialog = false;
257         $this->is_dialog=false;
258       }
259     }
261     /* Configuration dialog open*/
262     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
263       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
264       $pkg_config = array();
265       $pkg = $_POST['usedPackages'][0];
267       if(isset($this->ConfiguredPackages[$pkg])){
268         $pkg_config = $this->ConfiguredPackages[$pkg];
269       }
271       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
272       $this->dialog ->acl = $this->acl;
273       $this->is_dialog =true;
274     }
276     /* Configuration dialog open*/
277     if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
278       foreach($_POST['usedPackages'] as $pkg){
279         if (isset($this->usedPackages[$pkg])){
280           unset($this->usedPackages[$pkg]);
281           if (preg_match('/^-/', $pkg)){
282             $pkg= preg_replace('/^-/', '', $pkg);
283           } else {
284             $pkg= preg_replace('/^/', '-', $pkg);
285           }
286           $this->usedPackages[$pkg]= $pkg;
287         }
288       }
289     }
291     /* Save Configuration */
292     if(isset($_POST['SaveObjectConfig'])){
293       if($this->FAIstate != "freeze"){
294         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
295       }
296       $this->dialog = false;
297       $this->is_dialog=false;
298     }
300     /* cancel configuration */     
301     if(isset($_POST['CancelObjectConfig'])){
302       $this->dialog = false;
303       $this->is_dialog=false;
304     }
306     /* Display dialog */ 
307     if($this->is_dialog){
308       return $this->dialog->execute();
309     }
311     /* Assign section to smarty */
312     $strsec = "";
313     foreach($this->FAIdebianSection as $sec){
314       $strsec .= $sec." ";
315     }
317     foreach($this->attributes as $attr){
318       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
319     }
321     $smarty->assign("OptionsACL","");
323     $smarty->assign("releases",$this->releases);
324     $smarty->assign("release" ,$this->FAIdebianRelease);
325     $smarty->assign("sections",$this->sections);
326     $smarty->assign("section" ,$strsec);
327     $smarty->assign("usedPackages",$this->printUsedPackages());
328     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
329     return($display);
330   }
332   /* Delete me, and all my subtrees
333    */
334   function remove_from_parent()
335   {
336     $ldap = $this->config->get_ldap_link();
337     $ldap->cd ($this->dn);
338     $ldap->rmdir_recursive($this->dn);
339     show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
340     $this->handle_post_events("remove");    
341   }
344   /* Save data to object 
345    */
346   function save_object()
347   {
349     if($this->FAIstate == "freeze") return;  
350     plugin::save_object();
353     foreach($this->attributes as $attrs){
354       if(isset($_POST[$attrs])){
355         $this->$attrs = $_POST[$attrs];
356       }
357     }
358   }
361   /* Check supplied data */
362   function check()
363   {
364     /* Call common method to give check the hook */
365     $message= plugin::check();
367     if(count($this->usedPackages)==0){
368       $message[]=_("Please select a least one Package.");
369     }
371     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
372       $message[]=_("Please choose a valid combination for your repository setup.");
373     }
375     return ($message);
376   }
378   function printUsedPackages(){
379     $a_ret=array(); 
380     if(is_array($this->usedPackages)) {
381       foreach($this->usedPackages as $usedName){
383         $config = 0;
385         foreach($this->ConfiguredPackages as $name => $value){
386           if($name == $usedName){
387             $config ++;
388           }
389         }
391         $c_str ="";
392         if($config){
393           $c_str = " - "._("package is configured");
394         }
396         /* Adapt used name if we're marked for removal */
397         $dsc= "";
398         if (preg_match('/^-/', $usedName)){
399           $dsc= " - "._("Package marked for removal");
400           $usedName= preg_replace('/^-/', '! ', $usedName);
401         }
403         if(isset($this->list[$usedName][1])){
404           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str.$dsc;
405         }else{
406           $a_ret[$usedName] = $usedName.$c_str.$dsc;
407         }
408       }
409     }
410     return($a_ret);
411   }
413   function genPkgs(){
414     /* Generate a list off available packages for this mirror, section and release
415      */
416     /* Only read this file if it wasn't read before */
417     if($this->buffer==NULL){
418       $this->buffer=array();
419       $a_ret = array();
420       foreach($this->FAIdebianSection as $sec){
421         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
423         if(!is_file($strID)){
424           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
425           unset($this->buffer);
426           return(array());
427         }
428         $fp = fopen($strID,"r");
430         /* Parse every line and create an array */
431         while(!feof($fp)){
432           $str= fgets($fp,512);
433           $stra= split("\|",$str);
434           if(count($stra)==4){
435             $a_ret[$stra[0]] = $stra;
436           }
437         }
438         fclose($fp);
439         /* Save our Data, to avoid reading it again */
440       }
441       $this->buffer = $a_ret;
442       ksort($a_ret);
443       return($a_ret);
444     }else{
445       return $this->buffer;
446     }
447   }
450   /* Save to LDAP */
451   function save()
452   {
453     plugin::save();
455     $ldap = $this->config->get_ldap_link();
457     $this->attrs['FAIpackage'] = array();
458     foreach($this->usedPackages as $pkg => $obj){
459       $this->attrs['FAIpackage'][] = $pkg;
460     } 
462     $this->attrs['FAIdebianSection'] = array();
463     foreach($this->FAIdebianSection as $sec){
464       $this->attrs['FAIdebianSection'][] = $sec;
465     }
467     //    $this->attrs["FAIinstallMethod"]= "aptitude";
469     $ldap->cat($this->dn, array('dn'));
470     if($ldap->count()!=0){
471       /* Write FAIscript to ldap*/
472       $ldap->cd($this->dn);
473       $this->cleanup();
474       $ldap->modify ($this->attrs); 
476     }else{
477       /* Write FAIscript to ldap*/
478       $ldap->cd($this->config->current['BASE']);
479       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
480       $ldap->cd($this->dn);
481       $ldap->add($this->attrs);
482     }
483     show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
485     /* Do object tagging */
486     $this->handle_object_tagging();
487     $ldap->cd($this->dn);
489     /* Save Package configurations */
490     foreach($this->ConfiguredPackages as $pkgname => $attrs){
491       foreach($attrs as $name => $attr){
492       
493         $pkgattrs = array();
495         foreach($attr as $n=>$v){
496           if(empty($v)) $v = array();
497         }
499         /* Set attributes */
500         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
502         $pkgattrs['FAIpackage']           = $pkgname;
503         $pkgattrs['FAIvariable']          = $name;
504         $pkgattrs['FAIvariableType']      = $attr['Type'];
505         $pkgattrs['FAIvariableContent']   = $attr['Value'];
506         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
508         /* cehck if object already exists */
509         $ldap->cat($pkgdn,array("objectClass"));
511         /* Workaround for missing "gosaAdministrativeUnitTag" */
512         $attrs = $ldap->fetch();
513         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
514           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
515         }
516     
517         if($ldap->count()!=0){
518           $ldap->cd($pkgdn);
519           $this->cleanup();
520           $ldap->modify ($pkgattrs); 
522         }else{
523           $ldap->cd($this->config->current['BASE']);
524           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
525           $ldap->cd($pkgdn);
526           $ldap->add($pkgattrs);
527         }
528         show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
530         /* Handle tagging */
531         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
532       }
533     }
538   }
541 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
542 ?>