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