79cd57a0b87ca3d8a235fe422bf1bfa469758aba
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 if($this->buffer === NULL || !count($this->buffer)){
425 $this->buffer = array();
426 $q = new gosaSupportDaemon();
427 $ret = $q->FAI_get_packages($this->FAIdebianRelease);
428 foreach($ret as $attr){
429 $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
430 }
431 }
432 return $this->buffer;
434 # /* Generate a list off available packages for this mirror, section and release
435 # */
436 # /* Only read this file if it wasn't read before */
437 # $this->buffer = NULL;
438 # if($this->buffer === NULL){
439 # $this->buffer=array();
440 # $a_ret = array();
441 # foreach($this->FAIdebianSection as $sec){
442 # $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
443 #
444 # if(!is_file($strID)){
445 # msg_dialog::display(_("Error"), sprintf(_("Package file '%s' does not exist!"), $strID), ERROR_DIALOG);
446 # unset($this->buffer);
447 # return(array());
448 # }
449 # $fp = fopen($strID,"r");
450 #
451 # /* Parse every line and create an array */
452 # while(!feof($fp)){
453 # $str= fgets($fp,512);
454 # $stra= split("\|",$str);
455 # if(count($stra)==4){
456 # $a_ret[$stra[0]] = $stra;
457 # break;
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 # print_a($this->buffer);
466 # return($a_ret);
467 # }else{
468 # print_a($this->buffer);
469 # return $this->buffer;
470 # }
471 }
474 /* Save to LDAP */
475 function save()
476 {
478 /* Assemble release name */
479 if($this->FAIdebianRelease == "ClearFromCopyPaste"){
480 $faifilter = session::get('faifilter');
481 $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
482 $tmp= preg_replace('/ou=/', '', $tmp);
483 $rev= array_reverse(split(',', $tmp));
484 $this->FAIdebianRelease= "";
485 foreach ($rev as $part){
486 $this->FAIdebianRelease.= "/$part";
487 }
488 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
489 }
491 plugin::save();
493 $ldap = $this->config->get_ldap_link();
495 $this->attrs['FAIpackage'] = array();
496 foreach($this->usedPackages as $pkg => $obj){
497 $this->attrs['FAIpackage'][] = $pkg;
498 }
500 $this->attrs['FAIdebianSection'] = array();
501 foreach($this->FAIdebianSection as $sec){
502 $this->attrs['FAIdebianSection'][] = $sec;
503 }
505 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
506 if (!$ldap->success()){
507 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
508 }
510 if($this->initially_was_account){
511 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
512 }else{
513 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
514 }
516 $ldap->cd($this->dn);
518 /* Save Package configurations */
519 foreach($this->ConfiguredPackages as $pkgname => $attrs){
520 foreach($attrs as $name => $attr){
522 $pkgattrs = array();
524 foreach($attr as $n=>$v){
525 if(empty($v)) $v = array();
526 }
528 /* Set attributes */
529 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
531 $pkgattrs['FAIpackage'] = $pkgname;
532 $pkgattrs['FAIvariable'] = $name;
533 $pkgattrs['FAIvariableType'] = $attr['Type'];
534 $pkgattrs['FAIvariableContent'] = $attr['Value'];
535 $pkgdn = "FAIvariable=".$name.",".$this->dn;
537 /* Tag object */
538 $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
540 if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
541 FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
542 }
543 }
544 }
545 }
547 /* Return plugin informations for acl handling */
548 static function plInfo()
549 {
550 return (array(
551 "plShortName" => _("Package"),
552 "plDescription" => _("FAI Package list"),
553 "plSelfModify" => FALSE,
554 "plDepends" => array(),
555 "plPriority" => 28,
556 "plSection" => array("administration"),
557 "plCategory" => array("fai"),
558 "plProvidedAcls" => array(
559 "cn" => _("Name"),
560 "description" => _("Description"),
561 "FAIpackage" => _("Packages"),
562 "FAIdebianSection" => _("Section")." ("._("Readonly").")",
563 "FAIinstallMethod" => _("Install Method"),
564 "FAIdebconfInfo" => _("Package configuration"),
565 "FAIdebianRelease" => _("Release")." ("._("Readonly").")")
566 ));
567 }
572 function PrepareForCopyPaste($source)
573 {
574 plugin::PrepareForCopyPaste($source);
576 if(isset($source['FAIstate'][0])){
577 $this->FAIstate = $source['FAIstate'][0];
578 }
580 $this->FAIdebianRelease = "ClearFromCopyPaste";
582 if(isset($source['FAIpackage'])){
583 unset($source['FAIpackage']['count']);
584 foreach($source['FAIpackage'] as $pkg){
585 $this->usedPackages[$pkg] = $pkg;
586 }
587 ksort($this->usedPackages);
588 }else{
589 $this->usedPackages = array();
590 }
592 if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
593 $this->FAIdebianSection = array();
594 for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
595 $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
596 }
597 }
599 /* Create one filter with all package names,
600 instead of calling $ldap->search for every single package
601 */
602 $PackageFilter = "";
603 foreach($this->usedPackages as $name){
604 $PackageFilter .= "(FAIpackage=".$name.")";
605 }
606 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
608 /* Search for configuration objects */
609 $ldap = $this->config->get_ldap_link();
610 $ldap->cd($source['dn']);
611 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
613 /* Walk through configurations and append them to our list of ConfiguredPackages */
614 while($attr = $ldap->fetch()){
616 /* Skip objects, that are tagged as removed */
617 if(isset($object['FAIstate'][0])){
618 if(preg_match("/removed$/",$attr['FAIstate'][0])){
619 continue;
620 }
621 }
623 $tmp =array();
624 $tmp['Name'] = $attr['FAIvariable'][0];
625 $tmp['Type'] = $attr['FAIvariableType'][0];
627 if (isset($attr['FAIvariableContent'][0])){
628 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
629 $tmp['Value'] = $attr['FAIvariableContent'][0];
630 }else{
631 $content = array();
632 unset($attr['FAIvariableContent']['count']);
633 foreach($attr['FAIvariableContent'] as $attr){
634 $tmp['Value'][] = $attr;
635 }
636 }
637 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
638 }
639 }
640 }
643 function getServerInfos()
644 {
645 $ret = array();
646 $ldap = $this->config->get_ldap_link();
647 $ldap->cd($this->config->current['BASE']);
648 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
649 while($attrs = $ldap->fetch()){
650 if(isset($attrs['FAIrepository'])){
651 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
652 $obj = $attrs['FAIrepository'][$i];
653 $tmp = split("\|",$obj);
654 if(count($tmp)==4){
655 foreach(split(",",$tmp[3]) as $sec){
656 if(!empty($sec)){
657 $ret[$tmp[2]][] = $sec;
658 }
659 }
660 }
661 }
662 }
663 }
664 return($ret);
665 }
668 }
670 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
671 ?>