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\L10n\L10n as L10nClass;
11 use Friendica\Core\Logger;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBA;
17 use Friendica\Util\Strings;
20 * @author Hypolite Petovan <hypolite@mrpetovan.com>
22 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 $username = defaults($_REQUEST, 'username' , '');
65 $email = defaults($_REQUEST, 'email' , '');
66 $openid_url = defaults($_REQUEST, 'openid_url', '');
67 $nickname = defaults($_REQUEST, 'nickname' , '');
68 $photo = defaults($_REQUEST, 'photo' , '');
69 $invite_id = defaults($_REQUEST, 'invite_id' , '');
71 if (Config::get('system', 'no_openid')) {
76 $fillwith = L10n::t('You may (optionally) fill in this form via OpenID by supplying your OpenID and clicking "Register".');
77 $fillext = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
78 $oidlabel = L10n::t('Your OpenID (optional): ');
81 if (Config::get('system', 'publish_all')) {
82 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
84 $publish_tpl = Renderer::getMarkupTemplate('profile_publish.tpl');
85 $profile_publish = Renderer::replaceMacros($publish_tpl, [
87 '$pubdesc' => L10n::t('Include your profile in member directory?'),
88 '$yes_selected' => '',
89 '$no_selected' => ' checked="checked"',
90 '$str_yes' => L10n::t('Yes'),
91 '$str_no' => L10n::t('No'),
95 $ask_password = ! DBA::count('contact');
97 $tpl = Renderer::getMarkupTemplate('register.tpl');
99 $arr = ['template' => $tpl];
101 Hook::callAll('register_form', $arr);
103 $tpl = $arr['template'];
107 $o = Renderer::replaceMacros($tpl, [
108 '$invitations' => Config::get('system', 'invitation_only'),
109 '$permonly' => intval(Config::get('config', 'register_policy')) === self::APPROVE,
110 '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node'), 'required'],
111 '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
112 '$invite_label' => L10n::t('Your invitation code: '),
113 '$invite_id' => $invite_id,
114 '$regtitle' => L10n::t('Registration'),
115 '$registertext' => BBCode::convert(Config::get('config', 'register_text', '')),
116 '$fillwith' => $fillwith,
117 '$fillext' => $fillext,
118 '$oidlabel' => $oidlabel,
119 '$openid' => $openid_url,
120 '$namelabel' => L10n::t('Your Full Name (e.g. Joe Smith, real or real-looking): '),
121 '$addrlabel' => L10n::t('Your Email Address: (Initial information will be send there, so this has to be an existing address.)'),
122 '$ask_password' => $ask_password,
123 '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
124 '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
125 '$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()),
126 '$nicklabel' => L10n::t('Choose a nickname: '),
128 '$publish' => $profile_publish,
129 '$regbutt' => L10n::t('Register'),
130 '$username' => $username,
132 '$nickname' => $nickname,
133 '$sitename' => self::getApp()->getHostName(),
134 '$importh' => L10n::t('Import'),
135 '$importt' => L10n::t('Import your profile to this friendica instance'),
136 '$showtoslink' => Config::get('system', 'tosdisplay'),
137 '$tostext' => L10n::t('Terms of Service'),
138 '$showprivstatement' => Config::get('system', 'tosprivstatement'),
139 '$privstatement'=> $tos->privacy_complete,
140 '$form_security_token' => BaseModule::getFormSecurityToken('register'),
141 '$explicit_content' => Config::get('system', 'explicit_content', false),
142 '$explicit_content_note' => L10n::t('Note: This node explicitly contains adult content')
149 * @brief Module POST method to process submitted data
151 * Extend this method if the module is supposed to process POST requests.
152 * Doesn't display any content
154 public static function post()
156 BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register');
160 $arr = ['post' => $_POST];
161 Hook::callAll('register_post', $arr);
163 $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
165 $count = DBA::count('user', ['`register_date` > UTC_TIMESTAMP - INTERVAL 1 day']);
166 if ($count >= $max_dailies) {
171 switch (Config::get('config', 'register_policy')) {
184 if (empty($_SESSION['authenticated']) && empty($_SESSION['administrator'])) {
185 \notice(L10n::t('Permission denied.') . EOL);
193 $netpublish = !empty($_POST['profile_publish_reg']);
197 $arr['blocked'] = $blocked;
198 $arr['verified'] = $verified;
199 $arr['language'] = L10nClass::detectLanguage($a->getConfig()->get('system', 'language'));
202 $result = Model\User::create($arr);
203 } catch (\Exception $e) {
204 \notice($e->getMessage());
208 $user = $result['user'];
210 if ($netpublish && intval(Config::get('config', 'register_policy')) !== self::APPROVE) {
211 $url = $a->getBaseUrl() . '/profile/' . $user['nickname'];
212 Worker::add(PRIORITY_LOW, 'Directory', $url);
215 $using_invites = Config::get('system', 'invitation_only');
216 $num_invites = Config::get('system', 'number_invites');
217 $invite_id = (!empty($_POST['invite_id']) ? Strings::escapeTags(trim($_POST['invite_id'])) : '');
219 if (intval(Config::get('config', 'register_policy')) === self::OPEN) {
220 if ($using_invites && $invite_id) {
221 Model\Register::deleteByHash($invite_id);
222 PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
225 // Only send a password mail when the password wasn't manually provided
226 if (empty($_POST['password1']) || empty($_POST['confirm'])) {
227 $res = Model\User::sendRegisterOpenEmail(
229 Config::get('config', 'sitename'),
235 \info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
236 $a->internalRedirect();
239 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.',
246 \info(L10n::t('Registration successful.') . EOL);
247 $a->internalRedirect();
249 } elseif (intval(Config::get('config', 'register_policy')) === self::APPROVE) {
250 if (!strlen(Config::get('config', 'admin_email'))) {
251 \notice(L10n::t('Your registration can not be processed.') . EOL);
252 $a->internalRedirect();
255 // Check if the note to the admin is actually filled out
256 if (empty($_POST['permonlybox'])) {
257 \notice(L10n::t('You have to leave a request note for the admin.')
258 . L10n::t('Your registration can not be processed.') . EOL);
260 $a->internalRedirect('register/');
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();