<commit_before><?php
/**
 * @copyright Copyright (C) 2020, Friendica
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Module;

use Friendica\BaseModule;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Model;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Email;
use Friendica\Util\Strings;

/**
 * Invite people to friendica
 */
class Invite extends BaseModule
{
	public static function post(array $parameters = [])
	{
		if (!local_user()) {
			throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
		}

		self::checkFormSecurityTokenRedirectOnError('/', 'send_invite');

		$app = DI::app();
		$config = DI::config();

		$max_invites = intval($config->get('system', 'max_invites'));
		if (!$max_invites) {
			$max_invites = 50;
		}

		$current_invites = intval(DI::pConfig()->get(local_user(), 'system', 'sent_invites'));
		if ($current_invites > $max_invites) {
			throw new HTTPException\ForbiddenException(DI::l10n()->t('Total invitation limit exceeded.'));
		}


		$recipients = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
		$message = !empty($_POST['message']) ? Strings::escapeTags(trim($_POST['message'])) : '';

		$total = 0;
		$invitation_only = false;
		$invites_remaining = null;

		if ($config->get('system', 'invitation_only')) {
			$invitation_only = true;
			$invites_remaining = DI::pConfig()->get(local_user(), 'system', 'invites_remaining');
			if ((!$invites_remaining) && (!is_site_admin())) {
				throw new HTTPException\ForbiddenException();
			}
		}

		foreach ($recipients as $recipient) {
			$recipient = trim($recipient);

			if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
				notice(DI::l10n()->t('%s : Not a valid email address.', $recipient));
				continue;
			}

			if ($invitation_only && ($invites_remaining || is_site_admin())) {
				$code = Model\Register::createForInvitation();
				$nmessage = str_replace('$invite_code', $code, $message);

				if (!is_site_admin()) {
					$invites_remaining--;
					if ($invites_remaining >= 0) {
						DI::pConfig()->set(local_user(), 'system', 'invites_remaining', $invites_remaining);
					} else {
						return;
					}
				}
			} else {
				$nmessage = $message;
			}

			$additional_headers = 'From: ' . $app->user['email'] . "\n"
				. 'Sender: ' . DI::emailer()->getSiteEmailAddress() . "\n"
				. 'Content-type: text/plain; charset=UTF-8' . "\n"
				. 'Content-transfer-encoding: 8bit';

			$res = mail(
				$recipient,
				Email::encodeHeader(DI::l10n()->t('Please join us on Friendica'), 'UTF-8'),
				$nmessage,
				$additional_headers);

			if ($res) {
				$total++;
				$current_invites++;
				DI::pConfig()->set(local_user(), 'system', 'sent_invites', $current_invites);
				if ($current_invites > $max_invites) {
					notice(DI::l10n()->t('Invitation limit exceeded. Please contact your site administrator.'));
					return;
				}
			} else {
				notice(DI::l10n()->t('%s : Message delivery failed.', $recipient));
			}

		}
		info(DI::l10n()->tt('%d message sent.', '%d messages sent.', $total));
	}

	public static function content(array $parameters = [])
	{
		if (!local_user()) {
			throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
		}
<commit_msg>Issue 4755: Use the system mail address as sender<commit_after><?php
/**
 * @copyright Copyright (C) 2020, Friendica
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Module;

use Friendica\BaseModule;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Model;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Email;
use Friendica\Util\Strings;

/**
 * Invite people to friendica
 */
class Invite extends BaseModule
{
	public static function post(array $parameters = [])
	{
		if (!local_user()) {
			throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
		}

		self::checkFormSecurityTokenRedirectOnError('/', 'send_invite');

		$app = DI::app();
		$config = DI::config();

		$max_invites = intval($config->get('system', 'max_invites'));
		if (!$max_invites) {
			$max_invites = 50;
		}

		$current_invites = intval(DI::pConfig()->get(local_user(), 'system', 'sent_invites'));
		if ($current_invites > $max_invites) {
			throw new HTTPException\ForbiddenException(DI::l10n()->t('Total invitation limit exceeded.'));
		}


		$recipients = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
		$message = !empty($_POST['message']) ? Strings::escapeTags(trim($_POST['message'])) : '';

		$total = 0;
		$invitation_only = false;
		$invites_remaining = null;

		if ($config->get('system', 'invitation_only')) {
			$invitation_only = true;
			$invites_remaining = DI::pConfig()->get(local_user(), 'system', 'invites_remaining');
			if ((!$invites_remaining) && (!is_site_admin())) {
				throw new HTTPException\ForbiddenException();
			}
		}

		foreach ($recipients as $recipient) {
			$recipient = trim($recipient);

			if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
				notice(DI::l10n()->t('%s : Not a valid email address.', $recipient));
				continue;
			}

			if ($invitation_only && ($invites_remaining || is_site_admin())) {
				$code = Model\Register::createForInvitation();
				$nmessage = str_replace('$invite_code', $code, $message);

				if (!is_site_admin()) {
					$invites_remaining--;
					if ($invites_remaining >= 0) {
						DI::pConfig()->set(local_user(), 'system', 'invites_remaining', $invites_remaining);
					} else {
						return;
					}
				}
			} else {
				$nmessage = $message;
			}

			$additional_headers = 'From: "' . $app->user['email'] . '" <' . DI::emailer()->getSiteEmailAddress() . ">\n"
				. 'Content-type: text/plain; charset=UTF-8' . "\n"
				. 'Content-transfer-encoding: 8bit';

			$res = mail(
				$recipient,
				Email::encodeHeader(DI::l10n()->t('Please join us on Friendica'), 'UTF-8'),
				$nmessage,
				$additional_headers);

			if ($res) {
				$total++;
				$current_invites++;
				DI::pConfig()->set(local_user(), 'system', 'sent_invites', $current_invites);
				if ($current_invites > $max_invites) {
					notice(DI::l10n()->t('Invitation limit exceeded. Please contact your site administrator.'));
					return;
				}
			} else {
				notice(DI::l10n()->t('%s : Message delivery failed.', $recipient));
			}

		}
		info(DI::l10n()->tt('%d message sent.', '%d messages sent.', $total));
	}

	public static function content(array $parameters = [])
	{
		if (!local_user()) {
			throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
		}

{'fraction_non_alphanumeric': 0.16331152491192752, 'fraction_numerical': 0.007800704579768495, 'mean_word_length': 4.125080593165699, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 44, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3180208', 'n_tokens_mistral': 2926, 'n_tokens_neox': 2512, 'n_words': 638}