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
37 var $sections = array(); // All section types
39 var $list = NULL;
41 var $usedPackages = array();
42 var $buffer = NULL;
43 var $newDialogShown =false;
45 var $FAIstate = "";
46 var $view_logged = FALSE;
47 var $base;
48 var $FAIpackage ;
50 var $FAIinstallMethods = array( "install", "ninstall", "remove",
51 "dselect-upgrade", "taskinst", "taskrm",
52 "hold", "clean", "aptitude", "aptitude-r",
53 "pending", "dpkgc" );
56 function faiPackage (&$config, $dn= NULL)
57 {
58 /* Load Attributes */
59 plugin::plugin ($config, $dn);
61 /* If "dn==new" we try to create a new entry
62 * Else we must read all objects from ldap which belong to this entry.
63 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
64 */
65 if($dn != "new"){
66 $this->dn =$dn;
68 /* Get FAIstate */
69 if(isset($this->attrs['FAIstate'][0])){
70 $this->FAIstate = $this->attrs['FAIstate'][0];
71 }
73 /* Check if there are already some packages in this list
74 */
75 $this->usedPackages = array();
76 if(isset($this->attrs['FAIpackage'])){
77 unset($this->attrs['FAIpackage']['count']);
78 foreach($this->attrs['FAIpackage'] as $pkg){
79 $name = preg_replace("/^\-/","",$pkg);
80 $this->usedPackages[$name] = $pkg;
81 }
82 ksort($this->usedPackages);
83 }
85 /* Fetch all package configurations from ldap
86 */
87 $PackageFilter = "";
88 foreach($this->usedPackages as $name => $value){
89 $PackageFilter .= "(FAIpackage=".$name.")";
90 }
91 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
93 /* Search for configuration objects */
94 $ldap = $this->config->get_ldap_link();
95 $ldap->cd($this->dn);
96 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType",
97 "FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
99 /* Walk through configurations and append them to our list of ConfiguredPackages
100 */
101 while($attr = $ldap->fetch()){
103 /* Skip objects, that are tagged as removed */
104 if(isset($object['FAIstate'][0])){
105 if(preg_match("/removed$/",$attr['FAIstate'][0])){
106 continue;
107 }
108 }
110 $tmp =array();
111 $tmp['Name'] = $attr['FAIvariable'][0];
112 $tmp['Type'] = $attr['FAIvariableType'][0];
114 if (isset($attr['FAIvariableContent'][0])){
115 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
116 $tmp['Value'] = $attr['FAIvariableContent'][0];
117 }else{
118 $content = array();
119 unset($attr['FAIvariableContent']['count']);
120 foreach($attr['FAIvariableContent'] as $attr){
121 $tmp['Value'][] = $attr;
122 }
123 }
124 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
125 }
126 }
128 $this->FAIdebianSection = array();
129 if(isset($this->attrs['FAIdebianSection'])){
130 for($i = 0 ; $i < $this->attrs['FAIdebianSection']['count'] ; $i++ ){
131 $sec = $this->attrs['FAIdebianSection'][$i];
132 $this->FAIdebianSection[$sec]=$sec;
133 }
134 }
135 $this->FAIpackage = array();
137 } // ENDE dn != new
139 $methods = array();
140 foreach($this->FAIinstallMethods as $method){
141 $methods[$method] = $method;
142 }
143 $this->FAIinstallMethods = $methods;
145 /* Check if we exist already - no need to ask for revisions, etc. then */
146 if ($this->dn != "new"){
147 $this->newDialogShown= true;
148 }
149 $this->is_new = FALSE;
150 if($this->dn == "new"){
151 $this->is_new =TRUE;
152 }
154 /* Generate package list */
155 $this->list= $this->genPkgs(TRUE);
156 }
159 function execute()
160 {
161 /* Call parent execute */
162 plugin::execute();
164 if($this->is_account && !$this->view_logged){
165 $this->view_logged = TRUE;
166 new log("view","fai/".get_class($this),$this->dn);
167 }
169 /* Fill templating stuff */
170 $smarty= get_smarty();
171 $display= "";
173 /******
174 * Initialize a new Package List with release and section name
175 ******/
177 if(!$this->is_account){
179 /* Assemble release name */
180 $release = $this->parent->parent->fai_release;
181 $tmp= preg_replace('/[,]*'.normalizePreg(get_ou('faiou')).'.*$/', '', $release);
182 $tmp= preg_replace('/ou=/', '', $tmp);
183 $rev= array_reverse(split(',', $tmp));
184 $this->FAIdebianRelease= "/";
185 foreach ($rev as $part){
186 $this->FAIdebianRelease.= "/$part";
187 }
188 $this->FAIdebianRelease= preg_replace('#^[/]*#', '', $this->FAIdebianRelease);
190 /* Assemble sections */
191 $repos= $this->getServerInfos();
192 if(isset($repos[$this->FAIdebianRelease])){
193 $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
194 $this->FAIdebianSection= array_unique($this->FAIdebianSection);
195 }
197 /* Assign Repository settings*/
198 $this->is_account = true;
199 }
202 /******
203 * Add
204 ******/
206 /* + was pressed to open the package dialog */
207 if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
208 $this->dialog = new faiPackageEntry($this->config, $this->FAIdebianRelease,$this->usedPackages);
209 $this->is_dialog =true;
210 }
212 /* Check image Posts
213 */
214 foreach($_POST as $name => $value){
216 /******
217 * Mark as removed
218 ******/
220 if(!preg_match('/^freeze/', $this->FAIstate) && preg_match("/^removal_package_/",$name)){
221 $id = @base64_decode(preg_replace("/^removal_package_(.*)_[xy]*$/","\\1",$name));
222 if(isset($this->usedPackages[$id])){
223 $pkg = $this->usedPackages[$id];
224 if (preg_match('/^-/', $pkg)){
225 $pkg= preg_replace('/^-/', '', $pkg);
226 } else {
227 $pkg= preg_replace('/^/', '-', $pkg);
228 }
229 $this->usedPackages[$id] = $pkg;
230 }
231 break;
232 }
234 /******
235 * Delete Pkgs
236 ******/
237 if(!preg_match('/^freeze/', $this->FAIstate) && preg_match("/^remove_package_/",$name)){
238 $id = @base64_decode(preg_replace("/^remove_package_(.*)_[xy]*$/","\\1",$name));
240 if(isset($this->usedPackages[$id])){
241 $this->usedPackages[$id];
242 }
243 break;
244 }
246 /******
247 * Configure Pkgs
248 ******/
249 if(!preg_match('/^freeze/', $this->FAIstate) && preg_match("/^configure_package_/",$name)){
250 $pkg = @base64_decode(preg_replace("/^configure_package_(.*)_[xy]*$/","\\1",$name));
252 if(isset($this->usedPackages[$pkg])){
254 /* Configuration dialog open*/
255 $pkg_config = array();
256 if(isset($this->ConfiguredPackages[$pkg])){
257 $pkg_config = $this->ConfiguredPackages[$pkg];
258 }
259 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $this->FAIdebianRelease , $pkg_config);
260 $this->is_dialog =true;
261 }
262 break;
263 }
264 }
266 /* Abort package selection dialog */
267 if(isset($_POST['CancelSubObject'])){
268 $this->dialog = false;
269 $this->is_dialog=false;
270 }
272 /* attach new packages */
273 if(isset($_POST['SaveSubObject'])) {
274 if(!preg_match("/^freeze/", $this->FAIstate)){
275 $this->dialog->save_object();
276 if(count($this->dialog->check())){
277 foreach($this->dialog->check() as $msgs){
278 msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
279 }
280 }else{
281 $use = $this->dialog->save();
282 $this->usedPackages = $use;
283 $this->dialog = false;
284 $this->is_dialog=false;
285 ksort($this->usedPackages);
287 /* Generate package list */
288 $this->list= $this->genPkgs(TRUE);
289 }
290 }else{
291 $this->dialog = false;
292 $this->is_dialog=false;
293 }
294 }
296 /* Save Configuration */
297 if(isset($_POST['SaveObjectConfig'])){
298 if(!preg_match("/^freeze/", $this->FAIstate)){
299 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
300 }
301 $this->dialog = false;
302 $this->is_dialog=false;
303 }
305 /* cancel configuration */
306 if(isset($_POST['CancelObjectConfig'])){
307 $this->dialog = false;
308 $this->is_dialog=false;
309 }
311 /* Display dialog */
312 if($this->is_dialog){
313 $this->dialog->save_object();
314 return $this->dialog->execute();
315 }
318 /******
319 * Display UI / HTML / smarty
320 ******/
322 /* Create divlist to display a list of all currently used packages
323 */
324 $divlist = new divSelectBox("faiPackages");
326 ksort($this->usedPackages);
327 if(is_array($this->usedPackages)){
328 foreach($this->usedPackages as $usedName => $name){
330 /* Append message if package is configured */
331 $configured = "<img src='images/empty.png' alt=' ' class='center'>";
332 if(isset($this->ConfiguredPackages[$usedName])){
333 $configured = "<img src='images/package_configure.png' alt='"._("Configured")."'
334 title='"._("Configured")."' class='center'>";
335 }
337 /* Adapt used name if we're marked for removal */
338 $removal = "<img src='images/empty.png' alt=' ' class='center'>";
339 if (preg_match('/^-/', $name)){
340 $removal = "<img src='images/negate.png' alt='"._("Package marked for removal")."'
341 title='"._("Package marked for removal")."' class='center'>";
342 }
344 /* Get Version */
345 $version = "";
346 if(isset($this->list[$usedName]['VERSION'])){
347 $version = $this->list[$usedName]['VERSION'];
348 }
350 /* Get description */
351 $description = "";
352 if(isset($this->list[$usedName]['DESCRIPTION'])){
353 $description = $this->list[$usedName]['DESCRIPTION'];
354 }
356 $actions = "<input type='image' class='center' title='"._("Mark package for removal")."'
357 src='images/negate.png' name='removal_package_".base64_encode($usedName)."' >";
358 $actions.= " <input type='image' class='center' title='"._("Configure this package")."'
359 src='images/package_configure.png' name='configure_package_".base64_encode($usedName)."' >";
360 $actions.= " <input type='image' class='center' title='"._("Remove this package")."'
361 src='images/lists/trash.png' name='remove_package_".base64_encode($usedName)."' >";
363 $field1 = array("string" => $configured." ".$removal,"attach" => "style='width:40px;'");
364 $field2 = array("string" => $usedName ,"attach" => "style='width:200px;'");
365 $field3 = array("string" => $version);
366 $field4 = array("string" => $description);
367 $field5 = array("string" => $actions ,"attach" => "style='width:60px; border-right:0px;'");
368 $divlist->AddEntry(array($field1,$field2,$field3,$field4,$field5));
369 }
370 }
372 /* Assign variables */
373 foreach($this->attributes as $attrs){
374 $smarty->assign($attrs,$this->$attrs);
375 }
376 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
378 /* Assign section to smarty */
379 $strsec = "";
380 foreach($this->FAIdebianSection as $sec){
381 $strsec .= $sec." ";
382 }
384 $tmp = $this->plInfo();
385 foreach($tmp['plProvidedAcls'] as $name => $translated){
386 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
387 }
389 $smarty->assign("divlist",$divlist->DrawList());
390 $smarty->assign("release" ,$this->FAIdebianRelease);
391 $smarty->assign("sections",$this->sections);
392 $smarty->assign("section" ,$strsec);
393 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
394 return($display);
395 }
398 /*! \brief Removes this packageList from the ldap database
399 */
400 function remove_from_parent()
401 {
402 $ldap = $this->config->get_ldap_link();
403 $ldap->cd ($this->dn);
404 $release = $this->parent->parent->fai_release;
405 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
406 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
407 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
408 foreach($this->ConfiguredPackages as $pkgname => $attrs){
409 foreach($attrs as $name => $attr){
410 $pkgdn = "FAIvariable=".$name.",".$this->dn;
411 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $pkgdn);
412 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
413 }
414 }
415 $this->handle_post_events("remove");
416 }
419 /*! \brief Collect all relevant POST vars for this plugin
420 */
421 function save_object()
422 {
423 if(preg_match("/^freeze/", $this->FAIstate)) return;
424 plugin::save_object();
425 }
428 /*! \brief Check given inputs for this package list
429 @return Array Containing all error messages, or an empty array if no error occured
430 */
431 function check()
432 {
433 /* Call common method to give check the hook */
434 $message= plugin::check();
436 if(count($this->usedPackages)==0){
437 $message[]= _("Please select a least one package!");
438 }
440 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
441 $message[]=_("Please choose a valid release/section combination for your repository setup!");
442 }
444 /* Ensure that we do not overwrite an allready existing entry
445 */
446 if($this->is_new){
447 $release = $this->parent->parent->fai_release;
448 $new_dn= 'cn='.$this->cn.",".get_ou('faipackageou').get_ou('faiou').$release;
449 $res = faiManagement::check_class_name("FAIpackageList",$this->cn,$new_dn);
450 if(isset($res[$this->cn])){
451 $message[] = msgPool::duplicated(_("Name"));
452 }
453 }
454 return ($message);
455 }
458 /*! \brief Reload the list of cached packages.
459 @return Returns the currently cached list of packages.
460 */
461 function genPkgs($force = false)
462 {
463 if(!count($this->buffer) || $force){
464 $q = new gosaSupportDaemon();
465 $attrs = array("distribution", "package","version", "section", "description", "timestamp");
467 $packages = array_keys($this->usedPackages);
469 $ret = $q->FAI_get_packages($this->FAIdebianRelease,$attrs,$packages);
470 if($q->is_error()){
471 msg_dialog::display(_("Service infrastructure"),msgPool::siError($q->get_error()),ERROR_DIALOG);
472 }else{
473 foreach($ret as $attr){
474 $this->buffer[$attr['PACKAGE']] = $attr;
475 }
476 }
477 }
478 return $this->buffer;
479 }
482 /*! \brief Save packages and their configuration to ldap
483 */
484 function save()
485 {
487 /* Assemble release name */
488 if($this->FAIdebianRelease == "ClearFromCopyPaste"){
490 $current_release = $this->parent->parent->fai_release;
491 $tmp= preg_replace('/,'.normalizePreg(get_ou('faiou')).'.*$/', '', $current_release);
492 $tmp= preg_replace('/ou=/', '', $tmp);
493 $rev= array_reverse(split(',', $tmp));
494 $this->FAIdebianRelease= "";
495 foreach ($rev as $part){
496 $this->FAIdebianRelease.= "/$part";
497 }
498 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
499 }
501 plugin::save();
503 $ldap = $this->config->get_ldap_link();
505 $this->attrs['FAIpackage'] = array();
506 foreach($this->usedPackages as $pkg){
507 $this->attrs['FAIpackage'][] = $pkg;
508 }
510 $this->attrs['FAIdebianSection'] = array();
511 foreach($this->FAIdebianSection as $sec){
512 $this->attrs['FAIdebianSection'][] = $sec;
513 }
515 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
517 if($this->initially_was_account){
518 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
519 }else{
520 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
521 }
523 $ldap->cd($this->dn);
525 /* Save Package configurations */
526 foreach($this->ConfiguredPackages as $pkgname => $attrs){
527 foreach($attrs as $name => $attr){
529 $pkgattrs = array();
531 foreach($attr as $n=>$v){
532 if(empty($v)) $v = array();
533 }
535 /* Set attributes */
536 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
538 $pkgattrs['FAIpackage'] = $pkgname;
539 $pkgattrs['FAIvariable'] = $name;
540 $pkgattrs['FAIvariableType'] = $attr['Type'];
541 $pkgattrs['FAIvariableContent'] = $attr['Value'];
542 $pkgdn = "FAIvariable=".$name.",".$this->dn;
544 /* Tag object */
545 $this->tag_attrs($pkgattrs, $pkgdn, $this->gosaUnitTag);
547 if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
549 if($pkgattrs['FAIvariableType'] == "text" && $pkgattrs['FAIvariableContent'] == ""){
550 gosa_log("Skipped saving FAIvariable '$name' empty string can't be saved.");
551 }else{
552 FAI::prepare_to_save_FAI_object($pkgdn,$pkgattrs);
553 }
554 }
555 }
556 }
557 }
560 /*! \brief Return plugin informations for acl handling
561 @return Array ACL infos of this plugin.
562 */
563 static function plInfo()
564 {
565 return (array(
566 "plShortName" => _("Package"),
567 "plDescription" => _("FAI Package list"),
568 "plSelfModify" => FALSE,
569 "plDepends" => array(),
570 "plPriority" => 28,
571 "plSection" => array("administration"),
572 "plCategory" => array("fai"),
573 "plProvidedAcls" => array(
574 "cn" => _("Name"),
575 "description" => _("Description"),
576 "FAIpackage" => _("Packages"),
577 "FAIdebianSection" => _("Section")." ("._("Readonly").")",
578 "FAIinstallMethod" => _("Install Method"),
579 "FAIdebconfInfo" => _("Package configuration"),
580 "FAIdebianRelease" => _("Release")." ("._("Readonly").")")
581 ));
582 }
585 /*! \brief prepares this plugin to be inserted after it was copied or cut.
586 @param Array All attributes from the source object.
587 */
588 function PrepareForCopyPaste($source)
589 {
590 plugin::PrepareForCopyPaste($source);
592 if(isset($source['FAIstate'][0])){
593 $this->FAIstate = $source['FAIstate'][0];
594 }
596 $this->FAIdebianRelease = "ClearFromCopyPaste";
598 if(isset($source['FAIpackage'])){
599 unset($source['FAIpackage']['count']);
600 foreach($source['FAIpackage'] as $pkg){
601 $this->usedPackages[$pkg] = $pkg;
602 }
603 ksort($this->usedPackages);
604 }else{
605 $this->usedPackages = array();
606 }
608 if((isset($source['FAIdebianSection']))&&(is_array($source['FAIdebianSection']))){
609 $this->FAIdebianSection = array();
610 for($i = 0 ; $i < $source['FAIdebianSection']['count'] ; $i ++){
611 $this->FAIdebianSection[$source['FAIdebianSection'][$i]]=$source['FAIdebianSection'][$i];
612 }
613 }
615 /* Create one filter with all package names,
616 instead of calling $ldap->search for every single package
617 */
618 $PackageFilter = "";
619 foreach($this->usedPackages as $name){
620 $PackageFilter .= "(FAIpackage=".$name.")";
621 }
622 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
624 /* Search for configuration objects */
625 $ldap = $this->config->get_ldap_link();
626 $ldap->cd($source['dn']);
627 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
629 /* Walk through configurations and append them to our list of ConfiguredPackages */
630 while($attr = $ldap->fetch()){
632 /* Skip objects, that are tagged as removed */
633 if(isset($object['FAIstate'][0])){
634 if(preg_match("/removed$/",$attr['FAIstate'][0])){
635 continue;
636 }
637 }
639 $tmp =array();
640 $tmp['Name'] = $attr['FAIvariable'][0];
641 $tmp['Type'] = $attr['FAIvariableType'][0];
643 if (isset($attr['FAIvariableContent'][0])){
644 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
645 $tmp['Value'] = $attr['FAIvariableContent'][0];
646 }else{
647 $content = array();
648 unset($attr['FAIvariableContent']['count']);
649 foreach($attr['FAIvariableContent'] as $attr){
650 $tmp['Value'][] = $attr;
651 }
652 }
653 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
654 }
655 }
656 }
659 /*! \brief Returns a list of all configured servers with repositories.
660 @return Array All repository server
661 */
662 function getServerInfos()
663 {
664 $ret = array();
665 $ldap = $this->config->get_ldap_link();
666 $ldap->cd($this->config->current['BASE']);
667 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
668 while($attrs = $ldap->fetch()){
669 if(isset($attrs['FAIrepository'])){
670 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
671 $obj = $attrs['FAIrepository'][$i];
672 $tmp = split("\|",$obj);
673 if(count($tmp)==4){
674 foreach(split(",",$tmp[3]) as $sec){
675 if(!empty($sec)){
676 $ret[$tmp[2]][] = $sec;
677 }
678 }
679 }
680 }
681 }
682 }
683 return($ret);
684 }
687 /*! \brief Used for copy & paste.
688 Returns a HTML input mask, which allows to change the cn of this entry.
689 @param Array Array containing current status && a HTML template.
690 */
691 function getCopyDialog()
692 {
693 $vars = array("cn");
694 $smarty = get_smarty();
695 $smarty->assign("cn", htmlentities($this->cn));
696 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
697 $ret = array();
698 $ret['string'] = $str;
699 $ret['status'] = "";
700 return($ret);
701 }
704 /*! \brief Used for copy & paste.
705 Some entries must be renamed to avaoid duplicate entries.
706 */
707 function saveCopyDialog()
708 {
709 if(isset($_POST['cn'])){
710 $this->cn = get_post('cn');
711 }
712 }
713 }
715 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
716 ?>