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