Skip to content

Commit b7e2c5c

Browse files
committed
First round of changes after testing
1 parent 034cb6f commit b7e2c5c

File tree

7 files changed

+77
-50
lines changed

7 files changed

+77
-50
lines changed

php/db.php

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
declare(strict_types=1);
4-
// lib/php/db.php 20150225 - 20180512
5-
// Copyright (C) 2015-2018 Mark Constable <markc@renta.net> (AGPL-3.0)
4+
// lib/php/db.php 20150225 - 20250128
5+
// Copyright (C) 2015-2025 Mark Constable <markc@renta.net> (AGPL-3.0)
66

77
class Db extends \PDO
88
{
@@ -13,11 +13,12 @@ public function __construct(array $dbcfg)
1313
{
1414
elog(__METHOD__);
1515

16+
dbg($dbcfg);
1617
if (is_null(self::$dbh)) {
1718
extract($dbcfg);
1819
$dsn = 'mysql' === $type
19-
? 'mysql:'.($sock ? 'unix_socket='.$sock : 'host='.$host.';port='.$port).';dbname='.$name
20-
: 'sqlite:'.$path;
20+
? 'mysql:' . ($sock ? 'unix_socket=' . $sock : 'host=' . $host . ';port=' . $port) . ';dbname=' . $name
21+
: 'sqlite:' . $path;
2122
$pass = file_exists($pass) ? trim(file_get_contents($pass)) : $pass;
2223

2324
try {
@@ -27,7 +28,7 @@ public function __construct(array $dbcfg)
2728
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
2829
]);
2930
} catch (\PDOException $e) {
30-
exit(__FILE__.' '.__LINE__."<br>\n".$e->getMessage());
31+
exit(__FILE__ . ' ' . __LINE__ . "<br>\n" . $e->getMessage());
3132
}
3233
}
3334
}
@@ -47,7 +48,7 @@ public static function create(array $ary)
4748
$values = rtrim($values, ',');
4849

4950
$sql = '
50-
INSERT INTO `'.self::$tbl."` ({$fields})
51+
INSERT INTO `' . self::$tbl . "` ({$fields})
5152
VALUES ({$values})";
5253

5354
elog("sql={$sql}");
@@ -59,7 +60,7 @@ public static function create(array $ary)
5960

6061
return self::$dbh->lastInsertId();
6162
} catch (\PDOException $e) {
62-
exit(__FILE__.' '.__LINE__."<br>\n".$e->getMessage());
63+
exit(__FILE__ . ' ' . __LINE__ . "<br>\n" . $e->getMessage());
6364
}
6465
}
6566

@@ -69,8 +70,7 @@ public static function read(
6970
string $wval = '',
7071
string $extra = '',
7172
string $type = 'all'
72-
)
73-
{
73+
) {
7474
elog(__METHOD__);
7575

7676
$w = $where ? "
@@ -80,7 +80,7 @@ public static function read(
8080

8181
$sql = "
8282
SELECT {$field}
83-
FROM `".self::$tbl."`{$w} {$extra}";
83+
FROM `" . self::$tbl . "`{$w} {$extra}";
8484

8585
elog("sql={$sql}");
8686

@@ -101,13 +101,13 @@ public static function update(array $set, array $where)
101101
$where_str = '';
102102
$where_ary = [];
103103
foreach ($where as $k => $v) {
104-
$where_str .= ' '.$v[0].' '.$v[1].' :'.$v[0];
104+
$where_str .= ' ' . $v[0] . ' ' . $v[1] . ' :' . $v[0];
105105
$where_ary[$v[0]] = $v[2];
106106
}
107107
$ary = array_merge($set, $where_ary);
108108

109109
$sql = '
110-
UPDATE `'.self::$tbl."` SET{$set_str}
110+
UPDATE `' . self::$tbl . "` SET{$set_str}
111111
WHERE{$where_str}";
112112

113113
elog("sql={$sql}");
@@ -118,7 +118,7 @@ public static function update(array $set, array $where)
118118

119119
return $stm->execute();
120120
} catch (\PDOException $e) {
121-
exit(__FILE__.' '.__LINE__."<br>\n".$e->getMessage());
121+
exit(__FILE__ . ' ' . __LINE__ . "<br>\n" . $e->getMessage());
122122
}
123123
}
124124

@@ -129,12 +129,12 @@ public static function delete(array $where)
129129
$where_str = '';
130130
$where_ary = [];
131131
foreach ($where as $k => $v) {
132-
$where_str .= ' '.$v[0].' '.$v[1].' :'.$v[0];
132+
$where_str .= ' ' . $v[0] . ' ' . $v[1] . ' :' . $v[0];
133133
$where_ary[$v[0]] = $v[2];
134134
}
135135

136136
$sql = '
137-
DELETE FROM `'.self::$tbl."`
137+
DELETE FROM `' . self::$tbl . "`
138138
WHERE {$where_str}";
139139

140140
elog("sql={$sql}");
@@ -145,7 +145,7 @@ public static function delete(array $where)
145145

146146
return $stm->execute();
147147
} catch (\PDOException $e) {
148-
exit(__FILE__.' '.__LINE__."<br>\n".$e->getMessage());
148+
exit(__FILE__ . ' ' . __LINE__ . "<br>\n" . $e->getMessage());
149149
}
150150
}
151151

@@ -177,7 +177,7 @@ public static function qry(string $sql, array $ary = [], string $type = 'all')
177177

178178
return false;
179179
} catch (\PDOException $e) {
180-
exit(__FILE__.' '.__LINE__."<br>\n".$e->getMessage());
180+
exit(__FILE__ . ' ' . __LINE__ . "<br>\n" . $e->getMessage());
181181
}
182182
}
183183

@@ -213,7 +213,7 @@ public static function simple($request, $table, $primaryKey, $columns, $extra =
213213
elog(__METHOD__);
214214

215215
$db = self::$dbh;
216-
$cols = '`'.implode('`, `', self::pluck($columns, 'db')).'`';
216+
$cols = '`' . implode('`, `', self::pluck($columns, 'db')) . '`';
217217
$bind = [];
218218

219219
$limit = self::limit($request, $columns);
@@ -283,7 +283,7 @@ public static function limit($request, $columns)
283283
$limit = '';
284284

285285
if (isset($request['start']) && -1 != $request['length']) {
286-
$limit = 'LIMIT '.intval($request['start']).', '.intval($request['length']);
286+
$limit = 'LIMIT ' . intval($request['start']) . ', ' . intval($request['length']);
287287
}
288288

289289
return $limit;
@@ -297,23 +297,23 @@ public static function order($request, $columns)
297297

298298
if (isset($request['order']) && count($request['order'])) {
299299
$orderBy = [];
300-
// $dtColumns = self::pluck($columns, 'dt');
300+
// $dtColumns = self::pluck($columns, 'dt');
301301

302302
for ($i = 0, $ien = count($request['order']); $i < $ien; ++$i) {
303303
$columnIdx = intval($request['order'][$i]['column']);
304304
$requestColumn = $request['columns'][$columnIdx];
305-
// $columnIdx = array_search($requestColumn['data'], $dtColumns); // don't use $dtColumns
305+
// $columnIdx = array_search($requestColumn['data'], $dtColumns); // don't use $dtColumns
306306
$columnIdx = array_search($requestColumn['data'], array_column($columns, 'dt'));
307307
$column = $columns[$columnIdx];
308308

309309
if ('true' == $requestColumn['orderable']) {
310310
$dir = 'asc' === $request['order'][$i]['dir'] ? 'ASC' : 'DESC';
311-
$orderBy[] = '`'.$column['db'].'` '.$dir;
311+
$orderBy[] = '`' . $column['db'] . '` ' . $dir;
312312
}
313313
}
314314

315315
if (count($orderBy)) {
316-
$order = 'ORDER BY '.implode(', ', $orderBy);
316+
$order = 'ORDER BY ' . implode(', ', $orderBy);
317317
}
318318
}
319319

@@ -336,8 +336,8 @@ public static function filter($request, $columns, &$bindings)
336336
$column = $columns[$columnIdx];
337337

338338
if ('true' == $requestColumn['searchable'] && $column['db']) {
339-
$binding = self::bind($bindings, '%'.$str.'%', PDO::PARAM_STR);
340-
$globalSearch[] = '`'.$column['db'].'` LIKE '.$binding;
339+
$binding = self::bind($bindings, '%' . $str . '%', PDO::PARAM_STR);
340+
$globalSearch[] = '`' . $column['db'] . '` LIKE ' . $binding;
341341
}
342342
}
343343
}
@@ -352,9 +352,9 @@ public static function filter($request, $columns, &$bindings)
352352
$str = $requestColumn['search']['value'];
353353

354354
if ('true' == $requestColumn['searchable'] && '' != $str && null !== $column['db']) {
355-
$binding = self::bind($bindings, '%'.$str.'%', PDO::PARAM_STR);
355+
$binding = self::bind($bindings, '%' . $str . '%', PDO::PARAM_STR);
356356
if ($column['db']) {
357-
$columnSearch[] = '`'.$column['db'].'` LIKE '.$binding;
357+
$columnSearch[] = '`' . $column['db'] . '` LIKE ' . $binding;
358358
}
359359
}
360360
}
@@ -364,17 +364,17 @@ public static function filter($request, $columns, &$bindings)
364364
$where = '';
365365

366366
if (count($globalSearch)) {
367-
$where = '('.implode(' OR ', $globalSearch).')';
367+
$where = '(' . implode(' OR ', $globalSearch) . ')';
368368
}
369369

370370
if (count($columnSearch)) {
371371
$where = '' === $where ?
372372
implode(' AND ', $columnSearch) :
373-
$where.' AND '.implode(' AND ', $columnSearch);
373+
$where . ' AND ' . implode(' AND ', $columnSearch);
374374
}
375375

376376
if ('' !== $where) {
377-
$where = 'WHERE '.$where;
377+
$where = 'WHERE ' . $where;
378378
}
379379

380380
return $where;
@@ -405,7 +405,7 @@ public static function sql_exec($db, $bindings, $sql = null, string $type = 'all
405405
try {
406406
$stmt->execute();
407407
} catch (PDOException $e) {
408-
self::fatal('An SQL error occurred: '.$e->getMessage());
408+
self::fatal('An SQL error occurred: ' . $e->getMessage());
409409
}
410410

411411
if ('all' === $type) {
@@ -435,7 +435,7 @@ private static function bind(&$a, $val, $type)
435435
{
436436
elog(__METHOD__);
437437

438-
$key = ':binding_'.count($a);
438+
$key = ':binding_' . count($a);
439439
$a[] = ['key' => $key, 'val' => $val, 'type' => $type];
440440

441441
return $key;

php/init.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ class Init
1111
public function __construct(object $g)
1212
{
1313
elog(__METHOD__);
14+
1415
session_start();
1516

16-
elog('GET=' . var_export($_GET, true));
17-
elog('POST=' . var_export($_POST, true));
18-
elog('SESSION=' . var_export($_SESSION, true));
17+
// elog('GET=' . var_export($_GET, true));
18+
// elog('POST=' . var_export($_POST, true));
19+
// elog('SESSION=' . var_export($_SESSION, true));
1920

2021
//$_SESSION = []; // to reset session for testing
2122

@@ -67,6 +68,9 @@ public function __toString(): string
6768
elog(__METHOD__);
6869

6970
$g = $this->t->g;
71+
72+
dbg($g);
73+
7074
$x = $g->in['x'];
7175
if ('text' === $x) {
7276
return preg_replace('/^\h*\v+/m', '', strip_tags($g->out['main']));

php/plugins/auth.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
declare(strict_types=1);
4-
// lib/php/plugins/auth.php 20150101 - 20230604
5-
// Copyright (C) 2015-2023 Mark Constable <markc@renta.net> (AGPL-3.0)
4+
// lib/php/plugins/auth.php 20150101 - 20250118
5+
// Copyright (C) 2015-2025 Mark Constable <markc@renta.net> (AGPL-3.0)
66

77
class Plugins_Auth extends Plugin
88
{
@@ -33,6 +33,10 @@ public function create(): string
3333
if (filter_var($u, FILTER_VALIDATE_EMAIL)) {
3434
if ($usr = db::read('id,acl', 'login', $u, '', 'one')) {
3535
if (9 != $usr['acl']) {
36+
if (!isset($this->g->cfg['email']) || !filter_var($this->g->cfg['email'], FILTER_VALIDATE_EMAIL)) {
37+
util::log('System email address is not valid', 'danger');
38+
util::redirect($this->g->cfg['self'] . '?o=' . $this->g->in['o'] . '&m=list');
39+
}
3640
$newpass = util::genpw(self::OTP_LENGTH);
3741
if ($this->mail_forgotpw($u, $newpass, 'From: ' . $this->g->cfg['email'])) {
3842
db::update([
@@ -66,6 +70,21 @@ public function list(): string
6670
$u = $this->in['login'];
6771
$p = $this->in['webpw'];
6872

73+
if (filter_var($this->g->cfg['email'], FILTER_VALIDATE_EMAIL) ?? '') {
74+
$_SESSION['usr'] = [
75+
'id' => 0,
76+
'grp' => 0,
77+
'acl' => 0,
78+
'login' => $this->g->cfg['email'],
79+
'fname' => 'Admin',
80+
'lname' => 'User'
81+
];
82+
$_SESSION['adm'] = 0;
83+
util::log($u . ' is now logged in', 'success');
84+
$_SESSION['m'] = 'list';
85+
util::redirect($this->g->cfg['self']);
86+
}
87+
6988
if ($u) {
7089
if ($usr = db::read('id,grp,acl,login,fname,lname,webpw,cookie', 'login', $u, '', 'one')) {
7190
extract($usr);

php/plugins/processes.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22

33
declare(strict_types=1);
4-
// plugins/processes.php 20170225 - 20180430
5-
// Copyright (C) 2015-2018 Mark Constable <markc@renta.net> (AGPL-3.0)
4+
// plugins/processes.php 20170225 - 20260128
5+
// Copyright (C) 2015-2025 Mark Constable <markc@renta.net> (AGPL-3.0)
66

77
class Plugins_Processes extends Plugin
88
{
99
public function list(): string
1010
{
1111
elog(__METHOD__);
1212

13-
return $this->t->list(['procs' => shell_exec('sudo processes')]);
13+
$cmd = "ps -eo rss:10,vsz:10,%cpu:5,cmd --sort=rss | grep -v \"^\s\+0\" | cut -c -79";
14+
return $this->t->list(['procs' => shell_exec($cmd)]);
1415
}
1516
}

php/themes/bootstrap5/accounts.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function update(array $in): string
2929

3030
public function list(array $in): string
3131
{
32-
elog(__METHOD__ . ' ' . var_export($in, true));
32+
elog(__METHOD__);
33+
//elog(__METHOD__ . ' ' . var_export($in, true));
3334

3435
extract($in);
3536
$aclgrp_buf = '';

php/themes/bootstrap5/processes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function list(array $in): string
2626
</div><!-- END UPPER ROW -->
2727
<div class="row">
2828
<div class="col-12">
29-
<h5>Process List <small>(' . (count(explode("\n", $in['procs'])) - 1) . ')</small></h5>
30-
<pre><code>' . $in['procs'] . '
29+
<h5>Process List <small>(' . ($in['procs'] ? (count(explode("\n", $in['procs'])) - 1) : 0) . ')</small></h5>
30+
<pre><code>' . ($in['procs'] ?? 'No process data available') . '
3131
</code></pre>
3232
</div>
3333
</div>';

public/index.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
echo new Init(new class()
2525
{
2626
public $cfg = [
27-
'email' => 'markc@renta.net',
28-
'file' => __DIR__ . DS . 'lib' . DS . '.ht_conf.php', // settings override
27+
'email' => 'admin@example.com',
28+
'admpw' => 'admin123',
29+
'file' => '../php/.ht_conf.php', // settings override
2930
'hash' => 'SHA512-CRYPT',
3031
'host' => '',
3132
'perp' => 25,
@@ -39,7 +40,8 @@
3940
'l' => '', // Log (message)
4041
'm' => 'list', // Method (action)
4142
'o' => 'home', // Object (content)
42-
't' => 'bootstrap5', // Theme
43+
'r' => 'local', // Remotes (local)
44+
't' => 'bootstrap5', // Theme (Default)
4345
'x' => '', // XHR (request)
4446
];
4547
public $out = [
@@ -58,8 +60,8 @@
5860
public $db = [
5961
'host' => '127.0.0.1', // DB site
6062
'name' => 'sysadm', // DB name
61-
'pass' => 'php' . DS . '.ht_pw', // MySQL password override
62-
'path' => 'sysadm.db', // SQLite DB
63+
'pass' => '../php/.ht_pw', // MySQL password override
64+
'path' => '../sysadm/sysadm.db', // SQLite DB
6365
'port' => '3306', // DB port
6466
'sock' => '', // '/run/mysqld/mysqld.sock',
6567
'type' => 'sqlite', // mysql | sqlite
@@ -114,8 +116,8 @@
114116
'db' => [
115117
'host' => '127.0.0.1', // Alt DNS DB site
116118
'name' => 'pdns', // Alt DNS DB name
117-
'pass' => 'lib' . DS . '.ht_dns_pw', // MySQL DNS password override
118-
'path' => 'pdns.db', // DNS SQLite DB
119+
'pass' => '../php/.ht_dns_pw', // MySQL DNS password override
120+
'path' => '../sysadm/pdns.db', // DNS SQLite DB
119121
'port' => '3306', // Alt DNS DB port
120122
'sock' => '', // '/run/mysqld/mysqld.sock',
121123
'type' => 'sqlite', // mysql | sqlite | '' to disable

0 commit comments

Comments
 (0)