<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
defined('_JEXEC') or die;

if (version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisFactory', '\\Joomla\\CMS\\Factory');
}
else
{
	JLoader::registerAlias('EorisisFactory', 'JFactory');
}

class PlgSystemEorisis_pixelsInstallerScript
{
	const EXTENSION_NAME = 'Pixels';
	const MIN_JVERSION = '3.6.0';

	// --------------------------------------------------

	public function preflight($type, $parent)
	{
		if (defined('EORISIS_FORCE_UNINSTALL'))
		{
			return;
		}

		// Preflight is executed prior to any Joomla install,
		// update or discover_install actions (or uninstall for plugins).
		$type = strtolower($type);

		// --------------------------------------------------

		if ($type != 'uninstall')
		{
			if (!defined('EORISIS_PKG_INSTALL'))
			{
				if (defined('EORISIS_PKG_INSTALL_ABORT') or $this->is_invalid_install($type))
				{
					return false;
				}
			}

			if (defined('EORISIS_PKG_INSTALL_ABORT') or $this->is_old_jversion())
			{
				return false;
			}

			return;
		}

		if ($this->is_pkg_uninstall($parent))
		{
			return;
		}

		// Display a warning and abort on uninstall, if the uninstall does not originate from the package installer
		$txt  = 'This plugin is part of a full package and cannot be uninstalled separately. ';
		$txt .= 'You may uninstall <b>Eorisis '.self::EXTENSION_NAME.'</b> <span style="text-decoration:underline">Package</span> instead.<br>';
		$txt .= 'If you are facing trouble with the installation please report it directly at '.$this->support();
		$this->set_message($txt, 'warning');

		return false;
	}

	// --------------------------------------------------

	public function install($parent)
	{
		if (!defined('EORISIS_PKG_INSTALL'))
		{
			return false;
		}
	}

	// --------------------------------------------------

	public function update($parent)
	{
		if (!defined('EORISIS_PKG_INSTALL'))
		{
			return false;
		}
	}

	// --------------------------------------------------

//	public function uninstall($parent) {}

	// --------------------------------------------------

//	public function postflight($type, $parent) {}

	// --------------------------------------------------

	private function is_pkg_uninstall($parent) : bool
	{
		if (version_compare(JVERSION, '3.7', '>='))
		{
			return (bool)$parent->getParent()->get('packageUninstall');
		}

		if (version_compare(JVERSION, '3.6', '<'))
		{
			// 3.6.0 or older are not yet supported
			return true;
		}

		if (!function_exists('debug_backtrace'))
		{
			return false;
		}

		$arr = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
		if (!$arr)
		{
			return false;
		}

		foreach ($arr as $trace)
		{
			if (empty($trace['function']) or empty($trace['file']))
			{
				continue;
			}

			if ($trace['function'] != 'uninstall')
			{
				continue;
			}

			if (strpos((string)$trace['file'], '/libraries/cms/installer/adapter/package.php') !== false)
			{
				return true;
			}
		}

		return false;
	}

	// --------------------------------------------------

	private function support() : string
	{
		$email = 'support@eorisis.com';
		return '<a href="mailto:'.$email.'" target="_top">'.$email.'</a>';
	}

	// --------------------------------------------------

	private function set_message(string $txt, string $type)
	{
		EorisisFactory::getApplication()->enqueueMessage($txt, $type);
	}

	// --------------------------------------------------

	private function is_invalid_install($type) : bool
	{
		if (!in_array($type, ['install','update','discover_install']))
		{
			return false;
		}

		// Display a warning and abort on install, if the install does not originate from the package installer
		$txt  = 'This component is part of a full package and cannot be installed separately. ';
		$txt .= 'You may install <b>Eorisis '.self::EXTENSION_NAME.'</b> <span style="text-decoration:underline">Package</span> instead.<br>';
		$txt .= 'The downloadable archive file is intended to be used as-is at the Joomla Extensions Manager.<br>';
		$txt .= 'This will execute the package installer and perform all tasks required.<br>';
		$txt .= 'DO NOT unzip/extract or upload separate files that are included in the package in order to perform an install or update.<br>';
		$txt .= 'Doing so may lead to errors.<br>';
		$txt .= 'If you are facing trouble with the installation please report it directly at '.$this->support();
		$this->set_message($txt, 'warning');
		define('EORISIS_PKG_INSTALL_ABORT', 1);
		return true;
	}

	// --------------------------------------------------

	private function is_old_jversion() : bool
	{
		if (!version_compare(JVERSION, '2.5.4', '<'))
		{
			return false;
		}

		$txt  = '<b>Eorisis '.self::EXTENSION_NAME.'</b> ';
		$txt .= 'installation cannot continue because you are running Joomla <b>'.JVERSION.'</b>.<br>';
		$txt .= 'The minimum Joomla version for this extension is <b>'.self::MIN_JVERSION.'</b>.<br>';
		$txt .= 'You must upgrade your Joomla before you can install this extension.';
		$this->set_message($txt, 'warning');
		define('EORISIS_PKG_INSTALL_ABORT', 1);
		return true;
	}
}
