Code

8b1744ebd6928cbfaebfd7a9b1bd8e570900a24a
[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   }
164   function execute()
165   {
166     /* Call parent execute */
167     plugin::execute();
169     if($this->is_account && !$this->view_logged){
170       $this->view_logged = TRUE;
171       new log("view","fai/".get_class($this),$this->dn);
172     }
174     /* Fill templating stuff */
175     $smarty= get_smarty();
176     $display= "";
178     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
180     if(!$this->is_account){
182       /* Assemble release name */
183       $faifilter = session::get('faifilter');
184       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
185       $tmp= preg_replace('/ou=/', '', $tmp);
186       $rev= array_reverse(split(',', $tmp));
187       $this->FAIdebianRelease= "";
188       foreach ($rev as $part){
189         $this->FAIdebianRelease.= "/$part";
190       }
191       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
193       /* Assemble sections */
194       $repos= $this->getServerInfos();
195       if(isset($repos[$this->FAIdebianRelease])){
196         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
197         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
198       }
200       /* Assign Repository settings*/ 
201       $this->is_account     = true;
202     }
204     /* Assign variables */
205     foreach($this->attributes as $attrs){
206       $smarty->assign($attrs,$this->$attrs);
207     }
209     /* Generate package list */
210     $this->list= $this->genPkgs();
212     /* + was pressed to open the package dialog */
213     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
214       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
215       $this->is_dialog =true;
216     }
218     /* Delete selected package */ 
219     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
220       if($this->FAIstate != "freeze"){
221         foreach($_POST['usedPackages'] as $del){
222           if(isset($this->usedPackages[$del])){
223             unset($this->usedPackages[$del]);
224           }
225         }
226       }
227     }
229     /* Abort package selection dialog */ 
230     if(isset($_POST['CancelSubObject'])){
231       $this->dialog = false;
232       $this->is_dialog=false;
233     }
235     /* attach new packages */
236     if(isset($_POST['SaveSubObject'])) {
237       if($this->FAIstate != "freeze"){
238         $this->dialog->save_object();
239         if(count($this->dialog->check())){
240           foreach($this->dialog->check() as $msgs){
241             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
242           }
243         }else{
244           $use = $this->dialog->save();
245           $this->usedPackages = $use;
246           $this->dialog = false;
247           $this->is_dialog=false;
248           ksort($this->usedPackages);
249         }
250       }else{
251         $this->dialog = false;
252         $this->is_dialog=false;
253       }
254     }
256     /* Configuration dialog open*/
257     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
258       $pkg_config = array();
259       $pkg = $_POST['usedPackages'][0];
260       if(isset($this->ConfiguredPackages[$pkg])){
261         $pkg_config = $this->ConfiguredPackages[$pkg];
262       }
263       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
264       $this->is_dialog =true;
265     }
267     /* Configuration dialog open*/
268     if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
269       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
270         foreach($_POST['usedPackages'] as $pkg){
271           if (isset($this->usedPackages[$pkg])){
272             unset($this->usedPackages[$pkg]);
273             if (preg_match('/^-/', $pkg)){
274               $pkg= preg_replace('/^-/', '', $pkg);
275             } else {
276               $pkg= preg_replace('/^/', '-', $pkg);
277             }
278             $this->usedPackages[$pkg]= $pkg;
279           }
280         }
281       }
282     }
284     /* Save Configuration */
285     if(isset($_POST['SaveObjectConfig'])){
286       if($this->FAIstate != "freeze"){
287         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
288       }
289       $this->dialog = false;
290       $this->is_dialog=false;
291     }
293     /* cancel configuration */     
294     if(isset($_POST['CancelObjectConfig'])){
295       $this->dialog = false;
296       $this->is_dialog=false;
297     }
299     /* Display dialog */ 
300     if($this->is_dialog){
301       return $this->dialog->execute();
302     }
304     /* Assign section to smarty */
305     $strsec = "";
306     foreach($this->FAIdebianSection as $sec){
307       $strsec .= $sec." ";
308     }
310     $tmp = $this->plInfo();
311     foreach($tmp['plProvidedAcls'] as $name => $translated){
312       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
313     }
315     $smarty->assign("releases",$this->releases);
316     $smarty->assign("release" ,$this->FAIdebianRelease);
317     $smarty->assign("sections",$this->sections);
318     $smarty->assign("section" ,$strsec);
319     $smarty->assign("usedPackages",$this->printUsedPackages());
320     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
321     return($display);
322   }
324   /* Delete me, and all my subtrees
325    */
326   function remove_from_parent()
327   {
328     $ldap = $this->config->get_ldap_link();
329     $ldap->cd ($this->dn);
331     $faifilter = session::get('faifilter');
332     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
333     if($faifilter['branch'] == "main"){
334       $use_dn = $this->dn;
335     }
337     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
339     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
341     foreach($this->ConfiguredPackages as $pkgname => $attrs){
342       foreach($attrs as $name => $attr){
343         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
344         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
345         if($faifilter['branch'] == "main"){
346           $use_dn = $obj['dn'];
347         }
348         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
349       }
350     }
351     $this->handle_post_events("remove");
352   }
355   /* Save data to object 
356    */
357   function save_object()
358   {
359     if($this->FAIstate == "freeze") return;  
360     plugin::save_object();
361   }
364   /* Check supplied data */
365   function check()
366   {
367     /* Call common method to give check the hook */
368     $message= plugin::check();
370     if(count($this->usedPackages)==0){
371       $message[]= _("Please select a least one package!");
372     }
374     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
375       $message[]=_("Please choose a valid release/section combination for your repository setup!");
376     }
378     return ($message);
379   }
381   function printUsedPackages(){
382     $a_ret=array(); 
383     if(is_array($this->usedPackages)) {
384       foreach($this->usedPackages as $usedName){
386         $config = 0;
388         foreach($this->ConfiguredPackages as $name => $value){
389           if($name == $usedName){
390             $config ++;
391           }
392         }
394         $c_str ="";
395         if($config){
396           $c_str = " - "._("package is configured");
397         }
399         /* Adapt used name if we're marked for removal */
400         $dsc= "";
401         if (preg_match('/^-/', $usedName)){
402           $dsc= " - "._("Package marked for removal");
403           // Generally a bad idea here, because the toggel triggers on -, not on !
404           //$usedName= preg_replace('/^-/', '! ', $usedName);
405         }else{
406           $usedName2= $usedName;
407         }
409         if(isset($this->list[$usedName][1])){
410           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
411         }else{
412           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
413         }
414       }
415     }
416     return($a_ret);
417   }
420   function genPkgs()
421   {
422     $start = microtime(1);
423     if($this->buffer === NULL || !count($this->buffer)){
424       $this->buffer = array();
425       $q = new gosaSupportDaemon();
426       $attrs = array("distribution", "package","version", "section", "description", "timestamp","template=''");
427       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs);
428       if($q->is_error()){
429         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
430       }else{
431         foreach($ret as $attr){
432           $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
433         }
434       }
435     }
436     
437 #    printf("%0.6f",(microtime(1) - $start));
438     return $this->buffer;
439   }
442   /* Save to LDAP */
443   function save()
444   {
446     /* Assemble release name */
447     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
448       $faifilter = session::get('faifilter');
449       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
450       $tmp= preg_replace('/ou=/', '', $tmp);
451       $rev= array_reverse(split(',', $tmp));
452       $this->FAIdebianRelease= "";
453       foreach ($rev as $part){
454         $this->FAIdebianRelease.= "/$part";
455       }
456       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
457     }
459     plugin::save();
461     $ldap = $this->config->get_ldap_link();
463     $this->attrs['FAIpackage'] = array();
464     foreach($this->usedPackages as $pkg => $obj){
465       $this->attrs['FAIpackage'][] = $pkg;
466     } 
468     $this->attrs['FAIdebianSection'] = array();
469     foreach($this->FAIdebianSection as $sec){
470       $this->attrs['FAIdebianSection'][] = $sec;
471     }
473     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
474     $ldap->cd($this->dn);
476     if($this->initially_was_account){
477       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
478       $ldap->modify($this->attrs);
479     }else{
480       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
481       $ldap->add($this->attrs);
482     }
484     if (!$ldap->success()){
485       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
486     }
487     
489     /* Save Package configurations */
490     foreach($this->ConfiguredPackages as $pkgname => $attrs){
491       foreach($attrs as $name => $attr){
492       
493         $pkgattrs = array();
495         foreach($attr as $n=>$v){
496           if(empty($v)) $v = array();
497         }
499         /* Set attributes */
500         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
502         $pkgattrs['FAIpackage']           = $pkgname;
503         $pkgattrs['FAIvariable']          = $name;
504         $pkgattrs['FAIvariableType']      = $attr['Type'];
505         $pkgattrs['FAIvariableContent']   = $attr['Value'];
506         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
508         /* Tag object */
509         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
511         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
512           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
513         }
514       }
515     }
516   }
518   /* Return plugin informations for acl handling */ 
519   static function plInfo()
520   {
521     return (array( 
522           "plShortName" => _("Package"),
523           "plDescription" => _("FAI Package list"),
524           "plSelfModify"  => FALSE,
525           "plDepends"     => array(),
526           "plPriority"    => 28,
527           "plSection"     => array("administration"),
528           "plCategory"    => array("fai"),
529           "plProvidedAcls" => array(
530             "cn"                => _("Name"),
531             "description"       => _("Description"),
532             "FAIpackage"        => _("Packages"),
533             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
534             "FAIinstallMethod"  => _("Install Method"),
535             "FAIdebconfInfo"    => _("Package configuration"),
536             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
537           ));
538   }
543   function PrepareForCopyPaste($source)
544   {
545     plugin::PrepareForCopyPaste($source);
547     if(isset($source['FAIstate'][0])){
548       $this->FAIstate = $source['FAIstate'][0];
549     }
551     $this->FAIdebianRelease = "ClearFromCopyPaste";
553     if(isset($source['FAIpackage'])){
554       unset($source['FAIpackage']['count']);
555       foreach($source['FAIpackage'] as $pkg){
556         $this->usedPackages[$pkg] = $pkg;
557       }
558       ksort($this->usedPackages);
559     }else{
560       $this->usedPackages = array();
561     }
563     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
564       $this->FAIdebianSection = array();
565       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
566         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
567       }
568     }
570     /* Create one filter with all package names, 
571        instead of calling $ldap->search for every single package 
572      */
573     $PackageFilter = "";
574     foreach($this->usedPackages as $name){
575       $PackageFilter .= "(FAIpackage=".$name.")";
576     }
577     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
579     /* Search for configuration objects */ 
580     $ldap = $this->config->get_ldap_link();
581     $ldap->cd($source['dn']);
582     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
584     /* Walk through configurations and append them to our list of ConfiguredPackages */
585     while($attr = $ldap->fetch()){
587       /* Skip objects, that are tagged as removed */
588       if(isset($object['FAIstate'][0])){
589         if(preg_match("/removed$/",$attr['FAIstate'][0])){
590           continue;
591         }
592       }
594       $tmp =array(); 
595       $tmp['Name']  = $attr['FAIvariable'][0];
596       $tmp['Type']  = $attr['FAIvariableType'][0];
598       if (isset($attr['FAIvariableContent'][0])){
599         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
600           $tmp['Value'] = $attr['FAIvariableContent'][0];
601         }else{
602           $content = array();        
603           unset($attr['FAIvariableContent']['count']);
604           foreach($attr['FAIvariableContent'] as $attr){
605             $tmp['Value'][] = $attr;
606           }
607         }
608         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
609       }
610     }
611   }
614   function getServerInfos()
615   {
616     $ret = array();
617     $ldap = $this->config->get_ldap_link();
618     $ldap->cd($this->config->current['BASE']);
619     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
620     while($attrs = $ldap->fetch()){
621       if(isset($attrs['FAIrepository'])){
622         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
623           $obj = $attrs['FAIrepository'][$i];
624           $tmp = split("\|",$obj);
625           if(count($tmp)==4){
626             foreach(split(",",$tmp[3]) as $sec){
627               if(!empty($sec)){
628                 $ret[$tmp[2]][] =  $sec;
629               }
630             }
631           }
632         }
633       }
634     }
635     return($ret);
636   }
641 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
642 ?>