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