Code

Simplified the way we handle tags. This *may* break tagging. Needs testing.
[gosa.git] / plugins / 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 $dialog           = NULL;     // a dialog, e.g. new disk dialog
32   var $SubObjects       = array();  // All leafobjects of this object
34   var $FAIdebianRelease          = ""; // The selected release
35   var $FAIdebianSection          = ""; // selected section
36   var $FAIinstallMethod          = "aptitude"; // hard coded
37   var $mirror                    = ""; // selected mirror
39   var $servers          = array();  // All available servers
40   var $releases         = array();  // All possible releases 
41   var $sections         = array();  // All section types
43   var $list             = NULL;
45   var $mirrors          = array();  // The combination of server/release/section
46   var $confDir          = "";
47   var $usedPackages     = array();
48   var $buffer           = NULL; 
49   var $strID            ="";
50   var $newDialogShown   =false;
52   var $FAIstate         = "";
54   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
55       "dselect-upgrade", "taskinst", "taskrm",
56       "hold", "clean", "aptitude", "aptitude-r",
57       "pending", "dpkgc" );
59   var $base             = "";
60   var $release          = "";
61   var $copy_paste_mode  = false;
62   var $cut_paste_mode   = false;
64   var $CopyPasteVars  = array("ConfiguredPackages","FAIdebianRelease","FAIdebianSection","FAIinstallMethod","mirror","servers","releases","sections","list","mirrors","usedPackages");
67   function faiPackage ($config, $dn= NULL)
68   {
69     /* Load Attributes */
70     plugin::plugin ($config, $dn);
72     $this->acl ="#all#";
74     /* If "dn==new" we try to create a new entry
75      * Else we must read all objects from ldap which belong to this entry.
76      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
77      */
78     if($dn != "new"){
79       $this->dn =$dn;
81       /* Set acls
82        */
83       $ui   = get_userinfo();
84       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
85       $acli = get_module_permission($acl, "FAIclass", $this->dn);
86       $this->acl=$acli;
88       /* Get FAIstate
89        */
90       if(isset($this->attrs['FAIstate'][0])){
91         $this->FAIstate = $this->attrs['FAIstate'][0];
92       }
93     }
95     if(isset($this->attrs['FAIpackage'])){
96       unset($this->attrs['FAIpackage']['count']);
97       foreach($this->attrs['FAIpackage'] as $pkg){
98         $this->usedPackages[$pkg] = $pkg;
99       }
100       ksort($this->usedPackages);
101     }else{
102       $this->usedPackages = array();
103     }  
106     if($dn != "new"){
108       /* Create one filter with all package names, 
109          instead of calling $ldap->search for every single package 
110        */
111       $PackageFilter = "";
112       foreach($this->usedPackages as $name){
113         $PackageFilter .= "(FAIpackage=".$name.")";
114       }
115       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
117       /* Search for configuration objects */ 
118       $ldap = $this->config->get_ldap_link();
119       $ldap->cd($this->dn);
120       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
122       /* Walk through configurations and append them to our list of ConfiguredPackages */
123       while($attr = $ldap->fetch()){
124         $tmp =array(); 
125         $tmp['Name']  = $attr['FAIvariable'][0];
126         $tmp['Type']  = $attr['FAIvariableType'][0];
128         if (isset($attr['FAIvariableContent'][0])){
129           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
130             $tmp['Value'] = $attr['FAIvariableContent'][0];
131           }else{
132             $content = array();        
133             unset($attr['FAIvariableContent']['count']);
134             foreach($attr['FAIvariableContent'] as $attr){
135               $tmp['Value'][] = $attr;
136             }
137           }
138           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
139         }
140       }
141     }
143     if (isset($this->attrs['FAIdebianSection']['count'])){
144       unset($this->attrs['FAIdebianSection']['count']);
145     }
146     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
147       $this->FAIdebianSection = array();
148       foreach($this->attrs['FAIdebianSection'] as $sec){
149         $this->FAIdebianSection[$sec]=$sec;
150       }
151     }
153     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
154       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
155     }
156     $this->confDir = CONFIG_DIR."/fai/";
157     $this->FAIpackage = array();
160     $methods = array();
161     foreach($this->FAIinstallMethods as $method){
162       $methods[$method] = $method;
163     }
164     $this->FAIinstallMethods = $methods;
165     /* Check if we exist already - no need to ask for revisions, etc. then */
166     if ($this->dn != "new"){
167       $this->newDialogShown= true;
168     }
170   }
172   function execute()
173   {
174     /* Call parent execute */
176     plugin::execute();
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       $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['faifilter']['branch']);
188       $tmp= preg_replace('/ou=/', '', $tmp);
189       $rev= array_reverse(split(',', $tmp));
190       $this->FAIdebianRelease= "";
191       foreach ($rev as $part){
192         $this->FAIdebianRelease.= "/$part";
193       }
194       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
196       /* Assemble sections */
197       $repos= $this->getServerInfos();
199       if(isset($repos[$this->FAIdebianRelease])){
200         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
201         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
202       }else{
203         $this->FAIdebianSection = array();
204         print_red(sprintf(_("No repository server found that provide this release '%s'."), $this->FAIdebianRelease));
205       }
207       /* Assign Repository settings*/ 
208       $this->is_account     = true;
209     }
211     /* Assign variables */
212     foreach($this->attributes as $attrs){
213       $smarty->assign($attrs,$this->$attrs);
214     }
216     /* Generate package list */
217     $this->list= $this->genPkgs();
219     /* + was pressed to open the package dialog */
220     if(isset($_POST['Addpkg'])){
221       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
222       $this->is_dialog =true;
223     }
225     /* Delte selected package */ 
226     if(isset($_POST['Delpkg'])){
227       if($this->FAIstate != "freeze"){
228         foreach($_POST['usedPackages'] as $del){
229           if(isset($this->usedPackages[$del])){
230             unset($this->usedPackages[$del]);
231           }
232         }
233       }
234     }
236     /* Abort package selection dialog */ 
237     if(isset($_POST['CancelSubObject'])){
238       $this->dialog = false;
239       $this->is_dialog=false;
240     }
242     /* attach new packages */
243     if(isset($_POST['SaveSubObject'])) {
244       if($this->FAIstate != "freeze"){
245         $this->dialog->save_object();
246         if(count($this->dialog->check())){
247           foreach($this->dialog->check() as $msgs){
248             print_red($msgs);
249           }
250         }else{
251           $use = $this->dialog->save();
252           $this->usedPackages = $use;
253           $this->dialog = false;
254           $this->is_dialog=false;
255           ksort($this->usedPackages);
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]))){
266       $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
267       $pkg_config = array();
268       $pkg = $_POST['usedPackages'][0];
270       if(isset($this->ConfiguredPackages[$pkg])){
271         $pkg_config = $this->ConfiguredPackages[$pkg];
272       }
274       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
275       $this->dialog ->acl = $this->acl;
276       $this->is_dialog =true;
277     }
279     /* Configuration dialog open*/
280     if($this->FAIstate != "freeze"){
281       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
282         foreach($_POST['usedPackages'] as $pkg){
283           if (isset($this->usedPackages[$pkg])){
284             unset($this->usedPackages[$pkg]);
285             if (preg_match('/^-/', $pkg)){
286               $pkg= preg_replace('/^-/', '', $pkg);
287             } else {
288               $pkg= preg_replace('/^/', '-', $pkg);
289             }
290             $this->usedPackages[$pkg]= $pkg;
291           }
292         }
293       }
294     }    
296     /* Save Configuration */
297     if(isset($_POST['SaveObjectConfig'])){
298       if($this->FAIstate != "freeze"){
299         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
300       }
301       $this->dialog = false;
302       $this->is_dialog=false;
303     }
305     /* cancel configuration */     
306     if(isset($_POST['CancelObjectConfig'])){
307       $this->dialog = false;
308       $this->is_dialog=false;
309     }
311     /* Display dialog */ 
312     if($this->is_dialog){
313       return $this->dialog->execute();
314     }
316     /* Assign section to smarty */
317     $strsec = "";
318     foreach($this->FAIdebianSection as $sec){
319       $strsec .= $sec." ";
320     }
322     foreach($this->attributes as $attr){
323       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
324     }
326     $smarty->assign("OptionsACL","");
328     $smarty->assign("releases",$this->releases);
329     $smarty->assign("release" ,$this->FAIdebianRelease);
330     $smarty->assign("sections",$this->sections);
331     $smarty->assign("section" ,$strsec);
332     $smarty->assign("usedPackages",$this->printUsedPackages());
333     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
334     return($display);
335   }
337   /* Delete me, and all my subtrees
338    */
339   function remove_from_parent()
340   {
341     $ldap = $this->config->get_ldap_link();
342     $ldap->cd ($this->dn);
343     $ldap->rmdir_recursive($this->dn);
344     show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
345     $this->handle_post_events("remove");    
346   }
349   /* Save data to object 
350    */
351   function save_object()
352   {
354     if($this->FAIstate == "freeze") return;  
355     plugin::save_object();
358     foreach($this->attributes as $attrs){
359       if(isset($_POST[$attrs])){
360         $this->$attrs = $_POST[$attrs];
361       }
362     }
363   }
366   /* Check supplied data */
367   function check()
368   {
369     /* Call common method to give check the hook */
370     $message= plugin::check();
372     if(count($this->usedPackages)==0){
373       $message[]=_("Please select a least one Package.");
374     }
376     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
377       $message[]=_("Please choose a valid combination for your repository setup.");
378     }
380     /* If this is a new script, check if a script with this name already exists */
381     if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
383       /* Check if current name is already used for fai scripts in selected release */
384       $dn = 'cn='.$this->cn.",ou=packages,".$this->release;
385       $ldap = $this->config->get_ldap_link();
386       $ldap->cat($dn);
387       if($ldap->count()){
389         $r =convert_department_dn($this->release);;
390         $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);
391       }
392     }
393     return ($message);
394   }
397   function printUsedPackages(){
398     $a_ret=array(); 
399     if(is_array($this->usedPackages)) {
400       foreach($this->usedPackages as $usedName){
402         $config = 0;
404         foreach($this->ConfiguredPackages as $name => $value){
405           if($name == $usedName){
406             $config ++;
407           }
408         }
410         $c_str ="";
411         if($config){
412           $c_str = " - "._("package is configured");
413         }
415         /* Adapt used name if we're marked for removal */
416         $dsc= "";
417         if (preg_match('/^-/', $usedName)){
418           $dsc= " - "._("Package marked for removal");
419           // Generally a bad idea here, because the toggel triggers on -, not on !
420           //$usedName= preg_replace('/^-/', '! ', $usedName);
421         }
423         if(isset($this->list[$usedName][1])){
424           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str.$dsc;
425         }else{
426           $a_ret[$usedName] = $usedName.$c_str.$dsc;
427         }
428       }
429     }
430     return($a_ret);
431   }
433   function genPkgs(){
434     /* Generate a list off available packages for this mirror, section and release
435      */
436     /* Only read this file if it wasn't read before */
437     if($this->buffer==NULL){
438       $this->buffer=array();
439       $a_ret = array();
440       foreach($this->FAIdebianSection as $sec){
441         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
443         if(!is_file($strID)){
444           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
445           unset($this->buffer);
446           return(array());
447         }
448         $fp = fopen($strID,"r");
450         /* Parse every line and create an array */
451         while(!feof($fp)){
452           $str= fgets($fp,512);
453           $stra= split("\|",$str);
454           if(count($stra)==4){
455             $a_ret[$stra[0]] = $stra;
456           }
457         }
458         fclose($fp);
459         /* Save our Data, to avoid reading it again */
460       }
461       $this->buffer = $a_ret;
462       ksort($a_ret);
463       return($a_ret);
464     }else{
465       return $this->buffer;
466     }
467   }
470   /* Save to LDAP */
471   function save()
472   {
473     plugin::save();
475     $ldap = $this->config->get_ldap_link();
477     /* Copy & Paste : Ensure that FAIstate is copied too */
478     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
479       $this->attrs['FAIstate'] = $this->FAIstate;
480     }
482     $this->attrs['FAIpackage'] = array();
483     foreach($this->usedPackages as $pkg => $obj){
484       $this->attrs['FAIpackage'][] = $pkg;
485     } 
487     $this->attrs['FAIdebianSection'] = array();
488     foreach($this->FAIdebianSection as $sec){
489       $this->attrs['FAIdebianSection'][] = $sec;
490     }
492     //    $this->attrs["FAIinstallMethod"]= "aptitude";
494     $ldap->cat($this->dn, array('dn'));
495     if($ldap->count()!=0){
496       /* Write FAIscript to ldap*/
497       $ldap->cd($this->dn);
498       $this->cleanup();
499       $ldap->modify ($this->attrs); 
501     }else{
502       /* Write FAIscript to ldap*/
503       $ldap->cd($this->config->current['BASE']);
504       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
505       $ldap->cd($this->dn);
506       $ldap->add($this->attrs);
507     }
508     show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
510     $ldap->cd($this->dn);
512     /* Save Package configurations */
513     foreach($this->ConfiguredPackages as $pkgname => $attrs){
514       foreach($attrs as $name => $attr){
515       
516         $pkgattrs = array();
518         foreach($attr as $n=>$v){
519           if(empty($v)) $v = array();
520         }
522         /* Set attributes */
523         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
525         $pkgattrs['FAIpackage']           = $pkgname;
526         $pkgattrs['FAIvariable']          = $name;
527         $pkgattrs['FAIvariableType']      = $attr['Type'];
528         $pkgattrs['FAIvariableContent']   = $attr['Value'];
529         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
531         /* cehck if object already exists */
532         $ldap->cat($pkgdn,array("objectClass"));
534         /* Tag object */
535         $this->tag_attrs(&$pkgattrs, $sub_dn, $this->gosaUnitTag);
536   
537         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
538           if($ldap->count()!=0){
539             $ldap->cd($pkgdn);
540             $this->cleanup();
541             $ldap->modify ($pkgattrs); 
543           }else{
544             $ldap->cd($this->config->current['BASE']);
545             $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
546             $ldap->cd($pkgdn);
547             $ldap->add($pkgattrs);
548           }
549           show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
550         }
552       }
553     }
554   }
555   
556   /* return copy & paste dialog
557    */
558   function getCopyDialog()
559   {
560     /* Ask for cn */
561     $smarty = get_smarty();
563     /* Assemble release name */
564     $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['faifilter']['branch']);
565     $tmp= preg_replace('/ou=/', '', $tmp);
566     $rev= array_reverse(split(',', $tmp));
567     $this->FAIdebianRelease= "";
568     foreach ($rev as $part){
569       $this->FAIdebianRelease.= "/$part";
570     }
571     $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
573     $smarty->assign("cn" ,$this->cn);
574     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
575     $ret = array();
576     $ret['string'] = $str;
577     $ret['status'] = "";
578     return($ret);
579   }
581   /* Get posted cn */
582   function saveCopyDialog()
583   {
584     if(isset($_POST['cn'])){
585       $this->cn = $_POST['cn'];
586     }
587   }
589   function getServerInfos()
590   {
591     $ret = array();
592     $ldap = $this->config->get_ldap_link();
593     $ldap->cd($this->config->current['BASE']);
594     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
595     while($attrs = $ldap->fetch()){
596       if(isset($attrs['FAIrepository'])){
597         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
598           $obj = $attrs['FAIrepository'][$i];
599           $tmp = split("\|",$obj);
600           if(count($tmp)==4){
601             foreach(split(",",$tmp[3]) as $sec){
602               if(!empty($sec)){
603                 $ret[$tmp[2]][] =  $sec;
604               }
605             }
606           }
607         }
608       }
609     }
610     return($ret);
611   }
616 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
617 ?>