PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
11 KiB

  1. <?php
  2. class PostfixAdminDomain extends Shell {
  3. /**
  4. * Contains tasks to load and instantiate
  5. *
  6. * @var array
  7. * @access public
  8. */
  9. var $tasks = array('Add', 'Update', 'Delete', 'View');
  10. /**
  11. * Show help for this shell.
  12. *
  13. * @access public
  14. */
  15. function help() {
  16. $head = "Usage: postfixadmin-cli domain <task> [<domain>] [-desc \"<description>\"] [-a <aliases>] [-m <mailboxes>] [-q <quota in MB>] [-t <transport>] [-default] [-backup]\n";
  17. $head .= "-----------------------------------------------\n";
  18. $head .= "Parameters:\n\n";
  19. $commands = array(
  20. 'task' => "\t<task>\n" .
  21. "\t\tAvailable values:\n\n".
  22. "\t\t".sprintf("%-20s %s", "view: ", "View an existing domain.")."\n".
  23. "\t\t".sprintf("%-20s %s", "add: ", "Adds a domain.")."\n".
  24. "\t\t".sprintf("%-20s %s", "update: ", "Updates an domain.")."\n".
  25. "\t\t".sprintf("%-20s %s", "delete: ", "Deletes a domain")."\n",
  26. 'domain' => "\t[<domain>]\n" .
  27. "\t\tA address of recipient.\n",
  28. 'a' => "\t[<aliaes>]\n" .
  29. "\t\tNumber of max aliases. -1 = disable | 0 = unlimited\n",
  30. 'm' => "\t[<mailboxes>]\n" .
  31. "\t\tNumber of max mailboxes. -1 = disable | 0 = unlimited\n",
  32. 'q' => "\t[<quota in MB>]\n" .
  33. "\t\tMax Quota in MB. -1 = disable | 0 = unlimited\n",
  34. 't' => "\t[<transport>]\n" .
  35. "\t\tTransport options from config.inc.php.\n",
  36. 'default' => "\t\tSet to add default Aliases.\n",
  37. 'backup' => "\t\tSet if mailserver is backup MX.\n",
  38. );
  39. $this->out($head);
  40. if (!isset($this->args[1])) {
  41. foreach ($commands as $cmd) {
  42. $this->out("{$cmd}\n\n");
  43. }
  44. } elseif (isset($commands[low($this->args[1])])) {
  45. $this->out($commands[low($this->args[1])] . "\n\n");
  46. } else {
  47. $this->out("Command '" . $this->args[1] . "' not found");
  48. }
  49. }
  50. }
  51. class AddTask extends Shell {
  52. /**
  53. * Execution method always used for tasks
  54. *
  55. * @access public
  56. */
  57. function execute() {
  58. if (empty($this->args)) {
  59. $this->__interactive();
  60. }
  61. if (!empty($this->args[0])) {
  62. $this->__handle($this->args[0], $this->args[1]);
  63. }
  64. }
  65. /**
  66. * Interactive
  67. *
  68. * @access private
  69. */
  70. function __interactive() {
  71. while(0==0) {
  72. $question = "Enter domain:";
  73. $domain = $this->in($question);
  74. if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $domain) == 1)
  75. break;
  76. $this->err("Invalid domain");
  77. }
  78. $question = "Description:";
  79. $desc = $this->in($question);
  80. $question = "Number of Aliases:";
  81. $a = $this->in($question);
  82. $question = "Numer of Mailboxes:";
  83. $m = $this->in($question);
  84. $question = "Max Quota (in MB):";
  85. $q = $this->in($question);
  86. $handler = new DomainHandler($domain);
  87. $transports = $handler->getTransports();
  88. $qt[] = 'Choose transport option';
  89. foreach ($transports AS $key => $val) {
  90. //workaround. $this->in hates number 0
  91. $key = $key + 1;
  92. $qt[] = '['.$key.'] - '.$val;
  93. }
  94. $t = $this->in( join("\n", $qt) );
  95. $question = "Add default Aliases:";
  96. $default = $this->in($question, array('y','n'));
  97. ($default == 'y') ? $default = true : $default = false;
  98. $question = "Use as Backup MX:";
  99. $backup = $this->in($question, array('y','n'));
  100. ($backup == 'y') ? $backup = true : $backup = false;
  101. $this->__handle($domain, $desc, $a, $m, $t, $q, $default, $backup);
  102. }
  103. /**
  104. * Interactive
  105. *
  106. * @access private
  107. */
  108. function __handle($domain, $desc, $a, $m, $t, $q, $default, $backup) {
  109. $handler = new DomainHandler($domain);
  110. $return = $handler->add($desc, $a, $m, $t, $q, $default, $backup);
  111. if(!$return) {
  112. $this->error("Error:", join("\n", $handler->errormsg));
  113. } else {
  114. $this->out("");
  115. $this->out("Domain ( $domain ) generated.");
  116. $this->hr();
  117. }
  118. return;
  119. }
  120. /**
  121. * Displays help contents
  122. *
  123. * @access public
  124. */
  125. function help() {
  126. $this->hr();
  127. $this->out("Usage: postfixadmin-cli user add <address> [<password>] <name> <quota> [-g]");
  128. $this->hr();
  129. $this->out('Commands:');
  130. $this->out("\n\tadd\n\t\tAdds mailbox in interactive mode.");
  131. $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.");
  132. $this->out("");
  133. $this->_stop();
  134. }
  135. }
  136. class UpdateTask extends Shell {
  137. /**
  138. * Execution method always used for tasks
  139. *
  140. * @access public
  141. */
  142. function execute() {
  143. if (empty($this->args)) {
  144. $this->help();
  145. //$this->__interactive();
  146. }
  147. if (!empty($this->args[0])) {
  148. $this->help();
  149. }
  150. }
  151. /**
  152. * Interactive
  153. *
  154. * @access private
  155. */
  156. function __interactive() {
  157. }
  158. /**
  159. * Displays help contents
  160. *
  161. * @access public
  162. */
  163. function help() {
  164. $this->hr();
  165. $this->out("Not Implemented yet! ");
  166. /*$this->out("Usage: postfixadmin-cli user update <args>");
  167. $this->hr();
  168. $this->out('Commands:');
  169. $this->out("\n\tmodel\n\t\tbakes model in interactive mode.");
  170. $this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation");
  171. $this->out("");*/
  172. $this->_stop();
  173. }
  174. }
  175. class DeleteTask extends Shell {
  176. /**
  177. * Execution method always used for tasks
  178. *
  179. * @access public
  180. */
  181. function execute() {
  182. if (empty($this->args)) {
  183. $this->__interactive();
  184. }
  185. if (!empty($this->args[0])) {
  186. $output = $this->__handle($this->args[0]);
  187. $this->out($output);
  188. }
  189. }
  190. /**
  191. * Interactive
  192. *
  193. * @access private
  194. */
  195. function __interactive() {
  196. $question = "Which domain do you want to delete?";
  197. $address = $this->in($question);
  198. $question = "Do you really want to delete domain '$address'?";
  199. $create = $this->in($question, array('y','n'));
  200. $this->__handle($address);
  201. }
  202. /**
  203. * Interactive
  204. *
  205. * @access private
  206. */
  207. function __handle($address) {
  208. $handler = new DomainHandler($address);
  209. $status = $handler->delete();
  210. if ($status == true) {
  211. $this->out("Domain '$address' was deleted.");
  212. } else {
  213. $this->error("Error:", join("\n", $handler->errormsg));
  214. }
  215. return;
  216. }
  217. /**
  218. * Displays help contents
  219. *
  220. * @access public
  221. */
  222. function help() {
  223. $this->out("NOT Implemented yet.");
  224. $this->hr();
  225. $this->out("Usage: postfixadmin-cli user model <arg1>");
  226. $this->hr();
  227. //$this->out('Commands:');
  228. //$this->out("\n\tdelete\n\t\tdeletes mailbox in interactive mode.");
  229. //$this->out("\n\tdelete <address>\n\t\tdeletes mailbox with address <address>");
  230. //$this->out("");
  231. $this->_stop();
  232. }
  233. }
  234. ##Deleted PasswordTask because its silly in domain shell
  235. class ViewTask extends Shell {
  236. /**
  237. * Execution method always used for tasks
  238. *
  239. * @access public
  240. */
  241. function execute() {
  242. if (empty($this->args)) {
  243. $this->__interactive();
  244. }
  245. if (!empty($this->args[0])) {
  246. $output = $this->__handle($this->args[0]);
  247. $this->out($output);
  248. }
  249. }
  250. /**
  251. * Interactive
  252. *
  253. * @access private
  254. */
  255. function __interactive() {
  256. $question[] = "Which Domain do you want to view?";
  257. $domain = $this->in(join("\n", $question));
  258. $this->__handle($domain);
  259. }
  260. /**
  261. * Interactive
  262. *
  263. * @access private
  264. */
  265. function __handle($domain) {
  266. $handler = new DomainHandler($domain);
  267. $status = $handler->view();
  268. if (!$status) {
  269. $this->error("Error:",join("\n", $handler->errormsg));
  270. } else {
  271. $result = $handler->return;
  272. $this->out("Domain: \t".$result['domain']);
  273. $this->out("Description: \t".$result['description']);
  274. $this->out("Aliases: \t".$result['aliases']);
  275. $this->out("Mailboxes: \t".$result['mailboxes']);
  276. $this->out("Max. Quota: \t".$result['maxquota']);
  277. $this->out("Transport: \t".$result['transport']);
  278. $this->out("Backup MX: \t".$result['backupmx']);
  279. $this->out("Active: \t".$result['active']);
  280. $this->out("Modified: \t".$result['modified']);
  281. $this->out("Created: \t".$result['created']);
  282. return ;
  283. }
  284. }
  285. /**
  286. * Displays help contents
  287. *
  288. * @access public
  289. */
  290. function help() {
  291. $this->out("");
  292. $this->hr();
  293. $this->out("Usage: postfixadmin-cli user view <address>");
  294. $this->hr();
  295. $this->out('Commands:');
  296. $this->out("\n\tview\n\t\tView user. Select address in interactive mode.");
  297. $this->out("\n\tview <address>\n\t\tView user with address <address>");
  298. $this->out("");
  299. $this->_stop();
  300. }
  301. }