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