passwordsettings.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Change user password
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2008-2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Change password
  35. *
  36. * @category Settings
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @author Zach Copley <zach@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. class PasswordsettingsAction extends SettingsAction
  44. {
  45. /**
  46. * Title of the page
  47. *
  48. * @return string Title of the page
  49. */
  50. public function title()
  51. {
  52. // TRANS: Title for page where to change password.
  53. return _m('TITLE', 'Change password');
  54. }
  55. /**
  56. * Instructions for use
  57. *
  58. * @return string instructions for use
  59. */
  60. public function getInstructions()
  61. {
  62. // TRANS: Instructions for page where to change password.
  63. return _('Change your password.');
  64. }
  65. public function showScripts()
  66. {
  67. parent::showScripts();
  68. $this->autofocus('oldpassword');
  69. }
  70. public function showContent()
  71. {
  72. $this->elementStart('form', ['method' => 'POST',
  73. 'id' => 'form_password',
  74. 'class' => 'form_settings',
  75. 'action' => common_local_url('passwordsettings')]);
  76. $this->elementStart('fieldset');
  77. // TRANS: Fieldset legend on page where to change password.
  78. $this->element('legend', null, _('Password change'));
  79. $this->hidden('token', common_session_token());
  80. $this->elementStart('ul', 'form_data');
  81. // Users who logged in with OpenID won't have a pwd
  82. if ($this->scoped->hasPassword()) {
  83. $this->elementStart('li');
  84. // TRANS: Field label on page where to change password.
  85. $this->password('oldpassword', _('Old password'));
  86. $this->elementEnd('li');
  87. }
  88. $this->elementStart('li');
  89. // TRANS: Field label on page where to change password.
  90. $this->password('newpassword', _('New password'),
  91. // TRANS: Field title on page where to change password.
  92. _('6 or more characters.'));
  93. $this->elementEnd('li');
  94. $this->elementStart('li');
  95. // TRANS: Field label on page where to change password. In this field the new password should be typed a second time.
  96. $this->password('confirm', _m('LABEL', 'Confirm'),
  97. // TRANS: Field title on page where to change password.
  98. _('Same as password above.'));
  99. $this->elementEnd('li');
  100. $this->elementEnd('ul');
  101. // TRANS: Button text on page where to change password.
  102. $this->submit('changepass', _m('BUTTON', 'Change'));
  103. $this->elementEnd('fieldset');
  104. $this->elementEnd('form');
  105. }
  106. protected function doPost()
  107. {
  108. // FIXME: scrub input
  109. $newpassword = $this->arg('newpassword');
  110. $confirm = $this->arg('confirm');
  111. // Some validation
  112. if (strlen($newpassword) < 6) {
  113. // TRANS: Form validation error on page where to change password.
  114. throw new ClientException(_('Password must be 6 or more characters.'));
  115. } elseif (0 != strcmp($newpassword, $confirm)) {
  116. // TRANS: Form validation error on password change when password confirmation does not match.
  117. throw new ClientException(_('Passwords do not match.'));
  118. }
  119. $oldpassword = null;
  120. if ($this->scoped->hasPassword()) {
  121. $oldpassword = $this->arg('oldpassword');
  122. if (!common_check_user($this->scoped->getNickname(), $oldpassword)) {
  123. // TRANS: Form validation error on page where to change password.
  124. throw new ClientException(_('Incorrect old password.'));
  125. }
  126. }
  127. if (Event::handle('StartChangePassword', [$this->scoped, $oldpassword, $newpassword])) {
  128. // no handler changed the password, so change the password internally
  129. $user = $this->scoped->getUser();
  130. $user->setPassword($newpassword);
  131. Event::handle('EndChangePassword', [$this->scoped]);
  132. }
  133. // TRANS: Form validation notice on page where to change password.
  134. return _('Password saved.');
  135. }
  136. }