Code

Added patch from 2.5
[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       $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
195       $this->FAIdebianSection= array_unique($this->FAIdebianSection);
197       /* Assign Repository settings*/ 
198       $this->is_account     = true;
199     }
201     /* Assign variables */
202     foreach($this->attributes as $attrs){
203       $smarty->assign($attrs,$this->$attrs);
204     }
206     /* Generate package list */
207     $this->list= $this->genPkgs();
209     /* + was pressed to open the package dialog */
210     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
211       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
212       $this->is_dialog =true;
213     }
215     /* Delte selected package */ 
216     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
217       if($this->FAIstate != "freeze"){
218         foreach($_POST['usedPackages'] as $del){
219           if(isset($this->usedPackages[$del])){
220             unset($this->usedPackages[$del]);
221           }
222         }
223       }
224     }
226     /* Abort package selection dialog */ 
227     if(isset($_POST['CancelSubObject'])){
228       $this->dialog = false;
229       $this->is_dialog=false;
230     }
232     /* attach new packages */
233     if(isset($_POST['SaveSubObject'])) {
234       if($this->FAIstate != "freeze"){
235         $this->dialog->save_object();
236         if(count($this->dialog->check())){
237           foreach($this->dialog->check() as $msgs){
238             print_red($msgs);
239           }
240         }else{
241           $use = $this->dialog->save();
242           $this->usedPackages = $use;
243           $this->dialog = false;
244           $this->is_dialog=false;
245           ksort($this->usedPackages);
246         }
247       }else{
248         $this->dialog = false;
249         $this->is_dialog=false;
250       }
251     }
253     /* Configuration dialog open*/
254     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'])) && $this->acl_is_writeable("FAIdebconfInfo")){
255       $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
256       $pkg_config = array();
257       $pkg = $_POST['usedPackages'][0];
259       if(isset($this->ConfiguredPackages[$pkg])){
260         $pkg_config = $this->ConfiguredPackages[$pkg];
261       }
263       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
264       $this->is_dialog =true;
265     }
267     /* Configuration dialog open*/
268     if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
269       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
270         foreach($_POST['usedPackages'] as $pkg){
271           if (isset($this->usedPackages[$pkg])){
272             unset($this->usedPackages[$pkg]);
273             if (preg_match('/^-/', $pkg)){
274               $pkg= preg_replace('/^-/', '', $pkg);
275             } else {
276               $pkg= preg_replace('/^/', '-', $pkg);
277             }
278             $this->usedPackages[$pkg]= $pkg;
279           }
280         }
281       }
282     }
284     /* Save Configuration */
285     if(isset($_POST['SaveObjectConfig'])){
286       if($this->FAIstate != "freeze"){
287         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
288       }
289       $this->dialog = false;
290       $this->is_dialog=false;
291     }
293     /* cancel configuration */     
294     if(isset($_POST['CancelObjectConfig'])){
295       $this->dialog = false;
296       $this->is_dialog=false;
297     }
299     /* Display dialog */ 
300     if($this->is_dialog){
301       return $this->dialog->execute();
302     }
304     /* Assign section to smarty */
305     $strsec = "";
306     foreach($this->FAIdebianSection as $sec){
307       $strsec .= $sec." ";
308     }
310     $tmp = $this->plInfo();
311     foreach($tmp['plProvidedAcls'] as $name => $translated){
312       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
313     }
315     $smarty->assign("releases",$this->releases);
316     $smarty->assign("release" ,$this->FAIdebianRelease);
317     $smarty->assign("sections",$this->sections);
318     $smarty->assign("section" ,$strsec);
319     $smarty->assign("usedPackages",$this->printUsedPackages());
320     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
321     return($display);
322   }
324   /* Delete me, and all my subtrees
325    */
326   function remove_from_parent()
327   {
328     $ldap = $this->config->get_ldap_link();
329     $ldap->cd ($this->dn);
331 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
332     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
333     if($_SESSION['faifilter']['branch'] == "main"){
334       $use_dn = $this->dn;
335     }
337     prepare_to_save_FAI_object($use_dn,array(),true);
339     foreach($this->ConfiguredPackages as $pkgname => $attrs){
340       foreach($attrs as $name => $attr){
341         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
342 #        $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
343         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
344         if($_SESSION['faifilter']['branch'] == "main"){
345           $use_dn = $obj['dn'];
346         }
347         prepare_to_save_FAI_object($use_dn,array(),true);
348       }
349     }
350     $this->handle_post_events("remove");
351   }
354   /* Save data to object 
355    */
356   function save_object()
357   {
358     if($this->FAIstate == "freeze") return;  
359     plugin::save_object();
360   }
363   /* Check supplied data */
364   function check()
365   {
366     /* Call common method to give check the hook */
367     $message= plugin::check();
369     if(count($this->usedPackages)==0){
370       $message[]=_("Please select a least one Package.");
371     }
373     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
374       $message[]=_("Please choose a valid combination for your repository setup.");
375     }
377     return ($message);
378   }
380   function printUsedPackages(){
381     $a_ret=array(); 
382     if(is_array($this->usedPackages)) {
383       foreach($this->usedPackages as $usedName){
385         $config = 0;
387         foreach($this->ConfiguredPackages as $name => $value){
388           if($name == $usedName){
389             $config ++;
390           }
391         }
393         $c_str ="";
394         if($config){
395           $c_str = " - "._("package is configured");
396         }
398         /* Adapt used name if we're marked for removal */
399         $dsc= "";
400         if (preg_match('/^-/', $usedName)){
401           $dsc= " - "._("Package marked for removal");
402           // Generally a bad idea here, because the toggel triggers on -, not on !
403           //$usedName= preg_replace('/^-/', '! ', $usedName);
404         }else{
405           $usedName2= $usedName;
406         }
408         if(isset($this->list[$usedName][1])){
409           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
410         }else{
411           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
412         }
413       }
414     }
415     return($a_ret);
416   }
418   function genPkgs(){
419     /* Generate a list off available packages for this mirror, section and release
420      */
421     /* Only read this file if it wasn't read before */
422     if($this->buffer==NULL){
423       $this->buffer=array();
424       $a_ret = array();
425       foreach($this->FAIdebianSection as $sec){
426         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
428         if(!is_file($strID)){
429           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
430           unset($this->buffer);
431           return(array());
432         }
433         $fp = fopen($strID,"r");
435         /* Parse every line and create an array */
436         while(!feof($fp)){
437           $str= fgets($fp,512);
438           $stra= split("\|",$str);
439           if(count($stra)==4){
440             $a_ret[$stra[0]] = $stra;
441           }
442         }
443         fclose($fp);
444         /* Save our Data, to avoid reading it again */
445       }
446       $this->buffer = $a_ret;
447       ksort($a_ret);
448       return($a_ret);
449     }else{
450       return $this->buffer;
451     }
452   }
455   /* Save to LDAP */
456   function save()
457   {
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     prepare_to_save_FAI_object($this->dn,$this->attrs);
473     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
475     /* Do object tagging */
476     $this->handle_object_tagging();
477     $ldap->cd($this->dn);
479     /* Save Package configurations */
480     foreach($this->ConfiguredPackages as $pkgname => $attrs){
481       foreach($attrs as $name => $attr){
482       
483         $pkgattrs = array();
485         foreach($attr as $n=>$v){
486           if(empty($v)) $v = array();
487         }
489         /* Set attributes */
490         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
492         $pkgattrs['FAIpackage']           = $pkgname;
493         $pkgattrs['FAIvariable']          = $name;
494         $pkgattrs['FAIvariableType']      = $attr['Type'];
495         $pkgattrs['FAIvariableContent']   = $attr['Value'];
496         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
498         /* cehck if object already exists */
499         $ldap->cat($pkgdn,array("objectClass"));
501         /* Workaround for missing "gosaAdministrativeUnitTag" */
502         $attrs = $ldap->fetch();
503         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
504           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
505         }
507         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
508           prepare_to_save_FAI_object($pkgdn,$pkgattrs);
509         }
510   
511         /* Handle tagging */
512         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
513       }
514     }
515   }
517   /* Return plugin informations for acl handling */ 
518   function plInfo()
519   {
520     return (array( 
521           "plShortName" => _("Package"),
522           "plDescription" => _("FAI Package list"),
523           "plSelfModify"  => FALSE,
524           "plDepends"     => array(),
525           "plPriority"    => 28,
526           "plSection"     => array("administration"),
527           "plCategory"    => array("fai"),
528           "plProvidedAcls" => array(
529             "cn"                => _("Name"),
530             "description"       => _("Description"),
531             "FAIpackage"        => _("Packages"),
532             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
533             "FAIinstallMethod"  => _("Install Method"),
534             "FAIdebconfInfo"    => _("Package configuration"),
535             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
536           ));
537   }
539   function getServerInfos()
540   {
541     $ret = array();
542     $ldap = $this->config->get_ldap_link();
543     $ldap->cd($this->config->current['BASE']);
544     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
545     while($attrs = $ldap->fetch()){
546       if(isset($attrs['FAIrepository'])){
547         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
548           $obj = $attrs['FAIrepository'][$i];
549           $tmp = split("\|",$obj);
550           if(count($tmp)==4){
551             foreach(split(",",$tmp[3]) as $sec){
552               if(!empty($sec)){
553                 $ret[$tmp[2]][] =  $sec;
554               }
555             }
556           }
557         }
558       }
559     }
560     return($ret);
561   }
566 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
567 ?>