52 lines
1.2 KiB
PHP
52 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
/**
|
|
* Safe user login and authentication
|
|
*
|
|
* @param string $username
|
|
* @param string $password
|
|
* @return array|false Returns user array if successful, false otherwise
|
|
*/
|
|
function login_user($username, $password) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username LIMIT 1");
|
|
$stmt->execute([':username' => $username]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
return $user;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Fetch car listings
|
|
*
|
|
* @param array $filters Optional filters (make, province, etc.)
|
|
* @return array List of cars
|
|
*/
|
|
function fetch_cars($filters = []) {
|
|
$pdo = db();
|
|
$sql = "SELECT * FROM cars";
|
|
$params = [];
|
|
|
|
$where = [];
|
|
if (!empty($filters['status'])) {
|
|
$where[] = "status = :status";
|
|
$params[':status'] = $filters['status'];
|
|
}
|
|
|
|
// Add more filters as needed based on the app's requirements
|
|
|
|
if (!empty($where)) {
|
|
$sql .= " WHERE " . implode(" AND ", $where);
|
|
}
|
|
|
|
$sql .= " ORDER BY created_at DESC";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll();
|
|
}
|