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 GetDerivedValue($a_form_data, $s_word, &$a_errors)
{
    $s_value = "";
    //
    // a field name or a value specification
    // value specifications have the following format:
    //      %spec%
    //
    if (substr($s_word, 0, 1) == '%') {
        if (substr($s_word, -1, 1) != '%') {
            SendAlert(GetMessage(MSG_INV_VALUE_SPEC, array("SPEC" => $s_word)));
            $s_value = $s_word;
        } else {
            $s_spec = substr($s_word, 1, -1);
            $s_value = ValueSpec($s_spec, $a_form_data, $a_errors);
        }
    } else {
        $s_fld_name = $s_word;
        //
        // try form data first, then the environment/server data
        //
        if (IsFieldSet($s_fld_name, $a_form_data)) {
            $s_value = GetFieldValue($s_fld_name, $a_form_data);
        } elseif (($s_value = GetEnvValue($s_fld_name)) === false) {
            $s_value = "";
        }
        $s_value = trim($s_value);
    }
    return $s_value;
}
Esempio 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;
}