Code

Updated FAI Package stuff
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "";
16   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description"); 
23   var $sub64coded       = array();
25   var $ConfiguredPackages = array();
27   /* Specific attributes */
28   var $cn               = "";       // The class name for this object
29   var $description      = "";       // The description for this set of partitions
30   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
31   var $SubObjects       = array();  // All leafobjects of this object
33   var $FAIdebianRelease          = ""; // The selected release
34   var $FAIdebianSection          = array(); // selected section
35   var $FAIinstallMethod          = "aptitude"; // hard coded
36   var $mirror                    = ""; // selected mirror
38   var $servers          = array();  // All available servers
39   var $releases         = array();  // All possible releases 
40   var $sections         = array();  // All section types
42   var $list             = NULL;
44   var $mirrors          = array();  // The combination of server/release/section
45   var $confDir          = "";
46   var $usedPackages     = array();
47   var $buffer           = NULL; 
48   var $strID            ="";
49   var $newDialogShown   =false;
51   var $FAIstate         = "";
52   var $view_logged      = FALSE;
53   var $base;
55   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
56       "dselect-upgrade", "taskinst", "taskrm",
57       "hold", "clean", "aptitude", "aptitude-r",
58       "pending", "dpkgc" );
61   function faiPackage (&$config, $dn= NULL)
62   {
63     /* Load Attributes */
64     plugin::plugin ($config, $dn);
66     /* If "dn==new" we try to create a new entry
67      * Else we must read all objects from ldap which belong to this entry.
68      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
69      */
70     if($dn != "new"){
71       $this->dn =$dn;
73       /* Get FAIstate */
74       if(isset($this->attrs['FAIstate'][0])){
75         $this->FAIstate = $this->attrs['FAIstate'][0];
76       }
78       /* Check if there are already some packages in this list 
79        */
80       $this->usedPackages = array();
81       if(isset($this->attrs['FAIpackage'])){
82         unset($this->attrs['FAIpackage']['count']);
83         foreach($this->attrs['FAIpackage'] as $pkg){
84           $this->usedPackages[$pkg] = $pkg;
85         }
86         ksort($this->usedPackages);
87       }  
89       /* Fetch all package configurations from ldap 
90        */
91       $PackageFilter = "";
92       foreach($this->usedPackages as $name){
93         $PackageFilter .= "(FAIpackage=".$name.")";
94       }
95       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
97       /* Search for configuration objects */ 
98       $ldap = $this->config->get_ldap_link();
99       $ldap->cd($this->dn);
100       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
101             "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
103       /* Walk through configurations and append them to our list of ConfiguredPackages 
104        */
105       while($attr = $ldap->fetch()){
107         /* Skip objects, that are tagged as removed */
108         if(isset($object['FAIstate'][0])){
109           if(preg_match("/removed$/",$attr['FAIstate'][0])){
110             continue;
111           }
112         }
114         $tmp =array(); 
115         $tmp['Name']  = $attr['FAIvariable'][0];
116         $tmp['Type']  = $attr['FAIvariableType'][0];
118         if (isset($attr['FAIvariableContent'][0])){
119           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
120             $tmp['Value'] = $attr['FAIvariableContent'][0];
121           }else{
122             $content = array();        
123             unset($attr['FAIvariableContent']['count']);
124             foreach($attr['FAIvariableContent'] as $attr){
125               $tmp['Value'][] = $attr;
126             }
127           }
128           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
129         }
130       }
132       if (isset($this->attrs['FAIdebianSection']['count'])){
133         unset($this->attrs['FAIdebianSection']['count']);
134       }
135       if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
136         $this->FAIdebianSection = array();
137         foreach($this->attrs['FAIdebianSection'] as $sec){
138           $this->FAIdebianSection[$sec]=$sec;
139         }
140       }
142       if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
143         $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
144       }
145       $this->confDir = CONFIG_DIR."/fai/";
146       $this->FAIpackage = array();
148     } // ENDE  dn != new  
150     $methods = array();
151     foreach($this->FAIinstallMethods as $method){
152       $methods[$method] = $method;
153     }
154     $this->FAIinstallMethods = $methods;
156     /* Check if we exist already - no need to ask for revisions, etc. then */
157     if ($this->dn != "new"){
158       $this->newDialogShown= true;
159     }
160   }
163   function execute()
164   {
165     /* Call parent execute */
166     plugin::execute();
168     if($this->is_account && !$this->view_logged){
169       $this->view_logged = TRUE;
170       new log("view","fai/".get_class($this),$this->dn);
171     }
173     /* Fill templating stuff */
174     $smarty= get_smarty();
175     $display= "";
177     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
179     if(!$this->is_account){
181       /* Assemble release name */
182       $faifilter = session::get('faifilter');
183       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
184       $tmp= preg_replace('/ou=/', '', $tmp);
185       $rev= array_reverse(split(',', $tmp));
186       $this->FAIdebianRelease= "";
187       foreach ($rev as $part){
188         $this->FAIdebianRelease.= "/$part";
189       }
190       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
192       /* Assemble sections */
193       $repos= $this->getServerInfos();
194       if(isset($repos[$this->FAIdebianRelease])){
195         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
196         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
197       }
199       /* Assign Repository settings*/ 
200       $this->is_account     = true;
201     }
203     /* Assign variables */
204     foreach($this->attributes as $attrs){
205       $smarty->assign($attrs,$this->$attrs);
206     }
208     /* Generate package list */
209     $this->list= $this->genPkgs();
211     /* + was pressed to open the package dialog */
212     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
213       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
214       $this->is_dialog =true;
215     }
217     /* Delete selected package */ 
218     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
219       if($this->FAIstate != "freeze"){
220         foreach($_POST['usedPackages'] as $del){
221           if(isset($this->usedPackages[$del])){
222             unset($this->usedPackages[$del]);
223           }
224         }
225       }
226     }
228     /* Abort package selection dialog */ 
229     if(isset($_POST['CancelSubObject'])){
230       $this->dialog = false;
231       $this->is_dialog=false;
232     }
234     /* attach new packages */
235     if(isset($_POST['SaveSubObject'])) {
236       if($this->FAIstate != "freeze"){
237         $this->dialog->save_object();
238         if(count($this->dialog->check())){
239           foreach($this->dialog->check() as $msgs){
240             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
241           }
242         }else{
243           $use = $this->dialog->save();
244           $this->usedPackages = $use;
245           $this->dialog = false;
246           $this->is_dialog=false;
247           ksort($this->usedPackages);
248         }
249       }else{
250         $this->dialog = false;
251         $this->is_dialog=false;
252       }
253     }
255     /* Configuration dialog open*/
256     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
257       $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
258       $pkg_config = array();
259       $pkg = $_POST['usedPackages'][0];
261       if(isset($this->ConfiguredPackages[$pkg])){
262         $pkg_config = $this->ConfiguredPackages[$pkg];
263       }
265       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
266       $this->is_dialog =true;
267     }
269     /* Configuration dialog open*/
270     if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
271       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
272         foreach($_POST['usedPackages'] as $pkg){
273           if (isset($this->usedPackages[$pkg])){
274             unset($this->usedPackages[$pkg]);
275             if (preg_match('/^-/', $pkg)){
276               $pkg= preg_replace('/^-/', '', $pkg);
277             } else {
278               $pkg= preg_replace('/^/', '-', $pkg);
279             }
280             $this->usedPackages[$pkg]= $pkg;
281           }
282         }
283       }
284     }
286     /* Save Configuration */
287     if(isset($_POST['SaveObjectConfig'])){
288       if($this->FAIstate != "freeze"){
289         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
290       }
291       $this->dialog = false;
292       $this->is_dialog=false;
293     }
295     /* cancel configuration */     
296     if(isset($_POST['CancelObjectConfig'])){
297       $this->dialog = false;
298       $this->is_dialog=false;
299     }
301     /* Display dialog */ 
302     if($this->is_dialog){
303       return $this->dialog->execute();
304     }
306     /* Assign section to smarty */
307     $strsec = "";
308     foreach($this->FAIdebianSection as $sec){
309       $strsec .= $sec." ";
310     }
312     $tmp = $this->plInfo();
313     foreach($tmp['plProvidedAcls'] as $name => $translated){
314       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
315     }
317     $smarty->assign("releases",$this->releases);
318     $smarty->assign("release" ,$this->FAIdebianRelease);
319     $smarty->assign("sections",$this->sections);
320     $smarty->assign("section" ,$strsec);
321     $smarty->assign("usedPackages",$this->printUsedPackages());
322     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
323     return($display);
324   }
326   /* Delete me, and all my subtrees
327    */
328   function remove_from_parent()
329   {
330     $ldap = $this->config->get_ldap_link();
331     $ldap->cd ($this->dn);
333     $faifilter = session::get('faifilter');
334     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
335     if($faifilter['branch'] == "main"){
336       $use_dn = $this->dn;
337     }
339     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
341     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
343     foreach($this->ConfiguredPackages as $pkgname => $attrs){
344       foreach($attrs as $name => $attr){
345         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
346         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
347         if($faifilter['branch'] == "main"){
348           $use_dn = $obj['dn'];
349         }
350         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
351       }
352     }
353     $this->handle_post_events("remove");
354   }
357   /* Save data to object 
358    */
359   function save_object()
360   {
361     if($this->FAIstate == "freeze") return;  
362     plugin::save_object();
363   }
366   /* Check supplied data */
367   function check()
368   {
369     /* Call common method to give check the hook */
370     $message= plugin::check();
372     if(count($this->usedPackages)==0){
373       $message[]= _("Please select a least one package!");
374     }
376     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
377       $message[]=_("Please choose a valid release/section combination for your repository setup!");
378     }
380     return ($message);
381   }
383   function printUsedPackages(){
384     $a_ret=array(); 
385     if(is_array($this->usedPackages)) {
386       foreach($this->usedPackages as $usedName){
388         $config = 0;
390         foreach($this->ConfiguredPackages as $name => $value){
391           if($name == $usedName){
392             $config ++;
393           }
394         }
396         $c_str ="";
397         if($config){
398           $c_str = " - "._("package is configured");
399         }
401         /* Adapt used name if we're marked for removal */
402         $dsc= "";
403         if (preg_match('/^-/', $usedName)){
404           $dsc= " - "._("Package marked for removal");
405           // Generally a bad idea here, because the toggel triggers on -, not on !
406           //$usedName= preg_replace('/^-/', '! ', $usedName);
407         }else{
408           $usedName2= $usedName;
409         }
411         if(isset($this->list[$usedName][1])){
412           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
413         }else{
414           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
415         }
416       }
417     }
418     return($a_ret);
419   }
421   function genPkgs()
422   {
423     if($this->buffer === NULL || !count($this->buffer)){
424       $this->buffer = array();
425       $q = new gosaSupportDaemon();
426       $ret = $q->FAI_get_packages($this->FAIdebianRelease);
427       foreach($ret as $attr){
428         $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
429       }
430     }
431     return $this->buffer;
432  
433 #   /* Generate a list off available packages for this mirror, section and release
434 #    */
435 #   /* Only read this file if it wasn't read before */
436 #   $this->buffer = NULL;
437 #   if($this->buffer === NULL){
438 #     $this->buffer=array();
439 #     $a_ret = array();
440 #     foreach($this->FAIdebianSection as $sec){
441 #       $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
443 #       if(!is_file($strID)){
444 #         msg_dialog::display(_("Error"), sprintf(_("Package file '%s' does not exist!"), $strID), ERROR_DIALOG);
445 #         unset($this->buffer);
446 #         return(array());
447 #       }
448 #       $fp = fopen($strID,"r");
450 #       /* Parse every line and create an array */
451 #       while(!feof($fp)){
452 #         $str= fgets($fp,512);
453 #         $stra= split("\|",$str);
454 #         if(count($stra)==4){
455 #           $a_ret[$stra[0]] = $stra;
456 #         break;
457 #         }
458 #       }
459 #       fclose($fp);
460 #       /* Save our Data, to avoid reading it again */
461 #     }
462 #     $this->buffer = $a_ret;
463 #     ksort($a_ret);
464 #     print_a($this->buffer);
465 #     return($a_ret);
466 #   }else{
467 #     print_a($this->buffer);
468 #     return $this->buffer;
469 #   }
470   }
473   /* Save to LDAP */
474   function save()
475   {
477     /* Assemble release name */
478     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
479       $faifilter = session::get('faifilter');
480       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
481       $tmp= preg_replace('/ou=/', '', $tmp);
482       $rev= array_reverse(split(',', $tmp));
483       $this->FAIdebianRelease= "";
484       foreach ($rev as $part){
485         $this->FAIdebianRelease.= "/$part";
486       }
487       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
488     }
490     plugin::save();
492     $ldap = $this->config->get_ldap_link();
494     $this->attrs['FAIpackage'] = array();
495     foreach($this->usedPackages as $pkg => $obj){
496       $this->attrs['FAIpackage'][] = $pkg;
497     } 
499     $this->attrs['FAIdebianSection'] = array();
500     foreach($this->FAIdebianSection as $sec){
501       $this->attrs['FAIdebianSection'][] = $sec;
502     }
504     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
505     if (!$ldap->success()){
506       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
507     }
508     
509     if($this->initially_was_account){
510       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
511     }else{
512       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
513     }
515     $ldap->cd($this->dn);
517     /* Save Package configurations */
518     foreach($this->ConfiguredPackages as $pkgname => $attrs){
519       foreach($attrs as $name => $attr){
520       
521         $pkgattrs = array();
523         foreach($attr as $n=>$v){
524           if(empty($v)) $v = array();
525         }
527         /* Set attributes */
528         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
530         $pkgattrs['FAIpackage']           = $pkgname;
531         $pkgattrs['FAIvariable']          = $name;
532         $pkgattrs['FAIvariableType']      = $attr['Type'];
533         $pkgattrs['FAIvariableContent']   = $attr['Value'];
534         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
536         /* Tag object */
537         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
539         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
540           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
541         }
542       }
543     }
544   }
546   /* Return plugin informations for acl handling */ 
547   static function plInfo()
548   {
549     return (array( 
550           "plShortName" => _("Package"),
551           "plDescription" => _("FAI Package list"),
552           "plSelfModify"  => FALSE,
553           "plDepends"     => array(),
554           "plPriority"    => 28,
555           "plSection"     => array("administration"),
556           "plCategory"    => array("fai"),
557           "plProvidedAcls" => array(
558             "cn"                => _("Name"),
559             "description"       => _("Description"),
560             "FAIpackage"        => _("Packages"),
561             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
562             "FAIinstallMethod"  => _("Install Method"),
563             "FAIdebconfInfo"    => _("Package configuration"),
564             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
565           ));
566   }
571   function PrepareForCopyPaste($source)
572   {
573     plugin::PrepareForCopyPaste($source);
575     if(isset($source['FAIstate'][0])){
576       $this->FAIstate = $source['FAIstate'][0];
577     }
579     $this->FAIdebianRelease = "ClearFromCopyPaste";
581     if(isset($source['FAIpackage'])){
582       unset($source['FAIpackage']['count']);
583       foreach($source['FAIpackage'] as $pkg){
584         $this->usedPackages[$pkg] = $pkg;
585       }
586       ksort($this->usedPackages);
587     }else{
588       $this->usedPackages = array();
589     }
591     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
592       $this->FAIdebianSection = array();
593       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
594         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
595       }
596     }
598     /* Create one filter with all package names, 
599        instead of calling $ldap->search for every single package 
600      */
601     $PackageFilter = "";
602     foreach($this->usedPackages as $name){
603       $PackageFilter .= "(FAIpackage=".$name.")";
604     }
605     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
607     /* Search for configuration objects */ 
608     $ldap = $this->config->get_ldap_link();
609     $ldap->cd($source['dn']);
610     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
612     /* Walk through configurations and append them to our list of ConfiguredPackages */
613     while($attr = $ldap->fetch()){
615       /* Skip objects, that are tagged as removed */
616       if(isset($object['FAIstate'][0])){
617         if(preg_match("/removed$/",$attr['FAIstate'][0])){
618           continue;
619         }
620       }
622       $tmp =array(); 
623       $tmp['Name']  = $attr['FAIvariable'][0];
624       $tmp['Type']  = $attr['FAIvariableType'][0];
626       if (isset($attr['FAIvariableContent'][0])){
627         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
628           $tmp['Value'] = $attr['FAIvariableContent'][0];
629         }else{
630           $content = array();        
631           unset($attr['FAIvariableContent']['count']);
632           foreach($attr['FAIvariableContent'] as $attr){
633             $tmp['Value'][] = $attr;
634           }
635         }
636         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
637       }
638     }
639   }
642   function getServerInfos()
643   {
644     $ret = array();
645     $ldap = $this->config->get_ldap_link();
646     $ldap->cd($this->config->current['BASE']);
647     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
648     while($attrs = $ldap->fetch()){
649       if(isset($attrs['FAIrepository'])){
650         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
651           $obj = $attrs['FAIrepository'][$i];
652           $tmp = split("\|",$obj);
653           if(count($tmp)==4){
654             foreach(split(",",$tmp[3]) as $sec){
655               if(!empty($sec)){
656                 $ret[$tmp[2]][] =  $sec;
657               }
658             }
659           }
660         }
661       }
662     }
663     return($ret);
664   }
669 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
670 ?>