emailregister.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Register a user by their email address
  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 Email registration
  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. * Email registration
  37. *
  38. * There are four cases where we're called:
  39. *
  40. * 1. GET, no arguments. Initial registration; ask for an email address.
  41. * 2. POST, email address argument. Initial registration; send an email to confirm.
  42. * 3. GET, code argument. Confirming an invitation or a registration; look them up,
  43. * create the relevant user if possible, login as that user, and
  44. * show a password-entry form.
  45. * 4. POST, password argument. After confirmation, set the password for the new
  46. * user, and redirect to a registration complete action with some instructions.
  47. *
  48. * @category Action
  49. * @package StatusNet
  50. * @author Evan Prodromou <evan@status.net>
  51. * @copyright 2011 StatusNet, Inc.
  52. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  53. * @link http://status.net/
  54. */
  55. class EmailregisterAction extends Action
  56. {
  57. const NEWEMAIL = 1;
  58. const SETPASSWORD = 2;
  59. const NEWREGISTER = 3;
  60. const CONFIRMINVITE = 4;
  61. const CONFIRMREGISTER = 5;
  62. const CONFIRMTYPE = 'register';
  63. protected $user;
  64. protected $email;
  65. protected $code;
  66. protected $invitation;
  67. protected $confirmation;
  68. protected $password1;
  69. protected $password2;
  70. protected $state;
  71. protected $error;
  72. protected $complete;
  73. function prepare(array $args = [])
  74. {
  75. parent::prepare($args);
  76. if (common_config('site', 'closed')) {
  77. // TRANS: Client exception trown when registration by e-mail is not allowed.
  78. throw new ClientException(_m('Registration not allowed.'), 403);
  79. }
  80. if ($this->isPost()) {
  81. $this->checkSessionToken();
  82. $this->email = $this->trimmed('email');
  83. if (!empty($this->email)) {
  84. if (common_config('site', 'inviteonly')) {
  85. // TRANS: Client exception trown when trying to register without an invitation.
  86. throw new ClientException(_m('Sorry, only invited people can register.'), 403);
  87. }
  88. $this->email = common_canonical_email($this->email);
  89. $this->state = self::NEWEMAIL;
  90. } else {
  91. $this->state = self::SETPASSWORD;
  92. $this->code = $this->trimmed('code');
  93. if (empty($this->code)) {
  94. // TRANS: Client exception thrown when no confirmation code was provided.
  95. throw new ClientException(_m('No confirmation code.'));
  96. }
  97. $this->invitation = Invitation::getKV('code', $this->code);
  98. if (!empty($this->invitation)) {
  99. if (!empty($this->invitation->registered_user_id)) {
  100. // TRANS: Client exception trown when using an invitation multiple times.
  101. throw new ClientException(_m('Invitation already used.'), 403);
  102. }
  103. } else {
  104. $this->confirmation = Confirm_address::getKV('code', $this->code);
  105. if (empty($this->confirmation)) {
  106. // TRANS: Client exception thrown when given confirmation code was not issued.
  107. throw new ClientException(_m('No such confirmation code.'), 403);
  108. }
  109. }
  110. $this->nickname = Nickname::normalize($this->trimmed('nickname'));
  111. $this->password1 = $this->trimmed('password1');
  112. $this->password2 = $this->trimmed('password2');
  113. $this->tos = $this->boolean('tos');
  114. }
  115. } else { // GET
  116. $this->code = $this->trimmed('code');
  117. if (empty($this->code)) {
  118. if (common_config('site', 'inviteonly')) {
  119. // TRANS: Client exception trown when trying to register without an invitation.
  120. throw new ClientException(_m('Sorry, only invited people can register.'), 403);
  121. }
  122. $this->state = self::NEWREGISTER;
  123. } else {
  124. $this->invitation = Invitation::getKV('code', $this->code);
  125. if (!empty($this->invitation)) {
  126. if (!empty($this->invitation->registered_user_id)) {
  127. // TRANS: Client exception trown when using an invitation multiple times.
  128. throw new ClientException(_m('Invitation already used.'), 403);
  129. }
  130. $this->state = self::CONFIRMINVITE;
  131. } else {
  132. $this->state = self::CONFIRMREGISTER;
  133. $this->confirmation = Confirm_address::getKV('code', $this->code);
  134. if (empty($this->confirmation)) {
  135. // TRANS: Client exception thrown when given confirmation code was not issued.
  136. throw new ClientException(_m('No such confirmation code.'), 405);
  137. }
  138. }
  139. }
  140. }
  141. return true;
  142. }
  143. function title()
  144. {
  145. switch ($this->state) {
  146. case self::NEWREGISTER:
  147. case self::NEWEMAIL:
  148. // TRANS: Title for registration page.
  149. return _m('TITLE', 'Register');
  150. break;
  151. case self::SETPASSWORD:
  152. case self::CONFIRMINVITE:
  153. case self::CONFIRMREGISTER:
  154. // TRANS: Title for page where to register with a confirmation code.
  155. return _m('TITLE', 'Complete registration');
  156. break;
  157. }
  158. }
  159. /**
  160. * Handler method
  161. *
  162. * @return void
  163. * @throws Exception
  164. */
  165. function handle()
  166. {
  167. $cur = common_current_user();
  168. if (!empty($cur)) {
  169. common_redirect(common_local_url('all', array('nickname' => $cur->nickname)));
  170. }
  171. switch ($this->state) {
  172. case self::NEWREGISTER:
  173. $this->showRegistrationForm();
  174. break;
  175. case self::NEWEMAIL:
  176. $this->registerUser();
  177. break;
  178. case self::CONFIRMINVITE:
  179. $this->confirmRegistration();
  180. break;
  181. case self::CONFIRMREGISTER:
  182. $this->confirmRegistration();
  183. break;
  184. case self::SETPASSWORD:
  185. $this->setPassword();
  186. break;
  187. }
  188. return;
  189. }
  190. function showRegistrationForm()
  191. {
  192. $this->form = new EmailRegistrationForm($this, $this->email);
  193. $this->showPage();
  194. }
  195. function registerUser()
  196. {
  197. try {
  198. $confirm = EmailRegistrationPlugin::registerEmail($this->email);
  199. } catch (ClientException $ce) {
  200. $this->error = $ce->getMessage();
  201. $this->showRegistrationForm();
  202. return;
  203. }
  204. EmailRegistrationPlugin::sendConfirmEmail($confirm);
  205. // TRANS: Confirmation text after initial registration.
  206. // TRANS: %s an e-mail address.
  207. $prompt = sprintf(_m('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
  208. $this->email);
  209. $this->complete = $prompt;
  210. $this->showPage();
  211. }
  212. function confirmRegistration()
  213. {
  214. if (!empty($this->invitation)) {
  215. $email = $this->invitation->address;
  216. } else if (!empty($this->confirmation)) {
  217. $email = $this->confirmation->address;
  218. }
  219. $nickname = $this->nicknameFromEmail($email);
  220. $this->form = new ConfirmRegistrationForm($this,
  221. $nickname,
  222. $email,
  223. $this->code);
  224. $this->showPage();
  225. }
  226. function nicknameFromEmail($email)
  227. {
  228. return EmailRegistrationPlugin::nicknameFromEmail($email);
  229. }
  230. function setPassword()
  231. {
  232. if (Event::handle('StartRegistrationTry', array($this))) {
  233. if (!empty($this->invitation)) {
  234. $email = trim($this->invitation->address);
  235. } else if (!empty($this->confirmation)) {
  236. $email = trim($this->confirmation->address);
  237. } else {
  238. // TRANS: Client exception trown when trying to set password with an invalid confirmation code.
  239. throw new Exception(_m('No confirmation thing.'));
  240. }
  241. if (!$this->tos) {
  242. // TRANS: Error text when trying to register without agreeing to the terms.
  243. $this->error = _m('You must accept the terms of service and privacy policy to register.');
  244. } else if (empty($this->password1)) {
  245. // TRANS: Error text when trying to register without a password.
  246. $this->error = _m('You must set a password');
  247. } else if (strlen($this->password1) < 6) {
  248. // TRANS: Error text when trying to register with too short a password.
  249. $this->error = _m('Password must be 6 or more characters.');
  250. } else if ($this->password1 != $this->password2) {
  251. // TRANS: Error text when trying to register without providing the same password twice.
  252. $this->error = _m('Passwords do not match.');
  253. }
  254. if (!empty($this->error)) {
  255. $this->form = new ConfirmRegistrationForm($this, $this->nickname, $email, $this->code);
  256. $this->showPage();
  257. return;
  258. }
  259. try {
  260. $fields = array('nickname' => $this->nickname,
  261. 'email' => $email,
  262. 'password' => $this->password1,
  263. 'email_confirmed' => true);
  264. if (!empty($this->invitation)) {
  265. $fields['code'] = $this->invitation->code;
  266. }
  267. $this->user = User::register($fields);
  268. } catch (ClientException $e) {
  269. $this->error = $e->getMessage();
  270. $this->form = new ConfirmRegistrationForm($this, $this->nickname, $email, $this->code);
  271. $this->showPage();
  272. return;
  273. }
  274. if (empty($this->user)) {
  275. // TRANS: Exception trown when using an invitation multiple times.
  276. throw new Exception(_m('Failed to register user.'));
  277. }
  278. common_set_user($this->user);
  279. // this is a real login
  280. common_real_login(true);
  281. // Re-init language env in case it changed (not yet, but soon)
  282. common_init_language();
  283. if (!empty($this->confirmation)) {
  284. try {
  285. $this->confirmation->delete();
  286. } catch (ServerException $e) {
  287. common_log(LOG_ERR, $e->getMessage());
  288. }
  289. }
  290. Event::handle('EndRegistrationTry', array($this));
  291. }
  292. if (Event::handle('StartRegisterSuccess', array($this))) {
  293. Event::handle('EndRegisterSuccess', array($this));
  294. common_redirect(common_local_url('doc', array('title' => 'welcome')), 303);
  295. // common_redirect exits, so we can't run the event _after_ it of course.
  296. }
  297. }
  298. function sendConfirmEmail($confirm)
  299. {
  300. $sitename = common_config('site', 'name');
  301. $recipients = array($confirm->address);
  302. $headers['From'] = mail_notify_from();
  303. $headers['To'] = trim($confirm->address);
  304. // TRANS: Subject for confirmation e-mail.
  305. // TRANS: %s is the StatusNet sitename.
  306. $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
  307. $confirmUrl = common_local_url('register', array('code' => $confirm->code));
  308. // TRANS: Body for confirmation e-mail.
  309. // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
  310. $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.' .
  311. "\n" .
  312. 'To confirm the address, click the following URL or copy it into the address bar of your browser.' .
  313. "\n" .
  314. '%2$s' .
  315. "\n" .
  316. 'If it was not you, you can safely ignore this message.'),
  317. $sitename,
  318. $confirmUrl);
  319. mail_send($recipients, $headers, $body);
  320. }
  321. function showContent()
  322. {
  323. if ($this->complete) {
  324. $this->elementStart('p', 'success');
  325. $this->raw($this->complete);
  326. $this->elementEnd('p');
  327. } else {
  328. if ($this->error) {
  329. $this->elementStart('p', 'error');
  330. $this->raw($this->error);
  331. $this->elementEnd('p');
  332. }
  333. if (!empty($this->form)) {
  334. $this->form->show();
  335. }
  336. }
  337. }
  338. /**
  339. * Return true if read only.
  340. *
  341. * MAY override
  342. *
  343. * @param array $args other arguments
  344. *
  345. * @return boolean is read only action?
  346. */
  347. function isReadOnly($args)
  348. {
  349. return false;
  350. }
  351. /**
  352. * A local menu
  353. *
  354. * Shows different login/register actions.
  355. *
  356. * @return void
  357. */
  358. function showLocalNav()
  359. {
  360. $nav = new LoginGroupNav($this);
  361. $nav->show();
  362. }
  363. }