Ejemplo n.º 1
0
function SubstituteValueWorker($a_matches, $s_repl, $b_html = true)
{
    global $aSubstituteErrors, $aSubstituteValues, $SPECIAL_VALUES;
    $b_insert_br = true;
    // option to put "<br />" tags before newlines in HTML templates
    $n_max_chars = 0;
    $n_max_lines = 0;
    $s_name = $a_matches[0];
    assert(strlen($s_name) > 1 && $s_name[0] == '$');
    $s_name = substr($s_name, 1);
    if (($i_len = strlen($s_name)) > 0 && $s_name[0] == '{') {
        assert($s_name[$i_len - 1] == '}');
        $s_name = substr($s_name, 1, -1);
        //
        // grab any processing options
        //
        $a_args = explode(":", $s_name);
        $s_name = $a_args[0];
        if (($n_args = count($a_args)) > 1) {
            for ($ii = 1; $ii < $n_args; $ii++) {
                //
                // some options are followed by =X
                // where X is a value
                //
                $s_param = "";
                if (($i_pos = strpos($a_args[$ii], '=')) !== false) {
                    $s_param = substr($a_args[$ii], $i_pos + 1);
                    $s_opt = substr($a_args[$ii], 0, $i_pos);
                } else {
                    $s_opt = $a_args[$ii];
                }
                switch ($s_opt) {
                    case "nobr":
                        $b_insert_br = false;
                        break;
                    case "chars":
                        if ($s_param !== "") {
                            $n_max_chars = (int) $s_param;
                        }
                        break;
                    case "lines":
                        if ($s_param !== "") {
                            $n_max_lines = (int) $s_param;
                        }
                        break;
                }
            }
        }
    }
    $s_value = "";
    if (IsFieldSet($s_name, $aSubstituteValues) && !TestFieldEmpty($s_name, $aSubstituteValues, $s_mesg)) {
        $s_value = GetFieldValue($s_name, $aSubstituteValues, $SPECIAL_VALUES['template_list_sep']);
        if ($b_html) {
            $s_value = htmlspecialchars($s_value);
        }
        $s_value = Truncate($s_value, $n_max_chars, $n_max_lines);
        if ($b_html && $b_insert_br) {
            //
            // Insert HTML line breaks before newlines.
            //
            $s_value = nl2br($s_value);
        }
    } elseif (isset($SPECIAL_VALUES[$s_name])) {
        $s_value = $b_html ? htmlspecialchars((string) $SPECIAL_VALUES[$s_name]) : (string) $SPECIAL_VALUES[$s_name];
        $s_value = Truncate($s_value, $n_max_chars, $n_max_lines);
    } elseif (isset($s_repl)) {
        //
        // If a replacement value has been specified use it, and
        // don't call htmlspecialchars.  This allows the use
        // of HTML tags in a replacement string.
        //
        $s_value = $s_repl;
    } else {
        $s_value = "";
    }
    return $s_value;
}
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;
}
Ejemplo n.º 3
0
function SubstituteValueWorker($a_matches, $s_repl, $b_html = true)
{
    global $aSubstituteErrors, $aSubstituteValues, $SPECIAL_VALUES;
    $b_insert_br = true;
    // option to put "<br />" tags before newlines in HTML templates
    $s_name = $a_matches[0];
    assert(strlen($s_name) > 1 && $s_name[0] == '$');
    $s_name = substr($s_name, 1);
    if (($i_len = strlen($s_name)) > 0 && $s_name[0] == '{') {
        assert($s_name[$i_len - 1] == '}');
        $s_name = substr($s_name, 1, -1);
        //
        // grab any processing options
        //
        $a_args = explode(":", $s_name);
        $s_name = $a_args[0];
        if (($n_args = count($a_args)) > 1) {
            for ($ii = 1; $ii < $n_args; $ii++) {
                switch ($a_args[$ii]) {
                    case "nobr":
                        $b_insert_br = false;
                        break;
                }
            }
        }
    }
    $s_value = "";
    if (IsFieldSet($s_name, $aSubstituteValues) && !TestFieldEmpty($s_name, $aSubstituteValues, $s_mesg)) {
        if (is_array($aSubstituteValues[$s_name])) {
            //
            // note that the separator can include HTML special chars
            //
            $s_value = implode($SPECIAL_VALUES['template_list_sep'], $b_html ? ArrayHTMLSpecialChars($aSubstituteValues[$s_name]) : $aSubstituteValues[$s_name]);
        } else {
            $s_value = GetFieldValue($s_name, $aSubstituteValues);
            if ($b_html) {
                $s_value = htmlspecialchars($s_value);
            }
        }
        if ($b_html && $b_insert_br) {
            //
            // Insert HTML line breaks before newlines.
            //
            $s_value = nl2br($s_value);
        }
    } elseif (isset($SPECIAL_VALUES[$s_name])) {
        $s_value = $b_html ? htmlspecialchars((string) $SPECIAL_VALUES[$s_name]) : (string) $SPECIAL_VALUES[$s_name];
    } elseif (isset($s_repl)) {
        //
        // If a replacement value has been specified use it, and
        // don't call htmlspecialchars.  This allows the use
        // of HTML tags in a replacement string.
        //
        $s_value = $s_repl;
    } else {
        $aSubstituteErrors[] = GetMessage(MSG_FLD_NOTFOUND, array("FIELD" => $s_name));
    }
    return $s_value;
}