-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxc.php
212 lines (185 loc) · 6.89 KB
/
xc.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
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
<?php
/***
Disclaimer:
This is a better launcher search by keywords autocompletable, but it fully depends/relies on Termux-Launcher (https://github.com/amsitlab/termuxlauncher)
which generates a file on: /sdcard/termuxlauncher/.apps-launcher, and it will only work with this file.
*/
DEFINE("VERSION", "v0.94");
DEFINE("MAXARGS", 15);
DEFINE("APPLISTLOCATION", "/sdcard/termuxlauncher/.apps-launcher");
DEFINE("MAN",
"
MANUAL PAGES
");
require __DIR__ ."/../vendor/autoload.php";
class termuxCuteXCute {
// switches default
private $debug = false;
private $f = false;
////////
public $appList;
public $searchKeywords=array();
public function __construct(){
$appsTXT=trim(`(cat /sdcard/termuxlauncher/.apps-launcher | grep -B1 -e 'am start') || echo nok`);
if (($appsTXT)=="nok"){
$this->status = "NOK";
$this->error = APPLISTLOCATION." not found, perhaps you haven't installed termux-launcher";
return false;
}
$eachAppTXT=explode("--".PHP_EOL, $appsTXT);
foreach($eachAppTXT as $k=>$v){
$tmp = explode(PHP_EOL, $v);
$appinfo = explode("|", $tmp[0]); $appinfo = $this->atrim($appinfo);
$appname = strtolower($appinfo[1]); // this should be already in lowercase, but who knows
list($appcmd) = explode("&> /dev/null", trim($tmp[1])); //Extract the cmd, sorry for these ugly hacks, regex will do, but i'm so dumb on regex.
$txt[]=array("name"=>$appname, "cmd"=>$appcmd);
}
$this->appList = $txt;
// print_r($this->appList);exit;
$this->status = "OK";
// print_r($txt);
return true;
}
private function fixEncoding($i){
$t = array(
"\u00c0" =>"À", "\u00c1" =>"Á", "\u00c2" =>"Â", "\u00c3" =>"Ã", "\u00c4" =>"Ä", "\u00c5" =>"Å", "\u00c6" =>"Æ", "\u00c7" =>"Ç", "\u00c8" =>"È", "\u00c9" =>"É", "\u00ca" =>"Ê", "\u00cb" =>"Ë", "\u00cc" =>"Ì", "\u00cd" =>"Í", "\u00ce" =>"Î", "\u00cf" =>"Ï", "\u00d1" =>"Ñ", "\u00d2" =>"Ò", "\u00d3" =>"Ó", "\u00d4" =>"Ô", "\u00d5" =>"Õ", "\u00d6" =>"Ö", "\u00d8" =>"Ø", "\u00d9" =>"Ù", "\u00da" =>"Ú", "\u00db" =>"Û", "\u00dc" =>"Ü", "\u00dd" =>"Ý", "\u00df" =>"ß", "\u00e0" =>"à", "\u00e1" =>"á", "\u00e2" =>"â", "\u00e3" =>"ã", "\u00e4" =>"ä", "\u00e5" =>"å", "\u00e6" =>"æ", "\u00e7" =>"ç", "\u00e8" =>"è", "\u00e9" =>"é", "\u00ea" =>"ê", "\u00eb" =>"ë", "\u00ec" =>"ì", "\u00ed" =>"í", "\u00ee" =>"î", "\u00ef" =>"ï", "\u00f0" =>"ð", "\u00f1" =>"ñ", "\u00f2" =>"ò", "\u00f3" =>"ó", "\u00f4" =>"ô", "\u00f5" =>"õ", "\u00f6" =>"ö", "\u00f8" =>"ø", "\u00f9" =>"ù", "\u00fa" =>"ú", "\u00fb" =>"û", "\u00fc" =>"ü", "\u00fd" =>"ý", "\u00ff" =>"ÿ");
return strtr($i, $t);
}
private function arrSearch($hayStack, $needle){
$o = array_filter($hayStack, function($el) use ($needle) {
return ( stripos($el['name'], $needle) !== false || stripos($el['cmd'], $needle) !== false );
});
return $o;
}
private function trimapp($what){
$o= trim($what, "() \t\n\r\0\x0B");
return $o;
}
private function atrim($arr){
$arr = array_map(array($this, 'trimapp'), $arr);
return $arr;
}
private function jsonEnc($arr){
$o = json_encode($arr, JSON_PRETTY_PRINT);
$o = $this->fixEncoding($o);
return $o;
}
private function showJsonEnc($arr){
print_r($this->jsonEnc($arr));
}
public function showDebug($what){
if($this->debug){
echo ">-debug--".PHP_EOL;
echo $what;
echo "--debug-<".PHP_EOL;
}
}
public function searchFor($what){
$this->searchKeywords[] = $what;
$this->showDebug("searchFor searchKeywords:".print_r($this->searchKeywords, true));
}
public function setResponse($i){
$this->response = $i;
}
public function getResponse(){
return $this -> showJsonEnc($this->response, JSON_PRETTY_PRINT).PHP_EOL;
}
public function showResponse(){
echo $this -> showJsonEnc($this->response, JSON_PRETTY_PRINT).PHP_EOL;
}
public function doSearch(){
// if no search keywords show appList
if(sizeOf($this->searchKeywords)==0){
// $this->showMan(); // for example
$this->showList(); // default
exit;
}
// if search keywords > 0
$o = $this->appList;
for($i=0;$i<sizeOf($this->searchKeywords);$i++){
$o = $this->arrSearch($o, $this->searchKeywords[$i]);
}
$this->showDebug("doSearch:".print_r($o, true));
switch(sizeOf($o)){
case 0:
fwrite(STDERR, "No matches, try less keywords or run without arguments to show full list".PHP_EOL);
exit(2);
// no matches
break;
case 1:
$o = array_shift($o);
fwrite(STDERR, "Found 1 Match, Launching...".PHP_EOL);
$cmd = $o['cmd']." 2>&1";
// echo "cmd:$cmd";
$response = explode(PHP_EOL, trim(`$cmd`));
// $arrX = atrim($arrX);
foreach($response as $k=>$v){
$o['status'.$k] = $v;
}
$this->setResponse($o);
break;
default:
// more than one match
// verify -f
if (@$this->firstMatch){
$o = array_shift($o);
$o['status'] = "launched";
fwrite(STDERR, "Launching 1st match...".PHP_EOL);
$cmd = $o['cmd']." 2>&1";
// echo "cmd:$cmd";
$response = explode(PHP_EOL, trim(`$cmd`));
// $arrX = atrim($arrX);
foreach($response as $k=>$v){
$o['status'.$k] = $v;
}
$this->setResponse($o);
}else{
$this->setResponse($o);
fwrite(STDERR, "Multiple matches found, but -f not specified".PHP_EOL);
}
break;
}
}
public function showList(){
echo $this->jsonEnc($this->appList);
}
public function setFirstMatch(){
$this->firstMatch = true;
}
}
/**main**/
$opts = new Commando\Command();
// define script switches and check them
$opts->option('m')->aka('man')->
describedAs(MAN)->boolean()->defaultsTo(false);
$opts->option('u')->aka('update')->
describedAs('update newly installed or deleted apps')->boolean()->defaultsTo(false);
$opts->option('l')->aka('list')->
describedAs('list apps, this is the default, if you run the script without arguments, it will show the app list too')->boolean()->defaultsTo(false);
$opts->option('f')->aka('firstmatch')->
describedAs('Execute the first app on the list')->boolean()->defaultsTo(false);
$opts->option('V')->aka('version')->
describedAs(VERSION)->boolean()->defaultsTo(false);
if ($opts['version']){
echo VERSION.PHP_EOL;
exit;
}
$cc = new termuxCuteXCute();
if ($opts['update']){
$cc -> setFirstMatch();
$cc -> searchFor("termuxlauncher");
$cc -> doSearch();
$cc -> getResponse();
echo "termuxlauncher launched, newly installed show be searchable, deleted won't appear anymore ".PHP_EOL;
echo "you should execute ";
exit;
}
for ($i=0;$i<=MAXARGS;$i++){
if ($opts[$i]=="")
break;
// echo "$i:".$opts[$i].PHP_EOL;
$numArgs=$i+1;
$cc->searchFor($opts[$i]);
}
$cc -> doSearch();
$cc -> showResponse();