Code

Updated package stuff && si
[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     /* + was pressed to open the package dialog */
214     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
215       $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
216       $this->is_dialog =true;
217     }
219     /* Delete selected package */ 
220     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
221       if(!preg_match("/^freeze/", $this->FAIstate)){
222         foreach($_POST['usedPackages'] as $del){
223           if(isset($this->usedPackages[$del])){
224             unset($this->usedPackages[$del]);
225           }
226         }
228         /* Generate package list */
229         $this->list= $this->genPkgs(TRUE);
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);
254           /* Generate package list */
255           $this->list= $this->genPkgs(TRUE);
256         }
257       }else{
258         $this->dialog = false;
259         $this->is_dialog=false;
260       }
261     }
263     /* Configuration dialog open*/
264     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
265       $pkg_config = array();
266       $pkg = $_POST['usedPackages'][0];
267       if(isset($this->ConfiguredPackages[$pkg])){
268         $pkg_config = $this->ConfiguredPackages[$pkg];
269       }
270       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
271       $this->is_dialog =true;
272     }
274     /* Configuration dialog open*/
275     if(preg_match('/^freeze/', $this->FAIstate) && $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;
287           }
288         }
290         /* Generate package list */
291         $this->list= $this->genPkgs(TRUE);
292       }
293     }
295     /* Save Configuration */
296     if(isset($_POST['SaveObjectConfig'])){
297       if(!preg_match("/^freeze/", $this->FAIstate)){
298         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
299       }
300       $this->dialog = false;
301       $this->is_dialog=false;
302     }
304     /* cancel configuration */     
305     if(isset($_POST['CancelObjectConfig'])){
306       $this->dialog = false;
307       $this->is_dialog=false;
308     }
310     /* Display dialog */ 
311     if($this->is_dialog){
312       $this->dialog->save_object();
313       return $this->dialog->execute();
314     }
316     /* Assign section to smarty */
317     $strsec = "";
318     foreach($this->FAIdebianSection as $sec){
319       $strsec .= $sec." ";
320     }
322     $tmp = $this->plInfo();
323     foreach($tmp['plProvidedAcls'] as $name => $translated){
324       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
325     }
327     $smarty->assign("releases",$this->releases);
328     $smarty->assign("release" ,$this->FAIdebianRelease);
329     $smarty->assign("sections",$this->sections);
330     $smarty->assign("section" ,$strsec);
331     $smarty->assign("usedPackages",$this->printUsedPackages());
332     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
333     return($display);
334   }
336   /* Delete me, and all my subtrees
337    */
338   function remove_from_parent()
339   {
340     $ldap = $this->config->get_ldap_link();
341     $ldap->cd ($this->dn);
343     $faifilter = session::get('faifilter');
344     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
345     if($faifilter['branch'] == "main"){
346       $use_dn = $this->dn;
347     }
349     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
351     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
353     foreach($this->ConfiguredPackages as $pkgname => $attrs){
354       foreach($attrs as $name => $attr){
355         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
356         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
357         if($faifilter['branch'] == "main"){
358           $use_dn = $obj['dn'];
359         }
360         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
361       }
362     }
363     $this->handle_post_events("remove");
364   }
367   /* Save data to object 
368    */
369   function save_object()
370   {
371     if(preg_match("/^freeze/", $this->FAIstate)) return;
372     plugin::save_object();
373   }
376   /* Check supplied data */
377   function check()
378   {
379     /* Call common method to give check the hook */
380     $message= plugin::check();
382     if(count($this->usedPackages)==0){
383       $message[]= _("Please select a least one package!");
384     }
386     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
387       $message[]=_("Please choose a valid release/section combination for your repository setup!");
388     }
390     /* Ensure that we do not overwrite an allready existing entry 
391      */
392     if($this->is_new){
393       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
394       $faifilter = session::get('faifilter');
395       if($faifilter['branch']!="main"){
396         $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
397       }
399       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
400       if(isset($res[$this->cn])){
401         $message[] = msgPool::duplicated(_("Name"));
402       }
403     }
404     return ($message);
405   }
408   function printUsedPackages()
409   {
410     $a_ret=array(); 
411     if(is_array($this->usedPackages)){
412       foreach($this->usedPackages as $usedName){
414         /* Append message if package is configured */
415         $c_str ="";
416         if(isset($this->ConfiguredPackages[$usedName])){
417           $c_str = " - "._("package is configured");
418         }
420         /* Adapt used name if we're marked for removal */
421         $dsc= "";
422         if (preg_match('/^-/', $usedName)){
423           $dsc= " - "._("Package marked for removal");
424         }else{
425           $usedName2= $usedName;
426         }
428         /* Append version string, if available */
429         if(isset($this->list[$usedName]['VERSION'])){
430           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName]['VERSION']."]".$c_str.$dsc;
431         }else{
432           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
433         }
434       }
435     }
436     return($a_ret);
437   }
440   function genPkgs($force = false)
441   {
442     if(!count($this->buffer) || $force){
443       $q = new gosaSupportDaemon();
444       $attrs = array("distribution", "package","version", "section", "description", "timestamp");
445       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$this->usedPackages);
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']] = $attr;
451         }
452       }
453     }
454     return $this->buffer;
455   }
458   /* Save to LDAP */
459   function save()
460   {
462     /* Assemble release name */
463     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
464       $faifilter = session::get('faifilter');
465       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
466       $tmp= preg_replace('/ou=/', '', $tmp);
467       $rev= array_reverse(split(',', $tmp));
468       $this->FAIdebianRelease= "";
469       foreach ($rev as $part){
470         $this->FAIdebianRelease.= "/$part";
471       }
472       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
473     }
475     plugin::save();
477     $ldap = $this->config->get_ldap_link();
479     $this->attrs['FAIpackage'] = array();
480     foreach($this->usedPackages as $pkg => $obj){
481       $this->attrs['FAIpackage'][] = $pkg;
482     } 
484     $this->attrs['FAIdebianSection'] = array();
485     foreach($this->FAIdebianSection as $sec){
486       $this->attrs['FAIdebianSection'][] = $sec;
487     }
489     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
490     
491     if($this->initially_was_account){
492       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
493     }else{
494       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
495     }
497     $ldap->cd($this->dn);
499     /* Save Package configurations */
500     foreach($this->ConfiguredPackages as $pkgname => $attrs){
501       foreach($attrs as $name => $attr){
502       
503         $pkgattrs = array();
505         foreach($attr as $n=>$v){
506           if(empty($v)) $v = array();
507         }
509         /* Set attributes */
510         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
512         $pkgattrs['FAIpackage']           = $pkgname;
513         $pkgattrs['FAIvariable']          = $name;
514         $pkgattrs['FAIvariableType']      = $attr['Type'];
515         $pkgattrs['FAIvariableContent']   = $attr['Value'];
516         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
518         /* Tag object */
519         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
521         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
522           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
523         }
524       }
525     }
526   }
528   /* Return plugin informations for acl handling */ 
529   static function plInfo()
530   {
531     return (array( 
532           "plShortName" => _("Package"),
533           "plDescription" => _("FAI Package list"),
534           "plSelfModify"  => FALSE,
535           "plDepends"     => array(),
536           "plPriority"    => 28,
537           "plSection"     => array("administration"),
538           "plCategory"    => array("fai"),
539           "plProvidedAcls" => array(
540             "cn"                => _("Name"),
541             "description"       => _("Description"),
542             "FAIpackage"        => _("Packages"),
543             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
544             "FAIinstallMethod"  => _("Install Method"),
545             "FAIdebconfInfo"    => _("Package configuration"),
546             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
547           ));
548   }
553   function PrepareForCopyPaste($source)
554   {
555     plugin::PrepareForCopyPaste($source);
557     if(isset($source['FAIstate'][0])){
558       $this->FAIstate = $source['FAIstate'][0];
559     }
561     $this->FAIdebianRelease = "ClearFromCopyPaste";
563     if(isset($source['FAIpackage'])){
564       unset($source['FAIpackage']['count']);
565       foreach($source['FAIpackage'] as $pkg){
566         $this->usedPackages[$pkg] = $pkg;
567       }
568       ksort($this->usedPackages);
569     }else{
570       $this->usedPackages = array();
571     }
573     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
574       $this->FAIdebianSection = array();
575       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
576         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
577       }
578     }
580     /* Create one filter with all package names, 
581        instead of calling $ldap->search for every single package 
582      */
583     $PackageFilter = "";
584     foreach($this->usedPackages as $name){
585       $PackageFilter .= "(FAIpackage=".$name.")";
586     }
587     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
589     /* Search for configuration objects */ 
590     $ldap = $this->config->get_ldap_link();
591     $ldap->cd($source['dn']);
592     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
594     /* Walk through configurations and append them to our list of ConfiguredPackages */
595     while($attr = $ldap->fetch()){
597       /* Skip objects, that are tagged as removed */
598       if(isset($object['FAIstate'][0])){
599         if(preg_match("/removed$/",$attr['FAIstate'][0])){
600           continue;
601         }
602       }
604       $tmp =array(); 
605       $tmp['Name']  = $attr['FAIvariable'][0];
606       $tmp['Type']  = $attr['FAIvariableType'][0];
608       if (isset($attr['FAIvariableContent'][0])){
609         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
610           $tmp['Value'] = $attr['FAIvariableContent'][0];
611         }else{
612           $content = array();        
613           unset($attr['FAIvariableContent']['count']);
614           foreach($attr['FAIvariableContent'] as $attr){
615             $tmp['Value'][] = $attr;
616           }
617         }
618         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
619       }
620     }
621   }
624   function getServerInfos()
625   {
626     $ret = array();
627     $ldap = $this->config->get_ldap_link();
628     $ldap->cd($this->config->current['BASE']);
629     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
630     while($attrs = $ldap->fetch()){
631       if(isset($attrs['FAIrepository'])){
632         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
633           $obj = $attrs['FAIrepository'][$i];
634           $tmp = split("\|",$obj);
635           if(count($tmp)==4){
636             foreach(split(",",$tmp[3]) as $sec){
637               if(!empty($sec)){
638                 $ret[$tmp[2]][] =  $sec;
639               }
640             }
641           }
642         }
643       }
644     }
645     return($ret);
646   }
649   /*! \brief  Used for copy & paste.
650     Returns a HTML input mask, which allows to change the cn of this entry.
651     @param  Array   Array containing current status && a HTML template.
652    */
653   function getCopyDialog()
654   {
655     $vars = array("cn");
656     $smarty = get_smarty();
657     $smarty->assign("cn", htmlentities($this->cn));
658     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
659     $ret = array();
660     $ret['string'] = $str;
661     $ret['status'] = "";
662     return($ret);
663   }
666   /*! \brief  Used for copy & paste.
667     Some entries must be renamed to avaoid duplicate entries.
668    */
669   function saveCopyDialog()
670   {
671     if(isset($_POST['cn'])){
672       $this->cn = get_post('cn');
673     }
674   }
677 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
678 ?>