]> git.mxchange.org Git - friendica.git/blob - mod/register.php
Merge pull request #4331 from annando/other-hashtags
[friendica.git] / mod / register.php
1 <?php
2 /**
3  * @file mod/register.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Addon;
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\PConfig;
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 use Friendica\Model\User;
13
14 require_once 'include/enotify.php';
15 require_once 'include/bbcode.php';
16
17 function register_post(App $a)
18 {
19         check_form_security_token_redirectOnErr('/register', 'register');
20
21         global $lang;
22
23         $verified = 0;
24         $blocked  = 1;
25
26         $arr = ['post' => $_POST];
27         Addon::callHooks('register_post', $arr);
28
29         $max_dailies = intval(Config::get('system', 'max_daily_registrations'));
30         if ($max_dailies) {
31                 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
32                 if ($r && $r[0]['total'] >= $max_dailies) {
33                         return;
34                 }
35         }
36
37         switch ($a->config['register_policy']) {
38                 case REGISTER_OPEN:
39                         $blocked = 0;
40                         $verified = 1;
41                         break;
42
43                 case REGISTER_APPROVE:
44                         $blocked = 1;
45                         $verified = 0;
46                         break;
47
48                 default:
49                 case REGISTER_CLOSED:
50                         if ((!x($_SESSION, 'authenticated') && (!x($_SESSION, 'administrator')))) {
51                                 notice(L10n::t('Permission denied.') . EOL);
52                                 return;
53                         }
54                         $blocked = 1;
55                         $verified = 0;
56                         break;
57         }
58
59
60         $arr = $_POST;
61
62         $arr['blocked'] = $blocked;
63         $arr['verified'] = $verified;
64         $arr['language'] = L10n::getBrowserLanguage();
65
66         try {
67                 $result = User::create($arr);
68         } catch (Exception $e) {
69                 notice($e->getMessage());
70                 return;
71         }
72
73         $user = $result['user'];
74
75         if ($netpublish && $a->config['register_policy'] != REGISTER_APPROVE) {
76                 $url = System::baseUrl() . '/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 = ((x($_POST, 'invite_id')) ? notags(trim($_POST['invite_id'])) : '');
83
84         if ($a->config['register_policy'] == REGISTER_OPEN) {
85                 if ($using_invites && $invite_id) {
86                         q("delete * from register where hash = '%s' limit 1", dbesc($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 (!x($_POST, 'password1') || !x($_POST, 'confirm')) {
92                         $res = User::sendRegisterOpenEmail(
93                                         $user['email'], $a->config['sitename'], System::baseUrl(), $user['username'], $result['password']);
94
95                         if ($res) {
96                                 info(L10n::t('Registration successful. Please check your email for further instructions.') . EOL);
97                                 goaway(System::baseUrl());
98                         } else {
99                                 notice(
100                                         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.',
101                                                 $user['email'],
102                                                 $result['password'])
103                                         . EOL
104                                 );
105                         }
106                 } else {
107                         info(L10n::t('Registration successful.') . EOL);
108                         goaway(System::baseUrl());
109                 }
110         } elseif ($a->config['register_policy'] == REGISTER_APPROVE) {
111                 if (!strlen($a->config['admin_email'])) {
112                         notice(L10n::t('Your registration can not be processed.') . EOL);
113                         goaway(System::baseUrl());
114                 }
115
116                 $hash = random_string();
117                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password`, `language`, `note` ) VALUES ( '%s', '%s', %d, '%s', '%s', '%s' ) ",
118                         dbesc($hash),
119                         dbesc(datetime_convert()),
120                         intval($user['uid']),
121                         dbesc($result['password']),
122                         dbesc($lang),
123                         dbesc($_POST['permonlybox'])
124                 );
125
126                 // invite system
127                 if ($using_invites && $invite_id) {
128                         q("DELETE * FROM `register` WHERE `hash` = '%s' LIMIT 1", dbesc($invite_id));
129                         PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites);
130                 }
131
132                 // send email to admins
133                 $admin_mail_list = "'" . implode("','", array_map(dbesc, explode(",", str_replace(" ", "", $a->config['admin_email'])))) . "'";
134                 $adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
135                         $admin_mail_list
136                 );
137
138                 // send notification to admins
139                 foreach ($adminlist as $admin) {
140                         notification([
141                                 'type'         => NOTIFY_SYSTEM,
142                                 'event'        => 'SYSTEM_REGISTER_REQUEST',
143                                 'source_name'  => $user['username'],
144                                 'source_mail'  => $user['email'],
145                                 'source_nick'  => $user['nickname'],
146                                 'source_link'  => System::baseUrl() . "/admin/users/",
147                                 'link'         => System::baseUrl() . "/admin/users/",
148                                 'source_photo' => System::baseUrl() . "/photo/avatar/" . $user['uid'] . ".jpg",
149                                 'to_email'     => $admin['email'],
150                                 'uid'          => $admin['uid'],
151                                 'language'     => $admin['language'] ? $admin['language'] : 'en',
152                                 'show_in_notification_page' => false
153                         ]);
154                 }
155                 // send notification to the user, that the registration is pending
156                 User::sendRegisterPendingEmail(
157                         $user['email'], $a->config['sitename'], $user['username']);
158
159                 info(L10n::t('Your registration is pending approval by the site owner.') . EOL);
160                 goaway(System::baseUrl());
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()) && ($a->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('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 (x($_SESSION, 'theme')) {
194                 unset($_SESSION['theme']);
195         }
196         if (x($_SESSION, 'mobile-theme')) {
197                 unset($_SESSION['mobile-theme']);
198         }
199
200
201         $username   = x($_REQUEST, 'username')   ? $_REQUEST['username']   : '';
202         $email      = x($_REQUEST, 'email')      ? $_REQUEST['email']      : '';
203         $openid_url = x($_REQUEST, 'openid_url') ? $_REQUEST['openid_url'] : '';
204         $nickname   = x($_REQUEST, 'nickname')   ? $_REQUEST['nickname']   : '';
205         $photo      = x($_REQUEST, 'photo')      ? $_REQUEST['photo']      : '';
206         $invite_id  = x($_REQUEST, 'invite_id')  ? $_REQUEST['invite_id']  : '';
207
208         $noid = Config::get('system', 'no_openid');
209
210         if ($noid) {
211                 $oidhtml  = '';
212                 $fillwith = '';
213                 $fillext  = '';
214                 $oidlabel = '';
215         } else {
216                 $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" >';
217                 $fillwith = L10n::t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
218                 $fillext  = L10n::t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
219                 $oidlabel = L10n::t("Your OpenID \x28optional\x29: ");
220         }
221
222         // I set this and got even more fake names than before...
223         $realpeople = ''; // L10n::t('Members of this network prefer to communicate with real people who use their real names.');
224
225         if (Config::get('system', 'publish_all')) {
226                 $profile_publish = '<input type="hidden" name="profile_publish_reg" value="1" />';
227         } else {
228                 $publish_tpl = get_markup_template("profile_publish.tpl");
229                 $profile_publish = replace_macros($publish_tpl, [
230                         '$instance' => 'reg',
231                         '$pubdesc' => L10n::t('Include your profile in member directory?'),
232                         '$yes_selected' => ' checked="checked" ',
233                         '$no_selected' => '',
234                         '$str_yes' => L10n::t('Yes'),
235                         '$str_no' => L10n::t('No'),
236                 ]);
237         }
238
239         $r = q("SELECT COUNT(*) AS `contacts` FROM `contact`");
240         $passwords = !$r[0]["contacts"];
241
242         $license = '';
243
244         $tpl = get_markup_template("register.tpl");
245
246         $arr = ['template' => $tpl];
247
248         Addon::callHooks('register_form', $arr);
249
250         $tpl = $arr['template'];
251
252         $o = replace_macros($tpl, [
253                 '$oidhtml' => $oidhtml,
254                 '$invitations' => Config::get('system', 'invitation_only'),
255                 '$permonly'    => $a->config['register_policy'] == REGISTER_APPROVE,
256                 '$permonlybox' => ['permonlybox', L10n::t('Note for the admin'), '', L10n::t('Leave a message for the admin, why you want to join this node')],
257                 '$invite_desc' => L10n::t('Membership on this site is by invitation only.'),
258                 '$invite_label' => L10n::t('Your invitation ID: '),
259                 '$invite_id'  => $invite_id,
260                 '$realpeople' => $realpeople,
261                 '$regtitle'  => L10n::t('Registration'),
262                 '$registertext' => x($a->config, 'register_text') ? bbcode($a->config['register_text']) : "",
263                 '$fillwith'  => $fillwith,
264                 '$fillext'   => $fillext,
265                 '$oidlabel'  => $oidlabel,
266                 '$openid'    => $openid_url,
267                 '$namelabel' => L10n::t('Your Full Name ' . "\x28" . 'e.g. Joe Smith, real or real-looking' . "\x29" . ': '),
268                 '$addrlabel' => L10n::t("Your Email Address: \x28Initial information will be send there, so this has to be an existing address.\x29"),
269                 '$passwords' => $passwords,
270                 '$password1' => ['password1', L10n::t('New Password:'), '', L10n::t('Leave empty for an auto generated password.')],
271                 '$password2' => ['confirm', L10n::t('Confirm:'), '', ''],
272                 '$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()),
273                 '$nicklabel' => L10n::t('Choose a nickname: '),
274                 '$photo'     => $photo,
275                 '$publish'   => $profile_publish,
276                 '$regbutt'   => L10n::t('Register'),
277                 '$username'  => $username,
278                 '$email'     => $email,
279                 '$nickname'  => $nickname,
280                 '$license'   => $license,
281                 '$sitename'  => $a->get_hostname(),
282                 '$importh'   => L10n::t('Import'),
283                 '$importt'   => L10n::t('Import your profile to this friendica instance'),
284                 '$form_security_token' => get_form_security_token("register")
285         ]);
286         return $o;
287 }