Code

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