<?php /** * jParser example * */ require '../jparser-libs/jparser.php'; /** * Get some example source code from a *.js file */ $source = file_get_contents('complex.js'); /** * Get the full parse tree */ $Prog = JParser::parse_string($source); if (!$Prog instanceof JProgramNode) { die('Root of parse tree is not a JProgramNode'); } // Collapsing back to a string minifies by default, because whitespace and comments are not in the tree $min = $Prog->__toString(); // Obfuascate tree $protect = array(); $Prog->obfuscate($protect); $obf = $Prog->__toString(); echo '<?xml version="1.0" encoding="UTF-8" ?>'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>jParser example :: j_obfuscate</title>
<?php /** * Example shows how to import the jParser classes for library development * Be sure to set up your system config in conf/PLUG.conf.php */ // PLUG framework is required to import classes require '../../PLUG/plug.php'; // My aswell import everything from the JavaScript package import('PLUG.JavaScript.*'); // Using the JTokenizer class directly $src = 'alert("Hello World")'; $Tokenizer = new JTokenizer(true, true); $tokens = $Tokenizer->get_all_tokens($src); //var_dump( $tokens ); // Using the JParser class directly try { $Parser = new JParser(); $Tree = $Parser->parse($tokens); } catch (ParseError $Ex) { $error = $Ex->getMessage() . "\n----\n" . $Ex->snip($src); die('<pre>' . htmlentities($error, ENT_COMPAT, 'UTF-8') . '</pre>'); } // PLUG buffers errors, so we need to see if anything went wrong. if (PLUG::is_error()) { PLUG::dump_errors(); } else { echo 'Done without error.'; }