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 = ""; // 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 $this->acl ="#all#";
72 /* If "dn==new" we try to create a new entry
73 * Else we must read all objects from ldap which belong to this entry.
74 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
75 */
76 if($dn != "new"){
77 $this->dn =$dn;
79 /* Set acls
80 */
81 $ui = get_userinfo();
82 $acl = get_permissions ($this->dn, $ui->subtreeACL);
83 $acli = get_module_permission($acl, "FAIclass", $this->dn);
84 $this->acl=$acli;
86 /* Get FAIstate
87 */
88 if(isset($this->attrs['FAIstate'][0])){
89 $this->FAIstate = $this->attrs['FAIstate'][0];
90 }
91 }
93 if(isset($this->attrs['FAIpackage'])){
94 unset($this->attrs['FAIpackage']['count']);
95 foreach($this->attrs['FAIpackage'] as $pkg){
96 $this->usedPackages[$pkg] = $pkg;
97 }
98 ksort($this->usedPackages);
99 }else{
100 $this->usedPackages = array();
101 }
103 $ldap = $this->config->get_ldap_link();
104 foreach($this->usedPackages as $name){
105 $ldap->search("(&(objectClass=FAIdebconfInfo)(FAIpackage=".$name."))",array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection",""));
106 while($attr = $ldap->fetch()){
108 $tmp =array();
109 $tmp['Name'] = $attr['FAIvariable'][0];
110 $tmp['Type'] = $attr['FAIvariableType'][0];
112 if (isset($attr['FAIvariableContent'][0])){
113 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
114 $tmp['Value'] = $attr['FAIvariableContent'][0];
115 }else{
116 $content = array();
117 unset($attr['FAIvariableContent']['count']);
118 foreach($attr['FAIvariableContent'] as $attr){
119 $tmp['Value'][] = $attr;
120 }
121 }
122 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
123 }
124 }
125 }
127 if (isset($this->attrs['FAIdebianSection']['count'])){
128 unset($this->attrs['FAIdebianSection']['count']);
129 }
130 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
131 $this->FAIdebianSection = array();
132 foreach($this->attrs['FAIdebianSection'] as $sec){
133 $this->FAIdebianSection[$sec]=$sec;
134 }
135 }
137 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
138 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
139 }
140 $this->confDir = CONFIG_DIR."/fai/";
141 $this->FAIpackage = array();
144 $methods = array();
145 foreach($this->FAIinstallMethods as $method){
146 $methods[$method] = $method;
147 }
148 $this->FAIinstallMethods = $methods;
149 }
151 function execute()
152 {
153 /* Call parent execute */
155 plugin::execute();
157 /* Fill templating stuff */
158 $smarty= get_smarty();
159 $display= "";
161 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
163 /* Check if we exist already - no need to ask for revisions, etc. then */
164 if ($this->dn != "new"){
165 $this->newDialogShown= true;
166 }
168 if((!$this->is_account)&&(!$this->newDialogShown)){
170 if($this->dialog==NULL){
171 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
172 $this->is_dialog =true;
173 }
175 /* Assign posible changes, for mirror combinations */
176 $this->dialog->save_object();
178 /* Assign Repository settings*/
179 if(isset($_POST['SaveObjectNew'])){
180 $obj = $this->dialog->save();
182 $this->FAIdebianSection = $obj['FAIdebianSection'];
183 $this->FAIdebianRelease = $obj['FAIdebianRelease'];
185 unset($this->dialog);
186 $this->dialog = false;
187 $this->is_dialog=false;
188 $this->newDialogShown = true;
189 }
191 /* Draw dialog */
192 if($this->dialog){
193 $display=$this->dialog->execute();
194 return($display);
195 }
196 }
198 /* Assign variables */
199 foreach($this->attributes as $attrs){
200 $smarty->assign($attrs,$this->$attrs);
201 }
203 /* Generate package list */
204 $this->list=$this->genPkgs();
206 /* + was pressed to open the package dialog */
207 if(isset($_POST['Addpkg'])){
208 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
209 $this->is_dialog =true;
210 }
212 /* Delte selected package */
213 if(isset($_POST['Delpkg'])){
214 if($this->FAIstate != "freeze"){
215 foreach($_POST['usedPackages'] as $del){
216 if(isset($this->usedPackages[$del])){
217 unset($this->usedPackages[$del]);
218 }
219 }
220 }
221 }
223 /* Abort package selection dialog */
224 if(isset($_POST['CancelSubObject'])){
225 $this->dialog = false;
226 $this->is_dialog=false;
227 }
229 /* attach new packages */
230 if(isset($_POST['SaveSubObject'])) {
231 if($this->FAIstate != "freeze"){
232 $this->dialog->save_object();
233 if(count($this->dialog->check())){
234 foreach($this->dialog->check() as $msgs){
235 print_red($msgs);
236 }
237 }else{
238 $use = $this->dialog->save();
239 $this->usedPackages = $use;
240 $this->dialog = false;
241 $this->is_dialog=false;
242 ksort($this->usedPackages);
243 }
244 }else{
245 $this->dialog = false;
246 $this->is_dialog=false;
247 }
248 }
250 /* Configuration dialog open*/
251 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
252 $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
253 $pkg_config = array();
254 $pkg = $_POST['usedPackages'][0];
256 if(isset($this->ConfiguredPackages[$pkg])){
257 $pkg_config = $this->ConfiguredPackages[$pkg];
258 }
260 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
261 $this->dialog ->acl = $this->acl;
262 $this->is_dialog =true;
263 }
265 /* Save Configuration */
266 if(isset($_POST['SaveObjectConfig'])){
267 if($this->FAIstate != "freeze"){
268 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
269 }
270 $this->dialog = false;
271 $this->is_dialog=false;
272 }
274 /* cancel configuration */
275 if(isset($_POST['CancelObjectConfig'])){
276 $this->dialog = false;
277 $this->is_dialog=false;
278 }
280 /* Display dialog */
281 if($this->is_dialog){
282 return $this->dialog->execute();
283 }
285 /* Assign section to smarty */
286 $strsec = "";
287 foreach($this->FAIdebianSection as $sec){
288 $strsec .= $sec." ";
289 }
291 foreach($this->attributes as $attr){
292 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
293 }
295 $smarty->assign("OptionsACL","");
297 $smarty->assign("releases",$this->releases);
298 $smarty->assign("release" ,$this->FAIdebianRelease);
299 $smarty->assign("sections",$this->sections);
300 $smarty->assign("section" ,$strsec);
301 $smarty->assign("usedPackages",$this->printUsedPackages());
302 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
303 return($display);
304 }
306 /* Delete me, and all my subtrees
307 */
308 function remove_from_parent()
309 {
310 $ldap = $this->config->get_ldap_link();
311 $ldap->cd ($this->dn);
312 $ldap->rmdir_recursive($this->dn);
313 $this->handle_post_events("remove");
314 }
317 /* Save data to object
318 */
319 function save_object()
320 {
322 if($this->FAIstate == "freeze") return;
323 plugin::save_object();
326 foreach($this->attributes as $attrs){
327 if(isset($_POST[$attrs])){
328 $this->$attrs = $_POST[$attrs];
329 }
330 }
331 }
334 /* Check supplied data */
335 function check()
336 {
337 $message= array();
339 if(count($this->usedPackages)==0){
340 $message[]=_("Please select a least one Package.");
341 }
343 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
344 $message[]=_("Please choose a valid combination for your repository setup.");
345 }
347 return ($message);
348 }
350 function printUsedPackages(){
351 $a_ret=array();
352 if(is_array($this->usedPackages)) {
353 foreach($this->usedPackages as $usedName){
355 $config = 0;
357 foreach($this->ConfiguredPackages as $name => $value){
358 if($name == $usedName){
359 $config ++;
360 }
361 }
363 $c_str ="";
364 if($config){
365 $c_str = " - "._("package is configured");
366 }
368 if(isset($this->list[$usedName][1])){
369 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
370 }else{
371 $a_ret[$usedName] = $usedName.$c_str;
372 }
373 }
374 }
375 return($a_ret);
376 }
378 function genPkgs(){
379 /* Generate a list off available packages for this mirror, section and release
380 */
381 /* Only read this file if it wasn't read before */
382 if($this->buffer==NULL){
383 $this->buffer=array();
384 $a_ret = array();
385 foreach($this->FAIdebianSection as $sec){
386 $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
388 if(!is_file($strID)){
389 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
390 unset($this->buffer);
391 return(array());
392 }
393 $fp = fopen($strID,"r");
395 /* Parse every line and create an array */
396 while(!feof($fp)){
397 $str= fgets($fp,512);
398 $stra= split("\|",$str);
399 if(count($stra)==4){
400 $a_ret[$stra[0]] = $stra;
401 }
402 }
403 fclose($fp);
404 /* Save our Data, to avoid reading it again */
405 }
406 $this->buffer = $a_ret;
407 ksort($a_ret);
408 return($a_ret);
409 }else{
410 return $this->buffer;
411 }
412 }
415 /* Save to LDAP */
416 function save()
417 {
418 plugin::save();
420 $ldap = $this->config->get_ldap_link();
422 $this->attrs['FAIpackage'] = array();
423 foreach($this->usedPackages as $pkg => $obj){
424 $this->attrs['FAIpackage'][] = $pkg;
425 }
427 $this->attrs['FAIdebianSection'] = array();
428 foreach($this->FAIdebianSection as $sec){
429 $this->attrs['FAIdebianSection'][] = $sec;
430 }
432 // $this->attrs["FAIinstallMethod"]= "aptitude";
434 $ldap->cat($this->dn);
435 if($ldap->count()!=0){
436 /* Write FAIscript to ldap*/
437 $ldap->cd($this->dn);
438 $this->cleanup();
439 $ldap->modify ($this->attrs);
441 }else{
442 /* Write FAIscript to ldap*/
443 $ldap->cd($this->config->current['BASE']);
444 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
445 $ldap->cd($this->dn);
446 $ldap->add($this->attrs);
447 }
448 show_ldap_error($ldap->get_error());
450 /* Save Package configurations */
451 foreach($this->ConfiguredPackages as $pkgname => $attrs){
452 foreach($attrs as $name => $attr){
454 foreach($attr as $n=>$v){
455 if(empty($v)) $v = array();
456 }
458 /* Set attributes */
459 $pkgattrs['objectClass'] = "FAIdebconfInfo";
460 $pkgattrs['FAIpackage'] = $pkgname;
461 $pkgattrs['FAIvariable'] = $name;
462 $pkgattrs['FAIvariableType'] = $attr['Type'];
463 $pkgattrs['FAIvariableContent'] = $attr['Value'];
464 $pkgdn = "FAIvariable=".$name.",".$this->dn;
466 /* cehck if object already exists */
467 $ldap->cat($pkgdn);
468 if($ldap->count()!=0){
469 $ldap->cd($pkgdn);
470 $this->cleanup();
471 $ldap->modify ($pkgattrs);
473 }else{
474 $ldap->cd($this->config->current['BASE']);
475 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
476 $ldap->cd($pkgdn);
477 $ldap->add($pkgattrs);
478 }
479 show_ldap_error($ldap->get_error());
480 }
481 }
486 }
487 }
489 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
490 ?>