1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
#!/usr/bin/php
<?php
/**
* Command-line code generation utility to automate administrator tasks.
*
* Shell dispatcher class
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
* Modified for PostfixAdmin by Valkum 2011
* Modified for PostfixAdmin by Christian Boltz 2011-2013
*
* Copyright 2010
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
* @link http://postfixadmin.sourceforge.net/ Postfixadmin on Sourceforge
* @package postfixadmin
* @subpackage -
* @since -
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class PostfixAdmin {
/**
* Version
*
* @var string
*/
public $version ='0.3';
/**
* Standard input stream.
*
* @var resource
*/
public $stdin;
/**
* Standard output stream.
*
* @var resource
*/
public $stdout;
/**
* Standard error stream.
*
* @var resource
*/
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;
/**
* The class name of the shell that was invoked.
*
* @var string
*/
public $shellClass;
/**
* The command called if public methods are available.
*
* @var string
*/
public $shellCommand;
/**
* The name of the shell in camelized.
*
* @var string
*/
public $shellName;
/**
* 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', "0");
ini_set('implicit_flush', "1");
ini_set('max_execution_time', "0");
}
/**
* 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 (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();
}
/**
* postfixadmin-cli admin view admin@example.com
* - Create AdminHandler.
* - and then a CliView object (Shell class)
* - call CliView->view() ... which under the covers uses AdminHandler*
*/
public function dispatch() {
check_db_version(); # ensure the database layout is up to date
if (!isset($this->args[0])) {
$this->help();
return 1;
}
$this->shell = $this->args[0];
$this->shiftArgs();
$this->shellName = ucfirst($this->shell);
$this->shellClass = $this->shellName . 'Handler';
if ($this->shell == 'help') {
$this->help();
return 1;
}
$command = $this->args[0];
$this->shellCommand = $command;
$this->shellClass = 'Cli' . ucfirst($command);
if (ucfirst($command) == 'Add' || ucfirst($command) == 'Update') {
$this->shellClass = 'CliEdit';
}
if (!class_exists($this->shellClass)) {
$this->stderr('Unknown task ' . $this->shellCommand);
return 1;
}
$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 1;
}
$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') {
$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();
return 1;
} else {
$this->help();
return 1;
}
}
return $shell->execute();
}
}
$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(
'in', 'out', 'err', 'hr', 'log',
'__construct', 'dispatch', 'stdout', 'stderr'
);
if (in_array(strtolower($command), $protectedCommands)) {
$missingCommand = true;
}
if ($missingCommand && method_exists($shell, 'main')) {
$shell->startup();
return $shell->main();
} elseif (!$privateMethod && method_exists($shell, $command)) {
$this->shiftArgs();
$shell->startup();
return $shell->{$command}();
} else {
$this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
return 1;
}
}
/**
* 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 string 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) {
$count = count($params);
for ($i = 0; $i < $count; $i++) {
if ($params[$i] != '' && $params[$i][0] === '-' && $params[$i] != '-1') {
$key = substr($params[$i], 1);
if (isset($params[$i+1])) {
# 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+1][0] === '-' && $params[$i+1] != '-1') {
$this->params[$key] = true;
} else {
$this->params[$key] = $params[$i+1];
$i++;
}
}
} else {
$this->args[] = $params[$i];
}
}
}
/**
* 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();
}
}
define("POSTFIXADMIN_CLI", 1);
require_once(dirname(__FILE__) . '/../common.php');
$dispatcher = new PostfixAdmin($argv);
try {
$retval = $dispatcher->dispatch();
} catch (Exception $e) {
$dispatcher->stderr("Execution Exception: " . $e->getMessage());
$retval = 1;
}
exit($retval);
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|