Skip to content

Commit 451efd7

Browse files
committed
Improve finder. New test script
Global variable not always $GLOBALS
1 parent 703517d commit 451efd7

9 files changed

+35
-23
lines changed

src/AutoDecoder.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class AutoDecoder
3939
{
4040
protected $ast;
4141
protected $globalVarName;
42+
protected $globalVarKey;
4243
protected $delimiter;
4344
protected $data;
4445
protected $start;
@@ -64,6 +65,7 @@ public function findAndRemoveGlobalVariableName()
6465
$traverser->addVisitor($nodeVisitor);
6566
$this->ast = $traverser->traverse($this->ast);
6667
$this->globalVarName = $nodeVisitor->globalVarName;
68+
$this->globalVarKey = $nodeVisitor->globalVarKey;
6769
$this->delimiter = $nodeVisitor->delimiter;
6870
$this->data = $nodeVisitor->data;
6971
$this->start = $nodeVisitor->start;
@@ -73,7 +75,7 @@ public function findAndRemoveGlobalVariableName()
7375

7476
public function removeDefineGlobalVariableName()
7577
{
76-
$nodeVisitor = new RemoveDefineGlobalVariableNameNodeVisitor($this->globalVarName);
78+
$nodeVisitor = new RemoveDefineGlobalVariableNameNodeVisitor($this->globalVarKey);
7779
$traverser = new NodeTraverser();
7880
$traverser->addVisitor($nodeVisitor);
7981
$this->ast = $traverser->traverse($this->ast);
@@ -92,7 +94,7 @@ public function removeUnusedConstFetchNodeVisitor()
9294

9395
public function replaceGlobalString()
9496
{
95-
$nodeVisitor = new GlobalStringNodeVisitor($this->globalVarName, $this->stringArray);
97+
$nodeVisitor = new GlobalStringNodeVisitor($this->globalVarName, $this->globalVarKey, $this->stringArray);
9698
$traverser = new NodeTraverser();
9799
$traverser->addVisitor($nodeVisitor);
98100
$this->ast = $traverser->traverse($this->ast);
@@ -102,10 +104,11 @@ public function replaceGlobalString()
102104
public function replaceFunctionLikeGlobalString()
103105
{
104106
$globalVarName = $this->globalVarName;
107+
$globalVarKey = $this->globalVarKey;
105108
$stringArray = $this->stringArray;
106-
$nodeVisitor = new FunctionLikeNodeVisitor(function ($node) use ($globalVarName, $stringArray) {
109+
$nodeVisitor = new FunctionLikeNodeVisitor(function ($node) use ($globalVarName, $globalVarKey, $stringArray) {
107110
/** @var $node \PhpParser\Node\Stmt\Function_ */
108-
$nodeVisitor = new FunctionGlobalStringNodeVisitor($globalVarName, $stringArray);
111+
$nodeVisitor = new FunctionGlobalStringNodeVisitor($globalVarName, $globalVarKey, $stringArray);
109112
$traverser = new NodeTraverser();
110113
$traverser->addVisitor($nodeVisitor);
111114
$node->stmts = $traverser->traverse($node->stmts);

src/NodeVisitors/BeautifyNodeVisitor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public function leaveNode(Node $node)
1414
|| $node instanceof Node\Expr\MethodCall)
1515
&& $node->name instanceof Node\Scalar\String_) {
1616
$node->name = new Node\Name($node->name->value);
17-
1817
} elseif ($node instanceof Node\Expr\New_
1918
&& $node->class instanceof Node\Scalar\String_) {
2019
$node->class = new Node\Name($node->class->value);

src/NodeVisitors/FindAndRemoveGlobalVariableNameNodeVisitor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class FindAndRemoveGlobalVariableNameNodeVisitor extends NodeVisitorAbstract
1010
{
1111
public $globalVarName;
12+
public $globalVarKey;
1213
public $delimiter;
1314
public $data;
1415
public $start;
@@ -20,7 +21,6 @@ public function leaveNode(Node $node)
2021
&& $node->expr instanceof \PhpParser\Node\Expr\Assign
2122
&& $node->expr->var instanceof \PhpParser\Node\Expr\ArrayDimFetch
2223
&& $node->expr->var->var instanceof \PhpParser\Node\Expr\Variable
23-
&& $node->expr->var->var->name === 'GLOBALS'
2424
&& $node->expr->var->dim instanceof \PhpParser\Node\Expr\ConstFetch
2525
&& $node->expr->var->dim->name instanceof \PhpParser\Node\Name
2626
&& count($node->expr->var->dim->name->parts) === 1
@@ -63,7 +63,8 @@ public function leaveNode(Node $node)
6363
&& $node->expr->expr->args[1]->byRef === false
6464
&& $node->expr->expr->args[1]->unpack === false
6565
) {
66-
$this->globalVarName = $node->expr->var->dim->name->parts[0];
66+
$this->globalVarName = $node->expr->var->var->name;
67+
$this->globalVarKey = $node->expr->var->dim->name->parts[0];
6768
$this->delimiter = $node->expr->expr->args[0]->value->value;
6869
$this->data = $node->expr->expr->args[1]->value->args[0]->value->args[0]->value->value;
6970
$this->start = $node->expr->expr->args[1]->value->args[0]->value->args[1]->value->value;

src/NodeVisitors/FunctionGlobalStringNodeVisitor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ class FunctionGlobalStringNodeVisitor extends NodeVisitorAbstract
1010
{
1111
public $localVarName;
1212
public $globalVarName;
13+
public $globalVarKey;
1314
public $stringArray;
1415

15-
public function __construct($globalVarName, $stringArray)
16+
public function __construct($globalVarName, $globalVarKey, $stringArray)
1617
{
1718
$this->globalVarName = $globalVarName;
19+
$this->globalVarKey = $globalVarKey;
1820
$this->stringArray = $stringArray;
1921
}
2022

@@ -25,10 +27,10 @@ public function leaveNode(Node $node)
2527
&& $node->expr->var instanceof Node\Expr\Variable
2628
&& $node->expr->expr instanceof Node\Expr\ArrayDimFetch
2729
&& $node->expr->expr->var instanceof Node\Expr\Variable
28-
&& $node->expr->expr->var->name === 'GLOBALS'
30+
&& $node->expr->expr->var->name === $this->globalVarName
2931
&& $node->expr->expr->dim instanceof Node\Expr\ConstFetch
3032
&& $node->expr->expr->dim->name instanceof Node\Name
31-
&& $node->expr->expr->dim->name->parts[0] === $this->globalVarName
33+
&& $node->expr->expr->dim->name->parts[0] === $this->globalVarKey
3234
) {
3335
$this->localVarName = $node->expr->var->name;
3436
return NodeTraverser::REMOVE_NODE;

src/NodeVisitors/GlobalStringNodeVisitor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
class GlobalStringNodeVisitor extends NodeVisitorAbstract
99
{
1010
public $globalVarName;
11+
public $globalVarKey;
1112
public $stringArray;
1213

13-
public function __construct($globalVarName, $stringArray)
14+
public function __construct($globalVarName, $globalVarKey, $stringArray)
1415
{
1516
$this->globalVarName = $globalVarName;
17+
$this->globalVarKey = $globalVarKey;
1618
$this->stringArray = $stringArray;
1719
}
1820

@@ -21,10 +23,10 @@ public function leaveNode(Node $node)
2123
if ($node instanceof Node\Expr\ArrayDimFetch
2224
&& $node->var instanceof Node\Expr\ArrayDimFetch
2325
&& $node->var->var instanceof Node\Expr\Variable
24-
&& $node->var->var->name === 'GLOBALS'
26+
&& $node->var->var->name === $this->globalVarName
2527
&& $node->var->dim instanceof Node\Expr\ConstFetch
2628
&& $node->var->dim->name instanceof Node\Name
27-
&& $node->var->dim->name->parts[0] === $this->globalVarName
29+
&& $node->var->dim->name->parts[0] === $this->globalVarKey
2830
&& $node->dim instanceof Node\Scalar\LNumber
2931
) {
3032
return new Node\Scalar\String_($this->stringArray[$node->dim->value]);

src/NodeVisitors/RemoveDefineGlobalVariableNameNodeVisitor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
class RemoveDefineGlobalVariableNameNodeVisitor extends NodeVisitorAbstract
1010
{
11-
public $globalVarName;
11+
public $globalVarKey;
1212

13-
public function __construct($globalVarName)
13+
public function __construct($globalVarKey)
1414
{
15-
$this->globalVarName = $globalVarName;
15+
$this->globalVarKey = $globalVarKey;
1616
}
1717

1818
public function leaveNode(Node $node)
@@ -25,7 +25,7 @@ public function leaveNode(Node $node)
2525
&& count($node->expr->args) === 2
2626
&& $node->expr->args[0] instanceof \PhpParser\Node\Arg
2727
&& $node->expr->args[0]->value instanceof \PhpParser\Node\Scalar\String_
28-
&& $node->expr->args[0]->value->value === $this->globalVarName
28+
&& $node->expr->args[0]->value->value === $this->globalVarKey
2929
&& $node->expr->args[0]->byRef === false
3030
&& $node->expr->args[0]->unpack === false
3131
&& $node->expr->args[1] instanceof \PhpParser\Node\Arg

tests/assets/index.php

7.82 KB
Binary file not shown.

tests/index.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/test.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
error_reporting(E_ALL);
6+
7+
$code = file_get_contents(__DIR__ . '/assets/admin.php');
8+
$code = \Ganlv\EnphpDecoder\AutoDecoder::decode($code);
9+
10+
$code = file_get_contents(__DIR__ . '/assets/index.php');
11+
$code = \Ganlv\EnphpDecoder\AutoDecoder::decode($code);

0 commit comments

Comments
 (0)