Location: PHPKode > scripts > The PHP Compiler > examples/Compiler.php
#/usr/bin/php -q
<?php

//+----------------------------------------------------------------------+
//| The (sort of) PHP Compiler v0.1                                      |
//+----------------------------------------------------------------------+
//| Copyright (c) 2006 Warren Smith ( smythinc 'at' gmail 'dot' com )    |
//+----------------------------------------------------------------------+
//| This library is free software; you can redistribute it and/or modify |
//| it under the terms of the GNU Lesser General Public License as       |
//| published by the Free Software Foundation; either version 2.1 of the |
//| License, or (at your option) any later version.                      |
//|                                                                      |
//| This library is distributed in the hope that it will be useful, but  |
//| WITHOUT ANY WARRANTY; without even the implied warranty of           |
//| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
//| Lesser General Public License for more details.                      |
//|                                                                      |
//| You should have received a copy of the GNU Lesser General Public     |
//| License along with this library; if not, write to the Free Software  |
//| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 |
//| USA                                                                  |
//+----------------------------------------------------------------------+
//| Simple is good.                                                      |
//+----------------------------------------------------------------------+
//

/*
  +----------------------------------------------------------------------+
  | This is a command line application to serve both as a command line   |
  | front-end for, and an example of how to use, The (sort of) PHP       |
  | Compiler. Please refer to the PHP documentation for more information |
  | on how to use PHP from the command line for your system.             |
  +----------------------------------------------------------------------+
*/

/*-----------------*/
/* I N C L U D E S */
/*-----------------*/

include_once('../Compiler.class.php');

/*---------------*/
/* D E F I N E S */
/*---------------*/

define('BANNER', 'The (sort of) PHP Compiler v0.1');

/*------------------*/
/* M A I N  C O D E */
/*------------------*/

// If we have arguments
if ($argc > 1){

	// This is the array of input files for the compiler
	$InputFiles = array();

	// Loop through each argument in the argv array because getopt() doesn't work on Windows
	for($i = 1; $i < $argc; $i++){

		// If this argument isn't a command
		if ($argv[$i][0] != '-'){

			// If we have no last command
			if (!$LastCommand){

				// Then we assume this is an input file
				array_push($InputFiles, $argv[$i]);

			} else {

				// Use the $LastCommand as the variable name and this argument as the value
				$$LastCommand = $argv[$i];

				// We're done with this
				unset($LastCommand);
			}

		} else {

			// Switch through the commands
			switch ($argv[$i][1]){

				// Help command
				case '-':
				case '?':
				case 'h':

					// Show the usage instructions and exit
					Usage();
					break;

				// Output file
				case 'o':

					// Make sure we set this variable on the next iteration
					$LastCommand = 'OutputFile';
					break;

				// Filter command
				case 'f':

					// Make sure we set this variable on the next iteration
					$LastCommand = 'Filter';
					break;

				// Number of characters per line command
				case 'n':

					// Make sure we set this variable on the next iteration
					$LastCommand = 'CharsPerLine';
					break;

				// Setting the name of the variable that holds the obfuscated source
				case 'v':

					// Make sure we set this variable on the next iteration
					$LastCommand = 'VariableName';
					break;

				// Custom shebang line
				case 's':

					// Make sure we set this variable on the next iteration
					$LastCommand = 'ShebangLine';
					break;

				// Custom header comment
				case 'c':

					// Make sure we set this variable on the next iteration
					$LastCommand = 'HeaderComment';
					break;

				// The version information
				case 'V':

					// Output the banner and exit
					exit(BANNER."\n");
					break;
			}
		}
	}

	// If we have no input files
	if (!count($InputFiles)){

		// Tell the user
		exit('No input files specified. Do `'.$argv[0].' -h` for usage instructions'."\n");
	}

	// Instantiate the compiler object
	$Compiler = new Compiler;

	// If we have a valid alternative for the default number of chars per line, use it
	if ($CharsPerLine && is_numeric($CharsPerLine)) $Compiler->CharsPerLine = $CharsPerLine;

	// If we have an alternative for the default variable name, use it
	if ($VariableName) $Compiler->VariableName = $VariableName;

	// If we have a shebang line, use it
	if ($ShebangLine) $Compiler->ShebangLine = $ShebangLine;

	// If we have a header file (containing a comment block), use it
	if ($HeaderComment && file_exists($HeaderComment)) $Compiler->HeaderComment = file_get_contents($HeaderComment);

	// Compile our files and collect the output
	$Output = $Compiler->Compile($InputFiles, $OutputFile, $Filters);

	// If no output file was specified, output the output directly
	if (!$OutputFile) echo $Output;

} else {

	// Show the usage information
	Usage($argv[0]);
}

// This is the usage function to show the user how to use this script
function Usage($appName){

	// This is our usage information
	echo BANNER."\n";
	echo 'Copyright (c) 2006 Warren Smith ( smythinc \'at\' gmail \'dot\' com ). All rights reserved.'."\n";
	echo ''."\n";

	echo 'Usage: '.$appname.' [input files] [options]'."\n";
	echo '-h'."\n";
	echo '    This help.'."\n";
	echo '-o'."\n";
	echo '    The output file, if none is specified the compiled source is output to STDOUT.'."\n";
	echo '-f'."\n";
	echo '    The name of the extra filter to use, either "leet" or "octal".'."\n";
	echo '-n'."\n";
	echo '    The number of characters (not including indentation) to have per line.'."\n";
	echo '-v'."\n";
	echo '    The name of the variable to put the obfuscated code into.'."\n";
	echo '-s'."\n";
	echo '    The shebang line to put into the final file, eg. "#/usr/bin/php -q".'."\n";
	echo '-c'."\n";
	echo '    The name of the file containing the header comment block to add to the final code.'."\n";
	echo '-V'."\n";
	echo '    Show the version information'."\n";
	echo ''."\n";

	// Stop code execution
	exit(-1);
}

?>
Return current item: The PHP Compiler