Esempio n. 1
0
function importparams()
{
    global $lang;
    $i = 0;
    $errors = array();
    $valid = array();
    for ($i = 0; $i < func_num_args(); ++$i) {
        $pp = func_get_arg($i);
        if (!is_array($pp) || count($pp) < 2 || count($pp) > 4) {
            err("each SPEC must be an array of 2, 3 or 4 elements");
        }
        $allow_changes = false;
        $name = $pp[0];
        if (is_array($name)) {
            $allow_changes = $name[1];
            $name = $name[0];
        }
        if (!is_string($name)) {
            err("PARAMETER should be a string");
        }
        /* Obtain parameter value. */
        if (array_key_exists($name, $_POST)) {
            $val = $_POST[$name];
            if (!is_array($val)) {
                $val = trim($val);
            }
        } elseif (array_key_exists($name, $_GET)) {
            $val = $_GET[$name];
            if (!is_array($val)) {
                $val = trim($val);
            }
        } else {
            $val = null;
        }
        if (!is_null($val) && $allow_changes && $lang == 'eo') {
            $val = input_esperanto($val);
        }
        $check = $pp[1];
        $error = null;
        $have_default = false;
        if (is_callable($check)) {
            if (count($pp) == 4) {
                err("If CHECK is a function it should only be followed by an optional DEFAULT");
            } else {
                if (count($pp) == 3) {
                    $default = $pp[2];
                    $have_default = true;
                }
            }
        } else {
            if (is_string($check)) {
                if (preg_match($check, '') === false) {
                    err("If CHECK is a string, it must be a valid PCRE regular expression, not '{$check}'");
                } else {
                    if (count($pp) < 3 || !is_string($pp[2])) {
                        err("If CHECK is a regular expression, it must be followed by an ERROR string");
                    }
                }
                if (count($pp) == 4) {
                    $default = $pp[3];
                    $have_default = true;
                }
            } else {
                err("CHECK should be callable or a string");
            }
        }
        if (is_null($val)) {
            if ($have_default) {
                $val = $default;
            } else {
                $error = "Missing parameter '{$name}'";
            }
        } else {
            if (is_callable($check)) {
                $error = $check($val);
            } else {
                if (is_string($check) && 0 == preg_match($check, $val)) {
                    $error = $pp[2];
                }
            }
        }
        eval("global \$q_{$name};");
        eval("global \$q_h_{$name};");
        if (!is_null($error)) {
            $errors[$name] = $error;
        } else {
            eval("\$q_{$name} = \$val;");
            eval("\$q_h_{$name} = htmlspecialchars(\$val);");
        }
        eval("global \$q_unchecked_{$name};");
        eval("global \$q_unchecked_h_{$name};");
        if (is_null($val)) {
            $val = '';
        }
        eval("\$q_unchecked_{$name} = \$val;");
        eval("\$q_unchecked_h_{$name} = htmlspecialchars(\$val);");
    }
    if (count($errors) > 0) {
        return $errors;
    } else {
        return null;
    }
}
Esempio n. 2
0
function get_http_var($name, $default = '')
{
    global $lang;
    if (is_bool($default)) {
        $allow_changes = true;
        $default = '';
    } else {
        $allow_changes = false;
    }
    if (array_key_exists($name, $_GET)) {
        $var = $_GET[$name];
        if (!is_array($var)) {
            $var = trim($var);
        }
    } elseif (array_key_exists($name, $_POST)) {
        $var = $_POST[$name];
        if (!is_array($var)) {
            $var = trim($var);
        }
    } else {
        $var = $default;
    }
    if ($allow_changes && $lang == 'eo') {
        $var = input_esperanto($var);
    }
    $var = str_replace("\r", '', $var);
    return $var;
}