-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-phpmyadmin.php
More file actions
137 lines (113 loc) · 3.26 KB
/
setup-phpmyadmin.php
File metadata and controls
137 lines (113 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
*
* @Name: setup-phpmyadmin
* @Author: Max Base
* @Date: 06/06/2025
* @Repository: https://github.com/BaseMax/setup-phpmyadmin
*
*/
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
set_time_limit(0);
ini_set('memory_limit', '10240000000M');
ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/phpmyadmin_setup_errors.log');
// --- Helper Functions ---
function isCli() {
return (php_sapi_name() === 'cli' || defined('STDIN'));
}
function println($msg) {
echo $msg . (isCli() ? "\n" : "<br>\n");
}
function logMsg($msg) {
println('[' . date('H:i:s') . "] $msg");
}
function deleteDir($dir) {
if (!file_exists($dir)) return;
$items = array_diff(scandir($dir), ['.', '..']);
foreach ($items as $item) {
$path = "$dir/$item";
if (is_dir($path)) {
deleteDir($path);
} else {
if (!@unlink($path)) {
logMsg("⚠️ Failed to delete file: $path");
}
}
}
if (!@rmdir($dir)) {
logMsg("⚠️ Failed to remove directory: $dir");
}
}
function safeMkdir($dir, $permissions = 0755) {
if (!is_dir($dir)) {
mkdir($dir, $permissions, true);
}
}
// --- Configuration ---
$downloadUrl = 'https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.zip';
$zipFile = 'phpmyadmin.zip';
$pmaDir = 'pma';
$tempDir = 'temp_pma_extract';
// --- Step 1: Download zip ---
if (!file_exists($zipFile)) {
logMsg("Downloading phpMyAdmin...");
$download = file_get_contents($downloadUrl);
if ($download === false) {
println("❌ Failed to download from $downloadUrl");
exit(1);
}
file_put_contents($zipFile, $download);
}
// --- Step 2: Create necessary directories ---
safeMkdir($pmaDir);
safeMkdir($tempDir);
if (!extension_loaded('zip')) {
println("❌ PHP zip extension is not loaded.");
exit(1);
}
if (!class_exists('ZipArchive')) {
println("❌ PHP Zip extension is not installed or enabled.");
exit(1);
}
// --- Step 3: Extract zip to temp ---
$zip = new ZipArchive();
if ($zip->open($zipFile) !== TRUE) {
@unlink($zipFile);
println("❌ Failed to open ZIP file.");
exit(1);
}
// Detect top-level folder inside ZIP
$firstEntry = $zip->getNameIndex(0);
if ($firstEntry === false) {
println("❌ Failed to read ZIP entries.");
@unlink($zipFile);
exit(1);
}
$baseFolder = explode('/', $firstEntry)[0];
$sourcePath = "$tempDir/$baseFolder";
logMsg("Extracting ZIP to temporary directory...");
$zip->extractTo($tempDir);
$zip->close();
// --- Step 4: Copy extracted files into 'pma/' ---
logMsg("Moving files to '$pmaDir/'...");
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
$subPath = $iterator->getInnerIterator()->getSubPathName();
$targetPath = $pmaDir . DIRECTORY_SEPARATOR . $subPath;
if ($item->isDir()) {
safeMkdir($targetPath);
} else {
copy($item, $targetPath);
}
}
// --- Step 5: Cleanup ---
logMsg("Cleaning up temporary files...");
@unlink($zipFile);
deleteDir($tempDir);
logMsg("✅ phpMyAdmin is ready in the '$pmaDir/' directory.");