Your IP : 216.73.217.142


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

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

namespace Akeeba\TicketSystem\Site\Controller;

defined('_JEXEC') or die;

use Akeeba\TicketSystem\Site\Model\Cron as CronModel;
use Akeeba\TicketSystem\Site\Model\Exception\CronCommandMissing;
use Akeeba\TicketSystem\Site\Model\Exception\CronCommandNotFound;
use Akeeba\TicketSystem\Site\Model\Exception\SecretMismatch;
use Akeeba\TicketSystem\Site\Model\Exception\SecretNotConfigured;
use Exception;
use FOF40\Container\Container;
use FOF40\Controller\Controller;
use FOF40\Controller\Mixin\PredefinedTaskList;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;

/**
 * Frontend CRON job helper Controller
 *
 * @since  3.2.0
 */
class Cron extends Controller
{
	use PredefinedTaskList;

	/**
	 * @inheritDoc
	 */
	public function __construct(Container $container, array $config = [])
	{
		parent::__construct($container, $config);

		$this->predefinedTaskList = ['cron'];
		$this->cacheableTasks     = [];
	}

	/**
	 * Main and only entry point: executes a CRON command
	 *
	 * This is meant to be accessed as:
	 * index.php?option=com_ats&view=Cron&key=SECRET_KEY&command=CRON_COMMAND
	 *
	 * @return  void  This never returns; it's an immediate exit point.
	 *
	 * @since   3.2.0
	 */
	public function cron()
	{
		/** @var CronModel $model */
		$model = $this->getModel();
		$model->log('Starting the CRON job from URL');

		// Get a reference to the application or die trying
		try
		{
			/** @var CMSApplication $app */
			$app = Factory::getApplication();
		}
		catch (Exception $e)
		{
			$model->log($e->getMessage(), Log::ERROR);
			$model->log('Exit: Cannot instantiate application');
			$this->bellyUp('Internal Server Error', 500);

			// Technically unnecessary but helps with static code analysis :)
			return;
		}

		// Makes sure SiteGround's SuperCache doesn't cache the CRON view
		if (method_exists($app, 'setHeader'))
		{
			$app->setHeader('X-Cache-Control', 'False', true);
		}

		// Check the secret key
		$key = $this->input->get->get('key', '', 'raw');

		$model->log('Checking secret key');

		try
		{
			$model->checkSecret($key);
		}
		catch (SecretMismatch $e)
		{
			$model->log('Provided secret key does not match configuration', Log::ERROR);
			$model->log('Exit: secret key mismatch');
			$this->bellyUp('Forbidden', $e->getCode());

			// Technically unnecessary but helps with static code analysis :)
			return;
		}
		catch (SecretNotConfigured $e)
		{
			$model->log('A secret key has not been configured yet', Log::ERROR);
			$model->log('Exit: secret key not yet configured');
			$this->bellyUp('Service Unavailable', $e->getCode());

			// Technically unnecessary but helps with static code analysis :)
			return;
		}

		$command = $this->input->get->getCmd('command', '');

		// You might ask for a 'cmd' filter but get an array instead
		if (!is_string($command))
		{
			$model->log('The command specified in the URL has an invalid format', Log::ERROR);
			$model->log('Exit: invalid command format');
			$this->bellyUp('Bad Request', 400);

			// Technically unnecessary but helps with static code analysis :)
			return;
		}

		$model->log('Executing the command');

		// Run the command
		try
		{
			$timeLimit = (int) $this->container->params->get('time_limit', 10);
			$success   = $model->run($command, $timeLimit);
		}
		catch (CronCommandMissing $e)
		{
			$model->log('There was no command specified in the URL', Log::ERROR);
			$model->log('Exit: no command');
			$this->bellyUp('Bad Request', 400);

			// Technically unnecessary but helps with static code analysis :)
			return;
		}
		catch (CronCommandNotFound $e)
		{
			$model->log(sprintf('The requested command, “%s”, is not implemented', $command), Log::ERROR);
			$model->log('Exit: unknown command');
			$this->bellyUp('Not Implemented', 501);

			// Technically unnecessary but helps with static code analysis :)
			return;
		}
		catch (Exception $e)
		{
			$model->log(sprintf('An error occurred executing command “%s”', $command), Log::ERROR);
			$model->log($e->getMessage());
			$model->log($e->getFile() . '::' . $e->getLine());

			foreach (explode("\n", $e->getTraceAsString()) as $line)
			{
				$model->log($line);
			}

			$model->log('Exit: error executing command');
			$this->bellyUp('Internal Server Error', 500);

			// Technically unnecessary but helps with static code analysis :)
			return;
		}

		if ($success)
		{
			$model->log('Exit: COMMAND SUCCEEDED');

			echo "OK";

			$this->container->platform->closeApplication();

			// Technically unnecessary but helps with static code analysis :)
			return;
		}

		$model->log('Exit: COMMAND FAILED');

		$this->bellyUp('Command failed', 500);
	}

	/**
	 * Exit the script with an error
	 *
	 * @param   string  $reason    The reason we're quitting. Will be included in the HTTP header.
	 * @param   int     $httpCode  HTTP status code, default 500
	 *
	 * @return  void  We actually never return; this is an exit point.
	 * @since   3.2.0
	 */
	private function bellyUp($reason, $httpCode = 500)
	{
		header(sprintf('HTTP/1.1 %u %s', $httpCode, $reason));

		try
		{
			/** @var CMSApplication $app */
			$app = Factory::getApplication();
			$app->close($httpCode);
		}
		catch (Exception $e)
		{
			exit ($httpCode);
		}
	}
}