FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
phpbench/lib/Remote/Payload.php at program-executor · phpbench/phpbench · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
phpbench
/
phpbench
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
132
Star
2k
Code
Issues
28
Pull requests
10
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
phpbench
/
lib
/
Remote
/
Payload.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
269 lines (218 loc) · 6.09 KB
Breadcrumbs
phpbench
/
lib
/
Remote
/
Payload.php
Copy path
File metadata and controls
269 lines (218 loc) · 6.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace
PhpBench
\
Remote
;
use
PhpBench
\
Remote
\
Exception
\
ScriptErrorException
;
use
RuntimeException
;
use
Symfony
\
Component
\
Process
\
Process
;
use
function
escapeshellarg
;
/**
* Class representing the context from which a script can be generated and executed by a PHP binary.
*/
class
Payload
{
public
const
FLAG_DISABLE_INI
=
'
-n
'
;
/**
* Path to script template.
*/
private
$
template
;
/**
* Wrapper for PHP binary, e.g. "blackfire".
*/
private
$
wrapper
;
/**
* Associative array of PHP INI settings.
*/
private
$
phpConfig
= [];
/**
* Path to PHP binary.
*/
private
$
phpPath
;
/**
* Tokens to substitute in the script template.
*/
private
$
tokens
= [];
/**
* @var bool
*/
private
$
disableIni
=
false
;
/**
* @var IniStringBuilder
*/
private
$
iniStringBuilder
;
/**
* @var ProcessFactoryInterface
*/
private
$
processFactory
;
/**
* @var float
*/
private
$
timeout
;
/**
* @var string
*/
private
$
scriptPath
;
/**
* @var bool
*/
private
$
scriptRemove
;
/**
* Create a new Payload object with the given script template.
* The template must be the path to a script template.
*
* @param array<string, mixed> $tokens
*/
public
function
__construct
(
string
$
template
,
array
$
tokens
= [],
?
string
$
phpPath
=
null
,
?
float
$
timeout
=
null
,
ProcessFactoryInterface
$
processFactory
=
null
,
string
$
scriptPath
=
null
,
bool
$
scriptRemove
=
false
) {
$
this
->
template
=
$
template
;
$
this
->
tokens
=
$
tokens
;
$
this
->
processFactory
=
$
processFactory
?:
new
ProcessFactory
();
$
this
->
iniStringBuilder
=
new
IniStringBuilder
();
$
this
->
timeout
=
$
timeout
;
$
this
->
phpPath
=
$
phpPath
?:
PHP_BINARY
;
$
this
->
scriptPath
=
$
scriptPath
;
$
this
->
scriptRemove
=
$
scriptRemove
;
}
public
function
setWrapper
(
$
wrapper
):
void
{
$
this
->
wrapper
=
$
wrapper
;
}
public
function
mergePhpConfig
(
array
$
phpConfig
):
void
{
$
this
->
phpConfig
=
array_merge
(
$
this
->
phpConfig
,
$
phpConfig
);
}
public
function
disableIni
():
void
{
$
this
->
disableIni
=
true
;
}
public
function
setPhpPath
(
string
$
phpPath
):
void
{
$
this
->
phpPath
=
$
phpPath
;
}
public
function
launch
():
array
{
$
script
=
$
this
->
readFile
();
$
script
=
$
this
->
replaceTokens
(
$
script
);
$
scriptPath
=
$
this
->
writeTempFile
(
$
script
);
$
commandLine
=
$
this
->
buildCommandLine
(
$
scriptPath
);
$
process
=
$
this
->
processFactory
->
create
(
$
commandLine
,
$
this
->
timeout
);
$
process
->
run
();
$
this
->
removeTmpFile
(
$
scriptPath
);
if
(
false
===
$
process
->
isSuccessful
()) {
throw
new
ScriptErrorException
(
sprintf
(
'
%s%s
'
,
$
process
->
getErrorOutput
(),
$
process
->
getOutput
()
));
}
return
$
this
->
decodeResults
(
$
process
);
}
private
function
getIniString
():
string
{
if
(
empty
(
$
this
->
phpConfig
)) {
return
''
;
}
return
$
this
->
iniStringBuilder
->
build
(
$
this
->
phpConfig
);
}
private
function
replaceTokens
(
string
$
templateBody
):
string
{
$
tokenSubs
= [];
foreach
(
$
this
->
tokens
as
$
key
=>
$
value
) {
$
tokenSubs
[
'
{{
'
.
$
key
.
'
}}
'
] =
$
value
;
}
return
str_replace
(
array_keys
(
$
tokenSubs
),
array_values
(
$
tokenSubs
),
$
templateBody
);
}
private
function
readFile
():
string
{
if
(!
file_exists
(
$
this
->
template
)) {
throw
new
\
RuntimeException
(
sprintf
(
'
Could not find script template "%s"
'
,
$
this
->
template
));
}
return
file_get_contents
(
$
this
->
template
);
}
private
function
writeTempFile
(
string
$
script
):
string
{
$
scriptPath
=
$
this
->
scriptPath
?
$
this
->
scriptPath
.
'
/
'
.
basename
(
$
this
->
template
) :
tempnam
(
sys_get_temp_dir
(),
'
PhpBench
'
);
if
(
false
===
$
scriptPath
) {
throw
new
RuntimeException
(
'
Could not generate temporary script name
'
);
}
(
function
(
string
$
directory
):
void
{
if
(
file_exists
(
$
directory
)) {
return
;
}
if
(@
mkdir
(
$
directory
,
0744
,
true
)) {
return
;
}
throw
new
RuntimeException
(
sprintf
(
'
Could not create directory "%s"
'
,
$
directory
));
})(
dirname
(
$
scriptPath
));
file_put_contents
(
$
scriptPath
,
$
script
);
return
$
scriptPath
;
}
private
function
buildCommandLine
(
string
$
scriptPath
):
string
{
$
arguments
= [];
if
(
$
this
->
wrapper
) {
$
arguments
[] =
$
this
->
wrapper
;
}
$
arguments
[] =
escapeshellarg
(
$
this
->
phpPath
);
if
(
true
===
$
this
->
disableIni
) {
$
arguments
[] =
self
::
FLAG_DISABLE_INI
;
}
$
arguments
[] =
$
this
->
getIniString
();
$
arguments
[] =
escapeshellarg
(
$
scriptPath
);
return
implode
(
'
'
,
$
arguments
);
}
private
function
removeTmpFile
(
string
$
scriptPath
):
void
{
if
(!
$
this
->
scriptRemove
) {
return
;
}
unlink
(
$
scriptPath
);
}
/**
* @return array<string, mixed>
*/
private
function
decodeResults
(
Process
$
process
):
array
{
$
output
=
$
process
->
getOutput
();
$
result
= @
unserialize
(
$
output
);
if
(
is_array
(
$
result
)) {
return
$
result
;
}
throw
new
\
RuntimeException
(
sprintf
(
'
Script "%s" did not return an array, got: %s
'
,
$
this
->
template
,
$
output
));
}
}
Back
|
FazBrowse Home
|
New Git URL