app-store.php

6.17 KB
04/08/2025 05:50
PHP
app-store.php
<?php
/**
 * Application Configuration Override for Store Installation
 */

// Auto-detect base path from current installation
$scriptPath = dirname($_SERVER['SCRIPT_NAME'] ?? '');
$basePath = '';

// Extract base path from script path (remove /api/public or similar)
if (strpos($scriptPath, '/api/public') !== false) {
    $basePath = str_replace('/api/public', '', $scriptPath);
} elseif (strpos($scriptPath, '/api') !== false) {
    $basePath = str_replace('/api', '', $scriptPath);
} else {
    $basePath = $scriptPath;
}

// Ensure clean path
$basePath = rtrim($basePath, '/');
if (empty($basePath)) {
    $basePath = '';
}

return [
    // Application Settings
    'app_name' => 'E-commerce API',
    'app_version' => '1.0.0',
    'app_url' => 'http://localhost',
    'app_env' => 'development',
    'debug' => true,
    'timezone' => 'Asia/Bangkok',

    // API Configuration - Dynamic paths
    'api_base_path' => $basePath.'/api',
    'admin_base_path' => $basePath.'/admin',
    'api_version' => 'v1',

    // Deployment Configuration
    'deployment_type' => 'subfolder',
    'base_domain' => $_SERVER['HTTP_HOST'] ?? 'localhost',
    'subdomain_prefix' => 'api',
    'subfolder_path' => $basePath,

    // Security Settings
    'jwt_secret' => 'your-secret-key-change-this-in-production',
    'jwt_algorithm' => 'HS256',
    'jwt_expiry' => 3600, // 1 hour
    'jwt_refresh_expiry' => 86400, // 24 hours

    // Database Configuration
    'database' => [
        'host' => $_ENV['DB_HOST'] ?? 'localhost',
        'port' => $_ENV['DB_PORT'] ?? '3306',
        'database' => $_ENV['DB_NAME'] ?? 'ecommerce_store',
        'username' => $_ENV['DB_USER'] ?? 'root',
        'password' => $_ENV['DB_PASS'] ?? '',
        'charset' => $_ENV['DB_CHARSET'] ?? 'utf8mb4',
        'collation' => $_ENV['DB_COLLATION'] ?? 'utf8mb4_unicode_ci',
        'prefix' => $_ENV['DB_PREFIX'] ?? ''
    ],

    // Email Configuration
    'mail' => [
        'driver' => $_ENV['MAIL_DRIVER'] ?? 'smtp',
        'host' => $_ENV['MAIL_HOST'] ?? 'localhost',
        'port' => $_ENV['MAIL_PORT'] ?? 587,
        'username' => $_ENV['MAIL_USERNAME'] ?? '',
        'password' => $_ENV['MAIL_PASSWORD'] ?? '',
        'encryption' => $_ENV['MAIL_ENCRYPTION'] ?? 'tls',
        'from_address' => $_ENV['MAIL_FROM_ADDRESS'] ?? 'noreply@localhost',
        'from_name' => $_ENV['MAIL_FROM_NAME'] ?? 'E-commerce Store'
    ],

    // File Storage Configuration
    'storage' => [
        'uploads_path' => __DIR__.'/../storage/uploads',
        'cache_path' => __DIR__.'/../storage/cache',
        'logs_path' => __DIR__.'/../storage/logs',
        'temp_path' => __DIR__.'/../storage/temp',
        'max_file_size' => 10 * 1024 * 1024, // 10MB
        'allowed_image_types' => ['jpg', 'jpeg', 'png', 'gif', 'webp'],
        'allowed_file_types' => ['pdf', 'doc', 'docx', 'txt']
    ],

    // Cache Configuration
    'cache' => [
        'default' => $_ENV['CACHE_DRIVER'] ?? 'file',
        'ttl' => (int) ($_ENV['CACHE_TTL'] ?? 3600),
        'prefix' => $_ENV['CACHE_PREFIX'] ?? 'ecommerce_'
    ],

    // Session Configuration
    'session' => [
        'lifetime' => (int) ($_ENV['SESSION_LIFETIME'] ?? 120),
        'expire_on_close' => filter_var($_ENV['SESSION_EXPIRE_ON_CLOSE'] ?? false, FILTER_VALIDATE_BOOLEAN),
        'cookie_name' => $_ENV['SESSION_COOKIE'] ?? 'ecommerce_session',
        'cookie_path' => $_ENV['SESSION_PATH'] ?? $basePath ?: '/',
        'cookie_domain' => $_ENV['SESSION_DOMAIN'] ?? null,
        'cookie_secure' => filter_var($_ENV['SESSION_SECURE'] ?? false, FILTER_VALIDATE_BOOLEAN),
        'cookie_http_only' => filter_var($_ENV['SESSION_HTTP_ONLY'] ?? true, FILTER_VALIDATE_BOOLEAN)
    ],

    // Rate Limiting
    'rate_limit' => [
        'enabled' => filter_var($_ENV['RATE_LIMIT_ENABLED'] ?? true, FILTER_VALIDATE_BOOLEAN),
        'requests_per_minute' => (int) ($_ENV['RATE_LIMIT_RPM'] ?? 60),
        'burst_limit' => (int) ($_ENV['RATE_LIMIT_BURST'] ?? 10)
    ],

    // CORS Configuration
    'cors' => [
        'allowed_origins' => explode(',', $_ENV['CORS_ALLOWED_ORIGINS'] ?? '*'),
        'allowed_methods' => explode(',', $_ENV['CORS_ALLOWED_METHODS'] ?? 'GET,POST,PUT,DELETE,OPTIONS'),
        'allowed_headers' => explode(',', $_ENV['CORS_ALLOWED_HEADERS'] ?? 'Content-Type,Authorization,X-Requested-With'),
        'exposed_headers' => explode(',', $_ENV['CORS_EXPOSED_HEADERS'] ?? ''),
        'max_age' => (int) ($_ENV['CORS_MAX_AGE'] ?? 86400),
        'allow_credentials' => filter_var($_ENV['CORS_ALLOW_CREDENTIALS'] ?? true, FILTER_VALIDATE_BOOLEAN)
    ],

    // Logging Configuration
    'logging' => [
        'level' => $_ENV['LOG_LEVEL'] ?? 'info',
        'path' => __DIR__.'/../storage/logs',
        'daily' => filter_var($_ENV['LOG_DAILY'] ?? true, FILTER_VALIDATE_BOOLEAN),
        'max_files' => (int) ($_ENV['LOG_MAX_FILES'] ?? 14)
    ],

    // Backward compatibility - flat log config
    'log_level' => $_ENV['LOG_LEVEL'] ?? 'info',
    'log_path' => __DIR__.'/../storage/logs',

    // Payment Configuration
    'payment' => [
        'default_gateway' => $_ENV['PAYMENT_GATEWAY'] ?? 'stripe',
        'stripe' => [
            'public_key' => $_ENV['STRIPE_PUBLIC_KEY'] ?? '',
            'secret_key' => $_ENV['STRIPE_SECRET_KEY'] ?? '',
            'webhook_secret' => $_ENV['STRIPE_WEBHOOK_SECRET'] ?? ''
        ],
        'paypal' => [
            'client_id' => $_ENV['PAYPAL_CLIENT_ID'] ?? '',
            'client_secret' => $_ENV['PAYPAL_CLIENT_SECRET'] ?? '',
            'mode' => $_ENV['PAYPAL_MODE'] ?? 'sandbox' // sandbox or live
        ]
    ],

    // Inventory Configuration
    'inventory' => [
        'track_quantity' => filter_var($_ENV['TRACK_INVENTORY'] ?? true, FILTER_VALIDATE_BOOLEAN),
        'allow_backorders' => filter_var($_ENV['ALLOW_BACKORDERS'] ?? false, FILTER_VALIDATE_BOOLEAN),
        'low_stock_threshold' => (int) ($_ENV['LOW_STOCK_THRESHOLD'] ?? 10)
    ],

    // Order Configuration
    'orders' => [
        'auto_complete_after_days' => (int) ($_ENV['AUTO_COMPLETE_ORDERS'] ?? 30),
        'allow_guest_checkout' => filter_var($_ENV['ALLOW_GUEST_CHECKOUT'] ?? true, FILTER_VALIDATE_BOOLEAN),
        'require_phone' => filter_var($_ENV['REQUIRE_PHONE'] ?? false, FILTER_VALIDATE_BOOLEAN)
    ]
];