function RunTest($s_test, $a_vars)
{
    global $aAlertInfo;
    $s_op_chars = "&|^!=~#<>";
    // these are the characters for the operators
    $i_len = strlen($s_test);
    $b_ok = true;
    if ($i_len <= 0) {
        //
        // empty test - true
        //
    } elseif ($s_test == "!") {
        //
        // test asserts false
        //
        $b_ok = false;
    } elseif (($i_span = strcspn($s_test, $s_op_chars)) >= $i_len) {
        //
        // no operator - just check field presence
        //
        $b_ok = !TestFieldEmpty($s_test, $a_vars, $s_mesg);
    } else {
        //
        // get first field name
        //
        $s_fld1 = trim(substr($s_test, 0, $i_span));
        //
        // get the operator
        //
        $s_rem = substr($s_test, $i_span);
        $i_span = strspn($s_rem, $s_op_chars);
        $s_oper = substr($s_rem, 0, $i_span);
        switch ($s_oper) {
            case '&':
            case '|':
            case '^':
            case '=':
            case '!=':
                //
                // get the second field name
                //
                $s_fld2 = trim(substr($s_rem, $i_span));
                $b_ok = FieldTest($s_oper, $s_fld1, $s_fld2, $a_vars, $s_error_mesg);
                break;
            case '~':
            case '!~':
                //
                // get the regular expression
                //
                $s_pat = trim(substr($s_rem, $i_span));
                if (!TestFieldEmpty($s_fld1, $a_vars, $s_mesg)) {
                    $s_value = GetFieldValue($s_fld1, $a_vars);
                } else {
                    $s_value = "";
                }
                //echo "<p>Pattern: '".htmlspecialchars($s_pat)."': count=".preg_match($s_pat,$s_value)."<br /></p>";
                //
                // match the regular expression
                //
                if (preg_match($s_pat, $s_value) > 0) {
                    $b_ok = $s_oper == '~';
                } else {
                    $b_ok = $s_oper == '!~';
                }
                if (!$b_ok) {
                    $aAlertInfo[] = GetMessage(MSG_PAT_FAILED, array("OPER" => $s_oper, "PAT" => $s_pat, "VALUE" => $s_value));
                }
                break;
            case '#=':
            case '#!=':
            case '#<':
            case '#>':
            case '#<=':
            case '#>=':
                //
                // numeric tests
                //
                $s_num = trim(substr($s_rem, $i_span));
                //
                // if this is a file field, get the size of the file for
                // numeric tests
                //
                if (($s_value = GetFileSize($s_fld1)) === false) {
                    $s_value = $a_vars[$s_fld1];
                }
                if (strpos($s_num, '.') === false) {
                    //
                    // treat as integer
                    //
                    $m_num = (int) $s_num;
                    $m_fld = (int) $s_value;
                } else {
                    //
                    // treat as floating point
                    //
                    $m_num = (double) $s_num;
                    $m_fld = (double) $s_value;
                }
                switch ($s_oper) {
                    case '#=':
                        $b_ok = $m_fld == $m_num;
                        break;
                    case '#!=':
                        $b_ok = $m_fld != $m_num;
                        break;
                    case '#<':
                        $b_ok = $m_fld < $m_num;
                        break;
                    case '#>':
                        $b_ok = $m_fld > $m_num;
                        break;
                    case '#<=':
                        $b_ok = $m_fld <= $m_num;
                        break;
                    case '#>=':
                        $b_ok = $m_fld >= $m_num;
                        break;
                }
                break;
            default:
                SendAlert(GetMessage(MSG_COND_OPER, array("OPER" => $s_oper)));
                break;
        }
    }
    return $b_ok;
}
예제 #2
0
function RunTest($s_test, $a_vars)
{
    global $aAlertInfo;
    $s_op_chars = "|^!=~#<>";
    // these are the characters for the operators
    $i_len = strlen($s_test);
    $b_ok = true;
    if ($i_len <= 0) {
        //
        // empty test - true
        //
    } elseif ($s_test == "!") {
        //
        // test asserts false
        //
        $b_ok = false;
    } elseif (($i_span = strcspn($s_test, $s_op_chars)) >= $i_len) {
        //
        // no operator - just check field presence
        //
        $b_ok = isset($a_vars[$s_test]) && !empty($a_vars[$s_test]);
    } else {
        //
        // get first field name
        //
        $s_fld1 = trim(substr($s_test, 0, $i_span));
        //
        // get the operator
        //
        $s_rem = substr($s_test, $i_span);
        $i_span = strspn($s_rem, $s_op_chars);
        $s_oper = substr($s_rem, 0, $i_span);
        switch ($s_oper) {
            case '|':
            case '^':
            case '=':
            case '!=':
                //
                // get the second field name
                //
                $s_fld2 = trim(substr($s_rem, $i_span));
                $b_ok = FieldTest($s_oper, $s_fld1, $s_fld2, $a_vars, $s_error_mesg);
                break;
            case '~':
            case '!~':
                //
                // get the regular expression
                //
                $s_pat = trim(substr($s_rem, $i_span));
                if (isset($a_vars[$s_fld1])) {
                    $s_value = $a_vars[$s_fld1];
                } else {
                    $s_value = "";
                }
                //echo "<p>Pattern: '".htmlspecialchars($s_pat)."': count=".preg_match($s_pat,$s_value)."<br /></p>";
                //
                // match the regular expression
                //
                if (preg_match($s_pat, $s_value) > 0) {
                    $b_ok = $s_oper == '~';
                } else {
                    $b_ok = $s_oper == '!~';
                }
                if (!$b_ok) {
                    $aAlertInfo[] = "Pattern operator '{$s_oper}' failed: pattern '{$s_pat}', value searched was '{$s_value}'.";
                }
                break;
            case '#=':
            case '#!=':
            case '#<':
            case '#>':
            case '#<=':
            case '#>=':
                //
                // numeric tests
                //
                $s_num = trim(substr($s_rem, $i_span));
                if (strpos($s_num, '.') === false) {
                    //
                    // treat as integer
                    //
                    $m_num = (int) $s_num;
                    $m_fld = (int) $a_vars[$s_fld1];
                } else {
                    //
                    // treat as floating point
                    //
                    $m_num = (double) $s_num;
                    $m_fld = (double) $a_vars[$s_fld1];
                }
                switch ($s_oper) {
                    case '#=':
                        $b_ok = $m_fld == $m_num;
                        break;
                    case '#!=':
                        $b_ok = $m_fld != $m_num;
                        break;
                    case '#<':
                        $b_ok = $m_fld < $m_num;
                        break;
                    case '#>':
                        $b_ok = $m_fld > $m_num;
                        break;
                    case '#<=':
                        $b_ok = $m_fld <= $m_num;
                        break;
                    case '#>=':
                        $b_ok = $m_fld >= $m_num;
                        break;
                }
                break;
            default:
                SendAlert("Operator '{$s_oper}' is not valid for 'conditions'");
                break;
        }
    }
    return $b_ok;
}