Your IP : 216.73.216.240


Current Path : /home/juvelize/martine/administrator/components/com_easybookreloaded/
Upload File :
Current File : /home/juvelize/martine/administrator/components/com_easybookreloaded/script.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 Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;

/**
 * Class Com_EasybookReloadedInstallerScript
 *
 * @since 3.4.0-FREE
 */
class Com_EasybookReloadedInstallerScript
{
    public const MIN_VERSION_JOOMLA = '3.9.0';
    public const MIN_VERSION_PHP = '7.3.0';

    /**
     * Name of extension that is used in the error message
     *
     * @var string
     * @since 3.4.0-FREE
     */
    protected $extensionName = 'Easybook Reloaded';

    /**
     * Checks compatibility in the preflight event
     *
     * @param $type
     * @param $parent
     *
     * @return bool
     * @throws Exception
     * @since 3.4.0-FREE
     */
    public function preflight($type, $parent)
    {
        if (!$this->checkVersionJoomla()) {
            return false;
        }

        if (!$this->checkVersionPhp()) {
            return false;
        }

        $this->removeDeprecatedFiles();

        return true;
    }

    /**
     * Checks whether the Joomla! version meets the requirement
     *
     * @return bool
     * @throws Exception
     * @since 3.4.0-FREE
     */
    private function checkVersionJoomla()
    {
        // Using deprecated JVersion, JFactory and JText classes to avoid exceptions in old Joomla! versions
        $version = new JVersion();

        if (!$version->isCompatible(self::MIN_VERSION_JOOMLA)) {
            JFactory::getApplication()->enqueueMessage(JText::sprintf('KRJE_ERROR_JOOMLA_VERSION', $this->extensionName, self::MIN_VERSION_JOOMLA), 'error');

            return false;
        }

        return true;
    }

    /**
     * Checks whether the PHP version meets the requirement
     *
     * @return bool
     * @throws Exception
     * @since 3.4.0-FREE
     */
    private function checkVersionPhp()
    {
        if (!version_compare(phpversion(), self::MIN_VERSION_PHP, 'ge')) {
            JFactory::getApplication()->enqueueMessage(JText::sprintf('KRJE_ERROR_PHP_VERSION', $this->extensionName, self::MIN_VERSION_PHP), 'error');

            return false;
        }

        return true;
    }

    /**
     * Removes deprecated files
     *
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    private function removeDeprecatedFiles(): void
    {
        $deprecatedFiles = [
            JPATH_SITE . '/components/com_easybookreloaded/helpers/acl.php',
            JPATH_SITE . '/components/com_easybookreloaded/helpers/content.php',
            JPATH_SITE . '/components/com_easybookreloaded/helpers/menu.php',
            JPATH_SITE . '/components/com_easybookreloaded/helpers/route.php',
            JPATH_SITE . '/components/com_easybookreloaded/helpers/smilie.php',
            JPATH_SITE . '/components/com_easybookreloaded/scripts/moostarrating.js',
            JPATH_ADMINISTRATOR . '/components/com_easybookreloaded/helpers/easybookreloaded.php',
        ];

        foreach ($deprecatedFiles as $deprecatedFile) {
            if (File::exists($deprecatedFile)) {
                File::delete($deprecatedFile);
            }
        }
    }

    /**
     * Modifies database structure in the update trigger
     *
     * @param $parent
     *
     * @since 3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function update($parent): void
    {
        $db = Factory::getDbo();
        $db->setQuery("DESCRIBE " . $db->quoteName('#__easybook'));
        $ebrDbStructure = $db->loadAssocList();

        // Add the column gbid if the component was already installed previously - since version 3-2
        if (!$this->hasField('gbid', $ebrDbStructure)) {
            $db->setQuery("ALTER TABLE " . $db->quoteName('#__easybook') . " ADD " . $db->quoteName('gbid') . " INT NOT NULL DEFAULT '1' AFTER " . $db->quoteName('id'));
            $db->execute();
        }

        // Add the column gbimage if the component was already installed previously - since version 3.4.1-PRO
        if (!$this->hasField('gbimage', $ebrDbStructure)) {
            $db->setQuery("ALTER TABLE " . $db->quoteName('#__easybook') . " ADD " . $db->quoteName('gbimage') . " TINYTEXT DEFAULT NULL AFTER " . $db->quoteName('gbskype'));
            $db->execute();
        }

        // Add new table easybook_gb - since version 3-2
        $db->setQuery("CREATE TABLE IF NOT EXISTS " . $db->quoteName('#__easybook_gb') . " (" . $db->quoteName('id') . " INT(11) NOT NULL AUTO_INCREMENT, " . $db->quoteName('title') . " VARCHAR(255) NOT NULL DEFAULT '', " . $db->quoteName('introtext') . " TEXT NOT NULL, PRIMARY KEY (" . $db->quoteName('id') . ")) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;");
        $db->execute();

        // Do we have a default guestbook? - needed for existing entries - since version 3-2
        $db->setQuery("SELECT * FROM " . $db->quoteName('#__easybook_gb'));
        $ebrGbEntries = $db->loadAssoc();

        if (empty($ebrGbEntries)) {
            $db->setQuery("INSERT INTO " . $db->quoteName('#__easybook_gb') . " (" . $db->quoteName('id') . ", " . $db->quoteName('title') . ", " . $db->quoteName('introtext') . ") VALUES (1, '" . JText::_('COM_EASYBOOKRELOADED_SCRIPT_DEFAULT_GUESTBOOK') . "', '" . JText::_('COM_EASYBOOKRELOADED_SCRIPT_DEFAULT_GUESTBOOK_INTROTEXT') . "');");
            $db->execute();
        }

        // Rewrite the menu link entry if guestbook id is not set - since version 3-2
        $db->setQuery("SELECT * FROM " . $db->quoteName('#__menu') . " WHERE " . $db->quoteName('link') . " = 'index.php?option=com_easybookreloaded&view=easybookreloaded' AND " . $db->quoteName('client_id') . " = 0");
        $menuGbid = $db->loadAssoc();

        if (!empty($menuGbid)) {
            $db->setQuery("UPDATE " . $db->quoteName('#__menu') . " SET " . $db->quoteName('link') . " = 'index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=1' WHERE " . $db->quoteName('id') . " = " . $menuGbid['id'] . ";");
            $db->execute();
        }
    }

    /**
     * Checks database structure for a specific field name
     *
     * @param string $fieldName
     * @param array  $dbStructure
     *
     * @return bool
     * @since 3.4.0-FREE
     */
    private function hasField(string $fieldName, array $dbStructure): bool
    {
        if (empty($dbStructure)) {
            return false;
        }

        foreach ($dbStructure as $dbEntry) {
            if ($dbEntry['Field'] === $fieldName) {
                return true;
            }
        }

        return false;
    }
}