3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Register a user by their email address
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.
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.
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/>.
23 * @category Email registration
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
40 * There are four cases where we're called:
42 * 1. GET, no arguments. Initial registration; ask for an email address.
43 * 2. POST, email address argument. Initial registration; send an email to confirm.
44 * 3. GET, code argument. Confirming an invitation or a registration; look them up,
45 * create the relevant user if possible, login as that user, and
46 * show a password-entry form.
47 * 4. POST, password argument. After confirmation, set the password for the new
48 * user, and redirect to a registration complete action with some instructions.
52 * @author Evan Prodromou <evan@status.net>
53 * @copyright 2011 StatusNet, Inc.
54 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
55 * @link http://status.net/
57 class EmailregisterAction extends Action
60 const SETPASSWORD = 2;
61 const NEWREGISTER = 3;
62 const CONFIRMINVITE = 4;
63 const CONFIRMREGISTER = 5;
65 const CONFIRMTYPE = 'register';
70 protected $invitation;
71 protected $confirmation;
78 function prepare($argarray)
80 parent::prepare($argarray);
82 if ($this->isPost()) {
84 $this->checkSessionToken();
86 $this->email = $this->trimmed('email');
88 if (!empty($this->email)) {
89 $this->email = common_canonical_email($this->email);
90 $this->state = self::NEWEMAIL;
92 $this->state = self::SETPASSWORD;
94 $this->code = $this->trimmed('code');
96 if (empty($this->code)) {
97 // TRANS: Client exception thrown when no confirmation code was provided.
98 throw new ClientException(_m('No confirmation code.'));
101 $this->invitation = Invitation::staticGet('code', $this->code);
103 if (empty($this->invitation)) {
105 $this->confirmation = Confirm_address::staticGet('code', $this->code);
107 if (empty($this->confirmation)) {
108 // TRANS: Client exception thrown when given confirmation code was not issued.
109 throw new ClientException(_m('No such confirmation code.'), 403);
113 $this->password1 = $this->trimmed('password1');
114 $this->password2 = $this->trimmed('password2');
116 $this->tos = $this->boolean('tos');
119 $this->code = $this->trimmed('code');
121 if (empty($this->code)) {
122 $this->state = self::NEWREGISTER;
124 $this->invitation = Invitation::staticGet('code', $this->code);
125 if (!empty($this->invitation)) {
126 $this->state = self::CONFIRMINVITE;
128 $this->state = self::CONFIRMREGISTER;
129 $this->confirmation = Confirm_address::staticGet('code', $this->code);
131 if (empty($this->confirmation)) {
132 // TRANS: Client exception thrown when given confirmation code was not issued.
133 throw new ClientException(_m('No such confirmation code.'), 405);
144 switch ($this->state) {
145 case self::NEWREGISTER:
147 // TRANS: Title for registration page.
148 return _m('TITLE','Register');
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');
162 * @param array $argarray is ignored since it's now passed in in prepare()
167 function handle($argarray=null)
169 $cur = common_current_user();
172 common_redirect(common_local_url('all', array('nickname' => $cur->nickname)));
176 switch ($this->state) {
177 case self::NEWREGISTER:
178 $this->showRegistrationForm();
181 $this->registerUser();
183 case self::CONFIRMINVITE:
184 $this->confirmRegistration();
186 case self::CONFIRMREGISTER:
187 $this->confirmRegistration();
189 case self::SETPASSWORD:
190 $this->setPassword();
196 function showRegistrationForm()
198 $this->form = new EmailRegistrationForm($this, $this->email);
202 function registerUser()
205 $confirm = EmailRegistrationPlugin::registerEmail($this->email);
206 } catch (ClientException $ce) {
207 $this->error = $ce->getMessage();
208 $this->showRegistrationForm();
212 EmailRegistrationPlugin::sendConfirmEmail($confirm);
214 // TRANS: Confirmation text after initial registration.
215 // TRANS: %s an e-mail address.
216 $prompt = sprintf(_m('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
219 $this->complete = $prompt;
224 function confirmRegistration()
226 if (!empty($this->invitation)) {
227 $email = $this->invitation->address;
228 } else if (!empty($this->confirmation)) {
229 $email = $this->confirmation->address;
232 $nickname = $this->nicknameFromEmail($email);
234 $this->form = new ConfirmRegistrationForm($this,
241 function setPassword()
243 if (Event::handle('StartRegistrationTry', array($this))) {
244 if (!empty($this->invitation)) {
245 $email = trim($this->invitation->address);
246 } else if (!empty($this->confirmation)) {
247 $email = trim($this->confirmation->address);
249 throw new Exception('No confirmation thing.');
253 // TRANS: Error text when trying to register without agreeing to the terms.
254 $this->error = _m('You must accept the terms of service and privacy policy to register.');
255 } else if (empty($this->password1)) {
256 // TRANS: Error text when trying to register without a password.
257 $this->error = _m('You must set a password');
258 } else if (strlen($this->password1) < 6) {
259 // TRANS: Error text when trying to register with too short a password.
260 $this->error = _m('Password must be 6 or more characters.');
261 } else if ($this->password1 != $this->password2) {
262 // TRANS: Error text when trying to register without providing the same password twice.
263 $this->error = _m('Passwords do not match.');
266 if (!empty($this->error)) {
267 $nickname = $this->nicknameFromEmail($email);
268 $this->form = new ConfirmRegistrationForm($this, $nickname, $email, $this->code);
273 $nickname = $this->nicknameFromEmail($email);
276 $this->user = User::register(array('nickname' => $nickname,
278 'password' => $this->password1,
279 'email_confirmed' => true));
280 } catch (ClientException $e) {
281 $this->error = $e->getMessage();
282 $nickname = $this->nicknameFromEmail($email);
283 $this->form = new ConfirmRegistrationForm($this, $nickname, $email, $this->code);
288 if (empty($this->user)) {
289 throw new Exception('Failed to register user.');
292 common_set_user($this->user);
293 // this is a real login
294 common_real_login(true);
296 // Re-init language env in case it changed (not yet, but soon)
297 common_init_language();
299 if (!empty($this->invitation)) {
300 $inviter = User::staticGet('id', $this->invitation->user_id);
301 if (!empty($inviter)) {
302 Subscription::start($inviter->getProfile(),
303 $this->user->getProfile());
306 $this->invitation->delete();
307 } else if (!empty($this->confirmation)) {
308 $this->confirmation->delete();
310 throw new Exception('No confirmation thing.');
313 Event::handle('EndRegistrationTry', array($this));
316 if (Event::handle('StartRegisterSuccess', array($this))) {
317 common_redirect(common_local_url('doc', array('title' => 'welcome')),
319 Event::handle('EndRegisterSuccess', array($this));
323 function sendConfirmEmail($confirm)
325 $sitename = common_config('site', 'name');
327 $recipients = array($confirm->address);
329 $headers['From'] = mail_notify_from();
330 $headers['To'] = trim($confirm->address);
331 // TRANS: Subject for confirmation e-mail.
332 // TRANS: %s is the StatusNet sitename.
333 $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
335 $confirmUrl = common_local_url('register', array('code' => $confirm->code));
337 // TRANS: Body for confirmation e-mail.
338 // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
339 $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
341 'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
345 'If it was not you, you can safely ignore this message.'),
349 mail_send($recipients, $headers, $body);
352 function showContent()
354 if ($this->complete) {
355 $this->elementStart('p', 'success');
356 $this->raw($this->complete);
357 $this->elementEnd('p');
360 $this->elementStart('p', 'error');
361 $this->raw($this->error);
362 $this->elementEnd('p');
365 if (!empty($this->form)) {
372 * Return true if read only.
376 * @param array $args other arguments
378 * @return boolean is read only action?
380 function isReadOnly($args)
385 function nicknameFromEmail($email)
387 return EmailRegistrationPlugin::nicknameFromEmail($email);
393 * Shows different login/register actions.
397 function showLocalNav()
399 $nav = new LoginGroupNav($this);