8
0
ModCfg/Application/Model/Encoding/d3decoder.php

204 Zeilen
5.0 KiB
PHP

2024-04-19 16:15:46 +02:00
<?php
/**
2024-09-02 08:48:43 +02:00
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
2024-04-19 16:15:46 +02:00
*
2024-09-02 08:48:43 +02:00
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
2024-04-19 16:15:46 +02:00
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2024-09-02 08:48:43 +02:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
2024-04-19 16:15:46 +02:00
*/
namespace D3\ModCfg\Application\Model\Encoding;
2024-06-26 13:49:30 +02:00
use JsonException;
2024-04-19 16:15:46 +02:00
class d3decoder
{
public const DEC_DEFAULT = 'default';
public const DEC_JSON = 'json';
public const DEC_UTF8 = 'utf-8';
public const DEC_GZIP = 'gzip';
public const DEC_SERIALIZE = 'serialize';
public const DEC_RAWURL = 'rawurl';
public const DEC_URL = 'url';
public const DEC_BASE64 = 'base64';
public const DEC_UUENC = 'uuencode';
public const DEC_QUOTED = 'quoted';
/**
* @param string $_sDecodingType
*/
2024-06-26 13:49:30 +02:00
public function __construct(protected string $_sDecodingType = self::DEC_DEFAULT)
2024-04-19 16:15:46 +02:00
{
}
/**
* @return string
*/
2024-06-26 13:49:30 +02:00
public function getDecodingType(): string
2024-04-19 16:15:46 +02:00
{
return $this->_sDecodingType;
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
* @throws JsonException
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decode(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return match (strtolower($this->getDecodingType())) {
self::DEC_JSON => $this->decodeJson($encodedValue),
self::DEC_UTF8 => $this->decodeUtf8($encodedValue),
self::DEC_GZIP => $this->decodeGZip($encodedValue),
self::DEC_SERIALIZE => $this->decodeSerialize($encodedValue),
self::DEC_RAWURL => $this->decodeRawUrl($encodedValue),
self::DEC_URL => $this->decodeUrl($encodedValue),
self::DEC_BASE64 => $this->decodeBase64($encodedValue),
self::DEC_UUENC => $this->decodeUUEncode($encodedValue),
self::DEC_QUOTED => $this->decodeQuotedPrintable($encodedValue),
default => $this->decodeDefault($encodedValue)
};
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeDefault(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
rawurldecode(
base64_decode($encodedValue ?? '')
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
* @throws JsonException
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeJson(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return json_decode(
html_entity_decode($encodedValue ?? '', ENT_QUOTES),
null,
512,
JSON_THROW_ON_ERROR
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeUtf8(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
utf8_decode(
html_entity_decode($encodedValue ?? '', ENT_QUOTES)
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeSerialize(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
html_entity_decode($encodedValue ?? '', ENT_QUOTES)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeUrl(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
urldecode($encodedValue ?? '')
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeRawUrl(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
rawurldecode($encodedValue ?? '')
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
* @return string
*/
2024-06-26 13:49:30 +02:00
public function decodeBase64(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
base64_decode($encodedValue ?? '')
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeGZip(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
gzdecode(
html_entity_decode($encodedValue ?? '', ENT_QUOTES)
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeUUEncode(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
convert_uudecode(
html_entity_decode($encodedValue ?? '', ENT_QUOTES)
)
);
2024-04-19 16:15:46 +02:00
}
/**
2024-06-26 13:49:30 +02:00
* @param string|null $encodedValue
2024-04-19 16:15:46 +02:00
*
2024-06-26 13:49:30 +02:00
* @return mixed
2024-04-19 16:15:46 +02:00
*/
2024-06-26 13:49:30 +02:00
public function decodeQuotedPrintable(?string $encodedValue): mixed
2024-04-19 16:15:46 +02:00
{
2024-06-26 13:49:30 +02:00
return unserialize(
quoted_printable_decode(
html_entity_decode($encodedValue ?? '', ENT_QUOTES)
)
);
2024-04-19 16:15:46 +02:00
}
}