Add php serialize tool

This commit is contained in:
Illya Marchenko 2024-08-20 22:00:04 +03:00
parent 9778653a49
commit e99e92dda4
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
4 changed files with 131 additions and 0 deletions

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;
}

@ -60,6 +60,7 @@ export default {
'PHP': { 'PHP': {
'str_to_php_array': 'Str to PHP array', 'str_to_php_array': 'Str to PHP array',
'php_array_to_json': 'PHP array to Json', 'php_array_to_json': 'PHP array to Json',
'php_serialize': 'PHP serialize',
}, },
'GO': { 'GO': {
'go_json_to_struct': 'JSON to Go struct', 'go_json_to_struct': 'JSON to Go struct',

@ -95,6 +95,11 @@ const router = createRouter({
name: 'php_array_to_json', name: 'php_array_to_json',
component: () => import('../views/PHPArrayToJson.vue'), component: () => import('../views/PHPArrayToJson.vue'),
}, },
{
path: '/php_serialize',
name: 'php_serialize',
component: () => import('../views/PHPSerialize.vue'),
},
{ {
path: '/fix_ru_en_keyboard', path: '/fix_ru_en_keyboard',
name: 'fix_ru_en_keyboard', name: 'fix_ru_en_keyboard',

@ -0,0 +1,63 @@
<template>
<h2 class="tool-title">PHP serialize</h2>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="data">Data</label>
<textarea id="data" v-model="toolData.data" v-on:keyup="result"></textarea>
</div>
<div class="input-group">
<label for="data_delimiter">Mode</label>
<div>
<input id="mode_serialize" value="serialize" name="mode" v-model="toolData.mode" v-on:change="result" type="radio"> <label for="mode_serialize">serialize</label><br>
<input id="mode_unserialize" value="unserialize" name="mode" v-model="toolData.mode" v-on:change="result" type="radio"> <label for="mode_unserialize">unserialize</label><br>
</div>
</div>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="result">Result</label>
<MonacoEditor name="result" language="json" :value="toolResult"></MonacoEditor>
</div>
</template>
<script>
import axios from 'axios'
import { config } from '../../config';
import MonacoEditor from "@/components/MonacoEditor.vue";
export default {
components: {
MonacoEditor
},
data() {
return {
toolData: {
data: '',
mode: 'serialize',
},
toolResult: '',
};
},
methods: {
result() {
axios.post(`${config.APP_URL}/api/php/php_serialize.php`, {
data: this.toolData.data,
mode: this.toolData.mode,
})
.then((response) => {
this.toolResult = response.data;
Promise.resolve(response);
});
}
}
}
</script>
<style lang="scss">
</style>