1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Scrambler.php |
4
|
|
|
* |
5
|
|
|
* @package Obfuscator |
6
|
|
|
* @subpackage NodeVisitor |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Naneau\Obfuscator\Node\Visitor; |
10
|
|
|
|
11
|
|
|
use Naneau\Obfuscator\StringScrambler; |
12
|
|
|
|
13
|
|
|
use PhpParser\NodeVisitorAbstract; |
14
|
|
|
use PhpParser\Node; |
15
|
|
|
|
16
|
|
|
use \InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Scrambler |
20
|
|
|
* |
21
|
|
|
* Base class for scrambling visitors |
22
|
|
|
* |
23
|
|
|
* @category Naneau |
24
|
|
|
* @package Obfuscator |
25
|
|
|
* @subpackage NodeVisitor |
26
|
|
|
*/ |
27
|
|
|
abstract class Scrambler extends NodeVisitorAbstract |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The string scrambler |
31
|
|
|
* |
32
|
|
|
* @var StringScrambler |
33
|
|
|
**/ |
34
|
|
|
private $scrambler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Variables to ignore |
38
|
|
|
* |
39
|
|
|
* @var string[] |
40
|
|
|
**/ |
41
|
|
|
private $ignore = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param StringScrambler $scrambler |
47
|
|
|
* @return void |
|
|
|
|
48
|
|
|
**/ |
49
|
|
|
public function __construct(StringScrambler $scrambler) |
50
|
|
|
{ |
51
|
|
|
$this->setScrambler($scrambler); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Scramble a property of a node |
56
|
|
|
* |
57
|
|
|
* @param Node $node |
58
|
|
|
* @param string $var property to scramble |
59
|
|
|
* @return Node |
60
|
|
|
**/ |
61
|
|
|
protected function scramble(Node $node, $var = 'name') |
62
|
|
|
{ |
63
|
|
|
// String/value to scramble |
64
|
|
|
$toScramble = $node->$var; |
65
|
|
|
|
66
|
|
|
// We ignore to scramble if it's not string (ex: a variable variable name) |
67
|
|
|
if (!is_string($toScramble)) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Make sure there's something to scramble |
72
|
|
|
if (strlen($toScramble) === 0) { |
73
|
|
|
throw new InvalidArgumentException(sprintf( |
74
|
|
|
'"%s" value empty for node, can not scramble', |
75
|
|
|
$var |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Should we ignore it? |
80
|
|
|
if (in_array($toScramble, $this->getIgnore())) { |
81
|
|
|
return $node; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Prefix with 'p' so we dont' start with an number |
85
|
|
|
$node->$var = $this->scrambleString($toScramble); |
86
|
|
|
|
87
|
|
|
// Return the node |
88
|
|
|
return $node; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Scramble a string |
93
|
|
|
* |
94
|
|
|
* @param string $string |
95
|
|
|
* @return string |
96
|
|
|
**/ |
97
|
|
|
protected function scrambleString($string) |
98
|
|
|
{ |
99
|
|
|
return 's' . $this->getScrambler()->scramble($string); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the string scrambler |
104
|
|
|
* |
105
|
|
|
* @return StringScrambler |
106
|
|
|
*/ |
107
|
|
|
public function getScrambler() |
108
|
|
|
{ |
109
|
|
|
return $this->scrambler; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set the string scrambler |
114
|
|
|
* |
115
|
|
|
* @param StringScrambler $scrambler |
116
|
|
|
* @return RenameParameter |
117
|
|
|
*/ |
118
|
|
|
public function setScrambler(StringScrambler $scrambler) |
119
|
|
|
{ |
120
|
|
|
$this->scrambler = $scrambler; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get variables to ignore |
127
|
|
|
* |
128
|
|
|
* @return string[] |
129
|
|
|
*/ |
130
|
|
|
public function getIgnore() |
131
|
|
|
{ |
132
|
|
|
return $this->ignore; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set variables to ignore |
137
|
|
|
* |
138
|
|
|
* @param string[] $ignore |
139
|
|
|
* @return parent |
140
|
|
|
*/ |
141
|
|
|
public function setIgnore(array $ignore) |
142
|
|
|
{ |
143
|
|
|
$this->ignore = $ignore; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add a variable name to ignore |
150
|
|
|
* |
151
|
|
|
* @param string|string[] $ignore |
152
|
|
|
* @return RenameParameterVisitor |
153
|
|
|
**/ |
154
|
|
|
public function addIgnore($ignore) |
155
|
|
|
{ |
156
|
|
|
if (is_string($ignore)) { |
157
|
|
|
$this->ignore = array_merge($this->ignore, array($ignore)); |
158
|
|
|
} else if (is_array($ignore)) { |
159
|
|
|
$this->ignore = array_merge($this->ignore, $ignore); |
|
|
|
|
160
|
|
|
} else { |
161
|
|
|
throw new InvalidArgumentException('Invalid ignore type passed'); |
162
|
|
|
} |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.