random-web-tools/api/php/php_serialize.php
2024-08-20 22:00:04 +03:00

63 lines
1.5 KiB
PHP

<?php
require_once 'cors.php';
$input = json_decode(file_get_contents('php://input'), true);
function var_export_short($data, $return=true) {
$dump = var_export($data, true);
$dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
$dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
$dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties
if (gettype($data) == 'object') { // Deal with object states
$dump = str_replace('__set_state(array(', '__set_state([', $dump);
$dump = preg_replace('#\)\)$#', "])", $dump);
} else {
$dump = preg_replace('#\)$#', "]", $dump);
}
if ($return===true) {
return $dump;
} else {
echo $dump;
}
}
switch ($input['mode']) {
case 'serialize':
$data = '';
// try any vaue
try {
eval("\$data = " . $input['data'] . ";");
echo serialize($data);
} catch (Throwable $e) {
try {
eval("\$data = '" . addslashes($input['data']) . "';");
echo serialize($data);
} catch (Throwable $e) {
echo 'err: invalid syntax, use $val = [your text];';
}
}
break;
default:
$data = $input['data'];
$result = unserialize($data);
if ($result) {
if (is_string($result)) {
echo trim(var_export_short($result), "'");
} else {
echo var_export_short($result);
}
} else {
echo 'err: invalid syntax';
}
break;
}