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.
@@ -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
- http_response_code(403);
7
- die('Accesso diretto non consentito.');
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 DOTENV ---
29
- // dirname(__DIR__, 2) sale di un livello (da api/ a Berna-Stencil-out/)
30
- try {
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
- if (($_ENV['APP_ENV'] ?? 'production') === 'production') {
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' => 'e4d3643e35a44fdeefbcfd5a5ca35bf44191fd6d',
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' => 'e4d3643e35a44fdeefbcfd5a5ca35bf44191fd6d',
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(',', $_ENV['CORS_ALLOWED_ORIGINS'] ?? '')));
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 = $_ENV['API_KEY'] ?? '';
83
+ $validKey = $config['API_KEY'] ?? '';
84
84
 
85
85
  if ($validKey === '' || $apiKey !== $validKey) {
86
86
  Response::error('Unauthorized', 401);
package/.env.example DELETED
@@ -1,8 +0,0 @@
1
- API_KEY=TOKEN
2
- CORS_ALLOWED_ORIGINS=*
3
-
4
- MAIL_HOST=smtp.gmail.com
5
- MAIL_PORT=587
6
- MAIL_USERNAME=YOUR_EMAIL
7
- MAIL_PASSWORD=APP_PASSWORD
8
- MAIL_TO_ADDRESS=EMAIL_TO_ADDRESS
@@ -1,2 +0,0 @@
1
- # Impedisce a chiunque di accedere a questa cartella tramite URL
2
- Require all denied
@@ -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
- ]);