Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. private function __interactive() {
  31. while(true) {
  32. $question = "Which address' password do you want to change?";
  33. $address = $this->in($question);
  34. if(filter_var($address, FILTER_VALIDATE_EMAIL)) {
  35. break;
  36. }
  37. $this->err("Invalid emailaddress");
  38. }
  39. $question2[] = "Do you want to change the password?";
  40. $question2[] = "Are you really sure?";
  41. $sure = $this->in(join("\n", $question2), array('y','n'));
  42. if ($sure == 'n' ) {
  43. $this->out('You\'re not sure.');
  44. $this->_stop();
  45. }
  46. $question = "Do you want to generate a random password?";
  47. $random = $this->in($question, array('y','n'));
  48. $random == 'y' ? $random = true : $random = false;
  49. $password = NULL;
  50. if ($random == false) {
  51. $question = "Pleas enter the new password?";
  52. $password = $this->in($question);
  53. }
  54. $this->__handle($address, $password, $random);
  55. }
  56. /**
  57. * @param string $address email adress
  58. * @param string $password optional
  59. * @param boolean $random optional - true to generate random pw.
  60. */
  61. private function __handle($address, $password = NULL, $random = false) {
  62. if ($random == true) {
  63. $password = generate_password();
  64. }
  65. if ($password != NULL) {
  66. $handler = new MailboxHandler();
  67. if (!$handler->init($address)) {
  68. $this->error("Change Password",join("\n", $handler->errormsg));
  69. }
  70. if ( ! $handler->change_pw($password, NULL, false) ){
  71. $this->error("Change Password",join("\n", $handler->errormsg));
  72. }
  73. }
  74. $this->out("");
  75. $this->out("Password updated.");
  76. $this->hr();
  77. $this->out(sprintf('The Mail address is %20s', $address));
  78. $this->out(sprintf('The new password is %20s',$password));
  79. $this->hr();
  80. return ;
  81. }
  82. /**
  83. * Displays help contents
  84. *
  85. * @access public
  86. */
  87. public function help() {
  88. $this->out("");
  89. $this->hr();
  90. $this->out("Usage: postfixadmin-cli mailbox password <address> [<newpw>] [-g]");
  91. $this->hr();
  92. $this->out('Commands:');
  93. $this->out("\n\tpassword\n\t\tchanges the password in interactive mode.");
  94. $this->out("\n\tpassword <address> [<newpw>] [-g]\n\t\tchanges the password to <newpw> or if -g genereate a new pw for <address>");
  95. $this->out("");
  96. $this->_stop();
  97. }
  98. }