3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Search;
27 use Friendica\Core\Session;
30 use Friendica\Model\User;
31 use Friendica\Network\HTTPException;
32 use Friendica\Protocol\Email;
33 use Friendica\Util\Strings;
36 * Invite people to friendica
38 class Invite extends BaseModule
40 protected function post(array $request = [])
42 if (!Session::getLocalUser()) {
43 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
46 self::checkFormSecurityTokenRedirectOnError('/', 'send_invite');
49 $config = DI::config();
51 $max_invites = intval($config->get('system', 'max_invites'));
56 $current_invites = intval(DI::pConfig()->get(Session::getLocalUser(), 'system', 'sent_invites'));
57 if ($current_invites > $max_invites) {
58 throw new HTTPException\ForbiddenException(DI::l10n()->t('Total invitation limit exceeded.'));
62 $recipients = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
63 $message = !empty($_POST['message']) ? Strings::escapeHtml(trim($_POST['message'])) : '';
66 $invitation_only = false;
67 $invites_remaining = null;
69 if ($config->get('system', 'invitation_only')) {
70 $invitation_only = true;
71 $invites_remaining = DI::pConfig()->get(Session::getLocalUser(), 'system', 'invites_remaining');
72 if ((!$invites_remaining) && (!$app->isSiteAdmin())) {
73 throw new HTTPException\ForbiddenException();
77 $user = User::getById(Session::getLocalUser());
79 foreach ($recipients as $recipient) {
80 $recipient = trim($recipient);
82 if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
83 DI::sysmsg()->addNotice(DI::l10n()->t('%s : Not a valid email address.', $recipient));
87 if ($invitation_only && ($invites_remaining || $app->isSiteAdmin())) {
88 $code = Model\Register::createForInvitation();
89 $nmessage = str_replace('$invite_code', $code, $message);
91 if (!$app->isSiteAdmin()) {
93 if ($invites_remaining >= 0) {
94 DI::pConfig()->set(Session::getLocalUser(), 'system', 'invites_remaining', $invites_remaining);
100 $nmessage = $message;
103 $additional_headers = 'From: "' . $user['email'] . '" <' . DI::emailer()->getSiteEmailAddress() . ">\n"
104 . 'Content-type: text/plain; charset=UTF-8' . "\n"
105 . 'Content-transfer-encoding: 8bit';
109 Email::encodeHeader(DI::l10n()->t('Please join us on Friendica'), 'UTF-8'),
111 $additional_headers);
116 DI::pConfig()->set(Session::getLocalUser(), 'system', 'sent_invites', $current_invites);
117 if ($current_invites > $max_invites) {
118 DI::sysmsg()->addNotice(DI::l10n()->t('Invitation limit exceeded. Please contact your site administrator.'));
122 DI::sysmsg()->addNotice(DI::l10n()->t('%s : Message delivery failed.', $recipient));
126 DI::sysmsg()->addInfo(DI::l10n()->tt('%d message sent.', '%d messages sent.', $total));
129 protected function content(array $request = []): string
131 if (!Session::getLocalUser()) {
132 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
136 $config = DI::config();
140 if ($config->get('system', 'invitation_only')) {
142 $x = DI::pConfig()->get(Session::getLocalUser(), 'system', 'invites_remaining');
143 if ((!$x) && (!$app->isSiteAdmin())) {
144 throw new HTTPException\ForbiddenException(DI::l10n()->t('You have no more invitations available'));
148 $dirLocation = Search::getGlobalDirectory();
149 if (strlen($dirLocation)) {
150 if ($config->get('config', 'register_policy') === Register::CLOSED) {
151 $linkTxt = DI::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.', $dirLocation . '/servers');
153 $linkTxt = DI::l10n()->t('To accept this invitation, please visit and register at %s or any other public Friendica website.', DI::baseUrl()->get())
154 . "\r\n" . "\r\n" . DI::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.', $dirLocation . '/servers');
156 } else { // there is no global directory URL defined
157 if ($config->get('config', 'register_policy') === Register::CLOSED) {
158 return DI::l10n()->t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
160 $linkTxt = DI::l10n()->t('To accept this invitation, please visit and register at %s.', DI::baseUrl()->get()
161 . "\r\n" . "\r\n" . DI::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.'));
165 $tpl = Renderer::getMarkupTemplate('invite.tpl');
166 return Renderer::replaceMacros($tpl, [
167 '$form_security_token' => self::getFormSecurityToken('send_invite'),
168 '$title' => DI::l10n()->t('Send invitations'),
169 '$recipients' => ['recipients', DI::l10n()->t('Enter email addresses, one per line:')],
172 DI::l10n()->t('Your message:'),
173 DI::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"
175 . "\r\n" . "\r\n" . (($inviteOnly) ? DI::l10n()->t('You will need to supply this invitation code: $invite_code') . "\r\n" . "\r\n" : '') . DI::l10n()->t('Once you have registered, please connect with me via my profile page at:')
176 . "\r\n" . "\r\n" . DI::baseUrl()->get() . '/profile/' . $app->getLoggedInUserNickname()
177 . "\r\n" . "\r\n" . DI::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",
179 '$submit' => DI::l10n()->t('Submit')