HEX
Server: Apache/2.4.63 (Unix)
System: Linux TOMS-220NAS 4.4.302+ #86009 SMP Wed Nov 26 18:19:17 CST 2025 x86_64
User: flavio87 (1026)
PHP: 8.3.27
Disabled: NONE
Upload Files
File: /volume1/@appstore/WebStation/tools/merge_php_profiles.py
#!/usr/bin/env python
"""
WebStation PHP Profiles Merger

This tool merges new PHP profiles with existing ones while preserving user modifications.
Used during WebStation package installation.
"""

import json
import sys
import os
import syslog


def log_error(message: str):
    """Log error message to syslog."""
    syslog.openlog("webstation-merge-php-profiles", syslog.LOG_PID, syslog.LOG_DAEMON)
    syslog.syslog(syslog.LOG_ERR, message)
    syslog.closelog()


def merge_php_profiles(existing_file: str, new_file: str, output_file: str) -> bool:
    """
    Merge PHP profiles from new file into existing file.

    Args:
        existing_file: Path to existing PHPSettings.json
        new_file: Path to new PHPSettings.json (from misc/)
        output_file: Path where merged result should be written

    Returns:
        True if merge was successful, False otherwise
    """
    try:
        # Load existing profiles
        with open(existing_file, 'r', encoding='utf-8') as f:
            existing = json.load(f)

        # Load new profiles
        with open(new_file, 'r', encoding='utf-8') as f:
            new = json.load(f)

        # Update version if newer
        if 'version' in new:
            existing['version'] = max(existing.get('version', 0), new['version'])

        # Add new profiles (skip if UUID already exists to preserve user modifications)
        for uuid, profile in new.items():
            if uuid == 'version':
                continue
            if uuid not in existing:
                existing[uuid] = profile

        # Write merged result
        with open(output_file, 'w', encoding='utf-8') as f:
            json.dump(existing, f, indent='\t')

        return True

    except Exception as e:
        log_error(f"Merge failed: {e}")
        return False


def main():
    """Main entry point for the merge tool."""
    if len(sys.argv) != 4:
        log_error("Invalid arguments")
        sys.exit(1)

    existing_file, new_file, output_file = sys.argv[1:4]

    # Validate input files exist
    if not os.path.exists(existing_file):
        log_error(f"Existing file not found: {existing_file}")
        sys.exit(1)

    if not os.path.exists(new_file):
        log_error(f"New file not found: {new_file}")
        sys.exit(1)

    # Perform merge
    success = merge_php_profiles(existing_file, new_file, output_file)
    sys.exit(0 if success else 1)


if __name__ == "__main__":
    main()