<?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
 */
namespace com_eorisis_pixels\framework\install_package\lib\core\model;
\defined('_JEXEC') or die;

use version_compare, str_replace, preg_replace, strlen, substr, is_dir;
use com_eorisis_pixels\framework\install_package\lib\core\cms\component as cms_component;
use com_eorisis_pixels\framework\install_package\lib\core\model\component as component_model;
use com_eorisis_pixels\framework\install_package\lib\core\model\params;
use com_eorisis_pixels\framework\install_package\lib\core\model\setup;
use com_eorisis_pixels\framework\install_package\lib\core\model\clean;
use com_eorisis_pixels\framework\install_package\lib\core\model\zip;
use com_eorisis_pixels\framework\install_package\lib\core\model\xml;

class data
{
	public static $package = [];
	public static $extensions = [];
	public static $ext_version_old = '';
	public static $component_element = '';

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

	public function __construct() {}

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

	public function set_package($parent) : bool
	{
		$package_xml = $this->get_manifest($parent);
		if (!$package_xml)
		{
			return false;
		}

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

		$copyright = str_replace('(C)', '&copy;', params::xml_attr('copyright', $package_xml));

		self::$package['title']           = params::xml_attr('title', $package_xml);
		self::$package['author']          = params::xml_attr('author', $package_xml);
		self::$package['author_uri']      = params::xml_attr('authorUrl', $package_xml);
		self::$package['author_email']    = params::xml_attr('authorEmail', $package_xml);
		self::$package['min_jversion']    = params::xml_attr('version', $package_xml->attributes());
		self::$package['min_php_version'] = params::xml_attr('min_php_version', $package_xml);
		self::$package['version']         = params::xml_attr('version', $package_xml);
		self::$package['release_date']    = params::xml_attr('creationDate', $package_xml);
		self::$package['app_uri']         = params::xml_attr('app_uri', $package_xml);
		self::$package['short_uri']       = params::xml_attr('short_uri', $package_xml);
		self::$package['copyright']       = $copyright;
		self::$package['packagename']     = params::xml_attr('packagename', $package_xml);

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

		// Extensions
		foreach ($package_xml->files->file as $ext)
		{
			$attr = $ext->attributes();

			$arr['filename'] = (string)$ext;
			$arr['name']     = params::xml_attr('name', $attr);
			$arr['type']     = params::xml_attr('type', $attr);
			$arr['element']  = params::xml_attr('id', $attr);
			$arr['group']    = params::xml_attr('group', $attr);
			self::$extensions[] = $arr;
		}

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

		$zip = new zip;
		$current_path = setup::$current_path;
		$language_ini = null;

		foreach (self::$extensions as $key => $ext)
		{
			$xml_filename = '';

			switch ($ext['type'])
			{
				case 'component':
					$xml_filename = substr((string)$ext['element'], 4); // com_
					break;

				case 'module':
				case 'plugin':
					$xml_filename = $ext['element'];
					break;

				case 'template':
					$xml_filename = 'templateDetails';
					break;
			}

			if (!$xml_filename)
			{
				return false;
			}

			$zip_path = $current_path.'/extensions/'.$ext['filename'];
			$extension_xml = $zip->read_file_from_archive($xml_filename.'.xml', $zip_path);
			if (!$extension_xml)
			{
				return false;
			}

			$xml = xml::element($extension_xml);
			$area = '';

			if ($ext['type'] == 'component')
			{
				self::$component_element = self::$extensions[$key]['element'];

				if (isset($xml->administration))
				{
					$lang_path_rel = 'administrator/language/'.(string)$xml->administration->languages->language[1];
				}
				else
				{
					$lang_path_rel = 'site/language/'.(string)$xml->site->languages->language[1];
				}

				self::$extensions[$key]['area'] = $area;
			}
			else
			{
				$lang_path_rel = 'language/'.(string)$xml->languages->language[1];
				$area = params::xml_attr('client', $xml->attributes());
				self::$extensions[$key]['area'] = $area;
			}

			$language_ini = $zip->read_file_from_archive($lang_path_rel, $zip_path);
			if (!$language_ini)
			{
				return false;
			}

			$path_root = '';

			switch ($ext['type'])
			{
				case 'component':
					$path_root = JPATH_ADMINISTRATOR.'/components/';
					break;

				case 'template':
				case 'module':
				{
					$type_dir = '/'.$ext['type'].'s/';

					switch ($area)
					{
						case 'site':
							self::$extensions[$key]['client_id'] = '0';
							$path_root = JPATH_SITE.$type_dir;
							break;

						case 'administrator':
							self::$extensions[$key]['client_id'] = '1';
							$path_root = JPATH_ADMINISTRATOR.$type_dir;
							break;
					}
				} break;

				case 'plugin':
					$path_root = JPATH_PLUGINS.'/'.$ext['group'].'/';
					break;
			}

			if (!$path_root)
			{
				return false;
			}

			self::$extensions[$key]['media_dir'] = $this->get_media_dir($xml);
			self::$extensions[$key]['path'] = $path_root.$ext['element'].'/';
		}

		return true;
	}

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

	private function get_media_dir($xml) : string
	{
		$dir = (string)$xml->media['destination'];
		if (!$dir)
		{
			return '';
		}

		$dir = preg_replace('/[^a-z_-]+/', '', $dir);
		return strlen((string)$dir) ? JPATH_SITE.'/media/'.$dir.'/' : '';
	}

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

	private function get_manifest($parent)
	{
		if (version_compare(JVERSION, '3.6', '>='))
		{
			return $parent->manifest;
		}

		return $parent->get('manifest');
	}

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

	public static function get_auth_code() : string
	{
		$path = JPATH_ADMINISTRATOR.'/components/'.self::$component_element;
		if (!is_dir($path))
		{
			return '';
		}

		$auth_code = component_model::get_auth_code();
		if ($auth_code)
		{
			return $auth_code;
		}

		// Fallback to the old table
		$component_params = cms_component::get_params(self::$component_element);
		return clean::auth_code(params::get('auth_token', $component_params));
	}
}
