3 namespace Friendica\Module;
5 use Friendica\App\BaseURL;
6 use Friendica\BaseModule;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Config;
9 use Friendica\Core\Hook;
10 use Friendica\Core\L10n;
11 use Friendica\Core\L10n\L10n as L10nClass;
12 use Friendica\Core\Logger;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\Worker;
16 use Friendica\Database\DBA;
18 use Friendica\Util\Strings;
21 * @author Hypolite Petovan <hypolite@mrpetovan.com>
23 class Register extends BaseModule
30 * @brief Module GET method to display any content
32 * Extend this method if the module is supposed to return any display
33 * through a GET request. It can be an HTML page through templating or a
34 * XML feed or a JSON output.
38 public static function content()
40 // logged in users can register others (people/pages/groups)
41 // even with closed registrations, unless specifically prohibited by site policy.
42 // 'block_extended_register' blocks all registrations, period.
43 $block = Config::get('system', 'block_extended_register');
45 if (local_user() && ($block)) {
46 notice('Permission denied.' . EOL);
50 if ((!local_user()) && (intval(Config::get('config', 'register_policy')) === self::CLOSED)) {
51 notice('Permission denied.' . EOL);
55 $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
57 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
58 if ($count >= $max_dailies) {
59 Logger::log('max daily registrations exceeded.');
60 notice(L10n::t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
65 $username = $_REQUEST['username'] ?? '';
66 $email = $_REQUEST['email'] ?? '';
67 $openid_url = $_REQUEST['openid_url'] ?? '';
68 $nickname = $_REQUEST['nickname'] ?? '';
69 $photo = $_REQUEST['photo'] ?? '';
70 $invite_id = $_REQUEST['invite_id'] ?? '';
72 if (Config::get('system', 'no_openid')) {
77 $fillwith = L10n::t('You may (optionally) fill in this form via OpenID by supplying your OpenID and clicking "Register".');
78 $fillext = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
79 $oidlabel = L10n::t('Your OpenID (optional): ');
82 if (Config::get('system', 'publish_all')) {
83 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
85 $publish_tpl = Renderer::getMarkupTemplate('profile_publish.tpl');
86 $profile_publish = Renderer::replaceMacros($publish_tpl, [
88 '$pubdesc' => L10n::t('Include your profile in member directory?'),
89 '$yes_selected' => '',
90 '$no_selected' => ' checked="checked"',
91 '$str_yes' => L10n::t('Yes'),
92 '$str_no' => L10n::t('No'),
96 $ask_password = ! DBA::count('contact');
98 $tpl = Renderer::getMarkupTemplate('register.tpl');
100 $arr = ['template' => $tpl];
102 Hook::callAll('register_form', $arr);
104 $tpl = $arr['template'];
108 $o = Renderer::replaceMacros($tpl, [
109 '$invitations' => Config::get('system', 'invitation_only'),
110 '$permonly' => intval(Config::get('config', 'register_policy')) === self::APPROVE,
111 '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node'), 'required'],
112 '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
113 '$invite_label' => L10n::t('Your invitation code: '),
114 '$invite_id' => $invite_id,
115 '$regtitle' => L10n::t('Registration'),
116 '$registertext' => BBCode::convert(Config::get('config', 'register_text', '')),
117 '$fillwith' => $fillwith,
118 '$fillext' => $fillext,
119 '$oidlabel' => $oidlabel,
120 '$openid' => $openid_url,
121 '$namelabel' => L10n::t('Your Full Name (e.g. Joe Smith, real or real-looking): '),
122 '$addrlabel' => L10n::t('Your Email Address: (Initial information will be send there, so this has to be an existing address.)'),
123 '$ask_password' => $ask_password,
124 '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
125 '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
126 '$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()),
127 '$nicklabel' => L10n::t('Choose a nickname: '),
129 '$publish' => $profile_publish,
130 '$regbutt' => L10n::t('Register'),
131 '$username' => $username,
133 '$nickname' => $nickname,
134 '$sitename' => self::getApp()->getHostName(),
135 '$importh' => L10n::t('Import'),
136 '$importt' => L10n::t('Import your profile to this friendica instance'),
137 '$showtoslink' => Config::get('system', 'tosdisplay'),
138 '$tostext' => L10n::t('Terms of Service'),
139 '$showprivstatement' => Config::get('system', 'tosprivstatement'),
140 '$privstatement'=> $tos->privacy_complete,
141 '$form_security_token' => BaseModule::getFormSecurityToken('register'),
142 '$explicit_content' => Config::get('system', 'explicit_content', false),
143 '$explicit_content_note' => L10n::t('Note: This node explicitly contains adult content')
150 * @brief Module POST method to process submitted data
152 * Extend this method if the module is supposed to process POST requests.
153 * Doesn't display any content
155 public static function post()
157 BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register');
161 $arr = ['post' => $_POST];
162 Hook::callAll('register_post', $arr);
164 $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
166 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
167 if ($count >= $max_dailies) {
172 switch (Config::get('config', 'register_policy')) {
185 if (empty($_SESSION['authenticated']) && empty($_SESSION['administrator'])) {
186 \notice(L10n::t('Permission denied.') . EOL);
194 $netpublish = !empty($_POST['profile_publish_reg']);
198 $arr['blocked'] = $blocked;
199 $arr['verified'] = $verified;
200 $arr['language'] = L10nClass::detectLanguage($_SERVER, $_GET, $a->getConfig()->get('system', 'language'));
203 $result = Model\User::create($arr);
204 } catch (\Exception $e) {
205 \notice($e->getMessage());
209 $user = $result['user'];
211 $base_url = self::getClass(BaseURL::class)->get();
213 if ($netpublish && intval(Config::get('config', 'register_policy')) !== self::APPROVE) {
214 $url = $base_url . '/profile/' . $user['nickname'];
215 Worker::add(PRIORITY_LOW, 'Directory', $url);
218 $using_invites = Config::get('system', 'invitation_only');
219 $num_invites = Config::get('system', 'number_invites');
220 $invite_id = (!empty($_POST['invite_id']) ? Strings::escapeTags(trim($_POST['invite_id'])) : '');
222 if (intval(Config::get('config', 'register_policy')) === self::OPEN) {
223 if ($using_invites && $invite_id) {
224 Model\Register::deleteByHash($invite_id);
225 PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
228 // Only send a password mail when the password wasn't manually provided
229 if (empty($_POST['password1']) || empty($_POST['confirm'])) {
230 $res = Model\User::sendRegisterOpenEmail(
232 Config::get('config', 'sitename'),
238 \info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
239 $a->internalRedirect();
242 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.',
248 \info(L10n::t('Registration successful.') . EOL);
249 $a->internalRedirect();
251 } elseif (intval(Config::get('config', 'register_policy')) === self::APPROVE) {
252 if (!strlen(Config::get('config', 'admin_email'))) {
253 \notice(L10n::t('Your registration can not be processed.') . EOL);
254 $a->internalRedirect();
257 // Check if the note to the admin is actually filled out
258 if (empty($_POST['permonlybox'])) {
259 \notice(L10n::t('You have to leave a request note for the admin.')
260 . L10n::t('Your registration can not be processed.') . EOL);
262 $a->internalRedirect('register/');
265 Model\Register::createForApproval($user['uid'], Config::get('system', 'language'), $_POST['permonlybox']);
268 if ($using_invites && $invite_id) {
269 Model\Register::deleteByHash($invite_id);
270 PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
273 // send email to admins
274 $admins_stmt = DBA::select(
276 ['uid', 'language', 'email'],
277 ['email' => explode(',', str_replace(' ', '', Config::get('config', 'admin_email')))]
280 // send notification to admins
281 while ($admin = DBA::fetch($admins_stmt)) {
283 'type' => NOTIFY_SYSTEM,
284 'event' => 'SYSTEM_REGISTER_REQUEST',
285 'source_name' => $user['username'],
286 'source_mail' => $user['email'],
287 'source_nick' => $user['nickname'],
288 'source_link' => $base_url . '/admin/users/',
289 'link' => $base_url . '/admin/users/',
290 'source_photo' => $base_url . '/photo/avatar/' . $user['uid'] . '.jpg',
291 'to_email' => $admin['email'],
292 'uid' => $admin['uid'],
293 'language' => ($admin['language'] ?? '') ?: 'en',
294 'show_in_notification_page' => false
297 DBA::close($admins_stmt);
299 // send notification to the user, that the registration is pending
300 Model\User::sendRegisterPendingEmail(
302 Config::get('config', 'sitename'),
307 \info(L10n::t('Your registration is pending approval by the site owner.') . EOL);
308 $a->internalRedirect();