- md5
- 0f24e15c40275c041f37deb0b5e326b6
- sha1
- 0436894a2016c6d72b7e7a44a9bc6f37f6a2e4eb
- sha256
- 1d6d70caa4845e9bb7307b7322f502201582bbee258dd20949872a2b9d8254fa
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
<?php
/**
* @package Joomla.Installation
* @subpackage Controller
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Installation\Controller;
use Joomla\CMS\Factory;
use Joomla\CMS\Installation\Response\JsonResponse;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Session\Session;
/**
* Default JSON controller class for the Joomla Installer controllers.
*
* @since 4.0.0
*/
abstract class JSONController extends BaseController
{
/**
* Method to send a JSON response. The data parameter
* can be an Exception object for when an error has occurred or
* a JsonResponse for a good response.
*
* @param mixed $response JsonResponse on success, Exception on failure.
*
* @return void
*
* @since 4.0.0
*/
protected function sendJsonResponse($response)
{
$this->app->mimeType = 'application/json';
// Very crude workaround to give an error message when JSON is disabled
if (!function_exists('json_encode') || !function_exists('json_decode')) {
$this->app->setHeader('status', 500);
echo '{"token":"' . Session::getFormToken(true) . '","lang":"' . Factory::getLanguage()->getTag()
. '","error":true,"header":"' . Text::_('INSTL_HEADER_ERROR') . '","message":"' . Text::_('INSTL_WARNJSON') . '"}';
return;
}
// Check if we need to send an error code.
if ($response instanceof \Exception) {
// Send the appropriate error code response.
$this->app->setHeader('status', $response->getCode(), true);
}
// Send the JSON response.
echo json_encode(new JsonResponse($response));
}
/**
* Checks for a form token, if it is invalid a JSON response with the error code 403 is sent.
*
* @return void
*
* @since 4.0.0
* @see Session::checkToken()
*/
public function checkValidToken()
{
// Check for request forgeries.
if (!Session::checkToken()) {
$this->sendJsonResponse(new \Exception(Text::_('JINVALID_TOKEN_NOTICE'), 403));
$this->app->close();
}
}
}
The same content is in 5 releases, Joomla 4.2.0-beta3 to 4.2.1-rc2. See them all, or every version of this file.