Skip to content

Commit dc54d6d

Browse files
committed
update
1 parent 94dbd82 commit dc54d6d

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

src/AdminerTool.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace MintyPHP\Tools;
4+
5+
use MintyPHP\DB;
6+
7+
class AdminerTool
8+
{
9+
private ?string $host;
10+
private ?int $port;
11+
private ?string $username;
12+
private ?string $password;
13+
private ?string $db;
14+
private string $url;
15+
private string $storagePath;
16+
17+
public static function run(): void
18+
{
19+
(new self(
20+
DB::$host ?? null,
21+
DB::$port ?? null,
22+
DB::$username ?? null,
23+
DB::$password ?? null,
24+
DB::$database ?? null
25+
))->execute();
26+
}
27+
28+
/**
29+
* Constructor with optional parameters
30+
*/
31+
public function __construct(?string $host, ?int $port, ?string $username, ?string $password, ?string $db, ?string $url = null, ?string $storagePath = null)
32+
{
33+
$this->host = $host;
34+
$this->port = $port;
35+
$this->username = $username;
36+
$this->password = $password;
37+
$this->db = $db;
38+
$this->url = $url ?: 'https://www.adminer.org/latest-en.php';
39+
$this->storagePath = $storagePath ?: (__DIR__ . '/latest-en.txt');
40+
}
41+
42+
/**
43+
* Download Adminer from the specified URL and store it
44+
*/
45+
public function download(): bool
46+
{
47+
// Download the file from the URL
48+
$content = file_get_contents($this->url);
49+
50+
if ($content === false) {
51+
return false;
52+
}
53+
54+
return file_put_contents($this->storagePath, $content) !== false;
55+
}
56+
57+
/**
58+
* Execute Adminer by including the stored file
59+
*/
60+
public function execute(): void
61+
{
62+
// Check if the file exists
63+
if (!file_exists($this->storagePath)) {
64+
// Download if not present
65+
if (!$this->download()) {
66+
throw new \RuntimeException('Failed to download Adminer');
67+
}
68+
}
69+
70+
// Read the file
71+
$content = file_get_contents($this->storagePath);
72+
73+
if ($content === false) {
74+
throw new \RuntimeException('Failed to read Adminer file');
75+
}
76+
77+
// database auto-login credentials
78+
if (!isset($_GET["username"])) {
79+
$_POST["auth"] = array(
80+
'driver' => 'server',
81+
'server' => $this->host,
82+
'port' => $this->port,
83+
'username' => $this->username,
84+
'password' => $this->password,
85+
'db' => $this->db,
86+
);
87+
}
88+
89+
// Adminer Extension
90+
$extension = <<<'EOD'
91+
function adminer_object()
92+
{
93+
class AdminerSoftware extends Adminer\Adminer
94+
{
95+
public function loginForm()
96+
{
97+
echo "<p><a href='?'>Click to login</a></p>\n";
98+
}
99+
}
100+
return new AdminerSoftware();
101+
}
102+
EOD;
103+
104+
// Execute using eval
105+
eval($extension);
106+
eval('?>' . $content);
107+
}
108+
}

src/ConfiguratorTool.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MintyPHP\Tools;
4+
5+
class ConfiguratorTool
6+
{
7+
public static function run(): void
8+
{
9+
(new self())->execute();
10+
}
11+
12+
public function execute(): void
13+
{
14+
// Implementation of the configurator tool goes here
15+
echo "Configurator Tool executed.";
16+
}
17+
}

src/DebuggerTool.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace MintyPHP\Tools;
4+
5+
use MintyPHP\Core\Buffer;
6+
use MintyPHP\Debugger;
7+
8+
class DebuggerTool
9+
{
10+
public static function run(): void
11+
{
12+
(new self())->execute(Debugger::view());
13+
}
14+
15+
public function execute(string $html): void
16+
{
17+
echo $html;
18+
}
19+
}

src/Server.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace MintyPHP\Tools;
4+
5+
use MintyPHP\Debugger;
6+
7+
class Server
8+
{
9+
// Mapping of paths to their respective handler classes
10+
const array MAPPING = [
11+
'/debugger.php' => DebuggerTool::class,
12+
'/adminer.php' => AdminerTool::class,
13+
'/configurator.php' => ConfiguratorTool::class,
14+
];
15+
16+
private string $documentRoot;
17+
private string $scriptName;
18+
19+
public function __construct(string $documentRoot, string $scriptName)
20+
{
21+
$this->documentRoot = $documentRoot;
22+
$this->scriptName = $scriptName;
23+
}
24+
25+
public function run(): bool
26+
{
27+
$dir = $this->documentRoot;
28+
$file = realpath($dir . $this->scriptName) ?: '';
29+
if (file_exists($file) && (strpos($file, $dir) === 0)) {
30+
return false;
31+
}
32+
33+
if (Debugger::$enabled && array_key_exists($this->scriptName, self::MAPPING)) {
34+
Debugger::$enabled = false;
35+
$handlerClass = self::MAPPING[$this->scriptName];
36+
$handlerClass::run();
37+
} else {
38+
$_SERVER['SCRIPT_NAME'] = '/index.php';
39+
chdir('web');
40+
header('X-Content-Type-Options: nosniff');
41+
header('X-Frame-Options: DENY');
42+
require 'index.php';
43+
}
44+
return true;
45+
}
46+
}
47+
48+
if (isset($_SERVER['DOCUMENT_ROOT']) && isset($_SERVER['SCRIPT_NAME'])) {
49+
require 'vendor/autoload.php';
50+
require 'config/config.php';
51+
return (new Server($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_NAME']))->run();
52+
}

0 commit comments

Comments
 (0)