<commit_before><?php
/**
 * @author         <NAME> <<EMAIL>>
 * @copyright      (c) 2012-2018, <NAME>. All Rights Reserved.
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package        PH7 / App / System / Module / Mail / Form / Processing
 */

namespace PH7;

defined('PH7') or die('Restricted access');

use PH7\Framework\Mail\Mail;
use PH7\Framework\Mvc\Model\DbConfig;
use PH7\Framework\Mvc\Request\Http;
use PH7\Framework\Mvc\Router\Uri;
use PH7\Framework\Security\Spam\Spam;
use PH7\Framework\Url\Header;

class MailFormProcess extends Form
{
    const MAX_ALLOWED_LINKS = 0;

    /** @var UserCoreModel */
    private $oUserModel;

    public function __construct()
    {
        parent::__construct();

        $this->oUserModel = new UserCoreModel;
        $oMailModel = new MailModel;

        $bIsAdmin = $this->isAdminEligible();
        $sMessage = $this->httpRequest->post('message', Http::ONLY_XSS_CLEAN);
        $sCurrentTime = $this->dateTime->get()->dateTime('Y-m-d H:i:s');
        $iTimeDelay = (int)DbConfig::getSetting('timeDelaySendMail');
        $sRecipient = $this->httpRequest->post('recipient');
        $iRecipientId = $this->oUserModel->getId(null, $sRecipient);
        $iSenderId = (int)($bIsAdmin ? PH7_ADMIN_ID : $this->session->get('member_id'));

        if ($iSenderId === $iRecipientId) {
            \PFBC\Form::setError('form_compose_mail', t('Oops! You can not send a message to yourself.'));
        } elseif ($sRecipient === PH7_ADMIN_USERNAME) {
            \PFBC\Form::setError('form_compose_mail', t('Oops! You cannot reply to administrator! If you want to contact us, please use our <a href="%0%">contact form</a>.', Uri::get('contact', 'contact', 'index')));
        } elseif (!(new ExistsCoreModel)->id($iRecipientId, DbTableName::MEMBER)) {
            \PFBC\Form::setError('form_compose_mail', t('Oops! The username "%0%" does not exist.', escape(substr($this->httpRequest->post('recipient'), 0, PH7_MAX_USERNAME_LENGTH), true)));
        } elseif (!$bIsAdmin && !$oMailModel->checkWaitSend($iSenderId, $iTimeDelay, $sCurrentTime)) {
            \PFBC\Form::setError('form_compose_mail', Form::waitWriteMsg($iTimeDelay));
        } elseif (!$bIsAdmin && $oMailModel->isDuplicateContent($iSenderId, $sMessage)) {
            \PFBC\Form::setError('form_compose_mail', Form::duplicateContentMsg());
        } elseif (!$bIsAdmin && Spam::areUrls($sMessage, self::MAX_ALLOWED_LINKS)) {
            \PFBC\Form::setError('form_compose_mail', Form::tooManyUrlsMsg());
        } else {
            $mSendMsg = $oMailModel->sendMsg($iSenderId, $iRecipientId, $this->httpRequest->post('title'), $sMessage, $sCurrentTime);

            if (false === $mSendMsg) {
                \PFBC\Form::setError(
                    'form_compose_mail',
                    t('Problem while sending the message. Please try again later.')
                );
            } else {
                if ($this->canSendEmail($iRecipientId)) {
                    $this->sendMail($iRecipientId, $mSendMsg);
                }

                Header::redirect(
                    $this->getRedirectUrl(),
                    t('Your message has been successfully sent!')
                );
            }

            unset($oMailModel);
        }
    }

    /**
     * Send notification email.
     *
     * @param int $iRecipientId
     * @param int $iMsgId
     *
     * @return int Number of recipients who were accepted for delivery.
     */
    private function sendMail($iRecipientId, $iMsgId)
    {
        $this->view->content = t('Hello %0%!', $this->httpRequest->post('recipient')) . '<br />' .
            t('You received a new message from %0%', $this->session->get('member_username')) . '<br />' .
            '<a href="' . Uri::get('mail', 'main', 'inbox', $iMsgId) . '">' . t('Click here') . '</a>' . t('to read your message.');

        $sRecipientEmail = $this->oUserModel->getEmail($iRecipientId);

        $sMessageHtml = $this->view->parseMail(
            PH7_PATH_SYS . 'global/' . PH7_VIEWS . PH7_TPL_MAIL_NAME . '/tpl/mail/sys/mod/mail/new_msg.tpl',
            $sRecipientEmail
        );

        $aInfo = [
            'to' => $sRecipientEmail,
            'subject' => t('New private message from %0% on %site_name%', $this->session->get('member_first_name'))
        ];

        return (new Mail)->send($aInfo, $sMessageHtml);
    }

    /**
     * @param int $iRecipientId
     *
     * @return bool TRUE if the email notification is accepted and if the recipient isn't online.
     */
    private function canSendEmail($iRecipientId)
    {
        return $this->oUserModel->isNotification($iRecipientId, 'newMsg') &&
        !$this->oUserModel->isOnline($iRecipientId);
    }

    /**
     * @return bool
     */
    private function isAdminEligible()
    {
        return AdminCore::auth() && !UserCore::auth() && !UserCore::isAdminLoggedAs();
    }

    /**
     * @return string
     */
    private function getRedirectUrl()
    {
        if ($this->isAdminEligible()) {
            return Uri::get(PH7_ADMIN_MOD, 'user', 'browse');
        }
<commit_msg>Make comment shorter ("if" wasnt needed) + fix indentation<commit_after><?php
/**
 * @author         <NAME> <<EMAIL>>
 * @copyright      (c) 2012-2018, <NAME>. All Rights Reserved.
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package        PH7 / App / System / Module / Mail / Form / Processing
 */

namespace PH7;

defined('PH7') or die('Restricted access');

use PH7\Framework\Mail\Mail;
use PH7\Framework\Mvc\Model\DbConfig;
use PH7\Framework\Mvc\Request\Http;
use PH7\Framework\Mvc\Router\Uri;
use PH7\Framework\Security\Spam\Spam;
use PH7\Framework\Url\Header;

class MailFormProcess extends Form
{
    const MAX_ALLOWED_LINKS = 0;

    /** @var UserCoreModel */
    private $oUserModel;

    public function __construct()
    {
        parent::__construct();

        $this->oUserModel = new UserCoreModel;
        $oMailModel = new MailModel;

        $bIsAdmin = $this->isAdminEligible();
        $sMessage = $this->httpRequest->post('message', Http::ONLY_XSS_CLEAN);
        $sCurrentTime = $this->dateTime->get()->dateTime('Y-m-d H:i:s');
        $iTimeDelay = (int)DbConfig::getSetting('timeDelaySendMail');
        $sRecipient = $this->httpRequest->post('recipient');
        $iRecipientId = $this->oUserModel->getId(null, $sRecipient);
        $iSenderId = (int)($bIsAdmin ? PH7_ADMIN_ID : $this->session->get('member_id'));

        if ($iSenderId === $iRecipientId) {
            \PFBC\Form::setError('form_compose_mail', t('Oops! You can not send a message to yourself.'));
        } elseif ($sRecipient === PH7_ADMIN_USERNAME) {
            \PFBC\Form::setError('form_compose_mail', t('Oops! You cannot reply to administrator! If you want to contact us, please use our <a href="%0%">contact form</a>.', Uri::get('contact', 'contact', 'index')));
        } elseif (!(new ExistsCoreModel)->id($iRecipientId, DbTableName::MEMBER)) {
            \PFBC\Form::setError('form_compose_mail', t('Oops! The username "%0%" does not exist.', escape(substr($this->httpRequest->post('recipient'), 0, PH7_MAX_USERNAME_LENGTH), true)));
        } elseif (!$bIsAdmin && !$oMailModel->checkWaitSend($iSenderId, $iTimeDelay, $sCurrentTime)) {
            \PFBC\Form::setError('form_compose_mail', Form::waitWriteMsg($iTimeDelay));
        } elseif (!$bIsAdmin && $oMailModel->isDuplicateContent($iSenderId, $sMessage)) {
            \PFBC\Form::setError('form_compose_mail', Form::duplicateContentMsg());
        } elseif (!$bIsAdmin && Spam::areUrls($sMessage, self::MAX_ALLOWED_LINKS)) {
            \PFBC\Form::setError('form_compose_mail', Form::tooManyUrlsMsg());
        } else {
            $mSendMsg = $oMailModel->sendMsg($iSenderId, $iRecipientId, $this->httpRequest->post('title'), $sMessage, $sCurrentTime);

            if (false === $mSendMsg) {
                \PFBC\Form::setError(
                    'form_compose_mail',
                    t('Problem while sending the message. Please try again later.')
                );
            } else {
                if ($this->canSendEmail($iRecipientId)) {
                    $this->sendMail($iRecipientId, $mSendMsg);
                }

                Header::redirect(
                    $this->getRedirectUrl(),
                    t('Your message has been successfully sent!')
                );
            }

            unset($oMailModel);
        }
    }

    /**
     * Send notification email.
     *
     * @param int $iRecipientId
     * @param int $iMsgId
     *
     * @return int Number of recipients who were accepted for delivery.
     */
    private function sendMail($iRecipientId, $iMsgId)
    {
        $this->view->content = t('Hello %0%!', $this->httpRequest->post('recipient')) . '<br />' .
            t('You received a new message from %0%', $this->session->get('member_username')) . '<br />' .
            '<a href="' . Uri::get('mail', 'main', 'inbox', $iMsgId) . '">' . t('Click here') . '</a>' . t('to read your message.');

        $sRecipientEmail = $this->oUserModel->getEmail($iRecipientId);

        $sMessageHtml = $this->view->parseMail(
            PH7_PATH_SYS . 'global/' . PH7_VIEWS . PH7_TPL_MAIL_NAME . '/tpl/mail/sys/mod/mail/new_msg.tpl',
            $sRecipientEmail
        );

        $aInfo = [
            'to' => $sRecipientEmail,
            'subject' => t('New private message from %0% on %site_name%', $this->session->get('member_first_name'))
        ];

        return (new Mail)->send($aInfo, $sMessageHtml);
    }

    /**
     * @param int $iRecipientId
     *
     * @return bool TRUE if the email notification is accepted and the recipient isn't online.
     */
    private function canSendEmail($iRecipientId)
    {
        return $this->oUserModel->isNotification($iRecipientId, 'newMsg') &&
            !$this->oUserModel->isOnline($iRecipientId);
    }

    /**
     * @return bool
     */
    private function isAdminEligible()
    {
        return AdminCore::auth() && !UserCore::auth() && !UserCore::isAdminLoggedAs();
    }

    /**
     * @return string
     */
    private function getRedirectUrl()
    {
        if ($this->isAdminEligible()) {
            return Uri::get(PH7_ADMIN_MOD, 'user', 'browse');
        }

{'fraction_non_alphanumeric': 0.1586889657823914, 'fraction_numerical': 0.006343713956170703, 'mean_word_length': 2.6078363384188625, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '534529', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3250, 'n_words': 646}