32 lines
960 B
PHP
32 lines
960 B
PHP
<?php
|
|
// pagos/api/db.php
|
|
|
|
$db_file = __DIR__ . '/../db/finanzas.sqlite';
|
|
$schema_file = __DIR__ . '/../db/schema.sql';
|
|
|
|
// Función para obtener conexión PDO a SQLite
|
|
function getDB() {
|
|
global $db_file, $schema_file;
|
|
$is_new = !file_exists($db_file);
|
|
|
|
try {
|
|
$pdo = new PDO("sqlite:" . $db_file);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Si no existía, crear la BD desde schema y establecer permisos
|
|
if ($is_new && file_exists($schema_file)) {
|
|
$schema = file_get_contents($schema_file);
|
|
$pdo->exec($schema);
|
|
// Intentar dar permisos para www-data en el servidor
|
|
@chmod($db_file, 0666);
|
|
@chmod(dirname($db_file), 0777);
|
|
}
|
|
|
|
return $pdo;
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
die(json_encode(['error' => 'Connection failed: ' . $e->getMessage()]));
|
|
}
|
|
}
|
|
?>
|