Files
admin-php-module/models/Base.php
miroman-afk c4dc6e02a0 v.2.30
Добавлен расширенный отчет по реализации
Добавлен монитор активности
2023-08-03 11:05:15 +03:00

161 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Component\Models;
use Illuminate\Support\Facades\Log;
class Base
{
public static function tofloat($num) {
$dotPos = strrpos($num, '.');
$commaPos = strrpos($num, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
if (!$sep) {
return floatval(preg_replace("/[^0-9]/", "", $num));
}
return floatval(
preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
);
}
public static function validate_json($string)
{
// decode the JSON data
$result = json_decode(utf8_encode($string), true, JSON_INVALID_UTF8_SUBSTITUTE);
// switch and check possible JSON errors
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = ''; // JSON is valid // No error has occurred
break;
case JSON_ERROR_DEPTH:
$error = 'The maximum stack depth has been exceeded.';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Invalid or malformed JSON.';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Control character error, possibly incorrectly encoded.';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON.';
break;
// PHP >= 5.3.3
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
break;
// PHP >= 5.5.0
case JSON_ERROR_RECURSION:
$error = 'One or more recursive references in the value to be encoded.';
break;
// PHP >= 5.5.0
case JSON_ERROR_INF_OR_NAN:
$error = 'One or more NAN or INF values in the value to be encoded.';
break;
case JSON_ERROR_UNSUPPORTED_TYPE:
$error = 'A value of a type that cannot be encoded was given.';
break;
default:
$error = 'Unknown JSON error occured.';
break;
}
if ($error !== '') {
// throw the Exception or exit // or whatever :)
return false;
}
// everything is OK
return true;
}
public static function data_decode($data)
{
$alph = [
"А", "Б", "В", "Г", "Д",
"Е", "Ё", "Ж", "З", "И",
"Й", "К", "Л", "М", "Н",
"О", "П", "Р", "С", "Т",
"У", "Ф", "Х", "Ц", "Ч",
"Ш", "Щ", "Ъ", "Ы", "Ь",
"Э", "Ю", "Я",
"а", "б", "в", "г", "д",
"е", "ё", "ж", "з", "и",
"й", "к", "л", "м", "н",
"о", "п", "р", "с", "т",
"у", "ф", "х", "ц", "ч",
"ш", "щ", "ъ", "ы", "ь",
"э", "ю", "я",
];
foreach ($alph as $key => $letter) {
$haystack = mb_convert_encoding($data, "CP1251", "UTF-8");
$needle = $letter;
$pos = strripos($haystack, $needle);
if ($pos === false) {
$after_conv = false;
} else {
$after_conv = true;
break;
}
}
if (!$after_conv) {
foreach ($alph as $key => $letter) {
$haystack = $data;
$needle = $letter;
$pos = strripos($haystack, $needle);
if ($pos === false) {
$before_conv = false;
} else {
$before_conv = true;
break;
}
}
}
if ($after_conv) {
$retval = mb_convert_encoding($data, "CP1251", "UTF-8");
} elseif ($before_conv) {
$retval = $data;
} else {
$retval = $data;
}
return $retval;
}
public static function customBase64Decode($data) {
$base64 = $data;
$base64 = str_replace(array('\r\n', '\r', '\n'), '', $base64);
$base64 = str_replace(' ', '', $base64);
$decoded = base64_decode($base64);
$decoded = mb_convert_encoding($decoded, 'UTF-8');
log::debug(self::ascii2hex($decoded));
return $decoded;
}
public static function ascii2hex($ascii) {
$hex = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hex.=$byte." ";
}
return $hex;
}
public static function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && self::in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
}