AccountController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace app\Controllers;
  3. use app\Exceptions\FollowNotFoundException;
  4. use app\Exceptions\InvalidRequestException;
  5. use app\Exceptions\UnauthenticatedException;
  6. use app\Exceptions\VideoNotFoundException;
  7. use app\Hajeebtok;
  8. use app\Interfaces\IRouteController;
  9. use app\Logger;
  10. use app\Types\DatabaseObjects\Account;
  11. use app\Types\DatabaseObjects\Follow;
  12. use app\Types\DatabaseObjects\Link;
  13. use app\Types\DatabaseObjects\Session;
  14. use app\Types\LinkEnum;
  15. use Hajeebtok\Types\Exceptions\SecurityFaultException;
  16. use Mimey\MimeTypes;
  17. use Pecee\SimpleRouter\SimpleRouter;
  18. class AccountController implements IRouteController
  19. {
  20. public static function getToken(): string
  21. {
  22. $username = input("username");
  23. $password = password_hash(input("password"), PASSWORD_DEFAULT);
  24. $account = new Account(
  25. username: $username,
  26. password: $password,
  27. picture_hash: "default",
  28. verified: false
  29. );
  30. if ($account->Exists()) { // Account already exists
  31. $account->Load();
  32. if (password_verify($password, $account->password)) throw new UnauthenticatedException($account->id, 401);
  33. $session = new Session(account_id: $account->id);
  34. } else { // Create a new account
  35. $session = new Session(account_id: $account->Save());
  36. }
  37. CORSHelper();
  38. $token = $session->Save();
  39. $session->Load();
  40. return api_json([
  41. "token" => $token,
  42. "auth_date" => $session->date_authenticated
  43. ]);
  44. }
  45. public static function getAccount($id): string
  46. {
  47. if($id === "myself") $id = get_token_id(request());
  48. Logger::Debug("Getting account id ($id).");
  49. $account = new Account(id: $id);
  50. $account->Load();
  51. $followers = count(new Follow(followee_id: $id)->LoadMany());
  52. $following = count(new Follow(follower_id: $id)->LoadMany());
  53. $links = new Link(account_id: $id)->LoadMany();
  54. return api_json([
  55. "id" => $id,
  56. "username" => $account->username,
  57. "verified" => $account->verified,
  58. "bio" => $account->bio,
  59. "pictureHash" => $account->picture_hash,
  60. "followers" => $followers,
  61. "following" => $following,
  62. "links" => $links
  63. ]);
  64. }
  65. public static function search(): string
  66. {
  67. $query = input("query");
  68. $account = new Account(username: $query);
  69. $accounts = $account->LoadMany();
  70. return api_json($accounts);
  71. }
  72. public static function getVideos($id): string
  73. {
  74. if($id === "myself") $id = get_token_id(request());
  75. $account = new Account(id: $id);
  76. $data = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE author_id = :author_id", ["author_id" => $account->id]);
  77. if (empty($data)) throw new VideoNotFoundException(0, 404);
  78. return api_json($data);
  79. }
  80. public static function getPicture($id): string
  81. {
  82. $signed_in = signed_in(request());
  83. if($id === "myself") $id = get_token_id(request());
  84. if($signed_in) {
  85. $account = new Account(id: $id);
  86. $account->Load();
  87. $picture_path = APP_ROOT . "/usercontent/pictures/$account->picture_hash.png";
  88. } else {
  89. // this is hardcoded because i dont care
  90. $picture_path = APP_ROOT . "/usercontent/pictures/premium_" . rand(1, 57) . ".png";
  91. }
  92. $mimeTypes = new MimeTypes();
  93. $picture_contents = file_get_contents($picture_path);
  94. $picture_size = filesize($picture_path);
  95. $mime = $mimeTypes->getMimeType(pathinfo($picture_path, PATHINFO_EXTENSION));
  96. $response = response();
  97. $response->header("Content-Type: $mime");
  98. $response->header("Content-Length: $picture_size");
  99. $response->header("Cache-Control: max-age=3600, public");
  100. return $picture_contents;
  101. }
  102. public static function getAvailablePremiumProfilePictures(): string
  103. {
  104. $pictures = [];
  105. for ($i = 1; $i <= 57; $i++) {
  106. $pictures[] = $i;
  107. }
  108. return api_json($pictures);
  109. }
  110. public static function getPremiumProfilePicture($id): string
  111. {
  112. $picture_path = APP_ROOT . "/usercontent/pictures/premium_$id.png";
  113. $mime_types = new MimeTypes();
  114. $picture_contents = file_get_contents($picture_path);
  115. $picture_size = filesize($picture_path);
  116. $mime = $mime_types->getMimeType(pathinfo($picture_path, PATHINFO_EXTENSION));
  117. $response = response();
  118. $response->header("Content-Type: $mime");
  119. $response->header("Content-Length: $picture_size");
  120. $response->header("Cache-Control: max-age=3600, public");
  121. return $picture_contents;
  122. }
  123. public static function updateAccount(): string
  124. {
  125. if(!signed_in(request())) throw new UnauthenticatedException(0, 401);
  126. $id = get_token_id(request());
  127. $picture_hash = input("picture_hash");
  128. $picture = request()->getInputHandler()->file("picture");
  129. $bio = input("bio");
  130. if(empty($picture) && !empty($picture_hash)) {
  131. $valid_picture_hash_list = [
  132. "default",
  133. "premium_1",
  134. "premium_2",
  135. "premium_3",
  136. "premium_4",
  137. "premium_5",
  138. "premium_6",
  139. "premium_7",
  140. "premium_8",
  141. "premium_9",
  142. "premium_10",
  143. "premium_11",
  144. "premium_12",
  145. "premium_13",
  146. "premium_14",
  147. "premium_15",
  148. "premium_16",
  149. "premium_17",
  150. "premium_18",
  151. "premium_19",
  152. "premium_20",
  153. "premium_21",
  154. "premium_22",
  155. "premium_23",
  156. "premium_24",
  157. "premium_25",
  158. "premium_26",
  159. "premium_27",
  160. "premium_28",
  161. "premium_29",
  162. "premium_30",
  163. "premium_31",
  164. "premium_32",
  165. "premium_33",
  166. "premium_34",
  167. "premium_35",
  168. "premium_36",
  169. "premium_37",
  170. "premium_38",
  171. "premium_39",
  172. "premium_40",
  173. "premium_41",
  174. "premium_42",
  175. "premium_43",
  176. "premium_44",
  177. "premium_45",
  178. "premium_46",
  179. "premium_47",
  180. "premium_48",
  181. "premium_49",
  182. "premium_50",
  183. "premium_51",
  184. "premium_52",
  185. "premium_53",
  186. "premium_54",
  187. "premium_55",
  188. "premium_56",
  189. "premium_57"
  190. ];
  191. if(!in_array($picture_hash, $valid_picture_hash_list)) throw new SecurityFaultException("Attempt to path trace on /update endpoint.",400);
  192. } else if (!empty($picture) && empty($picture_hash)) {
  193. $picture_hash = hash("sha256", $picture);
  194. $picture_path = APP_ROOT . "/usercontent/pictures/$picture_hash.png";
  195. $size = getimagesize($picture);
  196. $crop = min($size[0], $size[1]);
  197. $image_contents = file_get_contents($picture);
  198. $image_string = imagecreatefromstring($image_contents);
  199. $cropped_image = imagecrop($image_string, [
  200. "x" => 0,
  201. "y" => 0,
  202. "width" => $crop,
  203. "height" => $crop
  204. ]);
  205. imagepng($cropped_image, $picture_path); // save image and crop and turn into png (i love php)
  206. } else if(!empty($picture) && !empty($picture_hash)) {
  207. throw new InvalidRequestException(400);
  208. }
  209. $old_account = new Account(id: $id);
  210. $old_account->Load();
  211. $new_account = new Account(
  212. id: $id,
  213. username: $old_account->username,
  214. password: $old_account->password,
  215. picture_hash: $picture_hash ?? $old_account->picture_hash,
  216. verified: $old_account->verified,
  217. bio: $bio ?? $old_account->bio,
  218. );
  219. CORSHelper();
  220. $new_account->Update();
  221. return api_json([
  222. "id" => $new_account->id,
  223. "username" => $new_account->username,
  224. "pictureHash" => $new_account->picture_hash,
  225. "bio" => $new_account->bio,
  226. "verified" => $new_account->verified,
  227. ]);
  228. }
  229. public static function addLink(): string
  230. {
  231. if(!signed_in(request())) throw new UnauthenticatedException(0, 401);
  232. $account_id = get_token_id(request());
  233. $link = input("link");
  234. $link_type = input("linkType");
  235. $link_enum_type = LinkEnum::tryFrom($link_type);
  236. // todo: add enum type filtering
  237. $link = new Link(account_id: $account_id, type: $link_enum_type, url: $link);
  238. $link->Save();
  239. return api_json([
  240. "id" => $link->id,
  241. "url" => $link->url,
  242. "type" => $link->type->value,
  243. "account_id" => $link->account_id
  244. ]);
  245. }
  246. public static function getFollowers($id): string {
  247. if($id === "myself") $id = get_token_id(request());
  248. $followers = new Follow(followee_id: $id)->LoadMany();
  249. if(empty($followers)) throw new FollowNotFoundException(0, 404);
  250. for ($i = 0; $i < count($followers); $i++) {
  251. $follower = $followers[$i]["follower_id"];
  252. $account = new Account(id: $follower);
  253. $account->Load();
  254. $followers[$i] = [
  255. "followee_id" => (int)$id,
  256. "follower_id" => $follower,
  257. "username" => $account->username,
  258. "pictureHash" => $account->picture_hash,
  259. "verified" => $account->verified
  260. ];
  261. }
  262. return api_json($followers);
  263. }
  264. public static function getFollowing($id): string {
  265. if($id === "myself") $id = get_token_id(request());
  266. $following = new Follow(follower_id: $id)->LoadMany();
  267. if(empty($following)) throw new FollowNotFoundException(0, 404);
  268. for ($i = 0; $i < count($following); $i++) {
  269. $followee = $following[$i]["followee_id"];
  270. $account = new Account(id: $followee);
  271. $account->Load();
  272. $following[$i] = [
  273. "followee_id" => $followee,
  274. "follower_id" => (int)$id,
  275. "username" => $account->username,
  276. "pictureHash" => $account->picture_hash,
  277. "verified" => $account->verified
  278. ];
  279. }
  280. return api_json($following);
  281. }
  282. public static function RegisterRoutes(): void
  283. {
  284. SimpleRouter::group([
  285. "prefix" => "/account/",
  286. ], function () {
  287. SimpleRouter::get("/availablePremiumProfilePictures", [AccountController::class, "getAvailablePremiumProfilePictures"]);
  288. SimpleRouter::get("/getPremiumProfilePicture/{id}", [AccountController::class, "getPremiumProfilePicture"]);
  289. SimpleRouter::get("/{id}/get", [AccountController::class, "getAccount"]);
  290. SimpleRouter::get("/{id}/videos", [AccountController::class, "getVideos"]);
  291. SimpleRouter::get("/{id}/picture", [AccountController::class, "getPicture"]);
  292. SimpleRouter::get("/{id}/followers", [AccountController::class, "getFollowers"]);
  293. SimpleRouter::get("/{id}/following", [AccountController::class, "getFollowing"]);
  294. SimpleRouter::post("/token", [AccountController::class, "getToken"]);
  295. SimpleRouter::post("/search", [AccountController::class, "search"]);
  296. SimpleRouter::post("/update", [AccountController::class, "updateAccount"]);
  297. SimpleRouter::post("/addLink", [AccountController::class, "addLink"]);
  298. SimpleRouter::options("/update", "CORSHelper");
  299. SimpleRouter::options("/token", "CORSHelper");
  300. });
  301. }
  302. }