-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
399 lines (373 loc) · 11.4 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
// AllScan Main controller (index.php)
// Author: David Gleason - AllScan.info
require_once('include/common.php');
require_once('include/hwUtils.php');
require_once('astapi/AMI.php');
require_once('astapi/nodeInfo.php');
$html = new Html();
$msg = [];
// Init base cfgs
asInit($msg);
// Init DB (exits on error)
$db = dbInit();
// Validate/create required DB tables. Returns count of users in user table (exits on error)
$userCnt = checkTables($db, $msg);
if(!$userCnt)
redirect('user/');
// Init gCfgs (exits on error)
$cfgModel = new CfgModel($db);
// Init Users module, validate user
$userModel = new UserModel($db);
$user = $userModel->validate();
if(!readOk())
redirect('user/');
$msg[] = "User: $user->name, IP: $user->ip_addr";
// Check disk free space
checkDiskSpace($msg);
// Load node and host definitions
$hosts = [];
$onLoad = '';
$nodes = getNodeCfg($msg, $hosts);
if(!empty($nodes) && !empty($hosts)) {
$node = $nodes[0];
$host = $hosts[0];
// Load ASL DB
$astdb = readAstDb($msg);
if($astdb !== false)
$onLoad = " onLoad=\"asInit('server.php?nodes=$node')\"";
// Handle form submits
$parms = getRequestParms();
if(isset($_POST['Submit']) && $astdb !== false) {
if(isset($parms['favsfile']) && $parms['favsfile'])
$favsFile = $parms['favsfile'];
processForm($parms, $msg);
// Reset parms after processing post submits
unset($_POST, $parms);
if(isset($favsFile))
$parms = ['favsfile' => $favsFile];
}
// Determine favorites file location(s)
$favsFile = '';
$favsFiles = findFavsFiles($favsFile);
if(!empty($favsFiles)) {
if(isset($parms['favsfile']) && in_array($parms['favsfile'], $favsFiles))
$favsFile = $parms['favsfile'];
} else {
unset($favsFile);
}
}
pageInit($onLoad);
if(!isset($node) || $astdb === false)
asExit(implode(BR, $msg));
if(!$gCfg[call] && adminUser()) {
p('Node Call Sign, Location and/or Title have not yet been set. Enter these below:');
showSetNodeInfoForm();
}
if(!isset($parms))
$parms = [];
$remNode = (isset($parms['node']) && validDbID($parms['node']) && strlen($parms['node']) < 9) ? $parms['node'] : '';
showConnStatusTable();
if(modifyOk())
showNodeCtrlForm();
h2('Favorites');
// Read in favorites.ini
$favs = [];
$favcmds = [];
if(!isset($favsFile) || !$favsFile) {
msg('favorites.ini not found. Click below to create ' . $gCfg[favsIniLoc][0]);
showFavsIniForm();
} else {
$favsIni = parse_ini_file($favsFile, true);
if($favsIni === false) {
p("Error parsing $favsFile. Check file format/permissions or create file with www-data writeable permissions.");
} else {
// Combine [general] stanza with this node's stanza
$favsCfg = $favsIni['general'];
if(isset($favsIni[$node])) {
foreach($favsIni[$node] as $type => $arr) {
if($type == 'label') {
foreach($arr as $label) {
$favsCfg['label'][] = $label;
}
} elseif($type == 'cmd') {
foreach($arr as $cmd) {
$favsCfg['cmd'][] = $cmd;
}
}
}
}
$favsCfg['label'] = array_map('trim', $favsCfg['label']);
$favsCfg['cmd'] = array_map('trim', $favsCfg['cmd']);
foreach($favsCfg['cmd'] as $i => $c) {
if(!$c) {
unset($favsCfg['cmd'][$i], $favsCfg['label'][$i]);
} else {
if(preg_match('/[0-9]{4,8}/', $c, $m) == 1)
$favs[$i] = (object)['node'=>$m[0], 'label'=>$favsCfg['label'][$i], 'cmd'=>$c];
else
$favcmds[$i] = (object)['label'=>$favsCfg['label'][$i], 'cmd'=>$c];
}
}
// if(count($favcmds))
// varDump($favcmds);
$msg[] = _count($favs) . " favorites read from $favsFile";
}
}
// Combine favs node, label data with astdb data into favList
$favList = [];
foreach($favs as $n => $f) {
if(array_key_exists($f->node, $astdb)) {
list($x, $call, $desc, $loc) = $astdb[$f->node];
} else {
if($f->node < 3000000) {
list($x, $call, $desc, $loc) = [$n, '-', '[Not in ASL DB]', '[Check Node Number]'];
} else {
$info = getELInfo($f->node);
if(empty($info))
list($x, $call, $desc, $loc) = [$n, '-', '[EchoLink Node]', '-'];
else {
if(preg_match('/(.*) (\[.*\])/', $info, $m) != 1)
$m = [1=>'-', 2=>"[EchoLink $f->node]"];
list($x, $call, $desc, $loc) = [$n, $m[1], $m[2], '-'];
}
}
}
$name = str_replace([$f->node, $call, $desc, $loc, ' ,'], ' ', $f->label);
//$msg[] = "$x, $call, $desc, $loc, $f->label";
//$msg[] = $name;
foreach(['call', 'name', 'desc', 'loc'] as $var)
$$var = trim(str_replace(' ', ' ', $$var), " .,;\n\r\t\v\x00");
if(!$name)
$name = $call;
elseif(strpos($name, $call) === false && $call !== '-')
$name = $call . ' ' . $name;
//$msg[] = $name;
$favList[] = [$n, $f->node, $name, $desc, $loc, NBSP, NBSP];
}
// Sort favList by specified column if fs parm is set
$colKey = ['num', 'node', 'name', 'desc', 'loc'];
$sortCol = isset($_GET['fs']) && in_array($_GET['fs'], $colKey) ? $_GET['fs'] : 'num';
if($sortCol && !empty($favList) && count($favList) > 1) {
$col = array_search($sortCol, $colKey);
$sortAsc = !(isset($_GET['fso']) && $_GET['fso'] === 'd');
$favList = sortArray($favList, $col, !$sortAsc);
}
// Output Favorites table
if(empty($favList)) {
p('No Favorites have yet been added');
} else {
$hdrCols = ['#', 'Node', 'Name', 'Desc', 'Location', '<small>Rx%</small>', '<small>LCnt</small>'];
if(count($favList) > 1) {
foreach($hdrCols as $key => &$col) {
if($key > 4)
break;
$ck = $colKey[$key];
if($sortCol === $ck) {
// Link to sort in opposite order
$col = getSortLink($parms, $ck, !$sortAsc, $col, 'fs', 'fso');
// Show an arrow indicating current sort col and direction
$col .= upDownArrow($sortAsc);
} else {
// Link to sort in ASC order (or DESC order for time cols)
$col = getSortLink($parms, $ck, true, $col, 'fs', 'fso');
}
}
}
$out = $html->tableOpen($hdrCols, null, 'favs', null, 'favs');
foreach($favList as $f) {
$nodeNumAttr = ['1' => 'class="nodeNum" onClick="setNodeBox(' . $f[1] . ')"'];
// Link name to ASL stats page for node
if($f[1] >= 2000 && $f[1] < 3000000)
$f[2] = $html->a("http://stats.allstarlink.org/stats/" . $f[1], null, $f[2], null, 'stats');
if($f[3] == '')
$f[3] = '-';
if($f[4] == '')
$f[4] = '-';
$out .= $html->tableRow($f, null, null, false, $nodeNumAttr);
}
$out .= $html->tableClose();
echo $out;
}
// Show Favorites File select control
showFavsSelect($favsFiles, $favsFile);
// Status Messages div
echo "<p id=\"scanmsg\"></p>";
$msg = implode(BR, $msg);
echo "<div id=\"statmsg\">$msg</div>" . BR;
$sep = ENSP . '|' . ENSP;
// Show CPU Temp if available
if(($ct = cpuTemp()))
echo '<span id="cputemp">' . $ct . '</span>' . $sep . NL;
// Show function buttons and Links
echo $html->linkButton('Node Stats', "http://stats.allstarlink.org/stats/$node", 'small', null, null, 'stats');
if(modifyOk())
echo $html->linkButton('Restart Asterisk', null, 'small', null, 'astrestart();');
showFooterLinks();
asExit();
// ---------------------------------------------------
function processForm($parms, &$msg) {
global $astdb, $favsFile, $gCfg, $cfgModel;
$node = $parms['node'];
switch($parms['Submit']) {
case "Add Favorite":
$msg[] = "Add Node $node to Favorites requested";
if(!validDbID($node)) {
$msg[] = "Invalid node#.";
break;
}
if(!array_key_exists($node, $astdb) && ($nodeNum >= 2000 && $nodeNum < 3000000)) {
$msg[] = "Node $node not found in ASL DB. Check Node Number and that your astdb file is up-to-date.";
break;
}
// Parse file lines and add new favorite after last label,cmd lines.
// Note: Does not look at [general] or [node#] sections (TBI)
if(isset($favsFile)) {
if(($favs = readFileLines($favsFile, $msg, true)) === false)
break;
$n = count($favs);
$msg[] = "$n lines read from $favsFile";
} else {
$favsFile = $gCfg[favsIniLoc][0];
$favs = ['[general]', ''];
$n = count($favs);
}
$insertLn = 0;
for($i=0; $i < $n; $i++) {
if(strpos($favs[$i], 'cmd[]') === 0) {
if(strpos($favs[$i], " $node\"")) {
$msg[] = "Node $node already exists in favorites.";
break(2);
}
$insertLn = $i + 2;
}
}
if(!$insertLn)
$insertLn = $n;
// Add blank line after last fav entry if not present
if($favs[$insertLn] !== '') {
array_splice($favs, $insertLn, 0, ['']);
$n++;
}
list($x, $call, $desc, $loc) = $astdb[$node];
$label = "label[] = \"$call $desc, $loc $node\"";
$cmd = "cmd[] = \"rpt cmd %node% ilink 3 $node\"";
array_splice($favs, $insertLn, 0, [$label, $cmd, '']);
$n = count($favs);
if(!writeFileLines($favsFile, $favs, $msg))
break;
$msg[] = "Successfully wrote $n lines to $favsFile";
break;
case "Delete Favorite":
$msg[] = "Delete Node $node from Favorites requested";
if(!isset($favsFile)) {
$msg[] = "Favorites file does not exist.";
break;
}
if(!validDbID($node)) {
$msg[] = "Invalid node#.";
break;
}
// Parse file lines and delete favorite's label,cmd lines.
// Note: Does not look for [general] or [node#] sections
if(($favs = readFileLines($favsFile, $msg, true)) === false)
break;
$n = count($favs);
$msg[] = "$n lines read from $favsFile";
for($i=0; $i < $n; $i++) {
if(strpos($favs[$i], 'cmd[]') == 0 && strpos($favs[$i], " $node\""))
$delLn = $i + 1;
}
if(!isset($delLn)) {
$msg[] = "Node $node not found in $favsFile";
break;
}
$nLines = 2;
$startLn = $delLn - $nLines;
if($startLn <= 0) {
$msg[] = error("Invalid $favsFile format");
break;
}
// Also delete blank line after entry if present
if($favs[$delLn] === '' && $delLn < $n - 1)
$nLines++;
for($i=0; $i < $nLines; $i++)
unset($favs[$startLn + $i]);
$n = count($favs);
if(!writeFileLines($favsFile, $favs, $msg))
break;
$msg[] = "Successfully wrote $n lines to $favsFile";
break;
case SET_NODE_INFO_CFGS:
if(!isset($parms['call']) || !isset($parms['location']) || !isset($parms['title']))
break;
$gCfg[call] = $parms['call'];
$gCfg[location] = $parms['location'];
$gCfg[title] = $parms['title'];
$cfgModel->saveCfgs();
if($cfgModel->error)
$msg[] = error('Error saving Node Info Cfgs: ' . $cfgModel->error);
else
$msg[] = 'Saved Node Info Cfgs OK';
break;
case CREATE_FAVORITESINI_FILE:
$from = 'docs/favorites.ini.sample';
$to = $gCfg[favsIniLoc][0];
if(copy($from, $to)) {
$msg[] = "Copied $from to $to OK";
chmod($to, 0664);
$favsFile = $to;
} else {
$msg[] = error("Copy $from to $to Error. Check directory permissions.");
}
break;
}
}
function getELInfo($n) {
global $node, $host, $ami;
static $servers, $fp, $cfg;
if(!$node || !$host) {
return;
}
if(empty($ami)) {
$ami = new AMI();
$servers = [];
$fp = [];
$cfg = readNodeCfg();
}
// Login to AMI
if(!array_key_exists($host, $servers)) {
// msg("Connecting to Asterisk Manager $node $host...");
$fp[$host] = $ami->connect($host);
if($fp[$host] === false) {
//msg('Connect Failed. Check allmon.ini settings.');
return;
}
$amiuser = $cfg[$node]['user'];
$pass = $cfg[$node]['passwd'];
if($ami->login($fp[$host], $amiuser, $pass) !== false) {
$servers[$host] = 'y';
//msg('Login OK');
} else {
//msg("Login Failed. Check allmon.ini settings.");
return;
}
}
return getAstInfo($fp[$host], $n);
}
function sortArray($list, $col, $desc) {
$colVals = [];
foreach($list as $val)
$colVals[] = $val[$col];
// Sort column, retain keys
$opt = $col ? (SORT_STRING | SORT_FLAG_CASE) : SORT_REGULAR;
if($desc)
arsort($colVals, $opt);
else
asort($colVals, $opt);
// Reorder input array
$out = [];
foreach(array_keys($colVals) as $k)
$out[] = $list[$k];
return $out;
}