PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.8 KiB

7 years ago
  1. <?php
  2. # $Id$
  3. /**
  4. * class to handle 'view' in Cli
  5. */
  6. class CliView extends Shell {
  7. protected $handler_to_use = 'invalid';
  8. /**
  9. * Execution method always used for tasks
  10. */
  11. public function execute() {
  12. if (empty($this->args)) {
  13. $this->__interactive();
  14. }
  15. if (!empty($this->args[0])) {
  16. $this->__handle($this->args[0]);
  17. }
  18. }
  19. /**
  20. * Interactive mode
  21. */
  22. protected function __interactive() {
  23. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  24. $module = strtolower($module);
  25. $question = "Which $module do you want to view?";
  26. $address = $this->in($question);
  27. $this->__handle($address);
  28. }
  29. /**
  30. * actually view something
  31. *
  32. * @param string address to view
  33. */
  34. protected function __handle($address) {
  35. $handler = new $this->handler_to_use($this->new);
  36. if (!$handler->init($address)) {
  37. $this->err($handler->errormsg);
  38. return;
  39. }
  40. if (!$handler->view()) {
  41. $this->err($handler->errormsg);
  42. return;
  43. }
  44. $result = $handler->result();
  45. $struct = $handler->getStruct();
  46. foreach (array_keys($struct) as $field) {
  47. if (isset($struct[$field]) && empty($struct[$field]['label'])) {
  48. # $struct[$field]['label'] = "--- $field ---";
  49. $struct[$field]['display_in_list'] = 0;
  50. }
  51. if ($struct[$field]['display_in_list'] == 0) {
  52. # do nothing
  53. } else {
  54. $value = $result[$field];
  55. $func="_formatted_".$field;
  56. if (method_exists($handler, $func)) {
  57. $value = $handler->{$func}($result); # call _formatted_$fieldname()
  58. }
  59. if ($struct[$field]['type'] == 'txtl') {
  60. # $value = join("\n" . str_repeat(" ", 20 + 2), $value); # multiline, one item per line
  61. $value = join(", ", $value); # one line, comma-separated
  62. } elseif ($struct[$field]['type'] == 'bool') {
  63. $value = Config::Lang($value ? 'YES' : 'NO');
  64. }
  65. $this->out(sprintf("%20s: %s", $struct[$field]['label'], $value));
  66. }
  67. }
  68. }
  69. /**
  70. * Display help contents
  71. *
  72. * @access public
  73. */
  74. public function help() {
  75. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  76. $module = strtolower($module);
  77. $this->out(
  78. "Usage:
  79. postfixadmin-cli $module view
  80. View $module in interactive mode.
  81. - or -
  82. postfixadmin-cli $module view <address>
  83. View $module <address> in non-interactive mode.
  84. "
  85. );
  86. $this->_stop();
  87. }
  88. }
  89. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */