Code

Avoid initial reset of ocMapping. It is array() anyway.
[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((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
286       foreach($_POST['usedPackages'] as $pkg){
287         if (isset($this->usedPackages[$pkg])){
288           unset($this->usedPackages[$pkg]);
289           if (preg_match('/^-/', $pkg)){
290             $pkg= preg_replace('/^-/', '', $pkg);
291           } else {
292             $pkg= preg_replace('/^/', '-', $pkg);
293           }
294           $this->usedPackages[$pkg]= $pkg;
295         }
296       }
297     }
299     /* Save Configuration */
300     if(isset($_POST['SaveObjectConfig'])){
301       if($this->FAIstate != "freeze"){
302         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
303       }
304       $this->dialog = false;
305       $this->is_dialog=false;
306     }
308     /* cancel configuration */     
309     if(isset($_POST['CancelObjectConfig'])){
310       $this->dialog = false;
311       $this->is_dialog=false;
312     }
314     /* Display dialog */ 
315     if($this->is_dialog){
316       return $this->dialog->execute();
317     }
319     /* Assign section to smarty */
320     $strsec = "";
321     foreach($this->FAIdebianSection as $sec){
322       $strsec .= $sec." ";
323     }
325     foreach($this->attributes as $attr){
326       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
327     }
329     $smarty->assign("OptionsACL","");
331     $smarty->assign("releases",$this->releases);
332     $smarty->assign("release" ,$this->FAIdebianRelease);
333     $smarty->assign("sections",$this->sections);
334     $smarty->assign("section" ,$strsec);
335     $smarty->assign("usedPackages",$this->printUsedPackages());
336     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
337     return($display);
338   }
340   /* Delete me, and all my subtrees
341    */
342   function remove_from_parent()
343   {
344     $ldap = $this->config->get_ldap_link();
345     $ldap->cd ($this->dn);
347     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
348     if($_SESSION['faifilter']['branch'] == "main"){
349       $use_dn = $this->dn;
350     }
352     prepare_to_save_FAI_object($use_dn,array(),true);
354     foreach($this->ConfiguredPackages as $pkgname => $attrs){
355       foreach($attrs as $name => $attr){
356         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
357         $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
358         if($_SESSION['faifilter']['branch'] == "main"){
359           $use_dn = $obj['dn'];
360         }
361         prepare_to_save_FAI_object($use_dn,array(),true);
362       }
363     }
364     $this->handle_post_events("remove");
365   }
368   /* Save data to object 
369    */
370   function save_object()
371   {
373     if($this->FAIstate == "freeze") return;  
374     plugin::save_object();
377     foreach($this->attributes as $attrs){
378       if(isset($_POST[$attrs])){
379         $this->$attrs = $_POST[$attrs];
380       }
381     }
382   }
385   /* Check supplied data */
386   function check()
387   {
388     /* Call common method to give check the hook */
389     $message= plugin::check();
391     if(count($this->usedPackages)==0){
392       $message[]=_("Please select a least one Package.");
393     }
395     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
396       $message[]=_("Please choose a valid combination for your repository setup.");
397     }
399     return ($message);
400   }
402   function printUsedPackages(){
403     $a_ret=array(); 
404     if(is_array($this->usedPackages)) {
405       foreach($this->usedPackages as $usedName){
407         $config = 0;
409         foreach($this->ConfiguredPackages as $name => $value){
410           if($name == $usedName){
411             $config ++;
412           }
413         }
415         $c_str ="";
416         if($config){
417           $c_str = " - "._("package is configured");
418         }
420         /* Adapt used name if we're marked for removal */
421         $dsc= "";
422         if (preg_match('/^-/', $usedName)){
423           $dsc= " - "._("Package marked for removal");
424           $usedName2= preg_replace('/^-/', '! ', $usedName);
425         }else{
426           $usedName2= $usedName;
427         }
429         if(isset($this->list[$usedName][1])){
430           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
431         }else{
432           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
433         }
434       }
435     }
436     return($a_ret);
437   }
439   function genPkgs(){
440     /* Generate a list off available packages for this mirror, section and release
441      */
442     /* Only read this file if it wasn't read before */
443     if($this->buffer==NULL){
444       $this->buffer=array();
445       $a_ret = array();
446       foreach($this->FAIdebianSection as $sec){
447         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
449         if(!is_file($strID)){
450           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
451           unset($this->buffer);
452           return(array());
453         }
454         $fp = fopen($strID,"r");
456         /* Parse every line and create an array */
457         while(!feof($fp)){
458           $str= fgets($fp,512);
459           $stra= split("\|",$str);
460           if(count($stra)==4){
461             $a_ret[$stra[0]] = $stra;
462           }
463         }
464         fclose($fp);
465         /* Save our Data, to avoid reading it again */
466       }
467       $this->buffer = $a_ret;
468       ksort($a_ret);
469       return($a_ret);
470     }else{
471       return $this->buffer;
472     }
473   }
476   /* Save to LDAP */
477   function save()
478   {
479     plugin::save();
481     $ldap = $this->config->get_ldap_link();
483     $this->attrs['FAIpackage'] = array();
484     foreach($this->usedPackages as $pkg => $obj){
485       $this->attrs['FAIpackage'][] = $pkg;
486     } 
488     $this->attrs['FAIdebianSection'] = array();
489     foreach($this->FAIdebianSection as $sec){
490       $this->attrs['FAIdebianSection'][] = $sec;
491     }
493     prepare_to_save_FAI_object($this->dn,$this->attrs);
494     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
496     /* Do object tagging */
497     $this->handle_object_tagging();
498     $ldap->cd($this->dn);
500     /* Save Package configurations */
501     foreach($this->ConfiguredPackages as $pkgname => $attrs){
502       foreach($attrs as $name => $attr){
503       
504         $pkgattrs = array();
506         foreach($attr as $n=>$v){
507           if(empty($v)) $v = array();
508         }
510         /* Set attributes */
511         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
513         $pkgattrs['FAIpackage']           = $pkgname;
514         $pkgattrs['FAIvariable']          = $name;
515         $pkgattrs['FAIvariableType']      = $attr['Type'];
516         $pkgattrs['FAIvariableContent']   = $attr['Value'];
517         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
519         /* cehck if object already exists */
520         $ldap->cat($pkgdn,array("objectClass"));
522         /* Workaround for missing "gosaAdministrativeUnitTag" */
523         $attrs = $ldap->fetch();
524         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
525           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
526         }
528         prepare_to_save_FAI_object($pkgdn,$pkgattrs);
530         /* Handle tagging */
531         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
532       }
533     }
534   }
536   /* Return plugin informations for acl handling */ 
537   function plInfo()
538   {
539     return (array( 
540           "plShortName" => _("Package"),
541           "plDescription" => _("FAI Package list"),
542           "plSelfModify"  => FALSE,
543           "plDepends"     => array(),
544           "plPriority"    => 0,
545           "plSection"     => array("administration"),
546           "plCategory"    => array("fai"),
547           "plProvidedAcls" => array(
548             "cn"                => _("Name"),
549             "description"       => _("Description"),
550             "FAIpackage"        => _("Packages"),
551             "FAIdebianSection"  => _("Section"),
552             "FAIinstallMethod"  => _("Install Method"),
553             "FAIdebianRelease"  => _("Release"))
554           ));
555   }
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>