[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/installation/src/Controller/ -> JSONController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Installation
   5   * @subpackage  Controller
   6   *
   7   * @copyright   (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\CMS\Installation\Controller;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Installation\Response\JsonResponse;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\BaseController;
  17  use Joomla\CMS\Session\Session;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Default JSON controller class for the Joomla Installer controllers.
  25   *
  26   * @since  4.0.0
  27   */
  28  abstract class JSONController extends BaseController
  29  {
  30      /**
  31       * Method to send a JSON response. The data parameter
  32       * can be an Exception object for when an error has occurred or
  33       * a JsonResponse for a good response.
  34       *
  35       * @param   mixed  $response  JsonResponse on success, Exception on failure.
  36       *
  37       * @return  void
  38       *
  39       * @since   4.0.0
  40       */
  41      protected function sendJsonResponse($response)
  42      {
  43          $this->app->mimeType = 'application/json';
  44  
  45          // Very crude workaround to give an error message when JSON is disabled
  46          if (!function_exists('json_encode') || !function_exists('json_decode')) {
  47              $this->app->setHeader('status', 500);
  48              echo '{"token":"' . Session::getFormToken(true) . '","lang":"' . Factory::getLanguage()->getTag()
  49                  . '","error":true,"header":"' . Text::_('INSTL_HEADER_ERROR') . '","message":"' . Text::_('INSTL_WARNJSON') . '"}';
  50  
  51              return;
  52          }
  53  
  54          // Check if we need to send an error code.
  55          if ($response instanceof \Exception) {
  56              // Send the appropriate error code response.
  57              $this->app->setHeader('status', $response->getCode(), true);
  58          }
  59  
  60          // Send the JSON response.
  61          echo json_encode(new JsonResponse($response));
  62      }
  63  
  64      /**
  65       * Checks for a form token, if it is invalid a JSON response with the error code 403 is sent.
  66       *
  67       * @return  void
  68       *
  69       * @since   4.0.0
  70       * @see     Session::checkToken()
  71       */
  72      public function checkValidToken()
  73      {
  74          // Check for request forgeries.
  75          if (!Session::checkToken()) {
  76              $this->sendJsonResponse(new \Exception(Text::_('JINVALID_TOKEN_NOTICE'), 403));
  77  
  78              $this->app->close();
  79          }
  80      }
  81  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer