Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

status.php 3.2KB

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