jscrambler php code

The php/smarty code below can be used to call the JScrambler API to obfuscate your javascript code. There are some Smarty variables in there as well so you may have to modify for straight PHP. This example was for use in a CMSMS (CMS Made Simple) plugin. The HTML formatting of the Smarty code was performed using the ejectamenta Code2HTML web tool

Here is the json configuration file example

{
    "connection":{ 
        "access_key":"MY_JSCRAMBLER_ACCESS_KEY",
        "secret_key":"MY_JSCRAMBLER_SECRET_KEY",
        "server":"api.jscrambler.com",
        "port":"443"
    },
    "files":["path/file1.js", 
			"path/file2.js", 
			"path/file3.js"],
    "parameters":{"domain_lock":"mydomain.com",
    			  "rename_local" : "%DEFAULT%",
    			  "exceptions_list" : "file_to_ignore",
    			  "mode":"html5"
    			}
}

Here is the php/smarty code

function obfuscate()
{
	$config_file_path = 'plugins/config.json';

	if ($_POST && !empty($_POST['check_list'])) {
		/* do obfusicate code */
		
		require_once 'jscrambler.php';

		$delay_seconds = 2;

		$destination_path = 'live';

		/* Parse config json parameters */
		$config_file_contents = file_get_contents($config_file_path);

		if($config_file_contents==false) {
			die("Failed to read configuration file '$config_file_path' on current directory.\n");
		}

		$cfc_obj = json_decode($config_file_contents);

		if(empty($cfc_obj)) {
			die("Failed to decode json configuration file.\n");
		}

		if(!isset($cfc_obj->files)) {
			die("'files' parameter was not found on configuration file.");
		}

		$files_list = array();

		/* get filenames from checkboxes instead */
		foreach($_POST['check_list'] as $filename) {
			if($filename)
			{
				$files_list[] = $filename;
				echo "processing file " . $filename . "
"; } } $post_params = array('files' => $files_list); if(!isset($cfc_obj->parameters)) { die("'parameters' parameter was not found on configuration file."); } foreach($cfc_obj->parameters as $key => $value) { $post_params[$key] = $value; } if(!isset($cfc_obj->connection)) { die("'connection' parameter was not found on configuration file."); } else if(!isset($cfc_obj->connection->access_key)) { die("'access_key' parameter was not found on configuration file."); } else if(!isset($cfc_obj->connection->secret_key)) { die("'secret_key' parameter was not found on configuration file."); } /* Init jscrambler client */ $jscrambler = new Jscrambler( $cfc_obj->connection->access_key, $cfc_obj->connection->secret_key, $cfc_obj->connection->server, $cfc_obj->connection->port); /* Send project to api server */ echo "Sending files to server.\n"; $post_response = $jscrambler->post('/code.json', $post_params); /* if sending project fails */ if($post_response == NULL) { die("Failed to send project to server.\n"); } /* parse json response */ $post_response_obj = json_decode($post_response); if(empty($post_response_obj)) { die("Failed to decode json POST response.\n"); } if(!isset($post_response_obj->id) || isset($post_response_obj->error)) { die("Something went wrong.\n$post_response\n"); } if(isset($post_response_obj->warnings)) { if(isset($post_response_obj->warnings->unknown_parameters)) { echo "Found unknown parameters [".implode(",", $post_response_obj->warnings->unknown_parameters)."]\n"; } } /* poll server */ echo "Polling server...\n"; while(true) { /* get project information */ $get_response = $jscrambler->get("/code/{$post_response_obj->id}.json"); /* parse GET response */ $get_response_obj = json_decode($get_response); if(empty($get_response_obj)) { die("Failed to decode json GET response.\n"); } if($get_response_obj->error_id == "0" && $get_response_obj->error_message == "OK") { echo "Ready to download.\n"; break; } else if(isset($get_response_obj->error_id) || isset($get_response_obj->error_message)) { die("Error found.\nid: {$get_response_obj->error_id}\nmessage: {$get_response_obj->error_message}\n"); } else if(!isset($get_response_obj->id) || isset($get_response_obj->error)) { die("Something went wrong.\n$get_response\n"); } echo "Not ready. Next poll in $delay_seconds seconds.\n"; sleep($delay_seconds); } $result_path = $destination_path.DIRECTORY_SEPARATOR."{$get_response_obj->id}.{$get_response_obj->extension}"; echo "Writting file to '$result_path'... "."
"; file_put_contents($result_path, $jscrambler->get("/code/{$post_response_obj->id}.zip")); $delay_seconds = 1; while(true) { // get project information */ if(file_exists($result_path)) { $zip = new ZipArchive; $zip->open($result_path); $zip->extractTo($destination_path); $zip->close(); unlink($result_path); echo "Done.\n"; break; } echo "Not ready. Next poll in $delay_seconds seconds.\n"; sleep($delay_seconds); } } else { /* Parse config json parameters */ $config_file_contents = file_get_contents($config_file_path); if($config_file_contents==false) { die("Failed to read configuration file '$config_file_path' on current directory.\n"); } $cfc_obj = json_decode($config_file_contents); if(empty($cfc_obj)) { die("Failed to decode json configuration file.\n"); } if(!isset($cfc_obj->files)) { die("'files' parameter was not found on configuration file."); } echo "

Obfusicate any of the following files:

"; echo '
'; foreach($cfc_obj->files as $filename) { echo ''.$filename.'
'; } echo "

Select and click submit to obfusicate

"; echo ''; echo '
'; } }