Code

Updated check
[gosa.git] / 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();
27   /* Specific attributes */
28   var $cn               = "";       // The class name for this object
29   var $description      = "";       // The description for this set of partitions
30   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
31   var $SubObjects       = array();  // All leafobjects of this object
33   var $FAIdebianRelease          = ""; // The selected release
34   var $FAIdebianSection          = array(); // selected section
35   var $FAIinstallMethod          = "aptitude"; // hard coded
37   var $sections         = array();  // All section types
39   var $list             = NULL;
41   var $usedPackages     = array();
42   var $buffer           = NULL; 
43   var $newDialogShown   =false;
45   var $FAIstate         = "";
46   var $view_logged      = FALSE;
47   var $base;
48   var $FAIpackage ;
50   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
51       "dselect-upgrade", "taskinst", "taskrm",
52       "hold", "clean", "aptitude", "aptitude-r",
53       "pending", "dpkgc" );
56   function faiPackage (&$config, $dn= NULL)
57   {
58     /* Load Attributes */
59     plugin::plugin ($config, $dn);
61     /* If "dn==new" we try to create a new entry
62      * Else we must read all objects from ldap which belong to this entry.
63      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
64      */
65     if($dn != "new"){
66       $this->dn =$dn;
68       /* Get FAIstate */
69       if(isset($this->attrs['FAIstate'][0])){
70         $this->FAIstate = $this->attrs['FAIstate'][0];
71       }
73       /* Check if there are already some packages in this list 
74        */
75       $this->usedPackages = array();
76       if(isset($this->attrs['FAIpackage'])){
77         unset($this->attrs['FAIpackage']['count']);
78         foreach($this->attrs['FAIpackage'] as $pkg){
79           $this->usedPackages[$pkg] = $pkg;
80         }
81         ksort($this->usedPackages);
82       }  
84       /* Fetch all package configurations from ldap 
85        */
86       $PackageFilter = "";
87       foreach($this->usedPackages as $name){
88         $PackageFilter .= "(FAIpackage=".$name.")";
89       }
90       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
92       /* Search for configuration objects */ 
93       $ldap = $this->config->get_ldap_link();
94       $ldap->cd($this->dn);
95       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
96             "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
98       /* Walk through configurations and append them to our list of ConfiguredPackages 
99        */
100       while($attr = $ldap->fetch()){
102         /* Skip objects, that are tagged as removed */
103         if(isset($object['FAIstate'][0])){
104           if(preg_match("/removed$/",$attr['FAIstate'][0])){
105             continue;
106           }
107         }
109         $tmp =array(); 
110         $tmp['Name']  = $attr['FAIvariable'][0];
111         $tmp['Type']  = $attr['FAIvariableType'][0];
113         if (isset($attr['FAIvariableContent'][0])){
114           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
115             $tmp['Value'] = $attr['FAIvariableContent'][0];
116           }else{
117             $content = array();        
118             unset($attr['FAIvariableContent']['count']);
119             foreach($attr['FAIvariableContent'] as $attr){
120               $tmp['Value'][] = $attr;
121             }
122           }
123           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
124         }
125       }
127       $this->FAIdebianSection = array();
128       if(isset($this->attrs['FAIdebianSection'])){
129         for($i = 0 ; $i < $this->attrs['FAIdebianSection']['count'] ; $i++ ){ 
130           $sec = $this->attrs['FAIdebianSection'][$i];
131           $this->FAIdebianSection[$sec]=$sec;
132         }
133       }
134       $this->FAIpackage = array();
136     } // ENDE  dn != new  
138     $methods = array();
139     foreach($this->FAIinstallMethods as $method){
140       $methods[$method] = $method;
141     }
142     $this->FAIinstallMethods = $methods;
144     /* Check if we exist already - no need to ask for revisions, etc. then */
145     if ($this->dn != "new"){
146       $this->newDialogShown= true;
147     }
148     $this->is_new = FALSE;
149     if($this->dn == "new"){
150       $this->is_new =TRUE;
151     }
153     /* Generate package list */
154     $this->list= $this->genPkgs(TRUE);
155   }
158   function execute()
159   {
160     /* Call parent execute */
161     plugin::execute();
163     if($this->is_account && !$this->view_logged){
164       $this->view_logged = TRUE;
165       new log("view","fai/".get_class($this),$this->dn);
166     }
168     /* Fill templating stuff */
169     $smarty= get_smarty();
170     $display= "";
172     /******
173      * Initialize a new Package List with release and section name
174      ******/
175     
176     if(!$this->is_account){
178       /* Assemble release name */
179       $release = $this->parent->parent->fai_release;
180       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $release);
181       $tmp= preg_replace('/ou=/', '', $tmp);
182       $rev= array_reverse(split(',', $tmp));
183       $this->FAIdebianRelease= "";
184       foreach ($rev as $part){
185         $this->FAIdebianRelease.= "/$part";
186       }
187       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
189       /* Assemble sections */
190       $repos= $this->getServerInfos();
191       if(isset($repos[$this->FAIdebianRelease])){
192         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
193         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
194       }
196       /* Assign Repository settings*/ 
197       $this->is_account     = true;
198     }
199     
201     /******
202      * Add && Remove Packages 
203      ******/
205     /* + was pressed to open the package dialog */
206     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
207       $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
208       $this->is_dialog =true;
209     }
211     /* Delete selected package */ 
212     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
213       if(!preg_match("/^freeze/", $this->FAIstate)){
214         foreach($_POST['usedPackages'] as $del){
215           if(isset($this->usedPackages[$del])){
216             unset($this->usedPackages[$del]);
217           }
218         }
220         /* Generate package list */
221         $this->list= $this->genPkgs(TRUE);
222       }
223     }
225     /* Abort package selection dialog */ 
226     if(isset($_POST['CancelSubObject'])){
227       $this->dialog = false;
228       $this->is_dialog=false;
229     }
231     /* attach new packages */
232     if(isset($_POST['SaveSubObject'])) {
233       if(!preg_match("/^freeze/", $this->FAIstate)){
234         $this->dialog->save_object();
235         if(count($this->dialog->check())){
236           foreach($this->dialog->check() as $msgs){
237             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
238           }
239         }else{
240           $use = $this->dialog->save();
241           $this->usedPackages = $use;
242           $this->dialog = false;
243           $this->is_dialog=false;
244           ksort($this->usedPackages);
246           /* Generate package list */
247           $this->list= $this->genPkgs(TRUE);
248         }
249       }else{
250         $this->dialog = false;
251         $this->is_dialog=false;
252       }
253     }
256     /******
257      * Configure Packages  
258      ******/
260     /* Configuration dialog open*/
261     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
262       $pkg_config = array();
263       $pkg = $_POST['usedPackages'][0];
264       if(isset($this->ConfiguredPackages[$pkg])){
265         $pkg_config = $this->ConfiguredPackages[$pkg];
266       }
267       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
268       $this->is_dialog =true;
269     }
271     /* Configuration dialog open*/
272     if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
273       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
274         foreach($_POST['usedPackages'] as $pkg){
275           if (isset($this->usedPackages[$pkg])){
276             unset($this->usedPackages[$pkg]);
277             if (preg_match('/^-/', $pkg)){
278               $pkg= preg_replace('/^-/', '', $pkg);
279             } else {
280               $pkg= preg_replace('/^/', '-', $pkg);
281             }
282             $this->usedPackages[$pkg]= $pkg;
283           }
284         }
285         /* Generate package list */
286         $this->list= $this->genPkgs(TRUE);
287       }
288     }
290     /* Save Configuration */
291     if(isset($_POST['SaveObjectConfig'])){
292       if(!preg_match("/^freeze/", $this->FAIstate)){
293         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
294       }
295       $this->dialog = false;
296       $this->is_dialog=false;
297     }
299     /* cancel configuration */     
300     if(isset($_POST['CancelObjectConfig'])){
301       $this->dialog = false;
302       $this->is_dialog=false;
303     }
305     /* Display dialog */ 
306     if($this->is_dialog){
307       $this->dialog->save_object();
308       return $this->dialog->execute();
309     }
312     /******
313      * Display UI / HTML / smarty 
314      ******/
316     /* Assign variables */
317     foreach($this->attributes as $attrs){
318       $smarty->assign($attrs,$this->$attrs);
319     }
320     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
322     /* Assign section to smarty */
323     $strsec = "";
324     foreach($this->FAIdebianSection as $sec){
325       $strsec .= $sec." ";
326     }
328     $tmp = $this->plInfo();
329     foreach($tmp['plProvidedAcls'] as $name => $translated){
330       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
331     }
333     $smarty->assign("release" ,$this->FAIdebianRelease);
334     $smarty->assign("sections",$this->sections);
335     $smarty->assign("section" ,$strsec);
336     $smarty->assign("usedPackages",$this->printUsedPackages());
337     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
338     return($display);
339   }
342   /*! \brief  Removes this packageList from the ldap database 
343    */
344   function remove_from_parent()
345   {
346     $ldap = $this->config->get_ldap_link();
347     $ldap->cd ($this->dn);
348     $release = $this->parent->parent->fai_release;
349     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
350     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
351     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
352     foreach($this->ConfiguredPackages as $pkgname => $attrs){
353       foreach($attrs as $name => $attr){
354         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
355         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $pkgdn);
356         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
357       }
358     }
359     $this->handle_post_events("remove");
360   }
363   /*! \brief  Collect all relevant POST vars for this plugin 
364    */
365   function save_object()
366   {
367     if(preg_match("/^freeze/", $this->FAIstate)) return;
368     plugin::save_object();
369   }
372   /*! \brief  Check given inputs for this package list
373       @return Array Containing all error messages, or an empty array if no error occured
374    */
375   function check()
376   {
377     /* Call common method to give check the hook */
378     $message= plugin::check();
380     if(count($this->usedPackages)==0){
381       $message[]= _("Please select a least one package!");
382     }
384     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
385       $message[]=_("Please choose a valid release/section combination for your repository setup!");
386     }
388     /* Ensure that we do not overwrite an allready existing entry 
389      */
390     if($this->is_new){
391       $release = $this->parent->parent->fai_release;
392       $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').$release;
393       $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
394       if(isset($res[$this->cn])){
395         $message[] = msgPool::duplicated(_("Name"));
396       }
397     }
398     return ($message);
399   }
402   /*! \brief  Returns all selected packages in a printable format 
403       @return Array   Containing all package infos.
404    */
405   function printUsedPackages()
406   {
407     $a_ret=array(); 
408     if(is_array($this->usedPackages)){
409       foreach($this->usedPackages as $usedName){
411         /* Append message if package is configured */
412         $c_str ="";
413         if(isset($this->ConfiguredPackages[$usedName])){
414           $c_str = " - "._("package is configured");
415         }
417         /* Adapt used name if we're marked for removal */
418         $dsc= "";
419         if (preg_match('/^-/', $usedName)){
420           $dsc= " - "._("Package marked for removal");
421         }else{
422           $usedName2= $usedName;
423         }
425         /* Append version string, if available */
426         if(isset($this->list[$usedName]['VERSION'])){
427           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName]['VERSION']."]".$c_str.$dsc;
428         }else{
429           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
430         }
431       }
432     }
433     return($a_ret);
434   }
437   /*! \brief  Reload the list of cached packages.
438       @return Returns the currently cached list of packages. 
439    */
440   function genPkgs($force = false)
441   {
442     if(!count($this->buffer) || $force){
443       $q = new gosaSupportDaemon();
444       $attrs = array("distribution", "package","version", "section", "description", "timestamp");
445       $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$this->usedPackages);
446       if($q->is_error()){
447         msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
448       }else{
449         foreach($ret as $attr){
450           $this->buffer[$attr['PACKAGE']] = $attr;
451         }
452       }
453     }
454     return $this->buffer;
455   }
458   /*! \brief Save packages and their configuration to ldap 
459    */
460   function save()
461   {
463     /* Assemble release name */
464     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
466       $current_release  = $this->parent->parent->fai_release;
467       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $current_release);
468       $tmp= preg_replace('/ou=/', '', $tmp);
469       $rev= array_reverse(split(',', $tmp));
470       $this->FAIdebianRelease= "";
471       foreach ($rev as $part){
472         $this->FAIdebianRelease.= "/$part";
473       }
474       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
475     }
477     plugin::save();
479     $ldap = $this->config->get_ldap_link();
481     $this->attrs['FAIpackage'] = array();
482     foreach($this->usedPackages as $pkg => $obj){
483       $this->attrs['FAIpackage'][] = $pkg;
484     } 
486     $this->attrs['FAIdebianSection'] = array();
487     foreach($this->FAIdebianSection as $sec){
488       $this->attrs['FAIdebianSection'][] = $sec;
489     }
491     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
492     
493     if($this->initially_was_account){
494       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
495     }else{
496       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
497     }
499     $ldap->cd($this->dn);
501     /* Save Package configurations */
502     foreach($this->ConfiguredPackages as $pkgname => $attrs){
503       foreach($attrs as $name => $attr){
504       
505         $pkgattrs = array();
507         foreach($attr as $n=>$v){
508           if(empty($v)) $v = array();
509         }
511         /* Set attributes */
512         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
514         $pkgattrs['FAIpackage']           = $pkgname;
515         $pkgattrs['FAIvariable']          = $name;
516         $pkgattrs['FAIvariableType']      = $attr['Type'];
517         $pkgattrs['FAIvariableContent']   = $attr['Value'];
518         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
520         /* Tag object */
521         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
523         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
525           if($pkgattrs['FAIvariableType'] == "text" && $pkgattrs['FAIvariableContent'] == ""){
526             gosa_log("Skipped saving FAIvariable '$name' empty string can't be saved.");
527           }else{
528             FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
529           }
530         }
531       }
532     }
533   }
536   /*! \brief  Return plugin informations for acl handling 
537       @return Array ACL infos of this plugin.
538    */ 
539   static function plInfo()
540   {
541     return (array( 
542           "plShortName" => _("Package"),
543           "plDescription" => _("FAI Package list"),
544           "plSelfModify"  => FALSE,
545           "plDepends"     => array(),
546           "plPriority"    => 28,
547           "plSection"     => array("administration"),
548           "plCategory"    => array("fai"),
549           "plProvidedAcls" => array(
550             "cn"                => _("Name"),
551             "description"       => _("Description"),
552             "FAIpackage"        => _("Packages"),
553             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
554             "FAIinstallMethod"  => _("Install Method"),
555             "FAIdebconfInfo"    => _("Package configuration"),
556             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
557           ));
558   }
561   /*! \brief prepares this plugin to be inserted after it was copied or cut.
562       @param Array  All attributes from the source object. 
563    */
564   function PrepareForCopyPaste($source)
565   {
566     plugin::PrepareForCopyPaste($source);
568     if(isset($source['FAIstate'][0])){
569       $this->FAIstate = $source['FAIstate'][0];
570     }
572     $this->FAIdebianRelease = "ClearFromCopyPaste";
574     if(isset($source['FAIpackage'])){
575       unset($source['FAIpackage']['count']);
576       foreach($source['FAIpackage'] as $pkg){
577         $this->usedPackages[$pkg] = $pkg;
578       }
579       ksort($this->usedPackages);
580     }else{
581       $this->usedPackages = array();
582     }
584     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
585       $this->FAIdebianSection = array();
586       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
587         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
588       }
589     }
591     /* Create one filter with all package names, 
592        instead of calling $ldap->search for every single package 
593      */
594     $PackageFilter = "";
595     foreach($this->usedPackages as $name){
596       $PackageFilter .= "(FAIpackage=".$name.")";
597     }
598     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
600     /* Search for configuration objects */ 
601     $ldap = $this->config->get_ldap_link();
602     $ldap->cd($source['dn']);
603     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
605     /* Walk through configurations and append them to our list of ConfiguredPackages */
606     while($attr = $ldap->fetch()){
608       /* Skip objects, that are tagged as removed */
609       if(isset($object['FAIstate'][0])){
610         if(preg_match("/removed$/",$attr['FAIstate'][0])){
611           continue;
612         }
613       }
615       $tmp =array(); 
616       $tmp['Name']  = $attr['FAIvariable'][0];
617       $tmp['Type']  = $attr['FAIvariableType'][0];
619       if (isset($attr['FAIvariableContent'][0])){
620         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
621           $tmp['Value'] = $attr['FAIvariableContent'][0];
622         }else{
623           $content = array();        
624           unset($attr['FAIvariableContent']['count']);
625           foreach($attr['FAIvariableContent'] as $attr){
626             $tmp['Value'][] = $attr;
627           }
628         }
629         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
630       }
631     }
632   }
635   /*! \brief  Returns a list of all configured servers with repositories.
636       @return Array  All repository server 
637    */
638   function getServerInfos()
639   {
640     $ret = array();
641     $ldap = $this->config->get_ldap_link();
642     $ldap->cd($this->config->current['BASE']);
643     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
644     while($attrs = $ldap->fetch()){
645       if(isset($attrs['FAIrepository'])){
646         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
647           $obj = $attrs['FAIrepository'][$i];
648           $tmp = split("\|",$obj);
649           if(count($tmp)==4){
650             foreach(split(",",$tmp[3]) as $sec){
651               if(!empty($sec)){
652                 $ret[$tmp[2]][] =  $sec;
653               }
654             }
655           }
656         }
657       }
658     }
659     return($ret);
660   }
663   /*! \brief  Used for copy & paste.
664     Returns a HTML input mask, which allows to change the cn of this entry.
665     @param  Array   Array containing current status && a HTML template.
666    */
667   function getCopyDialog()
668   {
669     $vars = array("cn");
670     $smarty = get_smarty();
671     $smarty->assign("cn", htmlentities($this->cn));
672     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
673     $ret = array();
674     $ret['string'] = $str;
675     $ret['status'] = "";
676     return($ret);
677   }
680   /*! \brief  Used for copy & paste.
681     Some entries must be renamed to avaoid duplicate entries.
682    */
683   function saveCopyDialog()
684   {
685     if(isset($_POST['cn'])){
686       $this->cn = get_post('cn');
687     }
688   }
691 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
692 ?>