Change \MailSo\Base\Utils::Php2js() in improved \RainLoop\Utils::jsonEncode()

This commit is contained in:
the-djmaze 2022-10-15 22:09:42 +02:00
parent f04a0ca7f3
commit 13492a9d0b
7 changed files with 29 additions and 52 deletions

View file

@ -802,45 +802,6 @@ abstract class Utils
return false;
}
private static function ClearArrayUtf8Values(array &$aInput)
{
foreach ($aInput as $mKey => $mItem)
{
if (\is_string($mItem))
{
$aInput[$mKey] = static::Utf8Clear($mItem);
}
else if (\is_array($mItem))
{
static::ClearArrayUtf8Values($mItem);
$aInput[$mKey] = $mItem;
}
}
}
/**
* @param mixed $mInput
*/
public static function Php2js($mInput, ?\MailSo\Log\Logger $oLogger = null) : string
{
$sResult = \json_encode($mInput, JSON_UNESCAPED_UNICODE);
if (!\is_string($sResult) || '' === $sResult) {
\trigger_error(
'json_encode: '.\trim(
(static::FunctionExistsAndEnabled('json_last_error') ? ' [Error Code: '.\json_last_error().']' : '').
(static::FunctionExistsAndEnabled('json_last_error_msg') ? ' [Error Message: '.\json_last_error_msg().']' : '')
), E_USER_WARNING
);
if (\is_array($mInput)) {
// $oLogger->WriteDump($mInput, \LOG_INFO, 'JSON');
// $oLogger->Write('Trying to clear Utf8 before json_encode', \LOG_INFO, 'JSON');
static::ClearArrayUtf8Values($mInput);
$sResult = \json_encode($mInput, JSON_UNESCAPED_UNICODE);
}
}
return $sResult;
}
public static function ClearFileName(string $sFileName) : string
{
return static::Trim(static::ClearNullBite(

View file

@ -1052,7 +1052,7 @@ class Actions
if ($this->StorageProvider()->Put($oAccount,
Providers\Storage\Enumerations\StorageType::CONFIG,
'background',
\json_encode(array(
Utils::jsonEncode(array(
'Name' => $aFile['name'],
'ContentType' => $sMimeType,
'Raw' => \base64_encode($sData)

View file

@ -176,7 +176,7 @@ abstract class Service
if ($oConfig->Get('labs', 'cache_system_data', true))
{
$sCacheFileName = 'TMPL:' . $sLanguage . \md5(
\json_encode(array(
Utils::jsonEncode(array(
$oConfig->Get('cache', 'index', ''),
$oActions->Plugins()->Hash(),
$sAppJsMin,

View file

@ -138,7 +138,7 @@ class ServiceActions
break;
}
*/
$this->Logger()->Write(\MailSo\Base\Utils::Php2js($aPost, $this->Logger()),
$this->Logger()->Write(Utils::jsonEncode($aPost),
\LOG_INFO, 'POST', true);
}
else if (3 < \count($this->aPaths) && $this->oHttp->IsGet())
@ -190,7 +190,7 @@ class ServiceActions
\header('Content-Type: application/json; charset=utf-8');
}
$sResult = \MailSo\Base\Utils::Php2js($aResponseItem, $this->Logger());
$sResult = Utils::jsonEncode($aResponseItem);
$sObResult = \ob_get_clean();
@ -317,7 +317,7 @@ class ServiceActions
\header('Content-Type: application/json; charset=utf-8');
$this->Plugins()->RunHook('filter.upload-response', array(&$aResponseItem));
$sResult = \MailSo\Base\Utils::Php2js($aResponseItem, $this->Logger());
$sResult = Utils::jsonEncode($aResponseItem);
$sObResult = \ob_get_clean();
if (\strlen($sObResult))
@ -593,7 +593,7 @@ class ServiceActions
}
}
return $bJson ? \MailSo\Base\Utils::Php2js(array($sTheme, $sResult), $this->Logger()) : $sResult;
return $bJson ? Utils::jsonEncode(array($sTheme, $sResult)) : $sResult;
}
public function ServiceAppData() : string
@ -804,7 +804,7 @@ class ServiceActions
}
}
return \MailSo\Base\Utils::Php2js($aResult, $this->Logger());
return Utils::jsonEncode($aResult);
}
$this->oActions->Location('./');
@ -838,7 +838,7 @@ class ServiceActions
$this->oHttp->ServerNoCache();
$sResult = 'rl.initData('
.\json_encode($this->oActions->AppData($bAdmin))
.Utils::jsonEncode($this->oActions->AppData($bAdmin))
.');';
$this->Logger()->Write($sResult, \LOG_INFO, 'APPDATA');

View file

@ -23,6 +23,23 @@ class Utils
*/
SESSION_TOKEN = 'smsession';
/**
* @param mixed $value
* @param int $flags Bitmask
*/
public static function jsonEncode($value, int $flags = \JSON_INVALID_UTF8_SUBSTITUTE) : string
{
try {
if (Api::Config()->Get('debug', 'enable', false)) {
$flags |= \JSON_PRETTY_PRINT;
}
return \json_encode($value, $flags | \JSON_UNESCAPED_UNICODE | \JSON_THROW_ON_ERROR);
} catch (\Throwable $e) {
Api::Logger()->WriteException($e, \LOG_ERR, 'JSON');
}
return '';
}
public static function EncodeKeyValuesQ(array $aValues, string $sCustomKey = '') : string
{
return \SnappyMail\Crypt::EncryptUrlSafe(

View file

@ -41,7 +41,7 @@ abstract class Stream
public static function JSON($data)
{
echo \MailSo\Base\Utils::Php2js($data) . "\n";
echo \RainLoop\Utils::jsonEncode($data) . "\n";
// \ob_flush();
\flush();
}

View file

@ -142,8 +142,7 @@ abstract class Repository
}
}
} catch (\Throwable $e) {
$sError = "{$e->getCode()} {$e->getMessage()}";
\RainLoop\Api::Logger()->Write($sError, \LOG_ERR, 'INSTALLER');
\SnappyMail\Log::error('INSTALLER', "{$e->getCode()} {$e->getMessage()}");
}
return $aResult;
}
@ -246,7 +245,7 @@ abstract class Repository
{
empty($_ENV['SNAPPYMAIL_INCLUDE_AS_API']) && \RainLoop\Api::Actions()->IsAdminLoggined();
\RainLoop\Api::Logger()->Write('Start package install: '.$sId.' ('.$sType.')', \LOG_INFO, 'INSTALLER');
\SnappyMail\Log::info('INSTALLER', 'Start package install: '.$sId.' ('.$sType.')');
$sRealFile = '';
@ -281,7 +280,7 @@ abstract class Repository
}
}
} catch (\Throwable $e) {
\RainLoop\Api::Logger()->Write("Install package {$sRealFile} failed: {$e->getMessage()}", \LOG_ERR, 'INSTALLER');
\SnappyMail\Log::error('INSTALLER', "Install package {$sRealFile} failed: {$e->getMessage()}");
throw $e;
} finally {
$sTmp && \unlink($sTmp);