Primer commit del sistema separado falta mejorar mucho
This commit is contained in:
125
Sistema_discord/config.php
Executable file
125
Sistema_discord/config.php
Executable file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
// config/config.php
|
||||
|
||||
// Cargar variables de entorno
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Determinar el nombre del archivo .env basado en la variable de entorno
|
||||
// Primero revisa getenv() para la línea de comandos, luego $_SERVER para el entorno web.
|
||||
$env = getenv('APP_ENVIRONMENT') ?: ($_SERVER['APP_ENVIRONMENT'] ?? null);
|
||||
|
||||
// Por defecto, si no hay entorno definido, no cargará ningún archivo específico.
|
||||
$envFile = '.env';
|
||||
if ($env) {
|
||||
$envFile = '.env.' . $env;
|
||||
}
|
||||
|
||||
// Cargar el archivo .env correspondiente
|
||||
$dotenv = null;
|
||||
if (file_exists(dirname(__DIR__) . '/' . $envFile)) {
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__), $envFile);
|
||||
} elseif (file_exists(dirname(__DIR__) . '/.env')) {
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
|
||||
} else {
|
||||
die('Error: No se pudo encontrar un archivo de configuración de entorno (.env) válido. Se buscó ' . htmlspecialchars($envFile) . ' y .env');
|
||||
}
|
||||
|
||||
try {
|
||||
$dotenv->load();
|
||||
} catch (Exception $e) {
|
||||
die('Error al cargar el archivo de entorno: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
// Validar variables requeridas
|
||||
$dotenv->required([
|
||||
'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS',
|
||||
'JWT_SECRET', 'APP_URL'
|
||||
]);
|
||||
|
||||
// Environment Configuration
|
||||
define('ENVIRONMENT', $_ENV['APP_ENV'] ?? 'production'); // 'development' or 'production'
|
||||
|
||||
|
||||
|
||||
// Detectar si se ejecuta desde la línea de comandos
|
||||
$is_cli = (php_sapi_name() === 'cli' || defined('STDIN'));
|
||||
|
||||
// Configurar la URL base y el protocolo
|
||||
if ($is_cli) {
|
||||
// En CLI, usar siempre la APP_URL del .env y no necesitamos protocolo
|
||||
define('BOT_BASE_URL', $_ENV['APP_URL']);
|
||||
$protocol = 'http'; // Valor por defecto, no se usa realmente
|
||||
} else {
|
||||
// En entorno web, detectar protocolo dinámicamente
|
||||
$protocol = 'http';
|
||||
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
|
||||
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') ||
|
||||
(!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] === 'on') ||
|
||||
(!empty($_SERVER['HTTP_CF_VISITOR']) && strpos($_SERVER['HTTP_CF_VISITOR'], 'https' ) !== false)) {
|
||||
$protocol = 'https';
|
||||
$_SERVER['HTTPS'] = 'on';
|
||||
$_SERVER['SERVER_PORT'] = 443;
|
||||
}
|
||||
define('BOT_BASE_URL', $_ENV['APP_URL'] ?? ($protocol . '://' . $_SERVER['HTTP_HOST']));
|
||||
$_SERVER['REQUEST_SCHEME'] = $protocol;
|
||||
}
|
||||
define('BASE_PATH', dirname(__DIR__));
|
||||
|
||||
// Database Configuration
|
||||
define('DB_HOST', $_ENV['DB_HOST']);
|
||||
define('DB_USER', $_ENV['DB_USER']);
|
||||
define('DB_PASS', $_ENV['DB_PASS']);
|
||||
define('DB_NAME', $_ENV['DB_NAME']);
|
||||
define('DB_DIALECT', $_ENV['DB_DIALECT']);
|
||||
define('DB_PORT', $_ENV['DB_PORT']);
|
||||
|
||||
// Session Configuration
|
||||
define('SESSION_SECRET', $_ENV['JWT_SECRET']);
|
||||
|
||||
// Discord API Configuration
|
||||
define('DISCORD_GUILD_ID', $_ENV['DISCORD_GUILD_ID']);
|
||||
define('DISCORD_CLIENT_ID', $_ENV['DISCORD_CLIENT_ID']);
|
||||
define('DISCORD_CLIENT_SECRET', $_ENV['DISCORD_CLIENT_SECRET']);
|
||||
define('DISCORD_BOT_TOKEN', $_ENV['DISCORD_BOT_TOKEN']);
|
||||
|
||||
// Telegram API Configuration
|
||||
define('TELEGRAM_BOT_TOKEN', $_ENV['TELEGRAM_BOT_TOKEN']);
|
||||
define('TELEGRAM_WEBHOOK_TOKEN', $_ENV['TELEGRAM_WEBHOOK_TOKEN']);
|
||||
|
||||
// Error Reporting
|
||||
if (defined('ENVIRONMENT')) {
|
||||
switch (ENVIRONMENT) {
|
||||
case 'development':
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('log_errors', '1');
|
||||
ini_set('error_log', dirname(__DIR__) . '/logs/php_errors.log');
|
||||
break;
|
||||
case 'production':
|
||||
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
|
||||
ini_set('display_errors', '0');
|
||||
ini_set('log_errors', '1');
|
||||
ini_set('error_log', dirname(__DIR__) . '/logs/php_errors.log');
|
||||
break;
|
||||
default:
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('log_errors', '1');
|
||||
ini_set('error_log', dirname(__DIR__) . '/logs/php_errors.log');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Helper function to get full URL
|
||||
function url($path = '') {
|
||||
$path = ltrim($path, '/');
|
||||
return BOT_BASE_URL . '/' . $path;
|
||||
}
|
||||
|
||||
// Helper function to get full asset URL
|
||||
function asset_url($path = '') {
|
||||
$path = ltrim($path, '/');
|
||||
return BOT_BASE_URL . '/assets/' . $path;
|
||||
}
|
||||
Reference in New Issue
Block a user