Code

Apply fix for #6900
[gosa.git] / trunk / gosa-plugins / fai / 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();
26   var $Removal = array();
28   /* Specific attributes */
29   var $cn               = "";       // The class name for this object
30   var $description      = "";       // The description for this set of partitions
31   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
32   var $SubObjects       = array();  // All leafobjects of this object
34   var $FAIdebianRelease          = ""; // The selected release
35   var $FAIdebianSection          = array(); // selected section
36   var $FAIinstallMethod          = "aptitude"; // hard coded
38   var $sections         = array();  // All section types
40   var $list             = NULL;
42   var $usedPackages     = array();
43   var $buffer           = NULL; 
44   var $newDialogShown   = false;
46   var $FAIstate         = "";
47   var $view_logged      = FALSE;
48   var $base;
49   var $FAIpackage ;
51   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
52       "dselect-upgrade", "taskinst", "taskrm",
53       "hold", "clean", "aptitude", "aptitude-r",
54       "pending", "dpkgc" );
56   var $direct_packages_add = FALSE;
57   var $configure_dialog_shown = FALSE;
58   var $debconf_configurations = FALSE;
60   function faiPackage (&$config, $dn= NULL)
61   {
62     /* Load Attributes */
63     plugin::plugin ($config, $dn);
65     /* If "dn==new" we try to create a new entry
66      * Else we must read all objects from ldap which belong to this entry.
67      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
68      */
69     if($dn != "new"){
70       $this->dn =$dn;
72       /* Check if there are already some packages in this list 
73        */
74       $this->usedPackages = array();
75       if(isset($this->attrs['FAIpackage'])){
76         unset($this->attrs['FAIpackage']['count']);
77         foreach($this->attrs['FAIpackage'] as $pkg){
78           $name = preg_replace("/\-$/","",$pkg);
79           $this->usedPackages[$name] = $pkg;
80         }
81         ksort($this->usedPackages);
82       }  
84       /* Fetch package configurations from LDAP */
85       $this->ConfiguredPackages = $this->get_configured_packages($this->usedPackages, $this->dn);
87       $this->FAIdebianSection = array();
88       if(isset($this->attrs['FAIdebianSection'])){
89         for($i = 0 ; $i < $this->attrs['FAIdebianSection']['count'] ; $i++ ){ 
90           $sec = $this->attrs['FAIdebianSection'][$i];
91           $this->FAIdebianSection[$sec]=$sec;
92         }
93       }
94       $this->FAIpackage = array();
96     } // ENDE  dn != new  
98     $methods = array();
99     foreach($this->FAIinstallMethods as $method){
100       $methods[$method] = $method;
101     }
102     $this->FAIinstallMethods = $methods;
104     /* Check if we exist already - no need to ask for revisions, etc. then */
105     if ($this->dn != "new"){
106       $this->newDialogShown= true;
107     }
108     $this->is_new = FALSE;
109     if($this->dn == "new"){
110       $this->is_new =TRUE;
111     }
112     /* Check weither its allowed to directly add packages */
113     $direct_packages_add = $config->search("faiManagement","direct_packages_add",array("menu"));
114     if (preg_match("/^true$/i", $direct_packages_add) || preg_match("/yes/i", $direct_packages_add)) {
115         $this->direct_packages_add = TRUE;
116     }
118     /* Force Release and Sections to the currently selected Defaults */
119     $this->FAIdebianRelease = $this->getRelease();
120     $this->FAIdebianSection = $this->getSections();
122     /* Generate package list */
123     $this->list= $this->genPkgs(TRUE);
124   }
126   function get_configured_packages($packages, $base)
127   {
128     /* Fetch all package configurations from ldap 
129      */
130     $PackageFilter = "";
131     foreach($packages as $name => $value){
132       $PackageFilter .= "(FAIpackage=".$name.")";
133     }
134     /* Also fetch d-i */
135     $PackageFilter .= "(FAIpackage=d-i)";
137     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
139     /* Search for configuration objects */ 
140     $ldap = $this->config->get_ldap_link();
141     $ldap->cd($base);
142     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
143           "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
145     $configured_packages = array();
147     /* Walk through configurations and append them to our list of ConfiguredPackages 
148      */
149     while($attr = $ldap->fetch()){
151       /* Skip objects, that are tagged as removed */
152       if(isset($object['FAIstate'][0])){
153         if(preg_match("/removed$/",$attr['FAIstate'][0])){
154           continue;
155         }
156       }
158       $tmp =array(); 
159       $tmp['Name']  = $attr['FAIvariable'][0];
160       $tmp['Type']  = $attr['FAIvariableType'][0];
161       $tmp['Save']  = TRUE;
163       if (isset($attr['FAIvariableContent'][0])){
164         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
165           $tmp['Value'] = $attr['FAIvariableContent'][0];
166         }else{
167           $content = array();        
168           unset($attr['FAIvariableContent']['count']);
169           foreach($attr['FAIvariableContent'] as $attr){
170             $tmp['Value'][] = $attr;
171           }
172         }
173         $configured_packages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
174       }
175       else {
176         $tmp['Value'] = "";
177         $configured_packages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
178       }
179     }
180   
181     return $configured_packages;
182   }
185   function execute()
186   {
187     /* Call parent execute */
188     plugin::execute();
190     if($this->is_account && !$this->view_logged){
191       $this->view_logged = TRUE;
192       new log("view","fai/".get_class($this),$this->dn);
193     }
195     /* Fill templating stuff */
196     $smarty= get_smarty();
197     $display= "";
199     /******
200      * Initialize a new Package List with release and section name
201      ******/
202     
203     if(!$this->is_account){
205       $this->FAIdebianRelease = $this->getRelease();
207       $this->FAIdebianSection = $this->getSections();
209       /* Assign Repository settings*/ 
210       $this->is_account     = true;
211     }
212   
214     /******
215      * Add 
216      ******/
218     if(isset($_POST['AddManualpkg']) && 
219         isset($_POST['addPpkgsText']) &&
220         strlen($_POST['addPpkgsText']) && 
221         $this->acl_is_writeable("FAIpackage") && 
222         !preg_match("/freeze/",$this->FAIstate)){
224       // Check all splitted packages for valid package names
225       $add_packages = preg_split( "/[\s,]+/", get_post('addPpkgsText'), -1, PREG_SPLIT_NO_EMPTY );
226       $valid_packages = array();
227       if( is_array($add_packages) ) {
228         $invalid_packages = array();
229         foreach ($add_packages as $value) {
230           if(!preg_match( "/^[a-z0-9][-0-9a-z+\.]+$/",$value)){
231             $invalid_packages[] = trim($value);
232           }else{
233             $valid_packages[] = trim($value);
234           }
235         }
236         if(count($invalid_packages)){
237           $str = implode(", ",$invalid_packages);
238           msg_dialog::display(_("Invalid package names"), 
239               sprintf(_("The following package names don't match the Debian policy: %s"),$str),
240               ERROR_DIALOG);
241         }
244         // If we have a complete list of valid packages, add them
245         if(count($valid_packages)){
247           foreach($valid_packages as $key =>  $value){
248             if(array_key_exists($value,$this->usedPackages)) {
249               unset($valid_packages[$key]);
250             }
251           }
253           // Query SI-Deamon for additional package information
254           $daemon       = new gosaSupportDaemon();
255           $query_attrs  = array("distribution", "package","version", "section", "description", "timestamp");
256           $do_si_query  = true;
259           foreach ($valid_packages as $value) {
260             if( $do_si_query == true ) {
261               $res = $daemon->FAI_get_packages($this->FAIdebianRelease,$query_attrs,array($value),0,1);
262               if( ! $daemon->is_error()){
263                 
264                 if(count($res)){
266                   // We just use the last answer - there shouldn't be multiple
267                   $res_attrs = array_pop( $res );
268                   $this->list[$value] = $res_attrs;
269                   $this->usedPackages[$value] = $res_attrs['PACKAGE'];
271                 }else{
272                   $this->usedPackages[$value] = $value;
273                 }
274               }else{
275                 msg_dialog::display(_("Service infrastructure"),
276                     msgPool::siError($daemon->get_error()),
277                     ERROR_DIALOG);
278                 $do_si_query = false;
279               }
280             }
281           }
283           ksort($this->usedPackages);
285           /* Generate package list */
286           $this->list= $this->genPkgs(TRUE);
287         }
288       }
289     }
291     if(isset($_POST['EditConfigurations']) &&
292       $this->acl_is_writeable("FAIdebconfInfo")) {
293         $this->dialog = new faiDebconfConfigurations($this->config, $this->dn, $this);
294         $this->dialog->set_acl_base($this->acl_base);
295         $this->is_dialog = true;
296     }
298     /* + was pressed to open the package dialog */
299     if(isset($_POST['Addpkg']) && 
300         $this->acl_is_writeable("FAIpackage") && 
301         !preg_match("/freeze/",$this->FAIstate)){
302       $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
303       $this->is_dialog =true;
304     }
307     /* Check image Posts 
308      */
309     foreach($_POST as $name => $value){
311       /*****
312        * Remove configuration
313        *****/
314       if(!preg_match('/freeze/i', $this->FAIstate) && preg_match('/^remove_configuration_/', $name)){
315         $id = base64_decode(preg_replace("/^remove_configuration_(.*)_[xy]*$/","\\1",$name));
316         if(isset($this->ConfiguredPackages[$id])) {
317           $this->Removal[$id] = $this->ConfiguredPackages[$id];
318           unset($this->ConfiguredPackages[$id]);
319         }
320       }
322       /******
323        * Mark as removed  
324        ******/
325       if(!preg_match('/freeze/i', $this->FAIstate) && preg_match("/^removal_package_/",$name)){
326         $id = @base64_decode(preg_replace("/^removal_package_(.*)_[xy]*$/","\\1",$name));
327         if(isset($this->usedPackages[$id])){
328           $pkg = $this->usedPackages[$id]; 
329           if (preg_match('/\-$/', $pkg)){
330             $pkg= preg_replace('/\-$/', '', $pkg);
331           } else {
332             $pkg= preg_replace('/$/', '-', $pkg);
333           }
334           $this->usedPackages[$id] = $pkg;
335         }
336         break;
337       }
339       /******
340        * Delete Pkgs   
341        ******/
342       if(!preg_match('/freeze/i', $this->FAIstate) && preg_match("/^remove_package_/",$name)){
343         $id = @base64_decode(preg_replace("/^remove_package_(.*)_[xy]*$/","\\1",$name));
344         if(isset($this->usedPackages[$id])){
345           unset($this->usedPackages[$id]);
346         }
347         break;
348       }
350       /******
351        * Configure Pkgs   
352        ******/
353       if(!preg_match('/freeze/i', $this->FAIstate) && preg_match("/^configure_package_/",$name)){
354         $pkg = @base64_decode(preg_replace("/^configure_package_(.*)_[xy]*$/","\\1",$name));
355          
356         if(isset($this->usedPackages[$pkg])){
358           /* Configuration dialog open*/
359           $pkg_config = array();
360           if(isset($this->ConfiguredPackages[$pkg])){
361             $pkg_config = $this->ConfiguredPackages[$pkg];
362           }
363           $this->configure_dialog_shown = TRUE;
364           $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
365           $this->is_dialog =true;
366         }
367         break;
368       }
369     }
371     /* Abort package selection dialog */ 
372     if(isset($_POST['CancelSubObject'])){
373       $this->dialog = false;
374       $this->is_dialog=false;
375     }
377     /* attach new packages */
378     if(isset($_POST['SaveSubObject'])) {
379       if(!preg_match("/freeze/i", $this->FAIstate)){
380         $this->dialog->save_object();
381         if(count($this->dialog->check())){
382           foreach($this->dialog->check() as $msgs){
383             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
384           }
385         }else{
386           $use = $this->dialog->save();
387           $this->usedPackages = $use;
388           $this->dialog = false;
389           $this->is_dialog=false;
390           ksort($this->usedPackages);
392           /* Generate package list */
393           $this->list= $this->genPkgs(TRUE);
394         }
395       }else{
396         $this->dialog = false;
397         $this->is_dialog=false;
398       }
399     }
401     /* Save Configuration */
402     if(isset($_POST['SaveObjectConfig']) && $this->configure_dialog_shown){
403       if(!preg_match("/^freeze/", $this->FAIstate)){
404         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
405       }
406       $this->dialog = false;
407       $this->is_dialog=false;
408       $this->configure_dialog_shown=false;
409     }
411     if (isset($_POST['SaveDebconfConfig'])){
412       $this->dialog->save_object();
413       $msgs = $this->dialog->check();
414       if(count($msgs)>0) {
415         foreach($msgs as $msg){
416           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
417         }
418       } else {
419         if(!preg_match("/^freeze/", $this->FAIstate)){
420           $this->ConfiguredPackages= $this->dialog->save();
421           foreach($this->ConfiguredPackages as $package => $attrs) {
422             foreach($attrs as $name => $data) {
423               if (isset($data['Save']) && !($data['Save'])) {
424                 $dn = "FAIvariable=".$name.",".$this->dn;
425                 $ldap = $this->config->get_ldap_link();
426                 $ldap->cat($dn);
427                 if (!$ldap->count()) {
428                   unset($this->ConfiguredPackages[$package][$name]);
429                 }
430               }
431             }
432             if (is_array($this->ConfiguredPackages[$package])) {
433               if (count($this->ConfiguredPackages[$package]) == 0) {
434                 unset($this->ConfiguredPackages[$package]);
435               }
436             }
437           }
438         }
439         $this->dialog = false;
440         $this->is_dialog = false;
441       }
442     }
444     /* cancel configuration */     
445     if(isset($_POST['CancelObjectConfig']) && $this->configure_dialog_shown){
446       $this->dialog = false;
447       $this->is_dialog=false;
448       $this->configure_dialog_shown=false;
449     }
451     /* Display dialog */ 
452     if($this->is_dialog){
453       $this->dialog->save_object();
454       return $this->dialog->execute();
455     }
458     /******
459      * Display UI / HTML / smarty 
460      ******/
462     /* Create divlist to display a list of all currently used packages
463      */
464     $divlist = new divSelectBox("faiPackages");
465     $divlist->setHeight(600);
467     ksort($this->usedPackages);
468     if(is_array($this->usedPackages)){
469       foreach($this->usedPackages as $usedName => $name){
470     
471         $actions = "";
473         /* Append message if package is configured */
474         $configured = "<img src='images/empty.png' alt=' '  class='center'>";
475         if(isset($this->ConfiguredPackages[$usedName])){
476           $configured = "<input type='image' name='remove_configuration_".base64_encode($usedName)."' src='plugins/fai/images/package_configure.png' title='"._("Remove configuration")."'
477             title='"._("Remove configuration")."' class='center'>";
478         }
480         /* Adapt used name if we're marked for removal */
481         $removal = "<img src='images/empty.png' alt=' '  class='center'>";
482         if (preg_match('/\-$/', $name)){
483           $removal = "<img src='plugins/fai/images/removal_mark.png' alt='"._("Package marked for removal")."'
484                       title='"._("Package marked for removal")."' class='center'>";
485         }
487         /* Get Version */
488         $version = "&nbsp;";
489         if(isset($this->list[$usedName]['VERSION'])){
490           $version = $this->list[$usedName]['VERSION'];
491         }
492     
493         /* Get description */
494         $description = "&nbsp;";
495         if(isset($this->list[$usedName]['DESCRIPTION'])){
496           $description = base64_decode($this->list[$usedName]['DESCRIPTION']);
497         }
498  
499         if(!preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
500           $actions = "<input type='image' class='center' title='"._("Mark package for removal")."' 
501             src='plugins/fai/images/removal_mark.png' name='removal_package_".base64_encode($usedName)."' >";
502         }
504         if(isset($this->list[$usedName]['TEMPLATE']) && 
505            !preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIdebconfInfo")){
506           $actions.= "&nbsp;<input type='image' class='center' title='"._("Configure this package")."' 
507             src='plugins/fai/images/package_configure.png' name='configure_package_".base64_encode($usedName)."' >";
508         }
509         if(!preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
510           $actions.= "&nbsp;<input type='image' class='center' title='"._("Remove this package")."' 
511             src='images/lists/trash.png' name='remove_package_".base64_encode($usedName)."' >";
512         }
514         $field1 = array("string" => $configured."&nbsp;".$removal,"attach" => "style='width:40px;'");
515         $field2 = array("string" => $usedName ,"attach" => "style='width:200px;'");
516         $field3 = array("string" => $version);
517         $field4 = array("string" => $description);
518         $field5 = array("string" => $actions ,"attach" => "style='width:60px; border-right:0px;'");
519         $divlist->AddEntry(array($field1,$field2,$field3,$field4,$field5));
520       }
521     }
523     /* Assign variables */
524     foreach($this->attributes as $attrs){
525       $smarty->assign($attrs,$this->$attrs);
526     }
527     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
529     /* Assign section to smarty */
530     $strsec = "";
531     foreach($this->FAIdebianSection as $sec){
532       $strsec .= $sec." ";
533     }
535     $tmp = $this->plInfo();
536     foreach($tmp['plProvidedAcls'] as $name => $translated){
537       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
538     }
540     if ($this->acl_is_writeable("FAIdebconfInfo")) {
541       $this->debconf_configurations = TRUE;
542     }
543  
544     $smarty->assign("freeze", preg_match("/freeze/",$this->FAIstate));
545     $smarty->assign("divlist",$divlist->DrawList());
546     $smarty->assign("release" ,$this->FAIdebianRelease);
547     $smarty->assign("sections",$this->sections);
548     $smarty->assign("section" ,$strsec);
549     $smarty->assign("direct_packages_add", $this->direct_packages_add);
550     $smarty->assign("debconf_configurations", $this->debconf_configurations);
551     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
552     return($display);
553   }
556   /*! \brief  Removes this packageList from the ldap database 
557    */
558   function remove_from_parent()
559   {
560     $ldap = $this->config->get_ldap_link();
561     $ldap->cd ($this->dn);
562     $release = $this->parent->parent->fai_release;
563     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
564     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
565     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
566     foreach($this->ConfiguredPackages as $pkgname => $attrs){
567       foreach($attrs as $name => $attr){
568         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
569         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $pkgdn);
570         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
571       }
572     }
573     $this->handle_post_events("remove");
574   }
577   /*! \brief  Collect all relevant POST vars for this plugin 
578    */
579   function save_object()
580   {
581     if(preg_match("/^freeze/", $this->FAIstate)) return;
582     plugin::save_object();
583   }
586   /*! \brief  Check given inputs for this package list
587       @return Array Containing all error messages, or an empty array if no error occured
588    */
589   function check()
590   {
591     /* Call common method to give check the hook */
592     $message= plugin::check();
594     if(count($this->usedPackages)==0){
595       $message[]= _("Please select a least one package!");
596     }
598     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
599       $message[]=_("Please choose a valid release/section combination for your repository setup!");
600     }
602     /* Ensure that we do not overwrite an allready existing entry 
603      */
604     if($this->is_new){
605       $release = $this->parent->parent->fai_release;
606       $new_dn= 'cn='.$this->cn.",".get_ou('faiPackageRDN').get_ou('faiBaseRDN').$release;
607       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
608       if(isset($res[$this->cn])){
609         $message[] = msgPool::duplicated(_("Name"));
610       }
611     }
613     if (preg_match("/^-/", $this->cn)) {
614       $message[] = sprintf(_("The specified classname '%s' is invalid. Classnames must not start with a dash."), $this->cn);
615     }
617     if($this->cn != preg_replace("/ /","",trim($this->cn))){
618       $message[] = msgPool::invalid(_("Name"),preg_replace("/ /","_",$this->cn),"/[^_]/");
619     }
621     return ($message);
622   }
625   /*! \brief  Reload the list of cached packages.
626       @return Returns the currently cached list of packages. 
627    */
628   function genPkgs($force = false)
629   {
630     if(empty($this->FAIdebianRelease)) return;
632     if(!count($this->buffer) || $force){
633       $q = new gosaSupportDaemon();
634       $attrs = array("distribution", "package","version", "section", "description", "timestamp","template");
636       $packages = array_keys($this->usedPackages);
638       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$packages);
639       if($q->is_error()){
640         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
641       }else{
642         foreach($ret as $attr){
643           $this->buffer[$attr['PACKAGE']] = $attr;
644         }
645       }
646     }
647     return $this->buffer;
648   }
651   /*! \brief Save packages and their configuration to ldap 
652    */
653   function save()
654   {
656     /* Assemble release name */
657     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
659       $current_release  = $this->parent->parent->fai_release;
660       $tmp= preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'.*$/', '', $current_release);
661       $tmp= preg_replace('/ou=/', '', $tmp);
662       $rev= array_reverse(split(',', $tmp));
663       $this->FAIdebianRelease= "";
664       foreach ($rev as $part){
665         $this->FAIdebianRelease.= "/$part";
666       }
667       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
668     }
670     plugin::save();
672     $ldap = $this->config->get_ldap_link();
674     $this->attrs['FAIpackage'] = array();
675     foreach($this->usedPackages as $pkg){
676       $this->attrs['FAIpackage'][] = $pkg;
677     } 
679     $this->attrs['FAIdebianSection'] = array();
680     foreach($this->FAIdebianSection as $sec){
681       $this->attrs['FAIdebianSection'][] = $sec;
682     }
684     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
685     
686     if($this->initially_was_account){
687       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
688     }else{
689       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
690     }
692     $ldap->cd($this->dn);
694     /* Save Package configurations */
695     foreach($this->Removal as $pkgname => $attrs){
696       /* Skip entries which are back in the ConfiguredPackages array */
697       if (isset($this->ConfiguredPackages[$pkgname])) {
698         continue;
699       }
700       foreach($attrs as $name => $attr){
701         $dn = "FAIvariable=".$name.",".$this->dn;
702         $ldap = $this->config->get_ldap_link();
703         $ldap->cat($dn);
704         if ($ldap->count()) {
705           FAI::prepare_to_save_FAI_object($dn,array(),true);
706         }
707       }
708     }
710     foreach($this->ConfiguredPackages as $pkgname => $attrs){
711       foreach($attrs as $name => $attr){
712         $pkgattrs = array();
714         foreach($attr as $n=>$v){
715           if(empty($v)) $v = array();
716         }
718         /* Set attributes */
719         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
721         $pkgattrs['FAIpackage']           = $pkgname;
722         $pkgattrs['FAIvariable']          = $name;
723         $pkgattrs['FAIvariableType']      = $attr['Type'];
724         $pkgattrs['FAIvariableContent']   = $attr['Value'];
725         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
727         if (!$attr['Save']) {
728           $ldap = $this->config->get_ldap_link();
729           $ldap->cat($pkgdn);
730           if ($ldap->count()) {
731             FAI::prepare_to_save_FAI_object($pkgdn,array(),true);
732           }
733           else {
734             unset($this->ConfiguredPackages[$pkgname][$name]);
735           }
736           if (is_array($this->ConfiguredPackages[$pkgname]) && count($this->ConfiguredPackages[$pkgname]) == 0) {
737             unset($this->ConfiguredPackages[$pkgname]);
738           }
740           continue;
742         }
744         if (empty($pkgattrs['FAIvariableContent'])) {
745           $pkgattrs['FAIvariableContent'] = array();
746         }
748         if (is_array($pkgattrs['FAIvariableContent'])) {
749           $ldap = $this->config->get_ldap_link();
750           $ldap->cat($pkgdn);
751           if (!$ldap->count()) {
752             /* Do not try to set FAIvariableContent to empty array if this
753              * debconfInfo object is new */
754             unset($pkgattrs['FAIvariableContent']);
755           }
756         }
758         /* Tag object */
759        $ui= get_userinfo();
760         $this->tag_attrs($pkgattrs, $pkgdn, $ui->gosaUnitTag);
762         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
764           if($pkgattrs['FAIvariableType'] == "text" && $pkgattrs['FAIvariableContent'] == ""){
765             gosa_log("Skipped saving FAIvariable '$name' empty string can't be saved.");
766           }else{
767             FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
768           }
769         }
770       }
771     }
772   }
775   /*! \brief  Return plugin informations for acl handling 
776       @return Array ACL infos of this plugin.
777    */ 
778   static function plInfo()
779   {
780     return (array( 
781           "plShortName" => _("Package"),
782           "plDescription" => _("FAI Package list"),
783           "plSelfModify"  => FALSE,
784           "plDepends"     => array(),
785           "plPriority"    => 28,
786           "plSection"     => array("administration"),
787           "plCategory"    => array("fai"),
788           "plProvidedAcls" => array(
789             "cn"                => _("Name"),
790             "description"       => _("Description"),
791             "FAIpackage"        => _("Packages"),
792             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
793             "FAIinstallMethod"  => _("Install Method"),
794             "FAIdebconfInfo"    => _("Package configuration"),
795             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
796           ));
797   }
800   /*! \brief prepares this plugin to be inserted after it was copied or cut.
801       @param Array  All attributes from the source object. 
802    */
803   function PrepareForCopyPaste($source)
804   {
805     plugin::PrepareForCopyPaste($source);
807     if(isset($source['FAIstate'][0])){
808       $this->FAIstate = $source['FAIstate'][0];
809     }
811     $this->FAIdebianRelease = "ClearFromCopyPaste";
813     if(isset($source['FAIpackage'])){
814       unset($source['FAIpackage']['count']);
815       foreach($source['FAIpackage'] as $pkg){
816         $this->usedPackages[$pkg] = $pkg;
817       }
818       ksort($this->usedPackages);
819     }else{
820       $this->usedPackages = array();
821     }
823     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
824       $this->FAIdebianSection = array();
825       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
826         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
827       }
828     }
829     $this->ConfiguredPackages = $this->get_configured_packages($this->usedPackages, $source['dn']);
831   }
834   /*! \brief  Returns a list of all configured servers with repositories.
835       @return Array  All repository server 
836    */
837   function getServerInfos()
838   {
839     $ret = array();
840     $ldap = $this->config->get_ldap_link();
841     $ldap->cd($this->config->current['BASE']);
842     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
843     while($attrs = $ldap->fetch()){
844       if(isset($attrs['FAIrepository'])){
845         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
846           $obj = $attrs['FAIrepository'][$i];
847           $tmp = split("\|",$obj);
848           if(count($tmp)==4){
849             foreach(split(",",$tmp[3]) as $sec){
850               if(!empty($sec)){
851                 $ret[$tmp[2]][] =  $sec;
852               }
853             }
854           }
855         }
856       }
857     }
858     return($ret);
859   }
861   /*! \brief Assemble release name 
862     Returns the currently used Debian Release.
863    */ 
864   function getRelease()
865   {
866     /* Assemble release name */
867     if (isset($this->parent->parent->fai_release)) {
868       $release = $this->parent->parent->fai_release;
869     } else {
870       $release = $this->dn;
871       # Remove CN
872       $tmp = preg_replace('/cn='.$this->cn.',/', '', $release);
873       # Remove ou=packages
874       $release = preg_replace('/'.preg_quote(get_ou('faiPackageRDN'), '/').'/', '', $tmp);
875     }
876     $tmp= preg_replace('/[,]*'.preg_quote(get_ou('faiBaseRDN'), '/').'.*$/', '', $release);
877     $tmp= preg_replace('/ou=/', '', $tmp);
878     $rev= array_reverse(split(',', $tmp));
879     $release = "/";
880     foreach ($rev as $part){
881       $release .= "/$part";
882     }
883     return(preg_replace('#^[/]*#', '', $release));
884   }
886   /*! \brief Assemble sections
887     Returns the currently used Debian archive sections.
888    */ 
889   function getSections()
890   {
891     $ret = array();
892     $repos= $this->getServerInfos();
893     if(isset($repos[$this->FAIdebianRelease])){
894       $ret = $repos[$this->FAIdebianRelease];
895       $ret = array_unique($ret);
896     }
897     return $ret;
898   }
900   /*! \brief  Used for copy & paste.
901     Returns a HTML input mask, which allows to change the cn of this entry.
902     @param  Array   Array containing current status && a HTML template.
903    */
904   function getCopyDialog()
905   {
906     $vars = array("cn");
907     $smarty = get_smarty();
908     $smarty->assign("cn", htmlentities($this->cn));
909     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
910     $ret = array();
911     $ret['string'] = $str;
912     $ret['status'] = "";
913     return($ret);
914   }
917   /*! \brief  Used for copy & paste.
918     Some entries must be renamed to avaoid duplicate entries.
919    */
920   function saveCopyDialog()
921   {
922     if(isset($_POST['cn'])){
923       $this->cn = get_post('cn');
924     }
925   }
928 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
929 ?>