Add php serialize tool
This commit is contained in:
62
api/php/php_serialize.php
Normal file
62
api/php/php_serialize.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?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;
|
||||
}
|
Reference in New Issue
Block a user