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.3KB

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