Your IP : 216.73.216.240


Current Path : /home/juvelize/martine/components/com_easybookreloaded/
Upload File :
Current File : /home/juvelize/martine/components/com_easybookreloaded/controller.php

<?php

/**
 * @copyright
 * @package    Easybook Reloaded - EBR for Joomla! 3.x
 * @author     Viktor Vogel <admin@kubik-rubik.de>
 * @version    3.4.1.1-FREE - 2021-08-29
 * @link       https://kubik-rubik.de/ebr-easybook-reloaded
 *
 * @license    GNU/GPL
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') || die('Restricted access');

use EasybookReloaded\{Helper, Route as EasybookReloadedRoute};
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\{Factory, Language\Text, Router\Route, Uri\Uri};

/**
 * Class EasybookReloadedController
 *
 * @since   3.4.0-FREE
 * @version 3.4.1.0-FREE
 */
class EasybookReloadedController extends BaseController
{
    /**
     * @var object $input
     * @since 3.4.0-FREE
     */
    protected $input;

    /**
     * @param bool $cachable
     * @param bool $urlparams
     *
     * @return JControllerLegacy|void
     * @throws Exception
     * @since 3.4.0-FREE
     */
    public function display($cachable = false, $urlparams = false)
    {
        parent::display();

        $this->input = Factory::getApplication()->input;
    }

    /**
     * This function is triggered when the user clicks on the publish link the notification mail
     *
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function publishMail(): void
    {
        $hashRequest = $this->input->getString('hash', '');
        $checkHash = $this->performValidationChecksMail($hashRequest);
        $gbId = $this->input->getInt('gbid');

        $message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_CHANGE_PUBLISH_STATUS');
        $type = 'error';

        if ($checkHash === true) {
            /* @var EasybookReloadedModelEntry $model */
            $model = $this->getModel('entry');

            switch ($model->publish()) {
                case -1:
                    $message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_CHANGE_PUBLISH_STATUS');
                    $type = 'error';
                    break;
                case 0:
                    $message = Text::_('COM_EASYBOOKRELOADED_ENTRY_UNPUBLISHED');
                    $type = 'success';
                    break;
                case 1:
                    $message = Text::_('COM_EASYBOOKRELOADED_ENTRY_PUBLISHED');
                    $type = 'success';
                    break;
            }
        }

        $this->setRedirect(Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false), $message, $type);
    }

    /**
     * Performs the mail validation checks
     *
     * @param string $hashRequest
     *
     * @return bool
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    private function performValidationChecksMail(string $hashRequest): bool
    {
        // No empty hash value allowed
        if (empty($hashRequest)) {
            return false;
        }

        // Prepare request string
        $hashData = explode('-', $hashRequest);

        // The hash_data array must have 2 entries - ID of entry and the hash itself
        if (count($hashData) !== 2) {
            return false;
        }

        if (empty($hashData[0]) || !is_numeric($hashData[0])) {
            return false;
        }

        // Check whether the ID is available and numeric - load the entry for the checks
        /* @var EasybookReloadedModelEntry $model */
        $model = $this->getModel('entry');
        $gbRow = $model->getRow((int)$hashData[0]);

        $app = Factory::getApplication();
        $offset = $app->get('offset');

        $dateEntry = Factory::getDate($gbRow->get('gbdate'), $offset);
        $dateNow = Factory::getDate('now', $offset);

        $validTimeEmailnot = Helper::getParams('validTimeEmailNot') * 60 * 60 * 24;

        if ($dateEntry->toUnix() + $validTimeEmailnot <= $dateNow->toUnix()) {
            return false;
        }

        // Create a second hash link from the same data and compare it with the transmitted hash value
        $hash = [];
        $hash['id'] = (int)$gbRow->get('id');
        $hash['gbmail'] = md5($gbRow->get('gbmail'));
        $hash['username'] = $gbRow->get('gbname');

        // Get config object for the secret word and sitename
        $hash['customSecret'] = Factory::getConfig()->get('secret');

        $secretWord = Helper::getParams('secretWord');

        if (!empty($secretWord)) {
            $hash['customSecret'] = Helper::getParams('secretWord');
        }

        $hash = substr(base64_encode(md5(serialize($hash))), 0, 16);

        return !($hash !== $hashData[1]);
    }

    /**
     * This function is triggered when the user clicks on the remove link the notification mail
     *
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function removeMail(): void
    {
        $hashRequest = $this->input->getString('hash');
        $checkHash = $this->performValidationChecksMail($hashRequest);
        $gbId = $this->input->getInt('gbid');

        $message = Text::_('COM_EASYBOOKRELOADED_ERROR_ENTRY_COULD_NOT_BE_DELETED');
        $type = 'error';

        if ($checkHash === true) {
            /* @var EasybookReloadedModelEntry $model */
            $model = $this->getModel('entry');

            if ($model->delete()) {
                $message = Text::_('COM_EASYBOOKRELOADED_ENTRY_DELETED');
                $type = 'success';
            }
        }

        $this->setRedirect(Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false), $message, $type);
    }

    /**
     * This function is triggered when the user clicks on the comment link the notification mail
     *
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function commentMail(): void
    {
        $hashRequest = $this->input->getString('hash');
        $checkHash = $this->performValidationChecksMail($hashRequest);
        $gbId = $this->input->getInt('gbid');

        if ($checkHash === true) {
            $this->input->set('view', 'entry');
            $this->input->set('layout', 'commentform_mail');
            $this->input->set('hidemainmenu', 1);
            parent::display();

            return;
        }

        $message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_SAVE_COMMENT');
        $type = 'error';
        $this->setRedirect(Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false), $message, $type);
    }

    /**
     * This function is triggered when the user saves the comment form which was called from the notification mail
     *
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function savecommentMail(): void
    {
        $hashRequest = $this->input->getString('hash');
        $checkHash = $this->performValidationChecksMail($hashRequest);
        $gbId = $this->input->getInt('gbid');

        $message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_SAVE_COMMENT');
        $type = 'error';

        if ($checkHash === true) {
            /* @var EasybookReloadedModelEntry $model */
            $model = $this->getModel('entry');
            $id = $model->saveComment();

            if ($id !== 0) {
                $message = Text::_('COM_EASYBOOKRELOADED_COMMENT_SAVED');
                $toggleState = Factory::getApplication()->input->getBool('toggle_state', false);

                if ($toggleState) {
                    $model->publish();
                }

                $informCreator = Factory::getApplication()->input->getBool('inform', false);

                if ($informCreator) {
                    $data = $model->getRow($id);
                    $uri = Uri::getInstance();
                    $mail = Factory::getMailer();

                    $href = $uri::base() . EasybookReloadedRoute::getRoute($data->get('id'), $gbId);
                    $mail->setSubject(Text::_('COM_EASYBOOKRELOADED_ADMIN_COMMENT_SUBJECT'));
                    $mail->setBody(Text::sprintf('COM_EASYBOOKRELOADED_ADMIN_COMMENT_BODY', $data->get('gbname'), $uri::base(), $href));

                    if (Helper::getParams('sendMailHtml')) {
                        $mail->isHtml(true);
                        $mail->setBody(Text::sprintf('COM_EASYBOOKRELOADED_ADMIN_COMMENT_BODY_HTML', $data->get('gbname'), $uri::base(), $href));
                    }

                    $mail->addRecipient($data->get('gbmail'));
                    $mail->Send();

                    $message = Text::_('COM_EASYBOOKRELOADED_COMMENT_SAVED_INFORM');
                }

                $type = 'success';
            }
        }

        $this->setRedirect(Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false), $message, $type);
    }

    /**
     * This function is triggered when the user clicks on the edit link the notification mail
     *
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function editMail(): void
    {
        $hashRequest = $this->input->getString('hash');
        $checkHash = $this->performValidationChecksMail($hashRequest);
        $gbId = $this->input->getInt('gbid');

        if ($checkHash === true) {
            $this->input->set('view', 'entry');
            $this->input->set('layout', 'form_mail');
            parent::display();

            return;
        }

        $message = Text::_('COM_EASYBOOKRELOADED_ERROR_PLEASE_VALIDATE_YOUR_INPUTS');
        $type = 'error';
        $this->setRedirect(Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false), $message, $type);
    }

    /**
     * This function is triggered when the user saves the edit form which was called from the notification mail
     *
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function saveMail(): void
    {
        $hashRequest = $this->input->getString('hash');
        $checkHash = $this->performValidationChecksMail($hashRequest);
        $gbId = $this->input->getInt('gbid');

        $message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_SAVE_COMMENT');
        $type = 'error';

        if ($checkHash === true) {
            // Reset the time to avoid error in the spam check
            $session = Factory::getSession();
            $time = $session->get('time', null, 'easybookreloaded');
            $session->set('time', $time - Helper::getParams('typeTimeSeconds'), 'easybookreloaded');

            /* @var EasybookReloadedModelEntry $model */
            $model = $this->getModel('entry');

            if ($model->store()) {
                $message = Text::_('COM_EASYBOOKRELOADED_ENTRY_SAVED_BUT_HAS_TO_BE_APPROVED');
                $type = 'message';

                if (Helper::getParams('defaultPublished', true)) {
                    $message = Text::_('COM_EASYBOOKRELOADED_ENTRY_SAVED');
                    $type = 'success';
                }
            }
        }

        $this->setRedirect(Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false), $message, $type);
    }
}