Code

Removed a couple of left over call time references
[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     $faifilter = session::get('faifilter');
340     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
341     if($faifilter['branch'] == "main"){
342       $use_dn = $this->dn;
343     }
345     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
347     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
349     foreach($this->ConfiguredPackages as $pkgname => $attrs){
350       foreach($attrs as $name => $attr){
351         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
352         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
353         if($faifilter['branch'] == "main"){
354           $use_dn = $obj['dn'];
355         }
356         FAI::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       $faifilter = session::get('faifilter');
472       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
473       $tmp= preg_replace('/ou=/', '', $tmp);
474       $rev= array_reverse(split(',', $tmp));
475       $this->FAIdebianRelease= "";
476       foreach ($rev as $part){
477         $this->FAIdebianRelease.= "/$part";
478       }
479       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
480     }
482     plugin::save();
484     $ldap = $this->config->get_ldap_link();
486     $this->attrs['FAIpackage'] = array();
487     foreach($this->usedPackages as $pkg => $obj){
488       $this->attrs['FAIpackage'][] = $pkg;
489     } 
491     $this->attrs['FAIdebianSection'] = array();
492     foreach($this->FAIdebianSection as $sec){
493       $this->attrs['FAIdebianSection'][] = $sec;
494     }
496     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
497     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
498     
499     if($this->initially_was_account){
500       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
501     }else{
502       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
503     }
505     $ldap->cd($this->dn);
507     /* Save Package configurations */
508     foreach($this->ConfiguredPackages as $pkgname => $attrs){
509       foreach($attrs as $name => $attr){
510       
511         $pkgattrs = array();
513         foreach($attr as $n=>$v){
514           if(empty($v)) $v = array();
515         }
517         /* Set attributes */
518         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
520         $pkgattrs['FAIpackage']           = $pkgname;
521         $pkgattrs['FAIvariable']          = $name;
522         $pkgattrs['FAIvariableType']      = $attr['Type'];
523         $pkgattrs['FAIvariableContent']   = $attr['Value'];
524         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
526         /* Tag object */
527         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
529         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
530           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
531         }
532       }
533     }
534   }
536   /* Return plugin informations for acl handling */ 
537   static function plInfo()
538   {
539     return (array( 
540           "plShortName" => _("Package"),
541           "plDescription" => _("FAI Package list"),
542           "plSelfModify"  => FALSE,
543           "plDepends"     => array(),
544           "plPriority"    => 28,
545           "plSection"     => array("administration"),
546           "plCategory"    => array("fai"),
547           "plProvidedAcls" => array(
548             "cn"                => _("Name"),
549             "description"       => _("Description"),
550             "FAIpackage"        => _("Packages"),
551             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
552             "FAIinstallMethod"  => _("Install Method"),
553             "FAIdebconfInfo"    => _("Package configuration"),
554             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
555           ));
556   }
561   function PrepareForCopyPaste($source)
562   {
563     plugin::PrepareForCopyPaste($source);
565     if(isset($source['FAIstate'][0])){
566       $this->FAIstate = $source['FAIstate'][0];
567     }
569     $this->FAIdebianRelease = "ClearFromCopyPaste";
571     if(isset($source['FAIpackage'])){
572       unset($source['FAIpackage']['count']);
573       foreach($source['FAIpackage'] as $pkg){
574         $this->usedPackages[$pkg] = $pkg;
575       }
576       ksort($this->usedPackages);
577     }else{
578       $this->usedPackages = array();
579     }
581     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
582       $this->FAIdebianSection = array();
583       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
584         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
585       }
586     }
588     /* Create one filter with all package names, 
589        instead of calling $ldap->search for every single package 
590      */
591     $PackageFilter = "";
592     foreach($this->usedPackages as $name){
593       $PackageFilter .= "(FAIpackage=".$name.")";
594     }
595     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
597     /* Search for configuration objects */ 
598     $ldap = $this->config->get_ldap_link();
599     $ldap->cd($source['dn']);
600     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
602     /* Walk through configurations and append them to our list of ConfiguredPackages */
603     while($attr = $ldap->fetch()){
605       /* Skip objects, that are tagged as removed */
606       if(isset($object['FAIstate'][0])){
607         if(preg_match("/removed$/",$attr['FAIstate'][0])){
608           continue;
609         }
610       }
612       $tmp =array(); 
613       $tmp['Name']  = $attr['FAIvariable'][0];
614       $tmp['Type']  = $attr['FAIvariableType'][0];
616       if (isset($attr['FAIvariableContent'][0])){
617         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
618           $tmp['Value'] = $attr['FAIvariableContent'][0];
619         }else{
620           $content = array();        
621           unset($attr['FAIvariableContent']['count']);
622           foreach($attr['FAIvariableContent'] as $attr){
623             $tmp['Value'][] = $attr;
624           }
625         }
626         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
627       }
628     }
629   }
632   function getServerInfos()
633   {
634     $ret = array();
635     $ldap = $this->config->get_ldap_link();
636     $ldap->cd($this->config->current['BASE']);
637     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
638     while($attrs = $ldap->fetch()){
639       if(isset($attrs['FAIrepository'])){
640         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
641           $obj = $attrs['FAIrepository'][$i];
642           $tmp = split("\|",$obj);
643           if(count($tmp)==4){
644             foreach(split(",",$tmp[3]) as $sec){
645               if(!empty($sec)){
646                 $ret[$tmp[2]][] =  $sec;
647               }
648             }
649           }
650         }
651       }
652     }
653     return($ret);
654   }
659 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
660 ?>