dencode.xrg.es/app/public/index.php

195 lines
6.9 KiB
PHP
Raw Normal View History

2019-07-18 06:53:55 +00:00
<?php
2021-09-15 11:27:22 +00:00
declare(strict_types=1);
2021-11-08 09:34:52 +00:00
$start_time = microtime(true);
2021-09-15 11:27:22 +00:00
require_once '../vendor/autoload.php';
2019-07-18 06:53:55 +00:00
// action => label
$buttons = [
'urlencode' => 'URL Encode',
'urldecode' => 'URL Decode',
'b64encode' => 'Base64 Encode',
'b64decode' => 'Base64 Decode',
'htmlencode' => 'HTML Encode',
'htmldecode' => 'HTML Decode',
2019-07-18 07:20:22 +00:00
'uuencode' => 'UU Encode',
'uudecode' => 'UU Decode',
2019-07-18 06:53:55 +00:00
'qprintencode' => 'Qprint Encode',
'qprintdecode' => 'Qprint Decode',
'hash' => 'Hash',
'hex' => 'Hex',
2021-04-19 11:48:39 +00:00
'prettyjson' => 'Pretty JSON',
2022-09-20 11:21:29 +00:00
'prettyxml' => 'Pretty XML',
2021-09-20 07:48:49 +00:00
'serializedtojson' => 'PHP Serialized to JSON',
2023-09-18 09:22:13 +00:00
'openssl_genrsa' => 'OpenSSL genrsa',
2019-07-18 06:53:55 +00:00
];
2023-08-29 14:43:53 +00:00
$str = $result = $_POST['str'] ?? $_GET['str'] ?? '';
$action = $_POST['action'] ?? $_GET['action'] ?? '';
2023-08-28 08:15:42 +00:00
$plain = str_contains($_SERVER['HTTP_ACCEPT'] ?? '', 'text/plain');
2021-09-15 15:26:10 +00:00
$help = 'https://www.php.net/manual/';
2021-09-20 07:48:49 +00:00
$jsonEncodeOptions = JSON_PRETTY_PRINT | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES;
2019-07-18 06:53:55 +00:00
if ($action == 'reset') {
http_response_code(302);
header("Location: /");
2023-09-18 09:22:13 +00:00
exit;
2019-07-18 06:53:55 +00:00
} elseif ($action == 'urlencode') {
2021-09-20 11:41:14 +00:00
$result = urlencode($str)?: $str;
$help = 'https://www.php.net/urlencode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'urldecode') {
2021-09-20 11:41:14 +00:00
$result = urldecode($str)?: $str;
$help = 'https://www.php.net/urldecode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'b64encode') {
$result = base64_encode($str)?: $str;
$help = 'https://www.php.net/base64_encode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'b64decode') {
$result = base64_decode($str)?: $str;
$help = 'https://www.php.net/base64_decode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'htmlencode') {
$result = htmlentities($str, ENT_QUOTES | ENT_HTML5)?: $str;
$help = 'https://www.php.net/htmlentities';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'htmldecode') {
$result = html_entity_decode($str, ENT_QUOTES | ENT_HTML5)?: $str;
$help = 'https://www.php.net/html_entity_decode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'uuencode') {
$result = convert_uuencode($str)?: $str;
$help = 'https://www.php.net/convert_uuencode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'uudecode') {
$result = convert_uudecode($str)?: $str;
$help = 'https://www.php.net/convert_uudecode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'hash') {
2021-11-08 09:34:52 +00:00
$result = "Algorithm Time Len Hash\n\nhash()\n";
2019-07-18 06:53:55 +00:00
foreach (hash_algos() as $algo) {
$t1 = microtime(true);
$r = hash($algo, $str, false);
$t2 = (microtime(true) - $t1) * 1000;
$result .= sprintf("%-15s %.3f %3d %s\n", $algo, $t2, strlen($r), $r);
}
2019-11-05 09:32:39 +00:00
2021-11-08 09:34:52 +00:00
$result .= "\npassword_hash()\n";
foreach (password_algos() as $algo) {
$t1 = microtime(true);
$r = password_hash($str, $algo);
$t2 = (microtime(true) - $t1);
$result .= sprintf("%-15s %.3f %3d %s\n", $algo, $t2, strlen($r), $r);
}
2019-11-05 09:32:39 +00:00
2019-07-18 06:53:55 +00:00
$result = trim($result);
$help = 'https://www.php.net/hash';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'hex') {
$dumper = new Clue\Hexdump\Hexdump();
$result = $dumper->dump($str);
$help = 'https://github.com/clue/php-hexdump';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'qprintencode') {
$result = quoted_printable_encode($str)?: $str;
$help = 'https://www.php.net/quoted_printable_encode';
2019-07-18 06:53:55 +00:00
} elseif ($action == 'qprintdecode') {
$result = quoted_printable_decode($str)?: $str;
$help = 'https://www.php.net/quoted_printable_decode';
2021-04-19 11:48:39 +00:00
} elseif ($action == 'prettyjson') {
$jd = json_decode($str);
if (JSON_ERROR_NONE !== json_last_error()) {
2021-09-20 07:48:49 +00:00
$result = 'Input text couldn\'t be decoded: '."\n\n".json_last_error_msg();
2021-04-19 11:48:39 +00:00
} else {
2021-09-20 07:48:49 +00:00
$result = json_encode($jd, $jsonEncodeOptions);
2021-04-19 11:48:39 +00:00
}
$help = 'https://www.php.net/json';
2022-09-20 11:21:29 +00:00
} elseif ($action == 'prettyxml') {
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
@$dom->loadXML($str);
$result = @$dom->saveXML();
$help = 'https://www.php.net/DOMDocument';
2021-09-20 07:48:49 +00:00
} elseif ($action == 'serializedtojson') {
$stdClass = preg_replace('~O:[0-9]+:"[^"]+"~', 'O:8:"stdClass"', $str);
2023-08-29 14:43:53 +00:00
$unserialized = unserialize($stdClass);
2021-09-20 07:48:49 +00:00
if ($unserialized === false) {
$result = 'Input text couldn\'t be unserialized: '."\n\n".(error_get_last()['message'] ?? 'Unknown error.');
} else {
$result = json_encode($unserialized, $jsonEncodeOptions);
}
$help = 'https://www.php.net/serialize';
2023-09-18 09:22:13 +00:00
} elseif ($action == 'openssl_genrsa') {
$passphrase = strlen($str) > 0 ? hash('sha256', $str) : bin2hex(openssl_random_pseudo_bytes(32));
$options = ['private_key_bits' => 2048, 'encrypt_key' => true, 'encrypt_key_cipher' => OPENSSL_CIPHER_AES_128_CBC];
$key = openssl_pkey_new($options);
openssl_pkey_export($key, $pkey, $passphrase);
$result = "Passphrase: " . $passphrase . "\n\n" . $pkey . "\n" . openssl_pkey_get_details($key)['key'];
$help = 'https://www.php.net/openssl_pkey_new';
} else {
$str = '';
$action = '';
$help = 'https://www.php.net/';
2021-09-20 07:48:49 +00:00
}
if ($plain) {
header('Content-Type: text/plain;charset=UTF-8');
die($result);
2019-07-18 06:53:55 +00:00
}
?><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
2021-09-15 15:26:10 +00:00
<meta name="viewport" content="width=device-width, initial-scale=1">
2019-07-18 06:53:55 +00:00
2023-09-18 09:22:13 +00:00
<title>Decode/Encode/Hash/Pretty/etc</title>
2021-09-15 11:27:22 +00:00
<meta name="author" content="Sergio Álvarez <correo@sergio.am>">
2021-09-15 15:26:10 +00:00
<meta name="description" content="Encode and Decode strings from/to URL, Base64, HTML entities, UU, Qprint, hash, hex and more." />
2021-09-15 11:27:22 +00:00
<link rel="apple-touch-icon" sizes="180x180" href="/img/favicon.io/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon.io/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon.io/favicon-16x16.png">
<link rel="manifest" href="/img/favicon.io/site.webmanifest">
2019-07-18 06:53:55 +00:00
2021-09-20 07:48:49 +00:00
<link rel="stylesheet" href="/css/pico.min.css">
2021-09-15 15:26:10 +00:00
<link rel="stylesheet" href="/css/custom.css">
2019-07-18 06:53:55 +00:00
</head>
2021-09-15 15:26:10 +00:00
<body class="container-fluid">
2023-08-29 14:43:53 +00:00
<form method="get" id="form" action="/">
2021-09-16 09:28:28 +00:00
<main class="layout">
<div>
<?php foreach ($buttons as $act => $label) { ?>
<button type="submit" name="action" value="<?=$act?>" class="<?=($action == $act? ' contrast': '')?>"><?=$label?></button>
<?php } ?>
<a href="/" role="button" class="secondary">Reset</a>
<a href="<?=$help?>" role="button" class="secondary">Help</a>
2021-10-15 18:59:01 +00:00
<hr />
2023-09-18 09:22:13 +00:00
<p><small><a href="mailto:dencode@sergio.am">contact</a> · <a href="https://sergio.am/code/dencode.xrg.es">source code</a> · <?php printf("%.6f", (microtime(true) - $start_time)); ?>s</small></p>
2021-09-16 09:28:28 +00:00
</div>
<div>
2023-08-29 14:43:53 +00:00
<textarea name="str" id="str" class="form-input" placeholder="Paste a string here"><?=htmlentities((string)$result, ENT_QUOTES | ENT_HTML5 | ENT_IGNORE)?></textarea>
2021-09-16 09:28:28 +00:00
</div>
2021-09-15 15:26:10 +00:00
</main>
</form>
2023-08-29 14:43:53 +00:00
<script src="/js/form.js"></script>
2019-07-18 06:53:55 +00:00
</body>
</html>