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 /* 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 }
139 if (isset($this->attrs['FAIdebianSection']['count'])){
140 unset($this->attrs['FAIdebianSection']['count']);
141 }
142 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
143 $this->FAIdebianSection = array();
144 foreach($this->attrs['FAIdebianSection'] as $sec){
145 $this->FAIdebianSection[$sec]=$sec;
146 }
147 }
149 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
150 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
151 }
152 $this->confDir = CONFIG_DIR."/fai/";
153 $this->FAIpackage = array();
156 $methods = array();
157 foreach($this->FAIinstallMethods as $method){
158 $methods[$method] = $method;
159 }
160 $this->FAIinstallMethods = $methods;
161 /* Check if we exist already - no need to ask for revisions, etc. then */
162 if ($this->dn != "new"){
163 $this->newDialogShown= true;
164 }
166 }
168 function execute()
169 {
170 /* Call parent execute */
172 plugin::execute();
174 /* Fill templating stuff */
175 $smarty= get_smarty();
176 $display= "";
178 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
180 if((!$this->is_account)&&(!$this->newDialogShown)){
182 if($this->dialog==NULL){
183 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
184 $this->is_dialog =true;
185 }
187 /* Assign posible changes, for mirror combinations */
188 $this->dialog->save_object();
190 /* Assign Repository settings*/
191 if(isset($_POST['SaveObjectNew'])){
192 $obj = $this->dialog->save();
194 $this->FAIdebianSection = $obj['FAIdebianSection'];
195 $this->FAIdebianRelease = $obj['FAIdebianRelease'];
197 unset($this->dialog);
198 $this->dialog = false;
199 $this->is_dialog = false;
200 $this->newDialogShown = true;
201 $this->is_account = true;
202 }
204 /* Draw dialog */
205 if($this->dialog){
206 $display=$this->dialog->execute();
207 return($display);
208 }
209 }
211 /* Assign variables */
212 foreach($this->attributes as $attrs){
213 $smarty->assign($attrs,$this->$attrs);
214 }
216 /* Generate package list */
217 $this->list=$this->genPkgs();
219 /* + was pressed to open the package dialog */
220 if(isset($_POST['Addpkg'])){
221 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
222 $this->is_dialog =true;
223 }
225 /* Delte selected package */
226 if(isset($_POST['Delpkg'])){
227 if($this->FAIstate != "freeze"){
228 foreach($_POST['usedPackages'] as $del){
229 if(isset($this->usedPackages[$del])){
230 unset($this->usedPackages[$del]);
231 }
232 }
233 }
234 }
236 /* Abort package selection dialog */
237 if(isset($_POST['CancelSubObject'])){
238 $this->dialog = false;
239 $this->is_dialog=false;
240 }
242 /* attach new packages */
243 if(isset($_POST['SaveSubObject'])) {
244 if($this->FAIstate != "freeze"){
245 $this->dialog->save_object();
246 if(count($this->dialog->check())){
247 foreach($this->dialog->check() as $msgs){
248 print_red($msgs);
249 }
250 }else{
251 $use = $this->dialog->save();
252 $this->usedPackages = $use;
253 $this->dialog = false;
254 $this->is_dialog=false;
255 ksort($this->usedPackages);
256 }
257 }else{
258 $this->dialog = false;
259 $this->is_dialog=false;
260 }
261 }
263 /* Configuration dialog open*/
264 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
265 $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
266 $pkg_config = array();
267 $pkg = $_POST['usedPackages'][0];
269 if(isset($this->ConfiguredPackages[$pkg])){
270 $pkg_config = $this->ConfiguredPackages[$pkg];
271 }
273 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
274 $this->is_dialog =true;
275 }
277 /* Configuration dialog open*/
278 if($this->FAIstate != "freeze"){
279 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
280 foreach($_POST['usedPackages'] as $pkg){
281 if (isset($this->usedPackages[$pkg])){
282 unset($this->usedPackages[$pkg]);
283 if (preg_match('/^-/', $pkg)){
284 $pkg= preg_replace('/^-/', '', $pkg);
285 } else {
286 $pkg= preg_replace('/^/', '-', $pkg);
287 }
288 $this->usedPackages[$pkg]= $pkg;
289 }
290 }
291 }
292 }
294 /* Save Configuration */
295 if(isset($_POST['SaveObjectConfig'])){
296 if($this->FAIstate != "freeze"){
297 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
298 }
299 $this->dialog = false;
300 $this->is_dialog=false;
301 }
303 /* cancel configuration */
304 if(isset($_POST['CancelObjectConfig'])){
305 $this->dialog = false;
306 $this->is_dialog=false;
307 }
309 /* Display dialog */
310 if($this->is_dialog){
311 return $this->dialog->execute();
312 }
314 /* Assign section to smarty */
315 $strsec = "";
316 foreach($this->FAIdebianSection as $sec){
317 $strsec .= $sec." ";
318 }
320 $tmp = $this->plInfo();
321 foreach($tmp['plProvidedAcls'] as $name => $translated){
322 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
323 }
325 $smarty->assign("releases",$this->releases);
326 $smarty->assign("release" ,$this->FAIdebianRelease);
327 $smarty->assign("sections",$this->sections);
328 $smarty->assign("section" ,$strsec);
329 $smarty->assign("usedPackages",$this->printUsedPackages());
330 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
331 return($display);
332 }
334 /* Delete me, and all my subtrees
335 */
336 function remove_from_parent()
337 {
338 $ldap = $this->config->get_ldap_link();
339 $ldap->cd ($this->dn);
341 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
342 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
343 if($_SESSION['faifilter']['branch'] == "main"){
344 $use_dn = $this->dn;
345 }
347 prepare_to_save_FAI_object($use_dn,array(),true);
349 foreach($this->ConfiguredPackages as $pkgname => $attrs){
350 foreach($attrs as $name => $attr){
351 $pkgdn = "FAIvariable=".$name.",".$this->dn;
352 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
353 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
354 if($_SESSION['faifilter']['branch'] == "main"){
355 $use_dn = $obj['dn'];
356 }
357 prepare_to_save_FAI_object($use_dn,array(),true);
358 }
359 }
360 $this->handle_post_events("remove");
361 }
364 /* Save data to object
365 */
366 function save_object()
367 {
369 if($this->FAIstate == "freeze") return;
370 plugin::save_object();
373 foreach($this->attributes as $attrs){
374 if(isset($_POST[$attrs])){
375 $this->$attrs = $_POST[$attrs];
376 }
377 }
378 }
381 /* Check supplied data */
382 function check()
383 {
384 /* Call common method to give check the hook */
385 $message= plugin::check();
387 if(count($this->usedPackages)==0){
388 $message[]=_("Please select a least one Package.");
389 }
391 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
392 $message[]=_("Please choose a valid combination for your repository setup.");
393 }
395 return ($message);
396 }
398 function printUsedPackages(){
399 $a_ret=array();
400 if(is_array($this->usedPackages)) {
401 foreach($this->usedPackages as $usedName){
403 $config = 0;
405 foreach($this->ConfiguredPackages as $name => $value){
406 if($name == $usedName){
407 $config ++;
408 }
409 }
411 $c_str ="";
412 if($config){
413 $c_str = " - "._("package is configured");
414 }
416 /* Adapt used name if we're marked for removal */
417 $dsc= "";
418 if (preg_match('/^-/', $usedName)){
419 $dsc= " - "._("Package marked for removal");
420 $usedName2= preg_replace('/^-/', '! ', $usedName);
421 }else{
422 $usedName2= $usedName;
423 }
425 if(isset($this->list[$usedName][1])){
426 $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
427 }else{
428 $a_ret[$usedName] = $usedName2.$c_str.$dsc;
429 }
430 }
431 }
432 return($a_ret);
433 }
435 function genPkgs(){
436 /* Generate a list off available packages for this mirror, section and release
437 */
438 /* Only read this file if it wasn't read before */
439 if($this->buffer==NULL){
440 $this->buffer=array();
441 $a_ret = array();
442 foreach($this->FAIdebianSection as $sec){
443 $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
445 if(!is_file($strID)){
446 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
447 unset($this->buffer);
448 return(array());
449 }
450 $fp = fopen($strID,"r");
452 /* Parse every line and create an array */
453 while(!feof($fp)){
454 $str= fgets($fp,512);
455 $stra= split("\|",$str);
456 if(count($stra)==4){
457 $a_ret[$stra[0]] = $stra;
458 }
459 }
460 fclose($fp);
461 /* Save our Data, to avoid reading it again */
462 }
463 $this->buffer = $a_ret;
464 ksort($a_ret);
465 return($a_ret);
466 }else{
467 return $this->buffer;
468 }
469 }
472 /* Save to LDAP */
473 function save()
474 {
475 plugin::save();
477 $ldap = $this->config->get_ldap_link();
479 $this->attrs['FAIpackage'] = array();
480 foreach($this->usedPackages as $pkg => $obj){
481 $this->attrs['FAIpackage'][] = $pkg;
482 }
484 $this->attrs['FAIdebianSection'] = array();
485 foreach($this->FAIdebianSection as $sec){
486 $this->attrs['FAIdebianSection'][] = $sec;
487 }
489 prepare_to_save_FAI_object($this->dn,$this->attrs);
490 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
492 /* Do object tagging */
493 $this->handle_object_tagging();
494 $ldap->cd($this->dn);
496 /* Save Package configurations */
497 foreach($this->ConfiguredPackages as $pkgname => $attrs){
498 foreach($attrs as $name => $attr){
500 $pkgattrs = array();
502 foreach($attr as $n=>$v){
503 if(empty($v)) $v = array();
504 }
506 /* Set attributes */
507 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
509 $pkgattrs['FAIpackage'] = $pkgname;
510 $pkgattrs['FAIvariable'] = $name;
511 $pkgattrs['FAIvariableType'] = $attr['Type'];
512 $pkgattrs['FAIvariableContent'] = $attr['Value'];
513 $pkgdn = "FAIvariable=".$name.",".$this->dn;
515 /* cehck if object already exists */
516 $ldap->cat($pkgdn,array("objectClass"));
518 /* Workaround for missing "gosaAdministrativeUnitTag" */
519 $attrs = $ldap->fetch();
520 if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
521 $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
522 }
524 prepare_to_save_FAI_object($pkgdn,$pkgattrs);
526 /* Handle tagging */
527 $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
528 }
529 }
530 }
532 /* Return plugin informations for acl handling */
533 function plInfo()
534 {
535 return (array(
536 "plShortName" => _("Package"),
537 "plDescription" => _("FAI Package list"),
538 "plSelfModify" => FALSE,
539 "plDepends" => array(),
540 "plPriority" => 30,
541 "plSection" => array("administration"),
542 "plCategory" => array("fai"),
543 "plProvidedAcls" => array(
544 "cn" => _("Name"),
545 "description" => _("Description"),
546 "FAIpackage" => _("Packages"),
547 "FAIdebianSection" => _("Section"),
548 "FAIinstallMethod" => _("Install Method"),
549 "FAIdebconfInfo" => _("Package configuration"),
550 "FAIdebianRelease" => _("Release"))
551 ));
552 }
553 }
555 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
556 ?>