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.

345 lines
10 KiB

  1. <?php
  2. class PostfixAdminAlias 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 alias <task> [<address>] [] [-m <method>]\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 alias.")."\n".
  23. "\t\t".sprintf("%-20s %s", "add: ", "Adds an alias.")."\n".
  24. "\t\t".sprintf("%-20s %s", "update: ", "Updates an alias.")."\n".
  25. "\t\t".sprintf("%-20s %s", "delete: ", "Deletes an alias")."\n",
  26. 'address' => "\t[<address>]\n" .
  27. "\t\tA address of recipient.\n",
  28. );
  29. $this->out($head);
  30. if (!isset($this->args[1])) {
  31. foreach ($commands as $cmd) {
  32. $this->out("{$cmd}\n\n");
  33. }
  34. } elseif (isset($commands[low($this->args[1])])) {
  35. $this->out($commands[low($this->args[1])] . "\n\n");
  36. } else {
  37. $this->out("Command '" . $this->args[1] . "' not found");
  38. }
  39. }
  40. }
  41. class AddTask extends Shell {
  42. /**
  43. * Execution method always used for tasks
  44. *
  45. * @access public
  46. */
  47. function execute() {
  48. if (empty($this->args)) {
  49. $this->__interactive();
  50. }
  51. if (!empty($this->args[0])) {
  52. $this->__handle($this->args[0], $this->args[1]);
  53. }
  54. }
  55. /**
  56. * Interactive
  57. *
  58. * @access private
  59. */
  60. function __interactive() {
  61. while(0==0) {
  62. $question = "Enter address:";
  63. $address = $this->in($question);
  64. 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)
  65. break;
  66. $this->err("Invalid emailaddress");
  67. }
  68. while(0==0) {
  69. $question = "Forward to:";
  70. $goto = $this->in($question);
  71. 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)
  72. break;
  73. $this->err("Invalid emailaddress");
  74. }
  75. $this->__handle($address, $goto);
  76. }
  77. /**
  78. * Interactive
  79. *
  80. * @access private
  81. */
  82. function __handle($address, $goto) {
  83. $handler = new AliasHandler($address);
  84. $return = $handler->add($goto);
  85. if($return == 1) {
  86. $this->error("Error:", join("\n", $handler->errormsg));
  87. } else {
  88. $this->out("");
  89. $this->out("Alias ( $address -> $goto ) generated.");
  90. $this->hr();
  91. }
  92. return;
  93. }
  94. /**
  95. * Displays help contents
  96. *
  97. * @access public
  98. */
  99. function help() {
  100. $this->hr();
  101. $this->out("Usage: postfixadmin-cli user add <address> [<password>] <name> <quota> [-g]");
  102. $this->hr();
  103. $this->out('Commands:');
  104. $this->out("\n\tadd\n\t\tAdds mailbox in interactive mode.");
  105. $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.");
  106. $this->out("");
  107. $this->_stop();
  108. }
  109. }
  110. #TODO: implement
  111. class UpdateTask extends Shell {
  112. /**
  113. * Execution method always used for tasks
  114. *
  115. * @access public
  116. */
  117. function execute() {
  118. if (empty($this->args)) {
  119. $this->help();
  120. //$this->__interactive();
  121. }
  122. if (!empty($this->args[0])) {
  123. $this->help();
  124. }
  125. }
  126. /**
  127. * Interactive
  128. *
  129. * @access private
  130. */
  131. function __interactive() {
  132. }
  133. /**
  134. * Displays help contents
  135. *
  136. * @access public
  137. */
  138. function help() {
  139. $this->hr();
  140. $this->out("Not Implemented yet!");
  141. /*$this->out("Usage: postfixadmin-cli user update <args>");
  142. //$this->hr();
  143. //$this->out('Commands:');
  144. //$this->out("\n\tmodel\n\t\tbakes model in interactive mode.");
  145. //$this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation");
  146. //$this->out("");*/
  147. $this->_stop();
  148. }
  149. }
  150. class DeleteTask extends Shell {
  151. /**
  152. * Execution method always used for tasks
  153. *
  154. * @access public
  155. */
  156. function execute() {
  157. if (empty($this->args)) {
  158. $this->help();
  159. //$this->__interactive();
  160. }
  161. if (!empty($this->args[0])) {
  162. $this->__handle($this->args[0]);
  163. }
  164. }
  165. /**
  166. * Interactive
  167. *
  168. * @access private
  169. */
  170. function __interactive() {
  171. $question[] = "Which Address do you want to view?";
  172. $address = $this->in(join("\n", $question));
  173. $question = "Do you really want to delete mailbox of '$address'?";
  174. $create = $this->in($question, array('y','n'));
  175. $create == 'y' ? $create = true : $create = false;
  176. if ($create)
  177. $this->__handle($address);
  178. }
  179. /**
  180. * Interactive
  181. *
  182. * @access private
  183. */
  184. function __handle($address) {
  185. ### TODO: don't use UserHandler, instead add delete function to AliasHandler (if not already there)
  186. ### using UserHandler for deleting aliases is like taking a sledgehammer to crack a nut
  187. ### (and will probably cause some error messages that I added today ;-)
  188. ### Implemented check it please!
  189. $handler = new AliasHandler($address);
  190. $status = $handler->delete();
  191. if ($status == true) {
  192. $this->out("Mailbox of '$address' was deleted.");
  193. } else {
  194. $this->error("Error:", join("\n", $handler->errormsg));
  195. }
  196. return;
  197. }
  198. /**
  199. * Displays help contents
  200. *
  201. * @access public
  202. */
  203. function help() {
  204. $this->hr();
  205. $this->out("NOT implemented yet.");
  206. //$this->out("Usage: postfixadmin-cli user model <arg1>");
  207. //$this->hr();
  208. //$this->out('Commands:');
  209. //$this->out("\n\tdelete\n\t\tdeletes mailbox in interactive mode.");
  210. //$this->out("\n\tdelete <address>\n\t\tdeletes mailbox with address <address>");
  211. //$this->out("");
  212. $this->_stop();
  213. }
  214. }
  215. class ViewTask extends Shell {
  216. /**
  217. * Execution method always used for tasks
  218. *
  219. * @access public
  220. */
  221. function execute() {
  222. if (empty($this->args)) {
  223. $this->__interactive();
  224. }
  225. if (!empty($this->args[0])) {
  226. $this->__handle($this->args[0]);
  227. }
  228. }
  229. /**
  230. * Interactive
  231. *
  232. * @access private
  233. */
  234. function __interactive() {
  235. $question[] = "Which Alias do you want to view?";
  236. $address = $this->in(join("\n", $question));
  237. $this->__handle($address);
  238. }
  239. /**
  240. * Interactive
  241. *
  242. * @access private
  243. */
  244. function __handle($address) {
  245. $handler = new AliasHandler($address);
  246. $status = $handler->get(); # TODO: set the "all" flag?
  247. if ( ! $status) {
  248. $this->error("Error: Not Found", "The requested alias was not found!");
  249. } else {
  250. $result = $handler->return;
  251. $this->out(sprintf("Entries for: %s\n", $address));
  252. $this->out("Goto: \t");
  253. foreach($result AS $goto) {
  254. $this->out("\t -> ".$goto);
  255. }
  256. # TODO: display "deliver to mailbox"
  257. ##NEED fix in is_mailbox_alias because user is not set correctly in this scenario!
  258. /**
  259. if( $handler->is_mailbox_alias($address) )
  260. $this->out("A mailbox was set for this alias!\n");
  261. }
  262. */
  263. # TODO: display if vacation is on?
  264. /**
  265. if( $handler->is_vacation_address($address) ) {
  266. $this->out("This alias is a vacation address!");
  267. }
  268. */
  269. }
  270. return;
  271. }
  272. /**
  273. * Displays help contents
  274. *
  275. * @access public
  276. */
  277. function help() {
  278. $this->out("");
  279. $this->hr();
  280. $this->out("Usage: postfixadmin-cli user view <address>");
  281. $this->hr();
  282. $this->out('Commands:');
  283. $this->out("\n\tview\n\t\tView user. Select address in interactive mode.");
  284. $this->out("\n\tview <address>\n\t\tView user with address <address>");
  285. $this->out("");
  286. $this->_stop();
  287. }
  288. }