1
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2025-12-03 23:11:12 +03:00
Files
postfixadmin/scripts/shells/mailbox.php
Christian Boltz c65e3293b6 Quite big CLI cleanup (and more)
model/CliHelp.php:
- new class, used for "postfixadmin-cli $module help"
- replaces the PostfixAdmin* classes in scripts/shells/*.php

model/PFAHandler.php
- add public $taskNames with the list of supported CLI commands

scripts/postfixadmin-cli.php - dispatch():
- directly set the classes to load instead of using shell->loadTasks()
- when "postfixadmin-cli $module" is called, display help instead of 
  error message about "invalid command ''"
- make it more readable by moving error checks to the beginning
  (replaces deeply nested if's with return statements)
- remove unused code parts

scripts/shells/*.php:
- remove PostfixAdmin* classes (obsoleted by CliHelp)
- remove $tasks (now in PFAHandler)
- delete alias.php and domain.php because nothing is left
- mailbox.php still contains PasswordTask

scripts/shells/shell.php:
- remove $taskNames (moved to PFAHandler)
- remove loadTasks() (integrated in postfixadmin-cli.php's dispatch())



git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1575 a1433add-5e2c-0410-b055-b7f2511e0802
2013-11-13 19:07:12 +00:00

131 lines
4.3 KiB
PHP

<?php
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);
}
}
/**
* 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;
$password = NULL;
if ($random == false) {
$question = "Pleas enter the new password?";
$password = $this->in($question);
}
$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();
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
*/
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();
}
}