Shell.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Base class for Shells
  4. *
  5. * Long description for file
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  10. * Copyright 2005-2008, Cake Software Foundation, Inc.
  11. * 1785 E. Sahara Avenue, Suite 490-204
  12. * Las Vegas, Nevada 89104
  13. * Modified for Postfixadmin by Valkum
  14. *
  15. * Copyright 2010
  16. *
  17. * Licensed under The MIT License
  18. * Redistributions of files must retain the above copyright notice.
  19. *
  20. * @filesource
  21. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
  22. * @link http://postfixadmin.sourceforge.net/ Postfixadmin on Sourceforge
  23. * @package postfixadmin
  24. * @subpackage -
  25. * @since -
  26. * @version $Revision$
  27. * @modifiedby $LastChangedBy$
  28. * @lastmodified $Date$
  29. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  30. */
  31. class Shell {
  32. /**
  33. * An instance of the ShellDispatcher object that loaded this script
  34. *
  35. * @var object
  36. * @access public
  37. */
  38. public $Dispatch;
  39. /**
  40. * If true, the script will ask for permission to perform actions.
  41. *
  42. * @var boolean
  43. * @access public
  44. */
  45. public $interactive = true;
  46. /**
  47. * Contains command switches parsed from the command line.
  48. *
  49. * @var array
  50. * @access public
  51. */
  52. public $params = array();
  53. /**
  54. * Contains arguments parsed from the command line.
  55. *
  56. * @var array
  57. * @access public
  58. */
  59. public $args = array();
  60. /**
  61. * The file name of the shell that was invoked.
  62. *
  63. * @var string
  64. * @access public
  65. */
  66. public $shell;
  67. /**
  68. * The class name of the shell that was invoked.
  69. *
  70. * @var string
  71. * @access public
  72. */
  73. public $className;
  74. /**
  75. * The command called if public methods are available.
  76. *
  77. * @var string
  78. * @access public
  79. */
  80. public $command;
  81. /**
  82. * The name of the shell in camelized.
  83. *
  84. * @var string
  85. * @access public
  86. */
  87. public $name;
  88. /**
  89. * @param string
  90. */
  91. public $handler_to_use;
  92. public $new;
  93. /**
  94. * Constructs this Shell instance.
  95. *
  96. */
  97. public function __construct(&$dispatch) {
  98. $vars = array('params', 'args', 'shell', 'shellCommand'=> 'command');
  99. foreach ($vars as $key => $var) {
  100. if (is_string($key)) {
  101. $this->{$var} =& $dispatch->{$key};
  102. } else {
  103. $this->{$var} =& $dispatch->{$var};
  104. }
  105. }
  106. $this->className = get_class($this);
  107. if ($this->name == null) {
  108. $this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
  109. }
  110. $this->Dispatch =& $dispatch;
  111. }
  112. /**
  113. * Starts up the the Shell
  114. * allows for checking and configuring prior to command or main execution
  115. * can be overriden in subclasses
  116. *
  117. * @access public
  118. */
  119. public function startup() {
  120. if (empty($this->params['q'])) {
  121. $this->_welcome();
  122. }
  123. }
  124. /**
  125. * Displays a header for the shell
  126. *
  127. * @access protected
  128. */
  129. public function _welcome() {
  130. $this->out("\nWelcome to Postfixadmin-CLI v" . $this->Dispatch->version);
  131. $this->hr();
  132. }
  133. /**
  134. * Prompts the user for input, and returns it.
  135. *
  136. * @param string $prompt Prompt text.
  137. * @param mixed $options Array or string of options.
  138. * @param string $default Default input value.
  139. * @return string either the default value, or the user-provided input.
  140. */
  141. public function in($prompt, $options = null, $default = '') {
  142. if (!$this->interactive) {
  143. return $default;
  144. }
  145. if ($prompt != '') {
  146. $this->out("");
  147. }
  148. $in = $this->Dispatch->getInput($prompt, $options, $default);
  149. if ($options && is_string($options)) {
  150. if (strpos($options, ',')) {
  151. $options = explode(',', $options);
  152. } elseif (strpos($options, '/')) {
  153. $options = explode('/', $options);
  154. } else {
  155. $options = array($options);
  156. }
  157. }
  158. if (is_array($options)) {
  159. while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
  160. $this->err("Invalid input"); # TODO: make translateable
  161. $in = $this->Dispatch->getInput($prompt, $options, $default);
  162. }
  163. }
  164. return $in;
  165. }
  166. /**
  167. * Outputs to the stdout filehandle.
  168. *
  169. * @param string|array $string String to output.
  170. * @param boolean $newline If true, the outputs gets an added newline.
  171. */
  172. public function out($string, $newline = true) {
  173. if (is_array($string)) {
  174. $str = '';
  175. foreach ($string as $message) {
  176. $str .= $message ."\n";
  177. }
  178. $string = $str;
  179. }
  180. return $this->Dispatch->stdout($string, $newline);
  181. }
  182. /**
  183. * Outputs to the stderr filehandle.
  184. *
  185. * @param string|array $string Error text to output.
  186. * @access public
  187. */
  188. public function err($string) {
  189. if (is_array($string)) {
  190. $str = '';
  191. foreach ($string as $message) {
  192. $str .= $message ."\n";
  193. }
  194. $string = $str;
  195. }
  196. return $this->Dispatch->stderr($string."\n");
  197. }
  198. /**
  199. * Outputs a series of minus characters to the standard output, acts as a visual separator.
  200. *
  201. * @param boolean $newline If true, the outputs gets an added newline.
  202. * @access public
  203. */
  204. public function hr($newline = false) {
  205. if ($newline) {
  206. $this->out("\n");
  207. }
  208. $this->out('---------------------------------------------------------------');
  209. if ($newline) {
  210. $this->out("\n");
  211. }
  212. }
  213. /**
  214. * Displays a formatted error message and exits the application
  215. *
  216. * @param string $title Title of the error message
  217. * @param string $msg Error message
  218. * @access public
  219. */
  220. public function error($title, $msg) {
  221. $out = "$title\n";
  222. $out .= "$msg\n";
  223. $out .= "\n";
  224. $this->err($out);
  225. $this->_stop(1);
  226. }
  227. /**
  228. * Outputs usage text on the standard output. Implement it in subclasses.
  229. *
  230. * @access public
  231. */
  232. public function help() {
  233. if ($this->command != null) {
  234. $this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
  235. } else {
  236. $this->Dispatch->help();
  237. }
  238. }
  239. /**
  240. * Stop execution of the current script
  241. *
  242. * @param $status see http://php.net/exit for values
  243. * @return void
  244. * @access public
  245. */
  246. public function _stop($status = 0) {
  247. exit($status);
  248. }
  249. }