mirror of https://github.com/MISP/MISP
chg: [internal] PHP 7.4 is required, so we can remove hacks for older versions
parent
a698b4a26e
commit
4303e488b8
|
@ -9,10 +9,7 @@ class JsonTool
|
|||
*/
|
||||
public static function encode($value, $prettyPrint = false)
|
||||
{
|
||||
$flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
|
||||
if (defined('JSON_THROW_ON_ERROR')) {
|
||||
$flags |= JSON_THROW_ON_ERROR; // Throw exception on error if supported
|
||||
}
|
||||
$flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
||||
if ($prettyPrint) {
|
||||
$flags |= JSON_PRETTY_PRINT;
|
||||
}
|
||||
|
@ -34,16 +31,8 @@ class JsonTool
|
|||
} catch (SimdJsonException $e) {
|
||||
throw new JsonException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
} elseif (defined('JSON_THROW_ON_ERROR')) {
|
||||
// JSON_THROW_ON_ERROR is supported since PHP 7.3
|
||||
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
||||
} else {
|
||||
$decoded = json_decode($value, true);
|
||||
if ($decoded === null) {
|
||||
throw new UnexpectedValueException('Could not parse JSON: ' . json_last_error_msg(), json_last_error());
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,4 +67,18 @@ class JsonTool
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON supports just unicode strings. This helper method converts non unicode chars to Unicode Replacement Character U+FFFD (UTF-8)
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public static function escapeNonUnicode($string)
|
||||
{
|
||||
if (json_encode($string, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS) !== false) {
|
||||
return $string; // string is valid unicode
|
||||
}
|
||||
|
||||
return htmlspecialchars_decode(htmlspecialchars($string, ENT_SUBSTITUTE, 'UTF-8'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,14 +8,14 @@ class ProcessException extends Exception
|
|||
private $stdout;
|
||||
|
||||
/**
|
||||
* @param string|array $command
|
||||
* @param array $command
|
||||
* @param int $returnCode
|
||||
* @param string $stderr
|
||||
* @param string $stdout
|
||||
*/
|
||||
public function __construct($command, $returnCode, $stderr, $stdout)
|
||||
public function __construct(array $command, $returnCode, $stderr, $stdout)
|
||||
{
|
||||
$commandForException = is_array($command) ? implode(' ', $command) : $command;
|
||||
$commandForException = implode(' ', $command);
|
||||
$message = "Command '$commandForException' finished with error code $returnCode.\nSTDERR: '$stderr'\nSTDOUT: '$stdout'";
|
||||
$this->stderr = $stderr;
|
||||
$this->stdout = $stdout;
|
||||
|
@ -56,11 +56,6 @@ class ProcessTool
|
|||
self::logMessage('Running command ' . implode(' ', $command));
|
||||
}
|
||||
|
||||
// PHP older than 7.4 do not support proc_open with array, so we need to convert values to string manually
|
||||
if (PHP_VERSION_ID < 70400) {
|
||||
$command = array_map('escapeshellarg', $command);
|
||||
$command = implode(' ', $command);
|
||||
}
|
||||
$process = proc_open($command, $descriptorSpec, $pipes, $cwd);
|
||||
if (!$process) {
|
||||
$commandForException = self::commandFormat($command);
|
||||
|
@ -136,8 +131,8 @@ class ProcessTool
|
|||
* @param array|string $command
|
||||
* @return string
|
||||
*/
|
||||
private static function commandFormat($command)
|
||||
private static function commandFormat(array $command)
|
||||
{
|
||||
return is_array($command) ? implode(' ', $command) : $command;
|
||||
return implode(' ', $command);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -202,7 +202,7 @@ class AccessLog extends AppModel
|
|||
|
||||
if ($includeSqlQueries && !empty($sqlLog['log'])) {
|
||||
foreach ($sqlLog['log'] as &$log) {
|
||||
$log['query'] = $this->escapeNonUnicode($log['query']);
|
||||
$log['query'] = JsonTool::escapeNonUnicode($log['query']);
|
||||
unset($log['affected']); // affected is the same as numRows
|
||||
unset($log['params']); // no need to save for your use case
|
||||
}
|
||||
|
@ -310,36 +310,4 @@ class AccessLog extends AppModel
|
|||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $string
|
||||
* @return string
|
||||
*/
|
||||
private function escapeNonUnicode($string)
|
||||
{
|
||||
if (json_encode($string, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS) !== false) {
|
||||
return $string; // string is valid unicode
|
||||
}
|
||||
|
||||
if (function_exists('mb_str_split')) {
|
||||
$result = mb_str_split($string);
|
||||
} else {
|
||||
$result = [];
|
||||
$length = mb_strlen($string);
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$result[] = mb_substr($string, $i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
$string = '';
|
||||
foreach ($result as $char) {
|
||||
if (strlen($char) === 1 && !preg_match('/[[:print:]]/', $char)) {
|
||||
$string .= '\x' . bin2hex($char);
|
||||
} else {
|
||||
$string .= $char;
|
||||
}
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/../Lib/Tools/JsonTool.php';
|
||||
require_once __DIR__ . '/../Lib/Tools/JSONConverterTool.php';
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
@ -57,11 +58,6 @@ class JSONConverterToolTest extends TestCase
|
|||
$jsonNormalWithoutSpaces = preg_replace("/\s+/", "", JSONConverterTool::convert($event));
|
||||
$this->assertEquals($jsonNormalWithoutSpaces, $jsonStreamWithoutSpaces);
|
||||
|
||||
if (defined('JSON_THROW_ON_ERROR')) {
|
||||
json_decode($json, true, 512, JSON_THROW_ON_ERROR);
|
||||
$this->assertTrue(true);
|
||||
} else {
|
||||
$this->assertNotNull(json_decode($json));
|
||||
}
|
||||
$this->assertTrue(JsonTool::isValid($json));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue