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