Skip to content

Commit

Permalink
Improve finder. New test script
Browse files Browse the repository at this point in the history
Global variable not always $GLOBALS
  • Loading branch information
ganlvtech committed Mar 4, 2019
1 parent 703517d commit 451efd7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 23 deletions.
11 changes: 7 additions & 4 deletions src/AutoDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AutoDecoder
{
protected $ast;
protected $globalVarName;
protected $globalVarKey;
protected $delimiter;
protected $data;
protected $start;
Expand All @@ -64,6 +65,7 @@ public function findAndRemoveGlobalVariableName()
$traverser->addVisitor($nodeVisitor);
$this->ast = $traverser->traverse($this->ast);
$this->globalVarName = $nodeVisitor->globalVarName;
$this->globalVarKey = $nodeVisitor->globalVarKey;
$this->delimiter = $nodeVisitor->delimiter;
$this->data = $nodeVisitor->data;
$this->start = $nodeVisitor->start;
Expand All @@ -73,7 +75,7 @@ public function findAndRemoveGlobalVariableName()

public function removeDefineGlobalVariableName()
{
$nodeVisitor = new RemoveDefineGlobalVariableNameNodeVisitor($this->globalVarName);
$nodeVisitor = new RemoveDefineGlobalVariableNameNodeVisitor($this->globalVarKey);
$traverser = new NodeTraverser();
$traverser->addVisitor($nodeVisitor);
$this->ast = $traverser->traverse($this->ast);
Expand All @@ -92,7 +94,7 @@ public function removeUnusedConstFetchNodeVisitor()

public function replaceGlobalString()
{
$nodeVisitor = new GlobalStringNodeVisitor($this->globalVarName, $this->stringArray);
$nodeVisitor = new GlobalStringNodeVisitor($this->globalVarName, $this->globalVarKey, $this->stringArray);
$traverser = new NodeTraverser();
$traverser->addVisitor($nodeVisitor);
$this->ast = $traverser->traverse($this->ast);
Expand All @@ -102,10 +104,11 @@ public function replaceGlobalString()
public function replaceFunctionLikeGlobalString()
{
$globalVarName = $this->globalVarName;
$globalVarKey = $this->globalVarKey;
$stringArray = $this->stringArray;
$nodeVisitor = new FunctionLikeNodeVisitor(function ($node) use ($globalVarName, $stringArray) {
$nodeVisitor = new FunctionLikeNodeVisitor(function ($node) use ($globalVarName, $globalVarKey, $stringArray) {
/** @var $node \PhpParser\Node\Stmt\Function_ */
$nodeVisitor = new FunctionGlobalStringNodeVisitor($globalVarName, $stringArray);
$nodeVisitor = new FunctionGlobalStringNodeVisitor($globalVarName, $globalVarKey, $stringArray);
$traverser = new NodeTraverser();
$traverser->addVisitor($nodeVisitor);
$node->stmts = $traverser->traverse($node->stmts);
Expand Down
1 change: 0 additions & 1 deletion src/NodeVisitors/BeautifyNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function leaveNode(Node $node)
|| $node instanceof Node\Expr\MethodCall)
&& $node->name instanceof Node\Scalar\String_) {
$node->name = new Node\Name($node->name->value);

} elseif ($node instanceof Node\Expr\New_
&& $node->class instanceof Node\Scalar\String_) {
$node->class = new Node\Name($node->class->value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class FindAndRemoveGlobalVariableNameNodeVisitor extends NodeVisitorAbstract
{
public $globalVarName;
public $globalVarKey;
public $delimiter;
public $data;
public $start;
Expand All @@ -20,7 +21,6 @@ public function leaveNode(Node $node)
&& $node->expr instanceof \PhpParser\Node\Expr\Assign
&& $node->expr->var instanceof \PhpParser\Node\Expr\ArrayDimFetch
&& $node->expr->var->var instanceof \PhpParser\Node\Expr\Variable
&& $node->expr->var->var->name === 'GLOBALS'
&& $node->expr->var->dim instanceof \PhpParser\Node\Expr\ConstFetch
&& $node->expr->var->dim->name instanceof \PhpParser\Node\Name
&& count($node->expr->var->dim->name->parts) === 1
Expand Down Expand Up @@ -63,7 +63,8 @@ public function leaveNode(Node $node)
&& $node->expr->expr->args[1]->byRef === false
&& $node->expr->expr->args[1]->unpack === false
) {
$this->globalVarName = $node->expr->var->dim->name->parts[0];
$this->globalVarName = $node->expr->var->var->name;
$this->globalVarKey = $node->expr->var->dim->name->parts[0];
$this->delimiter = $node->expr->expr->args[0]->value->value;
$this->data = $node->expr->expr->args[1]->value->args[0]->value->args[0]->value->value;
$this->start = $node->expr->expr->args[1]->value->args[0]->value->args[1]->value->value;
Expand Down
8 changes: 5 additions & 3 deletions src/NodeVisitors/FunctionGlobalStringNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class FunctionGlobalStringNodeVisitor extends NodeVisitorAbstract
{
public $localVarName;
public $globalVarName;
public $globalVarKey;
public $stringArray;

public function __construct($globalVarName, $stringArray)
public function __construct($globalVarName, $globalVarKey, $stringArray)
{
$this->globalVarName = $globalVarName;
$this->globalVarKey = $globalVarKey;
$this->stringArray = $stringArray;
}

Expand All @@ -25,10 +27,10 @@ public function leaveNode(Node $node)
&& $node->expr->var instanceof Node\Expr\Variable
&& $node->expr->expr instanceof Node\Expr\ArrayDimFetch
&& $node->expr->expr->var instanceof Node\Expr\Variable
&& $node->expr->expr->var->name === 'GLOBALS'
&& $node->expr->expr->var->name === $this->globalVarName
&& $node->expr->expr->dim instanceof Node\Expr\ConstFetch
&& $node->expr->expr->dim->name instanceof Node\Name
&& $node->expr->expr->dim->name->parts[0] === $this->globalVarName
&& $node->expr->expr->dim->name->parts[0] === $this->globalVarKey
) {
$this->localVarName = $node->expr->var->name;
return NodeTraverser::REMOVE_NODE;
Expand Down
8 changes: 5 additions & 3 deletions src/NodeVisitors/GlobalStringNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
class GlobalStringNodeVisitor extends NodeVisitorAbstract
{
public $globalVarName;
public $globalVarKey;
public $stringArray;

public function __construct($globalVarName, $stringArray)
public function __construct($globalVarName, $globalVarKey, $stringArray)
{
$this->globalVarName = $globalVarName;
$this->globalVarKey = $globalVarKey;
$this->stringArray = $stringArray;
}

Expand All @@ -21,10 +23,10 @@ public function leaveNode(Node $node)
if ($node instanceof Node\Expr\ArrayDimFetch
&& $node->var instanceof Node\Expr\ArrayDimFetch
&& $node->var->var instanceof Node\Expr\Variable
&& $node->var->var->name === 'GLOBALS'
&& $node->var->var->name === $this->globalVarName
&& $node->var->dim instanceof Node\Expr\ConstFetch
&& $node->var->dim->name instanceof Node\Name
&& $node->var->dim->name->parts[0] === $this->globalVarName
&& $node->var->dim->name->parts[0] === $this->globalVarKey
&& $node->dim instanceof Node\Scalar\LNumber
) {
return new Node\Scalar\String_($this->stringArray[$node->dim->value]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

class RemoveDefineGlobalVariableNameNodeVisitor extends NodeVisitorAbstract
{
public $globalVarName;
public $globalVarKey;

public function __construct($globalVarName)
public function __construct($globalVarKey)
{
$this->globalVarName = $globalVarName;
$this->globalVarKey = $globalVarKey;
}

public function leaveNode(Node $node)
Expand All @@ -25,7 +25,7 @@ public function leaveNode(Node $node)
&& count($node->expr->args) === 2
&& $node->expr->args[0] instanceof \PhpParser\Node\Arg
&& $node->expr->args[0]->value instanceof \PhpParser\Node\Scalar\String_
&& $node->expr->args[0]->value->value === $this->globalVarName
&& $node->expr->args[0]->value->value === $this->globalVarKey
&& $node->expr->args[0]->byRef === false
&& $node->expr->args[0]->unpack === false
&& $node->expr->args[1] instanceof \PhpParser\Node\Arg
Expand Down
Binary file added tests/assets/index.php
Binary file not shown.
6 changes: 0 additions & 6 deletions tests/index.php

This file was deleted.

11 changes: 11 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

error_reporting(E_ALL);

$code = file_get_contents(__DIR__ . '/assets/admin.php');
$code = \Ganlv\EnphpDecoder\AutoDecoder::decode($code);

$code = file_get_contents(__DIR__ . '/assets/index.php');
$code = \Ganlv\EnphpDecoder\AutoDecoder::decode($code);

0 comments on commit 451efd7

Please sign in to comment.