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