]> git.mxchange.org Git - friendica.git/blob - mod/invite.php
Merge pull request #5920 from MrPetovan/bug/remove-password-from-register
[friendica.git] / mod / invite.php
1 <?php
2 /**
3  * Module: invite.php
4  *
5  * Send email invitations to join social network
6  *
7  */
8
9 use Friendica\App;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Protocol\Email;
16 use Friendica\Util\DateTimeFormat;
17
18 function invite_post(App $a)
19 {
20         if (! local_user()) {
21                 notice(L10n::t('Permission denied.') . EOL);
22                 return;
23         }
24
25         check_form_security_token_redirectOnErr('/', 'send_invite');
26
27         $max_invites = intval(Config::get('system', 'max_invites'));
28         if (! $max_invites) {
29                 $max_invites = 50;
30         }
31
32         $current_invites = intval(PConfig::get(local_user(), 'system', 'sent_invites'));
33         if ($current_invites > $max_invites) {
34                 notice(L10n::t('Total invitation limit exceeded.') . EOL);
35                 return;
36         }
37
38
39         $recipients  = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
40         $message     = !empty($_POST['message'])    ? notags(trim($_POST['message']))     : '';
41
42         $total = 0;
43
44         if (Config::get('system', 'invitation_only')) {
45                 $invitation_only = true;
46                 $invites_remaining = PConfig::get(local_user(), 'system', 'invites_remaining');
47                 if ((! $invites_remaining) && (! is_site_admin())) {
48                         return;
49                 }
50         }
51
52         foreach ($recipients as $recipient) {
53                 $recipient = trim($recipient);
54
55                 if (! valid_email($recipient)) {
56                         notice(L10n::t('%s : Not a valid email address.', $recipient) . EOL);
57                         continue;
58                 }
59
60                 if ($invitation_only && ($invites_remaining || is_site_admin())) {
61                         $code = Friendica\Model\Register::createForInvitation();
62                         $nmessage = str_replace('$invite_code', $code, $message);
63
64                         if (! is_site_admin()) {
65                                 $invites_remaining --;
66                                 if ($invites_remaining >= 0) {
67                                         PConfig::set(local_user(), 'system', 'invites_remaining', $invites_remaining);
68                                 } else {
69                                         return;
70                                 }
71                         }
72                 } else {
73                         $nmessage = $message;
74                 }
75
76                 $additional_headers = 'From: ' . $a->user['email'] . "\n"
77                         . 'Sender: ' . $a->getSenderEmailAddress() . "\n"
78                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
79                         . 'Content-transfer-encoding: 8bit';
80
81                 $res = mail(
82                         $recipient,
83                         Email::encodeHeader(L10n::t('Please join us on Friendica'), 'UTF-8'),
84                         $nmessage,
85                         $additional_headers);
86
87                 if ($res) {
88                         $total ++;
89                         $current_invites ++;
90                         PConfig::set(local_user(), 'system', 'sent_invites', $current_invites);
91                         if ($current_invites > $max_invites) {
92                                 notice(L10n::t('Invitation limit exceeded. Please contact your site administrator.') . EOL);
93                                 return;
94                         }
95                 } else {
96                         notice(L10n::t('%s : Message delivery failed.', $recipient) . EOL);
97                 }
98
99         }
100         notice(L10n::tt("%d message sent.", "%d messages sent.", $total) . EOL);
101         return;
102 }
103
104 function invite_content(App $a) {
105
106         if (! local_user()) {
107                 notice(L10n::t('Permission denied.') . EOL);
108                 return;
109         }
110
111         $tpl = get_markup_template('invite.tpl');
112         $invonly = false;
113
114         if (Config::get('system', 'invitation_only')) {
115                 $invonly = true;
116                 $x = PConfig::get(local_user(), 'system', 'invites_remaining');
117                 if ((! $x) && (! is_site_admin())) {
118                         notice(L10n::t('You have no more invitations available') . EOL);
119                         return '';
120                 }
121         }
122
123         $dirloc = Config::get('system', 'directory');
124         if (strlen($dirloc)) {
125                 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
126                         $linktxt = L10n::t('Visit %s for a list of public sites that you can join. Friendica members on other sites can all connect with each other, as well as with members of many other social networks.', $dirloc . '/servers');
127                 } else {
128                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s or any other public Friendica website.', System::baseUrl())
129                         . "\r\n" . "\r\n" . L10n::t('Friendica sites all inter-connect to create a huge privacy-enhanced social web that is owned and controlled by its members. They can also connect with many traditional social networks. See %s for a list of alternate Friendica sites you can join.', $dirloc . '/servers');
130                 }
131         } else { // there is no global directory URL defined
132                 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
133                         $o = L10n::t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
134                         return $o;
135                 } else {
136                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s.', System::baseUrl()
137                         . "\r\n" . "\r\n" . L10n::t('Friendica sites all inter-connect to create a huge privacy-enhanced social web that is owned and controlled by its members. They can also connect with many traditional social networks.'));
138                 }
139         }
140
141         $o = replace_macros($tpl, [
142                 '$form_security_token' => get_form_security_token("send_invite"),
143                 '$title'               => L10n::t('Send invitations'),
144                 '$recipients'          => ['recipients', L10n::t('Enter email addresses, one per line:')],
145                 '$message'             => ['message', L10n::t('Your message:'),L10n::t('You are cordially invited to join me and other close friends on Friendica - and help us to create a better social web.') . "\r\n" . "\r\n"
146                         . $linktxt
147                         . "\r\n" . "\r\n" . (($invonly) ? L10n::t('You will need to supply this invitation code: $invite_code') . "\r\n" . "\r\n" : '') .L10n::t('Once you have registered, please connect with me via my profile page at:')
148                         . "\r\n" . "\r\n" . System::baseUrl() . '/profile/' . $a->user['nickname']
149                         . "\r\n" . "\r\n" . L10n::t('For more information about the Friendica project and why we feel it is important, please visit http://friendi.ca') . "\r\n" . "\r\n"],
150                 '$submit'              => L10n::t('Submit')
151         ]);
152
153         return $o;
154 }