]> git.mxchange.org Git - friendica.git/blob - mod/invite.php
Issue-#3873
[friendica.git] / mod / invite.php
1 <?php
2
3 /**
4  * module: invite.php
5  *
6  * send email invitations to join social network
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Config;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\System;
14
15 require_once('include/email.php');
16
17 function invite_post(App $a) {
18
19         if (! local_user()) {
20                 notice( 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( t('Total invitation limit exceeded.') . EOL);
34                 return;
35         }
36
37
38         $recips  = ((x($_POST,'recipients')) ? explode("\n",$_POST['recipients']) : array());
39         $message = ((x($_POST,'message'))    ? notags(trim($_POST['message']))    : '');
40
41         $total = 0;
42
43         if (Config::get('system','invitation_only')) {
44                 $invonly = true;
45                 $x = PConfig::get(local_user(),'system','invites_remaining');
46                 if ((! $x) && (! is_site_admin())) {
47                         return;
48                 }
49         }
50
51         foreach ($recips as $recip) {
52
53                 $recip = trim($recip);
54
55                 if (! valid_email($recip)) {
56                         notice(  sprintf( t('%s : Not a valid email address.'), $recip) . EOL);
57                         continue;
58                 }
59
60                 if ($invonly && ($x || 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                                 dbesc($code),
66                                 dbesc(datetime_convert())
67                         );
68
69                         if (! is_site_admin()) {
70                                 $x --;
71                                 if ($x >= 0) {
72                                         PConfig::set(local_user(),'system','invites_remaining',$x);
73                                 } else {
74                                         return;
75                                 }
76                         }
77                 } else {
78                         $nmessage = $message;
79                 }
80
81                 $res = mail($recip, email_header_encode( t('Please join us on Friendica'),'UTF-8'),
82                         $nmessage,
83                         "From: " . $a->user['email'] . "\n"
84                         . 'Content-type: text/plain; charset=UTF-8' . "\n"
85                         . 'Content-transfer-encoding: 8bit' );
86
87                 if ($res) {
88                         $total ++;
89                         $current_invites ++;
90                         PConfig::set(local_user(),'system','sent_invites',$current_invites);
91                         if($current_invites > $max_invites) {
92                                 notice( t('Invitation limit exceeded. Please contact your site administrator.') . EOL);
93                                 return;
94                         }
95                 } else {
96                         notice( sprintf( t('%s : Message delivery failed.'), $recip) . EOL);
97                 }
98
99         }
100         notice( sprintf( tt("%d message sent.", "%d messages sent.", $total) , $total) . EOL);
101         return;
102 }
103
104
105 function invite_content(App $a) {
106
107         if (! local_user()) {
108                 notice( t('Permission denied.') . EOL);
109                 return;
110         }
111
112         $tpl = get_markup_template('invite.tpl');
113         $invonly = false;
114
115         if (Config::get('system','invitation_only')) {
116                 $invonly = true;
117                 $x = PConfig::get(local_user(),'system','invites_remaining');
118                 if ((! $x) && (! is_site_admin())) {
119                         notice( t('You have no more invitations available') . EOL);
120                         return '';
121                 }
122         }
123
124         $dirloc = Config::get('system','directory');
125         if (strlen($dirloc)) {
126                 if ($a->config['register_policy'] == REGISTER_CLOSED) {
127                         $linktxt = sprintf( 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');
128                 } else {
129                         $linktxt = sprintf( t('To accept this invitation, please visit and register at %s or any other public Friendica website.'), System::baseUrl())
130                         . "\r\n" . "\r\n" . sprintf( 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');
131                 }
132         } else { // there is no global directory URL defined
133                 if ($a->config['register_policy'] == REGISTER_CLOSED) {
134                         $o = t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
135                         return $o;
136                 } else {
137                         $linktxt = sprintf( t('To accept this invitation, please visit and register at %s.'), System::baseUrl()
138                         . "\r\n" . "\r\n" . 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.'));
139                 }
140         }
141
142         $o = replace_macros($tpl, array(
143                 '$form_security_token' => get_form_security_token("send_invite"),
144                 '$invite'              => t('Send invitations'),
145                 '$addr_text'           => t('Enter email addresses, one per line:'),
146                 '$msg_text'            => t('Your message:'),
147                 '$default_message'     => 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"
148                         . $linktxt
149                         . "\r\n" . "\r\n" . (($invonly) ? t('You will need to supply this invitation code: $invite_code') . "\r\n" . "\r\n" : '') .t('Once you have registered, please connect with me via my profile page at:')
150                         . "\r\n" . "\r\n" . System::baseUrl() . '/profile/' . $a->user['nickname']
151                         . "\r\n" . "\r\n" . t('For more information about the Friendica project and why we feel it is important, please visit http://friendi.ca') . "\r\n" . "\r\n"  ,
152                 '$submit'              => t('Submit')
153         ));
154
155         return $o;
156 }