3fb126de055d6228a54852e1ddab868af58c8593
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 }
166 /* Generate package list */
167 $this->list= $this->genPkgs(TRUE);
168 }
171 function execute()
172 {
173 /* Call parent execute */
174 plugin::execute();
176 if($this->is_account && !$this->view_logged){
177 $this->view_logged = TRUE;
178 new log("view","fai/".get_class($this),$this->dn);
179 }
181 /* Fill templating stuff */
182 $smarty= get_smarty();
183 $display= "";
185 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
187 if(!$this->is_account){
189 /* Assemble release name */
190 $faifilter = session::get('faifilter');
191 $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $faifilter['branch']);
192 $tmp= preg_replace('/ou=/', '', $tmp);
193 $rev= array_reverse(split(',', $tmp));
194 $this->FAIdebianRelease= "";
195 foreach ($rev as $part){
196 $this->FAIdebianRelease.= "/$part";
197 }
198 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
200 /* Assemble sections */
201 $repos= $this->getServerInfos();
202 if(isset($repos[$this->FAIdebianRelease])){
203 $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
204 $this->FAIdebianSection= array_unique($this->FAIdebianSection);
205 }
207 /* Assign Repository settings*/
208 $this->is_account = true;
209 }
211 /* Assign variables */
212 foreach($this->attributes as $attrs){
213 $smarty->assign($attrs,$this->$attrs);
214 }
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 }
231 /* Generate package list */
232 $this->list= $this->genPkgs(TRUE);
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(!preg_match("/^freeze/", $this->FAIstate)){
245 $this->dialog->save_object();
246 if(count($this->dialog->check())){
247 foreach($this->dialog->check() as $msgs){
248 msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
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);
257 /* Generate package list */
258 $this->list= $this->genPkgs(TRUE);
259 }
260 }else{
261 $this->dialog = false;
262 $this->is_dialog=false;
263 }
264 }
266 /* Configuration dialog open*/
267 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
268 $pkg_config = array();
269 $pkg = $_POST['usedPackages'][0];
270 if(isset($this->ConfiguredPackages[$pkg])){
271 $pkg_config = $this->ConfiguredPackages[$pkg];
272 }
273 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
274 $this->is_dialog =true;
275 }
277 /* Configuration dialog open*/
278 if(preg_match('/^freeze/', $this->FAIstate) && $this->acl_is_writeable("FAIpackage")){
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;
290 }
291 }
293 /* Generate package list */
294 $this->list= $this->genPkgs(TRUE);
295 }
296 }
298 /* Save Configuration */
299 if(isset($_POST['SaveObjectConfig'])){
300 if(!preg_match("/^freeze/", $this->FAIstate)){
301 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
302 }
303 $this->dialog = false;
304 $this->is_dialog=false;
305 }
307 /* cancel configuration */
308 if(isset($_POST['CancelObjectConfig'])){
309 $this->dialog = false;
310 $this->is_dialog=false;
311 }
313 /* Display dialog */
314 if($this->is_dialog){
315 $this->dialog->save_object();
316 return $this->dialog->execute();
317 }
319 /* Assign section to smarty */
320 $strsec = "";
321 foreach($this->FAIdebianSection as $sec){
322 $strsec .= $sec." ";
323 }
325 $tmp = $this->plInfo();
326 foreach($tmp['plProvidedAcls'] as $name => $translated){
327 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
328 }
330 $smarty->assign("releases",$this->releases);
331 $smarty->assign("release" ,$this->FAIdebianRelease);
332 $smarty->assign("sections",$this->sections);
333 $smarty->assign("section" ,$strsec);
334 $smarty->assign("usedPackages",$this->printUsedPackages());
335 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
336 return($display);
337 }
339 /* Delete me, and all my subtrees
340 */
341 function remove_from_parent()
342 {
343 $ldap = $this->config->get_ldap_link();
344 $ldap->cd ($this->dn);
346 $faifilter = session::get('faifilter');
347 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
348 if($faifilter['branch'] == "main"){
349 $use_dn = $this->dn;
350 }
352 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
354 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
356 foreach($this->ConfiguredPackages as $pkgname => $attrs){
357 foreach($attrs as $name => $attr){
358 $pkgdn = "FAIvariable=".$name.",".$this->dn;
359 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $pkgdn);
360 if($faifilter['branch'] == "main"){
361 $use_dn = $obj['dn'];
362 }
363 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
364 }
365 }
366 $this->handle_post_events("remove");
367 }
370 /* Save data to object
371 */
372 function save_object()
373 {
374 if(preg_match("/^freeze/", $this->FAIstate)) return;
375 plugin::save_object();
376 }
379 /* Check supplied data */
380 function check()
381 {
382 /* Call common method to give check the hook */
383 $message= plugin::check();
385 if(count($this->usedPackages)==0){
386 $message[]= _("Please select a least one package!");
387 }
389 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
390 $message[]=_("Please choose a valid release/section combination for your repository setup!");
391 }
393 /* Ensure that we do not overwrite an allready existing entry
394 */
395 if($this->is_new){
396 $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').session::get('CurrentMainBase');
397 $faifilter = session::get('faifilter');
398 if($faifilter['branch']!="main"){
399 $new_dn ='cn='.$this->cn.",".get_ou('faipackageou').$faifilter['branch'];
400 }
402 $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
403 if(isset($res[$this->cn])){
404 $message[] = msgPool::duplicated(_("Name"));
405 }
406 }
407 return ($message);
408 }
411 function printUsedPackages()
412 {
413 $a_ret=array();
414 if(is_array($this->usedPackages)){
415 foreach($this->usedPackages as $usedName){
417 /* Append message if package is configured */
418 $c_str ="";
419 if(isset($this->ConfiguredPackages[$usedName])){
420 $c_str = " - "._("package is configured");
421 }
423 /* Adapt used name if we're marked for removal */
424 $dsc= "";
425 if (preg_match('/^-/', $usedName)){
426 $dsc= " - "._("Package marked for removal");
427 }else{
428 $usedName2= $usedName;
429 }
431 /* Append version string, if available */
432 if(isset($this->list[$usedName]['VERSION'])){
433 $a_ret[$usedName] = $usedName2." [".$this->list[$usedName]['VERSION']."]".$c_str.$dsc;
434 }else{
435 $a_ret[$usedName] = $usedName2.$c_str.$dsc;
436 }
437 }
438 }
439 return($a_ret);
440 }
443 function genPkgs($force = false)
444 {
445 if(!count($this->buffer) || $force){
446 $q = new gosaSupportDaemon();
447 $attrs = array("distribution", "package","version", "section", "description", "timestamp");
448 $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$this->usedPackages);
449 if($q->is_error()){
450 msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
451 }else{
452 foreach($ret as $attr){
453 $this->buffer[$attr['PACKAGE']] = $attr;
454 }
455 }
456 }
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"))){
526 if($pkgattrs['FAIvariableType'] == "text" && $pkgattrs['FAIvariableContent'] == ""){
527 gosa_log("Skipped saving FAIvariable '$name' empty string can't be saved.");
528 }else{
529 FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
530 }
531 }
532 }
533 }
534 }
536 /* Return plugin informations for acl handling */
537 static function plInfo()
538 {
539 return (array(
540 "plShortName" => _("Package"),
541 "plDescription" => _("FAI Package list"),
542 "plSelfModify" => FALSE,
543 "plDepends" => array(),
544 "plPriority" => 28,
545 "plSection" => array("administration"),
546 "plCategory" => array("fai"),
547 "plProvidedAcls" => array(
548 "cn" => _("Name"),
549 "description" => _("Description"),
550 "FAIpackage" => _("Packages"),
551 "FAIdebianSection" => _("Section")." ("._("Readonly").")",
552 "FAIinstallMethod" => _("Install Method"),
553 "FAIdebconfInfo" => _("Package configuration"),
554 "FAIdebianRelease" => _("Release")." ("._("Readonly").")")
555 ));
556 }
561 function PrepareForCopyPaste($source)
562 {
563 plugin::PrepareForCopyPaste($source);
565 if(isset($source['FAIstate'][0])){
566 $this->FAIstate = $source['FAIstate'][0];
567 }
569 $this->FAIdebianRelease = "ClearFromCopyPaste";
571 if(isset($source['FAIpackage'])){
572 unset($source['FAIpackage']['count']);
573 foreach($source['FAIpackage'] as $pkg){
574 $this->usedPackages[$pkg] = $pkg;
575 }
576 ksort($this->usedPackages);
577 }else{
578 $this->usedPackages = array();
579 }
581 if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
582 $this->FAIdebianSection = array();
583 for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
584 $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
585 }
586 }
588 /* Create one filter with all package names,
589 instead of calling $ldap->search for every single package
590 */
591 $PackageFilter = "";
592 foreach($this->usedPackages as $name){
593 $PackageFilter .= "(FAIpackage=".$name.")";
594 }
595 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
597 /* Search for configuration objects */
598 $ldap = $this->config->get_ldap_link();
599 $ldap->cd($source['dn']);
600 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
602 /* Walk through configurations and append them to our list of ConfiguredPackages */
603 while($attr = $ldap->fetch()){
605 /* Skip objects, that are tagged as removed */
606 if(isset($object['FAIstate'][0])){
607 if(preg_match("/removed$/",$attr['FAIstate'][0])){
608 continue;
609 }
610 }
612 $tmp =array();
613 $tmp['Name'] = $attr['FAIvariable'][0];
614 $tmp['Type'] = $attr['FAIvariableType'][0];
616 if (isset($attr['FAIvariableContent'][0])){
617 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
618 $tmp['Value'] = $attr['FAIvariableContent'][0];
619 }else{
620 $content = array();
621 unset($attr['FAIvariableContent']['count']);
622 foreach($attr['FAIvariableContent'] as $attr){
623 $tmp['Value'][] = $attr;
624 }
625 }
626 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
627 }
628 }
629 }
632 function getServerInfos()
633 {
634 $ret = array();
635 $ldap = $this->config->get_ldap_link();
636 $ldap->cd($this->config->current['BASE']);
637 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
638 while($attrs = $ldap->fetch()){
639 if(isset($attrs['FAIrepository'])){
640 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
641 $obj = $attrs['FAIrepository'][$i];
642 $tmp = split("\|",$obj);
643 if(count($tmp)==4){
644 foreach(split(",",$tmp[3]) as $sec){
645 if(!empty($sec)){
646 $ret[$tmp[2]][] = $sec;
647 }
648 }
649 }
650 }
651 }
652 }
653 return($ret);
654 }
657 /*! \brief Used for copy & paste.
658 Returns a HTML input mask, which allows to change the cn of this entry.
659 @param Array Array containing current status && a HTML template.
660 */
661 function getCopyDialog()
662 {
663 $vars = array("cn");
664 $smarty = get_smarty();
665 $smarty->assign("cn", htmlentities($this->cn));
666 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
667 $ret = array();
668 $ret['string'] = $str;
669 $ret['status'] = "";
670 return($ret);
671 }
674 /*! \brief Used for copy & paste.
675 Some entries must be renamed to avaoid duplicate entries.
676 */
677 function saveCopyDialog()
678 {
679 if(isset($_POST['cn'])){
680 $this->cn = get_post('cn');
681 }
682 }
683 }
685 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
686 ?>