Skip to content

Instantly share code, notes, and snippets.

@thecrypticace
Created September 21, 2013 21:37
Show Gist options
  • Save thecrypticace/6654461 to your computer and use it in GitHub Desktop.
Save thecrypticace/6654461 to your computer and use it in GitHub Desktop.
<?php
header("Content-Type:text/plain");
/* 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)
{
return implode(array_map(function($s){
return (ctype_alnum($s) ? $s : sprintf("\\x%02X", ord($s)));
}, str_split(htmlentities($data,ENT_QUOTES))));
}
echo 'document.body.innerHTML="'.js_string_escape(file_get_contents(__FILE__)).'";';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment