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'); $this->getApplication()->setHeader('Content-Type', $this->getApplication()->mimeType . '; charset=' . $this->getApplication()->charSet); // Check for a valid token. If invalid, send a 403 with the error message. if (!\JSession::checkToken('request')) { $response = new \JResponseJson(new \Exception(\JText::_('JINVALID_TOKEN'), 403)); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(1); } // Make sure we can fetch the data from GitHub - throw an error on < 10 available requests $github = Helper::initializeGithub(); try { $rate = $github->authorization->getRateLimit(); } catch (\Exception $e) { $response = new \JResponseJson( new \Exception( \JText::sprintf('COM_PATCHTESTER_COULD_NOT_CONNECT_TO_GITHUB', $e->getMessage()), $e->getCode(), $e ) ); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(1); } // If over the API limit, we can't build this list if ($rate->resources->core->remaining < 10) { $response = new \JResponseJson( new \Exception( \JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset)), 429 ) ); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(1); } $model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo()); // Initialize the state for the model $model->setState($this->initializeState($model)); try { // Sanity check, ensure there aren't any applied patches if (count($model->getAppliedPatches()) >= 1) { $response = new \JResponseJson(new \Exception(\JText::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES'), 500)); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(1); } } catch (\Exception $e) { $response = new \JResponseJson($e); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(1); } // We're able to successfully pull data, prepare our environment \JFactory::getSession()->set('com_patchtester_fetcher_page', 1); $response = new \JResponseJson( array('complete' => false, 'header' => \JText::_('COM_PATCHTESTER_FETCH_PROCESSING', true)), \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', 1), false, true ); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(); } }