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