Skip to content

Commit

Permalink
First cut at PHP 8.2 and bootstrap5 using vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
markc committed Jun 4, 2023
1 parent 875b823 commit 48ff9ec
Show file tree
Hide file tree
Showing 27 changed files with 406 additions and 402 deletions.
39 changes: 21 additions & 18 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,41 @@
// Copyright (C) 2015-2023 Mark Constable <markc@renta.net> (AGPL-3.0)

const DS = DIRECTORY_SEPARATOR;
const INC = __DIR__.DS.'lib'.DS.'php'.DS;
const DBG = false;
const INC = __DIR__ . DS . 'lib' . DS . 'php' . DS;
const DBG = true;

spl_autoload_register(function ($c): void {
$f = INC.str_replace(['\\', '_'], [DS, DS], strtolower($c)).'.php';
$f = INC . str_replace(['\\', '_'], [DS, DS], strtolower($c)) . '.php';
if (file_exists($f)) {
include $f;
if (DBG) {
error_log("include $f");
}
} else {
error_log("!!! {$f} does not exist");
}
});

echo new Init(new class() {
echo new Init(new class()
{
public $cfg = [
'email' => 'markc@renta.net',
'file' => __DIR__.DS.'lib'.DS.'.ht_conf.php', // settings override
'file' => __DIR__ . DS . 'lib' . DS . '.ht_conf.php', // settings override
'hash' => 'SHA512-CRYPT',
'host' => '',
'perp' => 25,
'self' => '/hcp/',
];
public $in = [
'a' => '', // API (apiusr:apikey)
'd' => '', // Domain (current)
'g' => null, // Group/Category
'i' => null, // Item or ID
'l' => '', // Log (message)
'm' => 'list', // Method (action)
'o' => 'home', // Object (content)
't' => 'bootstrap5',// Theme
'x' => '', // XHR (request)
'a' => '', // API (apiusr:apikey)
'd' => '', // Domain (current)
'g' => null, // Group/Category
'i' => null, // Item or ID
'l' => '', // Log (message)
'm' => 'list', // Method (action)
'o' => 'home', // Object (content)
't' => 'bootstrap5', // Theme
'x' => '', // XHR (request)
];
public $out = [
'doc' => 'NetServa',
Expand All @@ -53,7 +57,7 @@
public $db = [
'host' => '127.0.0.1', // DB site
'name' => 'sysadm', // DB name
'pass' => 'lib'.DS.'.ht_pw', // MySQL password override
'pass' => 'lib' . DS . '.ht_pw', // MySQL password override
'path' => '/var/lib/sqlite/sysadm/sysadm.db', // SQLite DB
'port' => '3306', // DB port
'sock' => '', // '/run/mysqld/mysqld.sock',
Expand Down Expand Up @@ -90,8 +94,7 @@
], 'fas fa-chart-line fa-fw'],
],
];
public $nav2 = [
];
public $nav2 = [];
public $dns = [
'a' => '127.0.0.1',
'mx' => '',
Expand All @@ -110,7 +113,7 @@
'db' => [
'host' => '127.0.0.1', // Alt DNS DB site
'name' => 'pdns', // Alt DNS DB name
'pass' => 'lib'.DS.'.ht_dns_pw', // MySQL DNS password override
'pass' => 'lib' . DS . '.ht_dns_pw', // MySQL DNS password override
'path' => '/var/lib/sqlite/sysadm/pdns.db', // DNS SQLite DB
'port' => '3306', // Alt DNS DB port
'sock' => '', // '/run/mysqld/mysqld.sock',
Expand Down
26 changes: 15 additions & 11 deletions lib/php/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ class Init
public function __construct(object $g)
{
elog(__METHOD__);

session_start();

elog('GET='.var_export($_GET, true));
elog('POST='.var_export($_POST, true));
elog('SESSION='.var_export($_SESSION, true));
elog('GET=' . var_export($_GET, true));
elog('POST=' . var_export($_POST, true));
elog('SESSION=' . var_export($_SESSION, true));

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

Expand All @@ -32,15 +31,17 @@ public function __construct(object $g)
util::ses('o');
util::ses('m');
util::ses('l');
$t = util::ses('t', '', $g->in['t']);

$t1 = 'themes_'.$t.'_'.$g->in['o'];
$t2 = 'themes_'.$t.'_theme';
$t = util::ses('t', '', $g->in['t']);
$t1 = 'themes_' . $t . '_' . $g->in['o'];
$t2 = 'themes_' . $t . '_theme';

$this->t = $thm = class_exists($t1) ? new $t1($g)
$this->t = $thm = class_exists($t1)
? new $t1($g)
: (class_exists($t2) ? new $t2($g) : new Theme($g));

$p = 'plugins_'.$g->in['o'];
$p = 'plugins_' . $g->in['o'];

if (class_exists($p)) {
$g->in['a'] ? util::chkapi($g) : util::remember($g);
$g->out['main'] = (string) new $p($thm);
Expand All @@ -58,7 +59,7 @@ public function __construct(object $g)
public function __destruct()
{
//error_log('SESSION=' . var_export($_SESSION, true));
elog(__FILE__.' '.$_SERVER['REMOTE_ADDR'].' '.round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 4)."\n");
elog(__FILE__ . ' ' . $_SERVER['REMOTE_ADDR'] . ' ' . round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), 4) . "\n");
}

public function __toString(): string
Expand Down Expand Up @@ -91,7 +92,10 @@ public function __toString(): string
function dbg($var = null): void
{
if (is_object($var)) {
error_log(ReflectionObject::export($var, true));
$refobj = new \ReflectionObject($var);
// get all public and protected properties
$var = $refobj->getProperties(\ReflectionProperty::IS_PUBLIC);
$var = \array_merge($var, $refobj->getProperties(\ReflectionProperty::IS_PROTECTED));
}
ob_start();
print_r($var);
Expand Down
20 changes: 11 additions & 9 deletions lib/php/plugin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

declare(strict_types=1);
// lib/php/plugin.php 20150101 - 20200414
// Copyright (C) 2015-2020 Mark Constable <markc@renta.net> (AGPL-3.0)
// lib/php/plugin.php 20150101 - 20230604
// Copyright (C) 2015-2023 Mark Constable <markc@renta.net> (AGPL-3.0)

class Plugin
{
Expand All @@ -18,13 +18,15 @@ public function __construct(public Theme $t)

$o = $t->g->in['o'];
$m = $t->g->in['m'];

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

$this->t = $t;
$this->g = $t->g;
$this->in = util::esc($this->in);

if ($this->tbl) {
if (!is_null($this->dbh)) {
db::$dbh = $this->dbh;
Expand All @@ -46,9 +48,9 @@ public function __toString(): string

public function __call(string $name, array $args): string
{
elog(__METHOD__.'() name = '.$name.', args = '.var_export($args, true));
elog(__METHOD__ . '() name = ' . $name . ', args = ' . var_export($args, true));

return 'Plugin::'.$name.'() not implemented';
return 'Plugin::' . $name . '() not implemented';
}

protected function create(): string
Expand All @@ -59,7 +61,7 @@ protected function create(): string
$this->in['updated'] = date('Y-m-d H:i:s');
$this->in['created'] = date('Y-m-d H:i:s');
$lid = db::create($this->in);
util::log('Item number '.$lid.' created', 'success');
util::log('Item number ' . $lid . ' created', 'success');
util::relist();
} else {
return $this->t->create($this->in);
Expand All @@ -80,7 +82,7 @@ protected function update(): string
if (util::is_post()) {
$this->in['updated'] = date('Y-m-d H:i:s');
if (db::update($this->in, [['id', '=', $this->g->in['i']]])) {
util::log('Item number '.$this->g->in['i'].' updated', 'success');
util::log('Item number ' . $this->g->in['i'] . ' updated', 'success');
util::relist();
} else {
util::log('Error updating item.');
Expand All @@ -90,14 +92,14 @@ protected function update(): string
return $this->read();
}

protected function delete(): string
protected function delete(): void
{
elog(__METHOD__);

if (util::is_post()) {
if ($this->g->in['i']) {
$res = db::delete([['id', '=', $this->g->in['i']]]);
util::log('Item number '.$this->g->in['i'].' removed', 'success');
util::log('Item number ' . $this->g->in['i'] . ' removed', 'success');
util::relist();
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/php/plugins/accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function list(): string
['dt' => null, 'db' => 'id'],
['dt' => 0, 'db' => 'login', 'formatter' => function ($d, $row) {
return '
<b><a href="?o=accounts&m=read&i='.$row['id'].'">'.$d.'</a></b>';
<b><a href="?o=accounts&m=read&i=' . $row['id'] . '">' . $d . '</a></b>';
}],
['dt' => 1, 'db' => 'fname'],
['dt' => 2, 'db' => 'lname'],
Expand All @@ -92,7 +92,7 @@ protected function switch_user(): void

if (util::is_adm() and !is_null($this->g->in['i'])) {
$_SESSION['usr'] = db::read('id,acl,grp,login,fname,lname,webpw,cookie', 'id', $this->g->in['i'], '', 'one');
util::log('Switch to user: '.$_SESSION['usr']['login'], 'success');
util::log('Switch to user: ' . $_SESSION['usr']['login'], 'success');
} else {
util::log('Not authorized to switch users');
}
Expand Down
32 changes: 16 additions & 16 deletions lib/php/plugins/auth.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

declare(strict_types=1);
// lib/php/plugins/auth.php 20150101 - 20200414
// Copyright (C) 2015-2020 Mark Constable <markc@renta.net> (AGPL-3.0)
// lib/php/plugins/auth.php 20150101 - 20230604
// Copyright (C) 2015-2023 Mark Constable <markc@renta.net> (AGPL-3.0)

class Plugins_Auth extends Plugin
{
Expand Down Expand Up @@ -34,16 +34,16 @@ public function create(): string
if ($usr = db::read('id,acl', 'login', $u, '', 'one')) {
if (9 != $usr['acl']) {
$newpass = util::genpw(self::OTP_LENGTH);
if ($this->mail_forgotpw($u, $newpass, 'From: '.$this->g->cfg['email'])) {
if ($this->mail_forgotpw($u, $newpass, 'From: ' . $this->g->cfg['email'])) {
db::update([
'otp' => $newpass,
'otpttl' => time(),
], [['id', '=', $usr['id']]]);
util::log('Sent reset password key for "'.$u.'" so please check your mailbox and click on the supplied link.', 'success');
util::log('Sent reset password key for "' . $u . '" so please check your mailbox and click on the supplied link.', 'success');
} else {
util::log('Problem sending message to '.$u, 'danger');
util::log('Problem sending message to ' . $u, 'danger');
}
util::redirect($this->cfg['self'].'?o='.$this->g->in['o'].'&m=list');
util::redirect($this->g->cfg['self'] . '?o=' . $this->g->in['o'] . '&m=list');
} else {
util::log('Account is disabled, contact your System Administrator');
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public function list(): string
util::put_cookie('remember', $uniq, self::REMEMBER_ME_EXP);
}
$_SESSION['usr'] = $usr;
util::log($login.' is now logged in', 'success');
util::log($login . ' is now logged in', 'success');
if (0 === (int) $acl) {
$_SESSION['adm'] = $id;
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public function update(): string
'otpttl' => 0,
'updated' => date('Y-m-d H:i:s'),
], [['id', '=', $i]])) {
util::log('Password reset for '.$usr['login'], 'success');
util::log('Password reset for ' . $usr['login'], 'success');
if (util::is_usr()) {
util::redirect($this->g->cfg['self']);
} else {
Expand All @@ -134,7 +134,7 @@ public function update(): string
util::log('Problem updating database');
}
} else {
util::log($usr['login'].' is not allowed access');
util::log($usr['login'] . ' is not allowed access');
}
} else {
util::log('Your one time password key has expired');
Expand Down Expand Up @@ -163,7 +163,7 @@ public function delete(): void
db::update(['cookie' => ''], [['id', '=', $id]]);
$this->setcookie('remember', '', strtotime('-1 hour', 0));
}
util::log($u.' is now logged out', 'success');
util::log($u . ' is now logged out', 'success');
}
util::redirect($this->g->cfg['self']);
}
Expand All @@ -184,7 +184,7 @@ public function resetpw(): string

return $this->t->update(['id' => $id, 'login' => $login]);
}
util::log($login.' is not allowed access');
util::log($login . ' is not allowed access');
} else {
util::log('Your one time password key has expired');
}
Expand All @@ -201,20 +201,20 @@ private function mail_forgotpw(string $email, string $newpass, string $headers =
{
elog(__METHOD__);

$host = $_SERVER['REQUEST_SCHEME'].'://'
.$this->g->cfg['host']
.$this->g->cfg['self'];
$host = $_SERVER['REQUEST_SCHEME'] . '://'
. $this->g->cfg['host']
. $this->g->cfg['self'];

return mail(
"{$email}",
'Reset password for '.$this->g->cfg['host'],
'Reset password for ' . $this->g->cfg['host'],
'Here is your new OTP (one time password) key that is valid for one hour.
Please click on the link below and continue with reseting your password.
If you did not request this action then please ignore this message.
'.$host.'?o=auth&m=resetpw&otp='.$newpass,
' . $host . '?o=auth&m=resetpw&otp=' . $newpass,
$headers
);
}
Expand Down

0 comments on commit 48ff9ec

Please sign in to comment.