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j '  'P$a ( `D$Na L?u80e J,K˷NI'0eݷ(NI'؀ 2ipIIKp`:O'`ʤxB8Ѥx Ѥx $ $P6 :vRNb 'p,>NB 'P]-->P T+*^h& p '‰a ‰ (ĵt#u33;Nt̵'ޯ; [3W ~]0KH1q@8]O2]3*̧7# *p>us p _6]/}-4|t'|Smx= DoʾM×M_8!)6lq':l7!|4} '\ne t!=hnLn (~Dn\+‰_4k)0e@OhZ`F `.m1} 'vp{F`ON7Srx 'D˸nV`><;yMx!IS钦OM)Ե٥x 'DSD6bS8!" ODz#R >S8!7ّxEh0m$MIPHi$IvS8IN$I p$O8I,sk&I)$IN$Hi$I^Ah.p$MIN$IR8I·N "IF9Ah0m$MIN$IR8IN$I 3jIU;kO$ɳN$+ q.x* tEXtComment

Viewing File: /home/u456810272/domains/filmmakejobs.com/public_html/api.php

<?php
// api.php - RESTful Backend for FILM Make Jobs
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}

// Start PHP session for dashboard authentication
session_start();

// Database configuration
$host = 'localhost';
$dbname = 'u456810272_filmmakejobs';
$user = 'u456810272_filmmakejobs';
$pass = 'Global@#980';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => 'Database connection failed: ' . $e->getMessage()]);
    exit;
}

// Helper function to send JSON response
function respond($status, $message, $data = null) {
    echo json_encode(['status' => $status, 'message' => $message, 'data' => $data]);
    exit;
}

// Parse request body for POST/PUT
$input = json_decode(file_get_contents('php://input'), true);

// Get action from query string
$action = isset($_GET['action']) ? $_GET['action'] : '';

// Route handling
switch ($action) {
    // -------------------- LOGIN --------------------
    case 'login':
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            respond('error', 'Method not allowed');
        }
        $email = trim($input['email'] ?? '');
        $password = $input['password'] ?? '';
        if (empty($email) || empty($password)) {
            respond('error', 'Email and password required');
        }
        // Fetch user from users table
        $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->execute([$email]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user) {
            respond('error', 'Invalid credentials');
        }
        // Verify password (plaintext stored for admin, but also hash for normal users)
        if (password_verify($password, $user['password']) || $password === $user['password']) {
            // Remove password from response
            unset($user['password']);
            // Store user in PHP session for dashboard
            $_SESSION['user'] = $user;
            respond('success', 'Login successful', $user);
        } else {
            respond('error', 'Invalid credentials');
        }
        break;

    // -------------------- REGISTER --------------------
    case 'register':
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            respond('error', 'Method not allowed');
        }
        $name = trim($input['name'] ?? '');
        $email = trim($input['email'] ?? '');
        $phone = trim($input['phone'] ?? '');
        $password = $input['password'] ?? '';
        if (empty($name) || empty($email) || empty($phone) || empty($password)) {
            respond('error', 'All fields are required');
        }
        if (!preg_match('/^[0-9]+$/', $phone)) {
            respond('error', 'Phone number must contain only digits');
        }
        if (strlen($password) < 6) {
            respond('error', 'Password must be at least 6 characters');
        }
        // Hash password
        $hashed = password_hash($password, PASSWORD_DEFAULT);
        // Insert into users (allow duplicate emails)
        try {
            $pdo->beginTransaction();
            $stmt = $pdo->prepare("INSERT INTO users (name, email, phone, password) VALUES (?, ?, ?, ?)");
            $stmt->execute([$name, $email, $phone, $hashed]);
            $userId = $pdo->lastInsertId();

            // Insert into members
            $stmt = $pdo->prepare("INSERT INTO members (user_id, name, email, phone) VALUES (?, ?, ?, ?)");
            $stmt->execute([$userId, $name, $email, $phone]);

            // Insert free subscription for new member
            $stmt = $pdo->prepare("INSERT INTO subscriptions (user_id, plan, amount, status) VALUES (?, 'Free', 0.00, 'active')");
            $stmt->execute([$userId]);

            $pdo->commit();
            respond('success', 'Registration successful. You can now login.');
        } catch (Exception $e) {
            $pdo->rollBack();
            respond('error', 'Registration failed: ' . $e->getMessage());
        }
        break;

    // -------------------- LOGOUT --------------------
    case 'logout':
        // Destroy session and redirect to index
        session_destroy();
        respond('success', 'Logged out');

    // -------------------- PRODUCTS (CRUD) --------------------
    case 'products':
        $method = $_SERVER['REQUEST_METHOD'];
        switch ($method) {
            case 'GET':
                // Fetch all products (or filter by category)
                $category = isset($_GET['category']) ? $_GET['category'] : null;
                $sql = "SELECT * FROM products";
                $params = [];
                if ($category && $category !== 'all') {
                    $sql .= " WHERE category = ?";
                    $params[] = $category;
                }
                $sql .= " ORDER BY id DESC";
                $stmt = $pdo->prepare($sql);
                $stmt->execute($params);
                $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
                respond('success', 'Products retrieved', $products);
                break;

            case 'POST':
                // Create product
                $name = trim($input['name'] ?? '');
                $price = floatval($input['price'] ?? 0);
                $category = trim($input['category'] ?? '');
                if (empty($name) || empty($price) || empty($category)) {
                    respond('error', 'Name, price, and category required');
                }
                $stmt = $pdo->prepare("INSERT INTO products (name, price, category) VALUES (?, ?, ?)");
                $stmt->execute([$name, $price, $category]);
                respond('success', 'Product created', ['id' => $pdo->lastInsertId()]);
                break;

            case 'PUT':
                // Update product
                $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                if (!$id) {
                    respond('error', 'Product ID required');
                }
                $name = trim($input['name'] ?? '');
                $price = floatval($input['price'] ?? 0);
                $category = trim($input['category'] ?? '');
                if (empty($name) || empty($price) || empty($category)) {
                    respond('error', 'Name, price, and category required');
                }
                $stmt = $pdo->prepare("UPDATE products SET name = ?, price = ?, category = ? WHERE id = ?");
                $stmt->execute([$name, $price, $category, $id]);
                respond('success', 'Product updated');
                break;

            case 'DELETE':
                $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                if (!$id) {
                    respond('error', 'Product ID required');
                }
                $stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
                $stmt->execute([$id]);
                respond('success', 'Product deleted');
                break;

            default:
                respond('error', 'Method not allowed');
        }
        break;

    // -------------------- MEMBERS (CRUD) --------------------
    case 'members':
        $method = $_SERVER['REQUEST_METHOD'];
        switch ($method) {
            case 'GET':
                $stmt = $pdo->query("SELECT * FROM members ORDER BY id DESC");
                $members = $stmt->fetchAll(PDO::FETCH_ASSOC);
                respond('success', 'Members retrieved', $members);
                break;

            case 'POST':
                // Create member (and corresponding user if not exists? We'll just insert into members)
                $name = trim($input['name'] ?? '');
                $email = trim($input['email'] ?? '');
                $phone = trim($input['phone'] ?? '');
                $password = isset($input['password']) ? $input['password'] : '';
                if (empty($name) || empty($email) || empty($phone)) {
                    respond('error', 'Name, email, and phone required');
                }
                if (!preg_match('/^[0-9]+$/', $phone)) {
                    respond('error', 'Phone must contain only digits');
                }
                // Start transaction to create user and member
                try {
                    $pdo->beginTransaction();
                    // Insert user (if password provided, else generate random)
                    if (empty($password)) {
                        $password = bin2hex(random_bytes(8));
                    }
                    $hashed = password_hash($password, PASSWORD_DEFAULT);
                    $stmt = $pdo->prepare("INSERT INTO users (name, email, phone, password) VALUES (?, ?, ?, ?)");
                    $stmt->execute([$name, $email, $phone, $hashed]);
                    $userId = $pdo->lastInsertId();

                    $stmt = $pdo->prepare("INSERT INTO members (user_id, name, email, phone) VALUES (?, ?, ?, ?)");
                    $stmt->execute([$userId, $name, $email, $phone]);

                    // Give free subscription
                    $stmt = $pdo->prepare("INSERT INTO subscriptions (user_id, plan, amount, status) VALUES (?, 'Free', 0.00, 'active')");
                    $stmt->execute([$userId]);

                    $pdo->commit();
                    respond('success', 'Member created', ['id' => $userId]);
                } catch (Exception $e) {
                    $pdo->rollBack();
                    respond('error', 'Failed to create member: ' . $e->getMessage());
                }
                break;

            case 'PUT':
                // Update member (and possibly user)
                $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                if (!$id) {
                    respond('error', 'Member ID required');
                }
                $name = trim($input['name'] ?? '');
                $email = trim($input['email'] ?? '');
                $phone = trim($input['phone'] ?? '');
                $password = isset($input['password']) ? trim($input['password']) : '';
                if (empty($name) || empty($email) || empty($phone)) {
                    respond('error', 'Name, email, and phone required');
                }
                if (!preg_match('/^[0-9]+$/', $phone)) {
                    respond('error', 'Phone must contain only digits');
                }
                try {
                    $pdo->beginTransaction();
                    // Update member table
                    $stmt = $pdo->prepare("UPDATE members SET name = ?, email = ?, phone = ? WHERE id = ?");
                    $stmt->execute([$name, $email, $phone, $id]);

                    // Update user table (linked by user_id)
                    $stmt = $pdo->prepare("SELECT user_id FROM members WHERE id = ?");
                    $stmt->execute([$id]);
                    $member = $stmt->fetch(PDO::FETCH_ASSOC);
                    if ($member) {
                        $userId = $member['user_id'];
                        // Update user email, name, phone
                        $stmt = $pdo->prepare("UPDATE users SET name = ?, email = ?, phone = ? WHERE id = ?");
                        $stmt->execute([$name, $email, $phone, $userId]);
                        // Update password if provided
                        if (!empty($password)) {
                            $hashed = password_hash($password, PASSWORD_DEFAULT);
                            $stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
                            $stmt->execute([$hashed, $userId]);
                        }
                    }
                    $pdo->commit();
                    respond('success', 'Member updated');
                } catch (Exception $e) {
                    $pdo->rollBack();
                    respond('error', 'Update failed: ' . $e->getMessage());
                }
                break;

            case 'DELETE':
                $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                if (!$id) {
                    respond('error', 'Member ID required');
                }
                // Delete member (and user) - cascading via FK?
                $stmt = $pdo->prepare("DELETE FROM members WHERE id = ?");
                $stmt->execute([$id]);
                respond('success', 'Member deleted');
                break;

            default:
                respond('error', 'Method not allowed');
        }
        break;

    // -------------------- SUBSCRIPTIONS (CRUD) --------------------
    case 'subscriptions':
        $method = $_SERVER['REQUEST_METHOD'];
        switch ($method) {
            case 'GET':
                $stmt = $pdo->query("SELECT * FROM subscriptions ORDER BY id DESC");
                $subs = $stmt->fetchAll(PDO::FETCH_ASSOC);
                respond('success', 'Subscriptions retrieved', $subs);
                break;

            case 'POST':
                $userId = intval($input['user_id'] ?? 0);
                $plan = trim($input['plan'] ?? '');
                $amount = floatval($input['amount'] ?? 0);
                $status = trim($input['status'] ?? 'active');
                if (empty($userId) || empty($plan) || empty($amount)) {
                    respond('error', 'User ID, plan, and amount required');
                }
                $stmt = $pdo->prepare("INSERT INTO subscriptions (user_id, plan, amount, status) VALUES (?, ?, ?, ?)");
                $stmt->execute([$userId, $plan, $amount, $status]);
                respond('success', 'Subscription created', ['id' => $pdo->lastInsertId()]);
                break;

            case 'PUT':
                $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                if (!$id) {
                    respond('error', 'Subscription ID required');
                }
                $userId = intval($input['user_id'] ?? 0);
                $plan = trim($input['plan'] ?? '');
                $amount = floatval($input['amount'] ?? 0);
                $status = trim($input['status'] ?? 'active');
                if (empty($userId) || empty($plan) || empty($amount)) {
                    respond('error', 'User ID, plan, and amount required');
                }
                $stmt = $pdo->prepare("UPDATE subscriptions SET user_id = ?, plan = ?, amount = ?, status = ? WHERE id = ?");
                $stmt->execute([$userId, $plan, $amount, $status, $id]);
                respond('success', 'Subscription updated');
                break;

            case 'DELETE':
                $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                if (!$id) {
                    respond('error', 'Subscription ID required');
                }
                $stmt = $pdo->prepare("DELETE FROM subscriptions WHERE id = ?");
                $stmt->execute([$id]);
                respond('success', 'Subscription deleted');
                break;

            default:
                respond('error', 'Method not allowed');
        }
        break;

    // -------------------- SETTINGS (GET/POST) --------------------
    case 'settings':
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method === 'GET') {
            $stmt = $pdo->query("SELECT * FROM settings");
            $settings = [];
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $settings[$row['setting_key']] = $row['setting_value'];
            }
            respond('success', 'Settings retrieved', $settings);
        } elseif ($method === 'POST') {
            // Update settings (key-value pairs)
            $data = $input;
            // Also handle security action separately
            if (isset($data['action']) && $data['action'] === 'security') {
                // Change admin password (for admin user id 1)
                $newPass = trim($data['new_password'] ?? '');
                if (empty($newPass)) {
                    respond('error', 'New password required');
                }
                // Update admin user (assuming id = 1)
                $hashed = password_hash($newPass, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = 1");
                $stmt->execute([$hashed]);
                respond('success', 'Password updated');
            } else {
                // General settings update
                try {
                    $pdo->beginTransaction();
                    foreach ($data as $key => $value) {
                        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
                        $stmt->execute([$key, $value, $value]);
                    }
                    $pdo->commit();
                    respond('success', 'Settings saved');
                } catch (Exception $e) {
                    $pdo->rollBack();
                    respond('error', 'Failed to save settings: ' . $e->getMessage());
                }
            }
        } else {
            respond('error', 'Method not allowed');
        }
        break;

    // -------------------- DASHBOARD STATS --------------------
    case 'dashboard_stats':
        // Fetch counts for dashboard
        $stmt = $pdo->query("SELECT COUNT(*) as total_members FROM members");
        $total_members = $stmt->fetch(PDO::FETCH_ASSOC)['total_members'];

        $stmt = $pdo->query("SELECT COUNT(*) as total_products FROM products");
        $total_products = $stmt->fetch(PDO::FETCH_ASSOC)['total_products'];

        $stmt = $pdo->query("SELECT COUNT(*) as active_subs FROM subscriptions WHERE status='active'");
        $active_subs = $stmt->fetch(PDO::FETCH_ASSOC)['active_subs'];

        $stmt = $pdo->query("SELECT SUM(amount) as revenue FROM subscriptions WHERE status='active'");
        $revenue = $stmt->fetch(PDO::FETCH_ASSOC)['revenue'] ?? 0;

        // Recent members
        $stmt = $pdo->query("SELECT * FROM members ORDER BY id DESC LIMIT 10");
        $recent_members = $stmt->fetchAll(PDO::FETCH_ASSOC);

        respond('success', 'Dashboard stats', [
            'total_members' => (int)$total_members,
            'total_products' => (int)$total_products,
            'active_subs' => (int)$active_subs,
            'revenue' => (float)$revenue,
            'recent_members' => $recent_members
        ]);
        break;

    // -------------------- UNKNOWN ACTION --------------------
    default:
        respond('error', 'Invalid action');
}
Back to Directory=ceiIENDB`