Code

list base image migration for gosa-plugins
[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       /* Search for configuration objects */ 
93       $ldap = $this->config->get_ldap_link();
94       $ldap->cd($this->dn);
95       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
96             "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
98       /* Walk through configurations and append them to our list of ConfiguredPackages 
99        */
100       while($attr = $ldap->fetch()){
102         /* Skip objects, that are tagged as removed */
103         if(isset($object['FAIstate'][0])){
104           if(preg_match("/removed$/",$attr['FAIstate'][0])){
105             continue;
106           }
107         }
109         $tmp =array(); 
110         $tmp['Name']  = $attr['FAIvariable'][0];
111         $tmp['Type']  = $attr['FAIvariableType'][0];
113         if (isset($attr['FAIvariableContent'][0])){
114           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
115             $tmp['Value'] = $attr['FAIvariableContent'][0];
116           }else{
117             $content = array();        
118             unset($attr['FAIvariableContent']['count']);
119             foreach($attr['FAIvariableContent'] as $attr){
120               $tmp['Value'][] = $attr;
121             }
122           }
123           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
124         }
125       }
127       $this->FAIdebianSection = array();
128       if(isset($this->attrs['FAIdebianSection'])){
129         for($i = 0 ; $i < $this->attrs['FAIdebianSection']['count'] ; $i++ ){ 
130           $sec = $this->attrs['FAIdebianSection'][$i];
131           $this->FAIdebianSection[$sec]=$sec;
132         }
133       }
134       $this->FAIpackage = array();
136     } // ENDE  dn != new  
138     $methods = array();
139     foreach($this->FAIinstallMethods as $method){
140       $methods[$method] = $method;
141     }
142     $this->FAIinstallMethods = $methods;
144     /* Check if we exist already - no need to ask for revisions, etc. then */
145     if ($this->dn != "new"){
146       $this->newDialogShown= true;
147     }
148     $this->is_new = FALSE;
149     if($this->dn == "new"){
150       $this->is_new =TRUE;
151     }
153     /* Generate package list */
154     $this->list= $this->genPkgs(TRUE);
155   }
158   function execute()
159   {
160     /* Call parent execute */
161     plugin::execute();
163     if($this->is_account && !$this->view_logged){
164       $this->view_logged = TRUE;
165       new log("view","fai/".get_class($this),$this->dn);
166     }
168     /* Fill templating stuff */
169     $smarty= get_smarty();
170     $display= "";
172     /******
173      * Initialize a new Package List with release and section name
174      ******/
175     
176     if(!$this->is_account){
178       /* Assemble release name */
179       $faifilter = session::get('faifilter');
180       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
181       $tmp= preg_replace('/ou=/', '', $tmp);
182       $rev= array_reverse(split(',', $tmp));
183       $this->FAIdebianRelease= "";
184       foreach ($rev as $part){
185         $this->FAIdebianRelease.= "/$part";
186       }
187       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
189       /* Assemble sections */
190       $repos= $this->getServerInfos();
191       if(isset($repos[$this->FAIdebianRelease])){
192         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
193         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
194       }
196       /* Assign Repository settings*/ 
197       $this->is_account     = true;
198     }
199     
201     /******
202      * Add && Remove Packages 
203      ******/
205     /* + was pressed to open the package dialog */
206     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
207       $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
208       $this->is_dialog =true;
209     }
211     /* Delete selected package */ 
212     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
213       if(!preg_match("/^freeze/", $this->FAIstate)){
214         foreach($_POST['usedPackages'] as $del){
215           if(isset($this->usedPackages[$del])){
216             unset($this->usedPackages[$del]);
217           }
218         }
220         /* Generate package list */
221         $this->list= $this->genPkgs(TRUE);
222       }
223     }
225     /* Abort package selection dialog */ 
226     if(isset($_POST['CancelSubObject'])){
227       $this->dialog = false;
228       $this->is_dialog=false;
229     }
231     /* attach new packages */
232     if(isset($_POST['SaveSubObject'])) {
233       if(!preg_match("/^freeze/", $this->FAIstate)){
234         $this->dialog->save_object();
235         if(count($this->dialog->check())){
236           foreach($this->dialog->check() as $msgs){
237             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
238           }
239         }else{
240           $use = $this->dialog->save();
241           $this->usedPackages = $use;
242           $this->dialog = false;
243           $this->is_dialog=false;
244           ksort($this->usedPackages);
246           /* Generate package list */
247           $this->list= $this->genPkgs(TRUE);
248         }
249       }else{
250         $this->dialog = false;
251         $this->is_dialog=false;
252       }
253     }
256     /******
257      * Configure Packages  
258      ******/
260     /* Configuration dialog open*/
261     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
262       $pkg_config = array();
263       $pkg = $_POST['usedPackages'][0];
264       if(isset($this->ConfiguredPackages[$pkg])){
265         $pkg_config = $this->ConfiguredPackages[$pkg];
266       }
267       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
268       $this->is_dialog =true;
269     }
271     /* Configuration dialog open*/
272     if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
273       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
274         foreach($_POST['usedPackages'] as $pkg){
275           if (isset($this->usedPackages[$pkg])){
276             unset($this->usedPackages[$pkg]);
277             if (preg_match('/^-/', $pkg)){
278               $pkg= preg_replace('/^-/', '', $pkg);
279             } else {
280               $pkg= preg_replace('/^/', '-', $pkg);
281             }
282             $this->usedPackages[$pkg]= $pkg;
283           }
284         }
285         /* Generate package list */
286         $this->list= $this->genPkgs(TRUE);
287       }
288     }
290     /* Save Configuration */
291     if(isset($_POST['SaveObjectConfig'])){
292       if(!preg_match("/^freeze/", $this->FAIstate)){
293         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
294       }
295       $this->dialog = false;
296       $this->is_dialog=false;
297     }
299     /* cancel configuration */     
300     if(isset($_POST['CancelObjectConfig'])){
301       $this->dialog = false;
302       $this->is_dialog=false;
303     }
305     /* Display dialog */ 
306     if($this->is_dialog){
307       $this->dialog->save_object();
308       return $this->dialog->execute();
309     }
312     /******
313      * Display UI / HTML / smarty 
314      ******/
316     /* Assign variables */
317     foreach($this->attributes as $attrs){
318       $smarty->assign($attrs,$this->$attrs);
319     }
320     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
322     /* Assign section to smarty */
323     $strsec = "";
324     foreach($this->FAIdebianSection as $sec){
325       $strsec .= $sec." ";
326     }
328     $tmp = $this->plInfo();
329     foreach($tmp['plProvidedAcls'] as $name => $translated){
330       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
331     }
333     $smarty->assign("release" ,$this->FAIdebianRelease);
334     $smarty->assign("sections",$this->sections);
335     $smarty->assign("section" ,$strsec);
336     $smarty->assign("usedPackages",$this->printUsedPackages());
337     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
338     return($display);
339   }
342   /*! \brief  Removes this packageList from the ldap database 
343    */
344   function remove_from_parent()
345   {
346     $ldap = $this->config->get_ldap_link();
347     $ldap->cd ($this->dn);
349     $faifilter = session::get('faifilter');
350     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
351     if($faifilter['branch'] == "main"){
352       $use_dn = $this->dn;
353     }
355     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
357     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
359     foreach($this->ConfiguredPackages as $pkgname => $attrs){
360       foreach($attrs as $name => $attr){
361         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
362         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
363         if($faifilter['branch'] == "main"){
364           $use_dn = $obj['dn'];
365         }
366         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
367       }
368     }
369     $this->handle_post_events("remove");
370   }
373   /*! \brief  Collect all relevant POST vars for this plugin 
374    */
375   function save_object()
376   {
377     if(preg_match("/^freeze/", $this->FAIstate)) return;
378     plugin::save_object();
379   }
382   /*! \brief  Check given inputs for this package list
383       @return Array Containing all error messages, or an empty array if no error occured
384    */
385   function check()
386   {
387     /* Call common method to give check the hook */
388     $message= plugin::check();
390     if(count($this->usedPackages)==0){
391       $message[]= _("Please select a least one package!");
392     }
394     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
395       $message[]=_("Please choose a valid release/section combination for your repository setup!");
396     }
398     /* Ensure that we do not overwrite an allready existing entry 
399      */
400     if($this->is_new){
401       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
402       $faifilter = session::get('faifilter');
403       if($faifilter['branch']!="main"){
404         $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
405       }
407       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
408       if(isset($res[$this->cn])){
409         $message[] = msgPool::duplicated(_("Name"));
410       }
411     }
412     return ($message);
413   }
416   /*! \brief  Returns all selected packages in a printable format 
417       @return Array   Containing all package infos.
418    */
419   function printUsedPackages()
420   {
421     $a_ret=array(); 
422     if(is_array($this->usedPackages)){
423       foreach($this->usedPackages as $usedName){
425         /* Append message if package is configured */
426         $c_str ="";
427         if(isset($this->ConfiguredPackages[$usedName])){
428           $c_str = " - "._("package is configured");
429         }
431         /* Adapt used name if we're marked for removal */
432         $dsc= "";
433         if (preg_match('/^-/', $usedName)){
434           $dsc= " - "._("Package marked for removal");
435         }else{
436           $usedName2= $usedName;
437         }
439         /* Append version string, if available */
440         if(isset($this->list[$usedName]['VERSION'])){
441           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName]['VERSION']."]".$c_str.$dsc;
442         }else{
443           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
444         }
445       }
446     }
447     return($a_ret);
448   }
451   /*! \brief  Reload the list of cached packages.
452       @return Returns the currently cached list of packages. 
453    */
454   function genPkgs($force = false)
455   {
456     if(!count($this->buffer) || $force){
457       $q = new gosaSupportDaemon();
458       $attrs = array("distribution", "package","version", "section", "description", "timestamp");
459       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$this->usedPackages);
460       if($q->is_error()){
461         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
462       }else{
463         foreach($ret as $attr){
464           $this->buffer[$attr['PACKAGE']] = $attr;
465         }
466       }
467     }
468     return $this->buffer;
469   }
472   /*! \brief Save packages and their configuration to ldap 
473    */
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     
506     if($this->initially_was_account){
507       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
508     }else{
509       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
510     }
512     $ldap->cd($this->dn);
514     /* Save Package configurations */
515     foreach($this->ConfiguredPackages as $pkgname => $attrs){
516       foreach($attrs as $name => $attr){
517       
518         $pkgattrs = array();
520         foreach($attr as $n=>$v){
521           if(empty($v)) $v = array();
522         }
524         /* Set attributes */
525         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
527         $pkgattrs['FAIpackage']           = $pkgname;
528         $pkgattrs['FAIvariable']          = $name;
529         $pkgattrs['FAIvariableType']      = $attr['Type'];
530         $pkgattrs['FAIvariableContent']   = $attr['Value'];
531         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
533         /* Tag object */
534         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
536         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
538           if($pkgattrs['FAIvariableType'] == "text" && $pkgattrs['FAIvariableContent'] == ""){
539             gosa_log("Skipped saving FAIvariable '$name' empty string can't be saved.");
540           }else{
541             FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
542           }
543         }
544       }
545     }
546   }
549   /*! \brief  Return plugin informations for acl handling 
550       @return Array ACL infos of this plugin.
551    */ 
552   static function plInfo()
553   {
554     return (array( 
555           "plShortName" => _("Package"),
556           "plDescription" => _("FAI Package list"),
557           "plSelfModify"  => FALSE,
558           "plDepends"     => array(),
559           "plPriority"    => 28,
560           "plSection"     => array("administration"),
561           "plCategory"    => array("fai"),
562           "plProvidedAcls" => array(
563             "cn"                => _("Name"),
564             "description"       => _("Description"),
565             "FAIpackage"        => _("Packages"),
566             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
567             "FAIinstallMethod"  => _("Install Method"),
568             "FAIdebconfInfo"    => _("Package configuration"),
569             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
570           ));
571   }
574   /*! \brief prepares this plugin to be inserted after it was copied or cut.
575       @param Array  All attributes from the source object. 
576    */
577   function PrepareForCopyPaste($source)
578   {
579     plugin::PrepareForCopyPaste($source);
581     if(isset($source['FAIstate'][0])){
582       $this->FAIstate = $source['FAIstate'][0];
583     }
585     $this->FAIdebianRelease = "ClearFromCopyPaste";
587     if(isset($source['FAIpackage'])){
588       unset($source['FAIpackage']['count']);
589       foreach($source['FAIpackage'] as $pkg){
590         $this->usedPackages[$pkg] = $pkg;
591       }
592       ksort($this->usedPackages);
593     }else{
594       $this->usedPackages = array();
595     }
597     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
598       $this->FAIdebianSection = array();
599       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
600         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
601       }
602     }
604     /* Create one filter with all package names, 
605        instead of calling $ldap->search for every single package 
606      */
607     $PackageFilter = "";
608     foreach($this->usedPackages as $name){
609       $PackageFilter .= "(FAIpackage=".$name.")";
610     }
611     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
613     /* Search for configuration objects */ 
614     $ldap = $this->config->get_ldap_link();
615     $ldap->cd($source['dn']);
616     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
618     /* Walk through configurations and append them to our list of ConfiguredPackages */
619     while($attr = $ldap->fetch()){
621       /* Skip objects, that are tagged as removed */
622       if(isset($object['FAIstate'][0])){
623         if(preg_match("/removed$/",$attr['FAIstate'][0])){
624           continue;
625         }
626       }
628       $tmp =array(); 
629       $tmp['Name']  = $attr['FAIvariable'][0];
630       $tmp['Type']  = $attr['FAIvariableType'][0];
632       if (isset($attr['FAIvariableContent'][0])){
633         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
634           $tmp['Value'] = $attr['FAIvariableContent'][0];
635         }else{
636           $content = array();        
637           unset($attr['FAIvariableContent']['count']);
638           foreach($attr['FAIvariableContent'] as $attr){
639             $tmp['Value'][] = $attr;
640           }
641         }
642         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
643       }
644     }
645   }
648   /*! \brief  Returns a list of all configured servers with repositories.
649       @return Array  All repository server 
650    */
651   function getServerInfos()
652   {
653     $ret = array();
654     $ldap = $this->config->get_ldap_link();
655     $ldap->cd($this->config->current['BASE']);
656     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
657     while($attrs = $ldap->fetch()){
658       if(isset($attrs['FAIrepository'])){
659         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
660           $obj = $attrs['FAIrepository'][$i];
661           $tmp = split("\|",$obj);
662           if(count($tmp)==4){
663             foreach(split(",",$tmp[3]) as $sec){
664               if(!empty($sec)){
665                 $ret[$tmp[2]][] =  $sec;
666               }
667             }
668           }
669         }
670       }
671     }
672     return($ret);
673   }
676   /*! \brief  Used for copy & paste.
677     Returns a HTML input mask, which allows to change the cn of this entry.
678     @param  Array   Array containing current status && a HTML template.
679    */
680   function getCopyDialog()
681   {
682     $vars = array("cn");
683     $smarty = get_smarty();
684     $smarty->assign("cn", htmlentities($this->cn));
685     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
686     $ret = array();
687     $ret['string'] = $str;
688     $ret['status'] = "";
689     return($ret);
690   }
693   /*! \brief  Used for copy & paste.
694     Some entries must be renamed to avaoid duplicate entries.
695    */
696   function saveCopyDialog()
697   {
698     if(isset($_POST['cn'])){
699       $this->cn = get_post('cn');
700     }
701   }
704 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
705 ?>