YANZ UPLOAD ANJG

success(['status' => 'ok', 'version' => APP_VERSION]); break; default: throw new Exception('Invalid action', 404); } } catch (Exception $e) { $response->error($e->getMessage(), $e->getCode() ?: 400); } // ============================================================================ // Handler Functions // ============================================================================ /** * Handle user sign in */ function handle_signin() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } $email = $_POST['email'] ?? ''; $password = $_POST['password'] ?? ''; // Validate input if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Invalid email address', 400); } if (strlen($password) < PASSWORD_MIN_LENGTH) { throw new Exception('Invalid password', 400); } // Get user from database $db = get_db(); $stmt = $db->prepare('SELECT * FROM users WHERE email = ? LIMIT 1'); $stmt->execute([$email]); $user = $stmt->fetch(); if (!$user || !password_verify($password, $user['password'])) { throw new Exception('Invalid email or password', 401); } // Create session $_SESSION['user_id'] = $user['id']; $_SESSION['user_email'] = $user['email']; $_SESSION['user_name'] = $user['name']; // Log activity log_message('User signed in: ' . $email, 'info', ['user_id' => $user['id']]); $response->success([ 'user' => [ 'id' => $user['id'], 'name' => $user['name'], 'email' => $user['email'], 'avatar' => $user['avatar'] ] ]); } /** * Handle user sign up */ function handle_signup() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } $first_name = $_POST['first_name'] ?? ''; $last_name = $_POST['last_name'] ?? ''; $business_name = $_POST['business_name'] ?? ''; $email = $_POST['email'] ?? ''; $phone = $_POST['phone'] ?? ''; $category = $_POST['category'] ?? ''; // Validate input $validator = new Validator(); $validator->required('first_name', $first_name); $validator->required('last_name', $last_name); $validator->required('business_name', $business_name); $validator->email('email', $email); $validator->required('phone', $phone); $validator->required('category', $category); if (!$validator->passes()) { throw new Exception(implode(', ', $validator->errors()), 400); } // Check if email already exists $db = get_db(); $stmt = $db->prepare('SELECT id FROM users WHERE email = ? LIMIT 1'); $stmt->execute([$email]); if ($stmt->fetch()) { throw new Exception('Email already registered', 409); } // Generate random password $password = bin2hex(random_bytes(8)); $password_hash = password_hash($password, PASSWORD_HASH_ALGO, PASSWORD_HASH_OPTIONS); // Insert user $stmt = $db->prepare(' INSERT INTO users (first_name, last_name, business_name, email, phone, category, password, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, NOW()) '); $stmt->execute([$first_name, $last_name, $business_name, $email, $phone, $category, $password_hash]); $user_id = $db->lastInsertId(); // Create session $_SESSION['user_id'] = $user_id; $_SESSION['user_email'] = $email; $_SESSION['user_name'] = $first_name . ' ' . $last_name; // Log activity log_message('New user registered: ' . $email, 'info', ['user_id' => $user_id]); // TODO: Send welcome email with temporary password $response->success([ 'user' => [ 'id' => $user_id, 'name' => $first_name . ' ' . $last_name, 'email' => $email, 'business_name' => $business_name ], 'message' => 'Account created successfully. Check your email for login details.' ]); } /** * Handle Google OAuth callback */ function handle_google_callback() { global $response; $token = $_POST['token'] ?? ''; if (empty($token)) { throw new Exception('Missing token', 400); } // Verify token with Google $auth = new Auth(); $user_info = $auth->verify_google_token($token); if (!$user_info) { throw new Exception('Invalid token', 401); } // Get or create user $db = get_db(); $stmt = $db->prepare('SELECT * FROM users WHERE email = ? LIMIT 1'); $stmt->execute([$user_info['email']]); $user = $stmt->fetch(); if (!$user) { // Create new user $stmt = $db->prepare(' INSERT INTO users (first_name, last_name, email, avatar, google_id, created_at) VALUES (?, ?, ?, ?, ?, NOW()) '); $stmt->execute([ $user_info['given_name'] ?? '', $user_info['family_name'] ?? '', $user_info['email'], $user_info['picture'] ?? '', $user_info['sub'] ]); $user_id = $db->lastInsertId(); } else { $user_id = $user['id']; // Update Google ID if not set if (empty($user['google_id'])) { $stmt = $db->prepare('UPDATE users SET google_id = ? WHERE id = ?'); $stmt->execute([$user_info['sub'], $user_id]); } } // Create session $_SESSION['user_id'] = $user_id; $_SESSION['user_email'] = $user_info['email']; $_SESSION['user_name'] = ($user_info['given_name'] ?? '') . ' ' . ($user_info['family_name'] ?? ''); // Log activity log_message('User signed in via Google: ' . $user_info['email'], 'info', ['user_id' => $user_id]); $response->success([ 'user' => [ 'id' => $user_id, 'name' => $_SESSION['user_name'], 'email' => $user_info['email'], 'avatar' => $user_info['picture'] ?? '' ] ]); } /** * Handle user logout */ function handle_logout() { global $response; session_destroy(); $response->success(['message' => 'Logged out successfully']); } /** * Handle business listing */ function handle_list_business() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } // Check if user is authenticated if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $business_name = $_POST['business_name'] ?? ''; $description = $_POST['description'] ?? ''; $industry = $_POST['industry'] ?? ''; $location = $_POST['location'] ?? ''; $website = $_POST['website'] ?? ''; $phone = $_POST['phone'] ?? ''; // Validate input $validator = new Validator(); $validator->required('business_name', $business_name); $validator->required('description', $description); $validator->required('industry', $industry); $validator->required('location', $location); $validator->required('phone', $phone); if (!$validator->passes()) { throw new Exception(implode(', ', $validator->errors()), 400); } // Insert business $db = get_db(); $stmt = $db->prepare(' INSERT INTO businesses (user_id, name, description, industry, location, website, phone, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, NOW()) '); $stmt->execute([ $_SESSION['user_id'], $business_name, $description, $industry, $location, $website, $phone ]); $business_id = $db->lastInsertId(); // Log activity log_message('Business listed: ' . $business_name, 'info', ['user_id' => $_SESSION['user_id'], 'business_id' => $business_id]); $response->success([ 'business_id' => $business_id, 'message' => 'Business listed successfully' ]); } /** * Handle get business */ function handle_get_business() { global $response; $business_id = $_GET['id'] ?? null; if (!$business_id) { throw new Exception('Missing business ID', 400); } $db = get_db(); $stmt = $db->prepare('SELECT * FROM businesses WHERE id = ? LIMIT 1'); $stmt->execute([$business_id]); $business = $stmt->fetch(); if (!$business) { throw new Exception('Business not found', 404); } $response->success($business); } /** * Handle search businesses */ function handle_search_businesses() { global $response; $query = $_GET['q'] ?? ''; $category = $_GET['category'] ?? ''; $location = $_GET['location'] ?? ''; $page = (int)($_GET['page'] ?? 1); $limit = min((int)($_GET['limit'] ?? ITEMS_PER_PAGE), MAX_ITEMS_PER_PAGE); $offset = ($page - 1) * $limit; $db = get_db(); $where = ['1=1']; $params = []; if (!empty($query)) { $where[] = '(name LIKE ? OR description LIKE ?)'; $search_term = '%' . $query . '%'; $params[] = $search_term; $params[] = $search_term; } if (!empty($category)) { $where[] = 'industry = ?'; $params[] = $category; } if (!empty($location)) { $where[] = 'location LIKE ?'; $params[] = '%' . $location . '%'; } $where_clause = implode(' AND ', $where); // Get total count $count_stmt = $db->prepare("SELECT COUNT(*) as total FROM businesses WHERE $where_clause"); $count_stmt->execute($params); $total = $count_stmt->fetch()['total']; // Get businesses $stmt = $db->prepare("SELECT * FROM businesses WHERE $where_clause ORDER BY created_at DESC LIMIT ? OFFSET ?"); $stmt->execute(array_merge($params, [$limit, $offset])); $businesses = $stmt->fetchAll(); $response->success([ 'businesses' => $businesses, 'pagination' => [ 'page' => $page, 'limit' => $limit, 'total' => $total, 'pages' => ceil($total / $limit) ] ]); } /** * Handle update business */ function handle_update_business() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $business_id = $_POST['business_id'] ?? null; if (!$business_id) { throw new Exception('Missing business ID', 400); } // Verify ownership $db = get_db(); $stmt = $db->prepare('SELECT user_id FROM businesses WHERE id = ? LIMIT 1'); $stmt->execute([$business_id]); $business = $stmt->fetch(); if (!$business || $business['user_id'] != $_SESSION['user_id']) { throw new Exception('Unauthorized', 403); } // Update business $updates = []; $params = []; if (isset($_POST['name'])) { $updates[] = 'name = ?'; $params[] = $_POST['name']; } if (isset($_POST['description'])) { $updates[] = 'description = ?'; $params[] = $_POST['description']; } if (isset($_POST['industry'])) { $updates[] = 'industry = ?'; $params[] = $_POST['industry']; } if (isset($_POST['location'])) { $updates[] = 'location = ?'; $params[] = $_POST['location']; } if (isset($_POST['website'])) { $updates[] = 'website = ?'; $params[] = $_POST['website']; } if (isset($_POST['phone'])) { $updates[] = 'phone = ?'; $params[] = $_POST['phone']; } if (empty($updates)) { throw new Exception('No fields to update', 400); } $updates[] = 'updated_at = NOW()'; $params[] = $business_id; $stmt = $db->prepare('UPDATE businesses SET ' . implode(', ', $updates) . ' WHERE id = ?'); $stmt->execute($params); // Log activity log_message('Business updated: ' . $business_id, 'info', ['user_id' => $_SESSION['user_id']]); $response->success(['message' => 'Business updated successfully']); } /** * Handle delete business */ function handle_delete_business() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $business_id = $_POST['business_id'] ?? null; if (!$business_id) { throw new Exception('Missing business ID', 400); } // Verify ownership $db = get_db(); $stmt = $db->prepare('SELECT user_id FROM businesses WHERE id = ? LIMIT 1'); $stmt->execute([$business_id]); $business = $stmt->fetch(); if (!$business || $business['user_id'] != $_SESSION['user_id']) { throw new Exception('Unauthorized', 403); } // Delete business $stmt = $db->prepare('DELETE FROM businesses WHERE id = ?'); $stmt->execute([$business_id]); // Log activity log_message('Business deleted: ' . $business_id, 'info', ['user_id' => $_SESSION['user_id']]); $response->success(['message' => 'Business deleted successfully']); } /** * Handle get pricing */ function handle_get_pricing() { global $response; $response->success(PRICING_PLANS); } /** * Handle select plan */ function handle_select_plan() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $plan = $_POST['plan'] ?? ''; if (!isset(PRICING_PLANS[$plan])) { throw new Exception('Invalid plan', 400); } // Update user plan $db = get_db(); $stmt = $db->prepare('UPDATE users SET plan = ?, updated_at = NOW() WHERE id = ?'); $stmt->execute([$plan, $_SESSION['user_id']]); // Log activity log_message('Plan selected: ' . $plan, 'info', ['user_id' => $_SESSION['user_id']]); $response->success([ 'plan' => $plan, 'message' => 'Plan selected successfully' ]); } /** * Handle contact form submission */ function handle_contact() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } $name = $_POST['name'] ?? ''; $email = $_POST['email'] ?? ''; $subject = $_POST['subject'] ?? ''; $message = $_POST['message'] ?? ''; // Validate input $validator = new Validator(); $validator->required('name', $name); $validator->email('email', $email); $validator->required('subject', $subject); $validator->required('message', $message); if (!$validator->passes()) { throw new Exception(implode(', ', $validator->errors()), 400); } // TODO: Send email // Log activity log_message('Contact form submitted: ' . $email, 'info', ['subject' => $subject]); $response->success(['message' => 'Message sent successfully']); } /** * Handle newsletter subscription */ function handle_subscribe() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } $email = $_POST['email'] ?? ''; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Invalid email address', 400); } // Check if already subscribed $db = get_db(); $stmt = $db->prepare('SELECT id FROM subscribers WHERE email = ? LIMIT 1'); $stmt->execute([$email]); if ($stmt->fetch()) { throw new Exception('Email already subscribed', 409); } // Add subscriber $stmt = $db->prepare('INSERT INTO subscribers (email, created_at) VALUES (?, NOW())'); $stmt->execute([$email]); // Log activity log_message('Newsletter subscription: ' . $email, 'info'); $response->success(['message' => 'Subscribed successfully']); } /** * Handle get user */ function handle_get_user() { global $response; if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $db = get_db(); $stmt = $db->prepare('SELECT id, first_name, last_name, email, phone, avatar, plan, created_at FROM users WHERE id = ? LIMIT 1'); $stmt->execute([$_SESSION['user_id']]); $user = $stmt->fetch(); if (!$user) { throw new Exception('User not found', 404); } $response->success($user); } /** * Handle update user */ function handle_update_user() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $updates = []; $params = []; if (isset($_POST['first_name'])) { $updates[] = 'first_name = ?'; $params[] = $_POST['first_name']; } if (isset($_POST['last_name'])) { $updates[] = 'last_name = ?'; $params[] = $_POST['last_name']; } if (isset($_POST['phone'])) { $updates[] = 'phone = ?'; $params[] = $_POST['phone']; } if (empty($updates)) { throw new Exception('No fields to update', 400); } $updates[] = 'updated_at = NOW()'; $params[] = $_SESSION['user_id']; $db = get_db(); $stmt = $db->prepare('UPDATE users SET ' . implode(', ', $updates) . ' WHERE id = ?'); $stmt->execute($params); // Log activity log_message('User profile updated', 'info', ['user_id' => $_SESSION['user_id']]); $response->success(['message' => 'Profile updated successfully']); } /** * Handle change password */ function handle_change_password() { global $response; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Method not allowed', 405); } if (empty($_SESSION['user_id'])) { throw new Exception('Unauthorized', 401); } $current_password = $_POST['current_password'] ?? ''; $new_password = $_POST['new_password'] ?? ''; $confirm_password = $_POST['confirm_password'] ?? ''; // Validate input if (strlen($new_password) < PASSWORD_MIN_LENGTH) { throw new Exception('Password must be at least ' . PASSWORD_MIN_LENGTH . ' characters', 400); } if ($new_password !== $confirm_password) { throw new Exception('Passwords do not match', 400); } // Verify current password $db = get_db(); $stmt = $db->prepare('SELECT password FROM users WHERE id = ? LIMIT 1'); $stmt->execute([$_SESSION['user_id']]); $user = $stmt->fetch(); if (!password_verify($current_password, $user['password'])) { throw new Exception('Current password is incorrect', 401); } // Update password $new_password_hash = password_hash($new_password, PASSWORD_HASH_ALGO, PASSWORD_HASH_OPTIONS); $stmt = $db->prepare('UPDATE users SET password = ?, updated_at = NOW() WHERE id = ?'); $stmt->execute([$new_password_hash, $_SESSION['user_id']]); // Log activity log_message('Password changed', 'info', ['user_id' => $_SESSION['user_id']]); $response->success(['message' => 'Password changed successfully']); } /** * Validate CSRF token */ function validate_csrf_token($token) { return !empty($token) && hash_equals($_SESSION['csrf_token'] ?? '', $token); } // ============================================================================ // End of API Handler // ============================================================================ ?>