Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F_isValidIP in shared/code/tce_functions_test.php is not validating IPV6 address #431

Open
koyalkarvarun opened this issue Jul 7, 2024 · 1 comment

Comments

@koyalkarvarun
Copy link

koyalkarvarun commented Jul 7, 2024

I my test management I have allowed test to be taken for all IP address that would be ... when I have made our user to write test so I have received complaints that some user where able see table of existing test and some where unable to see test. Instead message was being showed as not test is allotted/available . So I have drilled code a bit and got to know that F_isValidIP in shared/code/tce_functions_test.php is working fine with ipv4 version but it is not working with ipv6 version.
edited_tcexam_error.

I have found solution to this and if you want I can create a pull request which not only solve this issue and will solve some minor issue.

@koyalkarvarun
Copy link
Author

possibly code in F_isValidIP should be replaced with this code

function F_isValidIP($user_ip, $test_ips) {
    if (empty($user_ip) || empty($test_ips)) {
        return false;
    }

    // Normalize IPs
    $usrip = inet_pton($user_ip);
    if ($usrip === false) {
        return false;
    }

    $test_ip_ranges = explode(',', $test_ips);
    foreach ($test_ip_ranges as $ip_range) {
        // Handle wildcard *.*.*.*
        if ($ip_range === '*.*.*.*' || $ip_range === '::/0') {
            return true;
        }

        // Handle IP range with '-'
        if (strpos($ip_range, '-') !== false) {
            list($start_ip, $end_ip) = explode('-', $ip_range, 2);
            $start_ip = inet_pton(trim($start_ip));
            $end_ip = inet_pton(trim($end_ip));

            if ($start_ip === false || $end_ip === false) {
                continue;
            }

            if ($usrip >= $start_ip && $usrip <= $end_ip) {
                return true;
            }
        } else {
            // Handle single IP or wildcard within an IP
            $wildcard_ip = str_replace('*', '0', $ip_range);
            $wildcard_mask = str_replace('*', '255', $ip_range);

            $start_ip = inet_pton(trim($wildcard_ip));
            $end_ip = inet_pton(trim($wildcard_mask));

            if ($start_ip === false || $end_ip === false) {
                continue;
            }

            if ($usrip >= $start_ip && $usrip <= $end_ip) {
                return true;
            }
        }
    }

    return false;
}

Your project is awesome and can help many of us. Please review this issue and do need full thing to it. Thank you for bring this project on GitHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant