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