Code

Added several comments
[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     /* If "dn==new" we try to create a new entry
71      * Else we must read all objects from ldap which belong to this entry.
72      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
73      */
74     if($dn != "new"){
75       $this->dn =$dn;
77       /* Get FAIstate
78        */
79       if(isset($this->attrs['FAIstate'][0])){
80         $this->FAIstate = $this->attrs['FAIstate'][0];
81       }
82     }
84     if(isset($this->attrs['FAIpackage'])){
85       unset($this->attrs['FAIpackage']['count']);
86       foreach($this->attrs['FAIpackage'] as $pkg){
87         $this->usedPackages[$pkg] = $pkg;
88       }
89       ksort($this->usedPackages);
90     }else{
91       $this->usedPackages = array();
92     }  
94     if($dn != "new"){
96       /* Create one filter with all package names, 
97          instead of calling $ldap->search for every single package 
98        */
99       $PackageFilter = "";
100       foreach($this->usedPackages as $name){
101         $PackageFilter .= "(FAIpackage=".$name.")";
102       }
103       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
105       /* Search for configuration objects */ 
106       $ldap = $this->config->get_ldap_link();
107       $ldap->cd($this->dn);
108       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
110       /* Walk through configurations and append them to our list of ConfiguredPackages */
111       while($attr = $ldap->fetch()){
113         /* Skip objects, that are tagged as removed */
114         if(isset($object['FAIstate'][0])){
115           if(preg_match("/removed$/",$attr['FAIstate'][0])){
116             continue;
117           }
118         }
120         $tmp =array(); 
121         $tmp['Name']  = $attr['FAIvariable'][0];
122         $tmp['Type']  = $attr['FAIvariableType'][0];
124         if (isset($attr['FAIvariableContent'][0])){
125           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
126             $tmp['Value'] = $attr['FAIvariableContent'][0];
127           }else{
128             $content = array();        
129             unset($attr['FAIvariableContent']['count']);
130             foreach($attr['FAIvariableContent'] as $attr){
131               $tmp['Value'][] = $attr;
132             }
133           }
134           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
135         }
136       }
137     }
139     if (isset($this->attrs['FAIdebianSection']['count'])){
140       unset($this->attrs['FAIdebianSection']['count']);
141     }
142     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
143       $this->FAIdebianSection = array();
144       foreach($this->attrs['FAIdebianSection'] as $sec){
145         $this->FAIdebianSection[$sec]=$sec;
146       }
147     }
149     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
150       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
151     }
152     $this->confDir = CONFIG_DIR."/fai/";
153     $this->FAIpackage = array();
156     $methods = array();
157     foreach($this->FAIinstallMethods as $method){
158       $methods[$method] = $method;
159     }
160     $this->FAIinstallMethods = $methods;
161     /* Check if we exist already - no need to ask for revisions, etc. then */
162     if ($this->dn != "new"){
163       $this->newDialogShown= true;
164     }
166   }
168   function execute()
169   {
170     /* Call parent execute */
172     plugin::execute();
174     /* Fill templating stuff */
175     $smarty= get_smarty();
176     $display= "";
178     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
180     if((!$this->is_account)&&(!$this->newDialogShown)){
182       if($this->dialog==NULL){
183         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
184         $this->is_dialog =true;
185       }
187       /* Assign posible changes, for mirror combinations */
188       $this->dialog->save_object();
190       /* Assign Repository settings*/ 
191       if(isset($_POST['SaveObjectNew'])){
192         $obj = $this->dialog->save();
194         $this->FAIdebianSection = $obj['FAIdebianSection'];
195         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
197         unset($this->dialog);
198         $this->dialog         = false;
199         $this->is_dialog      = false;
200         $this->newDialogShown = true;
201         $this->is_account     = true;
202       }
204       /* Draw dialog */
205       if($this->dialog){
206         $display=$this->dialog->execute();
207         return($display); 
208       }
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']) && $this->acl_is_writeable("FAIpackage")){
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']) && $this->acl_is_writeable("FAIpackage")){
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'])) && $this->acl_is_writeable("FAIdebconfInfo")){
265       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
266       $pkg_config = array();
267       $pkg = $_POST['usedPackages'][0];
269       if(isset($this->ConfiguredPackages[$pkg])){
270         $pkg_config = $this->ConfiguredPackages[$pkg];
271       }
273       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
274       $this->is_dialog =true;
275     }
277     /* Configuration dialog open*/
278     if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
279       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
280         foreach($_POST['usedPackages'] as $pkg){
281           if (isset($this->usedPackages[$pkg])){
282             unset($this->usedPackages[$pkg]);
283             if (preg_match('/^-/', $pkg)){
284               $pkg= preg_replace('/^-/', '', $pkg);
285             } else {
286               $pkg= preg_replace('/^/', '-', $pkg);
287             }
288             $this->usedPackages[$pkg]= $pkg;
289           }
290         }
291       }
292     }
294     /* Save Configuration */
295     if(isset($_POST['SaveObjectConfig'])){
296       if($this->FAIstate != "freeze"){
297         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
298       }
299       $this->dialog = false;
300       $this->is_dialog=false;
301     }
303     /* cancel configuration */     
304     if(isset($_POST['CancelObjectConfig'])){
305       $this->dialog = false;
306       $this->is_dialog=false;
307     }
309     /* Display dialog */ 
310     if($this->is_dialog){
311       return $this->dialog->execute();
312     }
314     /* Assign section to smarty */
315     $strsec = "";
316     foreach($this->FAIdebianSection as $sec){
317       $strsec .= $sec." ";
318     }
320     $tmp = $this->plInfo();
321     foreach($tmp['plProvidedAcls'] as $name => $translated){
322       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
323     }
325     $smarty->assign("releases",$this->releases);
326     $smarty->assign("release" ,$this->FAIdebianRelease);
327     $smarty->assign("sections",$this->sections);
328     $smarty->assign("section" ,$strsec);
329     $smarty->assign("usedPackages",$this->printUsedPackages());
330     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
331     return($display);
332   }
334   /* Delete me, and all my subtrees
335    */
336   function remove_from_parent()
337   {
338     $ldap = $this->config->get_ldap_link();
339     $ldap->cd ($this->dn);
341 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
342     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
343     if($_SESSION['faifilter']['branch'] == "main"){
344       $use_dn = $this->dn;
345     }
347     prepare_to_save_FAI_object($use_dn,array(),true);
349     foreach($this->ConfiguredPackages as $pkgname => $attrs){
350       foreach($attrs as $name => $attr){
351         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
352 #        $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
353         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
354         if($_SESSION['faifilter']['branch'] == "main"){
355           $use_dn = $obj['dn'];
356         }
357         prepare_to_save_FAI_object($use_dn,array(),true);
358       }
359     }
360     $this->handle_post_events("remove");
361   }
364   /* Save data to object 
365    */
366   function save_object()
367   {
368     if($this->FAIstate == "freeze") return;  
369     plugin::save_object();
370   }
373   /* Check supplied data */
374   function check()
375   {
376     /* Call common method to give check the hook */
377     $message= plugin::check();
379     if(count($this->usedPackages)==0){
380       $message[]=_("Please select a least one Package.");
381     }
383     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
384       $message[]=_("Please choose a valid combination for your repository setup.");
385     }
387     return ($message);
388   }
390   function printUsedPackages(){
391     $a_ret=array(); 
392     if(is_array($this->usedPackages)) {
393       foreach($this->usedPackages as $usedName){
395         $config = 0;
397         foreach($this->ConfiguredPackages as $name => $value){
398           if($name == $usedName){
399             $config ++;
400           }
401         }
403         $c_str ="";
404         if($config){
405           $c_str = " - "._("package is configured");
406         }
408         /* Adapt used name if we're marked for removal */
409         $dsc= "";
410         if (preg_match('/^-/', $usedName)){
411           $dsc= " - "._("Package marked for removal");
412           $usedName2= preg_replace('/^-/', '! ', $usedName);
413         }else{
414           $usedName2= $usedName;
415         }
417         if(isset($this->list[$usedName][1])){
418           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
419         }else{
420           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
421         }
422       }
423     }
424     return($a_ret);
425   }
427   function genPkgs(){
428     /* Generate a list off available packages for this mirror, section and release
429      */
430     /* Only read this file if it wasn't read before */
431     if($this->buffer==NULL){
432       $this->buffer=array();
433       $a_ret = array();
434       foreach($this->FAIdebianSection as $sec){
435         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
437         if(!is_file($strID)){
438           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
439           unset($this->buffer);
440           return(array());
441         }
442         $fp = fopen($strID,"r");
444         /* Parse every line and create an array */
445         while(!feof($fp)){
446           $str= fgets($fp,512);
447           $stra= split("\|",$str);
448           if(count($stra)==4){
449             $a_ret[$stra[0]] = $stra;
450           }
451         }
452         fclose($fp);
453         /* Save our Data, to avoid reading it again */
454       }
455       $this->buffer = $a_ret;
456       ksort($a_ret);
457       return($a_ret);
458     }else{
459       return $this->buffer;
460     }
461   }
464   /* Save to LDAP */
465   function save()
466   {
467     plugin::save();
469     $ldap = $this->config->get_ldap_link();
471     $this->attrs['FAIpackage'] = array();
472     foreach($this->usedPackages as $pkg => $obj){
473       $this->attrs['FAIpackage'][] = $pkg;
474     } 
476     $this->attrs['FAIdebianSection'] = array();
477     foreach($this->FAIdebianSection as $sec){
478       $this->attrs['FAIdebianSection'][] = $sec;
479     }
481     prepare_to_save_FAI_object($this->dn,$this->attrs);
482     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
484     /* Do object tagging */
485     $this->handle_object_tagging();
486     $ldap->cd($this->dn);
488     /* Save Package configurations */
489     foreach($this->ConfiguredPackages as $pkgname => $attrs){
490       foreach($attrs as $name => $attr){
491       
492         $pkgattrs = array();
494         foreach($attr as $n=>$v){
495           if(empty($v)) $v = array();
496         }
498         /* Set attributes */
499         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
501         $pkgattrs['FAIpackage']           = $pkgname;
502         $pkgattrs['FAIvariable']          = $name;
503         $pkgattrs['FAIvariableType']      = $attr['Type'];
504         $pkgattrs['FAIvariableContent']   = $attr['Value'];
505         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
507         /* cehck if object already exists */
508         $ldap->cat($pkgdn,array("objectClass"));
510         /* Workaround for missing "gosaAdministrativeUnitTag" */
511         $attrs = $ldap->fetch();
512         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
513           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
514         }
516         prepare_to_save_FAI_object($pkgdn,$pkgattrs);
518         /* Handle tagging */
519         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
520       }
521     }
522   }
524   /* Return plugin informations for acl handling */ 
525   function plInfo()
526   {
527     return (array( 
528           "plShortName" => _("Package"),
529           "plDescription" => _("FAI Package list"),
530           "plSelfModify"  => FALSE,
531           "plDepends"     => array(),
532           "plPriority"    => 30,
533           "plSection"     => array("administration"),
534           "plCategory"    => array("fai"),
535           "plProvidedAcls" => array(
536             "cn"                => _("Name"),
537             "description"       => _("Description"),
538             "FAIpackage"        => _("Packages"),
539             "FAIdebianSection"  => _("Section"),
540             "FAIinstallMethod"  => _("Install Method"),
541             "FAIdebconfInfo"    => _("Package configuration"),
542             "FAIdebianRelease"  => _("Release"))
543           ));
544   }
547 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
548 ?>