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