N°6204 - Fix REST/JSON API crash when using JSON-P and iBackofficeDictXXX interfaces

This commit is contained in:
Molkobain
2023-04-18 15:32:24 +02:00
parent d46d1db8e4
commit 51617113eb
12 changed files with 170 additions and 92 deletions

View File

@@ -0,0 +1,73 @@
<?php
/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
/**
* Class JsonPPage
* Handles JSON-P calls {@link https://en.wikipedia.org/wiki/JSONP}
*
* @author Stephen Abello <stephen.abello@combodo.com>
* @since 3.1.0
*/
class JsonPPage extends JsonPage
{
/** @var string Name of the callback to call on response */
protected $sCallbackName;
/**
* JsonPPage constructor.
*
* @param string $sCallbackName
*
* @throws \CoreException
*/
public function __construct(string $sCallbackName)
{
$oKpi = new ExecutionKPI();
parent::__construct();
$this->sContentType = 'application/javascript';
$this->SetCallbackName($sCallbackName);
$oKpi->ComputeStats(get_class($this).' creation', 'JsonPPage');
}
/**
* @param string $sCallbackName
*
* @return $this
* @throws \CoreException
*@see \JsonPPage::$sCallbackName
*
*/
public function SetCallbackName(string $sCallbackName)
{
if (utils::IsNullOrEmptyString($sCallbackName)) {
throw new CoreException('JsonPPage callback cannot be empty');
}
$this->sCallbackName = $sCallbackName;
return $this;
}
/**
* @return string
*@see \JsonPPage::$sCallbackName
*/
public function GetCallbackName(): string
{
return $this->sCallbackName;
}
/**
* @inheritDoc
*/
protected function ComputeContent(): string
{
$sContent = parent::ComputeContent();
return $this->sCallbackName . '(' . $sContent . ');';
}
}

View File

@@ -29,6 +29,7 @@ class JsonPage extends WebPage
{
$oKpi = new ExecutionKPI();
parent::__construct('');
$this->sContentType = 'application/json';
$oKpi->ComputeStats(get_class($this).' creation', 'JsonPage');
}
@@ -78,29 +79,48 @@ class JsonPage extends WebPage
}
/**
* @inheritDoc
* Output the headers
*
* @return void
* @since 3.1.0
*/
public function output()
protected function OutputHeaders(): void
{
$oKpi = new ExecutionKPI();
$this->add_header('Content-type: application/json');
$this->add_header('Content-type: ' . $this->sContentType);
foreach ($this->a_headers as $s_header) {
header($s_header);
}
}
/**
* @return string Content to output
* @since 3.1.0
*/
protected function ComputeContent(): string
{
$aScripts = array_merge($this->a_init_scripts, $this->a_scripts, $this->a_ready_scripts);
$aJson = $this->bOutputDataOnly ? $this->aData : [
'data' => $this->aData,
'scripts' => $aScripts,
];
$sJSON = json_encode($aJson);
$oKpi->ComputeAndReport(get_class($this).' output');
echo $sJSON;
$oKpi->ComputeAndReport('Echoing ('.round(strlen($sJSON) / 1024).' Kb)');
ExecutionKPI::ReportStats();
return json_encode($aJson);
}
/**
* @inheritDoc
*/
public function output()
{
$oKpi = new ExecutionKPI();
$this->OutputHeaders();
$sContent = $this->ComputeContent();
$oKpi->ComputeAndReport(get_class($this).' output');
echo $sContent;
$oKpi->ComputeAndReport('Echoing ('.round(strlen($sContent) / 1024).' Kb)');
ExecutionKPI::ReportStats();
}
}