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.

CliEdit.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. # $Id$
  3. /**
  4. * class to handle add and edit in Cli
  5. *
  6. * extends the "Shell" class
  7. */
  8. class CliEdit extends Shell {
  9. public $handler_to_use = "";
  10. public $new = 0;
  11. /**
  12. * Execution method always used for tasks
  13. */
  14. public function execute() {
  15. if (empty($this->args)) {
  16. $this->__interactive();
  17. } else {
  18. $this->__handle_params();
  19. }
  20. }
  21. /**
  22. * non-interactive mode
  23. * read, check and handle all --* parameters
  24. * The list of allowed params is based on $handler->struct
  25. */
  26. private function __handle_params() {
  27. $handler = new $this->handler_to_use($this->new);
  28. $form_fields = $handler->getStruct();
  29. $id_field = $handler->getId_field();
  30. $values = array();
  31. $param_error = 0;
  32. foreach ($this->params as $key => $val) {
  33. $key = preg_replace('/^-/', '', $key); # allow --param, not only -param
  34. $key = str_replace('-', '_', $key); # allow --foo-bar even if field is named foo_bar
  35. if (isset($form_fields[$key]) && $form_fields[$key]['editable'] && $form_fields[$key]['display_in_form'] && $key != $id_field) {
  36. if ($form_fields[$key]['type'] == 'txtl') {
  37. $values[$key] = explode(',', $val);
  38. } elseif ($form_fields[$key]['type'] == 'bool') {
  39. if (strtolower($val) == 'y') {
  40. $val = 1;
  41. } # convert y to 1
  42. if (strtolower($val) == 'n') {
  43. $val = 0;
  44. } # convert n to 0
  45. $values[$key] = $val; # don't modify any other value - *Handler will complain if it's invalid ;-)
  46. } else {
  47. $values[$key] = $val;
  48. }
  49. } elseif ($key == 'webroot') {
  50. # always set, ignore
  51. } else { # not editable, unknown field etc.
  52. $param_error = 1;
  53. $this->err("invalid parameter --$key => $val");
  54. }
  55. }
  56. if ($param_error) {
  57. $this->_stop(1);
  58. }
  59. $this->__handle($this->args[0], $values);
  60. }
  61. /**
  62. * Interactive mode
  63. */
  64. private function __interactive() {
  65. $handler = new $this->handler_to_use($this->new);
  66. $form_fields = $handler->getStruct();
  67. $id_field = $handler->getId_field();
  68. $values[$id_field] = '';
  69. while ($form_fields[$id_field]['editable'] != 0) { # endlees loop - except if input is valid or id_field is not editable (like auto_increment)
  70. $question = $form_fields[$id_field]['label'] . ":";
  71. if ($form_fields[$id_field]['desc'] != '') {
  72. $question .= "\n(" . $form_fields[$id_field]['desc'] . ')';
  73. }
  74. $values[$id_field] = $this->in($form_fields[$id_field]['label'] . ':');
  75. if ($handler->init($values[$id_field])) {
  76. break;
  77. } else {
  78. $this->err($handler->errormsg);
  79. # always use a fresh handler to avoid problems with previous error messages
  80. $handler = new $this->handler_to_use($this->new);
  81. }
  82. }
  83. # update $form_fields (needed for example to display the correct allowed quota)
  84. # TODO: doesn't (always?) work - wrong time for the refresh?
  85. # $handler->set(array());
  86. $form_fields = $handler->getStruct();
  87. foreach ($form_fields as $key => $field) {
  88. if ($field['editable'] && $field['display_in_form'] && $key != $id_field) {
  89. while (0==0) { # endlees loop - except if input is valid
  90. $question = $field['label'] . ':';
  91. if ($field['desc'] != '') {
  92. $question .= "\n(" . $field['desc'] . ')';
  93. }
  94. if ($field['type'] == 'bool') {
  95. $values[$key] = $this->in($question, array('y', 'n'));
  96. if ($values[$key] == 'y') {
  97. $values[$key] = 1;
  98. } else {
  99. $values[$key] = 0;
  100. }
  101. } elseif ($field['type'] == 'enum') {
  102. $optiontxt = array();
  103. $optionlist = array();
  104. foreach ($field['options'] as $optionkey => $optionval) {
  105. // $this->in hates number 0
  106. $optionkey = $optionkey + 1;
  107. $optiontxt[] = '['.$optionkey.'] - '.$optionval;
  108. $optionlist[] = $optionkey;
  109. }
  110. $question .= "\n" . join("\n", $optiontxt) . "\n";
  111. $values[$key] = $this->in($question, $optionlist);
  112. $values[$key] = $field['options'][$values[$key]-1]; # convert int to option name
  113. } elseif ($field['type'] == 'txtl') {
  114. $values[$key] = array();
  115. $nextval = $this->in($question);
  116. while ($nextval != '') {
  117. if ($nextval != '') {
  118. $values[$key][] = $nextval;
  119. }
  120. $nextval = $this->in("");
  121. }
  122. } else {
  123. $values[$key] = $this->in($question);
  124. }
  125. if (is_null($values[$key])) { # TODO: insull() is probably obsoleted by change in Shell class
  126. echo "*** value of $key is NULL - this should not happen! ***";
  127. }
  128. if ($values[$key] == '' && (!$this->new)) { # edit mode
  129. unset($values[$key]); # empty input - don't change
  130. }
  131. # always use a fresh handler to avoid problems with previous error messages
  132. $handler = new $this->handler_to_use($this->new);
  133. $handler->init($values[$id_field]);
  134. $handler->set($values);
  135. if (isset($handler->errormsg[$key])) { # only check the errormessage for this field
  136. $this->err($handler->errormsg[$key]);
  137. } else {
  138. break;
  139. }
  140. } # end while
  141. } # end if $field[editable] etc.
  142. } # end foreach
  143. $this->__handle($values[$id_field], $values);
  144. }
  145. /**
  146. * (try to) store values
  147. */
  148. private function __handle($id, $values) {
  149. $handler = new $this->handler_to_use($this->new);
  150. if (!$handler->init($id)) {
  151. $this->err($handler->errormsg);
  152. return;
  153. }
  154. if (!$handler->set($values)) {
  155. $this->err($handler->errormsg);
  156. return;
  157. }
  158. if (!$handler->store()) {
  159. $this->err($handler->errormsg);
  160. } else {
  161. $this->out("");
  162. $this->out($handler->infomsg);
  163. $this->hr();
  164. }
  165. return;
  166. }
  167. /**
  168. * Displays help contents
  169. */
  170. public function help() {
  171. if ($this->new) {
  172. $cmd = 'add';
  173. $cmdtext = 'Adds';
  174. } else {
  175. $cmd = 'update';
  176. $cmdtext = 'Changes';
  177. }
  178. $module = preg_replace('/Handler$/', '', $this->handler_to_use);
  179. $module = strtolower($module);
  180. $this->out(
  181. "Usage:
  182. postfixadmin-cli $module $cmd
  183. $cmdtext $module in interactive mode.
  184. - or -
  185. postfixadmin-cli $module $cmd <address> --option value --option2 value [...]
  186. $cmdtext $module in non-interactive mode.
  187. Available options are:
  188. "
  189. );
  190. $handler = new $this->handler_to_use($this->new);
  191. $form_fields = $handler->getStruct();
  192. $id_field = $handler->getId_field();
  193. foreach ($form_fields as $key => $field) {
  194. if ($field['editable'] && $field['display_in_form'] && $key != $id_field) {
  195. $optkey = str_replace('_', '-', $key);
  196. $this->out(" --$optkey");
  197. $this->out(" " . $field['label']);
  198. if ($field['desc']) {
  199. $this->out(" " . $field['desc']);
  200. }
  201. $this->out("");
  202. }
  203. }
  204. $this->_stop();
  205. }
  206. }
  207. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */