|
| 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 | +} |
0 commit comments