Code

f3df812bbb9cc95507609786aefc31c2dd6cfb71
[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   }
422   function genPkgs()
423   {
424     if($this->buffer === NULL || !count($this->buffer)){
425       $this->buffer = array();
426       $q = new gosaSupportDaemon();
427       $attrs = array("distribution", "package","version", "section", "description", "timestamp","template=''");
428       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs);
429       if($q->is_error()){
430         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
431       }else{
432         foreach($ret as $attr){
433           $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
434         }
435       }
436     }
437     return $this->buffer;
438   }
441   /* Save to LDAP */
442   function save()
443   {
445     /* Assemble release name */
446     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
447       $faifilter = session::get('faifilter');
448       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
449       $tmp= preg_replace('/ou=/', '', $tmp);
450       $rev= array_reverse(split(',', $tmp));
451       $this->FAIdebianRelease= "";
452       foreach ($rev as $part){
453         $this->FAIdebianRelease.= "/$part";
454       }
455       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
456     }
458     plugin::save();
460     $ldap = $this->config->get_ldap_link();
462     $this->attrs['FAIpackage'] = array();
463     foreach($this->usedPackages as $pkg => $obj){
464       $this->attrs['FAIpackage'][] = $pkg;
465     } 
467     $this->attrs['FAIdebianSection'] = array();
468     foreach($this->FAIdebianSection as $sec){
469       $this->attrs['FAIdebianSection'][] = $sec;
470     }
472     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
473     if (!$ldap->success()){
474       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
475     }
476     
477     if($this->initially_was_account){
478       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
479     }else{
480       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
481     }
483     $ldap->cd($this->dn);
485     /* Save Package configurations */
486     foreach($this->ConfiguredPackages as $pkgname => $attrs){
487       foreach($attrs as $name => $attr){
488       
489         $pkgattrs = array();
491         foreach($attr as $n=>$v){
492           if(empty($v)) $v = array();
493         }
495         /* Set attributes */
496         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
498         $pkgattrs['FAIpackage']           = $pkgname;
499         $pkgattrs['FAIvariable']          = $name;
500         $pkgattrs['FAIvariableType']      = $attr['Type'];
501         $pkgattrs['FAIvariableContent']   = $attr['Value'];
502         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
504         /* Tag object */
505         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
507         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
508           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
509         }
510       }
511     }
512   }
514   /* Return plugin informations for acl handling */ 
515   static function plInfo()
516   {
517     return (array( 
518           "plShortName" => _("Package"),
519           "plDescription" => _("FAI Package list"),
520           "plSelfModify"  => FALSE,
521           "plDepends"     => array(),
522           "plPriority"    => 28,
523           "plSection"     => array("administration"),
524           "plCategory"    => array("fai"),
525           "plProvidedAcls" => array(
526             "cn"                => _("Name"),
527             "description"       => _("Description"),
528             "FAIpackage"        => _("Packages"),
529             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
530             "FAIinstallMethod"  => _("Install Method"),
531             "FAIdebconfInfo"    => _("Package configuration"),
532             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
533           ));
534   }
539   function PrepareForCopyPaste($source)
540   {
541     plugin::PrepareForCopyPaste($source);
543     if(isset($source['FAIstate'][0])){
544       $this->FAIstate = $source['FAIstate'][0];
545     }
547     $this->FAIdebianRelease = "ClearFromCopyPaste";
549     if(isset($source['FAIpackage'])){
550       unset($source['FAIpackage']['count']);
551       foreach($source['FAIpackage'] as $pkg){
552         $this->usedPackages[$pkg] = $pkg;
553       }
554       ksort($this->usedPackages);
555     }else{
556       $this->usedPackages = array();
557     }
559     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
560       $this->FAIdebianSection = array();
561       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
562         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
563       }
564     }
566     /* Create one filter with all package names, 
567        instead of calling $ldap->search for every single package 
568      */
569     $PackageFilter = "";
570     foreach($this->usedPackages as $name){
571       $PackageFilter .= "(FAIpackage=".$name.")";
572     }
573     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
575     /* Search for configuration objects */ 
576     $ldap = $this->config->get_ldap_link();
577     $ldap->cd($source['dn']);
578     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
580     /* Walk through configurations and append them to our list of ConfiguredPackages */
581     while($attr = $ldap->fetch()){
583       /* Skip objects, that are tagged as removed */
584       if(isset($object['FAIstate'][0])){
585         if(preg_match("/removed$/",$attr['FAIstate'][0])){
586           continue;
587         }
588       }
590       $tmp =array(); 
591       $tmp['Name']  = $attr['FAIvariable'][0];
592       $tmp['Type']  = $attr['FAIvariableType'][0];
594       if (isset($attr['FAIvariableContent'][0])){
595         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
596           $tmp['Value'] = $attr['FAIvariableContent'][0];
597         }else{
598           $content = array();        
599           unset($attr['FAIvariableContent']['count']);
600           foreach($attr['FAIvariableContent'] as $attr){
601             $tmp['Value'][] = $attr;
602           }
603         }
604         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
605       }
606     }
607   }
610   function getServerInfos()
611   {
612     $ret = array();
613     $ldap = $this->config->get_ldap_link();
614     $ldap->cd($this->config->current['BASE']);
615     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
616     while($attrs = $ldap->fetch()){
617       if(isset($attrs['FAIrepository'])){
618         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
619           $obj = $attrs['FAIrepository'][$i];
620           $tmp = split("\|",$obj);
621           if(count($tmp)==4){
622             foreach(split(",",$tmp[3]) as $sec){
623               if(!empty($sec)){
624                 $ret[$tmp[2]][] =  $sec;
625               }
626             }
627           }
628         }
629       }
630     }
631     return($ret);
632   }
637 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
638 ?>