some todos done or partially done

-added alias delete function
-remove silly password function in domain and alias

-changed some $this->err to $this->error
-error ends with stop(1) which returns 1 in *unix shells



git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@920 a1433add-5e2c-0410-b055-b7f2511e0802
pull/2/head
Valkum 14 years ago
parent 320a9331f1
commit c6771f7f04

@ -8,10 +8,6 @@ class AliasHandler {
private $username = null;
# TODO: implement a "delete" method. Pseudocode:
# - check if alias exists
# - check if mailbox exists - if yes, error out (never delete the alias of a mailbox!)
# - (if still here) delete alias
/**
* @param string $username
@ -210,7 +206,30 @@ class AliasHandler {
}
return false;
}
# TODO: implement a "delete" method. Pseudocode:
# - check if alias exists
# - check if mailbox exists - if yes, error out (never delete the alias of a mailbox!)
# - (if still here) delete alias
#HERE IT IS!
/**
* @param alias address
* @return true on success false on failure
*/
public function delete($address){
$E_address = escape_string($address);
$table_alias = table_by_key('alias');
if( $this->get($address) && !is_mailbox_alias($address) ) {
sql = "DELETE FROM $table_alias" WHERE address = '$E_address';
$result = db_query($sql);
if( $result['rows'] == 1 ) {
return true;
}
return false;
}
}
/**
* @return return value of previously called method
*/

@ -107,7 +107,7 @@ class AddTask extends Shell {
$return = $handler->add($goto);
if($return == 1) {
$this->err(join("\n", $handler->errormsg));
$this->error("Error:", join("\n", $handler->errormsg));
} else {
$this->out("");
$this->out("Alias ( $address -> $goto ) generated.");
@ -132,6 +132,7 @@ class AddTask extends Shell {
}
}
#TODO: implement
class UpdateTask extends Shell {
/**
* Execution method always used for tasks
@ -209,7 +210,7 @@ class DeleteTask extends Shell {
$create = $this->in($question, array('y','n'));
$create == 'y' ? $random = true : $random = false;
$create == 'y' ? $create = true : $create = false;
if ($create)
$this->__handle($address);
@ -227,13 +228,15 @@ class DeleteTask extends Shell {
### TODO: don't use UserHandler, instead add delete function to AliasHandler (if not already there)
### using UserHandler for deleting aliases is like taking a sledgehammer to crack a nut
### (and will probably cause some error messages that I added today ;-)
$handler = new UserHandler($address);
### Implemented check it please!
$handler = new AliasHandler($address);
$status = $handler->delete();
if ($status == true) {
$this->out("Mailbox of '$address' was deleted.");
} else {
$this->err(join("\n", $handler->errormsg));
$this->error("Error:", join("\n", $handler->errormsg));
}
return;
@ -256,128 +259,7 @@ class DeleteTask extends Shell {
}
}
class PasswordTask extends Shell {
/**
* Execution method always used for tasks
*
* @access public
*/
function execute() {
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]) && length($this->args[1]) > 8) {
$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);
}
var_dump($random);
$this->__handle($address, $password, $random);
}
/**
* Interactive
*
* @access private
*/
function __handle($address, $password = NULL, $random = false) {
# TODO: Does PasswordTask really make sense for Aliases? Probably not...
if ($random == true) {
$password = generate_password();
}
if ($password != NULL) {
$handler = new UserHandler($address);
if ($handler->change_pw($password, NULL, false) == 1){
$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 user 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();
}
}
### PasswordTask was a rest of Copy Paste :D Deleted. Check it!
class ViewTask extends Shell {
/**
* Execution method always used for tasks
@ -421,7 +303,7 @@ class ViewTask extends Shell {
$handler = new AliasHandler($address);
$status = $handler->get(); # TODO: set the "all" flag?
if ( ! $status) {
# TODO: error message
$this->error("Error: Not Found", "The requested alias was not found!");
} else {
$result = $handler->return;
@ -431,7 +313,18 @@ class ViewTask extends Shell {
$this->out("\t -> ".$goto);
}
# TODO: display "deliver to mailbox"
##NEED fix in is_mailbox_alias because user is not set correctly in this scenario!
/**
if( $handler->is_mailbox_alias($address) )
$this->out("A mailbox was set for this alias!\n");
}
*/
# TODO: display if vacation is on?
/**
if( $handler->is_vacation_address($address) ) {
$this->out("This alias is a vacation address!");
}
*/
}
return;

@ -139,8 +139,8 @@ class AddTask extends Shell {
$handler = new DomainHandler('CONSOLE');
$return = $handler->add($domain, $desc, $a, $m, $t, $q, $default, $backup);
if($return == 1) {
$this->err(join("\n", $handler->errormsg));
if(!$return) {
$this->error("Error:", join("\n", $handler->errormsg));
} else {
$this->out("");
$this->out("Domain ( $domain ) generated.");
@ -264,7 +264,7 @@ class DeleteTask extends Shell {
$this->out("Mailbox of '$address' was deleted.");
} else {
$this->err(join("\n", $handler->errormsg));
$this->error("Error:", join("\n", $handler->errormsg));
}
return;
@ -287,129 +287,7 @@ class DeleteTask extends Shell {
}
}
class PasswordTask extends Shell {
/**
* Execution method always used for tasks
*
* @access public
*/
function execute() {
if (empty($this->args)) {
$this->help();
// $this->__interactive();
}
if (!empty($this->args[0])) {
$this->help();
//$address = $this->args[0];
//if (isset($this->params['g']) && $this->params['g'] == true ) {
// $random = true;
// $password = NULL;
//} elseif (isset($this->args[1]) && length($this->args[1]) > 8) {
// $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);
}
var_dump($random);
$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 UserHandler($address);
if ($handler->change_pw($password, NULL, false) == 1){
$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("NOT implemented yet.");
$this->hr();
$this->out("Usage: postfixadmin-cli user 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();
}
}
##Deleted PasswordTask because its silly in domain shell
class ViewTask extends Shell {
/**
* Execution method always used for tasks
@ -453,7 +331,9 @@ class ViewTask extends Shell {
$handler = new DomainHandler('CONSOLE');
$status = $handler->view($domain);
if ($status == 0) {
if (!$status) {
$this->error("Error:",join("\n", $handler->errormsg));
} else {
$result = $handler->return;
$this->out("Domain: \t".$result['domain']);
$this->out("Description: \t".$result['description']);
@ -466,9 +346,8 @@ class ViewTask extends Shell {
$this->out("Modified: \t".$result['modified']);
$this->out("Created: \t".$result['created']);
return ;
}
return;
}
/**

@ -327,7 +327,7 @@ if ( empty($this->params['q'] ) ) {
$out .= "$msg\n";
$out .= "\n";
$this->err($out);
$this->_stop();
$this->_stop(1);
}
/**
* Outputs usage text on the standard output. Implement it in subclasses.
@ -341,7 +341,7 @@ if ( empty($this->params['q'] ) ) {
$this->Dispatch->help();
}
}
/**
/**
* Stop execution of the current script
*
* @param $status see http://php.net/exit for values

@ -57,6 +57,11 @@ class AddTask extends Shell {
*
* @access public
*/
# Find one function that matches all executes in shell childclasses.
# Eventually getopts like call in __handle??
function execute() {
if (empty($this->args)) {
$this->__interactive();
@ -143,8 +148,10 @@ if ( !empty($this->params['q']) ) {
}
} else {
if( $return == false ) {
$this->err($handler->errormsg);
$this->_stop(1);
### When $this->error is used, $this->_stop is useless.
### Changed $this->error to stop with level 1.
### Eventually param q check in $this->error is better!! !Important!
$this->error("Error:", $handler->errormsg);
} else {
$this->out("");
if ($name != '')
@ -270,7 +277,8 @@ class DeleteTask extends Shell {
$handler = new UserHandler($address);
$status = $handler->delete();
if ( ! $status ) {
$this->err(join("\n", $handler->errormsg));
$this->error("Error:", join("\n", $handler->errormsg));
} else {
$this->out("Mailbox of '$address' was deleted.");
}
@ -460,9 +468,8 @@ class ViewTask extends Shell {
$handler = new UserHandler($address);
if ( ! $handler->view() ) {
return ;
# TODO: display error message "not found"
}
$this->error("Not Found!", "The user you have searched could not be found.");
}
# TODO: offer alternative output formats (based on parameter)
# TODO: whitespace fix - 8 lines below
$result = $handler->return;

Loading…
Cancel
Save