]> git.mxchange.org Git - friendica.git/blob - src/Module/Invite.php
67d26f6fa78af31c955f82ff2f721b8150cf9244
[friendica.git] / src / Module / Invite.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\L10n;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Renderer;
9 use Friendica\DI;
10 use Friendica\Model;
11 use Friendica\Network\HTTPException;
12 use Friendica\Protocol\Email;
13 use Friendica\Util\Strings;
14
15 /**
16  * Invite people to friendica
17  */
18 class Invite extends BaseModule
19 {
20         public static function post(array $parameters = [])
21         {
22                 if (!local_user()) {
23                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
24                 }
25
26                 self::checkFormSecurityTokenRedirectOnError('/', 'send_invite');
27
28                 $app = DI::app();
29                 $config = DI::config();
30
31                 $max_invites = intval($config->get('system', 'max_invites'));
32                 if (!$max_invites) {
33                         $max_invites = 50;
34                 }
35
36                 $current_invites = intval(PConfig::get(local_user(), 'system', 'sent_invites'));
37                 if ($current_invites > $max_invites) {
38                         throw new HTTPException\ForbiddenException(L10n::t('Total invitation limit exceeded.'));
39                 }
40
41
42                 $recipients = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
43                 $message = !empty($_POST['message']) ? Strings::escapeTags(trim($_POST['message'])) : '';
44
45                 $total = 0;
46                 $invitation_only = false;
47                 $invites_remaining = null;
48
49                 if ($config->get('system', 'invitation_only')) {
50                         $invitation_only = true;
51                         $invites_remaining = PConfig::get(local_user(), 'system', 'invites_remaining');
52                         if ((!$invites_remaining) && (!is_site_admin())) {
53                                 throw new HTTPException\ForbiddenException();
54                         }
55                 }
56
57                 foreach ($recipients as $recipient) {
58                         $recipient = trim($recipient);
59
60                         if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
61                                 notice(L10n::t('%s : Not a valid email address.', $recipient) . EOL);
62                                 continue;
63                         }
64
65                         if ($invitation_only && ($invites_remaining || is_site_admin())) {
66                                 $code = Model\Register::createForInvitation();
67                                 $nmessage = str_replace('$invite_code', $code, $message);
68
69                                 if (!is_site_admin()) {
70                                         $invites_remaining--;
71                                         if ($invites_remaining >= 0) {
72                                                 PConfig::set(local_user(), 'system', 'invites_remaining', $invites_remaining);
73                                         } else {
74                                                 return;
75                                         }
76                                 }
77                         } else {
78                                 $nmessage = $message;
79                         }
80
81                         $additional_headers = 'From: ' . $app->user['email'] . "\n"
82                                 . 'Sender: ' . $app->getSenderEmailAddress() . "\n"
83                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
84                                 . 'Content-transfer-encoding: 8bit';
85
86                         $res = mail(
87                                 $recipient,
88                                 Email::encodeHeader(L10n::t('Please join us on Friendica'), 'UTF-8'),
89                                 $nmessage,
90                                 $additional_headers);
91
92                         if ($res) {
93                                 $total++;
94                                 $current_invites++;
95                                 PConfig::set(local_user(), 'system', 'sent_invites', $current_invites);
96                                 if ($current_invites > $max_invites) {
97                                         notice(L10n::t('Invitation limit exceeded. Please contact your site administrator.') . EOL);
98                                         return;
99                                 }
100                         } else {
101                                 notice(L10n::t('%s : Message delivery failed.', $recipient) . EOL);
102                         }
103
104                 }
105                 notice(L10n::tt('%d message sent.', '%d messages sent.', $total) . EOL);
106         }
107
108         public static function content(array $parameters = [])
109         {
110                 if (!local_user()) {
111                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
112                 }
113
114                 $app = DI::app();
115                 $config = DI::config();
116
117                 $inviteOnly = false;
118
119                 if ($config->get('system', 'invitation_only')) {
120                         $inviteOnly = true;
121                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
122                         if ((!$x) && (!is_site_admin())) {
123                                 throw new HTTPException\ForbiddenException(L10n::t('You have no more invitations available'));
124                         }
125                 }
126
127                 $dirLocation = $config->get('system', 'directory');
128                 if (strlen($dirLocation)) {
129                         if ($config->get('config', 'register_policy') === Register::CLOSED) {
130                                 $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.', $dirLocation . '/servers');
131                         } else {
132                                 $linkTxt = L10n::t('To accept this invitation, please visit and register at %s or any other public Friendica website.', DI::baseUrl()->get())
133                                         . "\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.', $dirLocation . '/servers');
134                         }
135                 } else { // there is no global directory URL defined
136                         if ($config->get('config', 'register_policy') === Register::CLOSED) {
137                                 return L10n::t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
138                         } else {
139                                 $linkTxt = L10n::t('To accept this invitation, please visit and register at %s.', DI::baseUrl()->get()
140                                         . "\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.'));
141                         }
142                 }
143
144                 $tpl = Renderer::getMarkupTemplate('invite.tpl');
145                 return Renderer::replaceMacros($tpl, [
146                         '$form_security_token' => self::getFormSecurityToken('send_invite'),
147                         '$title'               => L10n::t('Send invitations'),
148                         '$recipients'          => ['recipients', L10n::t('Enter email addresses, one per line:')],
149                         '$message'             => [
150                                 'message',
151                                 L10n::t('Your message:'),
152                                 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"
153                                 . $linkTxt
154                                 . "\r\n" . "\r\n" . (($inviteOnly) ? 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:')
155                                 . "\r\n" . "\r\n" . DI::baseUrl()->get() . '/profile/' . $app->user['nickname']
156                                 . "\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",
157                         ],
158                         '$submit'              => L10n::t('Submit')
159                 ]);
160         }
161 }