Code

Added remove multiple to fai
[gosa.git] / plugins / 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 $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIdebianRelease          = ""; // The selected release
40   var $FAIdebianSection          = ""; // selected section
41   var $FAIinstallMethod          = "aptitude"; // hard coded
42   var $mirror                    = ""; // selected mirror
44   var $servers          = array();  // All available servers
45   var $releases         = array();  // All possible releases 
46   var $sections         = array();  // All section types
48   var $list             = NULL;
50   var $mirrors          = array();  // The combination of server/release/section
51   var $confDir          = "";
52   var $usedPackages     = array();
53   var $buffer           = NULL; 
54   var $strID            ="";
55   var $newDialogShown   =false;
57   var $FAIstate         = "";
59   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
60       "dselect-upgrade", "taskinst", "taskrm",
61       "hold", "clean", "aptitude", "aptitude-r",
62       "pending", "dpkgc" );
65   function faiPackage ($config, $dn= NULL)
66   {
67     /* Load Attributes */
68     plugin::plugin ($config, $dn);
70     /* If "dn==new" we try to create a new entry
71      * Else we must read all objects from ldap which belong to this entry.
72      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
73      */
74     if($dn != "new"){
75       $this->dn =$dn;
77       /* Get FAIstate
78        */
79       if(isset($this->attrs['FAIstate'][0])){
80         $this->FAIstate = $this->attrs['FAIstate'][0];
81       }
82     }
84     if(isset($this->attrs['FAIpackage'])){
85       unset($this->attrs['FAIpackage']['count']);
86       foreach($this->attrs['FAIpackage'] as $pkg){
87         $this->usedPackages[$pkg] = $pkg;
88       }
89       ksort($this->usedPackages);
90     }else{
91       $this->usedPackages = array();
92     }  
94     if($dn != "new"){
96       /* Create one filter with all package names, 
97          instead of calling $ldap->search for every single package 
98        */
99       $PackageFilter = "";
100       foreach($this->usedPackages as $name){
101         $PackageFilter .= "(FAIpackage=".$name.")";
102       }
103       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
105       /* Search for configuration objects */ 
106       $ldap = $this->config->get_ldap_link();
107       $ldap->cd($this->dn);
108       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
110       /* Walk through configurations and append them to our list of ConfiguredPackages */
111       while($attr = $ldap->fetch()){
113         /* Skip objects, that are tagged as removed */
114         if(isset($object['FAIstate'][0])){
115           if(preg_match("/removed$/",$attr['FAIstate'][0])){
116             continue;
117           }
118         }
120         $tmp =array(); 
121         $tmp['Name']  = $attr['FAIvariable'][0];
122         $tmp['Type']  = $attr['FAIvariableType'][0];
124         if (isset($attr['FAIvariableContent'][0])){
125           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
126             $tmp['Value'] = $attr['FAIvariableContent'][0];
127           }else{
128             $content = array();        
129             unset($attr['FAIvariableContent']['count']);
130             foreach($attr['FAIvariableContent'] as $attr){
131               $tmp['Value'][] = $attr;
132             }
133           }
134           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
135         }
136       }
137     }
139     if (isset($this->attrs['FAIdebianSection']['count'])){
140       unset($this->attrs['FAIdebianSection']['count']);
141     }
142     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
143       $this->FAIdebianSection = array();
144       foreach($this->attrs['FAIdebianSection'] as $sec){
145         $this->FAIdebianSection[$sec]=$sec;
146       }
147     }
149     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
150       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
151     }
152     $this->confDir = CONFIG_DIR."/fai/";
153     $this->FAIpackage = array();
156     $methods = array();
157     foreach($this->FAIinstallMethods as $method){
158       $methods[$method] = $method;
159     }
160     $this->FAIinstallMethods = $methods;
161     /* Check if we exist already - no need to ask for revisions, etc. then */
162     if ($this->dn != "new"){
163       $this->newDialogShown= true;
164     }
166   }
168   function execute()
169   {
170     /* Call parent execute */
172     plugin::execute();
174     /* Fill templating stuff */
175     $smarty= get_smarty();
176     $display= "";
178     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
180     if(!$this->is_account){
182       /* Assemble release name */
183       $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['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     /* Delte 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             print_red($msgs);
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'])) && $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, $path, $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 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
334     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
335     if($_SESSION['faifilter']['branch'] == "main"){
336       $use_dn = $this->dn;
337     }
339     prepare_to_save_FAI_object($use_dn,array(),true);
341     foreach($this->ConfiguredPackages as $pkgname => $attrs){
342       foreach($attrs as $name => $attr){
343         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
344 #        $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
345         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
346         if($_SESSION['faifilter']['branch'] == "main"){
347           $use_dn = $obj['dn'];
348         }
349         prepare_to_save_FAI_object($use_dn,array(),true);
350       }
351     }
352     $this->handle_post_events("remove");
353   }
356   /* Save data to object 
357    */
358   function save_object()
359   {
360     if($this->FAIstate == "freeze") return;  
361     plugin::save_object();
362   }
365   /* Check supplied data */
366   function check()
367   {
368     /* Call common method to give check the hook */
369     $message= plugin::check();
371     if(count($this->usedPackages)==0){
372       $message[]=_("Please select a least one Package.");
373     }
375     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
376       $message[]=_("Please choose a valid combination for your repository setup.");
377     }
379     return ($message);
380   }
382   function printUsedPackages(){
383     $a_ret=array(); 
384     if(is_array($this->usedPackages)) {
385       foreach($this->usedPackages as $usedName){
387         $config = 0;
389         foreach($this->ConfiguredPackages as $name => $value){
390           if($name == $usedName){
391             $config ++;
392           }
393         }
395         $c_str ="";
396         if($config){
397           $c_str = " - "._("package is configured");
398         }
400         /* Adapt used name if we're marked for removal */
401         $dsc= "";
402         if (preg_match('/^-/', $usedName)){
403           $dsc= " - "._("Package marked for removal");
404           // Generally a bad idea here, because the toggel triggers on -, not on !
405           //$usedName= preg_replace('/^-/', '! ', $usedName);
406         }else{
407           $usedName2= $usedName;
408         }
410         if(isset($this->list[$usedName][1])){
411           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
412         }else{
413           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
414         }
415       }
416     }
417     return($a_ret);
418   }
420   function genPkgs(){
421     /* Generate a list off available packages for this mirror, section and release
422      */
423     /* Only read this file if it wasn't read before */
424     if($this->buffer==NULL){
425       $this->buffer=array();
426       $a_ret = array();
427       foreach($this->FAIdebianSection as $sec){
428         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
430         if(!is_file($strID)){
431           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
432           unset($this->buffer);
433           return(array());
434         }
435         $fp = fopen($strID,"r");
437         /* Parse every line and create an array */
438         while(!feof($fp)){
439           $str= fgets($fp,512);
440           $stra= split("\|",$str);
441           if(count($stra)==4){
442             $a_ret[$stra[0]] = $stra;
443           }
444         }
445         fclose($fp);
446         /* Save our Data, to avoid reading it again */
447       }
448       $this->buffer = $a_ret;
449       ksort($a_ret);
450       return($a_ret);
451     }else{
452       return $this->buffer;
453     }
454   }
457   /* Save to LDAP */
458   function save()
459   {
460     plugin::save();
462     $ldap = $this->config->get_ldap_link();
464     $this->attrs['FAIpackage'] = array();
465     foreach($this->usedPackages as $pkg => $obj){
466       $this->attrs['FAIpackage'][] = $pkg;
467     } 
469     $this->attrs['FAIdebianSection'] = array();
470     foreach($this->FAIdebianSection as $sec){
471       $this->attrs['FAIdebianSection'][] = $sec;
472     }
474     prepare_to_save_FAI_object($this->dn,$this->attrs);
475     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
477     /* Do object tagging */
478     $this->handle_object_tagging();
479     $ldap->cd($this->dn);
481     /* Save Package configurations */
482     foreach($this->ConfiguredPackages as $pkgname => $attrs){
483       foreach($attrs as $name => $attr){
484       
485         $pkgattrs = array();
487         foreach($attr as $n=>$v){
488           if(empty($v)) $v = array();
489         }
491         /* Set attributes */
492         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
494         $pkgattrs['FAIpackage']           = $pkgname;
495         $pkgattrs['FAIvariable']          = $name;
496         $pkgattrs['FAIvariableType']      = $attr['Type'];
497         $pkgattrs['FAIvariableContent']   = $attr['Value'];
498         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
500         /* cehck if object already exists */
501         $ldap->cat($pkgdn,array("objectClass"));
503         /* Workaround for missing "gosaAdministrativeUnitTag" */
504         $attrs = $ldap->fetch();
505         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
506           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
507         }
509         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
510           prepare_to_save_FAI_object($pkgdn,$pkgattrs);
511         }
512   
513         /* Handle tagging */
514         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
515       }
516     }
517   }
519   /* Return plugin informations for acl handling */ 
520   function plInfo()
521   {
522     return (array( 
523           "plShortName" => _("Package"),
524           "plDescription" => _("FAI Package list"),
525           "plSelfModify"  => FALSE,
526           "plDepends"     => array(),
527           "plPriority"    => 28,
528           "plSection"     => array("administration"),
529           "plCategory"    => array("fai"),
530           "plProvidedAcls" => array(
531             "cn"                => _("Name"),
532             "description"       => _("Description"),
533             "FAIpackage"        => _("Packages"),
534             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
535             "FAIinstallMethod"  => _("Install Method"),
536             "FAIdebconfInfo"    => _("Package configuration"),
537             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
538           ));
539   }
541   function getServerInfos()
542   {
543     $ret = array();
544     $ldap = $this->config->get_ldap_link();
545     $ldap->cd($this->config->current['BASE']);
546     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
547     while($attrs = $ldap->fetch()){
548       if(isset($attrs['FAIrepository'])){
549         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
550           $obj = $attrs['FAIrepository'][$i];
551           $tmp = split("\|",$obj);
552           if(count($tmp)==4){
553             foreach(split(",",$tmp[3]) as $sec){
554               if(!empty($sec)){
555                 $ret[$tmp[2]][] =  $sec;
556               }
557             }
558           }
559         }
560       }
561     }
562     return($ret);
563   }
568 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
569 ?>