|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Alxarafe\Base\Controller; |
|
20
|
|
|
|
|
21
|
|
|
use Alxarafe\Base\Config; |
|
22
|
|
|
use Alxarafe\Base\Controller\Trait\DbTrait; |
|
23
|
|
|
use Alxarafe\Base\Database; |
|
24
|
|
|
use CoreModules\Admin\Model\User; |
|
25
|
|
|
use Psr\Container\ContainerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class ApiController. The generic API controller contains what is necessary for any API controller |
|
29
|
|
|
* |
|
30
|
|
|
* @package Alxarafe\Base |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class ApiController |
|
33
|
|
|
{ |
|
34
|
|
|
use DbTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Contains the identified user |
|
38
|
|
|
* |
|
39
|
|
|
* @var User|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public static ?User $user = null; |
|
42
|
|
|
/** |
|
43
|
|
|
* Contains the JWT security key |
|
44
|
|
|
* |
|
45
|
|
|
* @var string|null |
|
46
|
|
|
*/ |
|
47
|
|
|
private static ?string $security_key = null; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct() |
|
50
|
|
|
{ |
|
51
|
|
|
$config = Config::getConfig(); |
|
52
|
|
|
if (!isset($config->db) || !static::connectDb($config->db)) { |
|
53
|
|
|
header('Location: ' . constant('BASE_URL') . '/index.php?module=Admin&controller=Config'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->db = new Database($config->db); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns a successful API response and ends the execution of the application. |
|
61
|
|
|
* |
|
62
|
|
|
* @param $response |
|
63
|
|
|
* @param $httpCode |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
final public static function jsonResponse($response, $httpCode = 200) |
|
67
|
|
|
{ |
|
68
|
|
|
http_response_code($httpCode); |
|
69
|
|
|
header('Content-Type: application/json'); |
|
70
|
|
|
die(json_encode($response)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return the JWT security Key |
|
75
|
|
|
* |
|
76
|
|
|
* @return string|null |
|
77
|
|
|
* @throws \DebugBar\DebugBarException |
|
78
|
|
|
* @throws \Random\RandomException |
|
79
|
|
|
*/ |
|
80
|
|
|
protected static function getSecurityKey() |
|
81
|
|
|
{ |
|
82
|
|
|
if (self::$security_key !== null) { |
|
83
|
|
|
return self::$security_key; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$config = Config::getConfig(); |
|
87
|
|
|
if (!isset($config->security->jwt_secret_key)) { |
|
88
|
|
|
$config->security->jwt_secret_key = bin2hex(random_bytes(20)); |
|
89
|
|
|
if (!Config::setConfig($config)) { |
|
90
|
|
|
static::badApiCall(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
self::$security_key = $config->security->jwt_secret_key; |
|
95
|
|
|
return self::$security_key; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns an erroneous API response and ends the execution of the call |
|
100
|
|
|
* |
|
101
|
|
|
* @param $response |
|
102
|
|
|
* @param $httpCode |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
final public static function badApiCall($response = 'Bad API call', $httpCode = 400) |
|
106
|
|
|
{ |
|
107
|
|
|
http_response_code($httpCode); |
|
108
|
|
|
header('Content-Type: application/json'); |
|
109
|
|
|
die(json_encode($response)); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|