Code

Starting move
[gosa.git] / gosa-core / 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 $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       $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['faifilter']['branch']);
189       $tmp= preg_replace('/ou=/', '', $tmp);
190       $rev= array_reverse(split(',', $tmp));
191       $this->FAIdebianRelease= "";
192       foreach ($rev as $part){
193         $this->FAIdebianRelease.= "/$part";
194       }
195       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
197       /* Assemble sections */
198       $repos= $this->getServerInfos();
199       if(isset($repos[$this->FAIdebianRelease])){
200         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
201         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
202       }
204       /* Assign Repository settings*/ 
205       $this->is_account     = true;
206     }
208     /* Assign variables */
209     foreach($this->attributes as $attrs){
210       $smarty->assign($attrs,$this->$attrs);
211     }
213     /* Generate package list */
214     $this->list= $this->genPkgs();
216     /* + was pressed to open the package dialog */
217     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
218       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
219       $this->is_dialog =true;
220     }
222     /* Delte selected package */ 
223     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
224       if($this->FAIstate != "freeze"){
225         foreach($_POST['usedPackages'] as $del){
226           if(isset($this->usedPackages[$del])){
227             unset($this->usedPackages[$del]);
228           }
229         }
230       }
231     }
233     /* Abort package selection dialog */ 
234     if(isset($_POST['CancelSubObject'])){
235       $this->dialog = false;
236       $this->is_dialog=false;
237     }
239     /* attach new packages */
240     if(isset($_POST['SaveSubObject'])) {
241       if($this->FAIstate != "freeze"){
242         $this->dialog->save_object();
243         if(count($this->dialog->check())){
244           foreach($this->dialog->check() as $msgs){
245             print_red($msgs);
246           }
247         }else{
248           $use = $this->dialog->save();
249           $this->usedPackages = $use;
250           $this->dialog = false;
251           $this->is_dialog=false;
252           ksort($this->usedPackages);
253         }
254       }else{
255         $this->dialog = false;
256         $this->is_dialog=false;
257       }
258     }
260     /* Configuration dialog open*/
261     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
262       $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
263       $pkg_config = array();
264       $pkg = $_POST['usedPackages'][0];
266       if(isset($this->ConfiguredPackages[$pkg])){
267         $pkg_config = $this->ConfiguredPackages[$pkg];
268       }
270       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
271       $this->is_dialog =true;
272     }
274     /* Configuration dialog open*/
275     if($this->FAIstate != "freeze" && $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       }
289     }
291     /* Save Configuration */
292     if(isset($_POST['SaveObjectConfig'])){
293       if($this->FAIstate != "freeze"){
294         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
295       }
296       $this->dialog = false;
297       $this->is_dialog=false;
298     }
300     /* cancel configuration */     
301     if(isset($_POST['CancelObjectConfig'])){
302       $this->dialog = false;
303       $this->is_dialog=false;
304     }
306     /* Display dialog */ 
307     if($this->is_dialog){
308       return $this->dialog->execute();
309     }
311     /* Assign section to smarty */
312     $strsec = "";
313     foreach($this->FAIdebianSection as $sec){
314       $strsec .= $sec." ";
315     }
317     $tmp = $this->plInfo();
318     foreach($tmp['plProvidedAcls'] as $name => $translated){
319       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
320     }
322     $smarty->assign("releases",$this->releases);
323     $smarty->assign("release" ,$this->FAIdebianRelease);
324     $smarty->assign("sections",$this->sections);
325     $smarty->assign("section" ,$strsec);
326     $smarty->assign("usedPackages",$this->printUsedPackages());
327     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
328     return($display);
329   }
331   /* Delete me, and all my subtrees
332    */
333   function remove_from_parent()
334   {
335     $ldap = $this->config->get_ldap_link();
336     $ldap->cd ($this->dn);
338 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
339     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
340     if($_SESSION['faifilter']['branch'] == "main"){
341       $use_dn = $this->dn;
342     }
344     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 = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
352         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
353         if($_SESSION['faifilter']['branch'] == "main"){
354           $use_dn = $obj['dn'];
355         }
356         prepare_to_save_FAI_object($use_dn,array(),true);
357       }
358     }
359     $this->handle_post_events("remove");
360   }
363   /* Save data to object 
364    */
365   function save_object()
366   {
367     if($this->FAIstate == "freeze") return;  
368     plugin::save_object();
369   }
372   /* Check supplied data */
373   function check()
374   {
375     /* Call common method to give check the hook */
376     $message= plugin::check();
378     if(count($this->usedPackages)==0){
379       $message[]=_("Please select a least one Package.");
380     }
382     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
383       $message[]=_("Please choose a valid combination for your repository setup.");
384     }
386     return ($message);
387   }
389   function printUsedPackages(){
390     $a_ret=array(); 
391     if(is_array($this->usedPackages)) {
392       foreach($this->usedPackages as $usedName){
394         $config = 0;
396         foreach($this->ConfiguredPackages as $name => $value){
397           if($name == $usedName){
398             $config ++;
399           }
400         }
402         $c_str ="";
403         if($config){
404           $c_str = " - "._("package is configured");
405         }
407         /* Adapt used name if we're marked for removal */
408         $dsc= "";
409         if (preg_match('/^-/', $usedName)){
410           $dsc= " - "._("Package marked for removal");
411           // Generally a bad idea here, because the toggel triggers on -, not on !
412           //$usedName= preg_replace('/^-/', '! ', $usedName);
413         }else{
414           $usedName2= $usedName;
415         }
417         if(isset($this->list[$usedName][1])){
418           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
419         }else{
420           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
421         }
422       }
423     }
424     return($a_ret);
425   }
427   function genPkgs()
428   {
429     /* Generate a list off available packages for this mirror, section and release
430      */
431     /* Only read this file if it wasn't read before */
432     if($this->buffer === NULL){
433       $this->buffer=array();
434       $a_ret = array();
435       foreach($this->FAIdebianSection as $sec){
436         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
438         if(!is_file($strID)){
439           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
440           unset($this->buffer);
441           return(array());
442         }
443         $fp = fopen($strID,"r");
445         /* Parse every line and create an array */
446         while(!feof($fp)){
447           $str= fgets($fp,512);
448           $stra= split("\|",$str);
449           if(count($stra)==4){
450             $a_ret[$stra[0]] = $stra;
451           }
452         }
453         fclose($fp);
454         /* Save our Data, to avoid reading it again */
455       }
456       $this->buffer = $a_ret;
457       ksort($a_ret);
458       return($a_ret);
459     }else{
460       return $this->buffer;
461     }
462   }
465   /* Save to LDAP */
466   function save()
467   {
469     /* Assemble release name */
470     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
471       $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['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     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     /* Do object tagging */
505     $this->handle_object_tagging();
506     $ldap->cd($this->dn);
508     /* Save Package configurations */
509     foreach($this->ConfiguredPackages as $pkgname => $attrs){
510       foreach($attrs as $name => $attr){
511       
512         $pkgattrs = array();
514         foreach($attr as $n=>$v){
515           if(empty($v)) $v = array();
516         }
518         /* Set attributes */
519         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
521         $pkgattrs['FAIpackage']           = $pkgname;
522         $pkgattrs['FAIvariable']          = $name;
523         $pkgattrs['FAIvariableType']      = $attr['Type'];
524         $pkgattrs['FAIvariableContent']   = $attr['Value'];
525         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
527         /* cehck if object already exists */
528         $ldap->cat($pkgdn,array("objectClass"));
530         /* Workaround for missing "gosaAdministrativeUnitTag" */
531         $attrs = $ldap->fetch();
532         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
533           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
534         }
536         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
537           prepare_to_save_FAI_object($pkgdn,$pkgattrs);
538         }
539   
540         /* Handle tagging */
541         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
542       }
543     }
544   }
546   /* Return plugin informations for acl handling */ 
547   static function plInfo()
548   {
549     return (array( 
550           "plShortName" => _("Package"),
551           "plDescription" => _("FAI Package list"),
552           "plSelfModify"  => FALSE,
553           "plDepends"     => array(),
554           "plPriority"    => 28,
555           "plSection"     => array("administration"),
556           "plCategory"    => array("fai"),
557           "plProvidedAcls" => array(
558             "cn"                => _("Name"),
559             "description"       => _("Description"),
560             "FAIpackage"        => _("Packages"),
561             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
562             "FAIinstallMethod"  => _("Install Method"),
563             "FAIdebconfInfo"    => _("Package configuration"),
564             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
565           ));
566   }
571   function PrepareForCopyPaste($source)
572   {
573     plugin::PrepareForCopyPaste($source);
575     if(isset($source['FAIstate'][0])){
576       $this->FAIstate = $source['FAIstate'][0];
577     }
579     $this->FAIdebianRelease = "ClearFromCopyPaste";
581     if(isset($source['FAIpackage'])){
582       unset($source['FAIpackage']['count']);
583       foreach($source['FAIpackage'] as $pkg){
584         $this->usedPackages[$pkg] = $pkg;
585       }
586       ksort($this->usedPackages);
587     }else{
588       $this->usedPackages = array();
589     }
591     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
592       $this->FAIdebianSection = array();
593       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
594         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
595       }
596     }
598     /* Create one filter with all package names, 
599        instead of calling $ldap->search for every single package 
600      */
601     $PackageFilter = "";
602     foreach($this->usedPackages as $name){
603       $PackageFilter .= "(FAIpackage=".$name.")";
604     }
605     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
607     /* Search for configuration objects */ 
608     $ldap = $this->config->get_ldap_link();
609     $ldap->cd($source['dn']);
610     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
612     /* Walk through configurations and append them to our list of ConfiguredPackages */
613     while($attr = $ldap->fetch()){
615       /* Skip objects, that are tagged as removed */
616       if(isset($object['FAIstate'][0])){
617         if(preg_match("/removed$/",$attr['FAIstate'][0])){
618           continue;
619         }
620       }
622       $tmp =array(); 
623       $tmp['Name']  = $attr['FAIvariable'][0];
624       $tmp['Type']  = $attr['FAIvariableType'][0];
626       if (isset($attr['FAIvariableContent'][0])){
627         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
628           $tmp['Value'] = $attr['FAIvariableContent'][0];
629         }else{
630           $content = array();        
631           unset($attr['FAIvariableContent']['count']);
632           foreach($attr['FAIvariableContent'] as $attr){
633             $tmp['Value'][] = $attr;
634           }
635         }
636         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
637       }
638     }
639   }
642   function getServerInfos()
643   {
644     $ret = array();
645     $ldap = $this->config->get_ldap_link();
646     $ldap->cd($this->config->current['BASE']);
647     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
648     while($attrs = $ldap->fetch()){
649       if(isset($attrs['FAIrepository'])){
650         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
651           $obj = $attrs['FAIrepository'][$i];
652           $tmp = split("\|",$obj);
653           if(count($tmp)==4){
654             foreach(split(",",$tmp[3]) as $sec){
655               if(!empty($sec)){
656                 $ret[$tmp[2]][] =  $sec;
657               }
658             }
659           }
660         }
661       }
662     }
663     return($ret);
664   }
669 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
670 ?>