-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCli.php
More file actions
343 lines (309 loc) · 10.6 KB
/
Copy pathCli.php
File metadata and controls
343 lines (309 loc) · 10.6 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
//declare(strict_types=1);
namespace cryodrift\shell;
use cryodrift\fw\cli\ParamFile;
use cryodrift\fw\Config;
use cryodrift\fw\Context;
use cryodrift\fw\Core;
use cryodrift\fw\interface\Handler;
use cryodrift\fw\Main;
use cryodrift\fw\trait\CliHandler;
use cryodrift\fw\trait\CmdRunner;
class Cli implements Handler
{
use CliHandler;
use CmdRunner;
public function __construct(private readonly string $browser, private readonly string $browserprofile, private readonly string $agent)
{
}
public function handle(Context $ctx): Context
{
$ctx->response()->setStatusFinal();
return $this->handleCli($ctx);
}
/**
* @cli undupe remove duplicates from lines
* @cli param: -file (get from pipe or filename)
*/
protected function undupe(ParamFile $file): string
{
$out = '';
if ((string)$file) {
$parts = array_flip($this->preparedLines((string)$file));
$parts = array_flip($parts);
$out = trim(implode(PHP_EOL, $parts));
}
return $out;
}
/**
* @cli sort lines
* @cli param: -file (get from pipe or filename)
*/
protected function sort(ParamFile $file): string
{
$out = '';
if ($file->value) {
$lines = $this->preparedLines((string)$file);
if ($lines) {
sort($lines);
$out = trim(implode(PHP_EOL, $lines));
}
}
return $out;
}
/**
* @cli Group Lines by Tokens
* @cli param: -file (pipe from stdin or filename)
*/
protected function group(ParamFile $file): string
{
$lines = self::preparedLines((string)$file);
$out = [];
$tree = [];
foreach ($lines as $line) {
$out[$line] = self::extractSpacedParts(self::getTokens(str_replace('//', '/ /', $line)));
}
$head =& $tree[];
foreach ($out as $command => $cmdparts) {
$next =& $head;
foreach ($cmdparts as $cmdpart) {
$next =& $next[$cmdpart];
}
$next[] = $command;
}
return self::printTree($tree);
}
/**
* @cli Replace content in a file or piped data
* @cli example: dir | php /shell/cli replace "searchstring" "replacestring" -file
* @cli example: php index.php /shell replace -search="searchstring" -replace="replacestring" -file="myfile.txt"
* @cli -file (stdin or pathname)
* @cli [-search] (searchstring)
* @cli [-replace] (replacestring)
*/
protected function replace(Context $ctx, ParamFile $file, string $search = '', string $replace = ''): string
{
if ($search && $replace) {
} else {
$search = $ctx->request()->getArgAfter('replace');
$replace = $ctx->request()->getArgAfter($search, 'replace');
}
$data = str_replace($search, $replace, $file);
if ($file->filename) {
Core::fileWrite($file->filename . '.new', $data);
}
return $data;
}
/**
* @cli Take a screenshot of the web page using Chrome
* @cli use browser to startup the userprofile and get rid of chrome dialogs
* @cli param: -url="" url or route
* @cli param: [-local=true] url is local route or external url
* @cli param: [-small=false]
* @cli param: [-open=false] open the browser in non headless mode to set the cookie
* @cli param: [-outfile=""] out filename without fext and path
*/
protected function screenshot(string $url, bool $local = true, bool $small = false, bool $open = false, string $outfile = '', bool $proc = false): array
{
$out = [];
if ($open) {
$chromeCmd = [$this->browser];
// $chromeCmd[] = '--disable-application-cache';
$chromeCmd[] = '--user-data-dir=' . $this->browserprofile;
$chromeCmd[] = $url;
[$returnCode, $null] = $this->runCmd($chromeCmd, null, true);
if ($returnCode !== 0) {
$out['step'] = 'browser';
$out['command'] = implode(' ', $chromeCmd);
$out['return_code'] = $returnCode;
} else {
$out[] = 'launched browser';
}
} else {
// Determine sizes and paths
$sizeArg = $small ? '--window-size=800,600' : '--window-size=1920,1200';
$tmpPath = Main::$rootdir . Config::$datadir . Config::$logdir . 'screenshot.html';
if (!$outfile) {
$outfile = 'sreenshot';
}
$imgPath = Main::$rootdir . $outfile . '.jpg';
// Prepare target URL (either local rendered file or remote URL)
$targetUrl = $url;
if ($local) {
// Render the route locally: php index.php <url> -> tmp.html
$phpCmd = ['php', 'index.php', $url];
[$phpCode, $html] = $this->runCmdCapture($phpCmd, null, true);
if ($phpCode !== 0) {
$out['step'] = 'render';
$out['command'] = implode(' ', $phpCmd);
$out['return_code'] = $phpCode;
return $out;
}
Core::fileWrite($tmpPath, (string)$html);
// file:/// URL for Chrome
$targetUrl = 'file:///' . str_replace('\\', '/', $tmpPath);
}
// Build Chrome command as array
$chromeCmd = [
$this->browser,
'--headless=new',
'--disable-application-cache',
$sizeArg,
'--screenshot=' . $imgPath,
];
$chromeCmd[] = '--user-data-dir=' . $this->browserprofile;
$chromeCmd[] = $targetUrl;
if ($proc) {
// $cmd = $this->prepareShellWindows($chromeCmd);
$descriptors = [
0 => ['pipe', 'r'],
1 => ['file', 'php://stdout', 'w'],
2 => ['file', 'php://stderr', 'w'],
];
// if (Core::isUnix()) {
// $shell = ['/bin/sh', '-c', $cmd];
// } else {
// $shell = ['cmd.exe', '/C', $cmd];
// }
// Core::echo(__METHOD__, $shell, $chromeCmd);
$proc = proc_open($chromeCmd, $descriptors, $pipes);
proc_close($proc);
} else {
[$returnCode, $null] = $this->runCmd($chromeCmd, null, true);
if ($returnCode !== 0) {
$out['step'] = 'screenshot';
$out['command'] = implode(' ', $chromeCmd);
$out['return_code'] = $returnCode;
} else {
$out[] = 'saved ' . $outfile . '.jpg';
}
}
}
return $out;
}
/**
* @cli capture html of a url
*
*/
protected function dumpdom(string $url): string
{
$html = Core::fileReadOnce($url, false, [
'http' => [
'method' => 'GET',
// 'header' => "Header-Name: value\r\nAnother: value\r\n", // or implode("\r\n", $headers)."\r\n"
// 'content' => string, // request body for POST/PUT
'timeout' => 1,
'ignore_errors' => true,
'user_agent' => $this->agent,
'protocol_version' => 1.1,
'follow_location' => 1,
'max_redirects' => 10,
],
]);
return $html;
}
/**
* @cli Simple url benchmark
*/
protected function bench(string $url, ParamFile $cookie, int $n = 10): array
{
$out = [];
$out['start'] = Core::time();
$headers = [];
$headers[] = 'Cookie: ' . $cookie->value;
$headers[] = 'Connection: keep-alive';
for ($a = 0, $b = $n; $a < $n; $a++) {
Core::echo(__METHOD__, 'beg', $a);
Core::fileReadOnce($url, true, [
'http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers) . "\r\n",
'user_agent' => $this->agent,
'timeout' => 1,
'ignore_errors' => true,
'protocol_version' => 1.1,
'follow_location' => 0,
],
]);
Core::echo(__METHOD__, 'end', $a);
}
$out['end'] = Core::time();
return $out;
}
/**
* @cli Jsonstring to Arraystring
*/
protected function json(ParamFile $in, string $outfile = ''): array
{
$out = Core::jsonRead($in->value);
if ($outfile) {
Core::fileWrite($outfile, Core::toLog($out));
}
return $out;
}
private static function printTree(array $data): string
{
static $level = 1;
$out = '';
foreach ($data as $key => $value) {
if ($key) {
$out .= str_pad($key, strlen($key) + $level, ' ', STR_PAD_LEFT) . PHP_EOL;
}
if (is_array($value)) {
$level++;
$out .= self::printTree($value);
$level--;
} else {
$out .= str_pad($value, strlen($value) + $level, ' ', STR_PAD_LEFT) . PHP_EOL;
}
}
return $out;
}
private static function extractSpacedParts(\Generator $tokens): array
{
$out = [];
$current = '';
foreach ($tokens as $token) {
switch (true) {
case $token->is(' '):
$out[] = $current;
$current = '';
break;
default:
$current .= $token->text;
}
}
$out[] = $current;
return $out;
}
private static function preparedLines(string $data): array
{
$data = str_replace("\r", '', $data);
$parts = explode("\n", $data);
return array_map('trim', $parts);
}
/**
* @param $pathname
* @return \Generator
*/
private static function getTokens($pathname): iterable
{
if (file_exists($pathname)) {
$file = file_get_contents($pathname);
} else {
$file = $pathname;
}
$tokens = \PhpToken::tokenize('<?php' . PHP_EOL . $file);
foreach ($tokens as $token) {
if (!$token->is(T_OPEN_TAG)) {
yield $token;
}
}
}
private function prepareShellWindows(array $parts): string
{
return implode(' ', array_map(function ($p) {
return '"' . str_replace('"', '\\"', $p) . '"';
}, $parts));
}
}