Skip to content

Instantly share code, notes, and snippets.

@mingtsay
Created May 1, 2011 05:59
Show Gist options
  • Save mingtsay/950287 to your computer and use it in GitHub Desktop.
Save mingtsay/950287 to your computer and use it in GitHub Desktop.
Data URL Praser
<?php
# Demo: http://mt.aa.am/dataurl.php
# data:[<MIME-type>][;charset=<encoding>][;base64],<data>
if(isset($_GET['uri']) || isset($_POST['uri']) || isset($_GET['file'])) {
if(isset($_GET['uri'])) $uri = $_GET['uri'];
else if(isset($_POST['uri'])) $uri = $_POST['uri'];
else if(isset($_GET['file'])) {
$file = $_GET['file'];
if(!(strtolower(substr($file, 0, 7)) == "http://" || strtolower(substr($file, 0, 8)) == "https://"))
if(realpath("dataurl/$file") != realpath("dataurl") . "/" . $file) die(header("HTTP/ 403"));
$uri = file_get_contents($_GET['file']);
}
$mime = "text/plain";
$charset = "";
$base64 = false;
$exp = explode(",", $uri, 2);
$head = explode(";", $exp[0]);
$data = $exp[1];
foreach($head as $v) {
if(strtolower(substr($v, 0, 5)) == "data:") $v = substr($v, 5);
if(strtolower(substr($v, 0, 8)) == "charset=") $charset = ";$v";
else if(strtolower($v) == "base64") $base64 = true;
else $mime = $v;
}
header("Content-type: $mime$charset");
if($base64) echo(base64_decode($data));
else echo(urldecode($data));
} else {
?>
<form method="post">
<input name="uri" />
<input type="submit" />
</form>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment