AccountController.php 12 KB

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