]> git.mxchange.org Git - friendica.git/blob - mod/register.php
Replace System::baseUrl() by App->getBaseURL() in mod/register
[friendica.git] / mod / register.php
1 <?php
2 /**
3  * @file mod/register.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
15 use Friendica\Model;
16 use Friendica\Module\Tos;
17 use Friendica\Util\DateTimeFormat;
18
19 require_once 'include/enotify.php';
20
21 function register_post(App $a)
22 {
23         check_form_security_token_redirectOnErr('/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::getBrowserLanguage();
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 = ((x($_POST, 'invite_id')) ? notags(trim($_POST['invite_id'])) : '');
86
87         if (intval(Config::get('config', 'register_policy')) === REGISTER_OPEN) {
88                 if ($using_invites && $invite_id) {
89                         q("delete * from register where hash = '%s' limit 1", DBA::escape($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 (!x($_POST, 'password1') || !x($_POST, 'confirm')) {
95                         $res = Model\User::sendRegisterOpenEmail(
96                                 $user['email'],
97                                 Config::get('config', 'sitename'),
98                                 $a->getBaseUrl(),
99                                 $user['username'],
100                                 $result['password'],
101                                 $user
102                         );
103
104                         if ($res) {
105                                 info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
106                                 goaway();
107                         } else {
108                                 notice(
109                                         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.',
110                                                 $user['email'],
111                                                 $result['password'])
112                                         . EOL
113                                 );
114                         }
115                 } else {
116                         info(L10n::t('Registration successful.') . EOL);
117                         goaway();
118                 }
119         } elseif (intval(Config::get('config', 'register_policy')) === REGISTER_APPROVE) {
120                 if (!strlen(Config::get('config', 'admin_email'))) {
121                         notice(L10n::t('Your registration can not be processed.') . EOL);
122                         goaway();
123                 }
124
125                 $hash = random_string();
126                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password`, `language`, `note` ) VALUES ( '%s', '%s', %d, '%s', '%s', '%s' ) ",
127                         DBA::escape($hash),
128                         DBA::escape(DateTimeFormat::utcNow()),
129                         intval($user['uid']),
130                         DBA::escape($result['password']),
131                         DBA::escape(Config::get('system', 'language')),
132                         DBA::escape($_POST['permonlybox'])
133                 );
134
135                 // invite system
136                 if ($using_invites && $invite_id) {
137                         q("DELETE * FROM `register` WHERE `hash` = '%s' LIMIT 1", DBA::escape($invite_id));
138                         PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
139                 }
140
141                 // send email to admins
142                 $admin_mail_list = "'" . implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))))) . "'";
143                 $adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
144                         $admin_mail_list
145                 );
146
147                 // send notification to admins
148                 foreach ($adminlist as $admin) {
149                         notification([
150                                 'type'         => NOTIFY_SYSTEM,
151                                 'event'        => 'SYSTEM_REGISTER_REQUEST',
152                                 'source_name'  => $user['username'],
153                                 'source_mail'  => $user['email'],
154                                 'source_nick'  => $user['nickname'],
155                                 'source_link'  => $a->getBaseUrl() . "/admin/users/",
156                                 'link'         => $a->getBaseUrl() . "/admin/users/",
157                                 'source_photo' => $a->getBaseUrl() . "/photo/avatar/" . $user['uid'] . ".jpg",
158                                 'to_email'     => $admin['email'],
159                                 'uid'          => $admin['uid'],
160                                 'language'     => $admin['language'] ? $admin['language'] : 'en',
161                                 'show_in_notification_page' => false
162                         ]);
163                 }
164                 // send notification to the user, that the registration is pending
165                 Model\User::sendRegisterPendingEmail(
166                         $user['email'],
167                         Config::get('config', 'sitename'),
168                         $user['username'],
169                         $a->getBaseURL(),
170                         $user['nickname'],
171                         $result['password']
172                 );
173
174                 info(L10n::t('Your registration is pending approval by the site owner.') . EOL);
175                 goaway();
176         }
177
178         return;
179 }
180
181 function register_content(App $a)
182 {
183         // logged in users can register others (people/pages/groups)
184         // even with closed registrations, unless specifically prohibited by site policy.
185         // 'block_extended_register' blocks all registrations, period.
186         $block = Config::get('system', 'block_extended_register');
187
188         if (local_user() && ($block)) {
189                 notice("Permission denied." . EOL);
190                 return;
191         }
192
193         if ((!local_user()) && (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED)) {
194                 notice("Permission denied." . EOL);
195                 return;
196         }
197
198         $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
199         if ($max_dailies) {
200                 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
201                 if ($r && $r[0]['total'] >= $max_dailies) {
202                         logger('max daily registrations exceeded.');
203                         notice(L10n::t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
204                         return;
205                 }
206         }
207
208         if (x($_SESSION, 'theme')) {
209                 unset($_SESSION['theme']);
210         }
211         if (x($_SESSION, 'mobile-theme')) {
212                 unset($_SESSION['mobile-theme']);
213         }
214
215
216         $username   = x($_REQUEST, 'username')   ? $_REQUEST['username']   : '';
217         $email      = x($_REQUEST, 'email')      ? $_REQUEST['email']      : '';
218         $openid_url = x($_REQUEST, 'openid_url') ? $_REQUEST['openid_url'] : '';
219         $nickname   = x($_REQUEST, 'nickname')   ? $_REQUEST['nickname']   : '';
220         $photo      = x($_REQUEST, 'photo')      ? $_REQUEST['photo']      : '';
221         $invite_id  = x($_REQUEST, 'invite_id')  ? $_REQUEST['invite_id']  : '';
222
223         $noid = Config::get('system', 'no_openid');
224
225         if ($noid) {
226                 $oidhtml  = '';
227                 $fillwith = '';
228                 $fillext  = '';
229                 $oidlabel = '';
230         } else {
231                 $oidhtml  = '<label for="register-openid" id="label-register-openid" >$oidlabel</label><input type="text" maxlength="60" size="32" name="openid_url" class="openid" id="register-openid" value="$openid" >';
232                 $fillwith = L10n::t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
233                 $fillext  = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
234                 $oidlabel = L10n::t("Your OpenID \x28optional\x29: ");
235         }
236
237         // I set this and got even more fake names than before...
238         $realpeople = ''; // L10n::t('Members of this network prefer to communicate with real people who use their real names.');
239
240         if (Config::get('system', 'publish_all')) {
241                 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
242         } else {
243                 $publish_tpl = get_markup_template("profile_publish.tpl");
244                 $profile_publish = replace_macros($publish_tpl, [
245                         '$instance' => 'reg',
246                         '$pubdesc' => L10n::t('Include your profile in member directory?'),
247                         '$yes_selected' => '',
248                         '$no_selected' => ' checked="checked"',
249                         '$str_yes' => L10n::t('Yes'),
250                         '$str_no' => L10n::t('No'),
251                 ]);
252         }
253
254         $r = q("SELECT COUNT(*) AS `contacts` FROM `contact`");
255         $passwords = !$r[0]["contacts"];
256
257         $license = '';
258
259         $tpl = get_markup_template("register.tpl");
260
261         $arr = ['template' => $tpl];
262
263         Addon::callHooks('register_form', $arr);
264
265         $tpl = $arr['template'];
266
267         $tos = new Tos();
268
269         $o = replace_macros($tpl, [
270                 '$oidhtml' => $oidhtml,
271                 '$invitations' => Config::get('system', 'invitation_only'),
272                 '$permonly'    => intval(Config::get('config', 'register_policy')) === REGISTER_APPROVE,
273                 '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node')],
274                 '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
275                 '$invite_label' => L10n::t('Your invitation code: '),
276                 '$invite_id'  => $invite_id,
277                 '$realpeople' => $realpeople,
278                 '$regtitle'  => L10n::t('Registration'),
279                 '$registertext' => BBCode::convert(Config::get('config', 'register_text', '')),
280                 '$fillwith'  => $fillwith,
281                 '$fillext'   => $fillext,
282                 '$oidlabel'  => $oidlabel,
283                 '$openid'    => $openid_url,
284                 '$namelabel' => L10n::t('Your Full Name ' . "\x28" . 'e.g. Joe Smith, real or real-looking' . "\x29" . ': '),
285                 '$addrlabel' => L10n::t("Your Email Address: \x28Initial information will be send there, so this has to be an existing address.\x29"),
286                 '$passwords' => $passwords,
287                 '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
288                 '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
289                 '$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()),
290                 '$nicklabel' => L10n::t('Choose a nickname: '),
291                 '$photo'     => $photo,
292                 '$publish'   => $profile_publish,
293                 '$regbutt'   => L10n::t('Register'),
294                 '$username'  => $username,
295                 '$email'     => $email,
296                 '$nickname'  => $nickname,
297                 '$license'   => $license,
298                 '$sitename'  => $a->getHostName(),
299                 '$importh'   => L10n::t('Import'),
300                 '$importt'   => L10n::t('Import your profile to this friendica instance'),
301                 '$showtoslink' => Config::get('system', 'tosdisplay'),
302                 '$tostext'   => L10n::t('Terms of Service'),
303                 '$showprivstatement' => Config::get('system', 'tosprivstatement'),
304                 '$privstatement' => $tos->privacy_complete,
305                 '$baseurl'   => System::baseurl(),
306                 '$form_security_token' => get_form_security_token("register"),
307                 '$explicit_content' => Config::get('system', 'explicit_content', false),
308                 '$explicit_content_note' => L10n::t('Note: This node explicitly contains adult content')
309         ]);
310         return $o;
311 }