PHP Javascript Packer High ASCII setting causes "SyntaxError: in Javascript"
C0nw0nk opened this issue · 4 comments
High ASCII setting causes "SyntaxError: illegal character" or "SyntaxError: missing ) after argument list" or "SyntaxError: missing semicolon" as well as others.
Example code to reproduce : (file can be called "example.php")
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables</p>
<!-- This should be modified by javascript and output should be 11 -->
<p id="demo"></p>
<script>
<?php
$script = "var x=5;var y=6;var z=x + y;document.getElementById('demo').innerHTML=z;";
echo "/* code before packing output inside the HTML demo id should be 11 \n" . $script . "\n*/\n\n";
$encoding = (int)95; //High ASCII
$fast_decode = true;
$special_char = true;
$remove_semicolon = false; //default is true
$packer = new Packer($script, $encoding, $fast_decode, $special_char, $remove_semicolon);
$packed = $packer->pack();
echo $packed;
?>
</script>
</body>
</html>
Output of above code :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables</p>
<!-- This should be modified by javascript and output should be 11 -->
<p id="demo"></p>
<script>
/* code before packing output inside the HTML demo id should be 11
var x=5;var y=6;var z=x + y;document.getElementById('demo').innerHTML=z;
*/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(c/a))+String.fromCharCode(c%a+161)};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\[\xa1-\xff]+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp(e(c),'g'),k[c])}}return p}('� x=5;� y=6;� z=x+y;�.�(\'�\').�=z;',5,5,'var|document|getElementById|demo|innerHTML'.split('|'),0,{}))
</script>
</body>
</html>
What is the way to get the Highest setting working without breaking the JavaScript it is packing / obfuscating like it currently seems to be doing.
Javascript can't execute the packed eval() script because of syntax errors.
Yes the soloution was with PHP's utf8_encode function there may be a more compatible / better way since not everybody has a HTML page that is UTF-8 but my pages are UTF-8 and the High ASCII setting caused the Unicode characters in the output to be unreadable.
$packed = utf8_encode($packer->pack());
echo $packed;
That was how i fixed it by UTF8 encoding all the High ASCII characters for UTF8 but if people are not using UTF8 HTML outputs that won't work for them a more universal technique may be needed i feel.