-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
487 lines (409 loc) · 14.2 KB
/
install.php
File metadata and controls
487 lines (409 loc) · 14.2 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* GeoLite Installation Handler
* Handles the installation process including database setup and configuration
*/
class GeoLiteInstaller {
private $db_host;
private $db_name;
private $db_user;
private $db_pass;
private $db_port;
private $admin_password;
private $pdo;
private $errors = [];
private $success_messages = [];
public function __construct($config) {
$this->db_host = trim($config['db_host'] ?? '');
$this->db_name = trim($config['db_name'] ?? '');
$this->db_user = trim($config['db_user'] ?? '');
$this->db_pass = trim($config['db_pass'] ?? '');
$this->db_port = trim($config['db_port'] ?? '5432');
$this->admin_password = trim($config['admin_password'] ?? '');
}
public function validate() {
$this->errors = [];
if (empty($this->db_host)) {
$this->errors[] = 'Database host is required.';
}
if (empty($this->db_name)) {
$this->errors[] = 'Database name is required.';
}
if (empty($this->db_user)) {
$this->errors[] = 'Database username is required.';
}
if (empty($this->db_pass)) {
$this->errors[] = 'Database password is required.';
}
if (empty($this->admin_password)) {
$this->errors[] = 'Admin password is required.';
}
if (!is_numeric($this->db_port) || $this->db_port < 1 || $this->db_port > 65535) {
$this->errors[] = 'Database port must be a valid port number (1-65535).';
}
if (strlen($this->admin_password) < 6) {
$this->errors[] = 'Admin password must be at least 6 characters long.';
}
return empty($this->errors);
}
public function testDatabaseConnection() {
try {
$this->pdo = new PDO(
"pgsql:host={$this->db_host};port={$this->db_port};dbname={$this->db_name}",
$this->db_user,
$this->db_pass,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false
]
);
return true;
} catch (PDOException $e) {
$this->errors[] = 'Database connection failed: ' . $e->getMessage();
return false;
}
}
public function updateConfigFile() {
$const_content = "<?php
const DB_HOST=\"{$this->db_host}\";
const DB_NAME=\"{$this->db_name}\";
const DB_USER=\"{$this->db_user}\";
const DB_PASS=\"{$this->db_pass}\";
const DB_PORT = {$this->db_port};
const DB_SCMA='public';
const SESS_USR_KEY = 'qgis_user';
const WWW_DIR = '".__DIR__."';
const DATA_DIR = '".dirname(__DIR__)."/data';
";
// Ensure incl directory exists
if (!is_dir('incl')) {
if (!mkdir('incl', 0755, true)) {
$this->errors[] = 'Failed to create incl directory. Check permissions.';
return false;
}
}
if (file_put_contents('incl/const.php', $const_content) === false) {
$this->errors[] = 'Failed to write const.php file. Check file permissions.';
return false;
}
$this->success_messages[] = 'Configuration file updated successfully.';
return true;
}
public function runDatabaseSetup() {
try {
// Read SQL files
$setup_sql = file_get_contents('installer/setup.sql');
$init_sql = file_get_contents('installer/init.sql');
if ($setup_sql === false) {
$this->errors[] = 'Failed to read installer/setup.sql file.';
return false;
}
if ($init_sql === false) {
$this->errors[] = 'Failed to read installer/init.sql file.';
return false;
}
// Replace placeholder password with actual hash
$admin_password_hash = password_hash($this->admin_password, PASSWORD_DEFAULT);
$init_sql = str_replace('ADMIN_APP_PASS', $admin_password_hash, $init_sql);
// Execute setup SQL (creates tables)
$this->pdo->exec($setup_sql);
$this->success_messages[] = 'Database tables created successfully.';
// Execute init SQL (inserts initial data)
$this->pdo->exec($init_sql);
$this->success_messages[] = 'Initial data inserted successfully.';
return true;
} catch (PDOException $e) {
$this->errors[] = 'Database setup failed: ' . $e->getMessage();
return false;
}
}
public function createDataDirectory() {
$data_dir = __DIR__ . '/../data';
if (!is_dir($data_dir)) {
if (!mkdir($data_dir, 0755, true)) {
$this->errors[] = 'Failed to create data directory. Check permissions.';
return false;
}
if (!mkdir($data_dir.'/uploads', 0755, true)) {
$this->errors[] = 'Failed to create data/uploads directory. Check permissions.';
return false;
}
}
if (!is_dir('assets')) {
if (!mkdir('assets', 0755, true)) {
$this->errors[] = 'Failed to create assets directory. Check permissions.';
return false;
}
}
if (!is_dir('assets/brand')) {
if (!mkdir('assets/brand', 0755, true)) {
$this->errors[] = 'Failed to create assets/brand directory. Check permissions.';
return false;
}
}
$this->success_messages[] = 'Required directories created successfully.';
return true;
}
public function install() {
if (!$this->validate()) {
return false;
}
if (!$this->testDatabaseConnection()) {
return false;
}
if (!$this->updateConfigFile()) {
return false;
}
if (!$this->createDataDirectory()) {
return false;
}
if (!$this->runDatabaseSetup()) {
return false;
}
$this->success_messages[] = 'Installation completed successfully!';
return true;
}
public function getErrors() {
return $this->errors;
}
public function getSuccessMessages() {
return $this->success_messages;
}
public function hasErrors() {
return !empty($this->errors);
}
public function getFirstError() {
return !empty($this->errors) ? $this->errors[0] : '';
}
public function getAllMessages() {
return array_merge($this->success_messages, $this->errors);
}
public function cleanup(){
# remove install files
$files = scandir('installer');
foreach($files as $f){
unlink('installer/'.$f);
}
rmdir('installer');
unlink('install.php');
}
}
// Check if already installed
if (file_exists('incl/const.php')) {
$const_content = file_get_contents('incl/const.php');
if (strpos($const_content, '${APP_DB}') === false) {
// Already installed, redirect to main page
header('Location: index.php');
exit;
}
}
$error = '';
$success = '';
// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$installer = new GeoLiteInstaller($_POST);
if ($installer->install()) {
$installer->cleanup();
$success = implode(' ', $installer->getSuccessMessages()) . ' You can now <a href="index.php">access the application</a>.';
} else {
$error = $installer->getFirstError();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GeoLite Installation</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.install-container {
background: white;
border-radius: 12px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
padding: 40px;
max-width: 500px;
width: 100%;
}
.logo {
text-align: center;
margin-bottom: 30px;
}
.logo h1 {
color: #667eea;
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 10px;
}
.logo p {
color: #666;
font-size: 1.1rem;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 500;
}
.form-group input {
width: 100%;
padding: 12px 16px;
border: 2px solid #e1e5e9;
border-radius: 8px;
font-size: 16px;
transition: border-color 0.3s ease;
}
.form-group input:focus {
outline: none;
border-color: #667eea;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.btn {
width: 100%;
padding: 14px;
background: #667eea;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: background-color 0.3s ease;
}
.btn:hover {
background: #5a6fd8;
}
.btn:disabled {
background: #ccc;
cursor: not-allowed;
}
.alert {
padding: 12px 16px;
border-radius: 8px;
margin-bottom: 20px;
font-weight: 500;
}
.alert-error {
background: #fee;
color: #c33;
border: 1px solid #fcc;
}
.alert-success {
background: #efe;
color: #363;
border: 1px solid #cfc;
}
.alert-success a {
color: #667eea;
text-decoration: none;
font-weight: 600;
}
.alert-success a:hover {
text-decoration: underline;
}
.requirements {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.requirements h3 {
color: #333;
margin-bottom: 10px;
}
.requirements ul {
list-style: none;
padding-left: 0;
}
.requirements li {
padding: 5px 0;
color: #666;
}
.requirements li:before {
content: "✓";
color: #28a745;
font-weight: bold;
margin-right: 8px;
}
</style>
</head>
<body>
<div class="install-container">
<div class="logo">
<h1>GeoLite</h1>
<p>Map Builder & Dashboard Installation</p>
</div>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<div style="text-align: center; margin-top: 20px;">
<a href="install_check.php" class="btn" style="background: #28a745; text-decoration: none; display: inline-block; width: auto; padding: 10px 20px;">Check Installation Status</a>
</div>
<?php else: ?>
<div class="requirements">
<h3>Requirements</h3>
<ul>
<li>PostgreSQL database server</li>
<li>PHP 7.4+ with PDO PostgreSQL extension</li>
<li>Web server (Apache/Nginx)</li>
<li>Write permissions for incl/ directory</li>
</ul>
</div>
<form method="POST">
<div class="form-group">
<label for="db_host">Database Host</label>
<input type="text" id="db_host" name="db_host" value="localhost" required>
</div>
<div class="form-row">
<div class="form-group">
<label for="db_name">Database Name</label>
<input type="text" id="db_name" name="db_name" required>
</div>
<div class="form-group">
<label for="db_port">Port</label>
<input type="number" id="db_port" name="db_port" value="5432" required>
</div>
</div>
<div class="form-group">
<label for="db_user">Database Username</label>
<input type="text" id="db_user" name="db_user" required>
</div>
<div class="form-group">
<label for="db_pass">Database Password</label>
<input type="password" id="db_pass" name="db_pass" required>
</div>
<div class="form-group">
<label for="admin_password">Admin Password</label>
<input type="password" id="admin_password" name="admin_password" placeholder="Password for admin user" required>
</div>
<button type="submit" class="btn">Install GeoLite</button>
</form>
<?php endif; ?>
</div>
</body>
</html>