Skip to content

Commit 8c01d73

Browse files
committed
Первая вресия
0 parents  commit 8c01d73

43 files changed

Lines changed: 2321 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
composer.lock
3+
vendor
4+
phpcs.xml
5+
phpunit.xml

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "genasyst/php-compressor",
3+
"description": "PHP code compressor or obfuscator",
4+
"keywords": ["php", "compressor", "obfuscator"],
5+
"type": "library",
6+
"license": "MIT",
7+
"homepage": "https://github.com/genasyst/phpCompressor",
8+
"authors": [
9+
{
10+
"name": "Gennadiy Ryzhov",
11+
"email": "support@genasyst.ru",
12+
"homepage": "http://genasyst.ru",
13+
"role": "Developer"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.4",
18+
"nikic/php-parser": "v3.0.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Genasyst\\phpCompressor\\": "src"
23+
}
24+
}
25+
}

src/Autoloader.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace Genasyst\phpCompressor;
3+
4+
class Autoloader
5+
{
6+
7+
protected static $namespace = 'Genasyst\\phpCompressor';
8+
private static $registered = false;
9+
10+
static public function register($prepend = false)
11+
{
12+
if (self::$registered === true) {
13+
return;
14+
}
15+
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
16+
self::$registered = true;
17+
}
18+
19+
static public function autoload($class)
20+
{
21+
if (0 === strpos($class, self::$namespace . '\\')) {
22+
$fileName = __DIR__ . strtr(substr($class, strlen(self::$namespace)), '\\', '/') . '.php';
23+
if (file_exists($fileName)) {
24+
require_once $fileName;
25+
} else {
26+
echo 'File not found' . $fileName;
27+
}
28+
}
29+
}
30+
}

src/Compressor.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace Genasyst\phpCompressor;
4+
5+
use Genasyst\phpCompressor\PhpParser;
6+
use Genasyst\phpCompressor\NamesRegistry;
7+
use Genasyst\phpCompressor\PhpParser\Visitors\Traverser\GlobalVisitor;
8+
9+
use PhpParser\NodeTraverser;
10+
use PhpParser\ParserFactory;
11+
use PhpParser\PrettyPrinter;
12+
use PhpParser\Error;
13+
14+
15+
class Compressor
16+
{
17+
18+
const COMPRESS_TYPE_LOCAL_VARIABLES = 'local_variables';
19+
const COMPRESS_TYPE_OBJECT_VARIABLES = 'object_variables';
20+
const COMPRESS_TYPE_OBJECT_METHODS = 'object_methods';
21+
22+
protected $content = '';
23+
24+
protected $whitespaces = false;
25+
26+
protected $comments = false;
27+
28+
protected $compress_types = [
29+
'local_variables' => false,
30+
'object_variables' => false,
31+
'object_methods' => false,
32+
];
33+
34+
protected $exclude_names = [
35+
'local_variables' => [
36+
'this' => 'this',/** use in Object method scope **/
37+
'GLOBALS' => 'GLOBALS',
38+
'_GET' => '_GET',
39+
'_POST' => '_POST',
40+
'_FILES' => '_FILES',
41+
'_COOKIE' => '_COOKIE',
42+
'_SESSION' => '_SESSION',
43+
'_SERVER' => '_SERVER',
44+
'http_response_header' => 'http_response_header',
45+
'php_errormsg' => 'php_errormsg',
46+
],
47+
'object_variables' => [
48+
'this' => 'this',
49+
],
50+
51+
'object_methods' => [
52+
//Magic
53+
'this' => 'this',
54+
'__construct' => '__construct',
55+
'__destruct' => '__destruct',
56+
'__call' => '__call',
57+
'__callStatic' => '__callStatic',
58+
'__get' => '__get',
59+
'__set' => '__set',
60+
'__isset' => '__isset',
61+
'__unset' => '__unset',
62+
'__sleep' => '__sleep',
63+
'__wakeup' => '__wakeup',
64+
'__toString' => '__toString',
65+
'__invoke' => '__invoke',
66+
'__set_state' => '__set_state',
67+
'__clone' => '__clone',
68+
'__debugInfo' => '__debugInfo',
69+
// only php 7
70+
'do' => 'do',
71+
'as' => 'as',
72+
]
73+
];
74+
/**
75+
* @var NodeTraverser
76+
*/
77+
protected $traverser = null;
78+
79+
public function __construct($data = [])
80+
{
81+
82+
}
83+
84+
public function compress()
85+
{
86+
try {
87+
$statements = $this->getStatements();
88+
$traverser = $this->getTraverser();
89+
$traverser->addVisitor(new GlobalVisitor($this->getCompressSettings()));
90+
$new_statements = $traverser->traverse($statements);
91+
$this->content = $this->getCompiller()->prettyPrint($new_statements);
92+
} catch (Error $e) {
93+
echo 'Parse Error: ', $e->getMessage();
94+
}
95+
}
96+
97+
public function setExcludeNames($type = '', array $exclude_names) {
98+
if(isset($this->compress_types[$type])) {
99+
$this->exclude_names[$type] += $exclude_names;
100+
}
101+
}
102+
protected function getCompressSettings()
103+
{
104+
$settings = [];
105+
foreach ($this->compress_types as $type => $enable) {
106+
$settings[$type] = [
107+
'enable' => $enable,
108+
'exclude_names' => $this->exclude_names[$type]
109+
];
110+
}
111+
return $settings;
112+
}
113+
public function getContent()
114+
{
115+
return $this->content;
116+
}
117+
118+
public function getStatements($perfer = ParserFactory::PREFER_PHP5)
119+
{
120+
$parser = (new ParserFactory)->create($perfer);
121+
122+
return $parser->parse($this->content);
123+
}
124+
125+
126+
/**
127+
* @return PrettyPrinter\Standard
128+
*/
129+
protected function getCompiller()
130+
{
131+
return new PrettyPrinter\Standard;
132+
}
133+
134+
protected function getTraverser()
135+
{
136+
return new \PhpParser\NodeTraverser();
137+
}
138+
139+
public function removeWhitespaces()
140+
{
141+
$this->whitespaces = true;
142+
}
143+
144+
public function removeComments()
145+
{
146+
$this->comments = true;
147+
}
148+
149+
/**********************
150+
* settings
151+
**********************/
152+
public function compressLocalVariablesName()
153+
{
154+
$this->compress_types['local_variables'] = true;
155+
}
156+
157+
public function compressObjectsVariablesName()
158+
{
159+
$this->compress_types['object_variables'] = true;
160+
}
161+
162+
public function compressObjectsMethodsName()
163+
{
164+
$this->compress_types['object_methods'] = true;
165+
}
166+
167+
168+
public function setContentByFile($file)
169+
{
170+
if (false === file_exists($file)) {
171+
throw new \Exception('Not found file '.$file);
172+
}
173+
return $this->content = file_get_contents($file);
174+
}
175+
176+
public function setContentByCode($code)
177+
{
178+
$this->content = $code;
179+
}
180+
181+
public function parseBlock($blockCode)
182+
{
183+
$this->content = '<?php ' . $blockCode;
184+
}
185+
186+
187+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Genasyst\phpCompressor\NamesCompressor;
4+
5+
use Genasyst\phpCompressor\NamesRegistry\NamesRegistry;
6+
use Genasyst\phpCompressor\Utils\StringGenerator;
7+
8+
class NamesCompressor
9+
{
10+
11+
protected $registry = null;
12+
13+
protected $data = [];
14+
15+
public function __construct(NamesRegistry $registry)
16+
{
17+
$this->init($registry);
18+
}
19+
20+
protected function init(NamesRegistry $registry)
21+
{
22+
$names = $registry->getAll();
23+
arsort($names);
24+
$generator = new StringGenerator();
25+
$short_names = $generator->range('a', 'zzzzz', count($names), $registry->getExcludeNames());
26+
$this->data = array_combine(array_keys($names), $short_names);
27+
}
28+
29+
public function getShortName($name)
30+
{
31+
return $this->data[$name];
32+
}
33+
34+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Genasyst\phpCompressor\NamesRegistry;
4+
5+
class ClassVariables
6+
{
7+
protected static $instance;
8+
9+
protected static $data = [];
10+
11+
protected $class = '';
12+
13+
public function __construct($class)
14+
{
15+
$this->class = $class;
16+
}
17+
18+
public function set($name)
19+
{
20+
if ($this->has($name)) {
21+
static::$data[$this->class][$name]++;
22+
} else {
23+
static::$data[$this->class][$name] = 1;
24+
}
25+
}
26+
27+
protected function hasClass($class)
28+
{
29+
return array_key_exists($class, static::$data);
30+
}
31+
32+
public function has($name)
33+
{
34+
return ($this->hasClass($this->class) && array_key_exists($name, static::$data[$this->class][$name]));
35+
}
36+
37+
public function get($name)
38+
{
39+
if ($this->has($name)) {
40+
return static::$data[$this->class][$name];
41+
}
42+
43+
return 0;
44+
}
45+
46+
public function getAll($class = null)
47+
{
48+
if ($class === false) {
49+
return static::$data;
50+
}
51+
if ($class === null) {
52+
return $this->hasClass($this->class) ?
53+
static::$data[$this->class] : [];
54+
}
55+
56+
return $this->hasClass($class) ?
57+
static::$data[$class] : [];
58+
}
59+
}

src/NamesRegistry/Constants.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Genasyst\phpCompressor\NamesRegistry;
4+
5+
class Constants extends NamesRegistry
6+
{
7+
protected static $instance;
8+
}

src/NamesRegistry/Functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Genasyst\phpCompressor\NamesRegistry;
4+
5+
class Functions extends NamesRegistry
6+
{
7+
protected static $instance;
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Genasyst\phpCompressor\NamesRegistry;
4+
5+
class LocalVariables extends NamesRegistry
6+
{
7+
protected static $instance;
8+
9+
protected $exclude_names = [
10+
'this' => 'this',
11+
'GLOBALS' => 'GLOBALS',
12+
'_GET' => '_GET',
13+
'_POST' => '_POST',
14+
'_FILES' => '_FILES',
15+
'_COOKIE' => '_COOKIE',
16+
'_SESSION' => '_SESSION',
17+
'_SERVER' => '_SERVER',
18+
'http_response_header' => 'http_response_header',
19+
'php_errormsg' => 'php_errormsg',
20+
];
21+
22+
23+
}

0 commit comments

Comments
 (0)