Exemple #1
0
function get_rqvar(&$env, $name, $type = false)
{
    if (!isset($env[$name])) {
        set_status_and_exit(400, "{$name} is a required variable");
    }
    if ($type !== false) {
        if ($type != "array" && is_array($env[$name])) {
            set_status_and_exit(400, "{$name} is formatted improperly.");
        }
        if ($type == "array" && !is_array($env[$name])) {
            return array($env[$name]);
        }
        if ($type == "integer") {
            return intval($env[$name]);
        }
        if ($type == "float") {
            return floatval($env[$name]);
        }
        if ($type == "time") {
            return parseTime($env[$name]);
        }
    }
    return $env[$name];
}
        }
    }
    return $image;
}
$height = get_rqvar($_GET, "h", "integer");
$width = get_rqvar($_GET, "w", "integer");
$data = get_rqvar($_GET, "data", "string");
if ($height < 1 || $width < 1) {
    set_status_and_exit(400, "height and width must be positive");
}
$allowed_img_types = array("png", "jpg");
$imgtype = "png";
if (isset($_GET['img'])) {
    $imgtype = strtolower($_GET['img']);
    if (!in_array($imgtype, $allowed_img_types)) {
        set_status_and_exit(400, "unsupported image format: {$imgtype}");
    }
}
$parts = explode("/", $data);
if (isset($_GET['N'])) {
    $upper = $_GET['N'];
} else {
    $upper = -1;
}
if (count($parts) > 1) {
    $idents = explode(";", $parts[1]);
    foreach ($idents as &$ids) {
        $ids = explode(",", $ids);
        for ($i = 0; $i < count($ids); $i++) {
            $ids[$i] = intval($ids[$i]);
            if ($ids[$i] > $upper) {
// query
//$csv = isset($_GET['csv']) && $_GET['csv'] == 1; // comma-separated values?
$q = trim($q);
if (strncmp($q, '@', 1) === 0) {
    $q = trim(substr($q, 1));
    if (strlen($q) == 0) {
        echo "{}";
        return;
    }
    $cond = "title LIKE '%{$q}%'";
} else {
    $cond = "course LIKE '{$q}%'";
}
$query = <<<END
\tSELECT\tDISTINCT c.subject, CONCAT(c.number, c.suffix) AS name, c.title
\tFROM\tN_COURSE c, NX_COURSE x
\tWHERE   c.crs_id = x.crs_id AND x.{$cond}
\tLIMIT\t0, 20
END;
$res = $conn->query($query);
if (!$res) {
    set_status_and_exit(400, "MySQL Error: " . $conn->error);
}
$data = array();
$q = strtoupper($q);
while ($row = $res->fetch_row()) {
    $data[] = array("title" => "{$row['2']}", "value" => "{$row['0']}{$row['1']}", "path" => "{$row['0']}/{$row['1']}");
}
ob_start("ob_gzhandler");
header("Content-Type: application/json");
echo json_encode(array("seq" => $seq, "data" => &$data));