Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2011 14:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1501834 to your computer and use it in GitHub Desktop.
Save anonymous/1501834 to your computer and use it in GitHub Desktop.
PHP Obfuscator / Compressor
<?php
/**
* Coded by PabloBandin ( p58l0.84nd1n@gmail.com )
*/
/**
* @example
* <pre>
$file = file_get_contents("someFile.php");
include "phpLiteObfuscator.php";
$diccionary = array('foo'=>'______foo');
$compressed = phpLiteObfuscator( $file,
array( "query" ),
array( "fetchAssoc","fetchArray","fetchAll","fetchColumn","query"),
array(),
$diccionary );
file_put_contents("someFile.compressed.php", $compressed);
* </pre>
*
*
* @param string $SourceString -php file string to compress
* @param array $_varsPrivate -Variables to ignore from compressing
* @param array $_funcPrivate -Function names to ignore from compression
* @param array $classPrivate -Class names to ignore from compression
* @param array $dicc -Will be modified! Array of definitions to share, each key is a name and the value is the result of a compresion.
* @return string
*/
function phpLiteObfuscator( $SourceString,
$_varsPrivate=array(),
$_funcPrivate=array(),
$classPrivate=array(),
&$dicc
)
{
$varsPrivate = array_merge( $_varsPrivate, array( '_SESSION','_GET','_POST','this','_SERVER' ) );
$funcPrivate = array_merge( $_funcPrivate, array( "__construct") );
/* FUNCTIONS */
$ref = phpLiteObfuscatorObscurer( $SourceString,
$funcPrivate,
"/function\s+(\w+)\s*\(/m",
"/(\W)VARNAME\s*(\()/",
$dicc
);
/* VARIABLES */
$ref = phpLiteObfuscatorObscurer( $SourceString,
$varsPrivate,
"/\\$(\w{2,})\W/m",
array(
"|([>])\s*VARNAME(\W)|",
"|(\\$)"."VARNAME(\W)|"
),
$ref );
/* CLASSNAMES */
$ref = phpLiteObfuscatorObscurer( $SourceString,
$classPrivate,
"/(?:class|interface) (\w+)[^\w]/m",
"/(\W)VARNAME(\W)/",
$ref );
##remove comments
//
$SourceString = preg_replace( "/(\s+)#(.*)\n/","$1\n",$SourceString );
$SourceString = preg_replace( "/(\s+)\/\/(.*)\n/","$1\n",$SourceString );
##remove spaces
$SourceString = preg_replace( "/ +/"," ",$SourceString );
$SourceString = preg_replace( "/\n\s*\n/","\n",$SourceString );
$SourceString = preg_replace( "/\t+/"," ",$SourceString );
$SourceString = preg_replace( "/<\?php\s*/","<?php ",$SourceString );
$SourceString = preg_replace( "/\n/","",$SourceString );
$SourceString = preg_replace( "#/\*(?:(?!\*/).)*\*/#","",$SourceString );
//$dicc = $ref;
$dicc = $ref;
return $SourceString;
}
/**@private*/
function phpLiteObfuscatorObscurer( &$Source, &$privateVars, $ColectorRegexp, $snifferRegexp, $refs = array() )
{
$find = array();
$replace = array();
$varNum = count($refs);
$diccionary = &$refs;
preg_match_all( $ColectorRegexp ,$Source, $matches );
foreach( $matches[1] as $i=>$varName )
{
if( in_array( $varName, $privateVars ) || array_key_exists( $varName, $diccionary) ){
continue;
}
$varNum++;
if( array_key_exists( $varName, $diccionary) )
{
$vname = $diccionary[$varName];
}
else
{
$diccionary[$varName] = $vname = "_" . dechex($varNum);
}
if( is_array( $snifferRegexp ) ){
foreach( $snifferRegexp as $_snifRegexp ){
$find[] = str_replace( "VARNAME",$varName, $_snifRegexp );
$replace[] = "$1". $vname . "$2";
}
}
else {
$find[] = str_replace("VARNAME",$varName, $snifferRegexp);
$replace[] = "$1". $vname . "$2";
}
}
$Source = preg_replace( $find, $replace, $Source );
return $diccionary;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment