-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress-installer.php
More file actions
210 lines (182 loc) · 5.08 KB
/
wordpress-installer.php
File metadata and controls
210 lines (182 loc) · 5.08 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* WordPress Auto Installer (FA/EN)
* Author: Seyyed Ali Mohammadiyeh (Max Base)
* License: MIT
* Year: 2025
*
* Supports:
* - Web mode: shows HTML form to select language
* - CLI mode: supports flags like `--lang=fa` or asks interactively
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('WP_FA_URL', 'https://fa.wordpress.org/latest-fa_IR.zip');
define('WP_EN_URL', 'https://wordpress.org/latest.zip');
define('DOWNLOAD_DIR', __DIR__ . '/downloads');
define('EXTRACT_DIR', __DIR__ . '/wordpress');
if (php_sapi_name() === 'cli') {
handle_cli();
} else {
handle_web();
}
/**
* ---------------------------
* CLI MODE HANDLER
* ---------------------------
*/
function handle_cli()
{
global $argv;
echo "=== WordPress Auto Installer ===\n";
// Parse language flag
$lang = null;
foreach ($argv as $arg) {
if (strpos($arg, '--lang=') === 0) {
$lang = substr($arg, strlen('--lang='));
}
}
if (!$lang) {
echo "Which language do you want? [fa/en]: ";
$lang = trim(fgets(STDIN));
}
$lang = strtolower($lang);
if (!in_array($lang, ['fa', 'en'])) {
echo "Invalid language. Must be 'fa' or 'en'.\n";
exit(1);
}
$url = $lang === 'fa' ? WP_FA_URL : WP_EN_URL;
echo "Selected language: $lang\n";
run_installation($url);
}
/**
* ---------------------------
* WEB MODE HANDLER
* ---------------------------
*/
function handle_web()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$lang = strtolower($_POST['lang'] ?? '');
if (!in_array($lang, ['fa', 'en'])) {
echo "<p style='color:red;'>Invalid language selected.</p>";
show_form();
return;
}
$url = $lang === 'fa' ? WP_FA_URL : WP_EN_URL;
echo "<pre>";
run_installation($url, true);
echo "</pre>";
return;
}
show_form();
}
/**
* ---------------------------
* HTML FORM (WEB MODE)
* ---------------------------
*/
function show_form()
{
echo <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WordPress Auto Installer</title>
<style>
body { font-family: sans-serif; background: #f7f7f7; padding: 30px; }
form { background: #fff; padding: 20px; border-radius: 8px; max-width: 400px; margin: auto; box-shadow: 0 0 5px rgba(0,0,0,0.1); }
select, button { width: 100%; padding: 10px; margin-top: 10px; }
</style>
</head>
<body>
<form method="POST">
<h2>WordPress Auto Installer</h2>
<label for="lang">Select language:</label>
<select name="lang" id="lang" required>
<option value="fa">فارسی (fa)</option>
<option value="en">English (en)</option>
</select>
<button type="submit">Install</button>
</form>
</body>
</html>
HTML;
}
/**
* ---------------------------
* DOWNLOAD + EXTRACT LOGIC
* ---------------------------
*/
function run_installation(string $url, bool $webOutput = false)
{
if (!is_dir(DOWNLOAD_DIR)) {
mkdir(DOWNLOAD_DIR, 0777, true);
}
$filename = basename($url);
$filepath = DOWNLOAD_DIR . '/' . $filename;
echo_msg("Downloading $url ...", $webOutput);
if (!download_file($url, $filepath)) {
echo_msg("❌ Failed to download file.", $webOutput);
exit(1);
}
echo_msg("✅ Download completed: $filepath", $webOutput);
if (!class_exists('ZipArchive')) {
echo_msg("⚠️ ZipArchive not available. Please extract manually.", $webOutput);
return;
}
if (is_dir(EXTRACT_DIR)) {
echo_msg("🧹 Removing old installation folder: " . EXTRACT_DIR, $webOutput);
remove_dir(EXTRACT_DIR);
}
echo_msg("📦 Extracting ZIP...", $webOutput);
$zip = new ZipArchive();
if ($zip->open($filepath) === true) {
$zip->extractTo(__DIR__);
$zip->close();
echo_msg("✅ Extraction completed to: " . __DIR__, $webOutput);
} else {
echo_msg("❌ Failed to extract ZIP file.", $webOutput);
}
echo_msg("🎉 WordPress setup ready in: " . realpath(EXTRACT_DIR), $webOutput);
}
/**
* ---------------------------
* HELPERS
* ---------------------------
*/
function download_file(string $url, string $path): bool
{
$fp = fopen($path, 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $result !== false;
}
function echo_msg(string $msg, bool $web = false)
{
if ($web) {
echo htmlspecialchars($msg) . "<br>";
@ob_flush(); flush();
} else {
echo $msg . PHP_EOL;
}
}
function remove_dir(string $dir)
{
if (!is_dir($dir)) return;
$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$path = $dir . '/' . $item;
if (is_dir($path)) remove_dir($path);
else unlink($path);
}
rmdir($dir);
}