confirmfirstemail.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 $argarray misc. arguments
  55. *
  56. * @return boolean true
  57. */
  58. function prepare($argarray)
  59. {
  60. parent::prepare($argarray);
  61. $user = common_current_user();
  62. if (!empty($user)) {
  63. // TRANS: Client exception thrown when trying to register while already logged in.
  64. throw new ClientException(_m('You are already logged in.'));
  65. }
  66. $this->code = $this->trimmed('code');
  67. $this->confirm = Confirm_address::getKV('code', $this->code);
  68. if (empty($this->confirm)) {
  69. // TRANS: Client exception thrown when trying to register with a non-existing confirmation code.
  70. throw new ClientException(_m('Confirmation code not found.'));
  71. return;
  72. }
  73. $this->user = User::getKV('id', $this->confirm->user_id);
  74. if (empty($this->user)) {
  75. // TRANS: Client exception thrown when trying to register with a confirmation code that is not connected with a user.
  76. throw new ServerException(_m('No user for that confirmation code.'));
  77. }
  78. $type = $this->confirm->address_type;
  79. if ($type != 'email') {
  80. // TRANS: Client exception thrown when trying to register with a invalid e-mail address.
  81. // TRANS: %s is the invalid e-mail address.
  82. throw new ServerException(sprintf(_m('Unrecognized address type %s.'), $type));
  83. }
  84. if (!empty($this->user->email) && $this->user->email == $confirm->address) {
  85. // TRANS: Client error for an already confirmed email/jabber/sms address.
  86. throw new ClientException(_m('That address has already been confirmed.'));
  87. }
  88. if ($this->isPost()) {
  89. $this->checkSessionToken();
  90. $password = $this->trimmed('password');
  91. $confirm = $this->trimmed('confirm');
  92. if (strlen($password) < 6) {
  93. // TRANS: Client exception thrown when trying to register with too short a password.
  94. throw new ClientException(_m('Password too short.'));
  95. return;
  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. return;
  100. }
  101. $this->password = $password;
  102. }
  103. return true;
  104. }
  105. /**
  106. * Handler method
  107. *
  108. * @param array $argarray is ignored since it's now passed in in prepare()
  109. *
  110. * @return void
  111. */
  112. function handle($argarray=null)
  113. {
  114. $homepage = common_local_url('all',
  115. array('nickname' => $this->user->nickname));
  116. if ($this->isPost()) {
  117. $this->confirmUser();
  118. common_set_user($this->user);
  119. common_real_login(true);
  120. common_redirect($homepage, 303);
  121. } else {
  122. $this->showPage();
  123. }
  124. return;
  125. }
  126. function confirmUser()
  127. {
  128. $orig = clone($this->user);
  129. $this->user->email = $this->confirm->address;
  130. // Throws exception on failure.
  131. $this->user->updateWithKeys($orig);
  132. $this->user->emailChanged();
  133. $orig = clone($this->user);
  134. $this->user->password = common_munge_password($this->password, $this->user->getProfile());
  135. $this->user->update($orig);
  136. $this->confirm->delete();
  137. }
  138. function showContent()
  139. {
  140. $this->element('p', 'instructions',
  141. // TRANS: Form instructions. %s is the nickname of the to be registered user.
  142. sprintf(_m('You have confirmed the email address for your new user account %s. '.
  143. 'Use the form below to set your new password.'),
  144. $this->user->nickname));
  145. $form = new ConfirmFirstEmailForm($this, $this->code);
  146. $form->show();
  147. }
  148. function title()
  149. {
  150. // TRANS: Page title.
  151. return _m('Set a password');
  152. }
  153. }
  154. class ConfirmFirstEmailForm extends Form
  155. {
  156. public $code;
  157. function __construct($out, $code)
  158. {
  159. parent::__construct($out);
  160. $this->code = $code;
  161. }
  162. function formLegend()
  163. {
  164. // TRANS: Form legend.
  165. return _m('Confirm email address');
  166. }
  167. function action()
  168. {
  169. return common_local_url('confirmfirstemail',
  170. array('code' => $this->code));
  171. }
  172. function formClass()
  173. {
  174. return 'form_settings';
  175. }
  176. function formData()
  177. {
  178. $this->out->elementStart('ul', 'form_data');
  179. $this->out->elementStart('li');
  180. // TRANS: Field label.
  181. $this->out->password('password', _m('New password'),
  182. // TRANS: Field title for password field.
  183. _m('6 or more characters.'));
  184. $this->out->elementEnd('li');
  185. $this->out->elementStart('li');
  186. // TRANS: Field label for repeat password field.
  187. $this->out->password('confirm', _m('LABEL','Confirm'),
  188. // TRANS: Field title for repeat password field.
  189. _m('Same as password above.'));
  190. $this->out->elementEnd('li');
  191. $this->out->elementEnd('ul');
  192. }
  193. function formActions()
  194. {
  195. // TRANS: Button text for completing registration by e-mail.
  196. $this->out->submit('save', _m('BUTTON','Save'));
  197. }
  198. }