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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 = null;
  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 = null;
  67. /**
  68. * The class name of the shell that was invoked.
  69. *
  70. * @var string
  71. * @access public
  72. */
  73. public $className = null;
  74. /**
  75. * The command called if public methods are available.
  76. *
  77. * @var string
  78. * @access public
  79. */
  80. public $command = null;
  81. /**
  82. * The name of the shell in camelized.
  83. *
  84. * @var string
  85. * @access public
  86. */
  87. public $name = null;
  88. /**
  89. * Constructs this Shell instance.
  90. *
  91. */
  92. public function __construct(&$dispatch) {
  93. $vars = array('params', 'args', 'shell', 'shellCommand'=> 'command');
  94. foreach ($vars as $key => $var) {
  95. if (is_string($key)) {
  96. $this->{$var} =& $dispatch->{$key};
  97. } else {
  98. $this->{$var} =& $dispatch->{$var};
  99. }
  100. }
  101. $this->className = get_class($this);
  102. if ($this->name == null) {
  103. $this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
  104. }
  105. $this->Dispatch =& $dispatch;
  106. }
  107. /**
  108. * Starts up the the Shell
  109. * allows for checking and configuring prior to command or main execution
  110. * can be overriden in subclasses
  111. *
  112. * @access public
  113. */
  114. public function startup() {
  115. if (empty($this->params['q'])) {
  116. $this->_welcome();
  117. }
  118. }
  119. /**
  120. * Displays a header for the shell
  121. *
  122. * @access protected
  123. */
  124. public function _welcome() {
  125. $this->out("\nWelcome to Postfixadmin-CLI v" . $this->Dispatch->version);
  126. $this->hr();
  127. }
  128. /**
  129. * Prompts the user for input, and returns it.
  130. *
  131. * @param string $prompt Prompt text.
  132. * @param mixed $options Array or string of options.
  133. * @param string $default Default input value.
  134. * @return Either the default value, or the user-provided input.
  135. * @access public
  136. */
  137. public function in($prompt, $options = null, $default = null) {
  138. if (!$this->interactive) {
  139. return $default;
  140. }
  141. if ($prompt != '') {
  142. $this->out("");
  143. }
  144. $in = $this->Dispatch->getInput($prompt, $options, $default);
  145. if ($options && is_string($options)) {
  146. if (strpos($options, ',')) {
  147. $options = explode(',', $options);
  148. } elseif (strpos($options, '/')) {
  149. $options = explode('/', $options);
  150. } else {
  151. $options = array($options);
  152. }
  153. }
  154. if (is_array($options)) {
  155. while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
  156. $this->err("Invalid input"); # TODO: make translateable
  157. $in = $this->Dispatch->getInput($prompt, $options, $default);
  158. }
  159. }
  160. return $in;
  161. }
  162. /**
  163. * Outputs to the stdout filehandle.
  164. *
  165. * @param string $string String to output.
  166. * @param boolean $newline If true, the outputs gets an added newline.
  167. * @access public
  168. */
  169. public function out($string, $newline = true) {
  170. if (is_array($string)) {
  171. $str = '';
  172. foreach ($string as $message) {
  173. $str .= $message ."\n";
  174. }
  175. $string = $str;
  176. }
  177. return $this->Dispatch->stdout($string, $newline);
  178. }
  179. /**
  180. * Outputs to the stderr filehandle.
  181. *
  182. * @param string $string Error text to output.
  183. * @access public
  184. */
  185. public function err($string) {
  186. if (is_array($string)) {
  187. $str = '';
  188. foreach ($string as $message) {
  189. $str .= $message ."\n";
  190. }
  191. $string = $str;
  192. }
  193. return $this->Dispatch->stderr($string."\n");
  194. }
  195. /**
  196. * Outputs a series of minus characters to the standard output, acts as a visual separator.
  197. *
  198. * @param boolean $newline If true, the outputs gets an added newline.
  199. * @access public
  200. */
  201. public function hr($newline = false) {
  202. if ($newline) {
  203. $this->out("\n");
  204. }
  205. $this->out('---------------------------------------------------------------');
  206. if ($newline) {
  207. $this->out("\n");
  208. }
  209. }
  210. /**
  211. * Displays a formatted error message and exits the application
  212. *
  213. * @param string $title Title of the error message
  214. * @param string $msg Error message
  215. * @access public
  216. */
  217. public function error($title, $msg) {
  218. $out = "$title\n";
  219. $out .= "$msg\n";
  220. $out .= "\n";
  221. $this->err($out);
  222. $this->_stop(1);
  223. }
  224. /**
  225. * Outputs usage text on the standard output. Implement it in subclasses.
  226. *
  227. * @access public
  228. */
  229. public function help() {
  230. if ($this->command != null) {
  231. $this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
  232. } else {
  233. $this->Dispatch->help();
  234. }
  235. }
  236. /**
  237. * Stop execution of the current script
  238. *
  239. * @param $status see http://php.net/exit for values
  240. * @return void
  241. * @access public
  242. */
  243. public function _stop($status = 0) {
  244. exit($status);
  245. }
  246. }