File "branded-url.php"

Full path: /home/u525140468/domains/productsizer.com/public_html/admin/branded-url.php
File size: 8.78 B (8.78 KB bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php


// Ensure 'go' folder exists
$go_dir = '../go';
if (!is_dir($go_dir)) mkdir($go_dir, 0755, true);

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $custom_name = preg_replace('/[^a-zA-Z0-9-_]/', '', trim($_POST['custom_name'] ?? ''));
    $target_url = trim($_POST['target_url'] ?? '');

    if ($target_url && !filter_var($target_url, FILTER_VALIDATE_URL)) {
        $error = 'Invalid URL format.';
    } elseif ($action === 'create' && $custom_name && $target_url) {
        $file = "$go_dir/$custom_name.php";
        if (!file_exists($file)) {
            file_put_contents($file, "<?php\nheader('Location: $target_url');\nexit;\n?>");
            header('Location: branded-url.php');
            exit;
        }
        $error = 'Custom name already exists.';
    } elseif ($action === 'update' && $custom_name && $target_url) {
        $file = "$go_dir/$custom_name.php";
        if (file_exists($file)) {
            file_put_contents($file, "<?php\nheader('Location: $target_url');\nexit;\n?>");
            header('Location: branded-url.php');
            exit;
        }
    } elseif ($action === 'delete' && $custom_name) {
        $file = "$go_dir/$custom_name.php";
        if (file_exists($file)) {
            unlink($file);
            header('Location: branded-url.php');
            exit;
        }
    }
}

$edit_mode = false;
$edit_custom_name = '';
$edit_target_url = '';
if ($_POST['action'] ?? '' === 'edit' && !empty($_POST['custom_name'])) {
    $edit_mode = true;
    $edit_custom_name = trim($_POST['custom_name']);
    $file = "$go_dir/$edit_custom_name.php";
    if (file_exists($file)) {
        preg_match("/header\('Location: (.+?)'\);/", file_get_contents($file), $matches);
        $edit_target_url = $matches[1] ?? '';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Branded URL Manager</title>
	  <?php include "data/styles.php"?>
    <style>
        .tooltip { 
            display: none; 
            position: absolute; 
            background: #333; 
            color: white; 
            padding: 4px 8px; 
            border-radius: 4px; 
            top: -30px; 
            left: 50%; 
            transform: translateX(-50%); 
            white-space: nowrap;
            z-index: 10;
        }
        .relative:hover .tooltip:not(.show) { display: block; }
        .tooltip.show { display: block; }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <h1 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
            <i data-lucide="<?php echo $edit_mode ? 'edit' : 'link'; ?>" class="mr-2"></i>
            <?php echo $edit_mode ? 'Edit URL' : 'Branded URL Manager'; ?>
        </h1>
        <?php if ($error): ?>
        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
            <?php echo htmlspecialchars($error); ?>
        </div>
        <?php endif; ?>
        <div class="bg-white p-6 rounded-lg shadow-md mb-6">
            <form action="" method="POST" class="flex flex-col md:flex-row gap-4">
                <div class="flex-1">
                    <label for="custom_name" class="block text-sm font-medium text-gray-700">Custom Name</label>
                    <input type="text" name="custom_name" id="custom_name" value="<?php echo htmlspecialchars($edit_custom_name); ?>" 
                           placeholder="e.g., custom-name1" class="mt-1 w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 <?php echo $edit_mode ? 'bg-gray-100' : ''; ?>" 
                           <?php echo $edit_mode ? 'readonly' : 'required'; ?>>
                </div>
                <div class="flex-1">
                    <label for="target_url" class="block text-sm font-medium text-gray-700">Target URL</label>
                    <input type="url" name="target_url" id="target_url" value="<?php echo htmlspecialchars($edit_target_url); ?>" 
                           placeholder="e.g., https://example.com" class="mt-1 w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" required>
                </div>
                <div class="flex items-end">
                    <button type="submit" name="action" value="<?php echo $edit_mode ? 'update' : 'create'; ?>" 
                            class="bg-indigo-600 text-white px-4 py-2 rounded-md hover:bg-indigo-700 flex items-center">
                        <i data-lucide="<?php echo $edit_mode ? 'save' : 'plus-circle'; ?>" class="mr-2"></i>
                        <?php echo $edit_mode ? 'Update' : 'Create'; ?>
                    </button>
                </div>
            </form>
        </div>
        <?php if (!$edit_mode): ?>
        <div class="bg-white p-6 rounded-lg shadow-md">
            <h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                <i data-lucide="list" class="mr-2"></i> Existing URLs
            </h2>
            <div class="overflow-x-auto">
                <table class="w-full text-sm text-left text-gray-500">
                    <thead class="text-xs text-gray-700 uppercase bg-gray-50">
                        <tr>
                            <th class="px-6 py-3">Custom Name</th>
                            <th class="px-6 py-3">Target URL</th>
                            <th class="px-6 py-3">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/go/';

                        $base_url = "https://productsizer.com/go/";
                        foreach (glob("$go_dir/*.php") as $url) {
                            $custom_name = basename($url, '.php');
                            preg_match("/header\('Location: (.+?)'\);/", file_get_contents($url), $matches);
                            $target_url = $matches[1] ?? '';
                            $short_url = $base_url . $custom_name;
                            echo "
                            <tr class='border-b'>
                                <td class='px-6 py-4'>$custom_name</td>
                                <td class='px-6 py-4'>$target_url</td>
                                <td class='px-6 py-4'>
                                    <form action='' method='POST' class='inline-flex gap-2'>
                                        <input type='hidden' name='custom_name' value='$custom_name'>
                                        <button type='submit' name='action' value='edit' class='text-blue-600 hover:text-blue-800'>
                                            <i data-lucide='edit'></i>
                                        </button>
                                        <button type='submit' name='action' value='delete' class='text-red-600 hover:text-red-800'>
                                            <i data-lucide='trash-2'></i>
                                        </button>
                                        <button type='button' onclick='copyUrl(\"$short_url\", this)' class='text-green-600 hover:text-green-800 relative'>
                                            <i data-lucide='copy' class='copy-icon'></i>
                                            <span class='tooltip'>Copied!</span>
                                        </button>
                                    </form>
                                </td>
                            </tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>
    <script>
        lucide.createIcons();
        async function copyUrl(url, button) {
            try {
                await navigator.clipboard.writeText(url);
                const icon = button.querySelector('.copy-icon');
                const tooltip = button.querySelector('.tooltip');
                
                // Change icon and show tooltip
                icon.setAttribute('data-lucide', 'circle-check-big');
                tooltip.classList.add('show');
                lucide.createIcons();
                
                // Revert after 3 seconds
                setTimeout(() => {
                    icon.setAttribute('data-lucide', 'copy');
                    tooltip.classList.remove('show');
                    lucide.createIcons();
                }, 3000);
            } catch (err) {
                console.error('Failed to copy: ', err);
            }
        }
    </script>
</body>
</html>