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.

shell.php 7.2KB

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