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($argarray)
  74. {
  75. parent::prepare($argarray);
  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->password1 = $this->trimmed('password1');
  111. $this->password2 = $this->trimmed('password2');
  112. $this->tos = $this->boolean('tos');
  113. }
  114. } else { // GET
  115. $this->code = $this->trimmed('code');
  116. if (empty($this->code)) {
  117. if (common_config('site', 'inviteonly')) {
  118. // TRANS: Client exception trown when trying to register without an invitation.
  119. throw new ClientException(_m('Sorry, only invited people can register.'), 403);
  120. }
  121. $this->state = self::NEWREGISTER;
  122. } else {
  123. $this->invitation = Invitation::getKV('code', $this->code);
  124. if (!empty($this->invitation)) {
  125. if (!empty($this->invitation->registered_user_id)) {
  126. // TRANS: Client exception trown when using an invitation multiple times.
  127. throw new ClientException(_m('Invitation already used.'), 403);
  128. }
  129. $this->state = self::CONFIRMINVITE;
  130. } else {
  131. $this->state = self::CONFIRMREGISTER;
  132. $this->confirmation = Confirm_address::getKV('code', $this->code);
  133. if (empty($this->confirmation)) {
  134. // TRANS: Client exception thrown when given confirmation code was not issued.
  135. throw new ClientException(_m('No such confirmation code.'), 405);
  136. }
  137. }
  138. }
  139. }
  140. return true;
  141. }
  142. function title()
  143. {
  144. switch ($this->state) {
  145. case self::NEWREGISTER:
  146. case self::NEWEMAIL:
  147. // TRANS: Title for registration page.
  148. return _m('TITLE','Register');
  149. break;
  150. case self::SETPASSWORD:
  151. case self::CONFIRMINVITE:
  152. case self::CONFIRMREGISTER:
  153. // TRANS: Title for page where to register with a confirmation code.
  154. return _m('TITLE','Complete registration');
  155. break;
  156. }
  157. }
  158. /**
  159. * Handler method
  160. *
  161. * @param array $argarray is ignored since it's now passed in in prepare()
  162. *
  163. * @return void
  164. */
  165. function handle($argarray=null)
  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 setPassword()
  227. {
  228. if (Event::handle('StartRegistrationTry', array($this))) {
  229. if (!empty($this->invitation)) {
  230. $email = trim($this->invitation->address);
  231. } else if (!empty($this->confirmation)) {
  232. $email = trim($this->confirmation->address);
  233. } else {
  234. // TRANS: Client exception trown when trying to set password with an invalid confirmation code.
  235. throw new Exception(_m('No confirmation thing.'));
  236. }
  237. if (!$this->tos) {
  238. // TRANS: Error text when trying to register without agreeing to the terms.
  239. $this->error = _m('You must accept the terms of service and privacy policy to register.');
  240. } else if (empty($this->password1)) {
  241. // TRANS: Error text when trying to register without a password.
  242. $this->error = _m('You must set a password');
  243. } else if (strlen($this->password1) < 6) {
  244. // TRANS: Error text when trying to register with too short a password.
  245. $this->error = _m('Password must be 6 or more characters.');
  246. } else if ($this->password1 != $this->password2) {
  247. // TRANS: Error text when trying to register without providing the same password twice.
  248. $this->error = _m('Passwords do not match.');
  249. }
  250. if (!empty($this->error)) {
  251. $nickname = $this->nicknameFromEmail($email);
  252. $this->form = new ConfirmRegistrationForm($this, $nickname, $email, $this->code);
  253. $this->showPage();
  254. return;
  255. }
  256. $nickname = $this->nicknameFromEmail($email);
  257. try {
  258. $fields = array('nickname' => $nickname,
  259. 'email' => $email,
  260. 'password' => $this->password1,
  261. 'email_confirmed' => true);
  262. if (!empty($this->invitation)) {
  263. $fields['code'] = $this->invitation->code;
  264. }
  265. $this->user = User::register($fields);
  266. } catch (ClientException $e) {
  267. $this->error = $e->getMessage();
  268. $nickname = $this->nicknameFromEmail($email);
  269. $this->form = new ConfirmRegistrationForm($this, $nickname, $email, $this->code);
  270. $this->showPage();
  271. return;
  272. }
  273. if (empty($this->user)) {
  274. // TRANS: Exception trown when using an invitation multiple times.
  275. throw new Exception(_m('Failed to register user.'));
  276. }
  277. common_set_user($this->user);
  278. // this is a real login
  279. common_real_login(true);
  280. // Re-init language env in case it changed (not yet, but soon)
  281. common_init_language();
  282. if (!empty($this->confirmation)) {
  283. $this->confirmation->delete();
  284. }
  285. Event::handle('EndRegistrationTry', array($this));
  286. }
  287. if (Event::handle('StartRegisterSuccess', array($this))) {
  288. Event::handle('EndRegisterSuccess', array($this));
  289. common_redirect(common_local_url('doc', array('title' => 'welcome')), 303);
  290. // common_redirect exits, so we can't run the event _after_ it of course.
  291. }
  292. }
  293. function sendConfirmEmail($confirm)
  294. {
  295. $sitename = common_config('site', 'name');
  296. $recipients = array($confirm->address);
  297. $headers['From'] = mail_notify_from();
  298. $headers['To'] = trim($confirm->address);
  299. // TRANS: Subject for confirmation e-mail.
  300. // TRANS: %s is the StatusNet sitename.
  301. $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
  302. $confirmUrl = common_local_url('register', array('code' => $confirm->code));
  303. // TRANS: Body for confirmation e-mail.
  304. // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
  305. $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
  306. "\n".
  307. 'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
  308. "\n".
  309. '%2$s'.
  310. "\n".
  311. 'If it was not you, you can safely ignore this message.'),
  312. $sitename,
  313. $confirmUrl);
  314. mail_send($recipients, $headers, $body);
  315. }
  316. function showContent()
  317. {
  318. if ($this->complete) {
  319. $this->elementStart('p', 'success');
  320. $this->raw($this->complete);
  321. $this->elementEnd('p');
  322. } else {
  323. if ($this->error) {
  324. $this->elementStart('p', 'error');
  325. $this->raw($this->error);
  326. $this->elementEnd('p');
  327. }
  328. if (!empty($this->form)) {
  329. $this->form->show();
  330. }
  331. }
  332. }
  333. /**
  334. * Return true if read only.
  335. *
  336. * MAY override
  337. *
  338. * @param array $args other arguments
  339. *
  340. * @return boolean is read only action?
  341. */
  342. function isReadOnly($args)
  343. {
  344. return false;
  345. }
  346. function nicknameFromEmail($email)
  347. {
  348. return EmailRegistrationPlugin::nicknameFromEmail($email);
  349. }
  350. /**
  351. * A local menu
  352. *
  353. * Shows different login/register actions.
  354. *
  355. * @return void
  356. */
  357. function showLocalNav()
  358. {
  359. $nav = new LoginGroupNav($this);
  360. $nav->show();
  361. }
  362. }