PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
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.

301 lines
8.7 KiB

  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. * Holds the DATABASE_CONFIG object for the app. Null if database.php could not be found,
  48. * or the app does not exist.
  49. *
  50. * @var object
  51. * @access public
  52. */
  53. var $DbConfig = null;
  54. /**
  55. * Contains command switches parsed from the command line.
  56. *
  57. * @var array
  58. * @access public
  59. */
  60. var $params = array();
  61. /**
  62. * Contains arguments parsed from the command line.
  63. *
  64. * @var array
  65. * @access public
  66. */
  67. var $args = array();
  68. /**
  69. * The file name of the shell that was invoked.
  70. *
  71. * @var string
  72. * @access public
  73. */
  74. var $shell = null;
  75. /**
  76. * The class name of the shell that was invoked.
  77. *
  78. * @var string
  79. * @access public
  80. */
  81. var $className = null;
  82. /**
  83. * The command called if public methods are available.
  84. *
  85. * @var string
  86. * @access public
  87. */
  88. var $command = null;
  89. /**
  90. * The name of the shell in camelized.
  91. *
  92. * @var string
  93. * @access public
  94. */
  95. var $name = null;
  96. /**
  97. * Contains tasks to load and instantiate
  98. *
  99. * @var array
  100. * @access public
  101. */
  102. var $tasks = array();
  103. /**
  104. * Contains models to load and instantiate
  105. *
  106. * @var array
  107. * @access public
  108. */
  109. var $uses = array();
  110. /**
  111. * Constructs this Shell instance.
  112. *
  113. */
  114. function __construct(&$dispatch) {
  115. $vars = array('params', 'args', 'shell', 'shellCommand'=> 'command');
  116. foreach ($vars as $key => $var) {
  117. if (is_string($key)) {
  118. $this->{$var} =& $dispatch->{$key};
  119. } else {
  120. $this->{$var} =& $dispatch->{$var};
  121. }
  122. }
  123. $this->className = get_class($this);
  124. if ($this->name == null) {
  125. $this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
  126. }
  127. $shellKey = Inflector::underscore($this->className);
  128. # if (!PHP5 && isset($this->args[0])) {
  129. # if(strpos($this->className, strtolower(Inflector::camelize($this->args[0]))) !== false) {
  130. # $dispatch->shiftArgs();
  131. # }
  132. # if (strtolower($this->command) == strtolower(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) {
  133. # $dispatch->shiftArgs();
  134. # }
  135. # }
  136. $this->Dispatch =& $dispatch;
  137. }
  138. /**
  139. * Initializes the Shell
  140. * acts as constructor for subclasses
  141. * allows configuration of tasks prior to shell execution
  142. *
  143. * @access public
  144. */
  145. function initialize() {
  146. }
  147. /**
  148. * Starts up the the Shell
  149. * allows for checking and configuring prior to command or main execution
  150. * can be overriden in subclasses
  151. *
  152. * @access public
  153. */
  154. function startup() {
  155. #CHECK!
  156. if ( empty($this->params['q'] ) ) {
  157. $this->_welcome();
  158. }
  159. $CONF = Config::read('all');
  160. }
  161. /**
  162. * Displays a header for the shell
  163. *
  164. * @access protected
  165. */
  166. function _welcome() {
  167. $this->out("\nWelcome to Postfixadmin-CLI v" . $this->Dispatch->version);
  168. $this->out("---------------------------------------------------------------");
  169. $this->out('Path: '. PATH);
  170. $this->hr();
  171. }
  172. /**
  173. * Prompts the user for input, and returns it.
  174. *
  175. * @param string $prompt Prompt text.
  176. * @param mixed $options Array or string of options.
  177. * @param string $default Default input value.
  178. * @return Either the default value, or the user-provided input.
  179. * @access public
  180. */
  181. function in($prompt, $options = null, $default = null) {
  182. if (!$this->interactive) {
  183. return $default;
  184. }
  185. if ($prompt != '') $this->out("");
  186. $in = $this->Dispatch->getInput($prompt, $options, $default);
  187. if ($options && is_string($options)) {
  188. if (strpos($options, ',')) {
  189. $options = explode(',', $options);
  190. } elseif (strpos($options, '/')) {
  191. $options = explode('/', $options);
  192. } else {
  193. $options = array($options);
  194. }
  195. }
  196. if (is_array($options)) {
  197. while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
  198. $this->err("Invalid input"); # TODO: make translateable
  199. $in = $this->Dispatch->getInput($prompt, $options, $default);
  200. }
  201. }
  202. return $in;
  203. }
  204. /**
  205. * Outputs to the stdout filehandle.
  206. *
  207. * @param string $string String to output.
  208. * @param boolean $newline If true, the outputs gets an added newline.
  209. * @access public
  210. */
  211. function out($string, $newline = true) {
  212. if (is_array($string)) {
  213. $str = '';
  214. foreach($string as $message) {
  215. $str .= $message ."\n";
  216. }
  217. $string = $str;
  218. }
  219. return $this->Dispatch->stdout($string, $newline);
  220. }
  221. /**
  222. * Outputs to the stderr filehandle.
  223. *
  224. * @param string $string Error text to output.
  225. * @access public
  226. */
  227. function err($string) {
  228. if (is_array($string)) {
  229. $str = '';
  230. foreach($string as $message) {
  231. $str .= $message ."\n";
  232. }
  233. $string = $str;
  234. }
  235. return $this->Dispatch->stderr($string."\n");
  236. }
  237. /**
  238. * Outputs a series of minus characters to the standard output, acts as a visual separator.
  239. *
  240. * @param boolean $newline If true, the outputs gets an added newline.
  241. * @access public
  242. */
  243. function hr($newline = false) {
  244. if ($newline) {
  245. $this->out("\n");
  246. }
  247. $this->out('---------------------------------------------------------------');
  248. if ($newline) {
  249. $this->out("\n");
  250. }
  251. }
  252. /**
  253. * Displays a formatted error message and exits the application
  254. *
  255. * @param string $title Title of the error message
  256. * @param string $msg Error message
  257. * @access public
  258. */
  259. function error($title, $msg) {
  260. $out = "$title\n";
  261. $out .= "$msg\n";
  262. $out .= "\n";
  263. $this->err($out);
  264. $this->_stop(1);
  265. }
  266. /**
  267. * Outputs usage text on the standard output. Implement it in subclasses.
  268. *
  269. * @access public
  270. */
  271. function help() {
  272. if ($this->command != null) {
  273. $this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
  274. } else {
  275. $this->Dispatch->help();
  276. }
  277. }
  278. /**
  279. * Stop execution of the current script
  280. *
  281. * @param $status see http://php.net/exit for values
  282. * @return void
  283. * @access public
  284. */
  285. function _stop($status = 0) {
  286. exit($status);
  287. }
  288. }