Code

Removed duplicated save_object
[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     /* Save Configuration */
277     if(isset($_POST['SaveObjectConfig'])){
278       if($this->FAIstate != "freeze"){
279         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
280       }
281       $this->dialog = false;
282       $this->is_dialog=false;
283     }
285     /* cancel configuration */     
286     if(isset($_POST['CancelObjectConfig'])){
287       $this->dialog = false;
288       $this->is_dialog=false;
289     }
291     /* Display dialog */ 
292     if($this->is_dialog){
293       return $this->dialog->execute();
294     }
296     /* Assign section to smarty */
297     $strsec = "";
298     foreach($this->FAIdebianSection as $sec){
299       $strsec .= $sec." ";
300     }
302     foreach($this->attributes as $attr){
303       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
304     }
306     $smarty->assign("OptionsACL","");
308     $smarty->assign("releases",$this->releases);
309     $smarty->assign("release" ,$this->FAIdebianRelease);
310     $smarty->assign("sections",$this->sections);
311     $smarty->assign("section" ,$strsec);
312     $smarty->assign("usedPackages",$this->printUsedPackages());
313     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
314     return($display);
315   }
317   /* Delete me, and all my subtrees
318    */
319   function remove_from_parent()
320   {
321     $ldap = $this->config->get_ldap_link();
322     $ldap->cd ($this->dn);
323     $ldap->rmdir_recursive($this->dn);
324     show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
325     $this->handle_post_events("remove");    
326   }
329   /* Save data to object 
330    */
331   function save_object()
332   {
334     if($this->FAIstate == "freeze") return;  
335     plugin::save_object();
338     foreach($this->attributes as $attrs){
339       if(isset($_POST[$attrs])){
340         $this->$attrs = $_POST[$attrs];
341       }
342     }
343   }
346   /* Check supplied data */
347   function check()
348   {
349     /* Call common method to give check the hook */
350     $message= plugin::check();
352     if(count($this->usedPackages)==0){
353       $message[]=_("Please select a least one Package.");
354     }
356     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
357       $message[]=_("Please choose a valid combination for your repository setup.");
358     }
360     return ($message);
361   }
363   function printUsedPackages(){
364     $a_ret=array(); 
365     if(is_array($this->usedPackages)) {
366       foreach($this->usedPackages as $usedName){
368         $config = 0;
370         foreach($this->ConfiguredPackages as $name => $value){
371           if($name == $usedName){
372             $config ++;
373           }
374         }
376         $c_str ="";
377         if($config){
378           $c_str = " - "._("package is configured");
379         }
381         if(isset($this->list[$usedName][1])){
382           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
383         }else{
384           $a_ret[$usedName] = $usedName.$c_str;
385         }
386       }
387     }
388     return($a_ret);
389   }
391   function genPkgs(){
392     /* Generate a list off available packages for this mirror, section and release
393      */
394     /* Only read this file if it wasn't read before */
395     if($this->buffer==NULL){
396       $this->buffer=array();
397       $a_ret = array();
398       foreach($this->FAIdebianSection as $sec){
399         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
401         if(!is_file($strID)){
402           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
403           unset($this->buffer);
404           return(array());
405         }
406         $fp = fopen($strID,"r");
408         /* Parse every line and create an array */
409         while(!feof($fp)){
410           $str= fgets($fp,512);
411           $stra= split("\|",$str);
412           if(count($stra)==4){
413             $a_ret[$stra[0]] = $stra;
414           }
415         }
416         fclose($fp);
417         /* Save our Data, to avoid reading it again */
418       }
419       $this->buffer = $a_ret;
420       ksort($a_ret);
421       return($a_ret);
422     }else{
423       return $this->buffer;
424     }
425   }
428   /* Save to LDAP */
429   function save()
430   {
431     plugin::save();
433     $ldap = $this->config->get_ldap_link();
435     $this->attrs['FAIpackage'] = array();
436     foreach($this->usedPackages as $pkg => $obj){
437       $this->attrs['FAIpackage'][] = $pkg;
438     } 
440     $this->attrs['FAIdebianSection'] = array();
441     foreach($this->FAIdebianSection as $sec){
442       $this->attrs['FAIdebianSection'][] = $sec;
443     }
445     //    $this->attrs["FAIinstallMethod"]= "aptitude";
447     $ldap->cat($this->dn, array('dn'));
448     if($ldap->count()!=0){
449       /* Write FAIscript to ldap*/
450       $ldap->cd($this->dn);
451       $this->cleanup();
452       $ldap->modify ($this->attrs); 
454     }else{
455       /* Write FAIscript to ldap*/
456       $ldap->cd($this->config->current['BASE']);
457       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
458       $ldap->cd($this->dn);
459       $ldap->add($this->attrs);
460     }
461     show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
463     /* Do object tagging */
464     $this->handle_object_tagging();
465     $ldap->cd($this->dn);
467     /* Save Package configurations */
468     foreach($this->ConfiguredPackages as $pkgname => $attrs){
469       foreach($attrs as $name => $attr){
470       
471         $pkgattrs = array();
473         foreach($attr as $n=>$v){
474           if(empty($v)) $v = array();
475         }
477         /* Set attributes */
478         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
480         $pkgattrs['FAIpackage']           = $pkgname;
481         $pkgattrs['FAIvariable']          = $name;
482         $pkgattrs['FAIvariableType']      = $attr['Type'];
483         $pkgattrs['FAIvariableContent']   = $attr['Value'];
484         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
486         /* cehck if object already exists */
487         $ldap->cat($pkgdn,array("objectClass"));
489         /* Workaround for missing "gosaAdministrativeUnitTag" */
490         $attrs = $ldap->fetch();
491         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
492           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
493         }
494     
495         if($ldap->count()!=0){
496           $ldap->cd($pkgdn);
497           $this->cleanup();
498           $ldap->modify ($pkgattrs); 
500         }else{
501           $ldap->cd($this->config->current['BASE']);
502           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
503           $ldap->cd($pkgdn);
504           $ldap->add($pkgattrs);
505         }
506         show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
508         /* Handle tagging */
509         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
510       }
511     }
516   }
519 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
520 ?>