passwordsettings.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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') && !defined('LACONICA')) {
  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. 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 instructions for use
  59. */
  60. function getInstructions()
  61. {
  62. // TRANS: Instructions for page where to change password.
  63. return _('Change your password.');
  64. }
  65. function showScripts()
  66. {
  67. parent::showScripts();
  68. $this->autofocus('oldpassword');
  69. }
  70. /**
  71. * Content area of the page
  72. *
  73. * Shows a form for changing the password
  74. *
  75. * @return void
  76. */
  77. function showContent()
  78. {
  79. $user = common_current_user();
  80. $this->elementStart('form', array('method' => 'POST',
  81. 'id' => 'form_password',
  82. 'class' => 'form_settings',
  83. 'action' =>
  84. common_local_url('passwordsettings')));
  85. $this->elementStart('fieldset');
  86. // TRANS: Fieldset legend on page where to change password.
  87. $this->element('legend', null, _('Password change'));
  88. $this->hidden('token', common_session_token());
  89. $this->elementStart('ul', 'form_data');
  90. // Users who logged in with OpenID won't have a pwd
  91. if ($user->password) {
  92. $this->elementStart('li');
  93. // TRANS: Field label on page where to change password.
  94. $this->password('oldpassword', _('Old password'));
  95. $this->elementEnd('li');
  96. }
  97. $this->elementStart('li');
  98. // TRANS: Field label on page where to change password.
  99. $this->password('newpassword', _('New password'),
  100. // TRANS: Field title on page where to change password.
  101. _('6 or more characters.'));
  102. $this->elementEnd('li');
  103. $this->elementStart('li');
  104. // TRANS: Field label on page where to change password. In this field the new password should be typed a second time.
  105. $this->password('confirm', _m('LABEL','Confirm'),
  106. // TRANS: Field title on page where to change password.
  107. _('Same as password above.'));
  108. $this->elementEnd('li');
  109. $this->elementEnd('ul');
  110. // TRANS: Button text on page where to change password.
  111. $this->submit('changepass', _m('BUTTON','Change'));
  112. $this->elementEnd('fieldset');
  113. $this->elementEnd('form');
  114. }
  115. /**
  116. * Handle a post
  117. *
  118. * Validate input and save changes. Reload the form with a success
  119. * or error message.
  120. *
  121. * @return void
  122. */
  123. function handlePost()
  124. {
  125. // CSRF protection
  126. $token = $this->trimmed('token');
  127. if (!$token || $token != common_session_token()) {
  128. // TRANS: Client error displayed when the session token does not match or is not given.
  129. $this->showForm(_('There was a problem with your session token. '.
  130. 'Try again, please.'));
  131. return;
  132. }
  133. $user = common_current_user();
  134. assert(!is_null($user)); // should already be checked
  135. // FIXME: scrub input
  136. $newpassword = $this->arg('newpassword');
  137. $confirm = $this->arg('confirm');
  138. // Some validation
  139. if (strlen($newpassword) < 6) {
  140. // TRANS: Form validation error on page where to change password.
  141. $this->showForm(_('Password must be 6 or more characters.'));
  142. return;
  143. } else if (0 != strcmp($newpassword, $confirm)) {
  144. // TRANS: Form validation error on password change when password confirmation does not match.
  145. $this->showForm(_('Passwords do not match.'));
  146. return;
  147. }
  148. if ($user->password) {
  149. $oldpassword = $this->arg('oldpassword');
  150. if (!common_check_user($user->nickname, $oldpassword)) {
  151. // TRANS: Form validation error on page where to change password.
  152. $this->showForm(_('Incorrect old password.'));
  153. return;
  154. }
  155. }else{
  156. $oldpassword = null;
  157. }
  158. $success = false;
  159. if(Event::handle('StartChangePassword', array($user, $oldpassword, $newpassword))){
  160. //no handler changed the password, so change the password internally
  161. $original = clone($user);
  162. $user->password = common_munge_password($newpassword, $user->id);
  163. $val = $user->validate();
  164. if ($val !== true) {
  165. // TRANS: Form validation error on page where to change password.
  166. $this->showForm(_('Error saving user; invalid.'));
  167. return;
  168. }
  169. if (!$user->update($original)) {
  170. // TRANS: Server error displayed on page where to change password when password change
  171. // TRANS: could not be made because of a server error.
  172. $this->serverError(_('Cannot save new password.'));
  173. }
  174. Event::handle('EndChangePassword', array($user));
  175. }
  176. // TRANS: Form validation notice on page where to change password.
  177. $this->showForm(_('Password saved.'), true);
  178. }
  179. }