<?php
// validation.php - Form Validation Functions
// File location: /bidwheels.net/includes/validation.php

<?php
// validation.php - Form Validation Functions
// File location: /bidwheels.net/includes/validation.php

/**
 * Validate email address
 */
function validateEmail($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

/**
 * Validate phone number (US format)
 */
function validatePhone($phone) {
    // Remove non-numeric characters
    $phone = preg_replace('/[^0-9]/', '', $phone);
    return strlen($phone) >= 10 && strlen($phone) <= 15;
}

/**
 * Validate URL
 */
function validateUrl($url) {
    return filter_var($url, FILTER_VALIDATE_URL) !== false;
}

/**
 * Validate password strength
 */
function validatePassword($password, $min_length = 6) {
    if (strlen($password) < $min_length) {
        return false;
    }
    return true;
}

/**
 * Validate bid amount
 */
function validateBidAmount($amount, $current_highest, $starting_price) {
    $min_bid = max($current_highest + 1, $starting_price);
    return $amount >= $min_bid;
}

/**
 * Validate date range
 */
function validateDateRange($start_date, $end_date) {
    $start = strtotime($start_date);
    $end = strtotime($end_date);
    return $start !== false && $end !== false && $start <= $end;
}

/**
 * Sanitize array input
 */
function sanitizeArray($array) {
    $sanitized = [];
    foreach ($array as $key => $value) {
        $sanitized[$key] = sanitize($value);
    }
    return $sanitized;
}

/**
 * Check if string contains only letters and spaces
 */
function isAlpha($str) {
    return preg_match('/^[a-zA-Z\s]+$/', $str);
}

/**
 * Check if string is alphanumeric
 */
function isAlphaNumeric($str) {
    return preg_match('/^[a-zA-Z0-9\s]+$/', $str);
}

/**
 * Validate credit card number (Luhn algorithm)
 */
function validateCreditCard($number) {
    $number = preg_replace('/\D/', '', $number);
    if (strlen($number) < 13 || strlen($number) > 19) {
        return false;
    }
    
    $sum = 0;
    $alt = false;
    for ($i = strlen($number) - 1; $i >= 0; $i--) {
        $n = $number[$i];
        if ($alt) {
            $n *= 2;
            if ($n > 9) {
                $n = ($n % 10) + 1;
            }
        }
        $sum += $n;
        $alt = !$alt;
    }
    return ($sum % 10 == 0);
}

/**
 * Validate expiry date (MM/YY)
 */
function validateExpiryDate($expiry) {
    if (!preg_match('/^(0[1-9]|1[0-2])\/\d{2}$/', $expiry)) {
        return false;
    }
    
    $exp_month = substr($expiry, 0, 2);
    $exp_year = '20' . substr($expiry, 3, 2);
    
    $current_month = date('m');
    $current_year = date('Y');
    
    if ($exp_year < $current_year) {
        return false;
    }
    if ($exp_year == $current_year && $exp_month < $current_month) {
        return false;
    }
    
    return true;
}

/**
 * Validate CSRF token
 */
function validateCsrfToken($token) {
    if (!isset($_SESSION['csrf_token']) || $_SESSION['csrf_token'] !== $token) {
        return false;
    }
    return true;
}

/**
 * Generate CSRF token
 */
function generateCsrfToken() {
    if (!isset($_SESSION['csrf_token'])) {
        $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
    }
    return $_SESSION['csrf_token'];
}

/**
 * CSRF token input field
 */
function csrfField() {
    return '<input type="hidden" name="csrf_token" value="' . generateCsrfToken() . '">';
}
?>