create-berna-stencil 1.0.19 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eleventy.js +1 -2
- package/.gitignore +1 -1
- package/bin/create.js +12 -12
- package/package.json +5 -6
- package/src/api/.htaccess +7 -8
- package/src/api/config.example.php +27 -0
- package/src/api/config.php +27 -0
- package/src/api/core/composer.lock +492 -492
- package/src/api/core/init.php +15 -17
- package/src/api/core/modules/Response.php +12 -0
- package/src/api/core/vendor/composer/installed.php +2 -2
- package/src/api/endpoints/protected/example-protected-endpoint.php +28 -0
- package/src/api/endpoints/public/example-public-endpoint.php +28 -0
- package/src/api/index.php +2 -2
- package/.env.example +0 -8
- package/src/api/core/.htaccess +0 -2
- package/src/api/endpoints/protected/prova-chiusa.php +0 -16
- package/src/api/endpoints/public/prova-aperta.php +0 -16
package/src/api/core/init.php
CHANGED
|
@@ -2,16 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
declare(strict_types=1);
|
|
4
4
|
|
|
5
|
+
// Impedisce l'accesso diretto a questo file
|
|
5
6
|
if (!defined('CORE_ACCESS')) {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
$errorPage = $_SERVER['DOCUMENT_ROOT'] . '/404.html';
|
|
8
|
+
http_response_code(404);
|
|
9
|
+
if (file_exists($errorPage)) {
|
|
10
|
+
header('Content-Type: text/html; charset=UTF-8');
|
|
11
|
+
echo file_get_contents($errorPage);
|
|
12
|
+
} else {
|
|
13
|
+
echo "404 Not Found";
|
|
14
|
+
}
|
|
15
|
+
exit;
|
|
8
16
|
}
|
|
9
17
|
|
|
10
18
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
11
19
|
require_once __DIR__ . '/modules/Response.php';
|
|
12
20
|
|
|
13
21
|
// --- GESTORE GLOBALE ERRORI E ECCEZIONI ---
|
|
14
|
-
// Trasforma ogni errore PHP in una risposta JSON pulita
|
|
15
22
|
set_exception_handler(function ($exception) {
|
|
16
23
|
Response::error(
|
|
17
24
|
$exception->getMessage(),
|
|
@@ -25,21 +32,12 @@ set_error_handler(function ($severity, $message, $file, $line) {
|
|
|
25
32
|
throw new ErrorException($message, 0, $severity, $file, $line);
|
|
26
33
|
});
|
|
27
34
|
|
|
28
|
-
// --- CARICAMENTO
|
|
29
|
-
// dirname(__DIR__
|
|
30
|
-
|
|
31
|
-
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__, 2));
|
|
32
|
-
$dotenv->load();
|
|
33
|
-
} catch (Exception $e) {
|
|
34
|
-
Response::error("Impossibile caricare il file .env. Assicurati che esista nella root e si chiami esattamente .env", 500);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
$dotenv->required([
|
|
38
|
-
'API_KEY',
|
|
39
|
-
'CORS_ALLOWED_ORIGINS',
|
|
40
|
-
]);
|
|
35
|
+
// --- CARICAMENTO CONFIGURAZIONE ---
|
|
36
|
+
// dirname(__DIR__) punta alla cartella /api/ dove ora si trova config.php
|
|
37
|
+
$config = require dirname(__DIR__) . '/config.php';
|
|
41
38
|
|
|
42
|
-
|
|
39
|
+
// --- CONFIGURAZIONE AMBIENTE ---
|
|
40
|
+
if (($config['APP_ENV'] ?? 'production') === 'production') {
|
|
43
41
|
ini_set('display_errors', '0');
|
|
44
42
|
error_reporting(0);
|
|
45
43
|
} else {
|
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
declare(strict_types=1);
|
|
4
4
|
|
|
5
|
+
if (!defined('CORE_ACCESS')) {
|
|
6
|
+
$errorPage = $_SERVER['DOCUMENT_ROOT'] . '/404.html';
|
|
7
|
+
http_response_code(404);
|
|
8
|
+
if (file_exists($errorPage)) {
|
|
9
|
+
header('Content-Type: text/html; charset=UTF-8');
|
|
10
|
+
echo file_get_contents($errorPage);
|
|
11
|
+
} else {
|
|
12
|
+
echo "404 Not Found";
|
|
13
|
+
}
|
|
14
|
+
exit;
|
|
15
|
+
}
|
|
16
|
+
|
|
5
17
|
class Response
|
|
6
18
|
{
|
|
7
19
|
public static function success(mixed $data = null, int $code = 200): never
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
'name' => '__root__',
|
|
4
4
|
'pretty_version' => 'dev-main',
|
|
5
5
|
'version' => 'dev-main',
|
|
6
|
-
'reference' => '
|
|
6
|
+
'reference' => 'bb9aa8d8f7ee79e3cd88ebf5d61bf137732aa4f1',
|
|
7
7
|
'type' => 'library',
|
|
8
8
|
'install_path' => __DIR__ . '/../../',
|
|
9
9
|
'aliases' => array(),
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
'__root__' => array(
|
|
14
14
|
'pretty_version' => 'dev-main',
|
|
15
15
|
'version' => 'dev-main',
|
|
16
|
-
'reference' => '
|
|
16
|
+
'reference' => 'bb9aa8d8f7ee79e3cd88ebf5d61bf137732aa4f1',
|
|
17
17
|
'type' => 'library',
|
|
18
18
|
'install_path' => __DIR__ . '/../../',
|
|
19
19
|
'aliases' => array(),
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
if (!defined('CORE_ACCESS')) {
|
|
6
|
+
$errorPage = $_SERVER['DOCUMENT_ROOT'] . '/404.html';
|
|
7
|
+
http_response_code(404);
|
|
8
|
+
if (file_exists($errorPage)) {
|
|
9
|
+
header('Content-Type: text/html; charset=UTF-8');
|
|
10
|
+
echo file_get_contents($errorPage);
|
|
11
|
+
} else {
|
|
12
|
+
echo "404 Not Found";
|
|
13
|
+
}
|
|
14
|
+
exit;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
require_once __DIR__ . '/../../core/modules/Response.php';
|
|
18
|
+
|
|
19
|
+
if ($method !== 'GET') {
|
|
20
|
+
Response::error('Method not allowed', 405);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
Response::success([
|
|
24
|
+
'message' => 'Protected endpoint is working',
|
|
25
|
+
'endpoint' => 'example-protected-endpoint',
|
|
26
|
+
'visibility' => 'protected',
|
|
27
|
+
'params' => $requestParams,
|
|
28
|
+
]);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
declare(strict_types=1);
|
|
4
|
+
|
|
5
|
+
if (!defined('CORE_ACCESS')) {
|
|
6
|
+
$errorPage = $_SERVER['DOCUMENT_ROOT'] . '/404.html';
|
|
7
|
+
http_response_code(404);
|
|
8
|
+
if (file_exists($errorPage)) {
|
|
9
|
+
header('Content-Type: text/html; charset=UTF-8');
|
|
10
|
+
echo file_get_contents($errorPage);
|
|
11
|
+
} else {
|
|
12
|
+
echo "404 Not Found";
|
|
13
|
+
}
|
|
14
|
+
exit;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
require_once __DIR__ . '/../../core/modules/Response.php';
|
|
18
|
+
|
|
19
|
+
if ($method !== 'GET') {
|
|
20
|
+
Response::error('Method not allowed', 405);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
Response::success([
|
|
24
|
+
'message' => 'Public endpoint is working',
|
|
25
|
+
'endpoint' => 'example-public-endpoint',
|
|
26
|
+
'visibility' => 'public',
|
|
27
|
+
'params' => $requestParams,
|
|
28
|
+
]);
|
package/src/api/index.php
CHANGED
|
@@ -60,7 +60,7 @@ header('Content-Type: application/json; charset=UTF-8');
|
|
|
60
60
|
header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS');
|
|
61
61
|
header('Access-Control-Allow-Headers: Content-Type, X-Api-Key');
|
|
62
62
|
|
|
63
|
-
$allowedOrigins = array_filter(array_map('trim', explode(',', $
|
|
63
|
+
$allowedOrigins = array_filter(array_map('trim', explode(',', $config['CORS_ALLOWED_ORIGINS'] ?? '')));
|
|
64
64
|
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
|
65
65
|
|
|
66
66
|
if (in_array($origin, $allowedOrigins, true) || in_array('*', $allowedOrigins, true)) {
|
|
@@ -80,7 +80,7 @@ if ($method === 'OPTIONS') {
|
|
|
80
80
|
|
|
81
81
|
if ($isProtected) {
|
|
82
82
|
$apiKey = $_SERVER['HTTP_X_API_KEY'] ?? '';
|
|
83
|
-
$validKey = $
|
|
83
|
+
$validKey = $config['API_KEY'] ?? '';
|
|
84
84
|
|
|
85
85
|
if ($validKey === '' || $apiKey !== $validKey) {
|
|
86
86
|
Response::error('Unauthorized', 401);
|
package/.env.example
DELETED
package/src/api/core/.htaccess
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
<?php
|
|
2
|
-
|
|
3
|
-
declare(strict_types=1);
|
|
4
|
-
|
|
5
|
-
require_once __DIR__ . '/../../core/modules/Response.php';
|
|
6
|
-
|
|
7
|
-
if ($method !== 'GET') {
|
|
8
|
-
Response::error('Method not allowed', 405);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
Response::success([
|
|
12
|
-
'message' => 'Protected endpoint is working',
|
|
13
|
-
'endpoint' => 'prova-chiusa',
|
|
14
|
-
'visibility' => 'protected',
|
|
15
|
-
'params' => $requestParams,
|
|
16
|
-
]);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
<?php
|
|
2
|
-
|
|
3
|
-
declare(strict_types=1);
|
|
4
|
-
|
|
5
|
-
require_once __DIR__ . '/../../core/modules/Response.php';
|
|
6
|
-
|
|
7
|
-
if ($method !== 'GET') {
|
|
8
|
-
Response::error('Method not allowed', 405);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
Response::success([
|
|
12
|
-
'message' => 'Public endpoint is working',
|
|
13
|
-
'endpoint' => 'prova-aperta',
|
|
14
|
-
'visibility' => 'public',
|
|
15
|
-
'params' => $requestParams,
|
|
16
|
-
]);
|