You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. require_once "utils.php";
  3. require_once "misc/config.php";
  4. function check_token()
  5. {
  6. $token = check_table_field("Authorization", "tokens", "token");
  7. if ($token === false)
  8. error(401, "Invalid token");
  9. return $token;
  10. }
  11. function status_confirm($id)
  12. {
  13. check_token();
  14. }
  15. function status_create()
  16. {
  17. $token = check_token();
  18. $status = get_post("status");
  19. if (strlen($status) < 10)
  20. error(422, "Status too short");
  21. $lat = check_float(get_post("latitude", false));
  22. $long = check_float(get_post("longitude", false));
  23. $media = null;
  24. if (isset($_FILES["media"])
  25. && getimagesize($_FILES["media"]["tmp_name"]) !== false)
  26. {
  27. global $media_path;
  28. $media = dechex(microtime());
  29. move_uploaded_file($_FILES["media"]["tmp_name"], $media_path . $media);
  30. }
  31. $u = database_exec("SELECT `id`, `username` FROM users WHERE `id` = ".
  32. "(SELECT `user` FROM tokens WHERE `token` = :token)",
  33. array(":token" => $token))->fetch();
  34. database_exec("INSERT INTO status (`status`, `user`, `longitude`, ".
  35. "`latitude`, `media`) VALUES(:status, :user, :long, :lat, :media)",
  36. array(":status" => $status, ":user" => $u["id"], ":long" => $long,
  37. ":lat" => $lat, ":media" => $media));
  38. $s = database_exec("SELECT * FROM status WHERE id = :id",
  39. array(":id" => database_get()->lastInsertId()))->fetch();
  40. $date = new DateTime($s["date"]);
  41. echo json_encode(array("status" => $s["status"],
  42. "user_id" => intval($u["id"]),
  43. "creation_date" => $date->format(DateTime::ISO8601),
  44. "latitude" => $s["latitude"] === null ? null : floatval($s["latitude"]),
  45. "longitude" => $s["longitude"] === null ? null : floatval($s["longitude"]),
  46. "media_url" => ($s["media"] !== null ) ? $media_url . $s["media"] : null,
  47. "id" => intval($s["id"])
  48. ));
  49. }
  50. function status_feed()
  51. {
  52. check_token();
  53. $limit = check_int(get_get("limit", false), 15);
  54. if ($limit < 1)
  55. $limit = 15;
  56. $page = max((check_int(get_get("page", false), 1) - 1), 0) * $limit;
  57. $s = database_query("SELECT * FROM status ORDER BY `id` DESC ".
  58. "LIMIT $page, $limit");
  59. $data = array();
  60. global $media_url;
  61. while (($u = $s->fetch()) !== false)
  62. {
  63. $a = database_exec("SELECT `id`, `username` FROM users WHERE `id` = :id",
  64. array(":id" => $u["user"]))->fetch();
  65. $date = new DateTime($u["date"]);
  66. $data[] = array("id" => intval($u["id"]),
  67. "status" => $u["status"],
  68. "creation_date" => $date->format(DateTime::ISO8601),
  69. "latitude" => $u["latitude"] === null ? null : floatval($u["latitude"]),
  70. "longitude" => $u["longitude"] === null ? null : floatval($u["longitude"]),
  71. "author_id" => intval($a["id"]),
  72. "author_username" => $a["username"],
  73. "nb_confirm_up" => intval($u["up"]),
  74. "nb_confirm_down" => intval($u["down"]),
  75. "media_url" => ($u["media"] !== null ) ? $media_url . $u["media"] : null);
  76. }
  77. echo json_encode($data);
  78. }
  79. ?>