confirmfirstemail.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Action for confirming first email registration
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Confirmation
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Class comment
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class ConfirmfirstemailAction extends Action
  46. {
  47. public $confirm;
  48. public $code;
  49. public $password;
  50. public $user;
  51. /**
  52. * For initializing members of the class.
  53. *
  54. * @param array $args misc. arguments
  55. *
  56. * @return boolean true
  57. * @throws ClientException
  58. * @throws ServerException
  59. */
  60. function prepare(array $args = [])
  61. {
  62. parent::prepare($args);
  63. $user = common_current_user();
  64. if (!empty($user)) {
  65. // TRANS: Client exception thrown when trying to register while already logged in.
  66. throw new ClientException(_m('You are already logged in.'));
  67. }
  68. $this->code = $this->trimmed('code');
  69. $this->confirm = Confirm_address::getKV('code', $this->code);
  70. if (empty($this->confirm)) {
  71. // TRANS: Client exception thrown when trying to register with a non-existing confirmation code.
  72. throw new ClientException(_m('Confirmation code not found.'));
  73. }
  74. $this->user = User::getKV('id', $this->confirm->user_id);
  75. if (empty($this->user)) {
  76. // TRANS: Client exception thrown when trying to register with a confirmation code that is not connected with a user.
  77. throw new ServerException(_m('No user for that confirmation code.'));
  78. }
  79. $type = $this->confirm->address_type;
  80. if ($type != 'email') {
  81. // TRANS: Client exception thrown when trying to register with a invalid e-mail address.
  82. // TRANS: %s is the invalid e-mail address.
  83. throw new ServerException(sprintf(_m('Unrecognized address type %s.'), $type));
  84. }
  85. if (!empty($this->user->email) && $this->user->email == $this->confirm->address) {
  86. // TRANS: Client error for an already confirmed email/jabber/sms address.
  87. throw new ClientException(_m('That address has already been confirmed.'));
  88. }
  89. if ($this->isPost()) {
  90. $this->checkSessionToken();
  91. $password = $this->trimmed('password');
  92. $confirm = $this->trimmed('confirm');
  93. if (strlen($password) < 6) {
  94. // TRANS: Client exception thrown when trying to register with too short a password.
  95. throw new ClientException(_m('Password too short.'));
  96. } else if (0 != strcmp($password, $confirm)) {
  97. // TRANS: Client exception thrown when trying to register without providing the same password twice.
  98. throw new ClientException(_m('Passwords do not match.'));
  99. }
  100. $this->password = $password;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Handler method
  106. *
  107. * @return void
  108. * @throws AuthorizationException
  109. */
  110. function handle()
  111. {
  112. $homepage = common_local_url('all',
  113. array('nickname' => $this->user->nickname));
  114. if ($this->isPost()) {
  115. $this->confirmUser();
  116. common_set_user($this->user);
  117. common_real_login(true);
  118. common_redirect($homepage, 303);
  119. } else {
  120. $this->showPage();
  121. }
  122. return;
  123. }
  124. function confirmUser()
  125. {
  126. $orig = clone($this->user);
  127. $this->user->email = $this->confirm->address;
  128. // Throws exception on failure.
  129. $this->user->updateWithKeys($orig);
  130. $this->user->emailChanged();
  131. $orig = clone($this->user);
  132. $this->user->password = common_munge_password($this->password, $this->user->getProfile());
  133. $this->user->update($orig);
  134. $this->confirm->delete();
  135. }
  136. function showContent()
  137. {
  138. $this->element('p', 'instructions',
  139. // TRANS: Form instructions. %s is the nickname of the to be registered user.
  140. sprintf(_m('You have confirmed the email address for your new user account %s. ' .
  141. 'Use the form below to set your new password.'),
  142. $this->user->nickname));
  143. $form = new ConfirmFirstEmailForm($this, $this->code);
  144. $form->show();
  145. }
  146. function title()
  147. {
  148. // TRANS: Page title.
  149. return _m('Set a password');
  150. }
  151. }
  152. class ConfirmFirstEmailForm extends Form
  153. {
  154. public $code;
  155. function __construct($out, $code)
  156. {
  157. parent::__construct($out);
  158. $this->code = $code;
  159. }
  160. function formLegend()
  161. {
  162. // TRANS: Form legend.
  163. return _m('Confirm email address');
  164. }
  165. function action()
  166. {
  167. return common_local_url('confirmfirstemail',
  168. array('code' => $this->code));
  169. }
  170. function formClass()
  171. {
  172. return 'form_settings';
  173. }
  174. function formData()
  175. {
  176. $this->out->elementStart('ul', 'form_data');
  177. $this->out->elementStart('li');
  178. // TRANS: Field label.
  179. $this->out->password('password', _m('New password'),
  180. // TRANS: Field title for password field.
  181. _m('6 or more characters.'));
  182. $this->out->elementEnd('li');
  183. $this->out->elementStart('li');
  184. // TRANS: Field label for repeat password field.
  185. $this->out->password('confirm', _m('LABEL', 'Confirm'),
  186. // TRANS: Field title for repeat password field.
  187. _m('Same as password above.'));
  188. $this->out->elementEnd('li');
  189. $this->out->elementEnd('ul');
  190. }
  191. function formActions()
  192. {
  193. // TRANS: Button text for completing registration by e-mail.
  194. $this->out->submit('save', _m('BUTTON', 'Save'));
  195. }
  196. }