Code

Updated fai copy&paste stuff
[gosa.git] / plugins / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description"); 
28   var $sub64coded       = array();
30   var $ConfiguredPackages = array();
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIdebianRelease          = ""; // The selected release
40   var $FAIdebianSection          = ""; // selected section
41   var $FAIinstallMethod          = "aptitude"; // hard coded
42   var $mirror                    = ""; // selected mirror
44   var $servers          = array();  // All available servers
45   var $releases         = array();  // All possible releases 
46   var $sections         = array();  // All section types
48   var $list             = NULL;
50   var $mirrors          = array();  // The combination of server/release/section
51   var $confDir          = "";
52   var $usedPackages     = array();
53   var $buffer           = NULL; 
54   var $strID            ="";
55   var $newDialogShown   =false;
57   var $FAIstate         = "";
59   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
60       "dselect-upgrade", "taskinst", "taskrm",
61       "hold", "clean", "aptitude", "aptitude-r",
62       "pending", "dpkgc" );
64   var $base             = "";
65   var $release          = "";
66   var $copy_paste_mode  = false;
67   var $cut_paste_mode   = false;
69   var $CopyPasteVars  = array("FAIstate","ConfiguredPackages","FAIdebianRelease","FAIdebianSection","FAIinstallMethod","mirror","servers","releases","sections","list","mirrors","usedPackages");
72   function faiPackage ($config, $dn= NULL)
73   {
74     /* Load Attributes */
75     plugin::plugin ($config, $dn);
77     $this->acl ="#all#";
79     /* If "dn==new" we try to create a new entry
80      * Else we must read all objects from ldap which belong to this entry.
81      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
82      */
83     if($dn != "new"){
84       $this->dn =$dn;
86       /* Set acls
87        */
88       $ui   = get_userinfo();
89       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
90       $acli = get_module_permission($acl, "FAIclass", $this->dn);
91       $this->acl=$acli;
93       /* Get FAIstate
94        */
95       if(isset($this->attrs['FAIstate'][0])){
96         $this->FAIstate = $this->attrs['FAIstate'][0];
97       }
98     }
100     if(isset($this->attrs['FAIpackage'])){
101       unset($this->attrs['FAIpackage']['count']);
102       foreach($this->attrs['FAIpackage'] as $pkg){
103         $this->usedPackages[$pkg] = $pkg;
104       }
105       ksort($this->usedPackages);
106     }else{
107       $this->usedPackages = array();
108     }  
111     if($dn != "new"){
113       /* Create one filter with all package names, 
114          instead of calling $ldap->search for every single package 
115        */
116       $PackageFilter = "";
117       foreach($this->usedPackages as $name){
118         $PackageFilter .= "(FAIpackage=".$name.")";
119       }
120       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
122       /* Search for configuration objects */ 
123       $ldap = $this->config->get_ldap_link();
124       $ldap->cd($this->dn);
125       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
127       /* Walk through configurations and append them to our list of ConfiguredPackages */
128       while($attr = $ldap->fetch()){
129         $tmp =array(); 
130         $tmp['Name']  = $attr['FAIvariable'][0];
131         $tmp['Type']  = $attr['FAIvariableType'][0];
133         if (isset($attr['FAIvariableContent'][0])){
134           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
135             $tmp['Value'] = $attr['FAIvariableContent'][0];
136           }else{
137             $content = array();        
138             unset($attr['FAIvariableContent']['count']);
139             foreach($attr['FAIvariableContent'] as $attr){
140               $tmp['Value'][] = $attr;
141             }
142           }
143           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
144         }
145       }
146     }
148     if (isset($this->attrs['FAIdebianSection']['count'])){
149       unset($this->attrs['FAIdebianSection']['count']);
150     }
151     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
152       $this->FAIdebianSection = array();
153       foreach($this->attrs['FAIdebianSection'] as $sec){
154         $this->FAIdebianSection[$sec]=$sec;
155       }
156     }
158     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
159       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
160     }
161     $this->confDir = CONFIG_DIR."/fai/";
162     $this->FAIpackage = array();
165     $methods = array();
166     foreach($this->FAIinstallMethods as $method){
167       $methods[$method] = $method;
168     }
169     $this->FAIinstallMethods = $methods;
170     /* Check if we exist already - no need to ask for revisions, etc. then */
171     if ($this->dn != "new"){
172       $this->newDialogShown= true;
173     }
175   }
177   function execute()
178   {
179     /* Call parent execute */
181     plugin::execute();
183     /* Fill templating stuff */
184     $smarty= get_smarty();
185     $display= "";
187     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
189     if((!$this->is_account)&&(!$this->newDialogShown)){
191       if($this->dialog==NULL){
192         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
193         $this->is_dialog =true;
194       }
196       /* Assign posible changes, for mirror combinations */
197       $this->dialog->save_object();
199       /* Assign Repository settings*/ 
200       if(isset($_POST['SaveObjectNew'])){
201         $obj = $this->dialog->save();
203         $this->FAIdebianSection = $obj['FAIdebianSection'];
204         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
206         unset($this->dialog);
207         $this->dialog         = false;
208         $this->is_dialog      = false;
209         $this->newDialogShown = true;
210         $this->is_account     = true;
211       }
213       /* Draw dialog */
214       if($this->dialog){
215         $display=$this->dialog->execute();
216         return($display); 
217       }
218     }
220     /* Assign variables */
221     foreach($this->attributes as $attrs){
222       $smarty->assign($attrs,$this->$attrs);
223     }
225     /* Generate package list */
226     $this->list=$this->genPkgs();
228     /* + was pressed to open the package dialog */
229     if(isset($_POST['Addpkg'])){
230       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
231       $this->is_dialog =true;
232     }
234     /* Delte selected package */ 
235     if(isset($_POST['Delpkg'])){
236       if($this->FAIstate != "freeze"){
237         foreach($_POST['usedPackages'] as $del){
238           if(isset($this->usedPackages[$del])){
239             unset($this->usedPackages[$del]);
240           }
241         }
242       }
243     }
245     /* Abort package selection dialog */ 
246     if(isset($_POST['CancelSubObject'])){
247       $this->dialog = false;
248       $this->is_dialog=false;
249     }
251     /* attach new packages */
252     if(isset($_POST['SaveSubObject'])) {
253       if($this->FAIstate != "freeze"){
254         $this->dialog->save_object();
255         if(count($this->dialog->check())){
256           foreach($this->dialog->check() as $msgs){
257             print_red($msgs);
258           }
259         }else{
260           $use = $this->dialog->save();
261           $this->usedPackages = $use;
262           $this->dialog = false;
263           $this->is_dialog=false;
264           ksort($this->usedPackages);
265         }
266       }else{
267         $this->dialog = false;
268         $this->is_dialog=false;
269       }
270     }
272     /* Configuration dialog open*/
273     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
274       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
275       $pkg_config = array();
276       $pkg = $_POST['usedPackages'][0];
278       if(isset($this->ConfiguredPackages[$pkg])){
279         $pkg_config = $this->ConfiguredPackages[$pkg];
280       }
282       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
283       $this->dialog ->acl = $this->acl;
284       $this->is_dialog =true;
285     }
287     /* Configuration dialog open*/
288     if($this->FAIstate != "freeze"){
289       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
290         foreach($_POST['usedPackages'] as $pkg){
291           if (isset($this->usedPackages[$pkg])){
292             unset($this->usedPackages[$pkg]);
293             if (preg_match('/^-/', $pkg)){
294               $pkg= preg_replace('/^-/', '', $pkg);
295             } else {
296               $pkg= preg_replace('/^/', '-', $pkg);
297             }
298             $this->usedPackages[$pkg]= $pkg;
299           }
300         }
301       }
302     }    
304     /* Save Configuration */
305     if(isset($_POST['SaveObjectConfig'])){
306       if($this->FAIstate != "freeze"){
307         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
308       }
309       $this->dialog = false;
310       $this->is_dialog=false;
311     }
313     /* cancel configuration */     
314     if(isset($_POST['CancelObjectConfig'])){
315       $this->dialog = false;
316       $this->is_dialog=false;
317     }
319     /* Display dialog */ 
320     if($this->is_dialog){
321       return $this->dialog->execute();
322     }
324     /* Assign section to smarty */
325     $strsec = "";
326     foreach($this->FAIdebianSection as $sec){
327       $strsec .= $sec." ";
328     }
330     foreach($this->attributes as $attr){
331       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
332     }
334     $smarty->assign("OptionsACL","");
336     $smarty->assign("releases",$this->releases);
337     $smarty->assign("release" ,$this->FAIdebianRelease);
338     $smarty->assign("sections",$this->sections);
339     $smarty->assign("section" ,$strsec);
340     $smarty->assign("usedPackages",$this->printUsedPackages());
341     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
342     return($display);
343   }
345   /* Delete me, and all my subtrees
346    */
347   function remove_from_parent()
348   {
349     $ldap = $this->config->get_ldap_link();
350     $ldap->cd ($this->dn);
351     $ldap->rmdir_recursive($this->dn);
352     show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
353     $this->handle_post_events("remove");    
354   }
357   /* Save data to object 
358    */
359   function save_object()
360   {
362     if($this->FAIstate == "freeze") return;  
363     plugin::save_object();
366     foreach($this->attributes as $attrs){
367       if(isset($_POST[$attrs])){
368         $this->$attrs = $_POST[$attrs];
369       }
370     }
371   }
374   /* Check supplied data */
375   function check()
376   {
377     /* Call common method to give check the hook */
378     $message= plugin::check();
380     if(count($this->usedPackages)==0){
381       $message[]=_("Please select a least one Package.");
382     }
384     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
385       $message[]=_("Please choose a valid combination for your repository setup.");
386     }
388     /* If this is a new script, check if a script with this name already exists */
389     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
391       /* Check if current name is already used for fai scripts in selected release */
392       $dn = 'cn='.$this->cn.",ou=packages,".$this->release;
393       $ldap = $this->config->get_ldap_link();
394       $ldap->cat($dn);
395       if($ldap->count()){
397         $r =convert_department_dn($this->release);;
398         $message[] = sprintf(_("Can't insert a fai package list named '%s' in '%s' there is already a package list with the given name."),$this->cn,$r);
399       }
400     }
401     return ($message);
402   }
405   function printUsedPackages(){
406     $a_ret=array(); 
407     if(is_array($this->usedPackages)) {
408       foreach($this->usedPackages as $usedName){
410         $config = 0;
412         foreach($this->ConfiguredPackages as $name => $value){
413           if($name == $usedName){
414             $config ++;
415           }
416         }
418         $c_str ="";
419         if($config){
420           $c_str = " - "._("package is configured");
421         }
423         /* Adapt used name if we're marked for removal */
424         $dsc= "";
425         if (preg_match('/^-/', $usedName)){
426           $dsc= " - "._("Package marked for removal");
427           $usedName= preg_replace('/^-/', '! ', $usedName);
428         }
430         if(isset($this->list[$usedName][1])){
431           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str.$dsc;
432         }else{
433           $a_ret[$usedName] = $usedName.$c_str.$dsc;
434         }
435       }
436     }
437     return($a_ret);
438   }
440   function genPkgs(){
441     /* Generate a list off available packages for this mirror, section and release
442      */
443     /* Only read this file if it wasn't read before */
444     if($this->buffer==NULL){
445       $this->buffer=array();
446       $a_ret = array();
447       foreach($this->FAIdebianSection as $sec){
448         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
450         if(!is_file($strID)){
451           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
452           unset($this->buffer);
453           return(array());
454         }
455         $fp = fopen($strID,"r");
457         /* Parse every line and create an array */
458         while(!feof($fp)){
459           $str= fgets($fp,512);
460           $stra= split("\|",$str);
461           if(count($stra)==4){
462             $a_ret[$stra[0]] = $stra;
463           }
464         }
465         fclose($fp);
466         /* Save our Data, to avoid reading it again */
467       }
468       $this->buffer = $a_ret;
469       ksort($a_ret);
470       return($a_ret);
471     }else{
472       return $this->buffer;
473     }
474   }
477   /* Save to LDAP */
478   function save()
479   {
480     plugin::save();
482     $ldap = $this->config->get_ldap_link();
484     /* Copy & Paste : Ensure that FAIstate is copied too */
485     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
486       $this->attrs['FAIstate'] = $this->FAIstate;
487     }
489     $this->attrs['FAIpackage'] = array();
490     foreach($this->usedPackages as $pkg => $obj){
491       $this->attrs['FAIpackage'][] = $pkg;
492     } 
494     $this->attrs['FAIdebianSection'] = array();
495     foreach($this->FAIdebianSection as $sec){
496       $this->attrs['FAIdebianSection'][] = $sec;
497     }
499     //    $this->attrs["FAIinstallMethod"]= "aptitude";
501     $ldap->cat($this->dn, array('dn'));
502     if($ldap->count()!=0){
503       /* Write FAIscript to ldap*/
504       $ldap->cd($this->dn);
505       $this->cleanup();
506       $ldap->modify ($this->attrs); 
508     }else{
509       /* Write FAIscript to ldap*/
510       $ldap->cd($this->config->current['BASE']);
511       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
512       $ldap->cd($this->dn);
513       $ldap->add($this->attrs);
514     }
515     show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
517     /* Do object tagging */
518     $this->handle_object_tagging();
519     $ldap->cd($this->dn);
521     /* Save Package configurations */
522     foreach($this->ConfiguredPackages as $pkgname => $attrs){
523       foreach($attrs as $name => $attr){
524       
525         $pkgattrs = array();
527         foreach($attr as $n=>$v){
528           if(empty($v)) $v = array();
529         }
531         /* Set attributes */
532         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
534         $pkgattrs['FAIpackage']           = $pkgname;
535         $pkgattrs['FAIvariable']          = $name;
536         $pkgattrs['FAIvariableType']      = $attr['Type'];
537         $pkgattrs['FAIvariableContent']   = $attr['Value'];
538         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
540         /* cehck if object already exists */
541         $ldap->cat($pkgdn,array("objectClass"));
543         /* Workaround for missing "gosaAdministrativeUnitTag" */
544         $attrs = $ldap->fetch();
545         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
546           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
547         }
548     
549         if($ldap->count()!=0){
550           $ldap->cd($pkgdn);
551           $this->cleanup();
552           $ldap->modify ($pkgattrs); 
554         }else{
555           $ldap->cd($this->config->current['BASE']);
556           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
557           $ldap->cd($pkgdn);
558           $ldap->add($pkgattrs);
559         }
560         show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
562         /* Handle tagging */
563         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
564       }
565     }
566   }
567   
568   /* return copy & paste dialog
569    */
570   function getCopyDialog()
571   {
572     /* Ask for cn */
573     $smarty = get_smarty();
574     $smarty->assign("cn" ,$this->cn);
575     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
576     $ret = array();
577     $ret['string'] = $str;
578     $ret['status'] = "";
579     return($ret);
580   }
582   /* Get posted cn */
583   function saveCopyDialog()
584   {
585     if(isset($_POST['cn'])){
586       $this->cn = $_POST['cn'];
587     }
588   }
591 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
592 ?>