mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2024-09-19 19:22:14 +02:00
reforamt cli commands; update code to php v5 syntax; remove regexp and use filter_var for email validation; use private/protected/public
git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1840 a1433add-5e2c-0410-b055-b7f2511e0802
This commit is contained in:
parent
a00e8a811d
commit
54603b0968
@ -33,510 +33,436 @@
|
||||
|
||||
|
||||
class PostfixAdmin {
|
||||
/**
|
||||
* Version
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
var $version ='0.2';
|
||||
/**
|
||||
* Version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $version ='0.2';
|
||||
|
||||
/**
|
||||
* Standard input stream.
|
||||
*
|
||||
* @var filehandle
|
||||
* @access public
|
||||
*/
|
||||
var $stdin;
|
||||
/**
|
||||
* Standard output stream.
|
||||
*
|
||||
* @var filehandle
|
||||
* @access public
|
||||
*/
|
||||
var $stdout;
|
||||
/**
|
||||
* Standard error stream.
|
||||
*
|
||||
* @var filehandle
|
||||
* @access public
|
||||
*/
|
||||
var $stderr;
|
||||
/**
|
||||
* Contains command switches parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $params = array();
|
||||
/**
|
||||
* Contains arguments parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $args = array();
|
||||
/**
|
||||
* The file name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $shell = null;
|
||||
/**
|
||||
* The class name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $shellClass = null;
|
||||
/**
|
||||
* The command called if public methods are available.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $shellCommand = null;
|
||||
/**
|
||||
* The name of the shell in camelized.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $shellName = null;
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $args the argv.
|
||||
*/
|
||||
function __construct($args = array()) {
|
||||
set_time_limit(0);
|
||||
$this->__initConstants();
|
||||
$this->parseParams($args);
|
||||
$this->__initEnvironment();
|
||||
/*$this->dispatch();
|
||||
die("\n");*/
|
||||
/**
|
||||
* Standard input stream.
|
||||
*
|
||||
* @var filehandle
|
||||
*/
|
||||
public $stdin;
|
||||
|
||||
/**
|
||||
* Standard output stream.
|
||||
*
|
||||
* @var filehandle
|
||||
*/
|
||||
public $stdout;
|
||||
|
||||
/**
|
||||
* Standard error stream.
|
||||
*
|
||||
* @var filehandle
|
||||
*/
|
||||
public $stderr;
|
||||
|
||||
/**
|
||||
* Contains command switches parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $params = array();
|
||||
|
||||
/**
|
||||
* Contains arguments parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $args = array();
|
||||
|
||||
/**
|
||||
* The file name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $shell = null;
|
||||
|
||||
/**
|
||||
* The class name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $shellClass = null;
|
||||
|
||||
/**
|
||||
* The command called if public methods are available.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $shellCommand = null;
|
||||
|
||||
/**
|
||||
* The name of the shell in camelized.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $shellName = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $args the argv.
|
||||
*/
|
||||
public function __construct($args = array()) {
|
||||
set_time_limit(0);
|
||||
$this->__initConstants();
|
||||
$this->parseParams($args);
|
||||
$this->__initEnvironment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines core configuration.
|
||||
*/
|
||||
private function __initConstants() {
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('error_reporting', E_ALL);
|
||||
ini_set('html_errors', false);
|
||||
ini_set('implicit_flush', true);
|
||||
ini_set('max_execution_time', 0);
|
||||
|
||||
define('DS', DIRECTORY_SEPARATOR);
|
||||
define('CORE_INCLUDE_PATH', dirname(__FILE__));
|
||||
define('CORE_PATH', dirname(CORE_INCLUDE_PATH) ); # CORE_INCLUDE_PATH/../
|
||||
|
||||
if(!defined('POSTFIXADMIN')) { # already defined if called from setup.php
|
||||
define('POSTFIXADMIN', 1); # checked in included files
|
||||
}
|
||||
/**
|
||||
* Defines core configuration.
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function __initConstants() {
|
||||
if (function_exists('ini_set')) {
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('error_reporting', E_ALL);
|
||||
ini_set('html_errors', false);
|
||||
ini_set('implicit_flush', true);
|
||||
ini_set('max_execution_time', 0);
|
||||
}
|
||||
}
|
||||
|
||||
define('DS', DIRECTORY_SEPARATOR);
|
||||
define('CORE_INCLUDE_PATH', dirname(__FILE__));
|
||||
define('CORE_PATH', dirname(CORE_INCLUDE_PATH) ); # CORE_INCLUDE_PATH/../
|
||||
/**
|
||||
* Defines current working environment.
|
||||
*/
|
||||
private function __initEnvironment() {
|
||||
$this->stdin = fopen('php://stdin', 'r');
|
||||
$this->stdout = fopen('php://stdout', 'w');
|
||||
$this->stderr = fopen('php://stderr', 'w');
|
||||
|
||||
if(!defined('POSTFIXADMIN')) { # already defined if called from setup.php
|
||||
define('POSTFIXADMIN', 1); # checked in included files
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* Defines current working environment.
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function __initEnvironment() {
|
||||
$this->stdin = fopen('php://stdin', 'r');
|
||||
$this->stdout = fopen('php://stdout', 'w');
|
||||
$this->stderr = fopen('php://stderr', 'w');
|
||||
|
||||
if (!$this->__bootstrap()) {
|
||||
$this->stderr("");
|
||||
$this->stderr("Unable to load.");
|
||||
$this->stderr("\tMake sure /config.inc.php exists in " . PATH);
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
if (basename(__FILE__) != basename($this->args[0])) {
|
||||
$this->stderr('Warning: the dispatcher may have been loaded incorrectly, which could lead to unexpected results...');
|
||||
if ($this->getInput('Continue anyway?', array('y', 'n'), 'y') == 'n') {
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
$this->shiftArgs();
|
||||
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* Initializes the environment and loads the Cake core.
|
||||
*
|
||||
* @return boolean Success.
|
||||
* @access private
|
||||
*/
|
||||
function __bootstrap() {
|
||||
if ($this->params['webroot'] != '' ) {
|
||||
define('PATH', $this->params['webroot'] );
|
||||
} else {
|
||||
define('PATH', CORE_PATH);
|
||||
}
|
||||
if (!file_exists(PATH)) {
|
||||
$this->stderr( PATH . " don't exists");
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if (!require_once(PATH . '/common.php')) {
|
||||
$this->stderr("Failed to load " . PATH . '/common.php');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (!$this->__bootstrap()) {
|
||||
$this->stderr("");
|
||||
$this->stderr("Unable to load.");
|
||||
$this->stderr("\tMake sure /config.inc.php exists in " . PATH);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a CLI request
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function dispatch() {
|
||||
|
||||
if (basename(__FILE__) != basename($this->args[0])) {
|
||||
$this->stderr('Warning: the dispatcher may have been loaded incorrectly, which could lead to unexpected results...');
|
||||
if ($this->getInput('Continue anyway?', array('y', 'n'), 'y') == 'n') {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
$this->shiftArgs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the environment and loads the Cake core.
|
||||
*
|
||||
* @return boolean Success.
|
||||
*/
|
||||
private function __bootstrap() {
|
||||
if ($this->params['webroot'] != '' ) {
|
||||
define('PATH', $this->params['webroot'] );
|
||||
} else {
|
||||
define('PATH', CORE_PATH);
|
||||
}
|
||||
|
||||
if (!file_exists(PATH)) {
|
||||
$this->stderr( PATH . " don't exists");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!require_once(PATH . '/common.php')) {
|
||||
$this->stderr("Failed to load " . PATH . '/common.php');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a CLI request
|
||||
*/
|
||||
public function dispatch() {
|
||||
$CONF = Config::read('all');
|
||||
|
||||
if (!isset($this->args[0])) {
|
||||
$this->help();
|
||||
return;
|
||||
}
|
||||
if (!isset($this->args[0])) {
|
||||
$this->help();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->shell = $this->args[0];
|
||||
$this->shiftArgs();
|
||||
$this->shellName = ucfirst($this->shell);
|
||||
$this->shellClass = $this->shellName . 'Handler';
|
||||
$this->shell = $this->args[0];
|
||||
$this->shiftArgs();
|
||||
$this->shellName = ucfirst($this->shell);
|
||||
$this->shellClass = $this->shellName . 'Handler';
|
||||
|
||||
|
||||
if ($this->shell == 'help') {
|
||||
$this->help();
|
||||
return;
|
||||
}
|
||||
# TODO: move shells/shell.php to model/ to enable autoloading
|
||||
if (!class_exists('Shell')) {
|
||||
require CORE_INCLUDE_PATH . DS . "shells" . DS . 'shell.php';
|
||||
}
|
||||
$command = 'help'; # not the worst default ;-)
|
||||
if (isset($this->args[0])) {
|
||||
$command = $this->args[0];
|
||||
}
|
||||
if ($this->shell == 'help') {
|
||||
$this->help();
|
||||
return;
|
||||
}
|
||||
# TODO: move shells/shell.php to model/ to enable autoloading
|
||||
if (!class_exists('Shell')) {
|
||||
require CORE_INCLUDE_PATH . DS . "shells" . DS . 'shell.php';
|
||||
}
|
||||
$command = 'help'; # not the worst default ;-)
|
||||
if (isset($this->args[0])) {
|
||||
$command = $this->args[0];
|
||||
}
|
||||
|
||||
$this->shellCommand = $command;
|
||||
$this->shellClass = 'Cli' . ucfirst($command);
|
||||
$this->shellCommand = $command;
|
||||
$this->shellClass = 'Cli' . ucfirst($command);
|
||||
|
||||
if (ucfirst($command) == 'Add' || ucfirst($command) == 'Update') {
|
||||
$this->shellClass = 'CliEdit';
|
||||
}
|
||||
if (ucfirst($command) == 'Add' || ucfirst($command) == 'Update') {
|
||||
$this->shellClass = 'CliEdit';
|
||||
}
|
||||
|
||||
if (!class_exists($this->shellClass)) {
|
||||
$this->stderr('Unknown task ' . $this->shellCommand);
|
||||
if (!class_exists($this->shellClass)) {
|
||||
$this->stderr('Unknown task ' . $this->shellCommand);
|
||||
return;
|
||||
}
|
||||
|
||||
$shell = new $this->shellClass($this);
|
||||
|
||||
$shell->handler_to_use = ucfirst($this->shell) . 'Handler';
|
||||
|
||||
if (!class_exists($shell->handler_to_use)) {
|
||||
$this->stderr('Unknown module ' . $this->shell);
|
||||
return;
|
||||
}
|
||||
|
||||
$task = ucfirst($command);
|
||||
|
||||
$shell->new = 0;
|
||||
if ($task == 'Add') {
|
||||
$shell->new = 1;
|
||||
}
|
||||
|
||||
# TODO: add a way to Cli* to signal if the selected handler is supported (for example, not all *Handler support changing the password)
|
||||
|
||||
if (strtolower(get_parent_class($shell)) == 'shell') {
|
||||
$shell->initialize();
|
||||
|
||||
$handler = new $shell->handler_to_use;
|
||||
if (in_array($task, $handler->taskNames)) {
|
||||
$this->shiftArgs();
|
||||
$shell->startup();
|
||||
|
||||
if (isset($this->args[0]) && $this->args[0] == 'help') {
|
||||
if (method_exists($shell, 'help')) {
|
||||
$shell->help();
|
||||
exit();
|
||||
} else {
|
||||
$this->help();
|
||||
}
|
||||
}
|
||||
|
||||
$shell->execute();
|
||||
return;
|
||||
}
|
||||
|
||||
$shell = new $this->shellClass($this);
|
||||
|
||||
$shell->handler_to_use = ucfirst($this->shell) . 'Handler';
|
||||
|
||||
if (!class_exists($shell->handler_to_use)) {
|
||||
$this->stderr('Unknown module ' . $this->shell);
|
||||
return;
|
||||
}
|
||||
|
||||
$task = ucfirst($command);
|
||||
|
||||
$shell->new = 0;
|
||||
if ($task == 'Add') {
|
||||
$shell->new = 1;
|
||||
}
|
||||
|
||||
# TODO: add a way to Cli* to signal if the selected handler is supported (for example, not all *Handler support changing the password)
|
||||
|
||||
if (strtolower(get_parent_class($shell)) == 'shell') {
|
||||
$shell->initialize();
|
||||
|
||||
$handler = new $shell->handler_to_use;
|
||||
if (in_array($task, $handler->taskNames)) {
|
||||
$this->shiftArgs();
|
||||
$shell->startup();
|
||||
|
||||
|
||||
if (isset($this->args[0]) && $this->args[0] == 'help') {
|
||||
if (method_exists($shell, 'help')) {
|
||||
$shell->help();
|
||||
exit();
|
||||
} else {
|
||||
$this->help();
|
||||
}
|
||||
}
|
||||
|
||||
$shell->execute();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$classMethods = get_class_methods($shell);
|
||||
|
||||
$privateMethod = $missingCommand = false;
|
||||
if ((in_array($command, $classMethods) || in_array(strtolower($command), $classMethods)) && strpos($command, '_', 0) === 0) {
|
||||
$privateMethod = true;
|
||||
}
|
||||
|
||||
if (!in_array($command, $classMethods) && !in_array(strtolower($command), $classMethods)) {
|
||||
$missingCommand = true;
|
||||
}
|
||||
|
||||
$protectedCommands = array(
|
||||
'initialize','in','out','err','hr',
|
||||
'createfile', 'isdir','copydir','object','tostring',
|
||||
'requestaction','log','cakeerror', 'shelldispatcher',
|
||||
'__initconstants','__initenvironment','__construct',
|
||||
'dispatch','__bootstrap','getinput','stdout','stderr','parseparams','shiftargs'
|
||||
);
|
||||
|
||||
if (in_array(strtolower($command), $protectedCommands)) {
|
||||
$missingCommand = true;
|
||||
}
|
||||
|
||||
if ($missingCommand && method_exists($shell, 'main')) {
|
||||
$shell->startup();
|
||||
$shell->main();
|
||||
} elseif (!$privateMethod && method_exists($shell, $command)) {
|
||||
$this->shiftArgs();
|
||||
$shell->startup();
|
||||
$shell->{$command}();
|
||||
} else {
|
||||
$this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompts the user for input, and returns it.
|
||||
*
|
||||
* @param string $prompt Prompt text.
|
||||
* @param mixed $options Array or string of options.
|
||||
* @param string $default Default input value.
|
||||
* @return Either the default value, or the user-provided input.
|
||||
* @access public
|
||||
*/
|
||||
function getInput($prompt, $options = null, $default = null) {
|
||||
if (!is_array($options)) {
|
||||
$print_options = '';
|
||||
} else {
|
||||
$print_options = '(' . implode('/', $options) . ')';
|
||||
}
|
||||
$classMethods = get_class_methods($shell);
|
||||
|
||||
if ($default == null) {
|
||||
$this->stdout($prompt . " $print_options \n" . '> ', false);
|
||||
} else {
|
||||
$this->stdout($prompt . " $print_options \n" . "[$default] > ", false);
|
||||
}
|
||||
$result = fgets($this->stdin);
|
||||
|
||||
if ($result === false){
|
||||
exit;
|
||||
}
|
||||
$result = trim($result);
|
||||
|
||||
if ($default != null && empty($result)) {
|
||||
return $default;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Outputs to the stdout filehandle.
|
||||
*
|
||||
* @param string $string String to output.
|
||||
* @param boolean $newline If true, the outputs gets an added newline.
|
||||
* @access public
|
||||
*/
|
||||
function stdout($string, $newline = true) {
|
||||
if ($newline) {
|
||||
fwrite($this->stdout, $string . "\n");
|
||||
} else {
|
||||
fwrite($this->stdout, $string);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Outputs to the stderr filehandle.
|
||||
*
|
||||
* @param string $string Error text to output.
|
||||
* @access public
|
||||
*/
|
||||
function stderr($string) {
|
||||
fwrite($this->stderr, 'Error: '. $string . "\n");
|
||||
$privateMethod = $missingCommand = false;
|
||||
if ((in_array($command, $classMethods) || in_array(strtolower($command), $classMethods)) && strpos($command, '_', 0) === 0) {
|
||||
$privateMethod = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses command line options
|
||||
*
|
||||
* @param array $params Parameters to parse
|
||||
* @access public
|
||||
*/
|
||||
function parseParams($params) {
|
||||
$this->__parseParams($params);
|
||||
|
||||
$defaults = array('webroot' => CORE_PATH);
|
||||
|
||||
$params = array_merge($defaults, array_intersect_key($this->params, $defaults));
|
||||
|
||||
$isWin = array_filter(array_map('strpos', $params, array('\\')));
|
||||
|
||||
$params = str_replace('\\', '/', $params);
|
||||
|
||||
|
||||
if (!empty($matches[0]) || !empty($isWin)) {
|
||||
$params = str_replace('/', '\\', $params);
|
||||
}
|
||||
|
||||
$this->params = array_merge($this->params, $params);
|
||||
}
|
||||
/**
|
||||
* Helper for recursively paraing params
|
||||
*
|
||||
* @return array params
|
||||
* @access private
|
||||
*/
|
||||
function __parseParams($params) {
|
||||
$count = count($params);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if (isset($params[$i])) {
|
||||
if ($params[$i] != '' && $params[$i]{0} === '-') {
|
||||
$key = substr($params[$i], 1);
|
||||
$this->params[$key] = true;
|
||||
unset($params[$i]);
|
||||
if (isset($params[++$i])) {
|
||||
# TODO: ideally we should know if a parameter can / must have a value instead of whitelisting known valid values starting with '-' (probably only bool doesn't need a value)
|
||||
if ($params[$i]{0} !== '-' or $params[$i] != '-1') {
|
||||
$this->params[$key] = $params[$i];
|
||||
unset($params[$i]);
|
||||
} else {
|
||||
$i--;
|
||||
$this->__parseParams($params);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->args[] = $params[$i];
|
||||
unset($params[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Removes first argument and shifts other arguments up
|
||||
*
|
||||
* @return boolean False if there are no arguments
|
||||
* @access public
|
||||
*/
|
||||
function shiftArgs() {
|
||||
if (empty($this->args)) {
|
||||
return false;
|
||||
}
|
||||
unset($this->args[0]);
|
||||
$this->args = array_values($this->args);
|
||||
return true;
|
||||
if (!in_array($command, $classMethods) && !in_array(strtolower($command), $classMethods)) {
|
||||
$missingCommand = true;
|
||||
}
|
||||
|
||||
function help() {
|
||||
$this->stdout("\nWelcome to Postfixadmin-CLI v" . $this->version);
|
||||
$this->stdout("---------------------------------------------------------------");
|
||||
/* users shouldn't need to specify -webroot - therefore let's "hide" it ;-)
|
||||
$this->stdout("Options:");
|
||||
$this->stdout(" -webroot: " . $this->params['webroot']);
|
||||
$this->stdout("");
|
||||
$this->stdout("Changing Paths:");
|
||||
$this->stdout("your webroot should be the same as your postfixadmin path");
|
||||
$this->stdout("to change your path use the '-webroot' param.");
|
||||
$this->stdout("Example: -webroot r/absolute/path/to/postfixadmin");
|
||||
*/
|
||||
$protectedCommands = array(
|
||||
'initialize','in','out','err','hr',
|
||||
'createfile', 'isdir','copydir','object','tostring',
|
||||
'requestaction','log','cakeerror', 'shelldispatcher',
|
||||
'__initconstants','__initenvironment','__construct',
|
||||
'dispatch','__bootstrap','getinput','stdout','stderr','parseparams','shiftargs'
|
||||
);
|
||||
|
||||
$this->stdout("Usage:");
|
||||
$this->stdout(" postfixadmin-cli <module> <task> [--option value --option2 value]");
|
||||
$this->stdout("");
|
||||
$this->stdout("Available modules:");
|
||||
if (in_array(strtolower($command), $protectedCommands)) {
|
||||
$missingCommand = true;
|
||||
}
|
||||
|
||||
$modules = explode(',','admin,domain,mailbox,alias,aliasdomain,fetchmail');
|
||||
foreach ($modules as $module) {
|
||||
$this->stdout(" $module");
|
||||
}
|
||||
$this->stdout("");
|
||||
$this->stdout("Most modules support the following tasks:");
|
||||
$this->stdout(" view View an item");
|
||||
$this->stdout(" add Add an item");
|
||||
$this->stdout(" update Update an item");
|
||||
$this->stdout(" delete Delete an item");
|
||||
$this->stdout(" scheme Print database scheme (useful for developers only)");
|
||||
$this->stdout(" help Print help output");
|
||||
$this->stdout("");
|
||||
$this->stdout("");
|
||||
$this->stdout("For module-specific help, see:");
|
||||
$this->stdout("");
|
||||
$this->stdout(" postfixadmin-cli <module> help");
|
||||
$this->stdout(" print a detailed list of available commands");
|
||||
$this->stdout("");
|
||||
$this->stdout(" postfixadmin-cli <module> <task> help");
|
||||
$this->stdout(" print a list of available options.");
|
||||
$this->stdout("");
|
||||
if ($missingCommand && method_exists($shell, 'main')) {
|
||||
$shell->startup();
|
||||
$shell->main();
|
||||
} elseif (!$privateMethod && method_exists($shell, $command)) {
|
||||
$this->shiftArgs();
|
||||
$shell->startup();
|
||||
$shell->{$command}();
|
||||
} else {
|
||||
$this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
$this->stdout("\nAvailable Commands:");
|
||||
foreach ($this->commands() AS $command => $desc) {
|
||||
if (is_array($desc)) {
|
||||
$this->stdout($command . ":");
|
||||
foreach($desc AS $command2 => $desc2) {
|
||||
$this->stdout(sprintf("%-20s %s", " ".$command2 .": ", $desc2));
|
||||
}
|
||||
$this->stdout("");
|
||||
/**
|
||||
* Prompts the user for input, and returns it.
|
||||
*
|
||||
* @param string $prompt Prompt text.
|
||||
* @param mixed $options Array or string of options.
|
||||
* @param string $default Default input value.
|
||||
* @return Either the default value, or the user-provided input.
|
||||
*/
|
||||
public function getInput($prompt, $options = null, $default = null) {
|
||||
if (!is_array($options)) {
|
||||
$print_options = '';
|
||||
} else {
|
||||
$print_options = '(' . implode('/', $options) . ')';
|
||||
}
|
||||
|
||||
if ($default == null) {
|
||||
$this->stdout($prompt . " $print_options \n" . '> ', false);
|
||||
} else {
|
||||
$this->stdout($prompt . " $print_options \n" . "[$default] > ", false);
|
||||
}
|
||||
$result = fgets($this->stdin);
|
||||
|
||||
if ($result === false){
|
||||
exit(1);
|
||||
}
|
||||
$result = trim($result);
|
||||
|
||||
if ($default != null && empty($result)) {
|
||||
return $default;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs to the stdout filehandle.
|
||||
*
|
||||
* @param string $string String to output.
|
||||
* @param boolean $newline If true, the outputs gets an added newline.
|
||||
*/
|
||||
public function stdout($string, $newline = true) {
|
||||
if ($newline) {
|
||||
fwrite($this->stdout, $string . "\n");
|
||||
} else {
|
||||
fwrite($this->stdout, $string);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs to the stderr filehandle.
|
||||
*
|
||||
* @param string $string Error text to output.
|
||||
*/
|
||||
public function stderr($string) {
|
||||
fwrite($this->stderr, 'Error: '. $string . "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses command line options
|
||||
*
|
||||
* @param array $params Parameters to parse
|
||||
*/
|
||||
public function parseParams($params) {
|
||||
$this->__parseParams($params);
|
||||
|
||||
$defaults = array('webroot' => CORE_PATH);
|
||||
|
||||
$params = array_merge($defaults, array_intersect_key($this->params, $defaults));
|
||||
|
||||
$isWin = array_filter(array_map('strpos', $params, array('\\')));
|
||||
|
||||
$params = str_replace('\\', '/', $params);
|
||||
|
||||
|
||||
if (!empty($matches[0]) || !empty($isWin)) {
|
||||
$params = str_replace('/', '\\', $params);
|
||||
}
|
||||
|
||||
$this->params = array_merge($this->params, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for recursively paraing params
|
||||
*
|
||||
* @return array params
|
||||
*/
|
||||
private function __parseParams($params) {
|
||||
$count = count($params);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if (isset($params[$i])) {
|
||||
if ($params[$i] != '' && $params[$i]{0} === '-') {
|
||||
$key = substr($params[$i], 1);
|
||||
$this->params[$key] = true;
|
||||
unset($params[$i]);
|
||||
if (isset($params[++$i])) {
|
||||
# TODO: ideally we should know if a parameter can / must have a value instead of whitelisting known valid values starting with '-' (probably only bool doesn't need a value)
|
||||
if ($params[$i]{0} !== '-' or $params[$i] != '-1') {
|
||||
$this->params[$key] = $params[$i];
|
||||
unset($params[$i]);
|
||||
} else {
|
||||
$this->stdout(sprintf("%-20s %s", $command .": ", $desc));
|
||||
$i--;
|
||||
$this->__parseParams($params);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->args[] = $params[$i];
|
||||
unset($params[$i]);
|
||||
}
|
||||
$this->stdout("\nTo run a command, type 'postfixadmin-cli command [args]'");
|
||||
$this->stdout("To get help on a specific command, type 'postfixadmin-cli command help'");
|
||||
*/
|
||||
exit();
|
||||
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Removes first argument and shifts other arguments up
|
||||
*
|
||||
* @return array List of commands
|
||||
* @access public
|
||||
*/
|
||||
/* currently unused (and outdated)
|
||||
function commands() {
|
||||
|
||||
|
||||
# TODO: this list is incomplete
|
||||
return array(
|
||||
'mailbox' => array(
|
||||
'add'=> 'Adds a new mailbox.',
|
||||
'update'=> 'Updates a mailbox.',
|
||||
'delete' => 'Deletes a mailbox.',
|
||||
'pw' => 'Changes the PW for a mailbox.',
|
||||
),
|
||||
'alias' => array(
|
||||
'add' => 'Adds a new alias.',
|
||||
'update' => 'Updates a alias.',
|
||||
'delete' => 'Deletes a alias.',
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes first argument and shifts other arguments up
|
||||
*
|
||||
* @return boolean False if there are no arguments
|
||||
*/
|
||||
public function shiftArgs() {
|
||||
if (empty($this->args)) {
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
unset($this->args[0]);
|
||||
$this->args = array_values($this->args);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* prints help message and exits.
|
||||
*/
|
||||
public function help() {
|
||||
$this->stdout("\nWelcome to Postfixadmin-CLI v" . $this->version);
|
||||
$this->stdout("---------------------------------------------------------------");
|
||||
$this->stdout("Usage:");
|
||||
$this->stdout(" postfixadmin-cli <module> <task> [--option value --option2 value]");
|
||||
$this->stdout("");
|
||||
$this->stdout("Available modules:");
|
||||
|
||||
$modules = explode(',','admin,domain,mailbox,alias,aliasdomain,fetchmail');
|
||||
foreach ($modules as $module) {
|
||||
$this->stdout(" $module");
|
||||
}
|
||||
$this->stdout("");
|
||||
$this->stdout("Most modules support the following tasks:");
|
||||
$this->stdout(" view View an item");
|
||||
$this->stdout(" add Add an item");
|
||||
$this->stdout(" update Update an item");
|
||||
$this->stdout(" delete Delete an item");
|
||||
$this->stdout(" scheme Print database scheme (useful for developers only)");
|
||||
$this->stdout(" help Print help output");
|
||||
$this->stdout("");
|
||||
$this->stdout("");
|
||||
$this->stdout("For module-specific help, see:");
|
||||
$this->stdout("");
|
||||
$this->stdout(" postfixadmin-cli <module> help");
|
||||
$this->stdout(" print a detailed list of available commands");
|
||||
$this->stdout("");
|
||||
$this->stdout(" postfixadmin-cli <module> <task> help");
|
||||
$this->stdout(" print a list of available options.");
|
||||
$this->stdout("");
|
||||
|
||||
exit();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -549,4 +475,3 @@ $CONF = Config::read('all');
|
||||
$dispatcher->dispatch();
|
||||
|
||||
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|
||||
?>
|
||||
|
@ -2,129 +2,125 @@
|
||||
|
||||
|
||||
class PasswordTask extends Shell {
|
||||
/**
|
||||
* Execution method always used for tasks
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function execute() {
|
||||
$random = false;
|
||||
if (empty($this->args)) {
|
||||
$this->__interactive();
|
||||
}
|
||||
|
||||
if (!empty($this->args[0])) {
|
||||
|
||||
$address = $this->args[0];
|
||||
|
||||
if (isset($this->params['g']) && $this->params['g'] == true ) {
|
||||
$random = true;
|
||||
$password = NULL;
|
||||
} elseif (isset($this->args[1]) && strlen($this->args[1]) > 8) { # TODO use validate_password()
|
||||
$password = $this->args[1];
|
||||
} else {
|
||||
|
||||
$this->Dispatch->stderr('Missing <newpw> or -g. Falling back to interactive mode.');
|
||||
$this->__interactive();
|
||||
}
|
||||
$this->__handle($address, $password, $random);
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* Execution method always used for tasks
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function execute() {
|
||||
$random = false;
|
||||
if (empty($this->args)) {
|
||||
$this->__interactive();
|
||||
}
|
||||
/**
|
||||
* Interactive
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function __interactive() {
|
||||
|
||||
while(0==0) {
|
||||
$question = "Which address' password do you want to change?";
|
||||
$address = $this->in($question);
|
||||
|
||||
if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $address) == 1)
|
||||
break;
|
||||
|
||||
$this->err("Invalid emailaddress");
|
||||
|
||||
}
|
||||
|
||||
|
||||
$question2[] = "Do you want to change the password?";
|
||||
$question2[] = "Are you really sure?";
|
||||
$sure = $this->in(join("\n", $question2), array('y','n'));
|
||||
|
||||
|
||||
if ($sure == 'n' ) {
|
||||
$this->out('You\'re not sure.');
|
||||
$this->_stop();
|
||||
}
|
||||
|
||||
$question = "Do you want to generate a random password?";
|
||||
$random = $this->in($question, array('y','n'));
|
||||
|
||||
$random == 'y' ? $random = true : $random = false;
|
||||
if (!empty($this->args[0])) {
|
||||
|
||||
$address = $this->args[0];
|
||||
|
||||
if (isset($this->params['g']) && $this->params['g'] == true ) {
|
||||
$random = true;
|
||||
$password = NULL;
|
||||
if ($random == false) {
|
||||
$question = "Pleas enter the new password?";
|
||||
$password = $this->in($question);
|
||||
}
|
||||
|
||||
$this->__handle($address, $password, $random);
|
||||
|
||||
} elseif (isset($this->args[1]) && strlen($this->args[1]) > 8) { # TODO use validate_password()
|
||||
$password = $this->args[1];
|
||||
} else {
|
||||
|
||||
$this->Dispatch->stderr('Missing <newpw> or -g. Falling back to interactive mode.');
|
||||
$this->__interactive();
|
||||
}
|
||||
$this->__handle($address, $password, $random);
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
* Interactive
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function __handle($address, $password = NULL, $random = false) {
|
||||
}
|
||||
|
||||
if ($random == true) {
|
||||
$password = generate_password();
|
||||
}
|
||||
if ($password != NULL) {
|
||||
$handler = new MailboxHandler();
|
||||
/**
|
||||
* Interactive
|
||||
*/
|
||||
private function __interactive() {
|
||||
|
||||
if (!$handler->init($address)) {
|
||||
$this->error("Change Password",join("\n", $handler->errormsg));
|
||||
}
|
||||
while(true) {
|
||||
$question = "Which address' password do you want to change?";
|
||||
$address = $this->in($question);
|
||||
|
||||
if ( ! $handler->change_pw($password, NULL, false) ){
|
||||
$this->error("Change Password",join("\n", $handler->errormsg));
|
||||
}
|
||||
}
|
||||
|
||||
$this->out("");
|
||||
$this->out("Password updated.");
|
||||
$this->hr();
|
||||
$this->out(sprintf('The Mail address is %20s', $address));
|
||||
$this->out(sprintf('The new password is %20s',$password));
|
||||
$this->hr();
|
||||
|
||||
return ;
|
||||
}
|
||||
/**
|
||||
* Displays help contents
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function help() {
|
||||
$this->out("");
|
||||
$this->hr();
|
||||
$this->out("Usage: postfixadmin-cli mailbox password <address> [<newpw>] [-g]");
|
||||
$this->hr();
|
||||
$this->out('Commands:');
|
||||
$this->out("\n\tpassword\n\t\tchanges the password in interactive mode.");
|
||||
$this->out("\n\tpassword <address> [<newpw>] [-g]\n\t\tchanges the password to <newpw> or if -g genereate a new pw for <address>");
|
||||
$this->out("");
|
||||
$this->_stop();
|
||||
if(filter_var($address, FILTER_VALIDATE_EMAIL)) {
|
||||
break;
|
||||
}
|
||||
$this->err("Invalid emailaddress");
|
||||
}
|
||||
|
||||
|
||||
$question2[] = "Do you want to change the password?";
|
||||
$question2[] = "Are you really sure?";
|
||||
$sure = $this->in(join("\n", $question2), array('y','n'));
|
||||
|
||||
|
||||
if ($sure == 'n' ) {
|
||||
$this->out('You\'re not sure.');
|
||||
$this->_stop();
|
||||
}
|
||||
|
||||
$question = "Do you want to generate a random password?";
|
||||
$random = $this->in($question, array('y','n'));
|
||||
|
||||
$random == 'y' ? $random = true : $random = false;
|
||||
|
||||
|
||||
$password = NULL;
|
||||
if ($random == false) {
|
||||
$question = "Pleas enter the new password?";
|
||||
$password = $this->in($question);
|
||||
}
|
||||
|
||||
$this->__handle($address, $password, $random);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address email adress
|
||||
* @param string $password optional
|
||||
* @param boolean $random optional - true to generate random pw.
|
||||
*/
|
||||
private function __handle($address, $password = NULL, $random = false) {
|
||||
|
||||
if ($random == true) {
|
||||
$password = generate_password();
|
||||
}
|
||||
if ($password != NULL) {
|
||||
$handler = new MailboxHandler();
|
||||
|
||||
if (!$handler->init($address)) {
|
||||
$this->error("Change Password",join("\n", $handler->errormsg));
|
||||
}
|
||||
|
||||
if ( ! $handler->change_pw($password, NULL, false) ){
|
||||
$this->error("Change Password",join("\n", $handler->errormsg));
|
||||
}
|
||||
}
|
||||
|
||||
$this->out("");
|
||||
$this->out("Password updated.");
|
||||
$this->hr();
|
||||
$this->out(sprintf('The Mail address is %20s', $address));
|
||||
$this->out(sprintf('The new password is %20s',$password));
|
||||
$this->hr();
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays help contents
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function help() {
|
||||
$this->out("");
|
||||
$this->hr();
|
||||
$this->out("Usage: postfixadmin-cli mailbox password <address> [<newpw>] [-g]");
|
||||
$this->hr();
|
||||
$this->out('Commands:');
|
||||
$this->out("\n\tpassword\n\t\tchanges the password in interactive mode.");
|
||||
$this->out("\n\tpassword <address> [<newpw>] [-g]\n\t\tchanges the password to <newpw> or if -g genereate a new pw for <address>");
|
||||
$this->out("");
|
||||
$this->_stop();
|
||||
}
|
||||
}
|
||||
|
@ -30,238 +30,238 @@
|
||||
*/
|
||||
|
||||
class Shell {
|
||||
/**
|
||||
* An instance of the ShellDispatcher object that loaded this script
|
||||
*
|
||||
* @var object
|
||||
* @access public
|
||||
*/
|
||||
var $Dispatch = null;
|
||||
/**
|
||||
* If true, the script will ask for permission to perform actions.
|
||||
*
|
||||
* @var boolean
|
||||
* @access public
|
||||
*/
|
||||
var $interactive = true;
|
||||
/**
|
||||
* Contains command switches parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $params = array();
|
||||
/**
|
||||
* Contains arguments parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $args = array();
|
||||
/**
|
||||
* The file name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $shell = null;
|
||||
/**
|
||||
* The class name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $className = null;
|
||||
/**
|
||||
* The command called if public methods are available.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $command = null;
|
||||
/**
|
||||
* The name of the shell in camelized.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $name = null;
|
||||
/**
|
||||
* An instance of the ShellDispatcher object that loaded this script
|
||||
*
|
||||
* @var object
|
||||
* @access public
|
||||
*/
|
||||
var $Dispatch = null;
|
||||
/**
|
||||
* If true, the script will ask for permission to perform actions.
|
||||
*
|
||||
* @var boolean
|
||||
* @access public
|
||||
*/
|
||||
var $interactive = true;
|
||||
/**
|
||||
* Contains command switches parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $params = array();
|
||||
/**
|
||||
* Contains arguments parsed from the command line.
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
var $args = array();
|
||||
/**
|
||||
* The file name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $shell = null;
|
||||
/**
|
||||
* The class name of the shell that was invoked.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $className = null;
|
||||
/**
|
||||
* The command called if public methods are available.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $command = null;
|
||||
/**
|
||||
* The name of the shell in camelized.
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
var $name = null;
|
||||
|
||||
/**
|
||||
* Constructs this Shell instance.
|
||||
*
|
||||
*/
|
||||
function __construct(&$dispatch) {
|
||||
$vars = array('params', 'args', 'shell', 'shellCommand'=> 'command');
|
||||
foreach ($vars as $key => $var) {
|
||||
if (is_string($key)) {
|
||||
$this->{$var} =& $dispatch->{$key};
|
||||
} else {
|
||||
$this->{$var} =& $dispatch->{$var};
|
||||
}
|
||||
}
|
||||
|
||||
$this->className = get_class($this);
|
||||
|
||||
if ($this->name == null) {
|
||||
$this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
|
||||
}
|
||||
|
||||
$this->Dispatch =& $dispatch;
|
||||
/**
|
||||
* Constructs this Shell instance.
|
||||
*
|
||||
*/
|
||||
function __construct(&$dispatch) {
|
||||
$vars = array('params', 'args', 'shell', 'shellCommand'=> 'command');
|
||||
foreach ($vars as $key => $var) {
|
||||
if (is_string($key)) {
|
||||
$this->{$var} =& $dispatch->{$key};
|
||||
} else {
|
||||
$this->{$var} =& $dispatch->{$var};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the Shell
|
||||
* acts as constructor for subclasses
|
||||
* allows configuration of tasks prior to shell execution
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function initialize() {
|
||||
}
|
||||
/**
|
||||
* Starts up the the Shell
|
||||
* allows for checking and configuring prior to command or main execution
|
||||
* can be overriden in subclasses
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function startup() {
|
||||
#CHECK!
|
||||
if ( empty($this->params['q'] ) ) {
|
||||
$this->_welcome();
|
||||
}
|
||||
$CONF = Config::read('all');
|
||||
}
|
||||
/**
|
||||
* Displays a header for the shell
|
||||
*
|
||||
* @access protected
|
||||
*/
|
||||
function _welcome() {
|
||||
$this->out("\nWelcome to Postfixadmin-CLI v" . $this->Dispatch->version);
|
||||
$this->hr();
|
||||
$this->className = get_class($this);
|
||||
|
||||
if ($this->name == null) {
|
||||
$this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompts the user for input, and returns it.
|
||||
*
|
||||
* @param string $prompt Prompt text.
|
||||
* @param mixed $options Array or string of options.
|
||||
* @param string $default Default input value.
|
||||
* @return Either the default value, or the user-provided input.
|
||||
* @access public
|
||||
*/
|
||||
function in($prompt, $options = null, $default = null) {
|
||||
if (!$this->interactive) {
|
||||
return $default;
|
||||
}
|
||||
if ($prompt != '') $this->out("");
|
||||
$this->Dispatch =& $dispatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the Shell
|
||||
* acts as constructor for subclasses
|
||||
* allows configuration of tasks prior to shell execution
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function initialize() {
|
||||
}
|
||||
/**
|
||||
* Starts up the the Shell
|
||||
* allows for checking and configuring prior to command or main execution
|
||||
* can be overriden in subclasses
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function startup() {
|
||||
#CHECK!
|
||||
if ( empty($this->params['q'] ) ) {
|
||||
$this->_welcome();
|
||||
}
|
||||
$CONF = Config::read('all');
|
||||
}
|
||||
/**
|
||||
* Displays a header for the shell
|
||||
*
|
||||
* @access protected
|
||||
*/
|
||||
function _welcome() {
|
||||
$this->out("\nWelcome to Postfixadmin-CLI v" . $this->Dispatch->version);
|
||||
$this->hr();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompts the user for input, and returns it.
|
||||
*
|
||||
* @param string $prompt Prompt text.
|
||||
* @param mixed $options Array or string of options.
|
||||
* @param string $default Default input value.
|
||||
* @return Either the default value, or the user-provided input.
|
||||
* @access public
|
||||
*/
|
||||
function in($prompt, $options = null, $default = null) {
|
||||
if (!$this->interactive) {
|
||||
return $default;
|
||||
}
|
||||
if ($prompt != '') $this->out("");
|
||||
$in = $this->Dispatch->getInput($prompt, $options, $default);
|
||||
|
||||
if ($options && is_string($options)) {
|
||||
if (strpos($options, ',')) {
|
||||
$options = explode(',', $options);
|
||||
} elseif (strpos($options, '/')) {
|
||||
$options = explode('/', $options);
|
||||
} else {
|
||||
$options = array($options);
|
||||
}
|
||||
}
|
||||
if (is_array($options)) {
|
||||
while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
|
||||
$this->err("Invalid input"); # TODO: make translateable
|
||||
$in = $this->Dispatch->getInput($prompt, $options, $default);
|
||||
}
|
||||
}
|
||||
|
||||
if ($options && is_string($options)) {
|
||||
if (strpos($options, ',')) {
|
||||
$options = explode(',', $options);
|
||||
} elseif (strpos($options, '/')) {
|
||||
$options = explode('/', $options);
|
||||
} else {
|
||||
$options = array($options);
|
||||
}
|
||||
}
|
||||
if (is_array($options)) {
|
||||
while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
|
||||
$this->err("Invalid input"); # TODO: make translateable
|
||||
$in = $this->Dispatch->getInput($prompt, $options, $default);
|
||||
}
|
||||
}
|
||||
|
||||
return $in;
|
||||
return $in;
|
||||
}
|
||||
/**
|
||||
* Outputs to the stdout filehandle.
|
||||
*
|
||||
* @param string $string String to output.
|
||||
* @param boolean $newline If true, the outputs gets an added newline.
|
||||
* @access public
|
||||
*/
|
||||
function out($string, $newline = true) {
|
||||
if (is_array($string)) {
|
||||
$str = '';
|
||||
foreach($string as $message) {
|
||||
$str .= $message ."\n";
|
||||
}
|
||||
$string = $str;
|
||||
}
|
||||
/**
|
||||
* Outputs to the stdout filehandle.
|
||||
*
|
||||
* @param string $string String to output.
|
||||
* @param boolean $newline If true, the outputs gets an added newline.
|
||||
* @access public
|
||||
*/
|
||||
function out($string, $newline = true) {
|
||||
if (is_array($string)) {
|
||||
$str = '';
|
||||
foreach($string as $message) {
|
||||
$str .= $message ."\n";
|
||||
}
|
||||
$string = $str;
|
||||
}
|
||||
return $this->Dispatch->stdout($string, $newline);
|
||||
return $this->Dispatch->stdout($string, $newline);
|
||||
}
|
||||
/**
|
||||
* Outputs to the stderr filehandle.
|
||||
*
|
||||
* @param string $string Error text to output.
|
||||
* @access public
|
||||
*/
|
||||
function err($string) {
|
||||
if (is_array($string)) {
|
||||
$str = '';
|
||||
foreach($string as $message) {
|
||||
$str .= $message ."\n";
|
||||
}
|
||||
$string = $str;
|
||||
}
|
||||
/**
|
||||
* Outputs to the stderr filehandle.
|
||||
*
|
||||
* @param string $string Error text to output.
|
||||
* @access public
|
||||
*/
|
||||
function err($string) {
|
||||
if (is_array($string)) {
|
||||
$str = '';
|
||||
foreach($string as $message) {
|
||||
$str .= $message ."\n";
|
||||
}
|
||||
$string = $str;
|
||||
}
|
||||
return $this->Dispatch->stderr($string."\n");
|
||||
return $this->Dispatch->stderr($string."\n");
|
||||
}
|
||||
/**
|
||||
* Outputs a series of minus characters to the standard output, acts as a visual separator.
|
||||
*
|
||||
* @param boolean $newline If true, the outputs gets an added newline.
|
||||
* @access public
|
||||
*/
|
||||
function hr($newline = false) {
|
||||
if ($newline) {
|
||||
$this->out("\n");
|
||||
}
|
||||
/**
|
||||
* Outputs a series of minus characters to the standard output, acts as a visual separator.
|
||||
*
|
||||
* @param boolean $newline If true, the outputs gets an added newline.
|
||||
* @access public
|
||||
*/
|
||||
function hr($newline = false) {
|
||||
if ($newline) {
|
||||
$this->out("\n");
|
||||
}
|
||||
$this->out('---------------------------------------------------------------');
|
||||
if ($newline) {
|
||||
$this->out("\n");
|
||||
}
|
||||
$this->out('---------------------------------------------------------------');
|
||||
if ($newline) {
|
||||
$this->out("\n");
|
||||
}
|
||||
/**
|
||||
* Displays a formatted error message and exits the application
|
||||
*
|
||||
* @param string $title Title of the error message
|
||||
* @param string $msg Error message
|
||||
* @access public
|
||||
*/
|
||||
function error($title, $msg) {
|
||||
$out = "$title\n";
|
||||
$out .= "$msg\n";
|
||||
$out .= "\n";
|
||||
$this->err($out);
|
||||
$this->_stop(1);
|
||||
}
|
||||
/**
|
||||
* Outputs usage text on the standard output. Implement it in subclasses.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function help() {
|
||||
if ($this->command != null) {
|
||||
$this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
|
||||
} else {
|
||||
$this->Dispatch->help();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Stop execution of the current script
|
||||
*
|
||||
* @param $status see http://php.net/exit for values
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function _stop($status = 0) {
|
||||
exit($status);
|
||||
}
|
||||
/**
|
||||
* Displays a formatted error message and exits the application
|
||||
*
|
||||
* @param string $title Title of the error message
|
||||
* @param string $msg Error message
|
||||
* @access public
|
||||
*/
|
||||
function error($title, $msg) {
|
||||
$out = "$title\n";
|
||||
$out .= "$msg\n";
|
||||
$out .= "\n";
|
||||
$this->err($out);
|
||||
$this->_stop(1);
|
||||
}
|
||||
/**
|
||||
* Outputs usage text on the standard output. Implement it in subclasses.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function help() {
|
||||
if ($this->command != null) {
|
||||
$this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
|
||||
} else {
|
||||
$this->Dispatch->help();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Stop execution of the current script
|
||||
*
|
||||
* @param $status see http://php.net/exit for values
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function _stop($status = 0) {
|
||||
exit($status);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user