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/
58 class EmailregisterAction extends Action
61 const SETPASSWORD = 2;
62 const NEWREGISTER = 3;
63 const CONFIRMINVITE = 4;
64 const CONFIRMREGISTER = 5;
66 const CONFIRMTYPE = 'register';
71 protected $invitation;
72 protected $confirmation;
79 function prepare($argarray)
81 parent::prepare($argarray);
83 if ($this->isPost()) {
85 $this->checkSessionToken();
87 $this->email = $this->trimmed('email');
89 if (!empty($this->email)) {
90 $this->email = common_canonical_email($this->email);
91 $this->state = self::NEWEMAIL;
93 $this->state = self::SETPASSWORD;
95 $this->code = $this->trimmed('code');
97 if (empty($this->code)) {
98 throw new ClientException(_('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 throw new ClientException(_('No such confirmation code.'), 403);
112 $this->password1 = $this->trimmed('password1');
113 $this->password2 = $this->trimmed('password2');
115 $this->tos = $this->boolean('tos');
118 $this->code = $this->trimmed('code');
120 if (empty($this->code)) {
121 $this->state = self::NEWREGISTER;
123 $this->invitation = Invitation::staticGet('code', $this->code);
124 if (!empty($this->invitation)) {
125 $this->state = self::CONFIRMINVITE;
127 $this->state = self::CONFIRMREGISTER;
128 $this->confirmation = Confirm_address::staticGet('code', $this->code);
130 if (empty($this->confirmation)) {
131 throw new ClientException(_('No such confirmation code.'), 405);
142 switch ($this->state) {
143 case self::NEWREGISTER:
145 // TRANS: Title for registration page.
146 return _m('TITLE','Register');
148 case self::SETPASSWORD:
149 case self::CONFIRMINVITE:
150 case self::CONFIRMREGISTER:
151 // TRANS: Title for page where to change password.
152 return _m('TITLE','Complete registration');
160 * @param array $argarray is ignored since it's now passed in in prepare()
165 function handle($argarray=null)
167 switch ($this->state) {
168 case self::NEWREGISTER:
169 $this->showRegistrationForm();
172 $this->registerUser();
174 case self::CONFIRMINVITE:
175 $this->confirmRegistration();
177 case self::CONFIRMREGISTER:
178 $this->confirmRegistration();
180 case self::SETPASSWORD:
181 $this->setPassword();
187 function showRegistrationForm()
189 $this->form = new EmailRegistrationForm($this, $this->email);
193 function registerUser()
195 $old = User::staticGet('email', $this->email);
198 $this->error = sprintf(_('A user with that email address already exists. You can use the '.
199 '<a href="%s">password recovery</a> tool to recover a missing password.'),
200 common_local_url('recoverpassword'));
201 $this->showRegistrationForm();
207 if (Event::handle('StartValidateUserEmail', array(null, $this->email, &$valid))) {
208 $valid = Validate::email($this->email, common_config('email', 'check_domain'));
209 Event::handle('EndValidateUserEmail', array(null, $this->email, &$valid));
213 $this->error = _('Not a valid email address.');
214 $this->showRegistrationForm();
218 $confirm = Confirm_address::getAddress($this->email, self::CONFIRMTYPE);
220 if (empty($confirm)) {
221 $confirm = Confirm_address::saveNew(null, $this->email, 'register');
222 $prompt = sprintf(_('An email was sent to %s to confirm that address. Check your email inbox for instructions.'),
225 $prompt = sprintf(_('The address %s was already registered but not confirmed. The confirmation code was resent.'),
229 $this->sendConfirmEmail($confirm);
231 $this->complete = $prompt;
236 function confirmRegistration()
238 if (!empty($this->invitation)) {
239 $email = $this->invitation->address;
240 } else if (!empty($this->confirmation)) {
241 $email = $this->confirmation->address;
244 $nickname = $this->nicknameFromEmail($email);
246 $this->form = new ConfirmRegistrationForm($this,
253 function setPassword()
255 if (Event::handle('StartRegistrationTry', array($this))) {
256 if (!empty($this->invitation)) {
257 $email = $this->invitation->address;
258 } else if (!empty($this->confirmation)) {
259 $email = $this->confirmation->address;
261 throw new Exception('No confirmation thing.');
265 $this->error = _('You must accept the terms of service and privacy policy to register.');
266 $nickname = $this->nicknameFromEmail($email);
267 $this->form = new ConfirmRegistrationForm($this, $nickname, $this->email, $this->code);
272 $nickname = $this->nicknameFromEmail($email);
274 $this->user = User::register(array('nickname' => $nickname,
276 'email_confirmed' => true));
278 if (empty($this->user)) {
279 throw new Exception("Failed to register user.");
282 common_set_user($this->user);
283 // this is a real login
284 common_real_login(true);
286 // Re-init language env in case it changed (not yet, but soon)
287 common_init_language();
289 if (!empty($this->invitation)) {
290 $inviter = User::staticGet('id', $this->invitation->user_id);
291 if (!empty($inviter)) {
292 Subscription::start($inviter->getProfile(),
293 $user->getProfile());
296 $this->invitation->delete();
297 } else if (!empty($this->confirmation)) {
298 $this->confirmation->delete();
300 throw new Exception('No confirmation thing.');
303 Event::handle('EndRegistrationTry', array($this));
306 common_redirect(common_local_url('doc', array('title' => 'welcome')),
310 function sendConfirmEmail($confirm)
312 $sitename = common_config('site', 'name');
314 $recipients = array($confirm->address);
316 $headers['From'] = mail_notify_from();
317 $headers['To'] = trim($confirm->address);
318 $headers['Subject'] = sprintf(_('Confirm your registration on %1$s'), $sitename);
320 $confirmUrl = common_local_url('register', array('code' => $confirm->code));
322 $body = sprintf(_('Someone (probably you) has requested an account on %1$s using this email address.'.
324 'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
328 'If it was not you, you can safely ignore this message.'),
332 mail_send($recipients, $headers, $body);
335 function showContent()
337 if ($this->complete) {
338 $this->elementStart('p', 'success');
339 $this->raw($this->complete);
340 $this->elementEnd('p');
343 $this->elementStart('p', 'error');
344 $this->raw($this->error);
345 $this->elementEnd('p');
348 if (!empty($this->form)) {
355 * Return true if read only.
359 * @param array $args other arguments
361 * @return boolean is read only action?
364 function isReadOnly($args)
369 function nicknameFromEmail($email)
371 $parts = explode('@', $email);
373 $nickname = $parts[0];
375 $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
377 $nickname = Nickname::normalize($nickname);
379 $original = $nickname;
383 while (User::staticGet('nickname', $nickname)) {
385 $nickname = $original . $n;
394 * Shows different login/register actions.
399 function showLocalNav()
401 $nav = new LoginGroupNav($this);