Code

Removed show_ldap_error() calls
[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
36   var $mirror                    = ""; // selected mirror
38   var $servers          = array();  // All available servers
39   var $releases         = array();  // All possible releases 
40   var $sections         = array();  // All section types
42   var $list             = NULL;
44   var $mirrors          = array();  // The combination of server/release/section
45   var $confDir          = "";
46   var $usedPackages     = array();
47   var $buffer           = NULL; 
48   var $strID            ="";
49   var $newDialogShown   =false;
51   var $FAIstate         = "";
52   var $view_logged      = FALSE;
53   var $base;
55   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
56       "dselect-upgrade", "taskinst", "taskrm",
57       "hold", "clean", "aptitude", "aptitude-r",
58       "pending", "dpkgc" );
61   function faiPackage (&$config, $dn= NULL)
62   {
63     /* Load Attributes */
64     plugin::plugin ($config, $dn);
66     /* If "dn==new" we try to create a new entry
67      * Else we must read all objects from ldap which belong to this entry.
68      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
69      */
70     if($dn != "new"){
71       $this->dn =$dn;
73       /* Get FAIstate
74        */
75       if(isset($this->attrs['FAIstate'][0])){
76         $this->FAIstate = $this->attrs['FAIstate'][0];
77       }
78     }
80     if(isset($this->attrs['FAIpackage'])){
81       unset($this->attrs['FAIpackage']['count']);
82       foreach($this->attrs['FAIpackage'] as $pkg){
83         $this->usedPackages[$pkg] = $pkg;
84       }
85       ksort($this->usedPackages);
86     }else{
87       $this->usedPackages = array();
88     }  
90     if($dn != "new"){
92       /* Create one filter with all package names, 
93          instead of calling $ldap->search for every single package 
94        */
95       $PackageFilter = "";
96       foreach($this->usedPackages as $name){
97         $PackageFilter .= "(FAIpackage=".$name.")";
98       }
99       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
101       /* Search for configuration objects */ 
102       $ldap = $this->config->get_ldap_link();
103       $ldap->cd($this->dn);
104       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
106       /* Walk through configurations and append them to our list of ConfiguredPackages */
107       while($attr = $ldap->fetch()){
109         /* Skip objects, that are tagged as removed */
110         if(isset($object['FAIstate'][0])){
111           if(preg_match("/removed$/",$attr['FAIstate'][0])){
112             continue;
113           }
114         }
116         $tmp =array(); 
117         $tmp['Name']  = $attr['FAIvariable'][0];
118         $tmp['Type']  = $attr['FAIvariableType'][0];
120         if (isset($attr['FAIvariableContent'][0])){
121           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
122             $tmp['Value'] = $attr['FAIvariableContent'][0];
123           }else{
124             $content = array();        
125             unset($attr['FAIvariableContent']['count']);
126             foreach($attr['FAIvariableContent'] as $attr){
127               $tmp['Value'][] = $attr;
128             }
129           }
130           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
131         }
132       }
133     }
135     if (isset($this->attrs['FAIdebianSection']['count'])){
136       unset($this->attrs['FAIdebianSection']['count']);
137     }
138     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
139       $this->FAIdebianSection = array();
140       foreach($this->attrs['FAIdebianSection'] as $sec){
141         $this->FAIdebianSection[$sec]=$sec;
142       }
143     }
145     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
146       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
147     }
148     $this->confDir = CONFIG_DIR."/fai/";
149     $this->FAIpackage = array();
152     $methods = array();
153     foreach($this->FAIinstallMethods as $method){
154       $methods[$method] = $method;
155     }
156     $this->FAIinstallMethods = $methods;
157     /* Check if we exist already - no need to ask for revisions, etc. then */
158     if ($this->dn != "new"){
159       $this->newDialogShown= true;
160     }
162   }
164   function execute()
165   {
166     /* Call parent execute */
167     plugin::execute();
169     if($this->is_account && !$this->view_logged){
170       $this->view_logged = TRUE;
171       new log("view","fai/".get_class($this),$this->dn);
172     }
174     /* Fill templating stuff */
175     $smarty= get_smarty();
176     $display= "";
178     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
180     if(!$this->is_account){
182       /* Assemble release name */
183       $faifilter = session::get('faifilter');
184       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
185       $tmp= preg_replace('/ou=/', '', $tmp);
186       $rev= array_reverse(split(',', $tmp));
187       $this->FAIdebianRelease= "";
188       foreach ($rev as $part){
189         $this->FAIdebianRelease.= "/$part";
190       }
191       $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
193       /* Assemble sections */
194       $repos= $this->getServerInfos();
195       if(isset($repos[$this->FAIdebianRelease])){
196         $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
197         $this->FAIdebianSection= array_unique($this->FAIdebianSection);
198       }
200       /* Assign Repository settings*/ 
201       $this->is_account     = true;
202     }
204     /* Assign variables */
205     foreach($this->attributes as $attrs){
206       $smarty->assign($attrs,$this->$attrs);
207     }
209     /* Generate package list */
210     $this->list= $this->genPkgs();
212     /* + was pressed to open the package dialog */
213     if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
214       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
215       $this->is_dialog =true;
216     }
218     /* Delte selected package */ 
219     if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
220       if($this->FAIstate != "freeze"){
221         foreach($_POST['usedPackages'] as $del){
222           if(isset($this->usedPackages[$del])){
223             unset($this->usedPackages[$del]);
224           }
225         }
226       }
227     }
229     /* Abort package selection dialog */ 
230     if(isset($_POST['CancelSubObject'])){
231       $this->dialog = false;
232       $this->is_dialog=false;
233     }
235     /* attach new packages */
236     if(isset($_POST['SaveSubObject'])) {
237       if($this->FAIstate != "freeze"){
238         $this->dialog->save_object();
239         if(count($this->dialog->check())){
240           foreach($this->dialog->check() as $msgs){
241             msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
242           }
243         }else{
244           $use = $this->dialog->save();
245           $this->usedPackages = $use;
246           $this->dialog = false;
247           $this->is_dialog=false;
248           ksort($this->usedPackages);
249         }
250       }else{
251         $this->dialog = false;
252         $this->is_dialog=false;
253       }
254     }
256     /* Configuration dialog open*/
257     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
258       $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
259       $pkg_config = array();
260       $pkg = $_POST['usedPackages'][0];
262       if(isset($this->ConfiguredPackages[$pkg])){
263         $pkg_config = $this->ConfiguredPackages[$pkg];
264       }
266       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
267       $this->is_dialog =true;
268     }
270     /* Configuration dialog open*/
271     if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
272       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
273         foreach($_POST['usedPackages'] as $pkg){
274           if (isset($this->usedPackages[$pkg])){
275             unset($this->usedPackages[$pkg]);
276             if (preg_match('/^-/', $pkg)){
277               $pkg= preg_replace('/^-/', '', $pkg);
278             } else {
279               $pkg= preg_replace('/^/', '-', $pkg);
280             }
281             $this->usedPackages[$pkg]= $pkg;
282           }
283         }
284       }
285     }
287     /* Save Configuration */
288     if(isset($_POST['SaveObjectConfig'])){
289       if($this->FAIstate != "freeze"){
290         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
291       }
292       $this->dialog = false;
293       $this->is_dialog=false;
294     }
296     /* cancel configuration */     
297     if(isset($_POST['CancelObjectConfig'])){
298       $this->dialog = false;
299       $this->is_dialog=false;
300     }
302     /* Display dialog */ 
303     if($this->is_dialog){
304       return $this->dialog->execute();
305     }
307     /* Assign section to smarty */
308     $strsec = "";
309     foreach($this->FAIdebianSection as $sec){
310       $strsec .= $sec." ";
311     }
313     $tmp = $this->plInfo();
314     foreach($tmp['plProvidedAcls'] as $name => $translated){
315       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
316     }
318     $smarty->assign("releases",$this->releases);
319     $smarty->assign("release" ,$this->FAIdebianRelease);
320     $smarty->assign("sections",$this->sections);
321     $smarty->assign("section" ,$strsec);
322     $smarty->assign("usedPackages",$this->printUsedPackages());
323     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
324     return($display);
325   }
327   /* Delete me, and all my subtrees
328    */
329   function remove_from_parent()
330   {
331     $ldap = $this->config->get_ldap_link();
332     $ldap->cd ($this->dn);
334     $faifilter = session::get('faifilter');
335     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
336     if($faifilter['branch'] == "main"){
337       $use_dn = $this->dn;
338     }
340     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
342     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
344     foreach($this->ConfiguredPackages as $pkgname => $attrs){
345       foreach($attrs as $name => $attr){
346         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
347         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
348         if($faifilter['branch'] == "main"){
349           $use_dn = $obj['dn'];
350         }
351         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
352       }
353     }
354     $this->handle_post_events("remove");
355   }
358   /* Save data to object 
359    */
360   function save_object()
361   {
362     if($this->FAIstate == "freeze") return;  
363     plugin::save_object();
364   }
367   /* Check supplied data */
368   function check()
369   {
370     /* Call common method to give check the hook */
371     $message= plugin::check();
373     if(count($this->usedPackages)==0){
374       $message[]=_("Please select a least one package!");
375     }
377     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
378       $message[]=_("Please choose a valid release/section combination for your repository setup!");
379     }
381     return ($message);
382   }
384   function printUsedPackages(){
385     $a_ret=array(); 
386     if(is_array($this->usedPackages)) {
387       foreach($this->usedPackages as $usedName){
389         $config = 0;
391         foreach($this->ConfiguredPackages as $name => $value){
392           if($name == $usedName){
393             $config ++;
394           }
395         }
397         $c_str ="";
398         if($config){
399           $c_str = " - "._("package is configured");
400         }
402         /* Adapt used name if we're marked for removal */
403         $dsc= "";
404         if (preg_match('/^-/', $usedName)){
405           $dsc= " - "._("Package marked for removal");
406           // Generally a bad idea here, because the toggel triggers on -, not on !
407           //$usedName= preg_replace('/^-/', '! ', $usedName);
408         }else{
409           $usedName2= $usedName;
410         }
412         if(isset($this->list[$usedName][1])){
413           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
414         }else{
415           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
416         }
417       }
418     }
419     return($a_ret);
420   }
422   function genPkgs()
423   {
424     /* Generate a list off available packages for this mirror, section and release
425      */
426     /* Only read this file if it wasn't read before */
427     if($this->buffer === NULL){
428       $this->buffer=array();
429       $a_ret = array();
430       foreach($this->FAIdebianSection as $sec){
431         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
433         if(!is_file($strID)){
434           msg_dialog::display(_("Error"), sprintf(_("Package file '%s' does not exist!"), $strID), ERROR_DIALOG);
435           unset($this->buffer);
436           return(array());
437         }
438         $fp = fopen($strID,"r");
440         /* Parse every line and create an array */
441         while(!feof($fp)){
442           $str= fgets($fp,512);
443           $stra= split("\|",$str);
444           if(count($stra)==4){
445             $a_ret[$stra[0]] = $stra;
446           }
447         }
448         fclose($fp);
449         /* Save our Data, to avoid reading it again */
450       }
451       $this->buffer = $a_ret;
452       ksort($a_ret);
453       return($a_ret);
454     }else{
455       return $this->buffer;
456     }
457   }
460   /* Save to LDAP */
461   function save()
462   {
464     /* Assemble release name */
465     if($this->FAIdebianRelease == "ClearFromCopyPaste"){
466       $faifilter = session::get('faifilter');
467       $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
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     if (!$ldap->success()){
493       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
494     }
495     
496     if($this->initially_was_account){
497       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
498     }else{
499       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
500     }
502     $ldap->cd($this->dn);
504     /* Save Package configurations */
505     foreach($this->ConfiguredPackages as $pkgname => $attrs){
506       foreach($attrs as $name => $attr){
507       
508         $pkgattrs = array();
510         foreach($attr as $n=>$v){
511           if(empty($v)) $v = array();
512         }
514         /* Set attributes */
515         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
517         $pkgattrs['FAIpackage']           = $pkgname;
518         $pkgattrs['FAIvariable']          = $name;
519         $pkgattrs['FAIvariableType']      = $attr['Type'];
520         $pkgattrs['FAIvariableContent']   = $attr['Value'];
521         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
523         /* Tag object */
524         $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
526         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
527           FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
528         }
529       }
530     }
531   }
533   /* Return plugin informations for acl handling */ 
534   static function plInfo()
535   {
536     return (array( 
537           "plShortName" => _("Package"),
538           "plDescription" => _("FAI Package list"),
539           "plSelfModify"  => FALSE,
540           "plDepends"     => array(),
541           "plPriority"    => 28,
542           "plSection"     => array("administration"),
543           "plCategory"    => array("fai"),
544           "plProvidedAcls" => array(
545             "cn"                => _("Name"),
546             "description"       => _("Description"),
547             "FAIpackage"        => _("Packages"),
548             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
549             "FAIinstallMethod"  => _("Install Method"),
550             "FAIdebconfInfo"    => _("Package configuration"),
551             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
552           ));
553   }
558   function PrepareForCopyPaste($source)
559   {
560     plugin::PrepareForCopyPaste($source);
562     if(isset($source['FAIstate'][0])){
563       $this->FAIstate = $source['FAIstate'][0];
564     }
566     $this->FAIdebianRelease = "ClearFromCopyPaste";
568     if(isset($source['FAIpackage'])){
569       unset($source['FAIpackage']['count']);
570       foreach($source['FAIpackage'] as $pkg){
571         $this->usedPackages[$pkg] = $pkg;
572       }
573       ksort($this->usedPackages);
574     }else{
575       $this->usedPackages = array();
576     }
578     if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
579       $this->FAIdebianSection = array();
580       for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
581         $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
582       }
583     }
585     /* Create one filter with all package names, 
586        instead of calling $ldap->search for every single package 
587      */
588     $PackageFilter = "";
589     foreach($this->usedPackages as $name){
590       $PackageFilter .= "(FAIpackage=".$name.")";
591     }
592     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
594     /* Search for configuration objects */ 
595     $ldap = $this->config->get_ldap_link();
596     $ldap->cd($source['dn']);
597     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
599     /* Walk through configurations and append them to our list of ConfiguredPackages */
600     while($attr = $ldap->fetch()){
602       /* Skip objects, that are tagged as removed */
603       if(isset($object['FAIstate'][0])){
604         if(preg_match("/removed$/",$attr['FAIstate'][0])){
605           continue;
606         }
607       }
609       $tmp =array(); 
610       $tmp['Name']  = $attr['FAIvariable'][0];
611       $tmp['Type']  = $attr['FAIvariableType'][0];
613       if (isset($attr['FAIvariableContent'][0])){
614         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
615           $tmp['Value'] = $attr['FAIvariableContent'][0];
616         }else{
617           $content = array();        
618           unset($attr['FAIvariableContent']['count']);
619           foreach($attr['FAIvariableContent'] as $attr){
620             $tmp['Value'][] = $attr;
621           }
622         }
623         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
624       }
625     }
626   }
629   function getServerInfos()
630   {
631     $ret = array();
632     $ldap = $this->config->get_ldap_link();
633     $ldap->cd($this->config->current['BASE']);
634     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
635     while($attrs = $ldap->fetch()){
636       if(isset($attrs['FAIrepository'])){
637         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
638           $obj = $attrs['FAIrepository'][$i];
639           $tmp = split("\|",$obj);
640           if(count($tmp)==4){
641             foreach(split(",",$tmp[3]) as $sec){
642               if(!empty($sec)){
643                 $ret[$tmp[2]][] =  $sec;
644               }
645             }
646           }
647         }
648       }
649     }
650     return($ret);
651   }
656 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
657 ?>