Last active
October 14, 2020 20:10
-
-
Save zhiyb/42794cc0036e977f68931060b8f07024 to your computer and use it in GitHub Desktop.
POST method ping-pong and hex dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require 'dbconf.php'; | |
| $db = new mysqli($dbhost, $dbuser, $dbpw, $dbname); | |
| if ($db->connect_error) | |
| die("Connection failed: " . $db->connect_error . "\n"); | |
| $db->set_charset('utf8mb4'); | |
| $name = basename($_SERVER['SCRIPT_FILENAME'], ".php"); | |
| if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
| $data = file_get_contents("php://input"); | |
| $stmt = $db->prepare('INSERT INTO `ping_pong` (`name`, `data`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `data` = VALUES(`data`)'); | |
| $stmt->bind_param('sb', $name, $null); | |
| $stmt->send_long_data(1, $data); | |
| if ($stmt->execute() !== true) { | |
| http_response_code(500); | |
| die($stmt->error . "\n"); | |
| } | |
| die(); | |
| } else if (isset($_GET['setup'])) { | |
| if (!$db->query('CREATE TABLE `ping_pong` ( | |
| `name` tinytext NOT NULL, `data` mediumblob DEFAULT NULL | |
| ) DEFAULT CHARSET=utf8mb4')) | |
| die("Error: " . $db->error); | |
| if (!$db->query('ALTER TABLE `ping_pong` ADD PRIMARY KEY (`name`(32))')) | |
| die("Error: " . $db->error); | |
| die("Success"); | |
| } else { | |
| $stmt = $db->prepare('SELECT `data` FROM `ping_pong` WHERE `name` = ?'); | |
| $stmt->bind_param('s', $name); | |
| if ($stmt->execute() !== true) { | |
| http_response_code(500); | |
| die($stmt->error . "\n"); | |
| } | |
| $data = $stmt->get_result()->fetch_row()[0]; | |
| if (isset($_GET['binary'])) { | |
| header("Content-type: application/octet-stream"); | |
| header("Content-Disposition: attachment; filename=" . $name . ".bin"); | |
| echo $data; | |
| die(); | |
| } else if (isset($_GET['echo'])) { | |
| echo $data; | |
| die(); | |
| } | |
| } | |
| ?><!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="apple-mobile-web-app-capable" content="yes"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>PING-PONG</title> | |
| </head> | |
| <body> | |
| <a href="?binary">Download</a> / <a href="?echo">Show</a><br> | |
| <pre> | |
| <?php | |
| // https://stackoverflow.com/a/4225813 | |
| function hex_dump($data, $newline="\n") | |
| { | |
| static $from = ''; | |
| static $to = ''; | |
| static $width = 16; # number of bytes per line | |
| static $pad = '.'; # padding for non-visible characters | |
| if ($from==='') | |
| { | |
| for ($i=0; $i<=0xFF; $i++) | |
| { | |
| $from .= chr($i); | |
| $to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad; | |
| } | |
| } | |
| $hex = str_split(bin2hex($data), $width*2); | |
| $chars = str_split(strtr($data, $from, $to), $width); | |
| $offset = 0; | |
| foreach ($hex as $i => $line) | |
| { | |
| echo sprintf('%6X',$offset).' : '.implode(' ', str_split($line,2)) . ' [' . $chars[$i] . ']' . $newline; | |
| $offset += $width; | |
| } | |
| } | |
| echo hex_dump($data); | |
| ?> | |
| </pre> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment