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