Your IP : 216.73.216.240


Current Path : /home/juvelize/martine/administrator/components/com_easybookreloaded/helpers/
Upload File :
Current File : /home/juvelize/martine/administrator/components/com_easybookreloaded/helpers/Helper.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/>.
 */

namespace EasybookReloaded;

defined('_JEXEC') || die('Restricted access');

use Exception;
use JHtmlSidebar;
use Joomla\CMS\{Language\Text, Component\ComponentHelper, Factory, Uri\Uri, User\UserHelper, Http\HttpFactory};
use Joomla\Registry\Registry;

/**
 * Class Helper
 *
 * @package EasybookReloaded
 *
 * @since   3.4.0-FREE
 * @version 3.4.1.1-FREE
 */
class Helper
{
    /**
     * @var string EASYBOOK_RELOADED_VERSION
     * @since   3.4.0-FREE
     * @version 3.4.1.1-FREE
     */
    public const EASYBOOK_RELOADED_VERSION = '3.4.1.1-FREE';

    /**
     * @var string $systemName
     * @since 3.4.0-FREE
     */
    public static $systemName = 'com_easybookreloaded';

    /**
     * Adds the sub menu
     *
     * @param string $viewName
     *
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function addSubmenu(string $viewName): void
    {
        JHtmlSidebar::addEntry(Text::_('COM_EASYBOOKRELOADED_SUBMENU_ENTRIES'), 'index.php?option=com_easybookreloaded', $viewName === 'easybookreloaded');
        JHtmlSidebar::addEntry(Text::_('COM_EASYBOOKRELOADED_SUBMENU_GB'), 'index.php?option=com_easybookreloaded&amp;view=gb', $viewName === 'gb');
        JHtmlSidebar::addEntry(Text::_('COM_EASYBOOKRELOADED_SUBMENU_BADWORDFILTER'), 'index.php?option=com_easybookreloaded&amp;view=badwords', $viewName === 'badwords');
    }

    /**
     * Gets the footer for the backend with the current version number
     *
     * @return string
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function getFooter(): string
    {
        return '<div style="text-align: center;"><p>' . Text::sprintf('COM_EASYBOOKRELOADED_VERSION', self::EASYBOOK_RELOADED_VERSION) . '</p></div>';
    }

    /**
     * Returns pro feature note in red colour
     *
     * @return string
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function isProFeature(): string
    {
        return '<span style="color: red; padding-top: 5px; display: inline-block;">PRO FEATURE</span>';
    }

    /**
     * Escapes a string
     *
     * @param string $string
     * @param bool   $doubleEncode
     *
     * @return string
     * @since 3.4.1.0-FREE
     */
    public static function escapeString(string $string, bool $doubleEncode = false): string
    {
        return htmlspecialchars($string, ENT_COMPAT, 'UTF-8', $doubleEncode);
    }

    /**
     * Checks whether the donation code was entered and if the code is correct.
     * The code is taken from the main Kubik-Rubik Donation Code Check field.
     *
     * @return string
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function getDonationCodeMessage(): string
    {
        $donationCode = self::getParams('donation_code', '');

        $session = Factory::getSession();
        $fieldValueSession = $session->get('field_value', null, 'krdonationcodecheck_footer');
        $fieldValueHeadSession = $session->get('field_value_head', null, 'krdonationcodecheck_footer');
        $donationCodeSession = $session->get('donation_code', null, 'krdonationcodecheck_footer');

        if ($fieldValueSession === 1 && ($donationCode === $donationCodeSession)) {
            return '';
        } elseif (!empty($fieldValueSession) && !empty($fieldValueHeadSession) && ($donationCode === $donationCodeSession)) {
            self::addHeadData($fieldValueHeadSession);

            return $fieldValueSession;
        }

        $fieldValue = '';
        $donationCodeCheck = false;
        $host = Uri::getInstance()->getHost();

        if ($host === 'localhost') {
            $fieldValue = '<div class="' . self::randomClassName($session) . '">' . Text::_('KR_DONATION_CODE_CHECK_DEFAULT_LOCALHOST') . '</div>';

            if (!empty($donationCode)) {
                $fieldValue .= '<div style="text-align: center; border: 1px solid #F2DB82; border-radius: 2px; padding: 5px; background-color: #F7EECA; font-size: 120%; margin: 10px 0;">' . Text::_('KR_DONATION_CODE_CHECK_ERROR_LOCALHOST') . '</div>';
            }
        } else {
            $donationCodeCheck = self::getDonationCodeStatus($host, $donationCode);

            if ($donationCodeCheck !== 1) {
                $fieldValue = '<div class="' . self::randomClassName($session) . '">' . Text::sprintf('KR_DONATION_CODE_CHECK_DEFAULT', $host) . '</div>';

                if ($donationCodeCheck === -1) {
                    $fieldValue .= '<div style="text-align: center; border: 1px solid #F2DB82; border-radius: 2px; padding: 5px; background-color: #F7EECA; font-size: 120%; margin: 10px 0;">' . Text::_('KR_DONATION_CODE_CHECK_ERROR_SERVER') . '</div>';
                }

                if ($donationCodeCheck === -2) {
                    $fieldValue .= '<div style="text-align: center; border: 1px solid #F2DB82; border-radius: 2px; padding: 5px; background-color: #F7EECA; font-size: 120%; margin: 10px 0;">' . Text::_('KR_DONATION_CODE_CHECK_ERROR') . '</div>';
                }
            }
        }

        if ($donationCodeCheck === 1) {
            $session->set('field_value', 1, 'krdonationcodecheck_footer');
        } else {
            $session->set('field_value', $fieldValue, 'krdonationcodecheck_footer');
        }

        $session->set('donation_code', $donationCode, 'krdonationcodecheck_footer');

        return $fieldValue;
    }

    /**
     * Gets legacy paths (underscore instead of camelcase) if new paths were not set yet after an update
     *
     * @param string $path
     * @param mixed  $default
     *
     * @return mixed
     * @since 3.4.0-FREE
     */
    public static function getParams(string $path, $default = null)
    {
        $params = ComponentHelper::getParams(self::$systemName);
        $param = $params->get($path);

        if (!is_null($param)) {
            return $param;
        }

        $pathLegacy = strtolower(preg_replace('@(?<!^)[A-Z]@', '_$0', $path));
        $paramLegacy = $params->get($pathLegacy);

        if (!is_null($paramLegacy)) {
            return $paramLegacy;
        }

        return $default;
    }

    /**
     * Adds the style definition to the head of the HTML page
     *
     * @staticvar bool $data_loaded
     *
     * @param string $data
     *
     * @since     3.4.0-FREE
     * @version   3.4.1.0-FREE
     */
    private static function addHeadData(string $data): void
    {
        static $dataLoaded = false;

        if (empty($dataLoaded)) {
            Factory::getDocument()->addCustomTag($data);

            $dataLoaded = true;
        }
    }

    /**
     * Creates a valid, random name for the class selector
     *
     * @param object $session
     *
     * @return string
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    private static function randomClassName(object $session): string
    {
        $characters = range('a', 'z');
        $className = $characters[random_int(0, count($characters) - 1)];
        $classNameLength = random_int(6, 12);
        $className .= UserHelper::genRandomPassword($classNameLength);

        $headData = '<style>div.' . $className . '{text-align: center; border: 1px solid #DD87A2; border-radius: 2px; padding: 5px; background-color: #F9CAD9; font-size: 120%; margin: 10px 0;}</style>';

        self::addHeadData($headData);
        $session->set('field_value_head', $headData, 'krdonationcodecheck_footer');

        return $className;
    }

    /**
     * Gets the status of the entered donation code from the donation code script
     *
     * @param string $host
     * @param string $donationCode
     *
     * @return int
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    private static function getDonationCodeStatus(string $host, string $donationCode): int
    {
        $donationCodeCheck = 0;

        if (HttpFactory::getAvailableDriver(new Registry()) === false) {
            return -2;
        }

        if (!empty($host) && !empty($donationCode)) {
            // First try it with the main validation server and with HTTPS
            $urlCheck = 'https://check.kubik-rubik.de/donationcode/validation.php?key=' . rawurlencode($donationCode) . '&host=' . rawurlencode($host);

            try {
                $donationCodeRequest = HttpFactory::getHttp()->get($urlCheck);
            } catch (Exception $e) {
                // Try it with the fall back server and without HTTPS
                $urlCheckFallback = 'http://check.kubik-rubik.eu/donationcode/validation.php?key=' . rawurlencode($donationCode) . '&host=' . rawurlencode($host);

                try {
                    $donationCodeRequest = HttpFactory::getHttp()->get($urlCheckFallback);
                } catch (Exception $e) {
                    return -1;
                }
            }

            if (!empty($donationCodeRequest->body)) {
                if (preg_match('@(error|access denied)@i', $donationCodeRequest->body)) {
                    return -1;
                }

                $donationCodeCheck = (int)$donationCodeRequest->body;
            }
        }

        return $donationCodeCheck;
    }
}