emailregister.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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->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. * @param array $argarray is ignored since it's now passed in in prepare()
  163. *
  164. * @return void
  165. */
  166. function handle($argarray=null)
  167. {
  168. $cur = common_current_user();
  169. if (!empty($cur)) {
  170. common_redirect(common_local_url('all', array('nickname' => $cur->nickname)));
  171. }
  172. switch ($this->state) {
  173. case self::NEWREGISTER:
  174. $this->showRegistrationForm();
  175. break;
  176. case self::NEWEMAIL:
  177. $this->registerUser();
  178. break;
  179. case self::CONFIRMINVITE:
  180. $this->confirmRegistration();
  181. break;
  182. case self::CONFIRMREGISTER:
  183. $this->confirmRegistration();
  184. break;
  185. case self::SETPASSWORD:
  186. $this->setPassword();
  187. break;
  188. }
  189. return;
  190. }
  191. function showRegistrationForm()
  192. {
  193. $this->form = new EmailRegistrationForm($this, $this->email);
  194. $this->showPage();
  195. }
  196. function registerUser()
  197. {
  198. try {
  199. $confirm = EmailRegistrationPlugin::registerEmail($this->email);
  200. } catch (ClientException $ce) {
  201. $this->error = $ce->getMessage();
  202. $this->showRegistrationForm();
  203. return;
  204. }
  205. EmailRegistrationPlugin::sendConfirmEmail($confirm);
  206. // TRANS: Confirmation text after initial registration.
  207. // TRANS: %s an e-mail address.
  208. $prompt = sprintf(_m('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
  209. $this->email);
  210. $this->complete = $prompt;
  211. $this->showPage();
  212. }
  213. function confirmRegistration()
  214. {
  215. if (!empty($this->invitation)) {
  216. $email = $this->invitation->address;
  217. } else if (!empty($this->confirmation)) {
  218. $email = $this->confirmation->address;
  219. }
  220. $nickname = $this->nicknameFromEmail($email);
  221. $this->form = new ConfirmRegistrationForm($this,
  222. $nickname,
  223. $email,
  224. $this->code);
  225. $this->showPage();
  226. }
  227. function setPassword()
  228. {
  229. if (Event::handle('StartRegistrationTry', array($this))) {
  230. if (!empty($this->invitation)) {
  231. $email = trim($this->invitation->address);
  232. } else if (!empty($this->confirmation)) {
  233. $email = trim($this->confirmation->address);
  234. } else {
  235. // TRANS: Client exception trown when trying to set password with an invalid confirmation code.
  236. throw new Exception(_m('No confirmation thing.'));
  237. }
  238. if (!$this->tos) {
  239. // TRANS: Error text when trying to register without agreeing to the terms.
  240. $this->error = _m('You must accept the terms of service and privacy policy to register.');
  241. } else if (empty($this->password1)) {
  242. // TRANS: Error text when trying to register without a password.
  243. $this->error = _m('You must set a password');
  244. } else if (strlen($this->password1) < 6) {
  245. // TRANS: Error text when trying to register with too short a password.
  246. $this->error = _m('Password must be 6 or more characters.');
  247. } else if ($this->password1 != $this->password2) {
  248. // TRANS: Error text when trying to register without providing the same password twice.
  249. $this->error = _m('Passwords do not match.');
  250. }
  251. if (!empty($this->error)) {
  252. $this->form = new ConfirmRegistrationForm($this, $this->nickname, $email, $this->code);
  253. $this->showPage();
  254. return;
  255. }
  256. try {
  257. $fields = array('nickname' => $this->nickname,
  258. 'email' => $email,
  259. 'password' => $this->password1,
  260. 'email_confirmed' => true);
  261. if (!empty($this->invitation)) {
  262. $fields['code'] = $this->invitation->code;
  263. }
  264. $this->user = User::register($fields);
  265. } catch (ClientException $e) {
  266. $this->error = $e->getMessage();
  267. $this->form = new ConfirmRegistrationForm($this, $this->nickname, $email, $this->code);
  268. $this->showPage();
  269. return;
  270. }
  271. if (empty($this->user)) {
  272. // TRANS: Exception trown when using an invitation multiple times.
  273. throw new Exception(_m('Failed to register user.'));
  274. }
  275. common_set_user($this->user);
  276. // this is a real login
  277. common_real_login(true);
  278. // Re-init language env in case it changed (not yet, but soon)
  279. common_init_language();
  280. if (!empty($this->confirmation)) {
  281. $this->confirmation->delete();
  282. }
  283. Event::handle('EndRegistrationTry', array($this));
  284. }
  285. if (Event::handle('StartRegisterSuccess', array($this))) {
  286. Event::handle('EndRegisterSuccess', array($this));
  287. common_redirect(common_local_url('doc', array('title' => 'welcome')), 303);
  288. // common_redirect exits, so we can't run the event _after_ it of course.
  289. }
  290. }
  291. function sendConfirmEmail($confirm)
  292. {
  293. $sitename = common_config('site', 'name');
  294. $recipients = array($confirm->address);
  295. $headers['From'] = mail_notify_from();
  296. $headers['To'] = trim($confirm->address);
  297. // TRANS: Subject for confirmation e-mail.
  298. // TRANS: %s is the StatusNet sitename.
  299. $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
  300. $confirmUrl = common_local_url('register', array('code' => $confirm->code));
  301. // TRANS: Body for confirmation e-mail.
  302. // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
  303. $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
  304. "\n".
  305. 'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
  306. "\n".
  307. '%2$s'.
  308. "\n".
  309. 'If it was not you, you can safely ignore this message.'),
  310. $sitename,
  311. $confirmUrl);
  312. mail_send($recipients, $headers, $body);
  313. }
  314. function showContent()
  315. {
  316. if ($this->complete) {
  317. $this->elementStart('p', 'success');
  318. $this->raw($this->complete);
  319. $this->elementEnd('p');
  320. } else {
  321. if ($this->error) {
  322. $this->elementStart('p', 'error');
  323. $this->raw($this->error);
  324. $this->elementEnd('p');
  325. }
  326. if (!empty($this->form)) {
  327. $this->form->show();
  328. }
  329. }
  330. }
  331. /**
  332. * Return true if read only.
  333. *
  334. * MAY override
  335. *
  336. * @param array $args other arguments
  337. *
  338. * @return boolean is read only action?
  339. */
  340. function isReadOnly($args)
  341. {
  342. return false;
  343. }
  344. function nicknameFromEmail($email)
  345. {
  346. return EmailRegistrationPlugin::nicknameFromEmail($email);
  347. }
  348. /**
  349. * A local menu
  350. *
  351. * Shows different login/register actions.
  352. *
  353. * @return void
  354. */
  355. function showLocalNav()
  356. {
  357. $nav = new LoginGroupNav($this);
  358. $nav->show();
  359. }
  360. }