-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_db.php
164 lines (135 loc) · 5.88 KB
/
setup_db.php
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
<link rel="icon" type="image/png" href="/img/logo.png">
<?php
require('environment_loader.php');
if ($db_type == "mysql") {
try {
$conn = new PDO("mysql:host=$servername;port=$db_port;", $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO';
START TRANSACTION;
SET time_zone = '+00:00';
CREATE DATABASE IF NOT EXISTS `$database` DEFAULT CHARACTER SET $db_charset COLLATE $db_collation;
USE `$database`;
DROP TABLE IF EXISTS `identificadores`;
CREATE TABLE IF NOT EXISTS `identificadores` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identificador` varchar(255) NOT NULL,
`texto` text NOT NULL,
`timestamp` timestamp NULL DEFAULT NULL,
`id_usuario` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_usuario` (`id_usuario`)
) ENGINE=$db_engine AUTO_INCREMENT=0 DEFAULT CHARSET=$db_charset COLLATE=$db_collation;
DROP TABLE IF EXISTS `ubicaciones`;
CREATE TABLE IF NOT EXISTS `ubicaciones` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`latitud` double DEFAULT NULL,
`longitud` double DEFAULT NULL,
`ip` varchar(255) DEFAULT NULL,
`hora` time DEFAULT NULL,
`timestamp` int(11) DEFAULT NULL,
`identificator` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=$db_engine AUTO_INCREMENT=0 DEFAULT CHARSET=$db_charset COLLATE=$db_collation;
DROP TABLE IF EXISTS `usuarios`;
CREATE TABLE IF NOT EXISTS `usuarios` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`timestamp_last_login` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=$db_engine AUTO_INCREMENT=100 DEFAULT CHARSET=$db_charset COLLATE=$db_collation;
INSERT INTO `usuarios` (`id`, `username`, `password`, `timestamp_last_login`) VALUES
(1, 'ROOT', 'TOOR', NULL);
ALTER TABLE `identificadores`
ADD CONSTRAINT `identificadores_ibfk_1` FOREIGN KEY (`id_usuario`) REFERENCES `usuarios` (`id`);
COMMIT;
";
$conn->exec($sql);
echo "Base de datos y tablas creadas correctamente";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
}elseif($db_type=="sqlite"){
try {
$conn = new PDO("sqlite:geolocate.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "
CREATE TABLE IF NOT EXISTS identificadores (
id INTEGER PRIMARY KEY,
identificador TEXT NOT NULL,
texto TEXT NOT NULL,
timestamp TEXT NULL,
id_usuario INTEGER DEFAULT NULL,
FOREIGN KEY (id_usuario) REFERENCES usuarios(id)
);
CREATE TABLE IF NOT EXISTS ubicaciones (
id INTEGER PRIMARY KEY,
latitud REAL DEFAULT NULL,
longitud REAL DEFAULT NULL,
ip TEXT DEFAULT NULL,
hora TEXT DEFAULT NULL,
timestamp INTEGER DEFAULT NULL,
identificator INTEGER DEFAULT NULL,
FOREIGN KEY (identificator) REFERENCES identificadores(id)
);
CREATE TABLE IF NOT EXISTS usuarios (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL,
timestamp_last_login TEXT NULL
);
INSERT INTO usuarios (id, username, password, timestamp_last_login) VALUES
(1, 'ROOT', 'TOOR', NULL);
";
$conn->exec($sql);
echo "Base de datos y tablas creadas correctamente";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
}elseif($db_type=="pgsql"){
try {
$conn = new PDO("pgsql:host=$servername;port=$db_port;", $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Crear la base de datos si no existe
$conn->exec("CREATE DATABASE $database");
// Conectarse a la base de datos recién creada
$conn->exec("USE $database");
$sql = "
CREATE TABLE IF NOT EXISTS identificadores (
id SERIAL PRIMARY KEY,
identificador VARCHAR(255) NOT NULL,
texto TEXT NOT NULL,
timestamp TIMESTAMP NULL,
id_usuario INT DEFAULT NULL,
CONSTRAINT fk_id_usuario FOREIGN KEY (id_usuario) REFERENCES usuarios(id)
);
CREATE TABLE IF NOT EXISTS ubicaciones (
id SERIAL PRIMARY KEY,
latitud DOUBLE PRECISION DEFAULT NULL,
longitud DOUBLE PRECISION DEFAULT NULL,
ip VARCHAR(255) DEFAULT NULL,
hora TIME DEFAULT NULL,
timestamp INT DEFAULT NULL,
identificator INT DEFAULT NULL,
CONSTRAINT fk_identificator FOREIGN KEY (identificator) REFERENCES identificadores(id)
);
CREATE TABLE IF NOT EXISTS usuarios (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
timestamp_last_login TIMESTAMP NULL
);
INSERT INTO usuarios (id, username, password, timestamp_last_login) VALUES
(1, 'ROOT', 'TOOR', NULL);
";
$conn->exec($sql);
echo "Base de datos y tablas creadas correctamente";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
}