Code

6d6826222ac9033f172d69f72a0825c457ed1687
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description"); 
28   var $sub64coded       = array();
30   var $ConfiguredPackages = array();
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $SubObjects       = array();  // All leafobjects of this object
38   var $FAIdebianRelease          = ""; // The selected release
39   var $FAIdebianSection          = array(); // selected section
40   var $FAIinstallMethod          = "aptitude"; // hard coded
41   var $mirror                    = ""; // selected mirror
43   var $servers          = array();  // All available servers
44   var $releases         = array();  // All possible releases 
45   var $sections         = array();  // All section types
47   var $list             = NULL;
49   var $mirrors          = array();  // The combination of server/release/section
50   var $confDir          = "";
51   var $usedPackages     = array();
52   var $buffer           = NULL; 
53   var $strID            ="";
54   var $newDialogShown   =false;
56   var $FAIstate         = "";
57   var $view_logged      = FALSE;
58   var $base;
60   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
61       "dselect-upgrade", "taskinst", "taskrm",
62       "hold", "clean", "aptitude", "aptitude-r",
63       "pending", "dpkgc" );
66   function faiPackage (&$config, $dn= NULL)
67   {
68     /* Load Attributes */
69     plugin::plugin ($config, $dn);
71     /* If "dn==new" we try to create a new entry
72      * Else we must read all objects from ldap which belong to this entry.
73      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
74      */
75     if($dn != "new"){
76       $this->dn =$dn;
78       /* Get FAIstate
79        */
80       if(isset($this->attrs['FAIstate'][0])){
81         $this->FAIstate = $this->attrs['FAIstate'][0];
82       }
83     }
85     if(isset($this->attrs['FAIpackage'])){
86       unset($this->attrs['FAIpackage']['count']);
87       foreach($this->attrs['FAIpackage'] as $pkg){
88         $this->usedPackages[$pkg] = $pkg;
89       }
90       ksort($this->usedPackages);
91     }else{
92       $this->usedPackages = array();
93     }  
95     if($dn != "new"){
97       /* Create one filter with all package names, 
98          instead of calling $ldap->search for every single package 
99        */
100       $PackageFilter = "";
101       foreach($this->usedPackages as $name){
102         $PackageFilter .= "(FAIpackage=".$name.")";
103       }
104       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
106       /* Search for configuration objects */ 
107       $ldap = $this->config->get_ldap_link();
108       $ldap->cd($this->dn);
109       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
111       /* Walk through configurations and append them to our list of ConfiguredPackages */
112       while($attr = $ldap->fetch()){
114         /* Skip objects, that are tagged as removed */
115         if(isset($object['FAIstate'][0])){
116           if(preg_match("/removed$/",$attr['FAIstate'][0])){
117             continue;
118           }
119         }
121         $tmp =array(); 
122         $tmp['Name']  = $attr['FAIvariable'][0];
123         $tmp['Type']  = $attr['FAIvariableType'][0];
125         if (isset($attr['FAIvariableContent'][0])){
126           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
127             $tmp['Value'] = $attr['FAIvariableContent'][0];
128           }else{
129             $content = array();        
130             unset($attr['FAIvariableContent']['count']);
131             foreach($attr['FAIvariableContent'] as $attr){
132               $tmp['Value'][] = $attr;
133             }
134           }
135           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
136         }
137       }
138     }
140     if (isset($this->attrs['FAIdebianSection']['count'])){
141       unset($this->attrs['FAIdebianSection']['count']);
142     }
143     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
144       $this->FAIdebianSection = array();
145       foreach($this->attrs['FAIdebianSection'] as $sec){
146         $this->FAIdebianSection[$sec]=$sec;
147       }
148     }
150     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
151       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
152     }
153     $this->confDir = CONFIG_DIR."/fai/";
154     $this->FAIpackage = array();
157     $methods = array();
158     foreach($this->FAIinstallMethods as $method){
159       $methods[$method] = $method;
160     }
161     $this->FAIinstallMethods = $methods;
162     /* Check if we exist already - no need to ask for revisions, etc. then */
163     if ($this->dn != "new"){
164       $this->newDialogShown= true;
165     }
167   }
169   function execute()
170   {
171     /* Call parent execute */
172     plugin::execute();
174     if($this->is_account && !$this->view_logged){
175       $this->view_logged = TRUE;
176       new log("view","fai/".get_class($this),$this->dn);
177     }
179     /* Fill templating stuff */
180     $smarty= get_smarty();
181     $display= "";
183     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
185     if(!$this->is_account){
187       /* Assemble release name */
188       $faifilter = session::get('faifilter');
189       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
190       $tmp= preg_replace('/ou=/', '', $tmp);
191       $rev= array_reverse(split(',', $tmp));
192       $this->FAIdebianRelease= "";
193       foreach ($rev as $part){
194         $this->FAIdebianRelease.= "/$part";
195       }
196       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
198       /* Assemble sections */
199       $repos= $this->getServerInfos();
200       if(isset($repos[$this->FAIdebianRelease])){
201         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
202         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
203       }
205       /* Assign Repository settings*/ 
206       $this->is_account     = true;
207     }
209     /* Assign variables */
210     foreach($this->attributes as $attrs){
211       $smarty->assign($attrs,$this->$attrs);
212     }
214     /* Generate package list */
215     $this->list= $this->genPkgs();
217     /* + was pressed to open the package dialog */
218     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
219       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
220       $this->is_dialog =true;
221     }
223     /* Delte selected package */ 
224     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
225       if($this->FAIstate != "freeze"){
226         foreach($_POST['usedPackages'] as $del){
227           if(isset($this->usedPackages[$del])){
228             unset($this->usedPackages[$del]);
229           }
230         }
231       }
232     }
234     /* Abort package selection dialog */ 
235     if(isset($_POST['CancelSubObject'])){
236       $this->dialog = false;
237       $this->is_dialog=false;
238     }
240     /* attach new packages */
241     if(isset($_POST['SaveSubObject'])) {
242       if($this->FAIstate != "freeze"){
243         $this->dialog->save_object();
244         if(count($this->dialog->check())){
245           foreach($this->dialog->check() as $msgs){
246             print_red($msgs);
247           }
248         }else{
249           $use = $this->dialog->save();
250           $this->usedPackages = $use;
251           $this->dialog = false;
252           $this->is_dialog=false;
253           ksort($this->usedPackages);
254         }
255       }else{
256         $this->dialog = false;
257         $this->is_dialog=false;
258       }
259     }
261     /* Configuration dialog open*/
262     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
263       $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
264       $pkg_config = array();
265       $pkg = $_POST['usedPackages'][0];
267       if(isset($this->ConfiguredPackages[$pkg])){
268         $pkg_config = $this->ConfiguredPackages[$pkg];
269       }
271       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
272       $this->is_dialog =true;
273     }
275     /* Configuration dialog open*/
276     if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
277       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
278         foreach($_POST['usedPackages'] as $pkg){
279           if (isset($this->usedPackages[$pkg])){
280             unset($this->usedPackages[$pkg]);
281             if (preg_match('/^-/', $pkg)){
282               $pkg= preg_replace('/^-/', '', $pkg);
283             } else {
284               $pkg= preg_replace('/^/', '-', $pkg);
285             }
286             $this->usedPackages[$pkg]= $pkg;
287           }
288         }
289       }
290     }
292     /* Save Configuration */
293     if(isset($_POST['SaveObjectConfig'])){
294       if($this->FAIstate != "freeze"){
295         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
296       }
297       $this->dialog = false;
298       $this->is_dialog=false;
299     }
301     /* cancel configuration */     
302     if(isset($_POST['CancelObjectConfig'])){
303       $this->dialog = false;
304       $this->is_dialog=false;
305     }
307     /* Display dialog */ 
308     if($this->is_dialog){
309       return $this->dialog->execute();
310     }
312     /* Assign section to smarty */
313     $strsec = "";
314     foreach($this->FAIdebianSection as $sec){
315       $strsec .= $sec." ";
316     }
318     $tmp = $this->plInfo();
319     foreach($tmp['plProvidedAcls'] as $name => $translated){
320       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
321     }
323     $smarty->assign("releases",$this->releases);
324     $smarty->assign("release" ,$this->FAIdebianRelease);
325     $smarty->assign("sections",$this->sections);
326     $smarty->assign("section" ,$strsec);
327     $smarty->assign("usedPackages",$this->printUsedPackages());
328     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
329     return($display);
330   }
332   /* Delete me, and all my subtrees
333    */
334   function remove_from_parent()
335   {
336     $ldap = $this->config->get_ldap_link();
337     $ldap->cd ($this->dn);
339     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
340     if($faifilter['branch'] == "main"){
341       $use_dn = $this->dn;
342     }
344     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
346     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
348     foreach($this->ConfiguredPackages as $pkgname => $attrs){
349       foreach($attrs as $name => $attr){
350         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
351         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
352         if($faifilter['branch'] == "main"){
353           $use_dn = $obj['dn'];
354         }
355         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
356       }
357     }
358     $this->handle_post_events("remove");
359   }
362   /* Save data to object 
363    */
364   function save_object()
365   {
366     if($this->FAIstate == "freeze") return;  
367     plugin::save_object();
368   }
371   /* Check supplied data */
372   function check()
373   {
374     /* Call common method to give check the hook */
375     $message= plugin::check();
377     if(count($this->usedPackages)==0){
378       $message[]=_("Please select a least one Package.");
379     }
381     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
382       $message[]=_("Please choose a valid combination for your repository setup.");
383     }
385     return ($message);
386   }
388   function printUsedPackages(){
389     $a_ret=array(); 
390     if(is_array($this->usedPackages)) {
391       foreach($this->usedPackages as $usedName){
393         $config = 0;
395         foreach($this->ConfiguredPackages as $name => $value){
396           if($name == $usedName){
397             $config ++;
398           }
399         }
401         $c_str ="";
402         if($config){
403           $c_str = " - "._("package is configured");
404         }
406         /* Adapt used name if we're marked for removal */
407         $dsc= "";
408         if (preg_match('/^-/', $usedName)){
409           $dsc= " - "._("Package marked for removal");
410           // Generally a bad idea here, because the toggel triggers on -, not on !
411           //$usedName= preg_replace('/^-/', '! ', $usedName);
412         }else{
413           $usedName2= $usedName;
414         }
416         if(isset($this->list[$usedName][1])){
417           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
418         }else{
419           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
420         }
421       }
422     }
423     return($a_ret);
424   }
426   function genPkgs()
427   {
428     /* Generate a list off available packages for this mirror, section and release
429      */
430     /* Only read this file if it wasn't read before */
431     if($this->buffer === NULL){
432       $this->buffer=array();
433       $a_ret = array();
434       foreach($this->FAIdebianSection as $sec){
435         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
437         if(!is_file($strID)){
438           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
439           unset($this->buffer);
440           return(array());
441         }
442         $fp = fopen($strID,"r");
444         /* Parse every line and create an array */
445         while(!feof($fp)){
446           $str= fgets($fp,512);
447           $stra= split("\|",$str);
448           if(count($stra)==4){
449             $a_ret[$stra[0]] = $stra;
450           }
451         }
452         fclose($fp);
453         /* Save our Data, to avoid reading it again */
454       }
455       $this->buffer = $a_ret;
456       ksort($a_ret);
457       return($a_ret);
458     }else{
459       return $this->buffer;
460     }
461   }
464   /* Save to LDAP */
465   function save()
466   {
468     /* Assemble release name */
469     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
470       $faifilter = session::get('faifilter');
471       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
472       $tmp= preg_replace('/ou=/', '', $tmp);
473       $rev= array_reverse(split(',', $tmp));
474       $this->FAIdebianRelease= "";
475       foreach ($rev as $part){
476         $this->FAIdebianRelease.= "/$part";
477       }
478       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
479     }
481     plugin::save();
483     $ldap = $this->config->get_ldap_link();
485     $this->attrs['FAIpackage'] = array();
486     foreach($this->usedPackages as $pkg => $obj){
487       $this->attrs['FAIpackage'][] = $pkg;
488     } 
490     $this->attrs['FAIdebianSection'] = array();
491     foreach($this->FAIdebianSection as $sec){
492       $this->attrs['FAIdebianSection'][] = $sec;
493     }
495     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
496     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
497     
498     if($this->initially_was_account){
499       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
500     }else{
501       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
502     }
504     $ldap->cd($this->dn);
506     /* Save Package configurations */
507     foreach($this->ConfiguredPackages as $pkgname => $attrs){
508       foreach($attrs as $name => $attr){
509       
510         $pkgattrs = array();
512         foreach($attr as $n=>$v){
513           if(empty($v)) $v = array();
514         }
516         /* Set attributes */
517         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
519         $pkgattrs['FAIpackage']           = $pkgname;
520         $pkgattrs['FAIvariable']          = $name;
521         $pkgattrs['FAIvariableType']      = $attr['Type'];
522         $pkgattrs['FAIvariableContent']   = $attr['Value'];
523         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
525         /* Tag object */
526         $this->tag_attrs(&$pkgattrs, $pkgdn, $this->gosaUnitTag);
528         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
529           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
530         }
531       }
532     }
533   }
535   /* Return plugin informations for acl handling */ 
536   static function plInfo()
537   {
538     return (array( 
539           "plShortName" => _("Package"),
540           "plDescription" => _("FAI Package list"),
541           "plSelfModify"  => FALSE,
542           "plDepends"     => array(),
543           "plPriority"    => 28,
544           "plSection"     => array("administration"),
545           "plCategory"    => array("fai"),
546           "plProvidedAcls" => array(
547             "cn"                => _("Name"),
548             "description"       => _("Description"),
549             "FAIpackage"        => _("Packages"),
550             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
551             "FAIinstallMethod"  => _("Install Method"),
552             "FAIdebconfInfo"    => _("Package configuration"),
553             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
554           ));
555   }
560   function PrepareForCopyPaste($source)
561   {
562     plugin::PrepareForCopyPaste($source);
564     if(isset($source['FAIstate'][0])){
565       $this->FAIstate = $source['FAIstate'][0];
566     }
568     $this->FAIdebianRelease = "ClearFromCopyPaste";
570     if(isset($source['FAIpackage'])){
571       unset($source['FAIpackage']['count']);
572       foreach($source['FAIpackage'] as $pkg){
573         $this->usedPackages[$pkg] = $pkg;
574       }
575       ksort($this->usedPackages);
576     }else{
577       $this->usedPackages = array();
578     }
580     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
581       $this->FAIdebianSection = array();
582       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
583         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
584       }
585     }
587     /* Create one filter with all package names, 
588        instead of calling $ldap->search for every single package 
589      */
590     $PackageFilter = "";
591     foreach($this->usedPackages as $name){
592       $PackageFilter .= "(FAIpackage=".$name.")";
593     }
594     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
596     /* Search for configuration objects */ 
597     $ldap = $this->config->get_ldap_link();
598     $ldap->cd($source['dn']);
599     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
601     /* Walk through configurations and append them to our list of ConfiguredPackages */
602     while($attr = $ldap->fetch()){
604       /* Skip objects, that are tagged as removed */
605       if(isset($object['FAIstate'][0])){
606         if(preg_match("/removed$/",$attr['FAIstate'][0])){
607           continue;
608         }
609       }
611       $tmp =array(); 
612       $tmp['Name']  = $attr['FAIvariable'][0];
613       $tmp['Type']  = $attr['FAIvariableType'][0];
615       if (isset($attr['FAIvariableContent'][0])){
616         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
617           $tmp['Value'] = $attr['FAIvariableContent'][0];
618         }else{
619           $content = array();        
620           unset($attr['FAIvariableContent']['count']);
621           foreach($attr['FAIvariableContent'] as $attr){
622             $tmp['Value'][] = $attr;
623           }
624         }
625         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
626       }
627     }
628   }
631   function getServerInfos()
632   {
633     $ret = array();
634     $ldap = $this->config->get_ldap_link();
635     $ldap->cd($this->config->current['BASE']);
636     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
637     while($attrs = $ldap->fetch()){
638       if(isset($attrs['FAIrepository'])){
639         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
640           $obj = $attrs['FAIrepository'][$i];
641           $tmp = split("\|",$obj);
642           if(count($tmp)==4){
643             foreach(split(",",$tmp[3]) as $sec){
644               if(!empty($sec)){
645                 $ret[$tmp[2]][] =  $sec;
646               }
647             }
648           }
649         }
650       }
651     }
652     return($ret);
653   }
658 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
659 ?>