Code

Updated C&P for FAI.
[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(!preg_match("/^freeze/", $this->FAIstate)){
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(!preg_match("/^freeze/", $this->FAIstate)){
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(preg_match('/^freeze/', $this->FAIstate) && $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(!preg_match("/^freeze/", $this->FAIstate)){
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(preg_match("/^freeze/", $this->FAIstate)) 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     /* Ensure that we do not overwrite an allready existing entry 
379      */
380     if($this->is_new){
381       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
382       $faifilter = session::get('faifilter');
383       if($faifilter['branch']!="main"){
384         $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
385       }
387       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
388       if(isset($res[$this->cn])){
389         $message[] = msgPool::duplicated(_("Name"));
390       }
391     }
392     return ($message);
393   }
395   function printUsedPackages(){
396     $a_ret=array(); 
397     if(is_array($this->usedPackages)) {
398       foreach($this->usedPackages as $usedName){
400         $config = 0;
402         foreach($this->ConfiguredPackages as $name => $value){
403           if($name == $usedName){
404             $config ++;
405           }
406         }
408         $c_str ="";
409         if($config){
410           $c_str = " - "._("package is configured");
411         }
413         /* Adapt used name if we're marked for removal */
414         $dsc= "";
415         if (preg_match('/^-/', $usedName)){
416           $dsc= " - "._("Package marked for removal");
417           // Generally a bad idea here, because the toggel triggers on -, not on !
418           //$usedName= preg_replace('/^-/', '! ', $usedName);
419         }else{
420           $usedName2= $usedName;
421         }
423         if(isset($this->list[$usedName][1])){
424           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
425         }else{
426           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
427         }
428       }
429     }
430     return($a_ret);
431   }
434   function genPkgs()
435   {
436     $start = microtime(1);
437     if($this->buffer === NULL || !count($this->buffer)){
438       $this->buffer = array();
439       $q = new gosaSupportDaemon();
440       $attrs = array("distribution", "package","version", "section", "description", "timestamp","template=''");
441       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs);
442       if($q->is_error()){
443         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
444       }else{
445         foreach($ret as $attr){
446           $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
447         }
448       }
449     }
450     
451 #    printf("%0.6f",(microtime(1) - $start));
452     return $this->buffer;
453   }
456   /* Save to LDAP */
457   function save()
458   {
460     /* Assemble release name */
461     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
462       $faifilter = session::get('faifilter');
463       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
464       $tmp= preg_replace('/ou=/', '', $tmp);
465       $rev= array_reverse(split(',', $tmp));
466       $this->FAIdebianRelease= "";
467       foreach ($rev as $part){
468         $this->FAIdebianRelease.= "/$part";
469       }
470       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
471     }
473     plugin::save();
475     $ldap = $this->config->get_ldap_link();
477     $this->attrs['FAIpackage'] = array();
478     foreach($this->usedPackages as $pkg => $obj){
479       $this->attrs['FAIpackage'][] = $pkg;
480     } 
482     $this->attrs['FAIdebianSection'] = array();
483     foreach($this->FAIdebianSection as $sec){
484       $this->attrs['FAIdebianSection'][] = $sec;
485     }
487     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
488     
489     if($this->initially_was_account){
490       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
491     }else{
492       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
493     }
495     $ldap->cd($this->dn);
497     /* Save Package configurations */
498     foreach($this->ConfiguredPackages as $pkgname => $attrs){
499       foreach($attrs as $name => $attr){
500       
501         $pkgattrs = array();
503         foreach($attr as $n=>$v){
504           if(empty($v)) $v = array();
505         }
507         /* Set attributes */
508         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
510         $pkgattrs['FAIpackage']           = $pkgname;
511         $pkgattrs['FAIvariable']          = $name;
512         $pkgattrs['FAIvariableType']      = $attr['Type'];
513         $pkgattrs['FAIvariableContent']   = $attr['Value'];
514         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
516         /* Tag object */
517         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
519         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
520           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
521         }
522       }
523     }
524   }
526   /* Return plugin informations for acl handling */ 
527   static function plInfo()
528   {
529     return (array( 
530           "plShortName" => _("Package"),
531           "plDescription" => _("FAI Package list"),
532           "plSelfModify"  => FALSE,
533           "plDepends"     => array(),
534           "plPriority"    => 28,
535           "plSection"     => array("administration"),
536           "plCategory"    => array("fai"),
537           "plProvidedAcls" => array(
538             "cn"                => _("Name"),
539             "description"       => _("Description"),
540             "FAIpackage"        => _("Packages"),
541             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
542             "FAIinstallMethod"  => _("Install Method"),
543             "FAIdebconfInfo"    => _("Package configuration"),
544             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
545           ));
546   }
551   function PrepareForCopyPaste($source)
552   {
553     plugin::PrepareForCopyPaste($source);
555     if(isset($source['FAIstate'][0])){
556       $this->FAIstate = $source['FAIstate'][0];
557     }
559     $this->FAIdebianRelease = "ClearFromCopyPaste";
561     if(isset($source['FAIpackage'])){
562       unset($source['FAIpackage']['count']);
563       foreach($source['FAIpackage'] as $pkg){
564         $this->usedPackages[$pkg] = $pkg;
565       }
566       ksort($this->usedPackages);
567     }else{
568       $this->usedPackages = array();
569     }
571     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
572       $this->FAIdebianSection = array();
573       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
574         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
575       }
576     }
578     /* Create one filter with all package names, 
579        instead of calling $ldap->search for every single package 
580      */
581     $PackageFilter = "";
582     foreach($this->usedPackages as $name){
583       $PackageFilter .= "(FAIpackage=".$name.")";
584     }
585     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
587     /* Search for configuration objects */ 
588     $ldap = $this->config->get_ldap_link();
589     $ldap->cd($source['dn']);
590     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
592     /* Walk through configurations and append them to our list of ConfiguredPackages */
593     while($attr = $ldap->fetch()){
595       /* Skip objects, that are tagged as removed */
596       if(isset($object['FAIstate'][0])){
597         if(preg_match("/removed$/",$attr['FAIstate'][0])){
598           continue;
599         }
600       }
602       $tmp =array(); 
603       $tmp['Name']  = $attr['FAIvariable'][0];
604       $tmp['Type']  = $attr['FAIvariableType'][0];
606       if (isset($attr['FAIvariableContent'][0])){
607         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
608           $tmp['Value'] = $attr['FAIvariableContent'][0];
609         }else{
610           $content = array();        
611           unset($attr['FAIvariableContent']['count']);
612           foreach($attr['FAIvariableContent'] as $attr){
613             $tmp['Value'][] = $attr;
614           }
615         }
616         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
617       }
618     }
619   }
622   function getServerInfos()
623   {
624     $ret = array();
625     $ldap = $this->config->get_ldap_link();
626     $ldap->cd($this->config->current['BASE']);
627     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
628     while($attrs = $ldap->fetch()){
629       if(isset($attrs['FAIrepository'])){
630         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
631           $obj = $attrs['FAIrepository'][$i];
632           $tmp = split("\|",$obj);
633           if(count($tmp)==4){
634             foreach(split(",",$tmp[3]) as $sec){
635               if(!empty($sec)){
636                 $ret[$tmp[2]][] =  $sec;
637               }
638             }
639           }
640         }
641       }
642     }
643     return($ret);
644   }
647   /*! \brief  Used for copy & paste.
648     Returns a HTML input mask, which allows to change the cn of this entry.
649     @param  Array   Array containing current status && a HTML template.
650    */
651   function getCopyDialog()
652   {
653     $vars = array("cn");
654     $smarty = get_smarty();
655     $smarty->assign("cn", htmlentities($this->cn));
656     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
657     $ret = array();
658     $ret['string'] = $str;
659     $ret['status'] = "";
660     return($ret);
661   }
664   /*! \brief  Used for copy & paste.
665     Some entries must be renamed to avaoid duplicate entries.
666    */
667   function saveCopyDialog()
668   {
669     if(isset($_POST['cn'])){
670       $this->cn = get_post('cn');
671     }
672   }
675 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
676 ?>