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