-
Notifications
You must be signed in to change notification settings - Fork 44
/
FileSystemCall.php
135 lines (117 loc) · 3.86 KB
/
FileSystemCall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace PHPDeobfuscator\Reducer\FuncCallReducer;
use PhpParser\Node\Expr\FuncCall;
use League\Flysystem\Filesystem;
use League\Flysystem\FileNotFoundException;
use PHPDeobfuscator\AttrName;
use PHPDeobfuscator\Exceptions;
use PHPDeobfuscator\Utils;
use PHPDeobfuscator\ValRef\ResourceValue;
class FileSystemCall implements FunctionReducer
{
private $fileSystem;
public function __construct(FileSystem $fileSystem)
{
$this->fileSystem = $fileSystem;
}
public function getSupportedNames()
{
return array(
'file_get_contents',
'file',
'fopen',
'fread',
'fwrite',
'fclose',
);
}
public function execute($name, array $args, FuncCall $node)
{
if (method_exists($this, $name . 'Prepare')) {
$args = call_user_func(array($this, $name . 'Prepare'), $args, $node);
} else {
$args = Utils::refsToValues($args);
}
return call_user_func_array(array($this, $name), $args);
}
private function file_get_contents($filename, $flags = 0, $context = null, $offset = -1, $maxlen = -1)
{
if (Utils::safeFileExists($this->fileSystem, $filename)) {
return Utils::scalarToNode($this->fileSystem->read($filename));
}
return null;
}
private function file($filename, $flags = 0, $context = null)
{
if (Utils::safeFileExists($this->fileSystem, $filename)) {
$content = $this->fileSystem->read($filename);
$lines = preg_split("/(\r\n|\r|\n)/", $content);
return Utils::scalarToNode($lines);
}
return null;
}
private function fopenPrepare(array $args, FuncCall $node)
{
return array_merge(array($node), Utils::refsToValues($args));
}
private function fopen(FuncCall $node, $filename, $mode, $use_include_path = false, $context = null)
{
if (strpos($mode, 'r') !== false) {
try {
$stream = $this->fileSystem->readStream($filename);
} catch (FileNotFoundException $e) {
return;
}
} elseif (strpos($mode, 'w') !== false) {
$stream = fopen('php://memory', 'w+b');
$this->fileSystem->writeStream($filename, $stream);
} else {
return;
}
$node->setAttribute(AttrName::VALUE, new ResourceValue($filename, $stream));
}
private function firstArgIsResource(array $args)
{
$newArgs = array();
foreach ($args as $i => $arg) {
if ($i == 0) {
if (!($arg instanceof ResourceValue)) {
throw new Exceptions\BadValueException("file handle is not a resource");
}
$newArgs[] = $arg;
} else {
$newArgs[] = $arg->getValue();
}
}
return $newArgs;
}
private function freadPrepare(array $args, FuncCall $node)
{
return $this->firstArgIsResource($args);
}
private function fread(ResourceValue $handle, $length)
{
return Utils::scalarToNode(fread($handle->getResource(), $length));
}
private function fwritePrepare(array $args, FuncCall $node)
{
return $this->firstArgIsResource($args);
}
private function fwrite(ResourceValue $handle, $string, $length = null)
{
if ($length !== null) {
fwrite($handle->getResource(), $string, $length);
} else {
fwrite($handle->getResource(), $string);
}
$this->fileSystem->writeStream($handle->getFilename(), $handle->getResource());
}
private function fclosePrepare(array $args, FuncCall $node)
{
return $this->firstArgIsResource($args);
}
private function fclose(ResourceValue $handle)
{
fclose($handle->getResource());
}
}