Code

Fixed saving of special character members
[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     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
351     if($_SESSION['faifilter']['branch'] == "main"){
352       $use_dn = $this->dn;
353     }
355     prepare_to_save_FAI_object($use_dn,array(),true);
357     foreach($this->ConfiguredPackages as $pkgname => $attrs){
358       foreach($attrs as $name => $attr){
359         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
360 #        $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
361         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
362         if($_SESSION['faifilter']['branch'] == "main"){
363           $use_dn = $obj['dn'];
364         }
365         prepare_to_save_FAI_object($use_dn,array(),true);
366       }
367     }
368     $this->handle_post_events("remove");
369   }
372   /* Save data to object 
373    */
374   function save_object()
375   {
377     if($this->FAIstate == "freeze") return;  
378     plugin::save_object();
381     foreach($this->attributes as $attrs){
382       if(isset($_POST[$attrs])){
383         $this->$attrs = $_POST[$attrs];
384       }
385     }
386   }
389   /* Check supplied data */
390   function check()
391   {
392     /* Call common method to give check the hook */
393     $message= plugin::check();
395     if(count($this->usedPackages)==0){
396       $message[]=_("Please select a least one Package.");
397     }
399     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
400       $message[]=_("Please choose a valid combination for your repository setup.");
401     }
403     return ($message);
404   }
406   function printUsedPackages(){
407     $a_ret=array(); 
408     if(is_array($this->usedPackages)) {
409       foreach($this->usedPackages as $usedName){
411         $config = 0;
413         foreach($this->ConfiguredPackages as $name => $value){
414           if($name == $usedName){
415             $config ++;
416           }
417         }
419         $c_str ="";
420         if($config){
421           $c_str = " - "._("package is configured");
422         }
424         /* Adapt used name if we're marked for removal */
425         $dsc= "";
426         if (preg_match('/^-/', $usedName)){
427           $dsc= " - "._("Package marked for removal");
428           $usedName2= preg_replace('/^-/', '! ', $usedName);
429         }else{
430           $usedName2= $usedName;
431         }
433         if(isset($this->list[$usedName][1])){
434           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
435         }else{
436           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
437         }
438       }
439     }
440     return($a_ret);
441   }
443   function genPkgs(){
444     /* Generate a list off available packages for this mirror, section and release
445      */
446     /* Only read this file if it wasn't read before */
447     if($this->buffer==NULL){
448       $this->buffer=array();
449       $a_ret = array();
450       foreach($this->FAIdebianSection as $sec){
451         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
453         if(!is_file($strID)){
454           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
455           unset($this->buffer);
456           return(array());
457         }
458         $fp = fopen($strID,"r");
460         /* Parse every line and create an array */
461         while(!feof($fp)){
462           $str= fgets($fp,512);
463           $stra= split("\|",$str);
464           if(count($stra)==4){
465             $a_ret[$stra[0]] = $stra;
466           }
467         }
468         fclose($fp);
469         /* Save our Data, to avoid reading it again */
470       }
471       $this->buffer = $a_ret;
472       ksort($a_ret);
473       return($a_ret);
474     }else{
475       return $this->buffer;
476     }
477   }
480   /* Save to LDAP */
481   function save()
482   {
483     plugin::save();
485     $ldap = $this->config->get_ldap_link();
487     $this->attrs['FAIpackage'] = array();
488     foreach($this->usedPackages as $pkg => $obj){
489       $this->attrs['FAIpackage'][] = $pkg;
490     } 
492     $this->attrs['FAIdebianSection'] = array();
493     foreach($this->FAIdebianSection as $sec){
494       $this->attrs['FAIdebianSection'][] = $sec;
495     }
497     prepare_to_save_FAI_object($this->dn,$this->attrs);
498     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
500     /* Do object tagging */
501     $this->handle_object_tagging();
502     $ldap->cd($this->dn);
504     /* Save Package configurations */
505     foreach($this->ConfiguredPackages as $pkgname => $attrs){
506       foreach($attrs as $name => $attr){
507       
508         $pkgattrs = array();
510         foreach($attr as $n=>$v){
511           if(empty($v)) $v = array();
512         }
514         /* Set attributes */
515         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
517         $pkgattrs['FAIpackage']           = $pkgname;
518         $pkgattrs['FAIvariable']          = $name;
519         $pkgattrs['FAIvariableType']      = $attr['Type'];
520         $pkgattrs['FAIvariableContent']   = $attr['Value'];
521         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
523         /* cehck if object already exists */
524         $ldap->cat($pkgdn,array("objectClass"));
526         /* Workaround for missing "gosaAdministrativeUnitTag" */
527         $attrs = $ldap->fetch();
528         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
529           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
530         }
532         prepare_to_save_FAI_object($pkgdn,$pkgattrs);
534         /* Handle tagging */
535         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
536       }
537     }
538   }
540   /* Return plugin informations for acl handling */ 
541   function plInfo()
542   {
543     return (array( 
544           "plShortName" => _("Package"),
545           "plDescription" => _("FAI Package list"),
546           "plSelfModify"  => FALSE,
547           "plDepends"     => array(),
548           "plPriority"    => 0,
549           "plSection"     => array("administration"),
550           "plCategory"    => array("fai"),
551           "plProvidedAcls" => array(
552             "cn"                => _("Name"),
553             "description"       => _("Description"),
554             "FAIpackage"        => _("Packages"),
555             "FAIdebianSection"  => _("Section"),
556             "FAIinstallMethod"  => _("Install Method"),
557             "FAIdebianRelease"  => _("Release"))
558           ));
559   }
562 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
563 ?>