Your IP : 216.73.217.142


Current Path : /var/www/consult-e-syn/public_html/cli/
Upload File :
Current File : /var/www/consult-e-syn/public_html/cli/ats-cron.php

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

use Akeeba\TicketSystem\Admin\Helper\Debug;
use Akeeba\TicketSystem\Site\Model\Cron;
use Akeeba\TicketSystem\Site\Model\Exception\CronCommandMissing;
use Akeeba\TicketSystem\Site\Model\Exception\CronCommandNotFound;
use FOF40\Container\Container;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Menu\AbstractMenu;
use Joomla\CMS\Router\Router;
use Joomla\CMS\Uri\Uri;

// Boilerplate -- START
define('_JEXEC', 1);

foreach ([__DIR__, getcwd()] as $curdir)
{
	if (file_exists($curdir . '/defines.php'))
	{
		define('JPATH_BASE', realpath($curdir . '/..'));
		require_once $curdir . '/defines.php';

		break;
	}

	if (file_exists($curdir . '/../includes/defines.php'))
	{
		define('JPATH_BASE', realpath($curdir . '/..'));
		require_once $curdir . '/../includes/defines.php';

		break;
	}
}

defined('JPATH_LIBRARIES') || die ('This script must be placed in or run from the cli folder of your site.');

require_once JPATH_LIBRARIES . '/fof40/Cli/Application.php';

// Boilerplate -- END

class ATSCron extends FOFApplicationCLI
{
	/**
	 * Returns the application Router object. Necessary for fetching emails.
	 *
	 * @param   string  $name     The name of the application.
	 * @param   array   $options  An optional associative array of configuration settings.
	 *
	 * @return  Router|null  A JRouter object
	 * @since   3.2.0
	 */
	public function getRouter($name = null, array $options = [])
	{
		try
		{
			return Router::getInstance('site', $options);
		}
		catch (Exception $e)
		{
			return null;
		}
	}

	public function getMenu($name = null, $options = [])
	{
		return AbstractMenu::getInstance($name, $options);
	}

	/**
	 * Something strange happened and the Factory wants to enqueue a system message. Let's log it for further inspection
	 *
	 * @param $msg
	 * @param $type
	 */
	public function enqueueMessage($msg, $type)
	{
		parent::enqueueMessage($msg, $type);

		Log::add($msg, Log::NOTICE, 'ats.cron');
	}

	/**
	 * The main entry point of the application
	 *
	 * @return void
	 * @since  3.2.0
	 */
	public function doExecute()
	{
		$this->out('Akeeba Ticket System -- CRON Script');
		$this->out('Copyright 2011-' . gmdate('Y') . ' Akeeba Ltd');
		$this->out(str_repeat('=', 79));

		// Load FOF
		if (!defined('FOF40_INCLUDED') && !@include_once(JPATH_LIBRARIES . '/fof40/include.php'))
		{
			die('This extension requires FOF 4.');
		}

		// Load version defines
		$path = JPATH_ADMINISTRATOR . '/components/com_ats/version.php';

		@include_once $path;

		if (!defined('ATS_PRO'))
		{
			define('ATS_PRO', 0);
		}
		if (!defined('ATS_VERSION'))
		{
			define('ATS_VERSION', 'dev');
		}
		if (!defined('ATS_DATE'))
		{
			define('ATS_DATE', date('Y-m-d'));
		}

		// Work around some misconfigured servers which print out notices
		if (function_exists('error_reporting'))
		{
			$oldLevel = error_reporting(0);
		}

		$container = Container::getInstance('com_ats');

		if (function_exists('error_reporting'))
		{
			error_reporting($oldLevel);
		}

		// Initializes the CLI session handler. Prevents Joomla from crashing e.g. when fetching the current user.
		$container->session->get('foobar');

		// Initialize routing from the CLI
		$container = Container::getInstance('com_ats');
		$siteURL   = $container->params->get('siteurl', 'http://www.example.com');

		$this->initCliRouting($siteURL);

		$debug = $this->input->getBool('debug', Debug::getJoomlaDebug());

		if ($debug)
		{
			$this->set('debug', 1);
		}

		if (!defined('JDEBUG'))
		{
			define('JDEBUG', $debug ? 1 : 0);
		}

		$verbose = $this->input->getBool('verbose', $debug);

		if ($verbose)
		{
			Debug::registerCLIOutputLogger('ats.cron');
			Debug::registerCLIOutputLogger('ats.emails');
		}

		// Get the command to execute
		$command = $this->input->getCmd('command');

		// Perform more rigorous filtering of the command name
		$command = (string) preg_replace('/[^A-Z0-9_]/i', '', $command);
		$command = strtolower($command);

		if ($verbose)
		{
			$this->out(sprintf(
				'Current memory usage : %s',
				$this->memUsage()
			));
			$this->out();
		}

		if (!empty($command) && !$verbose)
		{
			$this->out(sprintf('Executing “%s” CRON task.', $command));
			$this->out('Tip: Use the --verbose command line switch to follow its progress.');
		}

		$startTime = microtime(true);

		// Run the command
		/** @var Cron $model */
		$model = $container->factory->model('Cron')->tmpInstance();

		$model->log('Starting the CRON job from CLI');

		// Allow plugins to load under CLI
		$container->platform->setAllowPluginsInCli(true);

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

		try
		{
			// No time limit for CLI CRON jobs
			$success = $model->run($command, 0);

			$model->log('Exit: CRON execution finished');
		}
		catch (CronCommandMissing $e)
		{
			$model->log('There was no command specified in the command line', Log::ERROR);
			$model->log('Exit: no command');

			$this->out('You did not specify a command in the command line.');
			$this->out('Try adding --command=CRONTaskName where CRONTaskName is the name of the CRON task you want to execute');

			$this->close(1);

			// 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->out(sprintf('The requested command, “%s”, is not implemented', $command));
			$this->out('Did you forget to enable a plugin?');

			$this->close(2);

			// 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());

			if (!$verbose)
			{
				$this->out(sprintf('An error occurred executing command “%s”', $command));
				$this->out($e->getMessage());
				$this->out($e->getFile() . '::' . $e->getLine());
			}

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

				if (!$verbose)
				{
					$this->out($line);
				}
			}

			$model->log('Exit: error executing command');

			$this->close(255);

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

		$this->out();
		$this->out(sprintf(
			'%s -- CRON job finished after approximately %s',
			$success ? 'SUCCESS' : 'FAIL',
			$this->timeAgo($startTime, time(), '', false)
		));

		if ($verbose)
		{
			$this->out();
			$this->out(sprintf(
				'Current memory usage : %s',
				$this->memUsage()
			));
			$this->out(sprintf(
				'Peak memory usage    : %s',
				$this->peakMemUsage()
			));
		}

	}

	/**
	 * Check the client interface by name.
	 *
	 * @param   string  $identifier  String identifier for the application interface
	 *
	 * @return  boolean  True if this application is of the given type client interface.
	 *
	 * @since   3.3.1
	 */
	public function isClient($identifier)
	{
		return $identifier === 'cli';
	}

	/**
	 * Is admin interface?
	 *
	 * @return  boolean  True if this application is administrator.
	 *
	 * @since   3.3.1
	 */
	public function isAdmin()
	{
		return $this->isClient('administrator');
	}

	/**
	 * Is site interface?
	 *
	 * @return  boolean  True if this application is site.
	 *
	 * @since   3.3.1
	 */
	public function isSite()
	{
		return $this->isClient('site');
	}

	protected function initCliRouting($siteURL)
	{
		// Set up the base site URL in JUri
		$uri                    = Uri::getInstance($siteURL);
		$_SERVER['HTTP_HOST']   = $uri->toString(['host', 'port']);
		$_SERVER['REQUEST_URI'] = $uri->getPath();

		$refClass     = new ReflectionClass(Uri::class);
		$refInstances = $refClass->getProperty('instances');
		$refInstances->setAccessible(true);
		$instances           = $refInstances->getValue();
		$instances['SERVER'] = $uri;
		$refInstances->setValue($instances);

		$base = [
			'prefix' => $uri->toString(['scheme', 'host', 'port']),
			'path'   => rtrim($uri->toString(['path']), '/\\'),
		];

		$refBase = $refClass->getProperty('base');
		$refBase->setAccessible(true);
		$refBase->setValue($base);

		// Set up the SEF mode in the router
		if (version_compare(JVERSION, '3.999.999', 'le'))
		{
			$this->getRouter()->setMode($this->get('sef', 0));
		}
	}
}

if (!defined('AKEEBA_ATS_CRON_CALLED_FROM_LEGACY'))
{
	FOFApplicationCLI::getInstance('ATSCron')->execute();
}