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