Code

Added acls to mimetype
[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" );
65   function faiPackage ($config, $dn= NULL)
66   {
67     /* Load Attributes */
68     plugin::plugin ($config, $dn);
70     $this->acl ="#all#";
72     /* If "dn==new" we try to create a new entry
73      * Else we must read all objects from ldap which belong to this entry.
74      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
75      */
76     if($dn != "new"){
77       $this->dn =$dn;
79       /* Set acls
80        */
81       $ui   = get_userinfo();
82       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
83       $acli = get_module_permission($acl, "FAIclass", $this->dn);
84       $this->acl=$acli;
86       /* Get FAIstate
87        */
88       if(isset($this->attrs['FAIstate'][0])){
89         $this->FAIstate = $this->attrs['FAIstate'][0];
90       }
91     }
93     if(isset($this->attrs['FAIpackage'])){
94       unset($this->attrs['FAIpackage']['count']);
95       foreach($this->attrs['FAIpackage'] as $pkg){
96         $this->usedPackages[$pkg] = $pkg;
97       }
98       ksort($this->usedPackages);
99     }else{
100       $this->usedPackages = array();
101     }  
104     /* Create one filter with all package names, 
105        instead of calling $ldap->search for every single package 
106      */
107     $PackageFilter = "";
108     foreach($this->usedPackages as $name){
109       $PackageFilter .= "(FAIpackage=".$name.")";
110     }
111     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
112     
113     /* Search for configuration objects */ 
114     $ldap = $this->config->get_ldap_link();
115     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
117     /* Walk through configurations and append them to our list of ConfiguredPackages */
118     while($attr = $ldap->fetch()){
120       /* Skip objects, that are tagged as removed */
121       if(isset($object['FAIstate'][0])){
122         if(preg_match("/removed$/",$attr['FAIstate'][0])){
123           continue;
124         }
125       }
127       $tmp =array(); 
128       $tmp['Name']  = $attr['FAIvariable'][0];
129       $tmp['Type']  = $attr['FAIvariableType'][0];
131       if (isset($attr['FAIvariableContent'][0])){
132         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
133           $tmp['Value'] = $attr['FAIvariableContent'][0];
134         }else{
135           $content = array();        
136           unset($attr['FAIvariableContent']['count']);
137           foreach($attr['FAIvariableContent'] as $attr){
138             $tmp['Value'][] = $attr;
139           }
140         }
141         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
142       }
143     }
145     if (isset($this->attrs['FAIdebianSection']['count'])){
146       unset($this->attrs['FAIdebianSection']['count']);
147     }
148     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
149       $this->FAIdebianSection = array();
150       foreach($this->attrs['FAIdebianSection'] as $sec){
151         $this->FAIdebianSection[$sec]=$sec;
152       }
153     }
155     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
156       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
157     }
158     $this->confDir = CONFIG_DIR."/fai/";
159     $this->FAIpackage = array();
162     $methods = array();
163     foreach($this->FAIinstallMethods as $method){
164       $methods[$method] = $method;
165     }
166     $this->FAIinstallMethods = $methods;
167     /* Check if we exist already - no need to ask for revisions, etc. then */
168     if ($this->dn != "new"){
169       $this->newDialogShown= true;
170     }
172   }
174   function execute()
175   {
176     /* Call parent execute */
178     plugin::execute();
180     /* Fill templating stuff */
181     $smarty= get_smarty();
182     $display= "";
184     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
186     if((!$this->is_account)&&(!$this->newDialogShown)){
188       if($this->dialog==NULL){
189         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
190         $this->is_dialog =true;
191       }
193       /* Assign posible changes, for mirror combinations */
194       $this->dialog->save_object();
196       /* Assign Repository settings*/ 
197       if(isset($_POST['SaveObjectNew'])){
198         $obj = $this->dialog->save();
200         $this->FAIdebianSection = $obj['FAIdebianSection'];
201         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
203         unset($this->dialog);
204         $this->dialog         = false;
205         $this->is_dialog      = false;
206         $this->newDialogShown = true;
207         $this->is_account     = true;
208       }
210       /* Draw dialog */
211       if($this->dialog){
212         $display=$this->dialog->execute();
213         return($display); 
214       }
215     }
217     /* Assign variables */
218     foreach($this->attributes as $attrs){
219       $smarty->assign($attrs,$this->$attrs);
220     }
222     /* Generate package list */
223     $this->list=$this->genPkgs();
225     /* + was pressed to open the package dialog */
226     if(isset($_POST['Addpkg'])){
227       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
228       $this->is_dialog =true;
229     }
231     /* Delte selected package */ 
232     if(isset($_POST['Delpkg'])){
233       if($this->FAIstate != "freeze"){
234         foreach($_POST['usedPackages'] as $del){
235           if(isset($this->usedPackages[$del])){
236             unset($this->usedPackages[$del]);
237           }
238         }
239       }
240     }
242     /* Abort package selection dialog */ 
243     if(isset($_POST['CancelSubObject'])){
244       $this->dialog = false;
245       $this->is_dialog=false;
246     }
248     /* attach new packages */
249     if(isset($_POST['SaveSubObject'])) {
250       if($this->FAIstate != "freeze"){
251         $this->dialog->save_object();
252         if(count($this->dialog->check())){
253           foreach($this->dialog->check() as $msgs){
254             print_red($msgs);
255           }
256         }else{
257           $use = $this->dialog->save();
258           $this->usedPackages = $use;
259           $this->dialog = false;
260           $this->is_dialog=false;
261           ksort($this->usedPackages);
262         }
263       }else{
264         $this->dialog = false;
265         $this->is_dialog=false;
266       }
267     }
269     /* Configuration dialog open*/
270     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
271       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
272       $pkg_config = array();
273       $pkg = $_POST['usedPackages'][0];
275       if(isset($this->ConfiguredPackages[$pkg])){
276         $pkg_config = $this->ConfiguredPackages[$pkg];
277       }
279       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
280       $this->dialog ->acl = $this->acl;
281       $this->is_dialog =true;
282     }
284     /* Configuration dialog open*/
285     if($this->FAIstate != "freeze"){
286       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
287         foreach($_POST['usedPackages'] as $pkg){
288           if (isset($this->usedPackages[$pkg])){
289             unset($this->usedPackages[$pkg]);
290             if (preg_match('/^-/', $pkg)){
291               $pkg= preg_replace('/^-/', '', $pkg);
292             } else {
293               $pkg= preg_replace('/^/', '-', $pkg);
294             }
295             $this->usedPackages[$pkg]= $pkg;
296           }
297         }
298       }
299     }
301     /* Save Configuration */
302     if(isset($_POST['SaveObjectConfig'])){
303       if($this->FAIstate != "freeze"){
304         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
305       }
306       $this->dialog = false;
307       $this->is_dialog=false;
308     }
310     /* cancel configuration */     
311     if(isset($_POST['CancelObjectConfig'])){
312       $this->dialog = false;
313       $this->is_dialog=false;
314     }
316     /* Display dialog */ 
317     if($this->is_dialog){
318       return $this->dialog->execute();
319     }
321     /* Assign section to smarty */
322     $strsec = "";
323     foreach($this->FAIdebianSection as $sec){
324       $strsec .= $sec." ";
325     }
327     foreach($this->attributes as $attr){
328       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
329     }
331     $smarty->assign("OptionsACL","");
333     $smarty->assign("releases",$this->releases);
334     $smarty->assign("release" ,$this->FAIdebianRelease);
335     $smarty->assign("sections",$this->sections);
336     $smarty->assign("section" ,$strsec);
337     $smarty->assign("usedPackages",$this->printUsedPackages());
338     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
339     return($display);
340   }
342   /* Delete me, and all my subtrees
343    */
344   function remove_from_parent()
345   {
346     $ldap = $this->config->get_ldap_link();
347     $ldap->cd ($this->dn);
349     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
350     if($_SESSION['faifilter']['branch'] == "main"){
351       $use_dn = $this->dn;
352     }
354     prepare_to_save_FAI_object($use_dn,array(),true);
356     foreach($this->ConfiguredPackages as $pkgname => $attrs){
357       foreach($attrs as $name => $attr){
358         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
359         $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
360         if($_SESSION['faifilter']['branch'] == "main"){
361           $use_dn = $obj['dn'];
362         }
363         prepare_to_save_FAI_object($use_dn,array(),true);
364       }
365     }
366     $this->handle_post_events("remove");
367   }
370   /* Save data to object 
371    */
372   function save_object()
373   {
375     if($this->FAIstate == "freeze") return;  
376     plugin::save_object();
379     foreach($this->attributes as $attrs){
380       if(isset($_POST[$attrs])){
381         $this->$attrs = $_POST[$attrs];
382       }
383     }
384   }
387   /* Check supplied data */
388   function check()
389   {
390     /* Call common method to give check the hook */
391     $message= plugin::check();
393     if(count($this->usedPackages)==0){
394       $message[]=_("Please select a least one Package.");
395     }
397     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
398       $message[]=_("Please choose a valid combination for your repository setup.");
399     }
401     return ($message);
402   }
404   function printUsedPackages(){
405     $a_ret=array(); 
406     if(is_array($this->usedPackages)) {
407       foreach($this->usedPackages as $usedName){
409         $config = 0;
411         foreach($this->ConfiguredPackages as $name => $value){
412           if($name == $usedName){
413             $config ++;
414           }
415         }
417         $c_str ="";
418         if($config){
419           $c_str = " - "._("package is configured");
420         }
422         /* Adapt used name if we're marked for removal */
423         $dsc= "";
424         if (preg_match('/^-/', $usedName)){
425           $dsc= " - "._("Package marked for removal");
426           $usedName2= preg_replace('/^-/', '! ', $usedName);
427         }else{
428           $usedName2= $usedName;
429         }
431         if(isset($this->list[$usedName][1])){
432           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
433         }else{
434           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
435         }
436       }
437     }
438     return($a_ret);
439   }
441   function genPkgs(){
442     /* Generate a list off available packages for this mirror, section and release
443      */
444     /* Only read this file if it wasn't read before */
445     if($this->buffer==NULL){
446       $this->buffer=array();
447       $a_ret = array();
448       foreach($this->FAIdebianSection as $sec){
449         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
451         if(!is_file($strID)){
452           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
453           unset($this->buffer);
454           return(array());
455         }
456         $fp = fopen($strID,"r");
458         /* Parse every line and create an array */
459         while(!feof($fp)){
460           $str= fgets($fp,512);
461           $stra= split("\|",$str);
462           if(count($stra)==4){
463             $a_ret[$stra[0]] = $stra;
464           }
465         }
466         fclose($fp);
467         /* Save our Data, to avoid reading it again */
468       }
469       $this->buffer = $a_ret;
470       ksort($a_ret);
471       return($a_ret);
472     }else{
473       return $this->buffer;
474     }
475   }
478   /* Save to LDAP */
479   function save()
480   {
481     plugin::save();
483     $ldap = $this->config->get_ldap_link();
485     $this->attrs['FAIpackage'] = array();
486     foreach($this->usedPackages as $pkg => $obj){
487       $this->attrs['FAIpackage'][] = $pkg;
488     } 
490     $this->attrs['FAIdebianSection'] = array();
491     foreach($this->FAIdebianSection as $sec){
492       $this->attrs['FAIdebianSection'][] = $sec;
493     }
495     prepare_to_save_FAI_object($this->dn,$this->attrs);
496     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
498     /* Do object tagging */
499     $this->handle_object_tagging();
500     $ldap->cd($this->dn);
502     /* Save Package configurations */
503     foreach($this->ConfiguredPackages as $pkgname => $attrs){
504       foreach($attrs as $name => $attr){
505       
506         $pkgattrs = array();
508         foreach($attr as $n=>$v){
509           if(empty($v)) $v = array();
510         }
512         /* Set attributes */
513         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
515         $pkgattrs['FAIpackage']           = $pkgname;
516         $pkgattrs['FAIvariable']          = $name;
517         $pkgattrs['FAIvariableType']      = $attr['Type'];
518         $pkgattrs['FAIvariableContent']   = $attr['Value'];
519         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
521         /* cehck if object already exists */
522         $ldap->cat($pkgdn,array("objectClass"));
524         /* Workaround for missing "gosaAdministrativeUnitTag" */
525         $attrs = $ldap->fetch();
526         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
527           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
528         }
530         prepare_to_save_FAI_object($pkgdn,$pkgattrs);
532         /* Handle tagging */
533         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
534       }
535     }
536   }
538   /* Return plugin informations for acl handling */ 
539   function plInfo()
540   {
541     return (array( 
542           "plShortName" => _("Package"),
543           "plDescription" => _("FAI Package list"),
544           "plSelfModify"  => FALSE,
545           "plDepends"     => array(),
546           "plPriority"    => 0,
547           "plSection"     => array("administration"),
548           "plCategory"    => array("fai"),
549           "plProvidedAcls" => array(
550             "cn"                => _("Name"),
551             "description"       => _("Description"),
552             "FAIpackage"        => _("Packages"),
553             "FAIdebianSection"  => _("Section"),
554             "FAIinstallMethod"  => _("Install Method"),
555             "FAIdebianRelease"  => _("Release"))
556           ));
557   }
560 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
561 ?>