b299e7c40a35d362510361945e2391315d747440
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;
54 var $FAIpackage ;
56 var $FAIinstallMethods = array( "install", "ninstall", "remove",
57 "dselect-upgrade", "taskinst", "taskrm",
58 "hold", "clean", "aptitude", "aptitude-r",
59 "pending", "dpkgc" );
62 function faiPackage (&$config, $dn= NULL)
63 {
64 /* Load Attributes */
65 plugin::plugin ($config, $dn);
67 /* If "dn==new" we try to create a new entry
68 * Else we must read all objects from ldap which belong to this entry.
69 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
70 */
71 if($dn != "new"){
72 $this->dn =$dn;
74 /* Get FAIstate */
75 if(isset($this->attrs['FAIstate'][0])){
76 $this->FAIstate = $this->attrs['FAIstate'][0];
77 }
79 /* Check if there are already some packages in this list
80 */
81 $this->usedPackages = array();
82 if(isset($this->attrs['FAIpackage'])){
83 unset($this->attrs['FAIpackage']['count']);
84 foreach($this->attrs['FAIpackage'] as $pkg){
85 $this->usedPackages[$pkg] = $pkg;
86 }
87 ksort($this->usedPackages);
88 }
90 /* Fetch all package configurations from ldap
91 */
92 $PackageFilter = "";
93 foreach($this->usedPackages as $name){
94 $PackageFilter .= "(FAIpackage=".$name.")";
95 }
96 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
98 /* Search for configuration objects */
99 $ldap = $this->config->get_ldap_link();
100 $ldap->cd($this->dn);
101 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
102 "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
104 /* Walk through configurations and append them to our list of ConfiguredPackages
105 */
106 while($attr = $ldap->fetch()){
108 /* Skip objects, that are tagged as removed */
109 if(isset($object['FAIstate'][0])){
110 if(preg_match("/removed$/",$attr['FAIstate'][0])){
111 continue;
112 }
113 }
115 $tmp =array();
116 $tmp['Name'] = $attr['FAIvariable'][0];
117 $tmp['Type'] = $attr['FAIvariableType'][0];
119 if (isset($attr['FAIvariableContent'][0])){
120 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
121 $tmp['Value'] = $attr['FAIvariableContent'][0];
122 }else{
123 $content = array();
124 unset($attr['FAIvariableContent']['count']);
125 foreach($attr['FAIvariableContent'] as $attr){
126 $tmp['Value'][] = $attr;
127 }
128 }
129 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
130 }
131 }
133 if (isset($this->attrs['FAIdebianSection']['count'])){
134 unset($this->attrs['FAIdebianSection']['count']);
135 }
136 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
137 $this->FAIdebianSection = array();
138 foreach($this->attrs['FAIdebianSection'] as $sec){
139 $this->FAIdebianSection[$sec]=$sec;
140 }
141 }
143 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
144 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
145 }
146 $this->confDir = CONFIG_DIR."/fai/";
147 $this->FAIpackage = array();
149 } // ENDE dn != new
151 $methods = array();
152 foreach($this->FAIinstallMethods as $method){
153 $methods[$method] = $method;
154 }
155 $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 }
161 $this->is_new = FALSE;
162 if($this->dn == "new"){
163 $this->is_new =TRUE;
164 }
165 }
168 function execute()
169 {
170 /* Call parent execute */
171 plugin::execute();
173 if($this->is_account && !$this->view_logged){
174 $this->view_logged = TRUE;
175 new log("view","fai/".get_class($this),$this->dn);
176 }
178 /* Fill templating stuff */
179 $smarty= get_smarty();
180 $display= "";
182 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
184 if(!$this->is_account){
186 /* Assemble release name */
187 $faifilter = session::get('faifilter');
188 $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
189 $tmp= preg_replace('/ou=/', '', $tmp);
190 $rev= array_reverse(split(',', $tmp));
191 $this->FAIdebianRelease= "";
192 foreach ($rev as $part){
193 $this->FAIdebianRelease.= "/$part";
194 }
195 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
197 /* Assemble sections */
198 $repos= $this->getServerInfos();
199 if(isset($repos[$this->FAIdebianRelease])){
200 $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
201 $this->FAIdebianSection= array_unique($this->FAIdebianSection);
202 }
204 /* Assign Repository settings*/
205 $this->is_account = true;
206 }
208 /* Assign variables */
209 foreach($this->attributes as $attrs){
210 $smarty->assign($attrs,$this->$attrs);
211 }
213 /* Generate package list */
214 $this->list= $this->genPkgs();
216 /* + was pressed to open the package dialog */
217 if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
218 $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
219 $this->is_dialog =true;
220 }
222 /* Delete selected package */
223 if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
224 if(!preg_match("/^freeze/", $this->FAIstate)){
225 foreach($_POST['usedPackages'] as $del){
226 if(isset($this->usedPackages[$del])){
227 unset($this->usedPackages[$del]);
228 }
229 }
230 }
231 }
233 /* Abort package selection dialog */
234 if(isset($_POST['CancelSubObject'])){
235 $this->dialog = false;
236 $this->is_dialog=false;
237 }
239 /* attach new packages */
240 if(isset($_POST['SaveSubObject'])) {
241 if(!preg_match("/^freeze/", $this->FAIstate)){
242 $this->dialog->save_object();
243 if(count($this->dialog->check())){
244 foreach($this->dialog->check() as $msgs){
245 msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
246 }
247 }else{
248 $use = $this->dialog->save();
249 $this->usedPackages = $use;
250 $this->dialog = false;
251 $this->is_dialog=false;
252 ksort($this->usedPackages);
253 }
254 }else{
255 $this->dialog = false;
256 $this->is_dialog=false;
257 }
258 }
260 /* Configuration dialog open*/
261 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
262 $pkg_config = array();
263 $pkg = $_POST['usedPackages'][0];
264 if(isset($this->ConfiguredPackages[$pkg])){
265 $pkg_config = $this->ConfiguredPackages[$pkg];
266 }
267 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
268 $this->is_dialog =true;
269 }
271 /* Configuration dialog open*/
272 if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
273 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
274 foreach($_POST['usedPackages'] as $pkg){
275 if (isset($this->usedPackages[$pkg])){
276 unset($this->usedPackages[$pkg]);
277 if (preg_match('/^-/', $pkg)){
278 $pkg= preg_replace('/^-/', '', $pkg);
279 } else {
280 $pkg= preg_replace('/^/', '-', $pkg);
281 }
282 $this->usedPackages[$pkg]= $pkg;
283 }
284 }
285 }
286 }
288 /* Save Configuration */
289 if(isset($_POST['SaveObjectConfig'])){
290 if(!preg_match("/^freeze/", $this->FAIstate)){
291 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
292 }
293 $this->dialog = false;
294 $this->is_dialog=false;
295 }
297 /* cancel configuration */
298 if(isset($_POST['CancelObjectConfig'])){
299 $this->dialog = false;
300 $this->is_dialog=false;
301 }
303 /* Display dialog */
304 if($this->is_dialog){
305 $this->dialog->save_object();
306 return $this->dialog->execute();
307 }
309 /* Assign section to smarty */
310 $strsec = "";
311 foreach($this->FAIdebianSection as $sec){
312 $strsec .= $sec." ";
313 }
315 $tmp = $this->plInfo();
316 foreach($tmp['plProvidedAcls'] as $name => $translated){
317 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
318 }
320 $smarty->assign("releases",$this->releases);
321 $smarty->assign("release" ,$this->FAIdebianRelease);
322 $smarty->assign("sections",$this->sections);
323 $smarty->assign("section" ,$strsec);
324 $smarty->assign("usedPackages",$this->printUsedPackages());
325 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
326 return($display);
327 }
329 /* Delete me, and all my subtrees
330 */
331 function remove_from_parent()
332 {
333 $ldap = $this->config->get_ldap_link();
334 $ldap->cd ($this->dn);
336 $faifilter = session::get('faifilter');
337 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
338 if($faifilter['branch'] == "main"){
339 $use_dn = $this->dn;
340 }
342 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
344 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
346 foreach($this->ConfiguredPackages as $pkgname => $attrs){
347 foreach($attrs as $name => $attr){
348 $pkgdn = "FAIvariable=".$name.",".$this->dn;
349 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
350 if($faifilter['branch'] == "main"){
351 $use_dn = $obj['dn'];
352 }
353 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
354 }
355 }
356 $this->handle_post_events("remove");
357 }
360 /* Save data to object
361 */
362 function save_object()
363 {
364 if(preg_match("/^freeze/", $this->FAIstate)) return;
365 plugin::save_object();
366 }
369 /* Check supplied data */
370 function check()
371 {
372 /* Call common method to give check the hook */
373 $message= plugin::check();
375 if(count($this->usedPackages)==0){
376 $message[]= _("Please select a least one package!");
377 }
379 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
380 $message[]=_("Please choose a valid release/section combination for your repository setup!");
381 }
383 /* Ensure that we do not overwrite an allready existing entry
384 */
385 if($this->is_new){
386 $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
387 $faifilter = session::get('faifilter');
388 if($faifilter['branch']!="main"){
389 $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
390 }
392 $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
393 if(isset($res[$this->cn])){
394 $message[] = msgPool::duplicated(_("Name"));
395 }
396 }
397 return ($message);
398 }
400 function printUsedPackages(){
401 $a_ret=array();
402 if(is_array($this->usedPackages)) {
403 foreach($this->usedPackages as $usedName){
405 $config = 0;
407 foreach($this->ConfiguredPackages as $name => $value){
408 if($name == $usedName){
409 $config ++;
410 }
411 }
413 $c_str ="";
414 if($config){
415 $c_str = " - "._("package is configured");
416 }
418 /* Adapt used name if we're marked for removal */
419 $dsc= "";
420 if (preg_match('/^-/', $usedName)){
421 $dsc= " - "._("Package marked for removal");
422 // Generally a bad idea here, because the toggel triggers on -, not on !
423 //$usedName= preg_replace('/^-/', '! ', $usedName);
424 }else{
425 $usedName2= $usedName;
426 }
428 if(isset($this->list[$usedName][1])){
429 $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
430 }else{
431 $a_ret[$usedName] = $usedName2.$c_str.$dsc;
432 }
433 }
434 }
435 return($a_ret);
436 }
439 function genPkgs()
440 {
441 $start = microtime(1);
442 if($this->buffer === NULL || !count($this->buffer)){
443 $this->buffer = array();
444 $q = new gosaSupportDaemon();
445 $attrs = array("distribution", "package","version", "section", "description", "timestamp");
446 $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs);
447 if($q->is_error()){
448 msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
449 }else{
450 foreach($ret as $attr){
451 $this->buffer[$attr['PACKAGE']] = array($attr['PACKAGE'],$attr['VERSION'],$attr['SECTION'],$attr['DESCRIPTION']);
452 }
453 }
454 }
456 # printf("%0.6f",(microtime(1) - $start));
457 return $this->buffer;
458 }
461 /* Save to LDAP */
462 function save()
463 {
465 /* Assemble release name */
466 if($this->FAIdebianRelease == "ClearFromCopyPaste"){
467 $faifilter = session::get('faifilter');
468 $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
469 $tmp= preg_replace('/ou=/', '', $tmp);
470 $rev= array_reverse(split(',', $tmp));
471 $this->FAIdebianRelease= "";
472 foreach ($rev as $part){
473 $this->FAIdebianRelease.= "/$part";
474 }
475 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
476 }
478 plugin::save();
480 $ldap = $this->config->get_ldap_link();
482 $this->attrs['FAIpackage'] = array();
483 foreach($this->usedPackages as $pkg => $obj){
484 $this->attrs['FAIpackage'][] = $pkg;
485 }
487 $this->attrs['FAIdebianSection'] = array();
488 foreach($this->FAIdebianSection as $sec){
489 $this->attrs['FAIdebianSection'][] = $sec;
490 }
492 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
494 if($this->initially_was_account){
495 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
496 }else{
497 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
498 }
500 $ldap->cd($this->dn);
502 /* Save Package configurations */
503 foreach($this->ConfiguredPackages as $pkgname => $attrs){
504 foreach($attrs as $name => $attr){
506 $pkgattrs = array();
508 foreach($attr as $n=>$v){
509 if(empty($v)) $v = array();
510 }
512 /* Set attributes */
513 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
515 $pkgattrs['FAIpackage'] = $pkgname;
516 $pkgattrs['FAIvariable'] = $name;
517 $pkgattrs['FAIvariableType'] = $attr['Type'];
518 $pkgattrs['FAIvariableContent'] = $attr['Value'];
519 $pkgdn = "FAIvariable=".$name.",".$this->dn;
521 /* Tag object */
522 $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
524 if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
525 FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
526 }
527 }
528 }
529 }
531 /* Return plugin informations for acl handling */
532 static function plInfo()
533 {
534 return (array(
535 "plShortName" => _("Package"),
536 "plDescription" => _("FAI Package list"),
537 "plSelfModify" => FALSE,
538 "plDepends" => array(),
539 "plPriority" => 28,
540 "plSection" => array("administration"),
541 "plCategory" => array("fai"),
542 "plProvidedAcls" => array(
543 "cn" => _("Name"),
544 "description" => _("Description"),
545 "FAIpackage" => _("Packages"),
546 "FAIdebianSection" => _("Section")." ("._("Readonly").")",
547 "FAIinstallMethod" => _("Install Method"),
548 "FAIdebconfInfo" => _("Package configuration"),
549 "FAIdebianRelease" => _("Release")." ("._("Readonly").")")
550 ));
551 }
556 function PrepareForCopyPaste($source)
557 {
558 plugin::PrepareForCopyPaste($source);
560 if(isset($source['FAIstate'][0])){
561 $this->FAIstate = $source['FAIstate'][0];
562 }
564 $this->FAIdebianRelease = "ClearFromCopyPaste";
566 if(isset($source['FAIpackage'])){
567 unset($source['FAIpackage']['count']);
568 foreach($source['FAIpackage'] as $pkg){
569 $this->usedPackages[$pkg] = $pkg;
570 }
571 ksort($this->usedPackages);
572 }else{
573 $this->usedPackages = array();
574 }
576 if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
577 $this->FAIdebianSection = array();
578 for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
579 $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
580 }
581 }
583 /* Create one filter with all package names,
584 instead of calling $ldap->search for every single package
585 */
586 $PackageFilter = "";
587 foreach($this->usedPackages as $name){
588 $PackageFilter .= "(FAIpackage=".$name.")";
589 }
590 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
592 /* Search for configuration objects */
593 $ldap = $this->config->get_ldap_link();
594 $ldap->cd($source['dn']);
595 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
597 /* Walk through configurations and append them to our list of ConfiguredPackages */
598 while($attr = $ldap->fetch()){
600 /* Skip objects, that are tagged as removed */
601 if(isset($object['FAIstate'][0])){
602 if(preg_match("/removed$/",$attr['FAIstate'][0])){
603 continue;
604 }
605 }
607 $tmp =array();
608 $tmp['Name'] = $attr['FAIvariable'][0];
609 $tmp['Type'] = $attr['FAIvariableType'][0];
611 if (isset($attr['FAIvariableContent'][0])){
612 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
613 $tmp['Value'] = $attr['FAIvariableContent'][0];
614 }else{
615 $content = array();
616 unset($attr['FAIvariableContent']['count']);
617 foreach($attr['FAIvariableContent'] as $attr){
618 $tmp['Value'][] = $attr;
619 }
620 }
621 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
622 }
623 }
624 }
627 function getServerInfos()
628 {
629 $ret = array();
630 $ldap = $this->config->get_ldap_link();
631 $ldap->cd($this->config->current['BASE']);
632 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
633 while($attrs = $ldap->fetch()){
634 if(isset($attrs['FAIrepository'])){
635 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
636 $obj = $attrs['FAIrepository'][$i];
637 $tmp = split("\|",$obj);
638 if(count($tmp)==4){
639 foreach(split(",",$tmp[3]) as $sec){
640 if(!empty($sec)){
641 $ret[$tmp[2]][] = $sec;
642 }
643 }
644 }
645 }
646 }
647 }
648 return($ret);
649 }
652 /*! \brief Used for copy & paste.
653 Returns a HTML input mask, which allows to change the cn of this entry.
654 @param Array Array containing current status && a HTML template.
655 */
656 function getCopyDialog()
657 {
658 $vars = array("cn");
659 $smarty = get_smarty();
660 $smarty->assign("cn", htmlentities($this->cn));
661 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
662 $ret = array();
663 $ret['string'] = $str;
664 $ret['status'] = "";
665 return($ret);
666 }
669 /*! \brief Used for copy & paste.
670 Some entries must be renamed to avaoid duplicate entries.
671 */
672 function saveCopyDialog()
673 {
674 if(isset($_POST['cn'])){
675 $this->cn = get_post('cn');
676 }
677 }
678 }
680 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
681 ?>