Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/administrator/components/com_ats/
Upload File :
Current File : /var/www/consult-e-syn/public_html/administrator/components/com_ats/script.com_ats.php

<?php
/**
 * @package   ats
 * @copyright Copyright (c)2011-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
 * @license   GNU General Public License version 3, or later
 */

/** @noinspection MissingSinceTagDocInspection */

defined('_JEXEC') or die();

use Akeeba\TicketSystem\Admin\Helper\PluginParameters;
use FOF40\Container\Container;
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Installer\Adapter\ComponentAdapter;
use Joomla\CMS\Uri\Uri as JUri;
use Joomla\Registry\Registry as JRegistry;

// Load FOF if not already loaded
if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
{
	throw new RuntimeException('This extension requires FOF 4.');
}

/**
 * Post-installation script
 *
 * @since       2.0.0.b1
 *
 * @noinspection PhpUnused
 */
class Com_AtsInstallerScript extends \FOF40\InstallScript\Component
{
	/**
	 * The component's name
	 *
	 * @var   string
	 */
	public $componentName = 'com_ats';

	/**
	 * The title of the component (printed on installation and uninstallation messages)
	 *
	 * @var string
	 */
	protected $componentTitle = 'Akeeba Ticket System';

	/**
	 * The minimum PHP version required to install this extension
	 *
	 * @var   string
	 */
	protected $minimumPHPVersion = '7.2.0';

	/**
	 * The minimum Joomla! version required to install this extension
	 *
	 * @var   string
	 */
	protected $minimumJoomlaVersion = '3.9.0';

	/**
	 * Obsolete files and folders to remove from both paid and free releases. This is used when you refactor code and
	 * some files inevitably become obsolete and need to be removed.
	 *
	 * @var   array
	 */
	protected $removeFilesAllVersions = [
		'files'   => [
			// Obsolete CLI scripts
			'cli/ats-upgrade.php',

			// Obsolete update information
			'cache/com_ats.updates.php',
			'cache/com_ats.updates.ini',
			'administrator/cache/com_ats.updates.php',
			'administrator/cache/com_ats.updates.ini',
			'plugins/ats/postemail/test.email',

			// Obsolete Joomla! 2.5 support
			'media/com_ats/css/chosen.min.css',
			'media/com_ats/css/chosen-sprite.png',
			'media/com_ats/js/chosen.jquery.min.js',

			// After migration to FOF 3
			'administrator/components/com_ats/dispatcher.php',
			'administrator/components/com_ats/toolbar.php',
			'administrator/components/com_ats/index.html',
			'administrator/components/com_ats/assets/cacert.pem',
			'administrator/components/com_ats/assets/index.html',
			'administrator/components/com_ats/helpers/autooffline.php',
			'administrator/components/com_ats/helpers/avatar.php',
			'administrator/components/com_ats/helpers/bbcode.php',
			'administrator/components/com_ats/helpers/credits.php',
			'administrator/components/com_ats/helpers/editor.php',
			'administrator/components/com_ats/helpers/filter.php',
			'administrator/components/com_ats/helpers/format.php',
			'administrator/components/com_ats/helpers/html.php',
			'administrator/components/com_ats/helpers/jsonlib.php',
			'administrator/components/com_ats/helpers/mail.php',
			'administrator/components/com_ats/helpers/select.php',
			'administrator/components/com_ats/helpers/signature.php',
			'administrator/components/com_ats/helpers/subscriptions.php',
			'administrator/components/com_ats/models/autoreplies.php',
			'administrator/components/com_ats/models/buckets.php',
			'administrator/components/com_ats/models/cannedreplies.php',
			'administrator/components/com_ats/models/cpanels.php',
			'administrator/components/com_ats/models/creditconsumptions.php',
			'administrator/components/com_ats/models/credittransactions.php',
			'administrator/components/com_ats/models/emailtemplates.php',
			'administrator/components/com_ats/models/index.html',
			'administrator/components/com_ats/models/jusers.php',
			'administrator/components/com_ats/models/managernotes.php',
			'administrator/components/com_ats/models/posts.php',
			'administrator/components/com_ats/models/stats.php',
			'administrator/components/com_ats/models/tickets.php',
			'administrator/components/com_ats/models/updates.php',
			'administrator/components/com_ats/models/usertags.php',

			// Obsolete eAccelerator warning
			'administrator/components/com_ats/View/eaccelerator.php',

			// jQPlot (replaced with Canvas.js)
			'media/com_ats/css/jquery.jqplot.min.css',
			'media/com_ats/js/excanvas.min.js',
			'media/com_ats/js/jqplot.barRenderer.min.js',
			'media/com_ats/js/jqplot.canvasAxisLabelRenderer.min.js',
			'media/com_ats/js/jqplot.canvasAxisTickRenderer.min.js',
			'media/com_ats/js/jqplot.canvasTextRenderer.min.js',
			'media/com_ats/js/jqplot.categoryAxisRenderer.min.js',
			'media/com_ats/js/jqplot.dateAxisRenderer.min.js',
			'media/com_ats/js/jqplot.hermite.js',
			'media/com_ats/js/jqplot.highlighter.min.js',
			'media/com_ats/js/jqplot.pieRenderer.min.js',
			'media/com_ats/js/jquery.jqplot.min.js',

			// Old CSS files (replaced by minified files created from SCSS sources)
			'media/com_ats/css/backend.css',
			'media/com_ats/css/frontend.css',
			'media/com_ats/css/print.css',

			// Typo
			'components/com_ats/Model/AutomaticReplies.php',

			// Old mail fetch implementation
			'components/com_ats/Model/EmailCheck.php',
			'administrator/components/com_ats/Model/EmailCheck.php',

			// Obsolete PHP version (replaced with common code)
			'administrator/components/com_ats/ViewTemplates/ControlPanel/obsolete_php.blade.php',

			// Obsolete files 3.4.0
			'administrator/components/com_ats/Controller/TraitSaveUserTags.php',
			'administrator/components/com_ats/Helper/ComponentParams.php',
			'administrator/components/com_ats/Helper/Editor.php',
			'media/com_ats/css/uncompressed/sources/common/_markitup.scss',
			'media/com_ats/js/adm_buckets_choose.js',
			'media/com_ats/js/adm_control_panel.js',
			'media/com_ats/js/adm_tickets.js',
			'media/com_ats/js/ajax_posting.js',
			'media/com_ats/js/assigned_tickets.js',
			'media/com_ats/js/bbcode-set.js',
			// -- Note: the <!> marker indicates a case-sensitive file
			'<!>media/com_ats/js/buckets.js',
			'media/com_ats/js/buckets_choose.js',
			'media/com_ats/js/cannedreplies.js',
			'media/com_ats/js/cannedreply-keyboard.js',
			'<!>media/com_ats/js/cannedreply.js',
			'media/com_ats/js/editor_buckets.js',
			'<!>media/com_ats/js/instantreply.js',
			'media/com_ats/js/jquery.blockUI.js',
			'media/com_ats/js/jquery.form.js',
			'media/com_ats/js/jquery.markitup.js',
			'media/com_ats/js/latest.js',
			'media/com_ats/js/latestopen-keyboard.js',
			'media/com_ats/js/modal.js',
			'<!>media/com_ats/js/newticket.js',
			'media/com_ats/js/namespaced.js',
			'media/com_ats/js/preview.js',
			'media/com_ats/js/priority_credits.js',
			'media/com_ats/js/tabs_enable.js',
			'media/com_ats/js/ticket.js',
			'<!>media/com_ats/js/tickets.js',
			'media/com_ats/js/tickets_per_user.js',
			'media/com_ats/js/ticket_timer.js',
			'<!>media/com_ats/js/timecards.js',
			'media/com_ats/js/userfeedback.js',

			// Moving to Akeeba FEF 2
			'media/com_ats/js/Ajax.js',
			'media/com_ats/js/Ajax.min.js',
			'media/com_ats/js/Modal.js',
			'media/com_ats/js/Modal.min.js',
			'media/com_ats/js/System.js',
			'media/com_ats/js/System.min.js',
		],
		'folders' => [
			// After migration to FOF 3
			'administrator/components/com_ats/assets/datatables',
			'administrator/components/com_ats/assets/customfields',
			'administrator/components/com_ats/assets/mailer',
			'administrator/components/com_ats/controllers',
			'administrator/components/com_ats/fields',
			'administrator/components/com_ats/fof',
			'administrator/components/com_ats/tables',
			'administrator/components/com_ats/views',
			'components/com_ats/controllers',
			'components/com_ats/helpers',
			'components/com_ats/models',
			'components/com_ats/views',

			// After conversion to FEF
			'administrator/components/com_ats/Form',

			// Common tables (they're installed by FOF)
			'administrator/components/com_ats/sql/common',

			// Very old SQL files being picked up by Joomla 4...
			'administrator/components/com_ats/sql/install',
			'administrator/components/com_ats/sql/update',
			'administrator/components/com_ats/sql/updates',

			// CLI base script (replaced with FOF base CLI script)
			'administrator/components/com_ats/assets/cli',

			// Flag icons
			'media/com_ats/flags',

			// Refactored mail fetch
			'administrator/components/com_ats/Utils',

			// Old mail fetch implementation
			'administrator/components/com_ats/Model/EmailCheck',
			'administrator/components/com_ats/vendor',

			// Obsolete BBcode support
			'media/com_ats/css/markitup',
			'media/com_ats/js/markitup',

			// Update notification
			'administrator/components/com_ats/Controller/Updates.php',

			// Migrating to FOF 4
			'administrator/components/com_ats/View/AutoReplies/tmpl',
			'administrator/components/com_ats/View/Buckets/tmpl',
			'administrator/components/com_ats/View/CannedReplies/tmpl',
			'administrator/components/com_ats/View/ControlPanel/tmpl',
			'administrator/components/com_ats/View/CreditTransactions/tmpl',
			'administrator/components/com_ats/View/CustomFields/tmpl',
			'administrator/components/com_ats/View/EmailTemplates/tmpl',
			'administrator/components/com_ats/View/Log/tmpl',
			'administrator/components/com_ats/View/ManagerNotes/tmpl',
			'administrator/components/com_ats/View/OfflineSchedules/tmpl',
			'administrator/components/com_ats/View/Posts/tmpl',
			'administrator/components/com_ats/View/PostStatistics/tmpl',
			'administrator/components/com_ats/View/SchedulingInfo/tmpl',
			'administrator/components/com_ats/View/Tickets/tmpl',
			'administrator/components/com_ats/View/TicketStatistics/tmpl',
			'administrator/components/com_ats/View/TimeCards/tmpl',
			'administrator/components/com_ats/View/UserTags/tmpl',
			'administrator/components/com_ats/ViewTemplates',

			'components/com_ats/View/AssignedTickets/tmpl',
			'components/com_ats/View/Buckets/tmpl',
			'components/com_ats/View/CannedReplies/tmpl',
			'components/com_ats/View/Categories/tmpl',
			'components/com_ats/View/InstantReplies/tmpl',
			'components/com_ats/View/Jusers/tmpl',
			'components/com_ats/View/Latests/tmpl',
			'components/com_ats/View/ManagerNotes/tmpl',
			'components/com_ats/View/Mies/tmpl',
			'components/com_ats/View/NewTickets/tmpl',
			'components/com_ats/View/Posts/tmpl',
			'components/com_ats/View/Tickets/tmpl',
			'components/com_ats/ViewTemplates',

            // Latests template folder had the wrong name
            'components/com_ats/tmpl/Latest',

			// Update Charts.js
			'media/com_ats/js/Chart.bundle.min.js',
		],
	];

	/**
	 * Obsolete files and folders to remove from the free version only. This is used when you move a feature from the
	 * free version of your extension to its paid version. If you don't have such a distinction you can ignore this.
	 *
	 * @var   array
	 */
	protected $removeFilesFree = [
		'files'   => [
			'administrator/components/com_ats/Controller/Bucket.php',
			'administrator/components/com_ats/Controller/EmailTemplate.php',

			'administrator/components/com_ats/Helper/Mail.php',

			'administrator/components/com_ats/Model/Buckets.php',
			'administrator/components/com_ats/Model/CannedReplies.php',
			'administrator/components/com_ats/Model/CreditConsumptions.php',
			'administrator/components/com_ats/Model/CreditTransactions.php',
			'administrator/components/com_ats/Model/EmailCheck.php',
			'administrator/components/com_ats/Model/EmailTemplates.php',
			'administrator/components/com_ats/Model/OfflineSchedules.php',
			'administrator/components/com_ats/Model/TimeCards.php',

			'components/com_ats/Controller/AssignedTicket.php',
			'components/com_ats/Controller/Bucket.php',
			'components/com_ats/Controller/CannedReply.php',
			'components/com_ats/Model/Buckets.php',
			'components/com_ats/Model/CannedReplies.php',
		],
		'folders' => [
			'administrator/components/com_ats/Helper/Mail',

			'administrator/components/com_ats/View/Buckets',
			'administrator/components/com_ats/View/CannedReplies',
			'administrator/components/com_ats/View/EmailTemplates',

			'administrator/components/com_ats/tmpl/Buckets',
			'administrator/components/com_ats/tmpl/CannedReplies',
			'administrator/components/com_ats/tmpl/CreditTransactions',
			'administrator/components/com_ats/tmpl/EmailTemplates',
			'administrator/components/com_ats/tmpl/OfflineSchedules',
			'administrator/components/com_ats/tmpl/TimeCards',

			'components/com_ats/View/AssignedTickets',
			'components/com_ats/View/Buckets',
			'components/com_ats/View/CannedReplies',

			'components/com_ats/views/AssignedTickets',
			'components/com_ats/views/Buckets',

			'components/com_ats/tmpl/AssignedTickets',
			'components/com_ats/tmpl/Buckets',
			'components/com_ats/tmpl/CannedReplies',
		],
	];

	/**
	 * The list of obsolete extra modules and plugins to uninstall on component upgrade / installation.
	 *
	 * @var array
	 */
	protected $uninstallation_queue = [
		// modules => { (folder) => { (module) }* }*
		'modules' => [
			'admin' => [],
			'site'  => [],
		],
		// plugins => { (folder) => { (element) }* }*
		'plugins' => [
			'ats'    => [
				'akeebasubslegacy',
				'alphauserpoints',
			],
			'system' => [
				'atsreplybyemail',
			],
		],
	];

	public function postflight(string $type, ComponentAdapter $parent): void
	{
		// Remove the update sites for this component on installation. The update sites are now handled at the package
		// level.
		$this->removeObsoleteUpdateSites($parent);

		// If the default attachments folder is not there, let's make it
		if (!JFolder::exists(JPATH_ROOT . '/media/com_ats/attachments'))
		{
			JFolder::create(JPATH_ROOT . '/media/com_ats/attachments');
		}

		// Call the parent method
		parent::postflight($type, $parent);

		// DO NOT REMOVE -- We need it to load the FOF autoloader and register ATS mappings on it.
		$container = Container::getInstance('com_ats');

		// Migrate the component options into the plugins on update
		if (class_exists('\Akeeba\TicketSystem\Admin\Helper\PluginParameters'))
		{
			PluginParameters::migrate();
		}

		// Make sure the attachments folder is protected from direct web access / directory listing
		if (class_exists('\Akeeba\TicketSystem\Admin\Model\Attachments'))
		{
			\Akeeba\TicketSystem\Admin\Model\Attachments::secureAttachmentsDirectory();
		}
	}

	/**
	 * Renders the post-installation message
	 *
	 * @param   \JInstallerAdapterComponent
	 */
	protected function renderPostInstallation(ComponentAdapter $parent): void
	{
		$this->warnAboutJSNPowerAdmin();
		?>

		<h1>Akeeba Ticket System</h1>

		<img src="<?php echo JUri::base() ?>/../media/com_ats/images/ats-48.png" width="48" height="48"
			 alt="Akeeba Ticket System" align="left" />
		<h2 style="font-size: 14pt; font-weight: bold; padding: 0; margin: 0 0 0.5em;">&nbsp;Welcome to Akeeba Ticket
			System!</h2>
		<span>
	The simplest and easiest ticket system for Joomla!&trade;
</span>

		<?php

		$container = Container::getInstance('com_ats');
		/** @var \Akeeba\TicketSystem\Admin\Model\Stats $model */
		try
		{
			$model = $container->factory->model('Stats');

			if (method_exists($model, 'collectStatistics'))
			{
				$iframe = $model->collectStatistics(true);

				if ($iframe)
				{
					echo $iframe;
				}
			}
		}
		catch (\Exception $e)
		{
		}
	}

	protected function renderPostUninstallation(ComponentAdapter $parent): void
	{
		?>
		<h2 style="font-size: 14pt; font-weight: bold; padding: 0; margin: 0 0 0.5em;">&nbsp;Akeeba Ticket System
			Uninstallation</h2>
		<p>We are sorry that you decided to uninstall Akeeba Ticket System. Please let us know why by using the Contact
			Us form on our site. We appreciate your feedback; it helps us develop better software!</p>

		<?php
		parent::renderPostUninstallation($parent);
	}

	/**
	 * Removes obsolete update sites created for the component (we are now using an update site for the package, not the
	 * component).
	 *
	 * @param   JInstallerAdapterComponent  $parent  The parent installer
	 */
	protected function removeObsoleteUpdateSites($parent)
	{
		$db = $parent->getParent()->getDBO();

		$query = $db->getQuery(true)
			->select($db->qn('extension_id'))
			->from($db->qn('#__extensions'))
			->where($db->qn('type') . ' = ' . $db->q('component'))
			->where($db->qn('name') . ' = ' . $db->q($this->componentName));
		$db->setQuery($query);
		$extensionId = $db->loadResult();

		if (!$extensionId)
		{
			return;
		}

		$query = $db->getQuery(true)
			->select($db->qn('update_site_id'))
			->from($db->qn('#__update_sites_extensions'))
			->where($db->qn('extension_id') . ' = ' . $db->q($extensionId));
		$db->setQuery($query);

		$ids = $db->loadColumn(0);

		if (!is_array($ids) && empty($ids))
		{
			return;
		}

		foreach ($ids as $id)
		{
			$query = $db->getQuery(true)
				->delete($db->qn('#__update_sites'))
				->where($db->qn('update_site_id') . ' = ' . $db->q($id));
			$db->setQuery($query);

			try
			{
				$db->execute();
			}
			catch (\Exception $e)
			{
				// Do not fail in this case
			}
		}
	}

	/**
	 * The PowerAdmin extension makes menu items disappear. People assume it's our fault. JSN PowerAdmin authors don't
	 * own up to their software's issue. I have no choice but to warn our users about the faulty third party software.
	 */
	private function warnAboutJSNPowerAdmin()
	{
		$db            = JFactory::getDbo();
		$query         = $db->getQuery(true)
			->select('COUNT(*)')
			->from($db->qn('#__extensions'))
			->where($db->qn('type') . ' = ' . $db->q('component'))
			->where($db->qn('element') . ' = ' . $db->q('com_poweradmin'))
			->where($db->qn('enabled') . ' = ' . $db->q('1'));
		$hasPowerAdmin = $db->setQuery($query)->loadResult();

		if (!$hasPowerAdmin)
		{
			return;
		}

		$query         = $db->getQuery(true)
			->select('manifest_cache')
			->from($db->qn('#__extensions'))
			->where($db->qn('type') . ' = ' . $db->q('component'))
			->where($db->qn('element') . ' = ' . $db->q('com_poweradmin'))
			->where($db->qn('enabled') . ' = ' . $db->q('1'));
		$paramsJson    = $db->setQuery($query)->loadResult();
		$jsnPAManifest = new JRegistry();
		$jsnPAManifest->loadString($paramsJson, 'JSON');
		$version = $jsnPAManifest->get('version', '0.0.0');

		if (version_compare($version, '2.1.2', 'ge'))
		{
			return;
		}

		echo <<<HTML
<div class="well" style="margin: 2em 0;">
<h1 style="font-size: 32pt; line-height: 120%; color: red; margin-bottom: 1em">WARNING: Menu items for {$this->componentTitle} might not be displayed on your site.</h1>
<p style="font-size: 18pt; line-height: 150%; margin-bottom: 1.5em">
	We have detected that you are using JSN PowerAdmin on your site. This software ignores Joomla! standards and
	<b>hides</b> the Component menu items to {$this->componentTitle} in the administrator backend of your site. Unfortunately we
	can't provide support for third party software. Please contact the developers of JSN PowerAdmin for support
	regarding this issue.
</p>
<p style="font-size: 18pt; line-height: 120%; color: green;">
	Tip: You can disable JSN PowerAdmin to see the menu items to {$this->componentTitle}.
</p>
</div>

HTML;

	}

	/** @inheritDoc */
	protected function removeFilesAndFolders(array $removeList): void
	{
		/**
		 * Process case-sensitive files, denoted with <!> as the first 3 characters in the filename.
		 *
		 * This is necessary when we rename a file e.g. from foobar.php to FooBar.php. In this case we have a conundrum.
		 * On case-sensitive filesystems e.g. Linux' ext4 we need to delete the all-lowercase file foobar.php. However,
		 * on case-insensitive filesystems e.g. macOS' APFS or Windows' NTFS we can't do this. The file foobar.php has
		 * been replaced with FooBar.php. Trying to delete foobar.php deletes the new file FooBar.php!
		 *
		 * The solution to that is marking case-sensitive obsolete files with the special mark "<!>" as the first
		 * characters in their name. Then we check below if the filesystem behaves in a case-insensitive manner when we
		 * check for its existence. We do that by checking whether an all uppercase (e.g. FOOBAR.PHP) version of the
		 * file's name exists. We know that we never use all-uppercase filenames and extensions, therefore its existence
		 * is proof of a case-insensitive filesystem. If this is detected we pluck this entry out of the list of files
		 * to delete by setting it to null and filtering out null values below.
		 *
		 * That's how you properly manage updating your legacy code bases without breaking things.
		 */
		$removeList['files'] = array_map(function ($file) {
			// This check only applies on files which are marked as case-sensitive
			if (substr($file, 0, 3) !== '<!>')
			{
				return $file;
			}

			// Get the actual filename, without the case-sensitivity marker.
			$file = substr($file, 3);

			// There's no point processing files that are flat-out missing
			$filePath = JPATH_ROOT . '/' . $file;

			if (!is_file($filePath))
			{
				return null;
			}

			// Check if the filesystem is case-insensitive.
			$altFile  = dirname($filePath) . strtoupper(basename($filePath));

			if (is_file($altFile))
			{
				return null;
			}

			// Return the fixed filename
			return $file;

		}, $removeList['files']);

		// Remove empty entries. Empty entries are created by the array_map code above. See the big comment further up.
		array_filter($removeList['files'], function ($file) {
			return !empty($file);
		});

		parent::removeFilesAndFolders($removeList);
	}


}