Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for PHP 8.4 compatibility #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ whmcs/
rspamd/
phpliteadmin.*
.php-cs-fixer.cache
sysadm/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 36 additions & 37 deletions lib/php/util.php → php/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static function log(string $msg = '', string $lvl = 'danger'): array
elog(__METHOD__);

if ($msg) {
$_SESSION['log'][$lvl] = empty($_SESSION['log'][$lvl]) ? $msg : $_SESSION['log'][$lvl].'<br>'.$msg;
$_SESSION['log'][$lvl] = empty($_SESSION['log'][$lvl]) ? $msg : $_SESSION['log'][$lvl] . '<br>' . $msg;
} elseif (isset($_SESSION['log']) and $_SESSION['log']) {
$l = $_SESSION['log'];
$_SESSION['log'] = [];
Expand All @@ -24,7 +24,7 @@ public static function log(string $msg = '', string $lvl = 'danger'): array

public static function enc(string $v): string
{
elog(__METHOD__."({$v})");
elog(__METHOD__ . "({$v})");

return htmlentities(trim($v), ENT_QUOTES, 'UTF-8');
}
Expand All @@ -42,14 +42,13 @@ public static function esc(array $in): array
}

// TODO please document what $k, $v and $x are for?
public static function ses(string $k, string $v = '', string $x = null): string
public static function ses(string $k, string $v = '', ?string $x = null): string
{
elog(__METHOD__."({$k}, {$v}, {$x})");
elog(__METHOD__ . "({$k}, {$v}, {$x})");

return $_SESSION[$k] =
(!is_null($x) && (!isset($_SESSION[$k]) || ($_SESSION[$k] != $x))) ? $x :
(((isset($_REQUEST[$k]) && !isset($_SESSION[$k]))
|| (isset($_REQUEST[$k], $_SESSION[$k])
(!is_null($x) && (!isset($_SESSION[$k]) || ($_SESSION[$k] != $x))) ? $x : (((isset($_REQUEST[$k]) && !isset($_SESSION[$k]))
|| (isset($_REQUEST[$k], $_SESSION[$k])
&& ($_REQUEST[$k] != $_SESSION[$k])))
? self::enc($_REQUEST[$k])
: ($_SESSION[$k] ?? $v));
Expand All @@ -68,22 +67,22 @@ public static function cfg(object $g): void

public static function exe(string $cmd, bool $ret = false): bool
{
elog(__METHOD__."({$cmd})");
elog(__METHOD__ . "({$cmd})");

exec('sudo '.escapeshellcmd($cmd).' 2>&1', $retArr, $retVal);
util::log('<pre>'.trim(implode("\n", $retArr)).'</pre>', $retVal ? 'danger' : 'success');
exec('sudo ' . escapeshellcmd($cmd) . ' 2>&1', $retArr, $retVal);
util::log('<pre>' . trim(implode("\n", $retArr)) . '</pre>', $retVal ? 'danger' : 'success');

return (boolval($retVal) ? true : false);
}

public static function run(string $cmd): string
{
elog(__METHOD__."({$cmd})");
elog(__METHOD__ . "({$cmd})");

return exec('sudo '.escapeshellcmd($cmd).' 2>&1');
return exec('sudo ' . escapeshellcmd($cmd) . ' 2>&1');
}

public static function now(string $date1, string $date2 = null): string
public static function now(string $date1, ?string $date2 = null): string
{
elog(__METHOD__);

Expand Down Expand Up @@ -119,13 +118,13 @@ public static function now(string $date1, string $date2 = null): string
if ($diff / $block['v'] >= 1) {
$amount = floor($diff / $block['v']);
$plural = ($amount > 1) ? 's' : '';
$result[] = $amount.' '.$block['k'].$plural;
$result[] = $amount . ' ' . $block['k'] . $plural;
$diff -= $amount * $block['v'];
++$current_level;
}
}

return implode(' ', $result).' ago';
return implode(' ', $result) . ' ago';
}

public static function is_adm(): bool
Expand All @@ -135,7 +134,7 @@ public static function is_adm(): bool
return isset($_SESSION['adm']);
}

public static function is_usr(int|string $id = null): bool
public static function is_usr(mixed $id = null): bool
{
elog(__METHOD__);

Expand Down Expand Up @@ -278,7 +277,7 @@ public static function remember(object $g): void
if (0 == $acl) {
$_SESSION['adm'] = $id;
}
self::log($login.' is remembered and logged back in', 'success');
self::log($login . ' is remembered and logged back in', 'success');
self::ses('o', '', $g->in['o']);
self::ses('m', '', $g->in['m']);
}
Expand All @@ -288,16 +287,16 @@ public static function remember(object $g): void

public static function redirect(string $url, string $method = 'location', int $ttl = 5, string $msg = ''): void
{
elog(__METHOD__."({$url})");
elog(__METHOD__ . "({$url})");

if ('refresh' == $method) {
header('refresh:'.$ttl.'; url='.$url);
header('refresh:' . $ttl . '; url=' . $url);
echo '<!DOCTYPE html>
<title>Redirect...</title>
<h2 style="text-align:center">Redirecting in '.$ttl.' seconds...</h2>
<pre style="width:50em;margin:0 auto;">'.$msg.'</pre>';
<h2 style="text-align:center">Redirecting in ' . $ttl . ' seconds...</h2>
<pre style="width:50em;margin:0 auto;">' . $msg . '</pre>';
} else {
header('Location:'.$url);
header('Location:' . $url);
}

exit;
Expand All @@ -307,30 +306,30 @@ public static function relist(): void
{
elog(__METHOD__);

self::redirect('?o='.$_SESSION['o'].'&m=list');
self::redirect('?o=' . $_SESSION['o'] . '&m=list');
}

public static function numfmt(float $size, int $precision = null): string
public static function numfmt(float $size, ?int $precision = null): string
{
elog(__METHOD__);

if (0 == $size) {
return '0';
}
if ($size >= 1000000000000) {
return round(($size / 1000000000000), $precision ?? 3).' TB';
return round(($size / 1000000000000), $precision ?? 3) . ' TB';
}
if ($size >= 1000000000) {
return round(($size / 1000000000), $precision ?? 2).' GB';
return round(($size / 1000000000), $precision ?? 2) . ' GB';
}
if ($size >= 1000000) {
return round(($size / 1000000), $precision ?? 1).' MB';
return round(($size / 1000000), $precision ?? 1) . ' MB';
}
if ($size >= 1000) {
return round(($size / 1000), $precision ?? 0).' KB';
return round(($size / 1000), $precision ?? 0) . ' KB';
}

return $size.' Bytes';
return $size . ' Bytes';
}

// numfmt() was wrong, we want MB not MiB
Expand All @@ -344,7 +343,7 @@ public static function numfmtsi(float $size, int $precision = 2): string
$base = log($size, 1024);
$suffixes = [' Bytes', ' KiB', ' MiB', ' GiB', ' TiB'];

return round(1024 ** ($base - floor($base)), $precision).$suffixes[floor($base)];
return round(1024 ** ($base - floor($base)), $precision) . $suffixes[floor($base)];
}

public static function is_valid_domain_name(string $domainname): bool
Expand All @@ -354,8 +353,8 @@ public static function is_valid_domain_name(string $domainname): bool
$domainname = idn_to_ascii($domainname);

return preg_match('/^([a-z\\d](-*[a-z\\d])*)(\\.([a-z\\d](-*[a-z\\d])*))*$/i', $domainname)
&& preg_match('/^.{1,253}$/', $domainname)
&& preg_match('/^[^\\.]{1,63}(\\.[^\\.]{1,63})*$/', $domainname);
&& preg_match('/^.{1,253}$/', $domainname)
&& preg_match('/^[^\\.]{1,63}(\\.[^\\.]{1,63})*$/', $domainname);
}

public static function mail_password(string $pw, string $hash = 'SHA512-CRYPT'): string
Expand All @@ -365,8 +364,8 @@ public static function mail_password(string $pw, string $hash = 'SHA512-CRYPT'):
$salt_str = bin2hex(openssl_random_pseudo_bytes(8));

return 'SHA512-CRYPT' === $hash
? '{SHA512-CRYPT}'.crypt($pw, '$6$'.$salt_str.'$')
: '{SSHA256}'.base64_encode(hash('sha256', $pw.$salt_str, true).$salt_str);
? '{SHA512-CRYPT}' . crypt($pw, '$6$' . $salt_str . '$')
: '{SSHA256}' . base64_encode(hash('sha256', $pw . $salt_str, true) . $salt_str);
}

public static function sec2time(int $seconds): string
Expand All @@ -386,7 +385,7 @@ public static function is_post(): bool
if ('POST' === $_SERVER['REQUEST_METHOD']) {
if (!isset($_POST['c']) || $_SESSION['c'] !== $_POST['c']) {
self::log('Possible CSRF attack');
self::redirect('?o='.$_SESSION['o'].'&m=list');
self::redirect('?o=' . $_SESSION['o'] . '&m=list');
}

return true;
Expand All @@ -404,8 +403,8 @@ public static function inc_soa(string $soa): string
$day = substr($ary[2], 0, 8);
$rev = substr($ary[2], -2);
$ary[2] = ($day == $ymd)
? "{$ymd}".sprintf('%02d', $rev + 1)
: "{$ymd}".'00';
? "{$ymd}" . sprintf('%02d', $rev + 1)
: "{$ymd}" . '00';

return implode(' ', $ary);
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
19 changes: 10 additions & 9 deletions index.php → public/index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

declare(strict_types=1);
// index.php 20150101 - 20230604
// Copyright (C) 2015-2023 Mark Constable <[email protected]> (AGPL-3.0)
// index.php 20150101 - 20250128
// Copyright (C) 2015-2025 Mark Constable <[email protected]> (AGPL-3.0)

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

spl_autoload_register(function ($c): void {
Expand All @@ -28,7 +29,7 @@
'hash' => 'SHA512-CRYPT',
'host' => '',
'perp' => 25,
'self' => '/hcp/',
'self' => '/',
];
public $in = [
'a' => '', // API (apiusr:apikey)
Expand Down Expand Up @@ -57,11 +58,11 @@
public $db = [
'host' => '127.0.0.1', // DB site
'name' => 'sysadm', // DB name
'pass' => 'lib' . DS . '.ht_pw', // MySQL password override
'path' => '/var/lib/sqlite/sysadm/sysadm.db', // SQLite DB
'pass' => 'php' . DS . '.ht_pw', // MySQL password override
'path' => 'sysadm.db', // SQLite DB
'port' => '3306', // DB port
'sock' => '', // '/run/mysqld/mysqld.sock',
'type' => 'mysql', // mysql | sqlite
'type' => 'sqlite', // mysql | sqlite
'user' => 'sysadm', // DB user
];
public $nav1 = [
Expand Down Expand Up @@ -114,10 +115,10 @@
'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
'path' => '/var/lib/sqlite/sysadm/pdns.db', // DNS SQLite DB
'path' => 'pdns.db', // DNS SQLite DB
'port' => '3306', // Alt DNS DB port
'sock' => '', // '/run/mysqld/mysqld.sock',
'type' => '', // mysql | sqlite | '' to disable
'type' => 'sqlite', // mysql | sqlite | '' to disable
'user' => 'pdns', // Alt DNS DB user
],
];
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.