3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Email-based registration, as on the StatusNet OnDemand service
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.
38 * Email based registration plugin
40 * @category Email registration
42 * @author Brion Vibber <brionv@status.net>
43 * @author Evan Prodromou <evan@status.net>
44 * @copyright 2011 StatusNet, Inc.
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
46 * @link http://status.net/
48 class EmailRegistrationPlugin extends Plugin
50 const CONFIRMTYPE = 'register';
52 function onAutoload($cls)
54 $dir = dirname(__FILE__);
58 case 'EmailregisterAction':
59 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
61 case 'EmailRegistrationForm':
62 case 'ConfirmRegistrationForm':
63 include_once $dir . '/' . strtolower($cls) . '.php';
70 function onArgsInitialize(&$args)
72 if (array_key_exists('action', $args) && $args['action'] == 'register') {
74 $args['action'] = 'emailregister';
79 function onLoginAction($action, &$login)
81 if ($action == 'emailregister') {
88 function onStartLoadDoc(&$title, &$output)
90 $dir = dirname(__FILE__);
92 // @todo FIXME: i18n issue.
93 $docFile = DocFile::forTitle($title, $dir.'/doc-src/');
95 if (!empty($docFile)) {
96 $output = $docFile->toHTML();
103 static function registerEmail($email)
105 $old = User::staticGet('email', $email);
108 // TRANS: Error text when trying to register with an already registered e-mail address.
109 // TRANS: %s is the URL to recover password at.
110 throw new ClientException(sprintf(_m('A user with that email address already exists. You can use the '.
111 '<a href="%s">password recovery</a> tool to recover a missing password.'),
112 common_local_url('recoverpassword')));
117 if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
118 $valid = Validate::email($email, common_config('email', 'check_domain'));
119 Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
123 // TRANS: Error text when trying to register with an invalid e-mail address.
124 throw new ClientException(_m('Not a valid email address.'));
127 $confirm = Confirm_address::getAddress($email, self::CONFIRMTYPE);
129 if (empty($confirm)) {
130 $confirm = Confirm_address::saveNew(null, $email, 'register');
136 static function nicknameFromEmail($email)
138 $parts = explode('@', $email);
140 $nickname = $parts[0];
142 $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
144 $nickname = Nickname::normalize($nickname);
146 $original = $nickname;
150 while (User::staticGet('nickname', $nickname)) {
152 $nickname = $original . $n;
158 static function sendConfirmEmail($confirm)
160 $sitename = common_config('site', 'name');
162 $recipients = array($confirm->address);
164 $headers['From'] = mail_notify_from();
165 $headers['To'] = trim($confirm->address);
166 // TRANS: Subject for confirmation e-mail.
167 // TRANS: %s is the StatusNet sitename.
168 $headers['Subject'] = sprintf(_m('Confirm your registration on %s'), $sitename);
170 $confirmUrl = common_local_url('register', array('code' => $confirm->code));
172 // TRANS: Body for confirmation e-mail.
173 // TRANS: %1$s is the StatusNet sitename, %2$s is the confirmation URL.
174 $body = sprintf(_m('Someone (probably you) has requested an account on %1$s using this email address.'.
176 'To confirm the address, click the following URL or copy it into the address bar of your browser.'.
180 'If it was not you, you can safely ignore this message.'),
184 mail_send($recipients, $headers, $body);
187 function onPluginVersion(&$versions)
189 $versions[] = array('name' => 'EmailRegistration',
190 'version' => STATUSNET_VERSION,
191 'author' => 'Evan Prodromou',
192 'homepage' => 'http://status.net/wiki/Plugin:EmailRegistration',
194 // TRANS: Plugin description.
195 _m('Use email only for registration.'));