#!/bin/bash
# Install ionCube Loader for PHP 8.3 (billing site)
# Run with: sudo bash install-ioncube-php83.sh

echo "=== Installing ionCube Loader for PHP 8.3 ==="
echo ""

PHP_VERSION="8.3"
# PHP 8.3 uses extension directory 20230831
PHP_EXT_DIR="/usr/lib/php/20230831"

if [ ! -d "$PHP_EXT_DIR" ]; then
    echo "❌ PHP 8.3 extension directory not found: $PHP_EXT_DIR"
    exit 1
fi

echo "PHP Version: $PHP_VERSION"
echo "Extension Directory: $PHP_EXT_DIR"
echo ""

# Check if loader already exists
LOADER_FILE="$PHP_EXT_DIR/ioncube_loader_lin_${PHP_VERSION}.so"
if [ -f "$LOADER_FILE" ]; then
    echo "✓ Loader file already exists: $LOADER_FILE"
else
    echo "1. Downloading ionCube loader for PHP $PHP_VERSION..."
    cd /tmp
    wget -q https://downloads.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
    
    if [ $? -ne 0 ]; then
        echo "❌ Failed to download ionCube loader"
        echo "Please download manually from: https://downloads.ioncube.com/loader_downloads/"
        exit 1
    fi
    
    echo "✓ Downloaded"
    echo ""
    
    echo "2. Extracting..."
    tar -xzf ioncube_loaders_lin_x86-64.tar.gz
    echo "✓ Extracted"
    echo ""
    
    if [ -f "ioncube/ioncube_loader_lin_${PHP_VERSION}.so" ]; then
        echo "3. Copying loader to extension directory..."
        sudo cp "ioncube/ioncube_loader_lin_${PHP_VERSION}.so" "$LOADER_FILE"
        sudo chmod 644 "$LOADER_FILE"
        echo "✓ Copied to $LOADER_FILE"
    else
        echo "❌ Loader file not found in archive"
        echo "Available loaders:"
        ls -1 ioncube/ioncube_loader_lin_*.so 2>/dev/null | head -5
        rm -rf /tmp/ioncube /tmp/ioncube_loaders_lin_x86-64.tar.gz
        exit 1
    fi
    
    # Cleanup
    rm -rf /tmp/ioncube /tmp/ioncube_loaders_lin_x86-64.tar.gz
fi

echo ""

# Create configuration
echo "4. Creating PHP-FPM configuration..."
MODS_FILE="/etc/php/${PHP_VERSION}/mods-available/00-ioncube.ini"
CONFIG_FILE="/etc/php/${PHP_VERSION}/fpm/conf.d/00-ioncube.ini"

if [ ! -f "$MODS_FILE" ]; then
    echo "zend_extension=$LOADER_FILE" | sudo tee "$MODS_FILE" > /dev/null
    echo "✓ Created $MODS_FILE"
else
    echo "✓ $MODS_FILE already exists"
    # Update it to ensure correct path
    echo "zend_extension=$LOADER_FILE" | sudo tee "$MODS_FILE" > /dev/null
fi

if [ ! -L "$CONFIG_FILE" ] && [ ! -f "$CONFIG_FILE" ]; then
    sudo ln -s "$MODS_FILE" "$CONFIG_FILE"
    echo "✓ Created symlink $CONFIG_FILE"
elif [ -f "$CONFIG_FILE" ] && [ ! -L "$CONFIG_FILE" ]; then
    echo "zend_extension=$LOADER_FILE" | sudo tee "$CONFIG_FILE" > /dev/null
    echo "✓ Updated $CONFIG_FILE"
else
    echo "✓ $CONFIG_FILE already configured"
fi

echo ""

# Also add to main php.ini if not present
MAIN_INI="/etc/php/${PHP_VERSION}/fpm/php.ini"
if [ -f "$MAIN_INI" ] && ! grep -q "ioncube_loader" "$MAIN_INI"; then
    echo "zend_extension=$LOADER_FILE" | sudo tee -a "$MAIN_INI" > /dev/null
    echo "✓ Added to $MAIN_INI"
fi

echo ""

# Restart PHP-FPM 8.3
echo "5. Restarting PHP-FPM 8.3..."
sudo systemctl restart php8.3-fpm
sleep 2

if sudo systemctl is-active --quiet php8.3-fpm; then
    echo "✓ PHP-FPM 8.3 restarted"
else
    echo "❌ PHP-FPM 8.3 failed to restart"
    sudo systemctl status php8.3-fpm --no-pager | head -10
    exit 1
fi

echo ""

# Test
echo "6. Testing ionCube loader..."
if php8.3 -m 2>/dev/null | grep -qi ioncube; then
    echo "✅ ionCube Loader is loaded in PHP 8.3 CLI!"
    php8.3 -r "echo 'Version: ' . ioncube_loader_version() . PHP_EOL;" 2>/dev/null
else
    echo "⚠️  ionCube not loaded in CLI (might need FPM restart)"
fi

echo ""
echo "7. Testing billing site..."
sleep 2
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://billing.friendlytv.site/test-ioncube.php 2>&1)
echo "   HTTP Status: $HTTP_CODE"

if curl -s https://billing.friendlytv.site/test-ioncube.php 2>&1 | grep -qi "ionCube.*LOADED"; then
    echo ""
    echo "✅ SUCCESS! ionCube is now working on billing site!"
else
    echo ""
    echo "⚠️  Still not working. Check:"
    echo "   curl -s https://billing.friendlytv.site/test-ioncube.php"
    echo "   sudo tail -10 /var/log/php8.3-fpm.log"
fi


