<?php
// config.php - Generated by SonicPesa Installer
session_start();
date_default_timezone_set('Africa/Dar_es_Salaam');

// --- DATABASE CONFIGURATION (Update these for your shared hosting) ---
define('DB_HOST', 'localhost'); // Often 'localhost' on shared hosting, or your DB server IP
define('DB_USER', 'ndakkpdg_nyangabopay');      // Your cPanel/Hosting database username
define('DB_PASS', 'Embassykings_01');          // Your database password
define('DB_NAME', 'ndakkpdg_nyangabopay'); // Your database name

// Dynamic SITE_URL Detection - works on any hosting
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';

// Normalize path separators to forward slashes
$doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT'] ?? '');
$dir = str_replace('\\', '/', __DIR__);

// Calculate the relative web path securely
$base_dir = '';
if ($doc_root && stripos($dir, $doc_root) === 0) {
    $base_dir = substr($dir, strlen($doc_root));
} else {
    // Fallback if __DIR__ isn't inside DOCUMENT_ROOT (rare aliases or CLI)
    $script_name = $_SERVER['SCRIPT_NAME'] ?? '';
    $dirname = str_replace('\\', '/', dirname($script_name));
    
    // If Apache Windows mod_rewrite leaks the physical path into SCRIPT_NAME (e.g. C:/ or /C:/)
    if (preg_match('~^/?[a-zA-Z]:~', $dirname) || $dirname === '.') {
        $folderName = basename(__DIR__);
        $reqUri = $_SERVER['REQUEST_URI'] ?? '';
        // See if the folder name is actually part of the requested URL (meaning it's in a subfolder)
        if (preg_match('~^/(' . preg_quote($folderName, '~') . ')(/|$)~i', $reqUri)) {
            $base_dir = '/' . $folderName;
        } else {
            $base_dir = ''; // Assume it's hosted at the root domain
        }
    } else {
        $base_dir = $dirname;
    }
}

// Guarantee format
$base_dir = '/' . ltrim($base_dir, '/');
$detected_url = "$protocol://$host" . rtrim($base_dir, '/');
define('SITE_URL', $detected_url);

// Payment Gateway API (configurable from Admin Panel → Site Settings → Payment API)
// This is the FALLBACK if no value is stored in the database.
// Change the domain here or via the Admin Panel.
define('KINDAMBA_API_URL', 'https://www.pesagate.online/api');
define('KINDAMBA_PUBLIC_KEY', 'pk_live_f4795b429092469acc72b89300740035'); // Live Key (also configurable from Admin Panel)
define('KINDAMBA_SECRET_KEY', 'sk_live_xxxx'); // Optional but recommended

// Legacy: SonicPesa API (Phasing out)
define('SONICPESA_API_URL', 'https://api.sonicpesa.com/api/v1');
define('SONICPESA_API_TOKEN', 'sk_live_hqNOXO2GTWQqAJo54a4IunLCKOeK57gGzcDKmCyg');
define('SONICPESA_API_SECRET', 'REINuSYQnSPqsteL8d25CFj4bIGfAi7xmeccysM8DnC56z9nlT3iW6DM2erZUras');

// NextSMS Configuration (nextsms.co.tz)
define('NEXTSMS_USERNAME', 'YOUR_NEXTSMS_USERNAME');
define('NEXTSMS_PASSWORD', 'YOUR_NEXTSMS_PASSWORD');
define('NEXTSMS_SENDER_ID', 'NEXTSMS'); // Or your registered Sender ID
define('NEXTSMS_ADMIN_PHONE', '255xxxxxxxxx'); // Your phone number to receive notifications
?>
