mailbox.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class PasswordTask extends Shell {
  3. /**
  4. * Execution method always used for tasks
  5. *
  6. * @access public
  7. */
  8. function execute() {
  9. $random = false;
  10. if (empty($this->args)) {
  11. $this->__interactive();
  12. }
  13. if (!empty($this->args[0])) {
  14. $address = $this->args[0];
  15. if (isset($this->params['g']) && $this->params['g'] == true ) {
  16. $random = true;
  17. $password = NULL;
  18. } elseif (isset($this->args[1]) && strlen($this->args[1]) > 8) { # TODO use validate_password()
  19. $password = $this->args[1];
  20. } else {
  21. $this->Dispatch->stderr('Missing <newpw> or -g. Falling back to interactive mode.');
  22. $this->__interactive();
  23. }
  24. $this->__handle($address, $password, $random);
  25. }
  26. }
  27. /**
  28. * Interactive
  29. *
  30. * @access private
  31. */
  32. function __interactive() {
  33. while(0==0) {
  34. $question = "Which address' password do you want to change?";
  35. $address = $this->in($question);
  36. 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)
  37. break;
  38. $this->err("Invalid emailaddress");
  39. }
  40. $question2[] = "Do you want to change the password?";
  41. $question2[] = "Are you really sure?";
  42. $sure = $this->in(join("\n", $question2), array('y','n'));
  43. if ($sure == 'n' ) {
  44. $this->out('You\'re not sure.');
  45. $this->_stop();
  46. }
  47. $question = "Do you want to generate a random password?";
  48. $random = $this->in($question, array('y','n'));
  49. $random == 'y' ? $random = true : $random = false;
  50. $password = NULL;
  51. if ($random == false) {
  52. $question = "Pleas enter the new password?";
  53. $password = $this->in($question);
  54. }
  55. $this->__handle($address, $password, $random);
  56. }
  57. /**
  58. * Interactive
  59. *
  60. * @access private
  61. */
  62. function __handle($address, $password = NULL, $random = false) {
  63. if ($random == true) {
  64. $password = generate_password();
  65. }
  66. if ($password != NULL) {
  67. $handler = new MailboxHandler();
  68. if (!$handler->init($address)) {
  69. $this->error("Change Password",join("\n", $handler->errormsg));
  70. }
  71. if ( ! $handler->change_pw($password, NULL, false) ){
  72. $this->error("Change Password",join("\n", $handler->errormsg));
  73. }
  74. }
  75. $this->out("");
  76. $this->out("Password updated.");
  77. $this->hr();
  78. $this->out(sprintf('The Mail address is %20s', $address));
  79. $this->out(sprintf('The new password is %20s',$password));
  80. $this->hr();
  81. return ;
  82. }
  83. /**
  84. * Displays help contents
  85. *
  86. * @access public
  87. */
  88. function help() {
  89. $this->out("");
  90. $this->hr();
  91. $this->out("Usage: postfixadmin-cli mailbox password <address> [<newpw>] [-g]");
  92. $this->hr();
  93. $this->out('Commands:');
  94. $this->out("\n\tpassword\n\t\tchanges the password in interactive mode.");
  95. $this->out("\n\tpassword <address> [<newpw>] [-g]\n\t\tchanges the password to <newpw> or if -g genereate a new pw for <address>");
  96. $this->out("");
  97. $this->_stop();
  98. }
  99. }