]> git.mxchange.org Git - friendica.git/blob - mod/invite.php
Renaming functions + moving functions from security to Model/Item and BaseModule...
[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\System;
15 use Friendica\Database\DBA;
16 use Friendica\Protocol\Email;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Util\Security;
19
20 function invite_post(App $a)
21 {
22         if (! local_user()) {
23                 notice(L10n::t('Permission denied.') . EOL);
24                 return;
25         }
26
27         BaseModule::checkFormSecurityTokenRedirectOnError('/', 'send_invite');
28
29         $max_invites = intval(Config::get('system', 'max_invites'));
30         if (! $max_invites) {
31                 $max_invites = 50;
32         }
33
34         $current_invites = intval(PConfig::get(local_user(), 'system', 'sent_invites'));
35         if ($current_invites > $max_invites) {
36                 notice(L10n::t('Total invitation limit exceeded.') . EOL);
37                 return;
38         }
39
40
41         $recipients  = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
42         $message     = !empty($_POST['message'])    ? notags(trim($_POST['message']))     : '';
43
44         $total = 0;
45
46         if (Config::get('system', 'invitation_only')) {
47                 $invitation_only = true;
48                 $invites_remaining = PConfig::get(local_user(), 'system', 'invites_remaining');
49                 if ((! $invites_remaining) && (! is_site_admin())) {
50                         return;
51                 }
52         }
53
54         foreach ($recipients as $recipient) {
55                 $recipient = trim($recipient);
56
57                 if (! valid_email($recipient)) {
58                         notice(L10n::t('%s : Not a valid email address.', $recipient) . EOL);
59                         continue;
60                 }
61
62                 if ($invitation_only && ($invites_remaining || is_site_admin())) {
63                         $code = autoname(8) . srand(1000, 9999);
64                         $nmessage = str_replace('$invite_code', $code, $message);
65
66                         $r = q("INSERT INTO `register` (`hash`,`created`) VALUES ('%s', '%s') ",
67                                 DBA::escape($code),
68                                 DBA::escape(DateTimeFormat::utcNow())
69                         );
70
71                         if (! is_site_admin()) {
72                                 $invites_remaining --;
73                                 if ($invites_remaining >= 0) {
74                                         PConfig::set(local_user(), 'system', 'invites_remaining', $invites_remaining);
75                                 } else {
76                                         return;
77                                 }
78                         }
79                 } else {
80                         $nmessage = $message;
81                 }
82
83                 $additional_headers = 'From: ' . $a->user['email'] . "\n"
84                         . 'Sender: ' . $a->getSenderEmailAddress() . "\n"
85                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
86                         . 'Content-transfer-encoding: 8bit';
87
88                 $res = mail(
89                         $recipient,
90                         Email::encodeHeader(L10n::t('Please join us on Friendica'), 'UTF-8'),
91                         $nmessage,
92                         $additional_headers);
93
94                 if ($res) {
95                         $total ++;
96                         $current_invites ++;
97                         PConfig::set(local_user(), 'system', 'sent_invites', $current_invites);
98                         if ($current_invites > $max_invites) {
99                                 notice(L10n::t('Invitation limit exceeded. Please contact your site administrator.') . EOL);
100                                 return;
101                         }
102                 } else {
103                         notice(L10n::t('%s : Message delivery failed.', $recipient) . EOL);
104                 }
105
106         }
107         notice(L10n::tt("%d message sent.", "%d messages sent.", $total) . EOL);
108         return;
109 }
110
111 function invite_content(App $a) {
112
113         if (! local_user()) {
114                 notice(L10n::t('Permission denied.') . EOL);
115                 return;
116         }
117
118         $tpl = get_markup_template('invite.tpl');
119         $invonly = false;
120
121         if (Config::get('system', 'invitation_only')) {
122                 $invonly = true;
123                 $x = PConfig::get(local_user(), 'system', 'invites_remaining');
124                 if ((! $x) && (! is_site_admin())) {
125                         notice(L10n::t('You have no more invitations available') . EOL);
126                         return '';
127                 }
128         }
129
130         $dirloc = Config::get('system', 'directory');
131         if (strlen($dirloc)) {
132                 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
133                         $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');
134                 } else {
135                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s or any other public Friendica website.', System::baseUrl())
136                         . "\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');
137                 }
138         } else { // there is no global directory URL defined
139                 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
140                         $o = L10n::t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
141                         return $o;
142                 } else {
143                         $linktxt = L10n::t('To accept this invitation, please visit and register at %s.', System::baseUrl()
144                         . "\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.'));
145                 }
146         }
147
148         $o = replace_macros($tpl, [
149                 '$form_security_token' => BaseModule::getFormSecurityToken("send_invite"),
150                 '$title'               => L10n::t('Send invitations'),
151                 '$recipients'          => ['recipients', L10n::t('Enter email addresses, one per line:')],
152                 '$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"
153                         . $linktxt
154                         . "\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:')
155                         . "\r\n" . "\r\n" . System::baseUrl() . '/profile/' . $a->user['nickname']
156                         . "\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"],
157                 '$submit'              => L10n::t('Submit')
158         ]);
159
160         return $o;
161 }