]> jfr.im git - uguu.git/blame - src/Classes/Connector.php
replace name generator method
[uguu.git] / src / Classes / Connector.php
CommitLineData
e480c0e5 1<?php
9184e8d4
GJ
2/*
3 * Uguu
4 *
5 * @copyright Copyright (c) 2022-2024 Go Johansson (nokonoko) <neku@pomf.se>
6 *
7 * Note that this was previously distributed under the MIT license 2015-2022.
8 *
9 * If you are a company that wants to use Uguu I urge you to contact me to
10 * solve any potential license issues rather then using pre-2022 code.
11 *
12 * A special thanks goes out to the open source community around the world
13 * for supporting and being the backbone of projects like Uguu.
14 *
15 * This project can be found at <https://github.com/nokonoko/Uguu>.
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 */
30
31namespace Pomf\Uguu\Classes;
32
33use PDO;
34use Random\Randomizer;
35
36class Connector extends Database
37{
38 public PDO $DB;
39 public string $dbType;
40 public array $CONFIG;
41 public Response $response;
42 public Randomizer $randomizer;
43
44 public function errorHandler(int $errno, string $errstr):void
e480c0e5 45 {
9184e8d4
GJ
46 if ($this->CONFIG['DEBUG']) {
47 $this->response->error(500, 'Server error: ' . $errstr);
48 } else {
49 $this->response->error(500, 'Server error.');
50 }
51 }
96bb2d33 52
9184e8d4
GJ
53 public function fatalErrorHandler():void
54 {
55 if (!is_null($e = error_get_last())) {
52053519 56 if ($this->CONFIG['DEBUG']) {
9184e8d4 57 $this->response->error(500, 'FATAL Server error: ' . print_r($e, true));
52053519
GJ
58 } else {
59 $this->response->error(500, 'Server error.');
96bb2d33 60 }
52053519 61 }
9184e8d4 62 }
52053519 63
9184e8d4
GJ
64 /**
65 * Reads the config.json file and populates the CONFIG property with the settings
66 * Also assembles the PDO DB connection and registers error handlers.
67 *
68 */
69 public function __construct()
70 {
71 $this->response = new Response('json');
72 if (!file_exists(__DIR__ . '/../config.json')) {
73 $this->response->error(500, 'Cant read settings file.');
e480c0e5 74 }
9184e8d4
GJ
75 $this->CONFIG = json_decode(
76 file_get_contents(__DIR__ . '/../config.json'),
77 true,
78 );
79 ini_set('display_errors', 0);
80 set_error_handler([$this, "errorHandler"]);
81 register_shutdown_function([$this, "fatalErrorHandler"]);
82 $this->dbType = $this->CONFIG['DB_MODE'];
83 $this->DB = new PDO(
84 $this->CONFIG['DB_MODE'] . ':' . $this->CONFIG['DB_PATH'],
85 $this->CONFIG['DB_USER'],
86 $this->CONFIG['DB_PASS'],
87 );
88 $this->randomizer = new Randomizer();
89 }
90}