Example #1
0
 public static function sendExport($id)
 {
     $array = self::getExport($id);
     $json = $array['json'];
     header('Content-Description: File Transfer');
     header('Content-Type: application/json');
     header('Content-Disposition: attachment; filename=' . filterValue($array['name']) . '.json');
     header('Content-Transfer-Encoding: binary');
     header('Content-Length: ' . strlen($json));
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Expires: 0');
     header('Pragma: public');
     echo $json;
     exit;
 }
function checkVar($target, $untrusted_value, $awaited_type, $min, $max, $default_value, $label, $array_return, $die_on_fail)
{
    $value_accepted = true;
    $error = "";
    // 1. filter value according to target (web page or database)
    // converts to correct charset, removes unwanted values, encodes special chars
    // does nothing if not $target = ""
    $untrusted_value = filterValue($target, $untrusted_value);
    // 2. checks var content against awaited type
    if ($awaited_type != "") {
        $value_accepted = validateType($target, $untrusted_value, $awaited_type);
        if ($value_accepted == 0) {
            $error .= "bad type, " . $awaited_type . " awaited.";
        }
    } else {
        // sets var type if not specified, for next check against bounds
        if (is_numeric($untrusted_value)) {
            $awaited_type = "float";
        } else {
            $awaited_type = "string";
        }
    }
    // 3. checks var content against bounds
    if ($value_accepted) {
        // numeric : checks var content against values bounds
        if ($awaited_type == "int" || $awaited_type == "float" || $awaited_type == "hex") {
            echo $awaited_type . "<br>";
            $value_accepted = validateValue($untrusted_value, $min, $max);
            if (!$value_accepted) {
                $error .= "bad value, " . $min . " to " . $max . " expected.";
            }
        }
        // string : checks var content against length bounds
        if ($awaited_type == "string" || $awaited_type == "date" || $awaited_type == "url" || $awaited_type == "email") {
            $value_accepted = validateLength($untrusted_value, $min, $max);
            if (!$value_accepted) {
                $error .= "bad length, " . $min . " to " . $max . " chars expected.";
            }
        }
    }
    if ($value_accepted) {
        switch ($array_return) {
            case 0:
                // returns a single value without feedback
                return $untrusted_value;
                break;
            case 1:
                // returns an array with filtered value or default value with error feedback if validation fails (useful for form validation)
                return array("ok" => true, "value" => $untrusted_value, "error" => "");
        }
    } else {
        if ($die_on_fail) {
            exit("Fatal error :: bad var value detected");
            if ($debug_mode == "on") {
                echo "<br>'" . $label . "' " . $error;
            }
        }
        switch ($array_return) {
            case 0:
                // returns a single value without feedback
                return $default_value;
                break;
            case 1:
                // returns an array with filtered value or default value with error feedback if validation fails (useful for form validation)
                return array("ok" => false, "value" => $default_value, "error" => "'" . $label . "' " . $error);
        }
    }
}