2015-02-23 01:49:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Patch testing component for the Joomla! CMS
|
|
|
|
*
|
2017-01-21 19:54:53 +00:00
|
|
|
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
|
2015-02-23 01:49:59 +00:00
|
|
|
* @license GNU General Public License version 2 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PatchTester\Controller;
|
|
|
|
|
2017-08-17 23:22:01 +00:00
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
use Joomla\CMS\Response\JsonResponse;
|
|
|
|
use Joomla\CMS\Session\Session;
|
2015-02-23 01:49:59 +00:00
|
|
|
use PatchTester\Helper;
|
2016-03-26 22:08:01 +00:00
|
|
|
use PatchTester\Model\TestsModel;
|
2015-02-23 01:49:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller class to start fetching remote data
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2016-02-20 16:34:23 +00:00
|
|
|
class StartfetchController extends AbstractController
|
2015-02-23 01:49:59 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Execute the controller.
|
|
|
|
*
|
|
|
|
* @return void Redirects the application
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
// We don't want this request to be cached.
|
2015-12-05 18:08:38 +00:00
|
|
|
$this->getApplication()->setHeader('Expires', 'Mon, 1 Jan 2001 00:00:00 GMT', true);
|
|
|
|
$this->getApplication()->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT', true);
|
|
|
|
$this->getApplication()->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false);
|
|
|
|
$this->getApplication()->setHeader('Pragma', 'no-cache');
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->setHeader('Content-Type', $this->getApplication()->mimeType . '; charset=' . $this->getApplication()->charSet);
|
2015-02-23 01:49:59 +00:00
|
|
|
|
|
|
|
// Check for a valid token. If invalid, send a 403 with the error message.
|
2017-08-17 23:22:01 +00:00
|
|
|
if (!Session::checkToken('request'))
|
2015-02-23 01:49:59 +00:00
|
|
|
{
|
2017-08-17 23:22:01 +00:00
|
|
|
$response = new JsonResponse(new \Exception(\JText::_('JINVALID_TOKEN'), 403));
|
2015-02-23 01:49:59 +00:00
|
|
|
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->sendHeaders();
|
2015-02-23 01:49:59 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
|
|
|
|
$this->getApplication()->close(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can fetch the data from GitHub - throw an error on < 10 available requests
|
2015-05-09 16:58:58 +00:00
|
|
|
try
|
|
|
|
{
|
2016-06-25 16:34:48 +00:00
|
|
|
$rateResponse = Helper::initializeGithub()->getRateLimit();
|
|
|
|
$rate = json_decode($rateResponse->body);
|
2015-05-09 16:58:58 +00:00
|
|
|
}
|
2017-04-22 17:00:02 +00:00
|
|
|
catch (\Exception $e)
|
2015-05-09 16:58:58 +00:00
|
|
|
{
|
2017-08-17 23:22:01 +00:00
|
|
|
$response = new JsonResponse(
|
2015-05-09 16:58:58 +00:00
|
|
|
new \Exception(
|
2015-06-16 19:14:41 +00:00
|
|
|
\JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()),
|
|
|
|
$e->getCode(),
|
|
|
|
$e
|
2015-05-09 16:58:58 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->sendHeaders();
|
2015-05-09 16:58:58 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
|
|
|
|
$this->getApplication()->close(1);
|
|
|
|
}
|
2015-02-23 01:49:59 +00:00
|
|
|
|
|
|
|
// If over the API limit, we can't build this list
|
|
|
|
if ($rate->resources->core->remaining < 10)
|
|
|
|
{
|
2017-08-17 23:22:01 +00:00
|
|
|
$response = new JsonResponse(
|
2015-02-23 01:49:59 +00:00
|
|
|
new \Exception(
|
2017-08-17 23:22:01 +00:00
|
|
|
\JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', Factory::getDate($rate->resources->core->reset)),
|
2015-02-23 01:49:59 +00:00
|
|
|
429
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->sendHeaders();
|
2015-02-23 01:49:59 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
|
|
|
|
$this->getApplication()->close(1);
|
|
|
|
}
|
|
|
|
|
2017-08-17 23:22:01 +00:00
|
|
|
$testsModel = new TestsModel(null, Factory::getDbo());
|
2015-02-23 01:49:59 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Sanity check, ensure there aren't any applied patches
|
2016-03-26 22:08:01 +00:00
|
|
|
if (count($testsModel->getAppliedPatches()) >= 1)
|
2015-02-23 01:49:59 +00:00
|
|
|
{
|
2017-08-17 23:22:01 +00:00
|
|
|
$response = new JsonResponse(new \Exception(\JText::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES'), 500));
|
2015-02-23 01:49:59 +00:00
|
|
|
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->sendHeaders();
|
2015-02-23 01:49:59 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
|
|
|
|
$this->getApplication()->close(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2017-08-17 23:22:01 +00:00
|
|
|
$response = new JsonResponse($e);
|
2015-02-23 01:49:59 +00:00
|
|
|
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->sendHeaders();
|
2015-02-23 01:49:59 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
|
|
|
|
$this->getApplication()->close(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're able to successfully pull data, prepare our environment
|
2017-08-17 23:22:01 +00:00
|
|
|
Factory::getSession()->set('com_patchtester_fetcher_page', 1);
|
2015-02-23 01:49:59 +00:00
|
|
|
|
2017-08-17 23:22:01 +00:00
|
|
|
$response = new JsonResponse(
|
2015-02-23 01:49:59 +00:00
|
|
|
array('complete' => false, 'header' => \JText::_('COM_PATCHTESTER_FETCH_PROCESSING', true)),
|
|
|
|
\JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', 1),
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2015-12-05 17:43:54 +00:00
|
|
|
$this->getApplication()->sendHeaders();
|
2015-02-23 01:49:59 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
|
|
|
|
$this->getApplication()->close();
|
|
|
|
}
|
|
|
|
}
|