Skip to content

Commit 8065066

Browse files
committed
Add support for custom auth
1 parent 9263155 commit 8065066

5 files changed

Lines changed: 46 additions & 8 deletions

File tree

config.dist.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@
3737
],
3838
'timeformat' => 'd. m. Y H:i:s',
3939
'twigdebug' => false,
40+
// Auth function, this is an example with http auth, but you can add own logic
41+
/*'auth' => static function (): void {
42+
$username = 'admin';
43+
$password = 'pass';
44+
45+
if (
46+
!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) ||
47+
$_SERVER['PHP_AUTH_USER'] !== $username || $_SERVER['PHP_AUTH_PW'] !== $password
48+
) {
49+
Header('WWW-Authenticate: Basic realm="phpCacheAdmin Login"');
50+
Header('HTTP/1.0 401 Unauthorized');
51+
52+
echo 'Incorrect username or password!';
53+
exit;
54+
}
55+
56+
if (isset($_GET['logout'])) {
57+
$is_https = (
58+
(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1)) ||
59+
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
60+
);
61+
62+
$current_url = sprintf('http%s://reset:reset@%s', ($is_https ? 's' : ''), $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
63+
64+
RobiNN\Pca\Http::redirect([], [], $current_url);
65+
}
66+
},*/
4067
// Decoding/Encoding functions
4168
'encoding' => [
4269
'gzcompress' => [

index.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
declare(strict_types=1);
1212

1313
use RobiNN\Pca\Admin;
14+
use RobiNN\Pca\Config;
1415
use RobiNN\Pca\Http;
1516
use RobiNN\Pca\Template;
1617

@@ -31,6 +32,13 @@
3132
});
3233
}
3334

35+
$auth = false;
36+
37+
if (is_callable(Config::get('auth'))) {
38+
Config::get('auth')();
39+
$auth = true;
40+
}
41+
3442
$tpl = new Template();
3543
$admin = new Admin($tpl);
3644

@@ -54,6 +62,7 @@
5462
echo $tpl->render('layout', [
5563
'site_title' => $info['title'],
5664
'nav' => $nav,
65+
'logout_url' => $auth ? Http::queryString([], ['logout' => 'yes']) : null,
5766
'version' => Admin::VERSION,
5867
'back' => isset($_GET['moreinfo']) || isset($_GET['view']) || isset($_GET['form']),
5968
'back_url' => Http::queryString(['db', 's']),

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function get(?string $key = null) {
2929

3030
self::getEnvConfig($config);
3131

32-
return $config[$key] ?? $config;
32+
return $config[$key] ?? null;
3333
}
3434

3535
/**

src/Http.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ public static function queryString(array $filter = [], array $additional = []):
2626
$filter = array_flip(array_merge($keep, $filter));
2727
$url = parse_url($_SERVER['REQUEST_URI']);
2828

29-
if (empty($url['query'])) {
30-
return $url['path'];
31-
}
32-
33-
parse_str($url['query'], $query);
29+
parse_str($url['query'] ?? '', $query);
3430

3531
$query = array_intersect_key($query, $filter);
3632
$query += $additional;
@@ -91,11 +87,12 @@ public static function post(string $key, string $type = 'string') {
9187
*
9288
* @param array<int|string, string> $filter
9389
* @param array<int|string, string> $additional
90+
* @param ?string $url
9491
*
9592
* @return void
9693
*/
97-
public static function redirect(array $filter = [], array $additional = []): void {
98-
$location = self::queryString($filter, $additional);
94+
public static function redirect(array $filter = [], array $additional = [], ?string $url = null): void {
95+
$location = $url ?? self::queryString($filter, $additional);
9996

10097
if (!headers_sent()) {
10198
header('Location: '.$location, true);

templates/layout.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
</a>
1919
{% endfor %}
2020
<footer class="text-center text-white my-4">
21+
22+
{% if logout_url %}
23+
<a class="block py-2 text-white hover:text-gray-200" href="{{ logout_url }}">Logout</a>
24+
{% endif %}
25+
2126
<div class="font-light">
2227
&copy; {{ 'now'|date('Y') }}
2328
<a class="text-white hover:text-gray-200 font-semibold" href="https://github.com/RobiNN1/phpCacheAdmin" target="_blank" rel="noopener">

0 commit comments

Comments
 (0)