<?php
/**
 * @package     Reporter for VirtueMart
 * 
 * @copyright   Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Abhishek Das <info@virtueplanet.com> - https://www.virtueplanet.com
 */
 
defined('_JEXEC') or die;

use Joomla\Registry\Registry;

class VMReporterController extends JControllerLegacy
{
	/**
	 * Typical view method for MVC based architecture
	 *
	 * This function is provide as a default implementation, in most cases
	 * you will need to override it in your own controllers.
	 *
	 * @param   boolean  $cachable   If true, the view output will be cached
	 * @param   array    $urlparams  An array of safe URL parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
	 *
	 * @return  JControllerLegacy  A JControllerLegacy object to support chaining.
	 */
	public function display($cachable = false, $urlparams = array())
	{
		$time_start = microtime(true);
		
		JFactory::getLanguage()->load('com_vmreporter', JPATH_ADMINISTRATOR);
		
		$model      = $this->getModel('Cronjob');
		$message    = 'Cron job successful';
		
		if(!$model->run())
		{
			$message = 'Cron job failed. Check log to learn more.';
		}
		
		echo '<h1>' . $message . '</h1>';
		
		$execution_time = (microtime(true) - $time_start);
		
		if($execution_time < 1)
		{
			$execution_time = number_format(($execution_time * 1000), 2, '.', ',') . ' ms';
		}
		else
		{
			$execution_time = number_format($execution_time, 6, '.', ',') . ' s';
		}
		
		echo '<p><i>Execution Time: ' . $execution_time . '</i></p>';
		
		JFactory::getApplication()->close();
	}
	
	/**
	* Task to initiate the cron job
	* 
	* @return void
	*/
	public function cronjob()
	{
		$options = new Registry(array('follow_location' => true));
		$adapter = JHttpFactory::getAvailableDriver($options);
		
		if($adapter)
		{
			$url  = JUri::getInstance()->toString(array('scheme', 'user', 'pass', 'host', 'port'));
			$url .= JUri::root(true) . '/';
			$url .= 'index.php?option=com_vmreporter';
			
			$post_uri  = JUri::getInstance($url);
			$post      = http_build_query(array('task' => 'display'), '', '&');
			$headers   = null;
			$userAgent = 'VirtueMart Reporter - Cron Job';
			$response  = $adapter->request('POST', $post_uri, $post, $headers, 30, $userAgent);
		}
		
		header('Content-Type: image/gif');
		header('Content-Length: 43');
		header('Cache-Control: public,max-age=1,must-revalidate');
		header('Expires: ' . gmdate('D, d M Y H:i:s', ($_SERVER['REQUEST_TIME'] + 1)) . ' GMT');
		header('Last-modified: ' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT');

		if(function_exists('header_remove')) 
		{
			header_remove('Pragma');
		}

		// 1 x 1 gif
		echo chr(0x47).chr(0x49).chr(0x46).chr(0x38).chr(0x39).chr(0x61).chr(0x01).chr(0x00).
		     chr(0x01).chr(0x00).chr(0x80).chr(0x00).chr(0x00).chr(0x04).chr(0x02).chr(0x04).
		     chr(0x00).chr(0x00).chr(0x00).chr(0x21).chr(0xF9).chr(0x04).chr(0x01).chr(0x00).
		     chr(0x00).chr(0x00).chr(0x00).chr(0x2C).chr(0x00).chr(0x00).chr(0x00).chr(0x00).
		     chr(0x01).chr(0x00).chr(0x01).chr(0x00).chr(0x00).chr(0x02).chr(0x02).chr(0x44).
		     chr(0x01).chr(0x00).chr(0x3B);
		
		JFactory::getApplication()->close();
	}
}