function CheckConditions($m_conditions, $a_vars, &$s_missing, &$a_missing_list, $m_id = false)
{
    if (is_array($m_conditions)) {
        //
        // Sort the conditions by their numeric value.  This ensures
        // conditions are executed in the right order.
        //
        ksort($m_conditions, SORT_NUMERIC);
        foreach ($m_conditions as $m_key => $s_cond) {
            if (!CheckConditions($s_cond, $a_vars, $s_missing, $a_missing_list, $m_key)) {
                return false;
            }
        }
        return true;
    }
    $s_fld_name = "conditions" . ($m_id === false ? "" : $m_id + 1);
    if (!is_string($m_conditions)) {
        SendAlert(GetMessage(MSG_INV_COND, array("FLD" => $s_fld_name)));
        return true;
        // pass invalid conditions
    }
    if ($m_conditions == "") {
        return true;
    }
    // pass empty conditions
    $s_cond = $m_conditions;
    //
    // extract the separator characters
    //
    if (strlen($s_cond) < 2) {
        SendAlert(GetMessage(MSG_COND_CHARS, array("FLD" => $s_fld_name, "COND" => $s_cond)));
        return true;
        // pass invalid conditions
    }
    $s_list_sep = $s_cond[0];
    $s_int_sep = $s_cond[1];
    $s_full_cond = $s_cond = substr($s_cond, 2);
    $b_bad = false;
    $a_list = TrimArray(explode($s_list_sep, $s_cond));
    $s_missing = "";
    $a_missing_list = array();
    for ($ii = 0; $ii < count($a_list); $ii++) {
        $s_cond = $a_list[$ii];
        $i_len = strlen($s_cond);
        if ($i_len <= 0) {
            continue;
        }
        //
        // split the condition into its internal components
        //
        $a_components = TrimArray(explode($s_int_sep, $s_cond));
        if (count($a_components) < 5) {
            SendAlert(GetMessage(MSG_COND_INVALID, array("FLD" => $s_fld_name, "COND" => $s_cond, "SEP" => $s_int_sep)));
            //
            // the smallest condition has 5 components
            //
            continue;
        }
        //
        // first component is ignored (it's blank)
        //
        $a_components = array_slice($a_components, 1);
        switch ($a_components[0]) {
            case "TEST":
                if (count($a_components) > 5) {
                    SendAlert(GetMessage(MSG_COND_TEST_LONG, array("FLD" => $s_fld_name, "COND" => $s_cond, "SEP" => $s_list_sep)));
                    continue;
                }
                if (!RunTest($a_components[1], $a_vars)) {
                    $s_missing .= $a_components[2] . "\n";
                    $a_missing_list[] = $a_components[2];
                    $b_bad = true;
                }
                break;
            case "IF":
                if (count($a_components) < 6) {
                    SendAlert(GetMessage(MSG_COND_IF_SHORT, array("FLD" => $s_fld_name, "COND" => $s_cond, "SEP" => $s_int_sep)));
                    continue;
                }
                if (count($a_components) > 7) {
                    SendAlert(GetMessage(MSG_COND_IF_LONG, array("FLD" => $s_fld_name, "COND" => $s_cond, "SEP" => $s_list_sep)));
                    continue;
                }
                if (RunTest($a_components[1], $a_vars)) {
                    $b_test = RunTest($a_components[2], $a_vars);
                } else {
                    $b_test = RunTest($a_components[3], $a_vars);
                }
                if (!$b_test) {
                    $s_missing .= $a_components[4] . "\n";
                    $a_missing_list[] = $a_components[4];
                    $b_bad = true;
                }
                break;
            default:
                SendAlert(GetMessage(MSG_COND_UNK, array("FLD" => $s_fld_name, "COND" => $s_cond, "CMD" => $a_components[0])));
                break;
        }
    }
    return !$b_bad;
}
Exemple #2
0
function CheckConditions($m_conditions, $a_vars, &$s_missing, &$a_missing_list)
{
    if (is_array($m_conditions)) {
        foreach ($m_conditions as $s_cond) {
            if (!CheckConditions($s_cond, $a_vars, $s_missing, $a_missing_list)) {
                return false;
            }
        }
        return true;
    }
    if (!is_string($m_conditions)) {
        SendAlert("Invalid 'conditions' field - not a string or array.");
        return true;
        // pass invalid conditions
    }
    if ($m_conditions == "") {
        return true;
    }
    // pass empty conditions
    $s_cond = $m_conditions;
    //
    // extract the separator characters
    //
    if (strlen($s_cond) < 2) {
        SendAlert("The 'conditions' field is not valid.  You must provide the two separator characters.\n'{$s_cond}'");
        return true;
        // pass invalid conditions
    }
    $s_list_sep = $s_cond[0];
    $s_int_sep = $s_cond[1];
    $s_full_cond = $s_cond = substr($s_cond, 2);
    $b_bad = false;
    $a_list = TrimArray(explode($s_list_sep, $s_cond));
    $s_missing = "";
    $a_missing_list = array();
    for ($ii = 0; $ii < count($a_list); $ii++) {
        $s_cond = $a_list[$ii];
        $i_len = strlen($s_cond);
        if ($i_len <= 0) {
            continue;
        }
        //
        // split the condition into its internal components
        //
        $a_components = TrimArray(explode($s_int_sep, $s_cond));
        if (count($a_components) < 5) {
            SendAlert("Condition '{$s_cond}' is not valid");
            //
            // the smallest condition has 5 components
            //
            continue;
        }
        //
        // first component is ignored (it's blank)
        //
        $a_components = array_slice($a_components, 1);
        switch ($a_components[0]) {
            case "TEST":
                if (count($a_components) > 5) {
                    SendAlert("Too many components to 'TEST' command '{$s_cond}'.\nAre you missing a '{$s_list_sep}'?");
                    continue;
                }
                if (!RunTest($a_components[1], $a_vars)) {
                    $s_missing .= $a_components[2] . "\n";
                    $a_missing_list[] = $a_components[2];
                    $b_bad = true;
                }
                break;
            case "IF":
                if (count($a_components) < 6) {
                    SendAlert("Condition '{$s_cond}' is not a valid IF command");
                    continue;
                }
                if (count($a_components) > 7) {
                    SendAlert("Too many components to 'IF' command '{$s_cond}'.\nAre you missing a '{$s_list_sep}'?");
                    continue;
                }
                if (RunTest($a_components[1], $a_vars)) {
                    $b_test = RunTest($a_components[2], $a_vars);
                } else {
                    $b_test = RunTest($a_components[3], $a_vars);
                }
                if (!$b_test) {
                    $s_missing .= $a_components[4] . "\n";
                    $a_missing_list[] = $a_components[4];
                    $b_bad = true;
                }
                break;
            default:
                SendAlert("Condition '{$s_cond}' has an unknown command '" . $a_components[0] . "'");
                break;
        }
    }
    return !$b_bad;
}