]> git.mxchange.org Git - friendica.git/blob - mod/register.php
Merge pull request #6483 from MrPetovan/bug/fixes-after-2019-03-develop-rebase
[friendica.git] / mod / register.php
1 <?php
2 /**
3  * @file mod/register.php
4  */
5
6 use Friendica\App;
7 use Friendica\BaseModule;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Core\Config;
10 use Friendica\Core\Hook;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Logger;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Core\Worker;
17 use Friendica\Model;
18 use Friendica\Module\Tos;
19 use Friendica\Util\Strings;
20
21 function register_post(App $a)
22 {
23         BaseModule::checkFormSecurityTokenRedirectOnError('/register', 'register');
24
25         $arr = ['post' => $_POST];
26         Hook::callAll('register_post', $arr);
27
28         $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
29         if ($max_dailies) {
30                 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
31                 if ($r && $r[0]['total'] >= $max_dailies) {
32                         return;
33                 }
34         }
35
36         switch (Config::get('config', 'register_policy')) {
37                 case REGISTER_OPEN:
38                         $blocked = 0;
39                         $verified = 1;
40                         break;
41
42                 case REGISTER_APPROVE:
43                         $blocked = 1;
44                         $verified = 0;
45                         break;
46
47                 default:
48                 case REGISTER_CLOSED:
49                         if (empty($_SESSION['authenticated']) && empty($_SESSION['administrator'])) {
50                                 notice(L10n::t('Permission denied.') . EOL);
51                                 return;
52                         }
53                         $blocked = 1;
54                         $verified = 0;
55                         break;
56         }
57
58         $netpublish = !empty($_POST['profile_publish_reg']);
59
60         $arr = $_POST;
61
62         $arr['blocked'] = $blocked;
63         $arr['verified'] = $verified;
64         $arr['language'] = L10n::detectLanguage();
65
66         try {
67                 $result = Model\User::create($arr);
68         } catch (Exception $e) {
69                 notice($e->getMessage());
70                 return;
71         }
72
73         $user = $result['user'];
74
75         if ($netpublish && intval(Config::get('config', 'register_policy')) !== REGISTER_APPROVE) {
76                 $url = $a->getBaseUrl() . '/profile/' . $user['nickname'];
77                 Worker::add(PRIORITY_LOW, "Directory", $url);
78         }
79
80         $using_invites = Config::get('system', 'invitation_only');
81         $num_invites   = Config::get('system', 'number_invites');
82         $invite_id = (!empty($_POST['invite_id']) ? Strings::escapeTags(trim($_POST['invite_id'])) : '');
83
84         if (intval(Config::get('config', 'register_policy')) === REGISTER_OPEN) {
85                 if ($using_invites && $invite_id) {
86                         Model\Register::deleteByHash($invite_id);
87                         PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
88                 }
89
90                 // Only send a password mail when the password wasn't manually provided
91                 if (empty($_POST['password1']) || empty($_POST['confirm'])) {
92                         $res = Model\User::sendRegisterOpenEmail(
93                                 $user,
94                                 Config::get('config', 'sitename'),
95                                 $a->getBaseUrl(),
96                                 $result['password']
97                         );
98
99                         if ($res) {
100                                 info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
101                                 $a->internalRedirect();
102                         } else {
103                                 notice(
104                                         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.',
105                                                 $user['email'],
106                                                 $result['password'])
107                                         . EOL
108                                 );
109                         }
110                 } else {
111                         info(L10n::t('Registration successful.') . EOL);
112                         $a->internalRedirect();
113                 }
114         } elseif (intval(Config::get('config', 'register_policy')) === REGISTER_APPROVE) {
115                 if (!strlen(Config::get('config', 'admin_email'))) {
116                         notice(L10n::t('Your registration can not be processed.') . EOL);
117                         $a->internalRedirect();
118                 }
119
120                 Model\Register::createForApproval($user['uid'], Config::get('system', 'language'), $_POST['permonlybox']);
121
122                 // invite system
123                 if ($using_invites && $invite_id) {
124                         Model\Register::deleteByHash($invite_id);
125                         PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
126                 }
127
128                 // send email to admins
129                 $admin_mail_list = "'" . implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))))) . "'";
130                 $adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
131                         $admin_mail_list
132                 );
133
134                 // send notification to admins
135                 foreach ($adminlist as $admin) {
136                         notification([
137                                 'type'         => NOTIFY_SYSTEM,
138                                 'event'        => 'SYSTEM_REGISTER_REQUEST',
139                                 'source_name'  => $user['username'],
140                                 'source_mail'  => $user['email'],
141                                 'source_nick'  => $user['nickname'],
142                                 'source_link'  => $a->getBaseUrl() . "/admin/users/",
143                                 'link'         => $a->getBaseUrl() . "/admin/users/",
144                                 'source_photo' => $a->getBaseUrl() . "/photo/avatar/" . $user['uid'] . ".jpg",
145                                 'to_email'     => $admin['email'],
146                                 'uid'          => $admin['uid'],
147                                 'language'     => $admin['language'] ? $admin['language'] : 'en',
148                                 'show_in_notification_page' => false
149                         ]);
150                 }
151                 // send notification to the user, that the registration is pending
152                 Model\User::sendRegisterPendingEmail(
153                         $user,
154                         Config::get('config', 'sitename'),
155                         $a->getBaseURL(),
156                         $result['password']
157                 );
158
159                 info(L10n::t('Your registration is pending approval by the site owner.') . EOL);
160                 $a->internalRedirect();
161         }
162
163         return;
164 }
165
166 function register_content(App $a)
167 {
168         // logged in users can register others (people/pages/groups)
169         // even with closed registrations, unless specifically prohibited by site policy.
170         // 'block_extended_register' blocks all registrations, period.
171         $block = Config::get('system', 'block_extended_register');
172
173         if (local_user() && ($block)) {
174                 notice("Permission denied." . EOL);
175                 return;
176         }
177
178         if ((!local_user()) && (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED)) {
179                 notice("Permission denied." . EOL);
180                 return;
181         }
182
183         $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
184         if ($max_dailies) {
185                 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
186                 if ($r && $r[0]['total'] >= $max_dailies) {
187                         Logger::log('max daily registrations exceeded.');
188                         notice(L10n::t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
189                         return;
190                 }
191         }
192
193         if (!empty($_SESSION['theme'])) {
194                 unset($_SESSION['theme']);
195         }
196         if (!empty($_SESSION['mobile-theme'])) {
197                 unset($_SESSION['mobile-theme']);
198         }
199
200
201         $username   = defaults($_REQUEST, 'username'  , '');
202         $email      = defaults($_REQUEST, 'email'     , '');
203         $openid_url = defaults($_REQUEST, 'openid_url', '');
204         $nickname   = defaults($_REQUEST, 'nickname'  , '');
205         $photo      = defaults($_REQUEST, 'photo'     , '');
206         $invite_id  = defaults($_REQUEST, 'invite_id' , '');
207
208         $noid = Config::get('system', 'no_openid');
209
210         if ($noid) {
211                 $fillwith = '';
212                 $fillext  = '';
213                 $oidlabel = '';
214         } else {
215                 $fillwith = L10n::t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
216                 $fillext  = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
217                 $oidlabel = L10n::t("Your OpenID \x28optional\x29: ");
218         }
219
220         if (Config::get('system', 'publish_all')) {
221                 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
222         } else {
223                 $publish_tpl = Renderer::getMarkupTemplate("profile_publish.tpl");
224                 $profile_publish = Renderer::replaceMacros($publish_tpl, [
225                         '$instance' => 'reg',
226                         '$pubdesc' => L10n::t('Include your profile in member directory?'),
227                         '$yes_selected' => '',
228                         '$no_selected' => ' checked="checked"',
229                         '$str_yes' => L10n::t('Yes'),
230                         '$str_no' => L10n::t('No'),
231                 ]);
232         }
233
234         $r = q("SELECT COUNT(*) AS `contacts` FROM `contact`");
235         $passwords = !$r[0]["contacts"];
236
237         $tpl = Renderer::getMarkupTemplate("register.tpl");
238
239         $arr = ['template' => $tpl];
240
241         Hook::callAll('register_form', $arr);
242
243         $tpl = $arr['template'];
244
245         $tos = new Tos();
246
247         $o = Renderer::replaceMacros($tpl, [
248                 '$invitations' => Config::get('system', 'invitation_only'),
249                 '$permonly'    => intval(Config::get('config', 'register_policy')) === REGISTER_APPROVE,
250                 '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node')],
251                 '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
252                 '$invite_label' => L10n::t('Your invitation code: '),
253                 '$invite_id'  => $invite_id,
254                 '$regtitle'  => L10n::t('Registration'),
255                 '$registertext' => BBCode::convert(Config::get('config', 'register_text', '')),
256                 '$fillwith'  => $fillwith,
257                 '$fillext'   => $fillext,
258                 '$oidlabel'  => $oidlabel,
259                 '$openid'    => $openid_url,
260                 '$namelabel' => L10n::t('Your Full Name ' . "\x28" . 'e.g. Joe Smith, real or real-looking' . "\x29" . ': '),
261                 '$addrlabel' => L10n::t("Your Email Address: \x28Initial information will be send there, so this has to be an existing address.\x29"),
262                 '$passwords' => $passwords,
263                 '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
264                 '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
265                 '$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>\'.', $a->getHostName()),
266                 '$nicklabel' => L10n::t('Choose a nickname: '),
267                 '$photo'     => $photo,
268                 '$publish'   => $profile_publish,
269                 '$regbutt'   => L10n::t('Register'),
270                 '$username'  => $username,
271                 '$email'     => $email,
272                 '$nickname'  => $nickname,
273                 '$sitename'  => $a->getHostName(),
274                 '$importh'   => L10n::t('Import'),
275                 '$importt'   => L10n::t('Import your profile to this friendica instance'),
276                 '$showtoslink' => Config::get('system', 'tosdisplay'),
277                 '$tostext'   => L10n::t('Terms of Service'),
278                 '$showprivstatement' => Config::get('system', 'tosprivstatement'),
279                 '$privstatement' => $tos->privacy_complete,
280                 '$baseurl'   => System::baseurl(),
281                 '$form_security_token' => BaseModule::getFormSecurityToken("register"),
282                 '$explicit_content' => Config::get('system', 'explicit_content', false),
283                 '$explicit_content_note' => L10n::t('Note: This node explicitly contains adult content')
284         ]);
285         return $o;
286 }