3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Hook;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Worker;
31 use Friendica\Database\DBA;
34 use Friendica\Util\Strings;
37 * @author Hypolite Petovan <hypolite@mrpetovan.com>
39 class Register extends BaseModule
46 * Module GET method to display any content
48 * Extend this method if the module is supposed to return any display
49 * through a GET request. It can be an HTML page through templating or a
50 * XML feed or a JSON output.
54 public static function content(array $parameters = [])
56 // logged in users can register others (people/pages/groups)
57 // even with closed registrations, unless specifically prohibited by site policy.
58 // 'block_extended_register' blocks all registrations, period.
59 $block = DI::config()->get('system', 'block_extended_register');
61 if (local_user() && $block) {
62 notice(DI::l10n()->t('Permission denied.'));
67 $user = DBA::selectFirst('user', ['parent-uid'], ['uid' => local_user()]);
68 if (!empty($user['parent-uid'])) {
69 notice(DI::l10n()->t('Only parent users can create additional accounts.'));
74 if (!local_user() && (intval(DI::config()->get('config', 'register_policy')) === self::CLOSED)) {
75 notice(DI::l10n()->t('Permission denied.'));
79 $max_dailies = intval(DI::config()->get('system', 'max_daily_registrations'));
81 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
82 if ($count >= $max_dailies) {
83 Logger::log('max daily registrations exceeded.');
84 notice(DI::l10n()->t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.'));
89 $username = $_REQUEST['username'] ?? '';
90 $email = $_REQUEST['email'] ?? '';
91 $openid_url = $_REQUEST['openid_url'] ?? '';
92 $nickname = $_REQUEST['nickname'] ?? '';
93 $photo = $_REQUEST['photo'] ?? '';
94 $invite_id = $_REQUEST['invite_id'] ?? '';
96 if (local_user() || DI::config()->get('system', 'no_openid')) {
101 $fillwith = DI::l10n()->t('You may (optionally) fill in this form via OpenID by supplying your OpenID and clicking "Register".');
102 $fillext = DI::l10n()->t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
103 $oidlabel = DI::l10n()->t('Your OpenID (optional): ');
106 if (DI::config()->get('system', 'publish_all')) {
107 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
109 $publish_tpl = Renderer::getMarkupTemplate('profile/publish.tpl');
110 $profile_publish = Renderer::replaceMacros($publish_tpl, [
111 '$instance' => 'reg',
112 '$pubdesc' => DI::l10n()->t('Include your profile in member directory?'),
113 '$yes_selected' => '',
114 '$no_selected' => ' checked="checked"',
115 '$str_yes' => DI::l10n()->t('Yes'),
116 '$str_no' => DI::l10n()->t('No'),
120 $ask_password = !DBA::count('contact');
122 $tpl = Renderer::getMarkupTemplate('register.tpl');
124 $arr = ['template' => $tpl];
126 Hook::callAll('register_form', $arr);
128 $tpl = $arr['template'];
132 $o = Renderer::replaceMacros($tpl, [
133 '$invitations' => DI::config()->get('system', 'invitation_only'),
134 '$permonly' => intval(DI::config()->get('config', 'register_policy')) === self::APPROVE,
135 '$permonlybox' => ['permonlybox', DI::l10n()->t('Note for the admin'), '', DI::l10n()->t('Leave a message for the admin, why you want to join this node'), DI::l10n()->t('Required')],
136 '$invite_desc' => DI::l10n()->t('Membership on this site is by invitation only.'),
137 '$invite_label' => DI::l10n()->t('Your invitation code: '),
138 '$invite_id' => $invite_id,
139 '$regtitle' => DI::l10n()->t('Registration'),
140 '$registertext' => BBCode::convert(DI::config()->get('config', 'register_text', '')),
141 '$fillwith' => $fillwith,
142 '$fillext' => $fillext,
143 '$oidlabel' => $oidlabel,
144 '$openid' => $openid_url,
145 '$namelabel' => DI::l10n()->t('Your Full Name (e.g. Joe Smith, real or real-looking): '),
146 '$addrlabel' => DI::l10n()->t('Your Email Address: (Initial information will be send there, so this has to be an existing address.)'),
147 '$addrlabel2' => DI::l10n()->t('Please repeat your e-mail address:'),
148 '$ask_password' => $ask_password,
149 '$password1' => ['password1', DI::l10n()->t('New Password:'), '', DI::l10n()->t('Leave empty for an auto generated password.')],
150 '$password2' => ['confirm', DI::l10n()->t('Confirm:'), '', ''],
151 '$nickdesc' => DI::l10n()->t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be "<strong>nickname@%s</strong>".', DI::baseUrl()->getHostname()),
152 '$nicklabel' => DI::l10n()->t('Choose a nickname: '),
154 '$publish' => $profile_publish,
155 '$regbutt' => DI::l10n()->t('Register'),
156 '$username' => $username,
158 '$nickname' => $nickname,
159 '$sitename' => DI::baseUrl()->getHostname(),
160 '$importh' => DI::l10n()->t('Import'),
161 '$importt' => DI::l10n()->t('Import your profile to this friendica instance'),
162 '$showtoslink' => DI::config()->get('system', 'tosdisplay'),
163 '$tostext' => DI::l10n()->t('Terms of Service'),
164 '$showprivstatement' => DI::config()->get('system', 'tosprivstatement'),
165 '$privstatement'=> $tos->privacy_complete,
166 '$form_security_token' => BaseModule::getFormSecurityToken('register'),
167 '$explicit_content' => DI::config()->get('system', 'explicit_content', false),
168 '$explicit_content_note' => DI::l10n()->t('Note: This node explicitly contains adult content'),
169 '$additional' => !empty(local_user()),
170 '$parent_password' => ['parent_password', DI::l10n()->t('Parent Password:'), '', DI::l10n()->t('Please enter the password of the parent account to legitimize your request.')]
178 * Module POST method to process submitted data
180 * Extend this method if the module is supposed to process POST requests.
181 * Doesn't display any content
183 public static function post(array $parameters = [])
185 BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register');
187 $arr = ['post' => $_POST];
188 Hook::callAll('register_post', $arr);
190 $additional_account = false;
192 if (!local_user() && !empty($arr['post']['parent_password'])) {
193 notice(DI::l10n()->t('Permission denied.'));
195 } elseif (local_user() && !empty($arr['post']['parent_password'])) {
197 Model\User::getIdFromPasswordAuthentication(local_user(), $arr['post']['parent_password']);
198 } catch (\Exception $ex) {
199 notice(DI::l10n()->t("Password doesn't match."));
200 $regdata = ['nickname' => $arr['post']['nickname'], 'username' => $arr['post']['username']];
201 DI::baseUrl()->redirect('register?' . http_build_query($regdata));
203 $additional_account = true;
204 } elseif (local_user()) {
205 notice(DI::l10n()->t('Please enter your password.'));
206 $regdata = ['nickname' => $arr['post']['nickname'], 'username' => $arr['post']['username']];
207 DI::baseUrl()->redirect('register?' . http_build_query($regdata));
210 $max_dailies = intval(DI::config()->get('system', 'max_daily_registrations'));
212 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
213 if ($count >= $max_dailies) {
218 switch (DI::config()->get('config', 'register_policy')) {
231 if (empty($_SESSION['authenticated']) && empty($_SESSION['administrator'])) {
232 notice(DI::l10n()->t('Permission denied.'));
240 $netpublish = !empty($_POST['profile_publish_reg']);
244 // Is there text in the tar pit?
245 if (!empty($arr['email'])) {
246 Logger::info('Tar pit', $arr);
247 notice(DI::l10n()->t('You have entered too much information.'));
248 DI::baseUrl()->redirect('register/');
251 if ($additional_account) {
252 $user = DBA::selectFirst('user', ['email'], ['uid' => local_user()]);
253 if (!DBA::isResult($user)) {
254 notice(DI::l10n()->t('User not found.'));
255 DI::baseUrl()->redirect('register');
261 $arr['password1'] = $arr['confirm'] = $arr['parent_password'];
262 $arr['repeat'] = $arr['email'] = $user['email'];
264 // Overwriting the "tar pit" field with the real one
265 $arr['email'] = $arr['field1'];
268 if ($arr['email'] != $arr['repeat']) {
269 Logger::info('Mail mismatch', $arr);
270 notice(DI::l10n()->t('Please enter the identical mail address in the second field.'));
271 $regdata = ['email' => $arr['email'], 'nickname' => $arr['nickname'], 'username' => $arr['username']];
272 DI::baseUrl()->redirect('register?' . http_build_query($regdata));
275 $arr['blocked'] = $blocked;
276 $arr['verified'] = $verified;
277 $arr['language'] = L10n::detectLanguage($_SERVER, $_GET, DI::config()->get('system', 'language'));
280 $result = Model\User::create($arr);
281 } catch (\Exception $e) {
282 notice($e->getMessage());
286 $user = $result['user'];
288 $base_url = DI::baseUrl()->get();
290 if ($netpublish && intval(DI::config()->get('config', 'register_policy')) !== self::APPROVE) {
291 $url = $base_url . '/profile/' . $user['nickname'];
292 Worker::add(PRIORITY_LOW, 'Directory', $url);
295 if ($additional_account) {
296 DBA::update('user', ['parent-uid' => local_user()], ['uid' => $user['uid']]);
297 info(DI::l10n()->t('The additional account was created.'));
298 DI::baseUrl()->redirect('delegation');
301 $using_invites = DI::config()->get('system', 'invitation_only');
302 $num_invites = DI::config()->get('system', 'number_invites');
303 $invite_id = (!empty($_POST['invite_id']) ? Strings::escapeTags(trim($_POST['invite_id'])) : '');
305 if (intval(DI::config()->get('config', 'register_policy')) === self::OPEN) {
306 if ($using_invites && $invite_id) {
307 Model\Register::deleteByHash($invite_id);
308 DI::pConfig()->set($user['uid'], 'system', 'invites_remaining', $num_invites);
311 // Only send a password mail when the password wasn't manually provided
312 if (empty($_POST['password1']) || empty($_POST['confirm'])) {
313 $res = Model\User::sendRegisterOpenEmail(
314 DI::l10n()->withLang($arr['language']),
316 DI::config()->get('config', 'sitename'),
322 info(DI::l10n()->t('Registration successful. Please check your email for further instructions.'));
323 DI::baseUrl()->redirect();
326 DI::l10n()->t('Failed to send email message. Here your accout details:<br> login: %s<br> password: %s<br><br>You can change your password after login.',
332 info(DI::l10n()->t('Registration successful.'));
333 DI::baseUrl()->redirect();
335 } elseif (intval(DI::config()->get('config', 'register_policy')) === self::APPROVE) {
336 if (!strlen(DI::config()->get('config', 'admin_email'))) {
337 notice(DI::l10n()->t('Your registration can not be processed.'));
338 DI::baseUrl()->redirect();
341 // Check if the note to the admin is actually filled out
342 if (empty($_POST['permonlybox'])) {
343 notice(DI::l10n()->t('You have to leave a request note for the admin.')
344 . DI::l10n()->t('Your registration can not be processed.'));
346 DI::baseUrl()->redirect('register/');
349 Model\Register::createForApproval($user['uid'], DI::config()->get('system', 'language'), $_POST['permonlybox']);
352 if ($using_invites && $invite_id) {
353 Model\Register::deleteByHash($invite_id);
354 DI::pConfig()->set($user['uid'], 'system', 'invites_remaining', $num_invites);
357 // send email to admins
358 $admins_stmt = DBA::select(
360 ['uid', 'language', 'email'],
361 ['email' => explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')))]
364 // send notification to admins
365 while ($admin = DBA::fetch($admins_stmt)) {
367 'type' => Model\Notification\Type::SYSTEM,
368 'event' => 'SYSTEM_REGISTER_REQUEST',
369 'uid' => $admin['uid'],
370 'link' => $base_url . '/admin/users/',
371 'source_name' => $user['username'],
372 'source_mail' => $user['email'],
373 'source_nick' => $user['nickname'],
374 'source_link' => $base_url . '/admin/users/',
375 'source_photo' => $base_url . '/photo/avatar/' . $user['uid'] . '.jpg',
376 'show_in_notification_page' => false
379 DBA::close($admins_stmt);
381 // send notification to the user, that the registration is pending
382 Model\User::sendRegisterPendingEmail(
384 DI::config()->get('config', 'sitename'),
389 info(DI::l10n()->t('Your registration is pending approval by the site owner.'));
390 DI::baseUrl()->redirect();