Code

Moved to -cow
[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     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     
475     if($this->initially_was_account){
476       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
477     }else{
478       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
479     }
481     $ldap->cd($this->dn);
483     /* Save Package configurations */
484     foreach($this->ConfiguredPackages as $pkgname => $attrs){
485       foreach($attrs as $name => $attr){
486       
487         $pkgattrs = array();
489         foreach($attr as $n=>$v){
490           if(empty($v)) $v = array();
491         }
493         /* Set attributes */
494         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
496         $pkgattrs['FAIpackage']           = $pkgname;
497         $pkgattrs['FAIvariable']          = $name;
498         $pkgattrs['FAIvariableType']      = $attr['Type'];
499         $pkgattrs['FAIvariableContent']   = $attr['Value'];
500         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
502         /* Tag object */
503         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
505         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
506           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
507         }
508       }
509     }
510   }
512   /* Return plugin informations for acl handling */ 
513   static function plInfo()
514   {
515     return (array( 
516           "plShortName" => _("Package"),
517           "plDescription" => _("FAI Package list"),
518           "plSelfModify"  => FALSE,
519           "plDepends"     => array(),
520           "plPriority"    => 28,
521           "plSection"     => array("administration"),
522           "plCategory"    => array("fai"),
523           "plProvidedAcls" => array(
524             "cn"                => _("Name"),
525             "description"       => _("Description"),
526             "FAIpackage"        => _("Packages"),
527             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
528             "FAIinstallMethod"  => _("Install Method"),
529             "FAIdebconfInfo"    => _("Package configuration"),
530             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
531           ));
532   }
537   function PrepareForCopyPaste($source)
538   {
539     plugin::PrepareForCopyPaste($source);
541     if(isset($source['FAIstate'][0])){
542       $this->FAIstate = $source['FAIstate'][0];
543     }
545     $this->FAIdebianRelease = "ClearFromCopyPaste";
547     if(isset($source['FAIpackage'])){
548       unset($source['FAIpackage']['count']);
549       foreach($source['FAIpackage'] as $pkg){
550         $this->usedPackages[$pkg] = $pkg;
551       }
552       ksort($this->usedPackages);
553     }else{
554       $this->usedPackages = array();
555     }
557     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
558       $this->FAIdebianSection = array();
559       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
560         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
561       }
562     }
564     /* Create one filter with all package names, 
565        instead of calling $ldap->search for every single package 
566      */
567     $PackageFilter = "";
568     foreach($this->usedPackages as $name){
569       $PackageFilter .= "(FAIpackage=".$name.")";
570     }
571     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
573     /* Search for configuration objects */ 
574     $ldap = $this->config->get_ldap_link();
575     $ldap->cd($source['dn']);
576     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
578     /* Walk through configurations and append them to our list of ConfiguredPackages */
579     while($attr = $ldap->fetch()){
581       /* Skip objects, that are tagged as removed */
582       if(isset($object['FAIstate'][0])){
583         if(preg_match("/removed$/",$attr['FAIstate'][0])){
584           continue;
585         }
586       }
588       $tmp =array(); 
589       $tmp['Name']  = $attr['FAIvariable'][0];
590       $tmp['Type']  = $attr['FAIvariableType'][0];
592       if (isset($attr['FAIvariableContent'][0])){
593         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
594           $tmp['Value'] = $attr['FAIvariableContent'][0];
595         }else{
596           $content = array();        
597           unset($attr['FAIvariableContent']['count']);
598           foreach($attr['FAIvariableContent'] as $attr){
599             $tmp['Value'][] = $attr;
600           }
601         }
602         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
603       }
604     }
605   }
608   function getServerInfos()
609   {
610     $ret = array();
611     $ldap = $this->config->get_ldap_link();
612     $ldap->cd($this->config->current['BASE']);
613     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
614     while($attrs = $ldap->fetch()){
615       if(isset($attrs['FAIrepository'])){
616         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
617           $obj = $attrs['FAIrepository'][$i];
618           $tmp = split("\|",$obj);
619           if(count($tmp)==4){
620             foreach(split(",",$tmp[3]) as $sec){
621               if(!empty($sec)){
622                 $ret[$tmp[2]][] =  $sec;
623               }
624             }
625           }
626         }
627       }
628     }
629     return($ret);
630   }
635 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
636 ?>