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; 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 public static function ClearFileName(string $sFileName) : string
{ {
return static::Trim(static::ClearNullBite( return static::Trim(static::ClearNullBite(

View file

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

View file

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

View file

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

View file

@ -23,6 +23,23 @@ class Utils
*/ */
SESSION_TOKEN = 'smsession'; 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 public static function EncodeKeyValuesQ(array $aValues, string $sCustomKey = '') : string
{ {
return \SnappyMail\Crypt::EncryptUrlSafe( return \SnappyMail\Crypt::EncryptUrlSafe(

View file

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

View file

@ -142,8 +142,7 @@ abstract class Repository
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
$sError = "{$e->getCode()} {$e->getMessage()}"; \SnappyMail\Log::error('INSTALLER', "{$e->getCode()} {$e->getMessage()}");
\RainLoop\Api::Logger()->Write($sError, \LOG_ERR, 'INSTALLER');
} }
return $aResult; return $aResult;
} }
@ -246,7 +245,7 @@ abstract class Repository
{ {
empty($_ENV['SNAPPYMAIL_INCLUDE_AS_API']) && \RainLoop\Api::Actions()->IsAdminLoggined(); 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 = ''; $sRealFile = '';
@ -281,7 +280,7 @@ abstract class Repository
} }
} }
} catch (\Throwable $e) { } 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; throw $e;
} finally { } finally {
$sTmp && \unlink($sTmp); $sTmp && \unlink($sTmp);