/** * Execute the controller. * * @return void Redirects the application * * @since 2.0 */ public function execute() { // We don't want this request to be cached. header('Pragma: no-cache'); header('Cache-Control: no-cache'); header('Expires: -1'); try { // Fetch our page from the session $page = \JFactory::getSession()->get('com_patchtester_fetcher_page', 1); // TODO - Decouple the model and context? $model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo()); // Initialize the state for the model $model->setState($this->initializeState($model)); $status = $model->requestFromGithub($page); } catch (\Exception $e) { $response = new \JResponseJson($e); echo json_encode($response); $this->getApplication()->close(1); } // Update the UI and session now if (isset($status['page'])) { \JFactory::getSession()->set('com_patchtester_fetcher_page', $status['page']); $message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']); unset($status['page']); } else { $status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true); $message = \JText::_('COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW', true); } $response = new \JResponseJson($status, $message, false, true); echo json_encode($response); $this->getApplication()->close(); }
/** * Execute the controller. * * @return void Redirects the application * * @since 2.0 */ public function execute() { // We don't want this request to be cached. $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'); $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); } // TODO - Decouple the model and context? $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(); }
/** * Method to render the view. * * @return string The rendered view. * * @since 2.0 */ public function render() { if (!extension_loaded('openssl')) { $this->envErrors[] = \JText::_('COM_PATCHTESTER_REQUIREMENT_OPENSSL'); } if (!in_array('https', stream_get_wrappers())) { $this->envErrors[] = \JText::_('COM_PATCHTESTER_REQUIREMENT_HTTPS'); } // Only process the data if there are no environment errors if (!count($this->envErrors)) { $this->state = $this->model->getState(); $this->items = $this->model->getItems(); $this->pagination = $this->model->getPagination(); } $this->addToolbar(); return parent::render(); }
/** * Execute the controller. * * @return void Redirects the application * * @since 2.0 */ public function execute() { // We don't want this request to be cached. $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'); $this->getApplication()->setHeader('Content-Type', $this->getApplication()->mimeType . '; charset=' . $this->getApplication()->charSet); $session = \JFactory::getSession(); try { // Fetch our page from the session $page = $session->get('com_patchtester_fetcher_page', 1); $model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo()); // Initialize the state for the model $model->setState($this->initializeState($model)); $status = $model->requestFromGithub($page); } catch (\Exception $e) { $response = new \JResponseJson($e); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(1); } // Store the last page to the session if given one if (isset($status['lastPage']) && $status['lastPage'] !== false) { $session->set('com_patchtester_fetcher_last_page', $status['lastPage']); } // Update the UI and session now if ($status['complete'] || $page === $session->get('com_patchtester_fetcher_last_page', false)) { $status['complete'] = true; $status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true); $message = \JText::_('COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW', true); } elseif (isset($status['page'])) { $session->set('com_patchtester_fetcher_page', $status['page']); if ($session->has('com_patchtester_fetcher_last_page')) { $message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER_OF_TOTAL', $status['page'], $session->get('com_patchtester_fetcher_last_page')); } else { $message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']); } unset($status['page']); } $response = new \JResponseJson($status, $message, false, true); $this->getApplication()->sendHeaders(); echo json_encode($response); $this->getApplication()->close(); }
/** * Execute the controller. * * @return void Redirects the application * * @since 2.0 */ public function execute() { try { $hasErrors = false; $pullModel = new PullModel(null, \JFactory::getDbo()); $pullsModel = new PullsModel($this->context, null, \JFactory::getDbo()); $testsModel = new TestsModel(null, \JFactory::getDbo()); // Check the applied patches in the database first $appliedPatches = $testsModel->getAppliedPatches(); if (count($appliedPatches)) { $revertErrored = false; // Let's try to cleanly revert all applied patches foreach ($appliedPatches as $patch) { try { $pullModel->revert($patch->id); } catch (\RuntimeException $e) { $revertErrored = true; } } // If we errored out reverting patches, we'll need to truncate the table if ($revertErrored) { try { $testsModel->truncateTable(); } catch (\RuntimeException $e) { $hasErrors = true; $this->getApplication()->enqueueMessage(\JText::sprintf('COM_PATCHTESTER_ERROR_TRUNCATING_PULLS_TABLE', $e->getMessage()), 'error'); } } } // Now truncate the pulls table try { $pullsModel->truncateTable(); } catch (\RuntimeException $e) { $hasErrors = true; $this->getApplication()->enqueueMessage(\JText::sprintf('COM_PATCHTESTER_ERROR_TRUNCATING_TESTS_TABLE', $e->getMessage()), 'error'); } jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); // Check the backups directory to see if any .txt files remain; clear them if so $backups = \JFolder::files(JPATH_COMPONENT . '/backups', '.txt'); if (count($backups)) { foreach ($backups as $file) { if (!\JFile::delete(JPATH_COMPONENT . '/backups/' . $file)) { $this->getApplication()->enqueueMessage(\JText::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', JPATH_COMPONENT . '/backups/' . $file), 'error'); $hasErrors = true; } } } // Processing completed, inform the user of a success or fail if ($hasErrors) { $msg = \JText::sprintf('COM_PATCHTESTER_RESET_HAS_ERRORS', JPATH_COMPONENT . '/backups', \JFactory::getDbo()->replacePrefix('#__patchtester_tests')); $type = 'warning'; } else { $msg = \JText::_('COM_PATCHTESTER_RESET_OK'); $type = 'notice'; } } catch (\Exception $e) { $msg = $e->getMessage(); $type = 'error'; } $this->getApplication()->enqueueMessage($msg, $type); $this->getApplication()->redirect(\JRoute::_('index.php?option=com_patchtester', false)); }