]> git.mxchange.org Git - friendica.git/blob - mod/invite.php
Merge pull request #6055 from zeroadam/FileTagHotFix
[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\BaseModule;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Protocol\Email;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Security;
20
21 function invite_post(App $a)
22 {
23         if (! local_user()) {
24                 notice(L10n::t('Permission denied.') . EOL);
25                 return;
26         }
27
28         BaseModule::checkFormSecurityTokenRedirectOnError('/', 'send_invite');
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                 notice(L10n::t('Total invitation limit exceeded.') . EOL);
38                 return;
39         }
40
41
42         $recipients  = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
43         $message     = !empty($_POST['message'])    ? notags(trim($_POST['message']))     : '';
44
45         $total = 0;
46
47         if (Config::get('system', 'invitation_only')) {
48                 $invitation_only = true;
49                 $invites_remaining = PConfig::get(local_user(), 'system', 'invites_remaining');
50                 if ((! $invites_remaining) && (! is_site_admin())) {
51                         return;
52                 }
53         }
54
55         foreach ($recipients as $recipient) {
56                 $recipient = trim($recipient);
57
58                 if (! valid_email($recipient)) {
59                         notice(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 = Friendica\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                                         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: ' . $a->user['email'] . "\n"
80                         . 'Sender: ' . $a->getSenderEmailAddress() . "\n"
81                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
82                         . 'Content-transfer-encoding: 8bit';
83
84                 $res = mail(
85                         $recipient,
86                         Email::encodeHeader(L10n::t('Please join us on Friendica'), 'UTF-8'),
87                         $nmessage,
88                         $additional_headers);
89
90                 if ($res) {
91                         $total ++;
92                         $current_invites ++;
93                         PConfig::set(local_user(), 'system', 'sent_invites', $current_invites);
94                         if ($current_invites > $max_invites) {
95                                 notice(L10n::t('Invitation limit exceeded. Please contact your site administrator.') . EOL);
96                                 return;
97                         }
98                 } else {
99                         notice(L10n::t('%s : Message delivery failed.', $recipient) . EOL);
100                 }
101
102         }
103         notice(L10n::tt("%d message sent.", "%d messages sent.", $total) . EOL);
104         return;
105 }
106
107 function invite_content(App $a) {
108
109         if (! local_user()) {
110                 notice(L10n::t('Permission denied.') . EOL);
111                 return;
112         }
113
114         $tpl = Renderer::getMarkupTemplate('invite.tpl');
115         $invonly = false;
116
117         if (Config::get('system', 'invitation_only')) {
118                 $invonly = true;
119                 $x = PConfig::get(local_user(), 'system', 'invites_remaining');
120                 if ((! $x) && (! is_site_admin())) {
121                         notice(L10n::t('You have no more invitations available') . EOL);
122                         return '';
123                 }
124         }
125
126         $dirloc = Config::get('system', 'directory');
127         if (strlen($dirloc)) {
128                 if (intval(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.', $dirloc . '/servers');
130                 } else {
131                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s or any other public Friendica website.', System::baseUrl())
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.', $dirloc . '/servers');
133                 }
134         } else { // there is no global directory URL defined
135                 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
136                         $o = L10n::t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
137                         return $o;
138                 } else {
139                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s.', System::baseUrl()
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         $o = Renderer::replaceMacros($tpl, [
145                 '$form_security_token' => BaseModule::getFormSecurityToken("send_invite"),
146                 '$title'               => L10n::t('Send invitations'),
147                 '$recipients'          => ['recipients', L10n::t('Enter email addresses, one per line:')],
148                 '$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"
149                         . $linktxt
150                         . "\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:')
151                         . "\r\n" . "\r\n" . System::baseUrl() . '/profile/' . $a->user['nickname']
152                         . "\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"],
153                 '$submit'              => L10n::t('Submit')
154         ]);
155
156         return $o;
157 }