-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
189 lines (173 loc) · 8.19 KB
/
setup.php
File metadata and controls
189 lines (173 loc) · 8.19 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
<?php
declare(strict_types=1);
// Solo accesible desde localhost
if (!in_array($_SERVER['REMOTE_ADDR'] ?? '', ['127.0.0.1', '::1'], true)) {
http_response_code(403);
exit('Solo configurable desde este equipo (localhost).');
}
$envPath = __DIR__ . '/.env';
// Leer valores actuales del .env si existe
$cfg = [
'db_host' => '127.0.0.1',
'db_port' => '3306',
'db_user' => 'root',
'db_pass' => '',
'db_name' => 'mycloud',
'allow_registration' => 'true',
'base_url' => '',
'timezone' => 'Europe/Madrid',
];
if (is_file($envPath)) {
foreach (file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#') || !str_contains($line, '=')) continue;
[$k, $v] = array_map('trim', explode('=', $line, 2));
$map = [
'DB_HOST' => 'db_host',
'DB_PORT' => 'db_port',
'DB_USER' => 'db_user',
'DB_PASS' => 'db_pass',
'DB_NAME' => 'db_name',
'ALLOW_REGISTRATION' => 'allow_registration',
'BASE_URL' => 'base_url',
'TIMEZONE' => 'timezone',
];
if (isset($map[$k])) $cfg[$map[$k]] = $v;
}
}
$ok = '';
$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$cfg['db_host'] = trim((string)($_POST['db_host'] ?? '127.0.0.1'));
$cfg['db_port'] = (string)(int)($_POST['db_port'] ?? 3306);
$cfg['db_user'] = trim((string)($_POST['db_user'] ?? 'root'));
$cfg['db_pass'] = (string)($_POST['db_pass'] ?? '');
$cfg['db_name'] = trim((string)($_POST['db_name'] ?? 'mycloud'));
$cfg['allow_registration'] = isset($_POST['allow_registration']) ? 'true' : 'false';
$cfg['base_url'] = trim((string)($_POST['base_url'] ?? ''));
$cfg['timezone'] = trim((string)($_POST['timezone'] ?? 'Europe/Madrid'));
// Construir contenido del .env
$env = "# Nube familiar — configuración generada por setup.php\n";
$env .= "# NO subas este fichero a repositorios públicos\n\n";
$env .= "DB_HOST={$cfg['db_host']}\n";
$env .= "DB_PORT={$cfg['db_port']}\n";
$env .= "DB_USER={$cfg['db_user']}\n";
$env .= "DB_PASS={$cfg['db_pass']}\n";
$env .= "DB_NAME={$cfg['db_name']}\n\n";
$env .= "ALLOW_REGISTRATION={$cfg['allow_registration']}\n";
$env .= "BASE_URL={$cfg['base_url']}\n";
$env .= "TIMEZONE={$cfg['timezone']}\n";
if (@file_put_contents($envPath, $env) === false) {
$err = 'No se pudo escribir el fichero .env. Comprueba los permisos de escritura.';
} else {
try {
$pdoOpts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$tmp = new PDO(
"mysql:host={$cfg['db_host']};port={$cfg['db_port']};charset=utf8mb4",
$cfg['db_user'], $cfg['db_pass'], $pdoOpts
);
$tmp->exec("CREATE DATABASE IF NOT EXISTS `{$cfg['db_name']}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
$pdo = new PDO(
"mysql:host={$cfg['db_host']};port={$cfg['db_port']};dbname={$cfg['db_name']};charset=utf8mb4",
$cfg['db_user'], $cfg['db_pass'], $pdoOpts
);
$pdo->exec('
CREATE TABLE IF NOT EXISTS usuarios (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
pass_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
');
@mkdir(__DIR__ . '/files/video', 0755, true);
$ok = '.env guardado y base de datos verificada correctamente. Ya puedes ir a index.php';
} catch (Throwable $e) {
$err = 'Error al conectar con la base de datos: ' . $e->getMessage();
}
}
}
?>
<!doctype html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Asistente de configuración</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; background: #0b1020; color: #fff; font: 16px/1.5 system-ui, sans-serif; }
.card { max-width: 700px; margin: 28px auto; padding: 24px; background: #121a35;
border-radius: 16px; border: 1px solid #1e2d55; }
h2 { margin: 0 0 16px; }
h3 { margin: 20px 0 10px; color: #a8b3d1; font-size: 1rem; text-transform: uppercase;
letter-spacing: .05em; }
label { display: block; margin: 12px 0 4px; font-size: .9rem; color: #a8b3d1; }
input[type=text], input[type=password], input[type=number] {
width: 100%; padding: 10px 12px; border-radius: 10px;
border: 1px solid #26335a; background: #0f1837; color: #fff; font-size: 1rem; }
input:focus { outline: 2px solid #3f61ff; border-color: transparent; }
.row { display: flex; gap: 12px; }
.row > div { flex: 1; }
.chk { display: flex; align-items: center; gap: 8px; margin-top: 12px; cursor: pointer; }
.chk input { width: auto; }
button { background: #0e63e0; color: #fff; border: 0; border-radius: 10px;
padding: 10px 18px; margin-top: 16px; cursor: pointer; font-weight: 700;
font-size: 1rem; }
button:hover { background: #1a72f0; }
.muted { color: #a8b3d1; font-size: .85rem; }
.ok { color: #7cffb2; background: #0e3e20; padding: 10px 14px; border-radius: 10px; margin: 12px 0; }
.err { color: #ff9aa2; background: #3e0e17; padding: 10px 14px; border-radius: 10px; margin: 12px 0; }
a { color: #6ca0ff; }
code { background: #0f1837; padding: 2px 6px; border-radius: 4px; font-size: .9em; }
</style>
</head>
<body>
<div class="card">
<h2>⚙️ Asistente de configuración</h2>
<p class="muted">Este asistente genera el fichero <code>.env</code> con tus credenciales.
Las contraseñas <strong>no</strong> quedan en código fuente.</p>
<?php if ($ok): ?><div class="ok">✓ <?= htmlspecialchars($ok) ?></div><?php endif; ?>
<?php if ($err): ?><div class="err">✗ <?= htmlspecialchars($err) ?></div><?php endif; ?>
<form method="post" autocomplete="off">
<h3>Base de datos</h3>
<div class="row">
<div>
<label for="db_host">Host</label>
<input id="db_host" type="text" name="db_host" value="<?= htmlspecialchars($cfg['db_host']) ?>">
</div>
<div style="flex:0 0 110px">
<label for="db_port">Puerto</label>
<input id="db_port" type="number" name="db_port" value="<?= (int)$cfg['db_port'] ?>">
</div>
</div>
<div class="row">
<div>
<label for="db_user">Usuario</label>
<input id="db_user" type="text" name="db_user" value="<?= htmlspecialchars($cfg['db_user']) ?>">
</div>
<div>
<label for="db_pass">Contraseña</label>
<input id="db_pass" type="password" name="db_pass" value="<?= htmlspecialchars($cfg['db_pass']) ?>">
</div>
</div>
<label for="db_name">Nombre de la base de datos</label>
<input id="db_name" type="text" name="db_name" value="<?= htmlspecialchars($cfg['db_name']) ?>">
<h3>Aplicación</h3>
<label class="chk">
<input type="checkbox" name="allow_registration" <?= $cfg['allow_registration'] === 'true' ? 'checked' : '' ?>>
Permitir registro de nuevos usuarios
</label>
<label for="base_url">URL base (opcional, para producción)</label>
<input id="base_url" type="text" name="base_url" placeholder="https://tu-servidor.com/mycloud"
value="<?= htmlspecialchars($cfg['base_url']) ?>">
<label for="timezone">Zona horaria</label>
<input id="timezone" type="text" name="timezone" value="<?= htmlspecialchars($cfg['timezone']) ?>">
<button type="submit">💾 Guardar configuración</button>
</form>
<p class="muted" style="margin-top:16px">
✓ Tras guardar, visita <a href="index.php">index.php</a> para acceder.<br>
✓ Añade <code>.env</code> a tu <code>.gitignore</code> para no publicar credenciales.
</p>
</div>
</body>
</html>