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