]> git.mxchange.org Git - friendica.git/blob - mod/invite.php
Make frio more consistent by replacing textual links with icons everywhere. (#5415)
[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\Protocol\Email;
15 use Friendica\Util\DateTimeFormat;
16
17 function invite_post(App $a)
18 {
19         if (! local_user()) {
20                 notice(L10n::t('Permission denied.') . EOL);
21                 return;
22         }
23
24         check_form_security_token_redirectOnErr('/', 'send_invite');
25
26         $max_invites = intval(Config::get('system', 'max_invites'));
27         if (! $max_invites) {
28                 $max_invites = 50;
29         }
30
31         $current_invites = intval(PConfig::get(local_user(), 'system', 'sent_invites'));
32         if ($current_invites > $max_invites) {
33                 notice(L10n::t('Total invitation limit exceeded.') . EOL);
34                 return;
35         }
36
37
38         $recipients  = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
39         $message     = !empty($_POST['message'])    ? notags(trim($_POST['message']))     : '';
40
41         $total = 0;
42
43         if (Config::get('system', 'invitation_only')) {
44                 $invitation_only = true;
45                 $invites_remaining = PConfig::get(local_user(), 'system', 'invites_remaining');
46                 if ((! $invites_remaining) && (! is_site_admin())) {
47                         return;
48                 }
49         }
50
51         foreach ($recipients as $recipient) {
52                 $recipient = trim($recipient);
53
54                 if (! valid_email($recipient)) {
55                         notice(L10n::t('%s : Not a valid email address.', $recipient) . EOL);
56                         continue;
57                 }
58
59                 if ($invitation_only && ($invites_remaining || is_site_admin())) {
60                         $code = autoname(8) . srand(1000, 9999);
61                         $nmessage = str_replace('$invite_code', $code, $message);
62
63                         $r = q("INSERT INTO `register` (`hash`,`created`) VALUES ('%s', '%s') ",
64                                 dbesc($code),
65                                 dbesc(DateTimeFormat::utcNow())
66                         );
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: ' . $a->user['email'] . "\n"
81                         . 'Sender: ' . $a->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         return;
106 }
107
108 function invite_content(App $a) {
109
110         if (! local_user()) {
111                 notice(L10n::t('Permission denied.') . EOL);
112                 return;
113         }
114
115         $tpl = get_markup_template('invite.tpl');
116         $invonly = false;
117
118         if (Config::get('system', 'invitation_only')) {
119                 $invonly = true;
120                 $x = PConfig::get(local_user(), 'system', 'invites_remaining');
121                 if ((! $x) && (! is_site_admin())) {
122                         notice(L10n::t('You have no more invitations available') . EOL);
123                         return '';
124                 }
125         }
126
127         $dirloc = Config::get('system', 'directory');
128         if (strlen($dirloc)) {
129                 if (intval(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.', $dirloc . '/servers');
131                 } else {
132                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s or any other public Friendica website.', System::baseUrl())
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.', $dirloc . '/servers');
134                 }
135         } else { // there is no global directory URL defined
136                 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
137                         $o = L10n::t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
138                         return $o;
139                 } else {
140                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s.', System::baseUrl()
141                         . "\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.'));
142                 }
143         }
144
145         $o = replace_macros($tpl, [
146                 '$form_security_token' => get_form_security_token("send_invite"),
147                 '$title'               => L10n::t('Send invitations'),
148                 '$recipients'          => ['recipients', L10n::t('Enter email addresses, one per line:')],
149                 '$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"
150                         . $linktxt
151                         . "\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:')
152                         . "\r\n" . "\r\n" . System::baseUrl() . '/profile/' . $a->user['nickname']
153                         . "\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"],
154                 '$submit'              => L10n::t('Submit')
155         ]);
156
157         return $o;
158 }