Created
September 21, 2013 20:39
-
-
Save defuse/6653961 to your computer and use it in GitHub Desktop.
Escaping Challenge: Make a PHP script that (Z:) generates JavaScript code that generates an HTML page containing a PHP script that (goto Z)...
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 | |
/* Escaping Challenge: Make a PHP script that (Z:) generates JavaScript code | |
* that generates an HTML page containing a PHP script that (goto Z) */ | |
/* The purpose of this challenge is to demonstrate how complicated escaping can | |
* get when you're trying to combine 4 different languages (PHP, JavaScript, | |
* HTML, and string literals). */ | |
function js_string_escape($data) | |
{ | |
$safe = ""; | |
for($i = 0; $i < strlen($data); $i++) | |
{ | |
if(ctype_alnum($data[$i])) | |
$safe .= $data[$i]; | |
else | |
$safe .= sprintf("\\x%02X", ord($data[$i])); | |
} | |
return $safe; | |
} | |
function javascriptify($str) { | |
return $script; | |
} | |
echo "<html><body><script>\n"; | |
echo "var x = \"<pre>" . js_string_escape(htmlentities(file_get_contents(__FILE__), ENT_QUOTES)) . "</pre>\";\n"; | |
echo "document.body.innerHTML = x;\n"; | |
echo "</script></body></html>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment