3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Core\Config;
8 use Friendica\Core\Hook;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\System;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBA;
17 use Friendica\Util\Strings;
20 * @author Hypolite Petovan <hypolite@mrpetovan.com>
22 abstract class Register extends BaseModule
29 * @brief Module GET method to display any content
31 * Extend this method if the module is supposed to return any display
32 * through a GET request. It can be an HTML page through templating or a
33 * XML feed or a JSON output.
37 public static function content()
39 // logged in users can register others (people/pages/groups)
40 // even with closed registrations, unless specifically prohibited by site policy.
41 // 'block_extended_register' blocks all registrations, period.
42 $block = Config::get('system', 'block_extended_register');
44 if (local_user() && ($block)) {
45 notice('Permission denied.' . EOL);
49 if ((!local_user()) && (intval(Config::get('config', 'register_policy')) === self::CLOSED)) {
50 notice('Permission denied.' . EOL);
54 $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
56 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
57 if ($count >= $max_dailies) {
58 Logger::log('max daily registrations exceeded.');
59 notice(L10n::t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
64 if (!empty($_SESSION['theme'])) {
65 unset($_SESSION['theme']);
67 if (!empty($_SESSION['mobile-theme'])) {
68 unset($_SESSION['mobile-theme']);
71 $username = defaults($_REQUEST, 'username' , '');
72 $email = defaults($_REQUEST, 'email' , '');
73 $openid_url = defaults($_REQUEST, 'openid_url', '');
74 $nickname = defaults($_REQUEST, 'nickname' , '');
75 $photo = defaults($_REQUEST, 'photo' , '');
76 $invite_id = defaults($_REQUEST, 'invite_id' , '');
78 if (Config::get('system', 'no_openid')) {
83 $fillwith = L10n::t('You may (optionally) fill in this form via OpenID by supplying your OpenID and clicking "Register".');
84 $fillext = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
85 $oidlabel = L10n::t('Your OpenID (optional): ');
88 if (Config::get('system', 'publish_all')) {
89 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
91 $publish_tpl = Renderer::getMarkupTemplate('profile_publish.tpl');
92 $profile_publish = Renderer::replaceMacros($publish_tpl, [
94 '$pubdesc' => L10n::t('Include your profile in member directory?'),
95 '$yes_selected' => '',
96 '$no_selected' => ' checked="checked"',
97 '$str_yes' => L10n::t('Yes'),
98 '$str_no' => L10n::t('No'),
102 $ask_password = ! DBA::count('contact');
104 $tpl = Renderer::getMarkupTemplate('register.tpl');
106 $arr = ['template' => $tpl];
108 Hook::callAll('register_form', $arr);
110 $tpl = $arr['template'];
114 $o = Renderer::replaceMacros($tpl, [
115 '$invitations' => Config::get('system', 'invitation_only'),
116 '$permonly' => intval(Config::get('config', 'register_policy')) === self::APPROVE,
117 '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node')],
118 '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
119 '$invite_label' => L10n::t('Your invitation code: '),
120 '$invite_id' => $invite_id,
121 '$regtitle' => L10n::t('Registration'),
122 '$registertext' => BBCode::convert(Config::get('config', 'register_text', '')),
123 '$fillwith' => $fillwith,
124 '$fillext' => $fillext,
125 '$oidlabel' => $oidlabel,
126 '$openid' => $openid_url,
127 '$namelabel' => L10n::t('Your Full Name (e.g. Joe Smith, real or real-looking): '),
128 '$addrlabel' => L10n::t('Your Email Address: (Initial information will be send there, so this has to be an existing address.)'),
129 '$ask_password' => $ask_password,
130 '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
131 '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
132 '$nickdesc' => 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>".', self::getApp()->getHostName()),
133 '$nicklabel' => L10n::t('Choose a nickname: '),
135 '$publish' => $profile_publish,
136 '$regbutt' => L10n::t('Register'),
137 '$username' => $username,
139 '$nickname' => $nickname,
140 '$sitename' => self::getApp()->getHostName(),
141 '$importh' => L10n::t('Import'),
142 '$importt' => L10n::t('Import your profile to this friendica instance'),
143 '$showtoslink' => Config::get('system', 'tosdisplay'),
144 '$tostext' => L10n::t('Terms of Service'),
145 '$showprivstatement' => Config::get('system', 'tosprivstatement'),
146 '$privstatement'=> $tos->privacy_complete,
147 '$baseurl' => System::baseurl(),
148 '$form_security_token' => BaseModule::getFormSecurityToken('register'),
149 '$explicit_content' => Config::get('system', 'explicit_content', false),
150 '$explicit_content_note' => L10n::t('Note: This node explicitly contains adult content')
157 * @brief Module POST method to process submitted data
159 * Extend this method if the module is supposed to process POST requests.
160 * Doesn't display any content
162 public static function post()
164 BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register');
168 $arr = ['post' => $_POST];
169 Hook::callAll('register_post', $arr);
171 $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
173 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
174 if ($count >= $max_dailies) {
179 switch (Config::get('config', 'register_policy')) {
192 if (empty($_SESSION['authenticated']) && empty($_SESSION['administrator'])) {
193 \notice(L10n::t('Permission denied.') . EOL);
201 $netpublish = !empty($_POST['profile_publish_reg']);
205 $arr['blocked'] = $blocked;
206 $arr['verified'] = $verified;
207 $arr['language'] = L10n::detectLanguage();
210 $result = Model\User::create($arr);
211 } catch (\Exception $e) {
212 \notice($e->getMessage());
216 $user = $result['user'];
218 if ($netpublish && intval(Config::get('config', 'register_policy')) !== self::APPROVE) {
219 $url = $a->getBaseUrl() . '/profile/' . $user['nickname'];
220 Worker::add(PRIORITY_LOW, 'Directory', $url);
223 $using_invites = Config::get('system', 'invitation_only');
224 $num_invites = Config::get('system', 'number_invites');
225 $invite_id = (!empty($_POST['invite_id']) ? Strings::escapeTags(trim($_POST['invite_id'])) : '');
227 if (intval(Config::get('config', 'register_policy')) === self::OPEN) {
228 if ($using_invites && $invite_id) {
229 Model\Register::deleteByHash($invite_id);
230 PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
233 // Only send a password mail when the password wasn't manually provided
234 if (empty($_POST['password1']) || empty($_POST['confirm'])) {
235 $res = Model\User::sendRegisterOpenEmail(
237 Config::get('config', 'sitename'),
243 \info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
244 $a->internalRedirect();
247 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.',
254 \info(L10n::t('Registration successful.') . EOL);
255 $a->internalRedirect();
257 } elseif (intval(Config::get('config', 'register_policy')) === self::APPROVE) {
258 if (!strlen(Config::get('config', 'admin_email'))) {
259 \notice(L10n::t('Your registration can not be processed.') . EOL);
260 $a->internalRedirect();
263 Model\Register::createForApproval($user['uid'], Config::get('system', 'language'), $_POST['permonlybox']);
266 if ($using_invites && $invite_id) {
267 Model\Register::deleteByHash($invite_id);
268 PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
271 // send email to admins
272 $admins_stmt = DBA::select(
274 ['uid', 'language', 'email'],
275 ['email' => explode(',', str_replace(' ', '', Config::get('config', 'admin_email')))]
278 // send notification to admins
279 while ($admin = DBA::fetch($admins_stmt)) {
281 'type' => NOTIFY_SYSTEM,
282 'event' => 'SYSTEM_REGISTER_REQUEST',
283 'source_name' => $user['username'],
284 'source_mail' => $user['email'],
285 'source_nick' => $user['nickname'],
286 'source_link' => $a->getBaseUrl() . '/admin/users/',
287 'link' => $a->getBaseUrl() . '/admin/users/',
288 'source_photo' => $a->getBaseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
289 'to_email' => $admin['email'],
290 'uid' => $admin['uid'],
291 'language' => defaults($admin, 'language', 'en'),
292 'show_in_notification_page' => false
295 DBA::close($admins_stmt);
297 // send notification to the user, that the registration is pending
298 Model\User::sendRegisterPendingEmail(
300 Config::get('config', 'sitename'),
305 \info(L10n::t('Your registration is pending approval by the site owner.') . EOL);
306 $a->internalRedirect();