123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- require_once "utils.php";
- require_once "misc/config.php";
-
- function check_token()
- {
- $token = check_table_field("Authorization", "tokens", "token");
- if ($token === false)
- error(401, "Invalid token");
- return $token;
- }
-
- function status_confirm($id)
- {
- check_token();
- }
-
- function status_create()
- {
- $token = check_token();
- $status = get_post("status");
- if (strlen($status) < 10)
- error(422, "Status too short");
-
- $lat = check_float(get_post("latitude", false));
- $long = check_float(get_post("longitude", false));
- $media = null;
- if (isset($_FILES["media"])
- && getimagesize($_FILES["media"]["tmp_name"]) !== false)
- {
- global $media_path;
- $media = dechex(microtime(true)) . dechex(rand());
- move_uploaded_file($_FILES["media"]["tmp_name"], $media_path . $media);
- }
-
- $u = database_exec("SELECT `id`, `username` FROM users WHERE `id` = ".
- "(SELECT `user` FROM tokens WHERE `token` = :token)",
- array(":token" => $token))->fetch();
-
- database_exec("INSERT INTO status (`status`, `user`, `longitude`, ".
- "`latitude`, `media`) VALUES(:status, :user, :long, :lat, :media)",
- array(":status" => $status, ":user" => $u["id"], ":long" => $long,
- ":lat" => $lat, ":media" => $media));
-
- $s = database_exec("SELECT * FROM status WHERE id = :id",
- array(":id" => database_get()->lastInsertId()))->fetch();
-
- $date = new DateTime($s["date"]);
-
- global $media_url;
- echo json_encode(array("status" => $s["status"],
- "user_id" => intval($u["id"]),
- "creation_date" => $date->format(DateTime::ISO8601),
- "latitude" => $s["latitude"] === null ? null : floatval($s["latitude"]),
- "longitude" => $s["longitude"] === null ? null : floatval($s["longitude"]),
- "media_url" => ($s["media"] !== null ) ? $media_url . $s["media"] : null,
- "id" => intval($s["id"])
- ));
- }
-
- function status_feed()
- {
- check_token();
- $limit = check_int(get_get("limit", false), 15);
- if ($limit < 1)
- $limit = 15;
- $page = max((check_int(get_get("page", false), 1) - 1), 0) * $limit;
- $s = database_query("SELECT * FROM status ORDER BY `id` DESC ".
- "LIMIT $page, $limit");
- $data = array();
- global $media_url;
- while (($u = $s->fetch()) !== false)
- {
- $a = database_exec("SELECT `id`, `username` FROM users WHERE `id` = :id",
- array(":id" => $u["user"]))->fetch();
- $date = new DateTime($u["date"]);
- $data[] = array("id" => intval($u["id"]),
- "status" => $u["status"],
- "creation_date" => $date->format(DateTime::ISO8601),
- "latitude" => $u["latitude"] === null ? null : floatval($u["latitude"]),
- "longitude" => $u["longitude"] === null ? null : floatval($u["longitude"]),
- "author_id" => intval($a["id"]),
- "author_username" => $a["username"],
- "nb_confirm_up" => intval($u["up"]),
- "nb_confirm_down" => intval($u["down"]),
- "media_url" => ($u["media"] !== null ) ? $media_url . $u["media"] : null);
- }
- echo json_encode($data);
- }
- ?>
|