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