Code

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