blob: 30dfeff031fb2f882c3c0c134d141d77da54a547 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2if (php_sapi_name() != "cli") {
3 exit();
4}
5
6// Classes autoload
7spl_autoload_register(function($className) {
8 if ($className == "lbuchs\WebAuthn\Binary\ByteBuffer") include_once(__DIR__."/lib/WebAuthn/Binary/ByteBuffer.php");
9 else include_once(__DIR__."/inc/".$className.".php");
10});
11
12require_once("config.php");
13$con = @mysqli_connect($conf["db"]["server"], $conf["db"]["user"], $conf["db"]["password"], $conf["db"]["database"]) or die("There was an error connecting to the database.");
14mysqli_set_charset($con, "utf8mb4");
15
16echo "Benvingut a l'instal·lador de l'aplicació 'Hores'.\n\n";
17echo "Entra els detalls del primer usuari administrador de l'aplicació:\n";
18echo "Nom d'usuari: ";
19$username = mysqli_real_escape_string($con, trim(fgets(STDIN)));
20
21echo "Contrasenya: ";
22system('stty -echo');
23$pw = mysqli_real_escape_string($con, password_hash(trim(fgets(STDIN)), PASSWORD_DEFAULT));
24system('stty echo');
25echo "\n";
26
27echo "Nom complet: ";
28$name = mysqli_real_escape_string($con, trim(fgets(STDIN)));
29
30echo "DNI: ";
31$dni = mysqli_real_escape_string($con, trim(fgets(STDIN)));
32
33echo "Correu electrònic: ";
34$email = mysqli_real_escape_string($con, trim(fgets(STDIN)));
35
36echo "\nGenerant taules de la base de dades:\n";
37
38$sql = [];
39
40$sql["people"] = "CREATE TABLE people (
41 id INT NOT NULL AUTO_INCREMENT,
42 PRIMARY KEY(id),
43 username VARCHAR(50) NOT NULL UNIQUE,
44 password VARCHAR(255) NOT NULL UNIQUE,
45 type INT,
46 name VARCHAR(255) NOT NULL,
47 dni VARCHAR(10) NOT NULL,
48 email VARCHAR(255) NOT NULL,
49 category INT DEFAULT -1,
50 INDEX(category),
51 secondfactor INT DEFAULT 0
52)";
53
54$sql["companies"] = "CREATE TABLE companies (
55 id INT NOT NULL AUTO_INCREMENT,
56 PRIMARY KEY(id),
57 name VARCHAR(100) NOT NULL UNIQUE,
58 cif VARCHAR(100) NOT NULL
59)";
60
61$sql["workers"] = "CREATE TABLE workers (
62 id INT NOT NULL AUTO_INCREMENT,
63 PRIMARY KEY(id),
64 person INT NOT NULL,
65 INDEX(person),
66 company INT NOT NULL,
67 INDEX(company)
68)";
69
70$sql["workhistory"] = "CREATE TABLE workhistory (
71 id INT NOT NULL AUTO_INCREMENT,
72 PRIMARY KEY(id),
73 worker INT NOT NULL,
74 INDEX(worker),
75 day INT NOT NULL,
76 status INT NOT NULL
77)";
78
79$sql["categories"] = "CREATE TABLE categories (
80 id INT NOT NULL AUTO_INCREMENT,
81 PRIMARY KEY(id),
82 name VARCHAR(100) NOT NULL UNIQUE,
83 parent INT NOT NULL,
84 emails TEXT
85)";
86
87$sql["calendars"] = "CREATE TABLE calendars (
88 id INT NOT NULL AUTO_INCREMENT,
89 PRIMARY KEY(id),
90 category INT NOT NULL,
91 begins INT NOT NULL,
92 ends INT NOT NULL,
93 details TEXT NOT NULL
94)";
95
96$sql["scheduletemplates"] = "CREATE TABLE scheduletemplates (
97 id INT NOT NULL AUTO_INCREMENT,
98 PRIMARY KEY(id),
99 name VARCHAR(50) NOT NULL UNIQUE,
100 begins INT NOT NULL,
101 ends INT NOT NULL
102)";
103
104$sql["scheduletemplatesdays"] = "CREATE TABLE scheduletemplatesdays (
105 id INT NOT NULL AUTO_INCREMENT,
106 PRIMARY KEY(id),
107 template INT NOT NULL,
108 INDEX(template),
109 day INT NOT NULL,
110 typeday INT NOT NULL,
111 beginswork INT NOT NULL,
112 endswork INT NOT NULL,
113 beginsbreakfast INT,
114 endsbreakfast INT,
115 beginslunch INT,
116 endslunch INT
117)";
118
119$sql["schedules"] = "CREATE TABLE schedules (
120 id INT NOT NULL AUTO_INCREMENT,
121 PRIMARY KEY(id),
122 worker INT NOT NULL,
123 INDEX(worker),
124 begins INT NOT NULL,
125 ends INT NOT NULL,
126 active INT NOT NULL
127)";
128
129$sql["schedulesdays"] = "CREATE TABLE schedulesdays (
130 id INT NOT NULL AUTO_INCREMENT,
131 PRIMARY KEY(id),
132 schedule INT NOT NULL,
133 INDEX(schedule),
134 day INT NOT NULL,
135 typeday INT NOT NULL,
136 beginswork INT NOT NULL,
137 endswork INT NOT NULL,
138 beginsbreakfast INT,
139 endsbreakfast INT,
140 beginslunch INT,
141 endslunch INT
142)";
143
144$sql["typesincidents"] = "CREATE TABLE typesincidents (
145 id INT NOT NULL AUTO_INCREMENT,
146 PRIMARY KEY(id),
147 name VARCHAR(100) NOT NULL UNIQUE,
148 present INT NOT NULL,
149 paid INT NOT NULL,
150 workerfill INT NOT NULL DEFAULT 0,
151 notifies INT NOT NULL DEFAULT 0,
152 autovalidates INT NOT NULL DEFAULT 0,
153 hidden INT NOT NULL DEFAULT 0
154)";
155
156$sql["incidents"] = "CREATE TABLE incidents (
157 id INT NOT NULL AUTO_INCREMENT,
158 PRIMARY KEY(id),
159 worker INT,
160 INDEX(worker),
161 creator INT,
162 updatedby INT DEFAULT -1,
163 confirmedby INT DEFAULT -1,
164 type INT NOT NULL,
165 day INT NOT NULL,
166 INDEX(day),
167 begins INT NOT NULL,
168 ends INT NOT NULL,
169 details TEXT,
170 workerdetails TEXT DEFAULT NULL,
171 attachments TEXT,
172 verified INT DEFAULT 0,
173 invalidated INT DEFAULT 0,
174 INDEX(invalidated),
175 workervalidated INT DEFAULT 0,
176 INDEX(workervalidated),
177 workervalidation TEXT
178)";
179
180/*$sql["recurringincidents"] = "CREATE TABLE recurringincidents (
181 id INT NOT NULL AUTO_INCREMENT,
182 PRIMARY KEY(id),
183 worker INT,
184 creator INT,
185 type INT NOT NULL,
186 firstday INT NOT NULL,
187 lastday INT NOT NULL,
188 typedays TEXT,
189 begins INT NOT NULL,
190 ends INT NOT NULL,
191 details TEXT
192)";*/ // NOTE: No longer needed
193
194$sql["records"] = "CREATE TABLE records (
195 id INT NOT NULL AUTO_INCREMENT,
196 PRIMARY KEY(id),
197 worker INT NOT NULL,
198 INDEX(worker),
199 day INT NOT NULL,
200 INDEX(day),
201 created INT NOT NULL,
202 creator INT NOT NULL,
203 beginswork INT NOT NULL,
204 endswork INT NOT NULL,
205 beginsbreakfast INT NOT NULL,
206 endsbreakfast INT NOT NULL,
207 beginslunch INT NOT NULL,
208 endslunch INT NOT NULL,
209 invalidated INT DEFAULT 0,
210 INDEX(invalidated),
211 invalidatedby INT DEFAULT -1,
212 workervalidated INT DEFAULT 0,
213 INDEX(workervalidated),
214 workervalidation TEXT
215)";
216
217$sql["logs"] = "CREATE TABLE logs (
218 id INT NOT NULL AUTO_INCREMENT,
219 PRIMARY KEY(id),
220 realtime INT NOT NULL,
221 day INT NOT NULL,
222 logdetails TEXT,
223 executedby INT DEFAULT -1
224)";
225
226$sql["recovery"] = "CREATE TABLE recovery (
227 id INT NOT NULL AUTO_INCREMENT,
228 PRIMARY KEY(id),
229 user INT NOT NULL,
230 token VARCHAR(64) NOT NULL UNIQUE,
231 timecreated INT NOT NULL,
232 expires INT NOT NULL,
233 used INT DEFAULT 0
234)";
235
236$sql["help"] = "CREATE TABLE help (
237 id INT NOT NULL AUTO_INCREMENT,
238 PRIMARY KEY(id),
239 place INT NOT NULL UNIQUE,
240 url VARCHAR(256) NOT NULL
241)";
242
243$sql["totp"] = "CREATE TABLE totp (
244 id INT NOT NULL AUTO_INCREMENT,
245 PRIMARY KEY(id),
246 person INT NOT NULL UNIQUE,
247 secret VARCHAR(256) NOT NULL
248)";
249
250$sql["securitykeys"] = "CREATE TABLE securitykeys (
251 id INT NOT NULL AUTO_INCREMENT,
252 PRIMARY KEY(id),
253 person INT NOT NULL,
254 name VARCHAR(100),
255 credentialid VARBINARY(500),
256 credentialpublickey TEXT,
257 added INT NOT NULL,
258 lastused INT
259)";
260
261$sql["signinattempts"] = "CREATE TABLE signinattempts (
262 username VARCHAR(100) NOT NULL,
263 KEY username (username),
264 remoteip VARBINARY(16) NOT NULL,
265 KEY remoteip (remoteip),
266 remoteipblock VARBINARY(16) NOT NULL,
267 KEY remoteipblock (remoteipblock),
268 signinattempttime DATETIME NOT NULL,
269 KEY signinattempttime (signinattempttime)
270)";
271
272foreach ($sql as $table => $sentence) {
273 if (mysqli_query($con, $sentence)) {
274 echo "Taula ".$table." creada satisfactòriament.\n";
275 } else {
276 die("Hi ha hagut un error creant la taula ".$table.": ".mysqli_error($con)."\n");
277 }
278}
279
280echo "\n";
281
282if (mysqli_query($con, "INSERT INTO people (username, password, type, name, dni, email) VALUES ('".$username."', '".$pw."', 0, '".$name."', '".$dni."', '".$email."')")) {
283 echo "Afegit el primer usuari satisfactòriament.\n\n";
284 echo "Instal·lació completada correctament.\n";
285} else {
286 echo "Hi ha hagut un error afegint el primer usuari.\n";
287}