| Current Path : /home/jeromecohp/www/plugins/system/yooessentials/ |
| Current File : /home/jeromecohp/www/plugins/system/yooessentials/install.php |
<?php
/**
* @package Essentials YOOtheme Pro (Freemium) 2.0.19 build 1026.0920
* @author ZOOlanders https://www.zoolanders.com
* @copyright Copyright (C) Joolanders, SL
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Installer\InstallerHelper;
if (!class_exists('plgSystemYooessentialsHelper')) {
require_once __DIR__ . '/helper.php';
}
class plgSystemYooessentialsInstallerScript
{
public function preflight($type, $parent)
{
if (!in_array($type, ['install', 'update'])) {
return;
}
try {
plgSystemYooessentialsHelper::validatePlatform();
} catch (\RuntimeException $e) {
plgSystemYooessentialsHelper::adminNotice($e->getMessage());
return false;
}
// prevent downgrade
if ($this->isPremiumInstalled() and !$this->isPremiumBeingInstalled($parent)) {
plgSystemYooessentialsHelper::adminNotice(
'Essentials plugin downgrade has been prevented. Please uninstall Essentials before trying to downgrade to the free version.'
);
return false;
}
$this->relocateIcons();
// by deleting the update servers we keep them up to date as well as solving
// the potential issue of free update server overtaking the premium one
if ($type === 'update') {
$this->deleteUpdateServers($parent);
}
// delete modules to avoid potential update and downgrade issues
if (Folder::exists($path = JPATH_ROOT . '/plugins/system/yooessentials/modules')) {
Folder::delete($path);
}
}
public function postflight($type, $parent)
{
if (!in_array($type, ['install', 'update'])) {
return;
}
$this->installInstallerPackage($parent);
if ($type === 'install') {
$this->enableExtension();
}
plgSystemYooessentialsHelper::clearCache();
return true;
}
private function relocateIcons()
{
$src = JPATH_ROOT . '/plugins/system/yooessentials/modules/icons/icons';
$dst = JPATH_ROOT . '/media/yooessentials/icons';
if (Folder::exists($src)) {
Folder::copy($src, $dst, '', true);
Folder::delete($src);
}
}
protected function deleteUpdateServers($parent)
{
$db = Factory::getDBO();
$ids = $db
->setQuery(
'SELECT `update_site_id` FROM `#__update_sites_extensions`' .
" WHERE `extension_id` = (SELECT `extension_id` FROM `#__extensions` WHERE `type` = 'plugin' AND `folder` = 'system' AND `element` = 'yooessentials')"
)
->loadObjectList();
foreach ($ids as $id) {
$db->setQuery(
"DELETE FROM `#__update_sites_extensions` WHERE `update_site_id` = $id->update_site_id"
)->execute();
$db->setQuery("DELETE FROM `#__update_sites` WHERE `update_site_id` = $id->update_site_id")->execute();
}
}
protected function isPremiumBeingInstalled($parent)
{
$src = $parent->getParent()->getPath('source');
return Folder::exists("$src/modules/access");
}
protected function isPremiumInstalled()
{
return Folder::exists(JPATH_ROOT . '/plugins/system/yooessentials/modules/access');
}
// silent install of zoolanders installer
private function installInstallerPackage($parent)
{
$src = $parent->getParent()->getPath('source');
try {
if (File::exists("$src/installer.zip")) {
$package = InstallerHelper::unpack("$src/installer.zip")['dir'];
$tmpInstaller = new Installer();
$tmpInstaller->install($package);
}
} catch (\Exception $e) {
return;
}
}
private function enableExtension()
{
$db = Factory::getDbo();
try {
$query = $db
->getQuery(true)
->update('#__extensions')
->set($db->qn('enabled') . ' = ' . $db->q(1))
->where('type = ' . $db->quote('plugin'))
->where('folder = ' . $db->quote('system'))
->where('element = ' . $db->quote('yooessentials'));
$db->setQuery($query)->execute();
} catch (\Exception $e) {
return;
}
}
}