Code

5b9f1dee40c73b8aaef4c7b643b484a692042dda
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "";
16   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description"); 
23   var $sub64coded       = array();
25   var $ConfiguredPackages = array();
27   /* Specific attributes */
28   var $cn               = "";       // The class name for this object
29   var $description      = "";       // The description for this set of partitions
30   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
31   var $SubObjects       = array();  // All leafobjects of this object
33   var $FAIdebianRelease          = ""; // The selected release
34   var $FAIdebianSection          = array(); // selected section
35   var $FAIinstallMethod          = "aptitude"; // hard coded
36   var $mirror                    = ""; // selected mirror
38   var $servers          = array();  // All available servers
39   var $releases         = array();  // All possible releases 
40   var $sections         = array();  // All section types
42   var $list             = NULL;
44   var $mirrors          = array();  // The combination of server/release/section
45   var $confDir          = "";
46   var $usedPackages     = array();
47   var $buffer           = NULL; 
48   var $strID            ="";
49   var $newDialogShown   =false;
51   var $FAIstate         = "";
52   var $view_logged      = FALSE;
53   var $base;
54   var $FAIpackage ;
56   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
57       "dselect-upgrade", "taskinst", "taskrm",
58       "hold", "clean", "aptitude", "aptitude-r",
59       "pending", "dpkgc" );
62   function faiPackage (&$config, $dn= NULL)
63   {
64     /* Load Attributes */
65     plugin::plugin ($config, $dn);
67     /* If "dn==new" we try to create a new entry
68      * Else we must read all objects from ldap which belong to this entry.
69      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
70      */
71     if($dn != "new"){
72       $this->dn =$dn;
74       /* Get FAIstate */
75       if(isset($this->attrs['FAIstate'][0])){
76         $this->FAIstate = $this->attrs['FAIstate'][0];
77       }
79       /* Check if there are already some packages in this list 
80        */
81       $this->usedPackages = array();
82       if(isset($this->attrs['FAIpackage'])){
83         unset($this->attrs['FAIpackage']['count']);
84         foreach($this->attrs['FAIpackage'] as $pkg){
85           $this->usedPackages[$pkg] = $pkg;
86         }
87         ksort($this->usedPackages);
88       }  
90       /* Fetch all package configurations from ldap 
91        */
92       $PackageFilter = "";
93       foreach($this->usedPackages as $name){
94         $PackageFilter .= "(FAIpackage=".$name.")";
95       }
96       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
98       /* Search for configuration objects */ 
99       $ldap = $this->config->get_ldap_link();
100       $ldap->cd($this->dn);
101       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
102             "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
104       /* Walk through configurations and append them to our list of ConfiguredPackages 
105        */
106       while($attr = $ldap->fetch()){
108         /* Skip objects, that are tagged as removed */
109         if(isset($object['FAIstate'][0])){
110           if(preg_match("/removed$/",$attr['FAIstate'][0])){
111             continue;
112           }
113         }
115         $tmp =array(); 
116         $tmp['Name']  = $attr['FAIvariable'][0];
117         $tmp['Type']  = $attr['FAIvariableType'][0];
119         if (isset($attr['FAIvariableContent'][0])){
120           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
121             $tmp['Value'] = $attr['FAIvariableContent'][0];
122           }else{
123             $content = array();        
124             unset($attr['FAIvariableContent']['count']);
125             foreach($attr['FAIvariableContent'] as $attr){
126               $tmp['Value'][] = $attr;
127             }
128           }
129           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
130         }
131       }
133       if (isset($this->attrs['FAIdebianSection']['count'])){
134         unset($this->attrs['FAIdebianSection']['count']);
135       }
136       if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
137         $this->FAIdebianSection = array();
138         foreach($this->attrs['FAIdebianSection'] as $sec){
139           $this->FAIdebianSection[$sec]=$sec;
140         }
141       }
143       if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
144         $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
145       }
146       $this->confDir = CONFIG_DIR."/fai/";
147       $this->FAIpackage = array();
149     } // ENDE  dn != new  
151     $methods = array();
152     foreach($this->FAIinstallMethods as $method){
153       $methods[$method] = $method;
154     }
155     $this->FAIinstallMethods = $methods;
157     /* Check if we exist already - no need to ask for revisions, etc. then */
158     if ($this->dn != "new"){
159       $this->newDialogShown= true;
160     }
161     $this->is_new = FALSE;
162     if($this->dn == "new"){
163       $this->is_new =TRUE;
164     }
166     /* Generate package list */
167     $this->list= $this->genPkgs(TRUE);
168   }
171   function execute()
172   {
173     /* Call parent execute */
174     plugin::execute();
176     if($this->is_account && !$this->view_logged){
177       $this->view_logged = TRUE;
178       new log("view","fai/".get_class($this),$this->dn);
179     }
181     /* Fill templating stuff */
182     $smarty= get_smarty();
183     $display= "";
185     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
187     if(!$this->is_account){
189       /* Assemble release name */
190       $faifilter = session::get('faifilter');
191       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
192       $tmp= preg_replace('/ou=/', '', $tmp);
193       $rev= array_reverse(split(',', $tmp));
194       $this->FAIdebianRelease= "";
195       foreach ($rev as $part){
196         $this->FAIdebianRelease.= "/$part";
197       }
198       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
200       /* Assemble sections */
201       $repos= $this->getServerInfos();
202       if(isset($repos[$this->FAIdebianRelease])){
203         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
204         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
205       }
207       /* Assign Repository settings*/ 
208       $this->is_account     = true;
209     }
211     /* Assign variables */
212     foreach($this->attributes as $attrs){
213       $smarty->assign($attrs,$this->$attrs);
214     }
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->FAIdebianRelease,$this->usedPackages);
219       $this->is_dialog =true;
220     }
222     /* Delete selected package */ 
223     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
224       if(!preg_match("/^freeze/", $this->FAIstate)){
225         foreach($_POST['usedPackages'] as $del){
226           if(isset($this->usedPackages[$del])){
227             unset($this->usedPackages[$del]);
228           }
229         }
231         /* Generate package list */
232         $this->list= $this->genPkgs(TRUE);
233       }
234     }
236     /* Abort package selection dialog */ 
237     if(isset($_POST['CancelSubObject'])){
238       $this->dialog = false;
239       $this->is_dialog=false;
240     }
242     /* attach new packages */
243     if(isset($_POST['SaveSubObject'])) {
244       if(!preg_match("/^freeze/", $this->FAIstate)){
245         $this->dialog->save_object();
246         if(count($this->dialog->check())){
247           foreach($this->dialog->check() as $msgs){
248             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
249           }
250         }else{
251           $use = $this->dialog->save();
252           $this->usedPackages = $use;
253           $this->dialog = false;
254           $this->is_dialog=false;
255           ksort($this->usedPackages);
257           /* Generate package list */
258           $this->list= $this->genPkgs(TRUE);
259         }
260       }else{
261         $this->dialog = false;
262         $this->is_dialog=false;
263       }
264     }
266     /* Configuration dialog open*/
267     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
268       $pkg_config = array();
269       $pkg = $_POST['usedPackages'][0];
270       if(isset($this->ConfiguredPackages[$pkg])){
271         $pkg_config = $this->ConfiguredPackages[$pkg];
272       }
273       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
274       $this->is_dialog =true;
275     }
277     /* Configuration dialog open*/
278     if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
279       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
280         foreach($_POST['usedPackages'] as $pkg){
281           if (isset($this->usedPackages[$pkg])){
282             unset($this->usedPackages[$pkg]);
283             if (preg_match('/^-/', $pkg)){
284               $pkg= preg_replace('/^-/', '', $pkg);
285             } else {
286               $pkg= preg_replace('/^/', '-', $pkg);
287             }
288             $this->usedPackages[$pkg]= $pkg;
290           }
291         }
293         /* Generate package list */
294         $this->list= $this->genPkgs(TRUE);
295       }
296     }
298     /* Save Configuration */
299     if(isset($_POST['SaveObjectConfig'])){
300       if(!preg_match("/^freeze/", $this->FAIstate)){
301         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
302       }
303       $this->dialog = false;
304       $this->is_dialog=false;
305     }
307     /* cancel configuration */     
308     if(isset($_POST['CancelObjectConfig'])){
309       $this->dialog = false;
310       $this->is_dialog=false;
311     }
313     /* Display dialog */ 
314     if($this->is_dialog){
315       $this->dialog->save_object();
316       return $this->dialog->execute();
317     }
319     /* Assign section to smarty */
320     $strsec = "";
321     foreach($this->FAIdebianSection as $sec){
322       $strsec .= $sec." ";
323     }
325     $tmp = $this->plInfo();
326     foreach($tmp['plProvidedAcls'] as $name => $translated){
327       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
328     }
330     $smarty->assign("releases",$this->releases);
331     $smarty->assign("release" ,$this->FAIdebianRelease);
332     $smarty->assign("sections",$this->sections);
333     $smarty->assign("section" ,$strsec);
334     $smarty->assign("usedPackages",$this->printUsedPackages());
335     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
336     return($display);
337   }
339   /* Delete me, and all my subtrees
340    */
341   function remove_from_parent()
342   {
343     $ldap = $this->config->get_ldap_link();
344     $ldap->cd ($this->dn);
346     $faifilter = session::get('faifilter');
347     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
348     if($faifilter['branch'] == "main"){
349       $use_dn = $this->dn;
350     }
352     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
354     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
356     foreach($this->ConfiguredPackages as $pkgname => $attrs){
357       foreach($attrs as $name => $attr){
358         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
359         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
360         if($faifilter['branch'] == "main"){
361           $use_dn = $obj['dn'];
362         }
363         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
364       }
365     }
366     $this->handle_post_events("remove");
367   }
370   /* Save data to object 
371    */
372   function save_object()
373   {
374     if(preg_match("/^freeze/", $this->FAIstate)) return;
375     plugin::save_object();
376   }
379   /* Check supplied data */
380   function check()
381   {
382     /* Call common method to give check the hook */
383     $message= plugin::check();
385     if(count($this->usedPackages)==0){
386       $message[]= _("Please select a least one package!");
387     }
389     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
390       $message[]=_("Please choose a valid release/section combination for your repository setup!");
391     }
393     /* Ensure that we do not overwrite an allready existing entry 
394      */
395     if($this->is_new){
396       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
397       $faifilter = session::get('faifilter');
398       if($faifilter['branch']!="main"){
399         $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
400       }
402       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
403       if(isset($res[$this->cn])){
404         $message[] = msgPool::duplicated(_("Name"));
405       }
406     }
407     return ($message);
408   }
411   function printUsedPackages()
412   {
413     $a_ret=array(); 
414     if(is_array($this->usedPackages)){
415       foreach($this->usedPackages as $usedName){
417         /* Append message if package is configured */
418         $c_str ="";
419         if(isset($this->ConfiguredPackages[$usedName])){
420           $c_str = " - "._("package is configured");
421         }
423         /* Adapt used name if we're marked for removal */
424         $dsc= "";
425         if (preg_match('/^-/', $usedName)){
426           $dsc= " - "._("Package marked for removal");
427         }else{
428           $usedName2= $usedName;
429         }
431         /* Append version string, if available */
432         if(isset($this->list[$usedName]['VERSION'])){
433           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName]['VERSION']."]".$c_str.$dsc;
434         }else{
435           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
436         }
437       }
438     }
439     return($a_ret);
440   }
443   function genPkgs($force = false)
444   {
445     if(!count($this->buffer) || $force){
446       $q = new gosaSupportDaemon();
447       $attrs = array("distribution", "package","version", "section", "description", "timestamp");
448       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$this->usedPackages);
449       if($q->is_error()){
450         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
451       }else{
452         foreach($ret as $attr){
453           $this->buffer[$attr['PACKAGE']] = $attr;
454         }
455       }
456     }
457     return $this->buffer;
458   }
461   /* Save to LDAP */
462   function save()
463   {
465     /* Assemble release name */
466     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
467       $faifilter = session::get('faifilter');
468       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
469       $tmp= preg_replace('/ou=/', '', $tmp);
470       $rev= array_reverse(split(',', $tmp));
471       $this->FAIdebianRelease= "";
472       foreach ($rev as $part){
473         $this->FAIdebianRelease.= "/$part";
474       }
475       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
476     }
478     plugin::save();
480     $ldap = $this->config->get_ldap_link();
482     $this->attrs['FAIpackage'] = array();
483     foreach($this->usedPackages as $pkg => $obj){
484       $this->attrs['FAIpackage'][] = $pkg;
485     } 
487     $this->attrs['FAIdebianSection'] = array();
488     foreach($this->FAIdebianSection as $sec){
489       $this->attrs['FAIdebianSection'][] = $sec;
490     }
492     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
493     
494     if($this->initially_was_account){
495       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
496     }else{
497       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
498     }
500     $ldap->cd($this->dn);
502     /* Save Package configurations */
503     foreach($this->ConfiguredPackages as $pkgname => $attrs){
504       foreach($attrs as $name => $attr){
505       
506         $pkgattrs = array();
508         foreach($attr as $n=>$v){
509           if(empty($v)) $v = array();
510         }
512         /* Set attributes */
513         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
515         $pkgattrs['FAIpackage']           = $pkgname;
516         $pkgattrs['FAIvariable']          = $name;
517         $pkgattrs['FAIvariableType']      = $attr['Type'];
518         $pkgattrs['FAIvariableContent']   = $attr['Value'];
519         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
521         /* Tag object */
522         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
524         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
525           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
526         }
527       }
528     }
529   }
531   /* Return plugin informations for acl handling */ 
532   static function plInfo()
533   {
534     return (array( 
535           "plShortName" => _("Package"),
536           "plDescription" => _("FAI Package list"),
537           "plSelfModify"  => FALSE,
538           "plDepends"     => array(),
539           "plPriority"    => 28,
540           "plSection"     => array("administration"),
541           "plCategory"    => array("fai"),
542           "plProvidedAcls" => array(
543             "cn"                => _("Name"),
544             "description"       => _("Description"),
545             "FAIpackage"        => _("Packages"),
546             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
547             "FAIinstallMethod"  => _("Install Method"),
548             "FAIdebconfInfo"    => _("Package configuration"),
549             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
550           ));
551   }
556   function PrepareForCopyPaste($source)
557   {
558     plugin::PrepareForCopyPaste($source);
560     if(isset($source['FAIstate'][0])){
561       $this->FAIstate = $source['FAIstate'][0];
562     }
564     $this->FAIdebianRelease = "ClearFromCopyPaste";
566     if(isset($source['FAIpackage'])){
567       unset($source['FAIpackage']['count']);
568       foreach($source['FAIpackage'] as $pkg){
569         $this->usedPackages[$pkg] = $pkg;
570       }
571       ksort($this->usedPackages);
572     }else{
573       $this->usedPackages = array();
574     }
576     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
577       $this->FAIdebianSection = array();
578       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
579         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
580       }
581     }
583     /* Create one filter with all package names, 
584        instead of calling $ldap->search for every single package 
585      */
586     $PackageFilter = "";
587     foreach($this->usedPackages as $name){
588       $PackageFilter .= "(FAIpackage=".$name.")";
589     }
590     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
592     /* Search for configuration objects */ 
593     $ldap = $this->config->get_ldap_link();
594     $ldap->cd($source['dn']);
595     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
597     /* Walk through configurations and append them to our list of ConfiguredPackages */
598     while($attr = $ldap->fetch()){
600       /* Skip objects, that are tagged as removed */
601       if(isset($object['FAIstate'][0])){
602         if(preg_match("/removed$/",$attr['FAIstate'][0])){
603           continue;
604         }
605       }
607       $tmp =array(); 
608       $tmp['Name']  = $attr['FAIvariable'][0];
609       $tmp['Type']  = $attr['FAIvariableType'][0];
611       if (isset($attr['FAIvariableContent'][0])){
612         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
613           $tmp['Value'] = $attr['FAIvariableContent'][0];
614         }else{
615           $content = array();        
616           unset($attr['FAIvariableContent']['count']);
617           foreach($attr['FAIvariableContent'] as $attr){
618             $tmp['Value'][] = $attr;
619           }
620         }
621         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
622       }
623     }
624   }
627   function getServerInfos()
628   {
629     $ret = array();
630     $ldap = $this->config->get_ldap_link();
631     $ldap->cd($this->config->current['BASE']);
632     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
633     while($attrs = $ldap->fetch()){
634       if(isset($attrs['FAIrepository'])){
635         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
636           $obj = $attrs['FAIrepository'][$i];
637           $tmp = split("\|",$obj);
638           if(count($tmp)==4){
639             foreach(split(",",$tmp[3]) as $sec){
640               if(!empty($sec)){
641                 $ret[$tmp[2]][] =  $sec;
642               }
643             }
644           }
645         }
646       }
647     }
648     return($ret);
649   }
652   /*! \brief  Used for copy & paste.
653     Returns a HTML input mask, which allows to change the cn of this entry.
654     @param  Array   Array containing current status && a HTML template.
655    */
656   function getCopyDialog()
657   {
658     $vars = array("cn");
659     $smarty = get_smarty();
660     $smarty->assign("cn", htmlentities($this->cn));
661     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
662     $ret = array();
663     $ret['string'] = $str;
664     $ret['status'] = "";
665     return($ret);
666   }
669   /*! \brief  Used for copy & paste.
670     Some entries must be renamed to avaoid duplicate entries.
671    */
672   function saveCopyDialog()
673   {
674     if(isset($_POST['cn'])){
675       $this->cn = get_post('cn');
676     }
677   }
680 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
681 ?>