tecnickcom / tcexam

TCExam is a CBA (Computer-Based Assessment) system (e-exam, CBT - Computer Based Testing) for universities, schools and companies, that enables educators and trainers to author, schedule, deliver, and report on surveys, quizzes, tests and exams.
http://www.tcexam.org
Other
570 stars 401 forks source link

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

Open koyalkarvarun opened 4 months ago

koyalkarvarun commented 4 months ago

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 commented 4 months ago

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