genasyst/php-compressor

PHP代码压缩器或混淆器

1.0 2018-11-11 16:55 UTC

This package is auto-updated.

Last update: 2024-09-13 13:46:08 UTC


README

查看工作和使用压缩器 DEMO

README.RU.md 俄语说明

Packagist Github issues

phpCompressor - 主要任务是压缩PHP代码,它也可以用作简单的PHP混淆器。压缩器通过删除空格、代码注释、函数中局部变量的名称缩写、属性类(变量)的名称缩写、类方法的名称缩写来压缩代码。

压缩设置

  • 函数中局部变量名的压缩
  • 类属性(变量)的名称压缩
  • 类方法的名称压缩
  • 删除空格、注释和换行符

名称缩写的原则

使用别名缩写名称,这些别名根据名称的使用频率形成。

例如:名称 'data' 在代码中使用了98次,'options' - 70次,'values' - 68次等。结果将是

  • 'data' => 'a'
  • 'options' => 'b'
  • 'values' => 'c'
  • '...' => 'd...aa'
  • 'rare_name' => 'ab'
  • .....

安装

要安装,请使用composer composer

php composer.phar require genasyst/php-compressor

使用示例

<?php

$compressor = new \Genasyst\phpCompressor\Compressor();

$code = 'echo $data;..... ';
/* Setting the code with the opening tag <?php first */
$compressor->setContentByCode('<?php '.$code);

/ * Install code from php file */
$file_path = __DIR__ .'/ExampleTestEcho.php';
$compressor->setContentByFile($file_path);


/**
 * SETTINGS
 *
 * Set reduction of local variables
 */
$compressor->compressLocalVariablesName();

/**
 * Set the reduction of class properties
 */
$compressor->compressObjectsVariablesName();

/**
 * Setting abbreviations for method names
 */
$compressor->compressObjectsMethodsName();


/**
 * WITH THE EXCEPTION OF COMPRESSION NAMES
 *
 *
 * Set the exception of the names of local variables
 */
$compressor->setExcludeNames(
    \Genasyst\phpCompressor\Compressor::COMPRESS_TYPE_LOCAL_VARIABLES,
    ['not_compress_local' => 'not_compress_local']
);

/**
 * Set the exception of the names of object properties
 */
$compressor->setExcludeNames(
    \Genasyst\phpCompressor\Compressor::COMPRESS_TYPE_OBJECT_VARIABLES,
    ['not_compressed_name' => 'not_compressed_name']
);

/**
 * Set the exception of the names of the methods on the object
 */
$compressor->setExcludeNames(
    \Genasyst\phpCompressor\Compressor::COMPRESS_TYPE_OBJECT_METHODS,
    ['thisMethodNameNotCompressed' => 'thisMethodNameNotCompressed']
);


/* Set the code of a piece */
$code = <<<CODE
function test1(\$long_name1,  \$not_compress_local = ' ++') {
    \$long_name1 = strtolower(\$long_name1);
    if(strlen(\$long_name1) > 10) {
        return strtoupper(\$long_name1);
    }
    return ucfirst(\$long_name1).\$not_compress_local;
}
class My {

    protected \$long_variable = '';

    protected \$super_long_variable = '';

    protected \$not_compressed_name = '';

    public function __construct(\$long_variable, \$super_long_variable, \$not_compressed_name)
    {
        \$this->long_variable = \$long_variable;
        \$this->super_long_variable = \$super_long_variable;
        \$this->not_compressed_name = \$not_compressed_name;
    }

    public function getSuperLongVariable()
    {
        return \$this->super_long_variable;
    }

    public function getLongVariable()
    {
        return   \$this->long_variable;
    }

    public function thisMethodNameNotCompressed()
    {
        return  \$this->not_compressed_name;
    }
}

\$my = new My('lONg','SuperLongUpper','NOT_compressed');

echo test1(\$my->getLongVariable());//Long ++
echo test1(\$my->getSuperLongVariable());//SUPERLONGUPPER ++
echo test1(\$my->thisMethodNameNotCompressed());//NOT_COMPRESSED  ++
CODE;
$compressor->parseBlock($code);


/**
 * Start compression
 */
$compressor->compress();


/**
 * Get back the compressed code
 */
$code = $compressor->getContent();
echo $code;
/**
 * RESULT
 * function test1($a, $not_compress_local = ' ++')
 * {
 *     $a = strtolower($a);
 *     if (strlen($a) > 10) {
 *         return strtoupper($a);
 *     }
 *     return ucfirst($a) . $not_compress_local;
 * }
 * class My
 * {
 *     protected $b = '';
 *     protected $a = '';
 *     protected $not_compressed_name = '';
 *     public function __construct($c, $b, $a)
 *     {
 *         $this->b = $c;
 *         $this->a = $b;
 *         $this->not_compressed_name = $a;
 *     }
 *     function b()
 *     {
 *         return $this->a;
 *     }
 *     function a()
 *     {
 *         return $this->b;
 *     }
 *     public function thisMethodNameNotCompressed()
 *     {
 *         return $this->not_compressed_name;
 *     }
 * }
 * $a = new My('lONg', 'SuperLongUpper', 'NOT_compressed');
 * echo test1($a->a()); //Long ++
 * echo test1($a->b());//SUPERLONGUPPER 
 * echo test1($a->thisMethodNameNotCompressed());//NOT_COMPRESSED  
*/