Skip to content

Commit e037227

Browse files
committed
Added back Accounts and Auth without almost working modals
1 parent 6c967c6 commit e037227

File tree

15 files changed

+902
-104
lines changed

15 files changed

+902
-104
lines changed

public/index.php

+33-31
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@
1010

1111
use HCP\Init;
1212

13-
function dbg($var = null): void
14-
{
15-
if (is_object($var)) {
16-
$refobj = new \ReflectionObject($var);
17-
// get all public and protected properties
18-
$var = $refobj->getProperties(\ReflectionProperty::IS_PUBLIC);
19-
$var = \array_merge($var, $refobj->getProperties(\ReflectionProperty::IS_PROTECTED));
20-
}
21-
ob_start();
22-
print_r($var);
23-
$ob = ob_get_contents();
24-
ob_end_clean();
25-
error_log($ob);
26-
}
27-
28-
function elog(string $content): void
29-
{
30-
if (DBG) {
31-
error_log($content);
32-
}
33-
}
34-
3513
echo new Init(new class()
3614
{
3715
public $cfg = [
@@ -53,7 +31,7 @@ function elog(string $content): void
5331
'm' => 'list', // Method (action)
5432
'o' => 'Home', // Object (content)
5533
'r' => 'local', // Remotes (local)
56-
't' => 'TopNav', // Theme (Default)
34+
't' => 'TopNav', // Theme (Default)
5735
'x' => '', // XHR (request)
5836
];
5937

@@ -71,6 +49,8 @@ function elog(string $content): void
7149
'end' => '',
7250
];
7351

52+
public $t;
53+
7454
public $db = [
7555
'host' => '127.0.0.1', // DB site
7656
'name' => 'sysadm', // DB name
@@ -97,18 +77,18 @@ function elog(string $content): void
9777
['Phpmyadmin', 'phpmyadmin/', 'bi bi-globe'],
9878
], 'bi bi-list'],
9979
['Admin', [
100-
['Accounts', '?o=accounts', 'bi bi-people'],
101-
['Vhosts', '?o=vhosts', 'bi bi-globe'],
102-
['Mailboxes', '?o=vmails', 'bi bi-envelope'],
103-
['Aliases', '?o=valias', 'bi bi-envelope-fill'],
104-
['DKIM', '?o=dkim', 'bi bi-person-vcard'],
105-
['Domains', '?o=domains', 'bi bi-server'],
80+
['Accounts', '?o=Accounts', 'bi bi-people'],
81+
['Vhosts', '?o=Vhosts', 'bi bi-globe'],
82+
['Mailboxes', '?o=Vmails', 'bi bi-envelope'],
83+
['Aliases', '?o=Valias', 'bi bi-envelope-fill'],
84+
['DKIM', '?o=Dkim', 'bi bi-person-vcard'],
85+
['Domains', '?o=Domains', 'bi bi-server'],
10686
], 'bi bi-gear-fill'],
10787
['Stats', [
10888
['Sys Info', '?o=InfoSys', 'bi bi-speedometer'],
10989
['Processes', '?o=Processes', 'bi bi-diagram-2'],
110-
['Mail Info', '?o=infomail', 'bi bi-envelope-fill'],
111-
['Mail Graph', '?o=mailgraph', 'bi bi-envelope'],
90+
['Mail Info', '?o=Infomail', 'bi bi-envelope-fill'],
91+
['Mail Graph', '?o=Mailgraph', 'bi bi-envelope'],
11292
], 'bi bi-graph-up'],
11393
],
11494
];
@@ -152,3 +132,25 @@ function elog(string $content): void
152132
9 => 'Anonymous',
153133
];
154134
});
135+
136+
function elog(string $content): void
137+
{
138+
if (DBG) {
139+
error_log($content);
140+
}
141+
}
142+
143+
function dbg($var = null): void
144+
{
145+
if (is_object($var)) {
146+
$refobj = new \ReflectionObject($var);
147+
// get all public and protected properties
148+
$var = $refobj->getProperties(\ReflectionProperty::IS_PUBLIC);
149+
$var = \array_merge($var, $refobj->getProperties(\ReflectionProperty::IS_PROTECTED));
150+
}
151+
ob_start();
152+
print_r($var);
153+
$ob = ob_get_contents();
154+
ob_end_clean();
155+
error_log($ob);
156+
}

src/Db.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public function __construct(array $dbcfg)
1515
{
1616
elog(__METHOD__);
1717

18-
dbg($dbcfg);
18+
//dbg($dbcfg);
19+
1920
if (is_null(self::$dbh)) {
2021
extract($dbcfg);
2122
$dsn = 'mysql' === $type
@@ -53,7 +54,7 @@ public static function create(array $ary)
5354
INSERT INTO `' . self::$tbl . "` ({$fields})
5455
VALUES ({$values})";
5556

56-
elog("sql={$sql}");
57+
//elog("sql={$sql}");
5758

5859
try {
5960
$stm = self::$dbh->prepare($sql);
@@ -84,7 +85,7 @@ public static function read(
8485
SELECT {$field}
8586
FROM `" . self::$tbl . "`{$w} {$extra}";
8687

87-
elog("sql={$sql}");
88+
//elog("sql={$sql}");
8889

8990
return self::qry($sql, $a, $type);
9091
}
@@ -112,7 +113,7 @@ public static function update(array $set, array $where)
112113
UPDATE `' . self::$tbl . "` SET{$set_str}
113114
WHERE{$where_str}";
114115

115-
elog("sql={$sql}");
116+
//elog("sql={$sql}");
116117

117118
try {
118119
$stm = self::$dbh->prepare($sql);
@@ -139,7 +140,7 @@ public static function delete(array $where)
139140
DELETE FROM `' . self::$tbl . "`
140141
WHERE {$where_str}";
141142

142-
elog("sql={$sql}");
143+
//elog("sql={$sql}");
143144

144145
try {
145146
$stm = self::$dbh->prepare($sql);
@@ -226,7 +227,7 @@ public static function simple($request, $table, $primaryKey, $columns, $extra =
226227
$where .= $where ? " AND ({$extra})" : " WHERE {$extra}";
227228
}
228229

229-
elog("where={$where}");
230+
//elog("where={$where}");
230231

231232
$query = "
232233
SELECT {$cols}
@@ -386,7 +387,7 @@ public static function sql_exec($db, $bindings, $sql = null, string $type = 'all
386387
{
387388
elog(__METHOD__);
388389

389-
elog("sql={$sql}");
390+
//elog("sql={$sql}");
390391

391392
// Argument shifting
392393
if (null === $sql) {

src/Init.php

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ private function initializeSession(object $g): void
2525
elog(__METHOD__);
2626

2727
session_start();
28+
29+
//elog(var_export($_SESSION, true));
30+
//$_SESSION = [];
31+
2832
$g->cfg['host'] ??= getenv('HOSTNAME');
2933
Util::cfg($g);
3034
$g->in = Util::esc($g->in);
@@ -56,6 +60,9 @@ private function loadTheme(object $g): void
5660
// Fallback to default Theme
5761
$this->theme = new Theme($g);
5862
}
63+
64+
// Assign theme instance to g->t for access in plugins
65+
$g->t = $this->theme;
5966
}
6067

6168
private function processPlugin(object $g): void
@@ -109,6 +116,10 @@ public function __toString(): string
109116
}
110117

111118
if ($x) {
119+
if ($x === 'html') {
120+
return $g->out['main'];
121+
}
122+
112123
$out = $g->out[$x] ?? '';
113124
if ($out) {
114125
header('Content-Type: application/json');

src/Plugin.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function __construct(public Theme $t)
2525
$o = $t->g->in['o'];
2626
$m = $t->g->in['m'];
2727

28-
if (!util::is_usr() && ('auth' !== $o || ('list' !== $m && 'create' !== $m && 'resetpw' !== $m))) {
29-
util::redirect($t->g->cfg['self'] . '?o=auth');
28+
if (!util::is_usr() && ('Auth' !== $o || ('list' !== $m && 'create' !== $m && 'resetpw' !== $m))) {
29+
util::redirect($t->g->cfg['self'] . '?o=Auth');
3030
}
3131

3232
$this->t = $t;
@@ -98,7 +98,7 @@ protected function update(): string
9898
return $this->read();
9999
}
100100

101-
protected function delete(): void
101+
protected function delete(): string
102102
{
103103
elog(__METHOD__);
104104

@@ -107,10 +107,11 @@ protected function delete(): void
107107
$res = db::delete([['id', '=', $this->g->in['i']]]);
108108
util::log('Item number ' . $this->g->in['i'] . ' removed', 'success');
109109
util::relist();
110+
} else {
111+
util::log('Error deleting item');
110112
}
111113
}
112-
113-
util::log('Error deleting item');
114+
return '';
114115
}
115116

116117
protected function list(): string

src/Plugins/Accounts/Model.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
// lib/php/plugins/users.php 20150101 - 20200414
5+
// Copyright (C) 2015-2020 Mark Constable <markc@renta.net> (AGPL-3.0)
6+
7+
namespace HCP\Plugins\Accounts;
8+
9+
use HCP\Plugin;
10+
use HCP\Util;
11+
use HCP\Db;
12+
13+
class Model extends Plugin
14+
{
15+
protected string $tbl = 'accounts';
16+
protected array $in = [
17+
'grp' => 1,
18+
'acl' => 2,
19+
'vhosts' => 1,
20+
'login' => '',
21+
'fname' => '',
22+
'lname' => '',
23+
'altemail' => '',
24+
];
25+
26+
protected function create(): string
27+
{
28+
elog(__METHOD__);
29+
30+
if (Util::is_adm()) return parent::create();
31+
Util::log('You are not authorized to perform this action, please contact your administrator.');
32+
Util::relist();
33+
return '';
34+
}
35+
36+
protected function read(): string
37+
{
38+
elog(__METHOD__);
39+
40+
$usr = Db::read('*', 'id', $this->g->in['i'], '', 'one');
41+
if (!$usr) {
42+
Util::log('User not found.');
43+
Util::relist();
44+
return '';
45+
}
46+
47+
if (Util::is_acl(0)) {
48+
// superadmin
49+
} elseif (Util::is_acl(1)) { // normal admin
50+
if ((int)$_SESSION['usr']['grp'] !== (int)$usr['grp']) {
51+
Util::log('You are not authorized to perform this action.');
52+
Util::relist();
53+
return '';
54+
}
55+
} else { // Other users
56+
if ((int)$_SESSION['usr']['id'] !== (int)$usr['id']) {
57+
Util::log('You are not authorized to perform this action.');
58+
Util::relist();
59+
return '';
60+
}
61+
}
62+
return $this->g->t->read($usr);
63+
}
64+
65+
protected function delete(): string
66+
{
67+
elog(__METHOD__);
68+
69+
if (Util::is_post()) {
70+
parent::delete();
71+
return '';
72+
}
73+
return $this->g->t->delete();
74+
}
75+
76+
protected function list(): string
77+
{
78+
elog(__METHOD__);
79+
80+
if ($this->g->in['x'] === 'json') {
81+
$columns = [
82+
['dt' => null, 'db' => 'id'],
83+
['dt' => 0, 'db' => 'login', 'formatter' => function ($d, array $row): string {
84+
return '<b><a href="?o=Accounts&m=read&i=' . $row['id'] . '&x=html" class="bslink">' . $d . '</a></b>';
85+
}],
86+
['dt' => 1, 'db' => 'fname'],
87+
['dt' => 2, 'db' => 'lname'],
88+
['dt' => 3, 'db' => 'altemail'],
89+
['dt' => 4, 'db' => 'acl', 'formatter' => function ($d): string {
90+
return $this->g->acl[is_string($d) ? (int)$d : $d];
91+
}],
92+
['dt' => 5, 'db' => 'grp'],
93+
];
94+
return json_encode(Db::simple($_GET, 'accounts', 'id', $columns));
95+
}
96+
return $this->g->t->list($this->in);
97+
}
98+
99+
protected function switch_user(): string
100+
{
101+
elog(__METHOD__);
102+
103+
if (Util::is_adm() && !is_null($this->g->in['i'])) {
104+
$usr = Db::read('id,acl,grp,login,fname,lname,webpw,cookie', 'id', $this->g->in['i'], '', 'one');
105+
if ($usr) {
106+
$_SESSION['usr'] = $usr;
107+
Util::log('Switch to user: ' . $usr['login'], 'success');
108+
}
109+
} else {
110+
Util::log('Not authorized to switch users');
111+
}
112+
Util::relist();
113+
return '';
114+
}
115+
}

0 commit comments

Comments
 (0)