You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postfixadmin/scripts/shells/domain.php

396 lines
13 KiB
PHTML

<?php
class PostfixAdminDomain extends Shell {
/**
* Contains tasks to load and instantiate
*
* @var array
* @access public
*/
var $tasks = array('Add', 'Update', 'Delete', 'View');
/**
* Show help for this shell.
*
* @access public
*/
function help() {
$head = "Usage: postfixadmin-cli domain <task> [<domain>] [-desc \"<description>\"] [-a <aliases>] [-m <mailboxes>] [-q <quota in MB>] [-t <transport>] [-default] [-backup]\n";
$head .= "-----------------------------------------------\n";
$head .= "Parameters:\n\n";
$commands = array(
'task' => "\t<task>\n" .
"\t\tAvailable values:\n\n".
"\t\t".sprintf("%-20s %s", "view: ", "View an existing domain.")."\n".
"\t\t".sprintf("%-20s %s", "add: ", "Adds a domain.")."\n".
"\t\t".sprintf("%-20s %s", "update: ", "Updates an domain.")."\n".
"\t\t".sprintf("%-20s %s", "delete: ", "Deletes a domain")."\n",
'domain' => "\t[<domain>]\n" .
"\t\tA address of recipient.\n",
'a' => "\t[<aliaes>]\n" .
"\t\tNumber of max aliases. -1 = disable | 0 = unlimited\n",
'm' => "\t[<mailboxes>]\n" .
"\t\tNumber of max mailboxes. -1 = disable | 0 = unlimited\n",
'q' => "\t[<quota in MB>]\n" .
"\t\tMax Quota in MB. -1 = disable | 0 = unlimited\n",
'd' => "\t[<domain quota in MB>]\n" .
"\t\tDomain Quota in MB. -1 = disable | 0 = unlimited\n",
't' => "\t[<transport>]\n" .
"\t\tTransport options from config.inc.php.\n",
'default' => "\t\tSet to add default Aliases.\n",
'backup' => "\t\tSet if mailserver is backup MX.\n",
);
$this->out($head);
if (!isset($this->args[1])) {
foreach ($commands as $cmd) {
$this->out("{$cmd}\n\n");
}
} elseif (isset($commands[strtolower($this->args[1])])) {
$this->out($commands[strtolower($this->args[1])] . "\n\n");
} else {
$this->out("Command '" . $this->args[1] . "' not found");
}
}
}
class AddTask extends Shell {
/**
* Execution method always used for tasks
*
* @access public
*/
function execute() {
if (empty($this->args)) {
$this->__interactive();
}
if (!empty($this->args[0])) {
$this->__handle($this->args[0], $this->args[1]);
# TODO: handle the various -* parameters (see help)
}
}
/**
* Interactive
*
* @access private
*/
function __interactive() {
while(0==0) {
$question = "Enter domain:";
$domain = $this->in($question);
if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $domain) == 1)
break;
$this->err("Invalid domain");
}
$question = "Description:";
$desc = $this->in($question);
$question = "Number of Aliases:";
$a = $this->in($question);
$question = "Numer of Mailboxes:";
$m = $this->in($question);
$question = "Max Quota (in MB):";
$q = $this->in($question);
$question = "Domain Quota (in MB):";
$d = $this->in($question);
$handler = new DomainHandler($domain);
$transports = $handler->getTransports();
$qt[] = 'Choose transport option';
foreach ($transports AS $key => $val) {
//workaround. $this->in hates number 0
$key = $key + 1;
$qt[] = '['.$key.'] - '.$val;
}
$t = $this->in( join("\n", $qt) );
$question = "Add default Aliases:";
$default = $this->in($question, array('y','n'));
($default == 'y') ? $default = true : $default = false;
$question = "Use as Backup MX:";
$backup = $this->in($question, array('y','n'));
($backup == 'y') ? $backup = true : $backup = false;
$this->__handle($domain, $desc, $a, $m, $t, $q, $d, $default, $backup);
}
/**
* Interactive
*
* @access private
*/
function __handle($domain, $desc, $a, $m, $t, $q, $d, $default, $backup) {
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
$handler = new DomainHandler($domain, 1);
if (!$handler->result()) {
$this->error("Error:",join("\n", $handler->errormsg));
return;
}
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
$values = array(
'description' => $desc,
'aliases' => $a,
'mailboxes' => $m,
'maxquota' => $q,
'quota' => $d,
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
'transport' => $handler->getTransport($t),
'backupmx' => $backup,
'active' => 1,
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
'default_aliases' => $default,
);
if (!$handler->set($values)) {
$this->error("Error:", join("\n", $handler->errormsg));
}
if (!$handler->store()) {
$this->error("Error:", join("\n", $handler->errormsg));
} else {
$this->out("");
$this->out("Domain ( $domain ) generated.");
$this->hr();
}
return;
}
/**
* Displays help contents
*
* @access public
*/
function help() {
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
# TODO: this is the DOMAIN shell...
$this->hr();
$this->out("Usage: postfixadmin-cli user add <address> [<password>] <name> <quota> [-g]");
$this->hr();
$this->out('Commands:');
$this->out("\n\tadd\n\t\tAdds mailbox in interactive mode.");
$this->out("\n\tadd <address> [<password>] [-g] <name> <quota>\n\t\tAdds mailbox for <address> with password <password> of if -g with rand pw. <quota> in MB.");
$this->out("");
$this->_stop();
}
}
class UpdateTask 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();
}
}
/**
* Interactive
*
* @access private
*/
function __interactive() {
}
/**
* Displays help contents
*
* @access public
*/
function help() {
$this->hr();
$this->out("Not Implemented yet! ");
/*$this->out("Usage: postfixadmin-cli user update <args>");
$this->hr();
$this->out('Commands:');
$this->out("\n\tmodel\n\t\tbakes model in interactive mode.");
$this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation");
$this->out("");*/
$this->_stop();
}
}
class DeleteTask extends Shell {
/**
* Execution method always used for tasks
*
* @access public
*/
function execute() {
if (empty($this->args)) {
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
$this->__interactive();
}
if (!empty($this->args[0])) {
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
$output = $this->__handle($this->args[0]);
$this->out($output);
}
}
/**
* Interactive
*
* @access private
*/
function __interactive() {
$question = "Which domain do you want to delete?";
$address = $this->in($question);
$question = "Do you really want to delete domain '$address'?";
$create = $this->in($question, array('y','n'));
$this->__handle($address);
}
/**
* Interactive
*
* @access private
*/
function __handle($address) {
$handler = new DomainHandler($address);
if (!$handler->result()) {
$this->error("Error:",join("\n", $handler->errormsg));
return;
}
$status = $handler->delete();
if ($status == true) {
$this->out("Domain '$address' was deleted.");
} else {
$this->error("Error:", join("\n", $handler->errormsg));
}
return;
}
/**
* Displays help contents
*
* @access public
*/
function help() {
$this->out("NOT Implemented yet.");
$this->hr();
$this->out("Usage: postfixadmin-cli user model <arg1>");
$this->hr();
//$this->out('Commands:');
//$this->out("\n\tdelete\n\t\tdeletes mailbox in interactive mode.");
//$this->out("\n\tdelete <address>\n\t\tdeletes mailbox with address <address>");
//$this->out("");
$this->_stop();
}
}
##Deleted PasswordTask because its silly in domain shell
class ViewTask extends Shell {
/**
* Execution method always used for tasks
*
* @access public
*/
function execute() {
if (empty($this->args)) {
$this->__interactive();
}
if (!empty($this->args[0])) {
$output = $this->__handle($this->args[0]);
$this->out($output);
}
}
/**
* Interactive
*
* @access private
*/
function __interactive() {
$question[] = "Which Domain do you want to view?";
$domain = $this->in(join("\n", $question));
$this->__handle($domain);
}
/**
* Interactive
*
* @access private
*/
function __handle($domain) {
$handler = new DomainHandler($domain);
if (!$handler->result()) {
$this->error("Error:",join("\n", $handler->errormsg));
return;
}
$status = $handler->view();
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']);
$this->out("Aliases: \t".$result['aliases']);
$this->out("Mailboxes: \t".$result['mailboxes']);
$this->out("Max. Quota: \t".$result['maxquota']);
$this->out("Domain Quota: \t".$result['quota']);
# TODO: show allocated domain quota (sum of mailbox quota)
$this->out("Transport: \t".$result['transport']);
$this->out("Backup MX: \t".$result['backupmx']);
$this->out("Active: \t".$result['active']);
$this->out("Modified: \t".$result['modified']);
$this->out("Created: \t".$result['created']);
return ;
}
}
/**
* Displays help contents
*
* @access public
*/
function help() {
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
# TODO: this is the DOMAIN shell...
$this->out("");
$this->hr();
$this->out("Usage: postfixadmin-cli user view <address>");
$this->hr();
$this->out('Commands:');
$this->out("\n\tview\n\t\tView user. Select address in interactive mode.");
$this->out("\n\tview <address>\n\t\tView user with address <address>");
$this->out("");
$this->_stop();
}
}
DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@978 a1433add-5e2c-0410-b055-b7f2511e0802
14 years ago
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */