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.

status.php 4.3KB

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