Code

Fixed saving for existing entries
[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     }
165   }
168   function execute()
169   {
170     /* Call parent execute */
171     plugin::execute();
173     if($this->is_account && !$this->view_logged){
174       $this->view_logged = TRUE;
175       new log("view","fai/".get_class($this),$this->dn);
176     }
178     /* Fill templating stuff */
179     $smarty= get_smarty();
180     $display= "";
182     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
184     if(!$this->is_account){
186       /* Assemble release name */
187       $faifilter = session::get('faifilter');
188       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $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     /* 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         }
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(!preg_match("/^freeze/", $this->FAIstate)){
242         $this->dialog->save_object();
243         if(count($this->dialog->check())){
244           foreach($this->dialog->check() as $msgs){
245             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
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       $pkg_config = array();
263       $pkg = $_POST['usedPackages'][0];
264       if(isset($this->ConfiguredPackages[$pkg])){
265         $pkg_config = $this->ConfiguredPackages[$pkg];
266       }
267       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
268       $this->is_dialog =true;
269     }
271     /* Configuration dialog open*/
272     if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
273       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
274         foreach($_POST['usedPackages'] as $pkg){
275           if (isset($this->usedPackages[$pkg])){
276             unset($this->usedPackages[$pkg]);
277             if (preg_match('/^-/', $pkg)){
278               $pkg= preg_replace('/^-/', '', $pkg);
279             } else {
280               $pkg= preg_replace('/^/', '-', $pkg);
281             }
282             $this->usedPackages[$pkg]= $pkg;
283           }
284         }
285       }
286     }
288     /* Save Configuration */
289     if(isset($_POST['SaveObjectConfig'])){
290       if(!preg_match("/^freeze/", $this->FAIstate)){
291         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
292       }
293       $this->dialog = false;
294       $this->is_dialog=false;
295     }
297     /* cancel configuration */     
298     if(isset($_POST['CancelObjectConfig'])){
299       $this->dialog = false;
300       $this->is_dialog=false;
301     }
303     /* Display dialog */ 
304     if($this->is_dialog){
305       return $this->dialog->execute();
306     }
308     /* Assign section to smarty */
309     $strsec = "";
310     foreach($this->FAIdebianSection as $sec){
311       $strsec .= $sec." ";
312     }
314     $tmp = $this->plInfo();
315     foreach($tmp['plProvidedAcls'] as $name => $translated){
316       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
317     }
319     $smarty->assign("releases",$this->releases);
320     $smarty->assign("release" ,$this->FAIdebianRelease);
321     $smarty->assign("sections",$this->sections);
322     $smarty->assign("section" ,$strsec);
323     $smarty->assign("usedPackages",$this->printUsedPackages());
324     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
325     return($display);
326   }
328   /* Delete me, and all my subtrees
329    */
330   function remove_from_parent()
331   {
332     $ldap = $this->config->get_ldap_link();
333     $ldap->cd ($this->dn);
335     $faifilter = session::get('faifilter');
336     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
337     if($faifilter['branch'] == "main"){
338       $use_dn = $this->dn;
339     }
341     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
343     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
345     foreach($this->ConfiguredPackages as $pkgname => $attrs){
346       foreach($attrs as $name => $attr){
347         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
348         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
349         if($faifilter['branch'] == "main"){
350           $use_dn = $obj['dn'];
351         }
352         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
353       }
354     }
355     $this->handle_post_events("remove");
356   }
359   /* Save data to object 
360    */
361   function save_object()
362   {
363     if(preg_match("/^freeze/", $this->FAIstate)) return;
364     plugin::save_object();
365   }
368   /* Check supplied data */
369   function check()
370   {
371     /* Call common method to give check the hook */
372     $message= plugin::check();
374     if(count($this->usedPackages)==0){
375       $message[]= _("Please select a least one package!");
376     }
378     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
379       $message[]=_("Please choose a valid release/section combination for your repository setup!");
380     }
382     /* Ensure that we do not overwrite an allready existing entry 
383      */
384     if($this->is_new){
385       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
386       $faifilter = session::get('faifilter');
387       if($faifilter['branch']!="main"){
388         $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
389       }
391       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
392       if(isset($res[$this->cn])){
393         $message[] = msgPool::duplicated(_("Name"));
394       }
395     }
396     return ($message);
397   }
399   function printUsedPackages(){
400     $a_ret=array(); 
401     if(is_array($this->usedPackages)) {
402       foreach($this->usedPackages as $usedName){
404         $config = 0;
406         foreach($this->ConfiguredPackages as $name => $value){
407           if($name == $usedName){
408             $config ++;
409           }
410         }
412         $c_str ="";
413         if($config){
414           $c_str = " - "._("package is configured");
415         }
417         /* Adapt used name if we're marked for removal */
418         $dsc= "";
419         if (preg_match('/^-/', $usedName)){
420           $dsc= " - "._("Package marked for removal");
421           // Generally a bad idea here, because the toggel triggers on -, not on !
422           //$usedName= preg_replace('/^-/', '! ', $usedName);
423         }else{
424           $usedName2= $usedName;
425         }
427         if(isset($this->list[$usedName][1])){
428           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
429         }else{
430           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
431         }
432       }
433     }
434     return($a_ret);
435   }
438   function genPkgs()
439   {
440     $start = microtime(1);
441     if($this->buffer === NULL || !count($this->buffer)){
442       $this->buffer = array();
443       $q = new gosaSupportDaemon();
444       $attrs = array("distribution", "package","version", "section", "description", "timestamp","template=''");
445       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs);
446       if($q->is_error()){
447         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
448       }else{
449         foreach($ret as $attr){
450           $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
451         }
452       }
453     }
454     
455 #    printf("%0.6f",(microtime(1) - $start));
456     return $this->buffer;
457   }
460   /* Save to LDAP */
461   function save()
462   {
464     /* Assemble release name */
465     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
466       $faifilter = session::get('faifilter');
467       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
468       $tmp= preg_replace('/ou=/', '', $tmp);
469       $rev= array_reverse(split(',', $tmp));
470       $this->FAIdebianRelease= "";
471       foreach ($rev as $part){
472         $this->FAIdebianRelease.= "/$part";
473       }
474       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
475     }
477     plugin::save();
479     $ldap = $this->config->get_ldap_link();
481     $this->attrs['FAIpackage'] = array();
482     foreach($this->usedPackages as $pkg => $obj){
483       $this->attrs['FAIpackage'][] = $pkg;
484     } 
486     $this->attrs['FAIdebianSection'] = array();
487     foreach($this->FAIdebianSection as $sec){
488       $this->attrs['FAIdebianSection'][] = $sec;
489     }
491     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
492     
493     if($this->initially_was_account){
494       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
495     }else{
496       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
497     }
499     $ldap->cd($this->dn);
501     /* Save Package configurations */
502     foreach($this->ConfiguredPackages as $pkgname => $attrs){
503       foreach($attrs as $name => $attr){
504       
505         $pkgattrs = array();
507         foreach($attr as $n=>$v){
508           if(empty($v)) $v = array();
509         }
511         /* Set attributes */
512         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
514         $pkgattrs['FAIpackage']           = $pkgname;
515         $pkgattrs['FAIvariable']          = $name;
516         $pkgattrs['FAIvariableType']      = $attr['Type'];
517         $pkgattrs['FAIvariableContent']   = $attr['Value'];
518         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
520         /* Tag object */
521         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
523         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
524           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
525         }
526       }
527     }
528   }
530   /* Return plugin informations for acl handling */ 
531   static function plInfo()
532   {
533     return (array( 
534           "plShortName" => _("Package"),
535           "plDescription" => _("FAI Package list"),
536           "plSelfModify"  => FALSE,
537           "plDepends"     => array(),
538           "plPriority"    => 28,
539           "plSection"     => array("administration"),
540           "plCategory"    => array("fai"),
541           "plProvidedAcls" => array(
542             "cn"                => _("Name"),
543             "description"       => _("Description"),
544             "FAIpackage"        => _("Packages"),
545             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
546             "FAIinstallMethod"  => _("Install Method"),
547             "FAIdebconfInfo"    => _("Package configuration"),
548             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
549           ));
550   }
555   function PrepareForCopyPaste($source)
556   {
557     plugin::PrepareForCopyPaste($source);
559     if(isset($source['FAIstate'][0])){
560       $this->FAIstate = $source['FAIstate'][0];
561     }
563     $this->FAIdebianRelease = "ClearFromCopyPaste";
565     if(isset($source['FAIpackage'])){
566       unset($source['FAIpackage']['count']);
567       foreach($source['FAIpackage'] as $pkg){
568         $this->usedPackages[$pkg] = $pkg;
569       }
570       ksort($this->usedPackages);
571     }else{
572       $this->usedPackages = array();
573     }
575     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
576       $this->FAIdebianSection = array();
577       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
578         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
579       }
580     }
582     /* Create one filter with all package names, 
583        instead of calling $ldap->search for every single package 
584      */
585     $PackageFilter = "";
586     foreach($this->usedPackages as $name){
587       $PackageFilter .= "(FAIpackage=".$name.")";
588     }
589     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
591     /* Search for configuration objects */ 
592     $ldap = $this->config->get_ldap_link();
593     $ldap->cd($source['dn']);
594     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
596     /* Walk through configurations and append them to our list of ConfiguredPackages */
597     while($attr = $ldap->fetch()){
599       /* Skip objects, that are tagged as removed */
600       if(isset($object['FAIstate'][0])){
601         if(preg_match("/removed$/",$attr['FAIstate'][0])){
602           continue;
603         }
604       }
606       $tmp =array(); 
607       $tmp['Name']  = $attr['FAIvariable'][0];
608       $tmp['Type']  = $attr['FAIvariableType'][0];
610       if (isset($attr['FAIvariableContent'][0])){
611         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
612           $tmp['Value'] = $attr['FAIvariableContent'][0];
613         }else{
614           $content = array();        
615           unset($attr['FAIvariableContent']['count']);
616           foreach($attr['FAIvariableContent'] as $attr){
617             $tmp['Value'][] = $attr;
618           }
619         }
620         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
621       }
622     }
623   }
626   function getServerInfos()
627   {
628     $ret = array();
629     $ldap = $this->config->get_ldap_link();
630     $ldap->cd($this->config->current['BASE']);
631     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
632     while($attrs = $ldap->fetch()){
633       if(isset($attrs['FAIrepository'])){
634         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
635           $obj = $attrs['FAIrepository'][$i];
636           $tmp = split("\|",$obj);
637           if(count($tmp)==4){
638             foreach(split(",",$tmp[3]) as $sec){
639               if(!empty($sec)){
640                 $ret[$tmp[2]][] =  $sec;
641               }
642             }
643           }
644         }
645       }
646     }
647     return($ret);
648   }
651   /*! \brief  Used for copy & paste.
652     Returns a HTML input mask, which allows to change the cn of this entry.
653     @param  Array   Array containing current status && a HTML template.
654    */
655   function getCopyDialog()
656   {
657     $vars = array("cn");
658     $smarty = get_smarty();
659     $smarty->assign("cn", htmlentities($this->cn));
660     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
661     $ret = array();
662     $ret['string'] = $str;
663     $ret['status'] = "";
664     return($ret);
665   }
668   /*! \brief  Used for copy & paste.
669     Some entries must be renamed to avaoid duplicate entries.
670    */
671   function saveCopyDialog()
672   {
673     if(isset($_POST['cn'])){
674       $this->cn = get_post('cn');
675     }
676   }
679 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
680 ?>