-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·133 lines (115 loc) · 4.54 KB
/
index.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
<?php error_reporting(E_ALL);
/**
* Use Many\Dev\Used temporary as a Webservice
* using PHPs dev-server
*
* ~$ cd ~/bin/many/get-used/www/used
* ~$ php -S localhost:8000
* ~$ firefox http://localhost:8000
*
* # Or, set an Alias in ~/.bash_aliases
*
* ~$ alias phpserver='firefox http://localhost:8000; php -S localhost:8000'
* # and run
* ~$ cd ~/bin/many/get-used/www/used
* ~$ phpserver
*/
/** shell_exec() extension is required */
if (!function_exists('shell_exec'))
exit('PHP shell_exec() extension is required<hr /><b>'.php_ini_loaded_file().'</b>');
/** @return string valid html */
function h() {return htmlspecialchars(...func_get_args());}
/**
* @var string $executable path, Aliases don't work here out of the box, so
* @var string $commentOut Comment existing use Keywords out
* @var array $getConfig Used config
* @var array $exec collects commandos ain an array ['php', '-S', 'localhost'] = 'php -S localhost'
* @var array $getable available $_GET-keys
* @var array $usedOptions arg options
*/
$executable = '~/bin/many/get-used/GetUsed.php';
$commentOut = 'false' !== ($_GET['comment_out'] ?? false) ? null : 'false';
$getConfig = json_decode(shell_exec("{$executable} -c"), true)['config'] ?? [];
$exec = ["{$executable} return=json"];
$getable = $getConfig['args'] ?? [];
$usedOptions = $getConfig['options'] ?? [];
/**
* @var string|null Set output content type, default: json
*/
$returnType = null;
if ('terminal' === ($_GET['return'] ?? false)) {
$returnType = 'terminal';
$exec[0] .= ' return=terminal';
unset($_GET['return']);
}
/**
* @var array prepare shell command and exec
*/
if ($_GET['file'] ?? null) {
// allowed parameters to args
foreach($getable as $key) {
if ($getCmd = ($_GET[$key] ?? null) AND is_string($getCmd)) {
if (in_array($getCmd, $usedOptions))
$exec[] = $getCmd;
else $exec[] = sprintf('%s=%s', $key, $getCmd);
}
}
// run cmd
$out = json_decode($execRun = shell_exec(implode(' ', $exec)), true);
if ($execRun AND (!$out OR $returnType))
$out['print'] = ($returnType ? null : "unexpected content\n\n") . print_r($execRun, true);
// check if -option is requested
foreach(array_keys($usedOptions) as $opt)
if ($out[$opt] ?? false) {
$out['print'] = json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
break;
}
}
/**
* @var mixed Link to File for VSCode
*/
$linkToFile = $_GET['file'] ?? null;
$explLink = $linkToFile ? explode(' ', $linkToFile) : $linkToFile;
if (($explLink[0] ?? null) AND !is_file($explLink[0]))
$linkToFile = null;
/**
* @var mixed Template Engin © 1999 eypsilon
*/
?><!DOCTYPE html>
<html><head><meta charset="utf-8" />
<title><?= h($linkToFile ?? 'Many\Dev\Used') ?> | local-dev-many-title</title>
<meta name="description" content="Many\Dev\Used - use Keywords generator" />
<link rel="icon" sizes="16x16" href="/assets/favicon.ico" />
<style><?= file_get_contents(dirname(__FILE__) . '/assets/style.css') ?></style>
</head><body>
<header>
<form action="" method="get">
<div>
<span>file</span>
<input type="text" name="file" value="<?= h($_GET['file'] ?? '') ?>" required />
<label title="Remove comments from use Keywords - '// '">
uncomment <input type="checkbox" name="comment_out" value="false" <?= 'false' === $commentOut ? 'checked' : null ?> />
</label>
<label title="Return Terminal Response">
trmnal <input type="checkbox" name="return" value="terminal" <?= $returnType ? 'checked' : null ?> />
</label>
</div>
<div>
<button type="submit">Get</button>
<?= $_GET ? '<a href="/" title="reset">✕</a>' : null ?>
<?= $linkToFile ? sprintf('<a href="vscode://file//%1$s" title="vscode %1$s">%1$s</a>', $linkToFile) : null ?>
</div>
</form>
</header>
<main>
<pre><?= ($out['print'] ?? null) ? trim(h($out['print'])) : "\n/**\n * Nothing to use\n */" ?></pre>
<pre><b>shell_exec(</b><?= "\n " . h(implode("\n ", explode(' ', implode(' ', $exec)))) . "\n" ?><b>)</b></pre>
<?= ($execRun ?? null) ? '<pre>' . h(implode(' ', $exec)) . '</pre>' : null ?>
</main>
<footer>
<h1>Many\Dev\Used</h1>
<nav><?php foreach($usedOptions as $key => $short)
$navs[] = sprintf($short === ($_GET['file'] ?? null) ? '<a>%1$s</a>' : '<a href="?file=%2$s">%1$s</a>', $key, $short);
?><?= implode(' / ', $navs ?? []) ?></nav>
</footer>
</body></html>