Code

Added some logging
[gosa.git] / plugins / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description"); 
28   var $sub64coded       = array();
30   var $ConfiguredPackages = array();
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIdebianRelease          = ""; // The selected release
40   var $FAIdebianSection          = array(); // selected section
41   var $FAIinstallMethod          = "aptitude"; // hard coded
42   var $mirror                    = ""; // selected mirror
44   var $servers          = array();  // All available servers
45   var $releases         = array();  // All possible releases 
46   var $sections         = array();  // All section types
48   var $list             = NULL;
50   var $mirrors          = array();  // The combination of server/release/section
51   var $confDir          = "";
52   var $usedPackages     = array();
53   var $buffer           = NULL; 
54   var $strID            ="";
55   var $newDialogShown   =false;
57   var $FAIstate         = "";
59   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
60       "dselect-upgrade", "taskinst", "taskrm",
61       "hold", "clean", "aptitude", "aptitude-r",
62       "pending", "dpkgc" );
65   function faiPackage ($config, $dn= NULL)
66   {
67     /* Load Attributes */
68     plugin::plugin ($config, $dn);
70     /* If "dn==new" we try to create a new entry
71      * Else we must read all objects from ldap which belong to this entry.
72      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
73      */
74     if($dn != "new"){
75       $this->dn =$dn;
77       /* Get FAIstate
78        */
79       if(isset($this->attrs['FAIstate'][0])){
80         $this->FAIstate = $this->attrs['FAIstate'][0];
81       }
82     }
84     if(isset($this->attrs['FAIpackage'])){
85       unset($this->attrs['FAIpackage']['count']);
86       foreach($this->attrs['FAIpackage'] as $pkg){
87         $this->usedPackages[$pkg] = $pkg;
88       }
89       ksort($this->usedPackages);
90     }else{
91       $this->usedPackages = array();
92     }  
94     if($dn != "new"){
96       /* Create one filter with all package names, 
97          instead of calling $ldap->search for every single package 
98        */
99       $PackageFilter = "";
100       foreach($this->usedPackages as $name){
101         $PackageFilter .= "(FAIpackage=".$name.")";
102       }
103       $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
105       /* Search for configuration objects */ 
106       $ldap = $this->config->get_ldap_link();
107       $ldap->cd($this->dn);
108       $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
110       /* Walk through configurations and append them to our list of ConfiguredPackages */
111       while($attr = $ldap->fetch()){
113         /* Skip objects, that are tagged as removed */
114         if(isset($object['FAIstate'][0])){
115           if(preg_match("/removed$/",$attr['FAIstate'][0])){
116             continue;
117           }
118         }
120         $tmp =array(); 
121         $tmp['Name']  = $attr['FAIvariable'][0];
122         $tmp['Type']  = $attr['FAIvariableType'][0];
124         if (isset($attr['FAIvariableContent'][0])){
125           if(!in_array($attr['FAIvariableType'],array("multiselect"))){
126             $tmp['Value'] = $attr['FAIvariableContent'][0];
127           }else{
128             $content = array();        
129             unset($attr['FAIvariableContent']['count']);
130             foreach($attr['FAIvariableContent'] as $attr){
131               $tmp['Value'][] = $attr;
132             }
133           }
134           $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
135         }
136       }
137       @log::log("view","fai/".get_class($this),$this->dn);
138     }
140     if (isset($this->attrs['FAIdebianSection']['count'])){
141       unset($this->attrs['FAIdebianSection']['count']);
142     }
143     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
144       $this->FAIdebianSection = array();
145       foreach($this->attrs['FAIdebianSection'] as $sec){
146         $this->FAIdebianSection[$sec]=$sec;
147       }
148     }
150     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
151       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
152     }
153     $this->confDir = CONFIG_DIR."/fai/";
154     $this->FAIpackage = array();
157     $methods = array();
158     foreach($this->FAIinstallMethods as $method){
159       $methods[$method] = $method;
160     }
161     $this->FAIinstallMethods = $methods;
162     /* Check if we exist already - no need to ask for revisions, etc. then */
163     if ($this->dn != "new"){
164       $this->newDialogShown= true;
165     }
167   }
169   function execute()
170   {
171     /* Call parent execute */
173     plugin::execute();
175     /* Fill templating stuff */
176     $smarty= get_smarty();
177     $display= "";
179     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
181     if(!$this->is_account){
183       /* Assemble release name */
184       $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['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             print_red($msgs);
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'])) && $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 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
335     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
336     if($_SESSION['faifilter']['branch'] == "main"){
337       $use_dn = $this->dn;
338     }
340     prepare_to_save_FAI_object($use_dn,array(),true);
342     @log::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 = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
348         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
349         if($_SESSION['faifilter']['branch'] == "main"){
350           $use_dn = $obj['dn'];
351         }
352         prepare_to_save_FAI_object($use_dn,array(),true);
353       }
354     }
355     $this->handle_post_events("remove");
356   }
359   /* Save data to object 
360    */
361   function save_object()
362   {
363     if($this->FAIstate == "freeze") return;  
364     plugin::save_object();
365   }
368   /* Check supplied data */
369   function check()
370   {
371     /* Call common method to give check the hook */
372     $message= plugin::check();
374     if(count($this->usedPackages)==0){
375       $message[]=_("Please select a least one Package.");
376     }
378     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
379       $message[]=_("Please choose a valid combination for your repository setup.");
380     }
382     return ($message);
383   }
385   function printUsedPackages(){
386     $a_ret=array(); 
387     if(is_array($this->usedPackages)) {
388       foreach($this->usedPackages as $usedName){
390         $config = 0;
392         foreach($this->ConfiguredPackages as $name => $value){
393           if($name == $usedName){
394             $config ++;
395           }
396         }
398         $c_str ="";
399         if($config){
400           $c_str = " - "._("package is configured");
401         }
403         /* Adapt used name if we're marked for removal */
404         $dsc= "";
405         if (preg_match('/^-/', $usedName)){
406           $dsc= " - "._("Package marked for removal");
407           // Generally a bad idea here, because the toggel triggers on -, not on !
408           //$usedName= preg_replace('/^-/', '! ', $usedName);
409         }else{
410           $usedName2= $usedName;
411         }
413         if(isset($this->list[$usedName][1])){
414           $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
415         }else{
416           $a_ret[$usedName] = $usedName2.$c_str.$dsc;
417         }
418       }
419     }
420     return($a_ret);
421   }
423   function genPkgs()
424   {
425     /* Generate a list off available packages for this mirror, section and release
426      */
427     /* Only read this file if it wasn't read before */
428     if($this->buffer==NULL){
429       $this->buffer=array();
430       $a_ret = array();
431       foreach($this->FAIdebianSection as $sec){
432         $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
434         if(!is_file($strID)){
435           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
436           unset($this->buffer);
437           return(array());
438         }
439         $fp = fopen($strID,"r");
441         /* Parse every line and create an array */
442         while(!feof($fp)){
443           $str= fgets($fp,512);
444           $stra= split("\|",$str);
445           if(count($stra)==4){
446             $a_ret[$stra[0]] = $stra;
447           }
448         }
449         fclose($fp);
450         /* Save our Data, to avoid reading it again */
451       }
452       $this->buffer = $a_ret;
453       ksort($a_ret);
454       return($a_ret);
455     }else{
456       return $this->buffer;
457     }
458   }
461   /* Save to LDAP */
462   function save()
463   {
464     plugin::save();
466     $ldap = $this->config->get_ldap_link();
468     $this->attrs['FAIpackage'] = array();
469     foreach($this->usedPackages as $pkg => $obj){
470       $this->attrs['FAIpackage'][] = $pkg;
471     } 
473     $this->attrs['FAIdebianSection'] = array();
474     foreach($this->FAIdebianSection as $sec){
475       $this->attrs['FAIdebianSection'][] = $sec;
476     }
478     prepare_to_save_FAI_object($this->dn,$this->attrs);
479     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
480     
481     if($this->initially_was_account){
482       @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
483     }else{
484       @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
485     }
487     /* Do object tagging */
488     $this->handle_object_tagging();
489     $ldap->cd($this->dn);
491     /* Save Package configurations */
492     foreach($this->ConfiguredPackages as $pkgname => $attrs){
493       foreach($attrs as $name => $attr){
494       
495         $pkgattrs = array();
497         foreach($attr as $n=>$v){
498           if(empty($v)) $v = array();
499         }
501         /* Set attributes */
502         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
504         $pkgattrs['FAIpackage']           = $pkgname;
505         $pkgattrs['FAIvariable']          = $name;
506         $pkgattrs['FAIvariableType']      = $attr['Type'];
507         $pkgattrs['FAIvariableContent']   = $attr['Value'];
508         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
510         /* cehck if object already exists */
511         $ldap->cat($pkgdn,array("objectClass"));
513         /* Workaround for missing "gosaAdministrativeUnitTag" */
514         $attrs = $ldap->fetch();
515         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
516           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
517         }
519         if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
520           prepare_to_save_FAI_object($pkgdn,$pkgattrs);
521         }
522   
523         /* Handle tagging */
524         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
525       }
526     }
527   }
529   /* Return plugin informations for acl handling */ 
530   function plInfo()
531   {
532     return (array( 
533           "plShortName" => _("Package"),
534           "plDescription" => _("FAI Package list"),
535           "plSelfModify"  => FALSE,
536           "plDepends"     => array(),
537           "plPriority"    => 28,
538           "plSection"     => array("administration"),
539           "plCategory"    => array("fai"),
540           "plProvidedAcls" => array(
541             "cn"                => _("Name"),
542             "description"       => _("Description"),
543             "FAIpackage"        => _("Packages"),
544             "FAIdebianSection"  => _("Section")."&nbsp;("._("Readonly").")",
545             "FAIinstallMethod"  => _("Install Method"),
546             "FAIdebconfInfo"    => _("Package configuration"),
547             "FAIdebianRelease"  => _("Release")."&nbsp;("._("Readonly").")")
548           ));
549   }
551   function getServerInfos()
552   {
553     $ret = array();
554     $ldap = $this->config->get_ldap_link();
555     $ldap->cd($this->config->current['BASE']);
556     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
557     while($attrs = $ldap->fetch()){
558       if(isset($attrs['FAIrepository'])){
559         for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
560           $obj = $attrs['FAIrepository'][$i];
561           $tmp = split("\|",$obj);
562           if(count($tmp)==4){
563             foreach(split(",",$tmp[3]) as $sec){
564               if(!empty($sec)){
565                 $ret[$tmp[2]][] =  $sec;
566               }
567             }
568           }
569         }
570       }
571     }
572     return($ret);
573   }
578 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
579 ?>