Code

Updated FAI Package
[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
37   var $sections         = array();  // All section types
39   var $list             = NULL;
41   var $usedPackages     = array();
42   var $buffer           = NULL; 
43   var $newDialogShown   =false;
45   var $FAIstate         = "";
46   var $view_logged      = FALSE;
47   var $base;
48   var $FAIpackage ;
50   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
51       "dselect-upgrade", "taskinst", "taskrm",
52       "hold", "clean", "aptitude", "aptitude-r",
53       "pending", "dpkgc" );
56   function faiPackage (&$config, $dn= NULL)
57   {
58     /* Load Attributes */
59     plugin::plugin ($config, $dn);
61     /* If "dn==new" we try to create a new entry
62      * Else we must read all objects from ldap which belong to this entry.
63      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
64      */
65     if($dn != "new"){
66       $this->dn =$dn;
68       /* Get FAIstate */
69       if(isset($this->attrs['FAIstate'][0])){
70         $this->FAIstate = $this->attrs['FAIstate'][0];
71       }
73       /* Check if there are already some packages in this list 
74        */
75       $this->usedPackages = array();
76       if(isset($this->attrs['FAIpackage'])){
77         unset($this->attrs['FAIpackage']['count']);
78         foreach($this->attrs['FAIpackage'] as $pkg){
79           $this->usedPackages[$pkg] = $pkg;
80         }
81         ksort($this->usedPackages);
82       }  
84       /* Fetch all package configurations from ldap 
85        */
86       $PackageFilter = "";
87       foreach($this->usedPackages as $name){
88         $PackageFilter .= "(FAIpackage=".$name.")";
89       }
90       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
92       $res = FAI::get_all_objects_for_given_base(FAI::get_release_dn($dn),$PackageFilter);
93       $ldap = $this->config->get_ldap_link();
94       $ldap->cd($this->config->current['BASE']);
95       foreach($res as $dn_v){
96         if(preg_match("/".normalizePreg($dn)."$/",$dn_v['dn'])){
98           $ldap->cat($dn_v['dn']);
99           $attr = $ldap->fetch();
101           /* Walk through configurations and append them to our list of ConfiguredPackages 
102            */
104           /* Skip objects, that are tagged as removed */
105           if(isset($object['FAIstate'][0])){
106             if(preg_match("/removed$/",$attr['FAIstate'][0])){
107               continue;
108             }
109           }
111           $tmp =array(); 
112           $tmp['Name']  = $attr['FAIvariable'][0];
113           $tmp['Type']  = $attr['FAIvariableType'][0];
115           if (isset($attr['FAIvariableContent'][0])){
116             if(!in_array($attr['FAIvariableType'],array("multiselect"))){
117               $tmp['Value'] = $attr['FAIvariableContent'][0];
118             }else{
119               $content = array();        
120               unset($attr['FAIvariableContent']['count']);
121               foreach($attr['FAIvariableContent'] as $attr){
122                 $tmp['Value'][] = $attr;
123               }
124             }
125             $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
126           }
127         }
128       }
130       $this->FAIdebianSection = array();
131       if(isset($this->attrs['FAIdebianSection'])){
132         for($i = 0 ; $i < $this->attrs['FAIdebianSection']['count'] ; $i++ ){ 
133           $sec = $this->attrs['FAIdebianSection'][$i];
134           $this->FAIdebianSection[$sec]=$sec;
135         }
136       }
137       $this->FAIpackage = array();
139     } // ENDE  dn != new  
141     $methods = array();
142     foreach($this->FAIinstallMethods as $method){
143       $methods[$method] = $method;
144     }
145     $this->FAIinstallMethods = $methods;
147     /* Check if we exist already - no need to ask for revisions, etc. then */
148     if ($this->dn != "new"){
149       $this->newDialogShown= true;
150     }
151     $this->is_new = FALSE;
152     if($this->dn == "new"){
153       $this->is_new =TRUE;
154     }
156     /* Generate package list */
157     $this->list= $this->genPkgs(TRUE);
158   }
161   function execute()
162   {
163     /* Call parent execute */
164     plugin::execute();
166     if($this->is_account && !$this->view_logged){
167       $this->view_logged = TRUE;
168       new log("view","fai/".get_class($this),$this->dn);
169     }
171     /* Fill templating stuff */
172     $smarty= get_smarty();
173     $display= "";
175     /******
176      * Initialize a new Package List with release and section name
177      ******/
178     
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     }
202     
204     /******
205      * Add && Remove Packages 
206      ******/
208     /* + was pressed to open the package dialog */
209     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
210       $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
211       $this->is_dialog =true;
212     }
214     /* Delete selected package */ 
215     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
216       if(!preg_match("/^freeze/", $this->FAIstate)){
217         foreach($_POST['usedPackages'] as $del){
218           if(isset($this->usedPackages[$del])){
219             unset($this->usedPackages[$del]);
220           }
221         }
223         /* Generate package list */
224         $this->list= $this->genPkgs(TRUE);
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(!preg_match("/^freeze/", $this->FAIstate)){
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);
249           /* Generate package list */
250           $this->list= $this->genPkgs(TRUE);
251         }
252       }else{
253         $this->dialog = false;
254         $this->is_dialog=false;
255       }
256     }
259     /******
260      * Configure Packages  
261      ******/
263     /* Configuration dialog open*/
264     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
265       $pkg_config = array();
266       $pkg = $_POST['usedPackages'][0];
267       if(isset($this->ConfiguredPackages[$pkg])){
268         $pkg_config = $this->ConfiguredPackages[$pkg];
269       }
270       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
271       $this->is_dialog =true;
272     }
274     /* Configuration dialog open*/
275     if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
276       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
277         foreach($_POST['usedPackages'] as $pkg){
278           if (isset($this->usedPackages[$pkg])){
279             unset($this->usedPackages[$pkg]);
280             if (preg_match('/^-/', $pkg)){
281               $pkg= preg_replace('/^-/', '', $pkg);
282             } else {
283               $pkg= preg_replace('/^/', '-', $pkg);
284             }
285             $this->usedPackages[$pkg]= $pkg;
286           }
287         }
288         /* Generate package list */
289         $this->list= $this->genPkgs(TRUE);
290       }
291     }
293     /* Save Configuration */
294     if(isset($_POST['SaveObjectConfig'])){
295       if(!preg_match("/^freeze/", $this->FAIstate)){
296         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
297       }
298       $this->dialog = false;
299       $this->is_dialog=false;
300     }
302     /* cancel configuration */     
303     if(isset($_POST['CancelObjectConfig'])){
304       $this->dialog = false;
305       $this->is_dialog=false;
306     }
308     /* Display dialog */ 
309     if($this->is_dialog){
310       $this->dialog->save_object();
311       return $this->dialog->execute();
312     }
315     /******
316      * Display UI / HTML / smarty 
317      ******/
319     /* Assign variables */
320     foreach($this->attributes as $attrs){
321       $smarty->assign($attrs,$this->$attrs);
322     }
323     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
325     /* Assign section to smarty */
326     $strsec = "";
327     foreach($this->FAIdebianSection as $sec){
328       $strsec .= $sec." ";
329     }
331     $tmp = $this->plInfo();
332     foreach($tmp['plProvidedAcls'] as $name => $translated){
333       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
334     }
336     $smarty->assign("release" ,$this->FAIdebianRelease);
337     $smarty->assign("sections",$this->sections);
338     $smarty->assign("section" ,$strsec);
339     $smarty->assign("usedPackages",$this->printUsedPackages());
340     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
341     return($display);
342   }
345   /*! \brief  Removes this packageList from the ldap database 
346    */
347   function remove_from_parent()
348   {
349     $ldap = $this->config->get_ldap_link();
350     $ldap->cd ($this->dn);
352     $faifilter = session::get('faifilter');
353     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
354     if($faifilter['branch'] == "main"){
355       $use_dn = $this->dn;
356     }
358     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
360     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
362     foreach($this->ConfiguredPackages as $pkgname => $attrs){
363       foreach($attrs as $name => $attr){
364         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
365         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
366         if($faifilter['branch'] == "main"){
367           $use_dn = $obj['dn'];
368         }
369         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
370       }
371     }
372     $this->handle_post_events("remove");
373   }
376   /*! \brief  Collect all relevant POST vars for this plugin 
377    */
378   function save_object()
379   {
380     if(preg_match("/^freeze/", $this->FAIstate)) return;
381     plugin::save_object();
382   }
385   /*! \brief  Check given inputs for this package list
386       @return Array Containing all error messages, or an empty array if no error occured
387    */
388   function check()
389   {
390     /* Call common method to give check the hook */
391     $message= plugin::check();
393     if(count($this->usedPackages)==0){
394       $message[]= _("Please select a least one package!");
395     }
397     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
398       $message[]=_("Please choose a valid release/section combination for your repository setup!");
399     }
401     /* Ensure that we do not overwrite an allready existing entry 
402      */
403     if($this->is_new){
404       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
405       $faifilter = session::get('faifilter');
406       if($faifilter['branch']!="main"){
407         $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
408       }
410       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
411       if(isset($res[$this->cn])){
412         $message[] = msgPool::duplicated(_("Name"));
413       }
414     }
415     return ($message);
416   }
419   /*! \brief  Returns all selected packages in a printable format 
420       @return Array   Containing all package infos.
421    */
422   function printUsedPackages()
423   {
424     $a_ret=array(); 
425     if(is_array($this->usedPackages)){
426       foreach($this->usedPackages as $usedName){
428         /* Append message if package is configured */
429         $c_str ="";
430         if(isset($this->ConfiguredPackages[$usedName])){
431           $c_str = " - "._("package is configured");
432         }
434         /* Adapt used name if we're marked for removal */
435         $dsc= "";
436         if (preg_match('/^-/', $usedName)){
437           $dsc= " - "._("Package marked for removal");
438         }else{
439           $usedName2= $usedName;
440         }
442         /* Append version string, if available */
443         if(isset($this->list[$usedName]['VERSION'])){
444           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName]['VERSION']."]".$c_str.$dsc;
445         }else{
446           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
447         }
448       }
449     }
450     return($a_ret);
451   }
454   /*! \brief  Reload the list of cached packages.
455       @return Returns the currently cached list of packages. 
456    */
457   function genPkgs($force = false)
458   {
459     if(!count($this->buffer) || $force){
460       $q = new gosaSupportDaemon();
461       $attrs = array("distribution", "package","version", "section", "description", "timestamp");
462       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$this->usedPackages);
463       if($q->is_error()){
464         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
465       }else{
466         foreach($ret as $attr){
467           $this->buffer[$attr['PACKAGE']] = $attr;
468         }
469       }
470     }
471     return $this->buffer;
472   }
475   /*! \brief Save packages and their configuration to ldap 
476    */
477   function save()
478   {
480     /* Assemble release name */
481     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
482       $faifilter = session::get('faifilter');
483       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
484       $tmp= preg_replace('/ou=/', '', $tmp);
485       $rev= array_reverse(split(',', $tmp));
486       $this->FAIdebianRelease= "";
487       foreach ($rev as $part){
488         $this->FAIdebianRelease.= "/$part";
489       }
490       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
491     }
493     plugin::save();
495     $ldap = $this->config->get_ldap_link();
497     $this->attrs['FAIpackage'] = array();
498     foreach($this->usedPackages as $pkg => $obj){
499       $this->attrs['FAIpackage'][] = $pkg;
500     } 
502     $this->attrs['FAIdebianSection'] = array();
503     foreach($this->FAIdebianSection as $sec){
504       $this->attrs['FAIdebianSection'][] = $sec;
505     }
507     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
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"))){
541           if($pkgattrs['FAIvariableType'] == "text" && $pkgattrs['FAIvariableContent'] == ""){
542             gosa_log("Skipped saving FAIvariable '$name' empty string can't be saved.");
543           }else{
544             FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
545           }
546         }
547       }
548     }
549   }
552   /*! \brief  Return plugin informations for acl handling 
553       @return Array ACL infos of this plugin.
554    */ 
555   static function plInfo()
556   {
557     return (array( 
558           "plShortName" => _("Package"),
559           "plDescription" => _("FAI Package list"),
560           "plSelfModify"  => FALSE,
561           "plDepends"     => array(),
562           "plPriority"    => 28,
563           "plSection"     => array("administration"),
564           "plCategory"    => array("fai"),
565           "plProvidedAcls" => array(
566             "cn"                => _("Name"),
567             "description"       => _("Description"),
568             "FAIpackage"        => _("Packages"),
569             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
570             "FAIinstallMethod"  => _("Install Method"),
571             "FAIdebconfInfo"    => _("Package configuration"),
572             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
573           ));
574   }
577   /*! \brief prepares this plugin to be inserted after it was copied or cut.
578       @param Array  All attributes from the source object. 
579    */
580   function PrepareForCopyPaste($source)
581   {
582     plugin::PrepareForCopyPaste($source);
584     if(isset($source['FAIstate'][0])){
585       $this->FAIstate = $source['FAIstate'][0];
586     }
588     $this->FAIdebianRelease = "ClearFromCopyPaste";
590     if(isset($source['FAIpackage'])){
591       unset($source['FAIpackage']['count']);
592       foreach($source['FAIpackage'] as $pkg){
593         $this->usedPackages[$pkg] = $pkg;
594       }
595       ksort($this->usedPackages);
596     }else{
597       $this->usedPackages = array();
598     }
600     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
601       $this->FAIdebianSection = array();
602       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
603         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
604       }
605     }
607     /* Create one filter with all package names, 
608        instead of calling $ldap->search for every single package 
609      */
610     $PackageFilter = "";
611     foreach($this->usedPackages as $name){
612       $PackageFilter .= "(FAIpackage=".$name.")";
613     }
614     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
616     /* Search for configuration objects */ 
617     $ldap = $this->config->get_ldap_link();
618     $ldap->cd($source['dn']);
619     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
621     /* Walk through configurations and append them to our list of ConfiguredPackages */
622     while($attr = $ldap->fetch()){
624       /* Skip objects, that are tagged as removed */
625       if(isset($object['FAIstate'][0])){
626         if(preg_match("/removed$/",$attr['FAIstate'][0])){
627           continue;
628         }
629       }
631       $tmp =array(); 
632       $tmp['Name']  = $attr['FAIvariable'][0];
633       $tmp['Type']  = $attr['FAIvariableType'][0];
635       if (isset($attr['FAIvariableContent'][0])){
636         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
637           $tmp['Value'] = $attr['FAIvariableContent'][0];
638         }else{
639           $content = array();        
640           unset($attr['FAIvariableContent']['count']);
641           foreach($attr['FAIvariableContent'] as $attr){
642             $tmp['Value'][] = $attr;
643           }
644         }
645         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
646       }
647     }
648   }
651   /*! \brief  Returns a list of all configured servers with repositories.
652       @return Array  All repository server 
653    */
654   function getServerInfos()
655   {
656     $ret = array();
657     $ldap = $this->config->get_ldap_link();
658     $ldap->cd($this->config->current['BASE']);
659     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
660     while($attrs = $ldap->fetch()){
661       if(isset($attrs['FAIrepository'])){
662         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
663           $obj = $attrs['FAIrepository'][$i];
664           $tmp = split("\|",$obj);
665           if(count($tmp)==4){
666             foreach(split(",",$tmp[3]) as $sec){
667               if(!empty($sec)){
668                 $ret[$tmp[2]][] =  $sec;
669               }
670             }
671           }
672         }
673       }
674     }
675     return($ret);
676   }
679   /*! \brief  Used for copy & paste.
680     Returns a HTML input mask, which allows to change the cn of this entry.
681     @param  Array   Array containing current status && a HTML template.
682    */
683   function getCopyDialog()
684   {
685     $vars = array("cn");
686     $smarty = get_smarty();
687     $smarty->assign("cn", htmlentities($this->cn));
688     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
689     $ret = array();
690     $ret['string'] = $str;
691     $ret['status'] = "";
692     return($ret);
693   }
696   /*! \brief  Used for copy & paste.
697     Some entries must be renamed to avaoid duplicate entries.
698    */
699   function saveCopyDialog()
700   {
701     if(isset($_POST['cn'])){
702       $this->cn = get_post('cn');
703     }
704   }
707 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
708 ?>