AccountController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 app\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 (!signed_in(request())) {
  49. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  50. CORSHelper();
  51. return api_json([
  52. "id" => $id,
  53. "password" => "password1234",
  54. "username" => $usernames[rand(0, count($usernames) - 1)],
  55. "verified" => rand(1, 10) > 8,
  56. "bio" => "SIGN IN to see this EPIC content!!!",
  57. "followers" => rand(5, 38203),
  58. "following" => rand(5, 6243),
  59. "myself" => false,
  60. "links" => [],
  61. "socialCredit" => rand(-20223, 20223)
  62. ]);
  63. }
  64. if ($id === "myself") $id = get_token_id(request());
  65. Logger::Debug($id);
  66. Logger::Debug("Getting account id ($id).");
  67. $account = new Account(id: $id);
  68. $account->Load();
  69. $followers = count(new Follow(followee_id: $id)->LoadMany());
  70. $following = count(new Follow(follower_id: $id)->LoadMany());
  71. try {
  72. $links = new Link(account_id: $id)->LoadMany();
  73. } catch (Exception $e) {
  74. $links = [];
  75. }
  76. CORSHelper();
  77. return api_json([
  78. "id" => $id,
  79. "username" => $account->username,
  80. "verified" => $account->verified,
  81. "bio" => $account->bio,
  82. "followers" => $followers,
  83. "following" => $following,
  84. "myself" => $id == get_token_id(request()),
  85. "links" => $links,
  86. "socialCredit" => $account->social_credit
  87. ]);
  88. }
  89. public static function getVideos($id): string
  90. {
  91. if(!signed_in(request())) {
  92. $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
  93. $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
  94. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  95. $feed = [];
  96. for($i = 0; $i < rand(6, 20); $i++) {
  97. $feed[] = [
  98. "id" => rand(1, 10),
  99. "title" => $titles[rand(0, count($titles) - 1)],
  100. "description" => $descriptions[rand(0, count($descriptions) - 1)],
  101. "likes" => rand(20000, 37020),
  102. "dislikes" => rand(2, 12343),
  103. "comments" => rand(2, 1029),
  104. "shares" => rand(2, 200000),
  105. "author" => [
  106. "id" => rand(2, 59),
  107. "verified" => rand(1, 10) > 8,
  108. "username" => $usernames[rand(0, count($usernames) - 1)],
  109. ],
  110. ];
  111. }
  112. CORSHelper();
  113. return api_json($feed);
  114. }
  115. if ($id === "myself") $id = get_token_id(request());
  116. $account = new Account(id: $id);
  117. $video_number = intval(input("video") ?? "0");
  118. $data = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE author_id = :author_id", ["author_id" => $account->id]);
  119. if (empty($data)) throw new VideoNotFoundException(0, 404);
  120. $videos = [];
  121. $valid_video = false;
  122. foreach ($data as $video) {
  123. if($video_number === 0) $valid_video = true;
  124. if($video["id"] === $video_number) $valid_video = true;
  125. if(!$valid_video) continue;
  126. $account = new Account($video["author_id"]);
  127. $account->Load();
  128. $videos[] = ["id" => $video["id"],
  129. "title" => $video["title"],
  130. "description" => $video["description"],
  131. "likes" => $video["likes"],
  132. "dislikes" => $video["dislikes"],
  133. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $video["id"]]),
  134. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $video["id"]]),
  135. "author" => [
  136. "id" => $video["author_id"],
  137. "verified" => $account->verified,
  138. "username" => $account->username,
  139. ],
  140. ];
  141. }
  142. CORSHelper();
  143. return api_json($videos);
  144. }
  145. public static function getPicture($id): string
  146. {
  147. $signed_in = signed_in(request());
  148. if ($id === "myself") $id = get_token_id(request());
  149. if ($signed_in) {
  150. try {
  151. $account = new Account(id: $id);
  152. $account->Load();
  153. $picture_path = APP_ROOT . "/usercontent/pictures/$account->picture_hash.png";
  154. } catch (Exception $e) {
  155. $picture_path = APP_ROOT . "/usercontent/pictures/not_found.png";
  156. }
  157. } else {
  158. // this is hardcoded because i dont care
  159. $picture_path = APP_ROOT . "/usercontent/pictures/premium_" . rand(1, 57) . ".png";
  160. }
  161. $mimeTypes = new MimeTypes();
  162. $picture_contents = file_get_contents($picture_path);
  163. $picture_size = filesize($picture_path);
  164. $mime = $mimeTypes->getMimeType(pathinfo($picture_path, PATHINFO_EXTENSION));
  165. $response = response();
  166. $response->header("Content-Type: $mime");
  167. $response->header("Content-Length: $picture_size");
  168. $response->header("Cache-Control: max-age=3600, public");
  169. return $picture_contents;
  170. }
  171. public static function getAvailablePremiumProfilePictures(): string
  172. {
  173. $pictures = [];
  174. for ($i = 1; $i <= 47; $i++) {
  175. $pictures[] = $i;
  176. }
  177. CORSHelper();
  178. return api_json($pictures);
  179. }
  180. public static function getPremiumProfilePicture($id): string
  181. {
  182. $picture_path = APP_ROOT . "/usercontent/pictures/premium_$id.png";
  183. $mime_types = new MimeTypes();
  184. $picture_contents = file_get_contents($picture_path);
  185. $picture_size = filesize($picture_path);
  186. $mime = $mime_types->getMimeType(pathinfo($picture_path, PATHINFO_EXTENSION));
  187. $response = response();
  188. $response->header("Content-Type: $mime");
  189. $response->header("Content-Length: $picture_size");
  190. $response->header("Cache-Control: max-age=3600, public");
  191. return $picture_contents;
  192. }
  193. public static function updateAccount(): string
  194. {
  195. if (!signed_in(request())) throw new UnauthenticatedException(0, 401);
  196. $id = get_token_id(request());
  197. $picture_hash = input("picture_hash");
  198. $picture = request()->getInputHandler()->file("picture");
  199. $bio = input("bio");
  200. $valid_picture_hash_list = [
  201. "default",
  202. "premium_1",
  203. "premium_2",
  204. "premium_3",
  205. "premium_4",
  206. "premium_5",
  207. "premium_6",
  208. "premium_7",
  209. "premium_8",
  210. "premium_9",
  211. "premium_10",
  212. "premium_11",
  213. "premium_12",
  214. "premium_13",
  215. "premium_14",
  216. "premium_15",
  217. "premium_16",
  218. "premium_17",
  219. "premium_18",
  220. "premium_19",
  221. "premium_20",
  222. "premium_21",
  223. "premium_22",
  224. "premium_23",
  225. "premium_24",
  226. "premium_25",
  227. "premium_26",
  228. "premium_27",
  229. "premium_28",
  230. "premium_29",
  231. "premium_30",
  232. "premium_31",
  233. "premium_32",
  234. "premium_33",
  235. "premium_34",
  236. "premium_35",
  237. "premium_36",
  238. "premium_37",
  239. "premium_38",
  240. "premium_39",
  241. "premium_40",
  242. "premium_41",
  243. "premium_42",
  244. "premium_43",
  245. "premium_44",
  246. "premium_45",
  247. "premium_46",
  248. "premium_47",
  249. "premium_48",
  250. "premium_49",
  251. "premium_50",
  252. "premium_51",
  253. "premium_52",
  254. "premium_53",
  255. "premium_54",
  256. "premium_55",
  257. "premium_56",
  258. "premium_57"
  259. ];
  260. if (empty($picture) && !empty($picture_hash)) {
  261. if (!in_array($picture_hash, $valid_picture_hash_list)) throw new SecurityFaultException("Attempt to path trace on /update endpoint.", 400);
  262. } else if (!empty($picture) && empty($picture_hash)) {
  263. $picture_hash = hash("sha256", $picture);
  264. $picture_path = APP_ROOT . "/usercontent/pictures/$picture_hash.png";
  265. $size = getimagesize($picture);
  266. $crop = min($size[0], $size[1]);
  267. $image_contents = file_get_contents($picture);
  268. $image_string = imagecreatefromstring($image_contents);
  269. $cropped_image = imagecrop($image_string, [
  270. "x" => 0,
  271. "y" => 0,
  272. "width" => $crop,
  273. "height" => $crop
  274. ]);
  275. imagepng($cropped_image, $picture_path); // save image and crop and turn into png (i love php)
  276. } else if (!empty($picture) && !empty($picture_hash)) {
  277. throw new InvalidRequestException(400);
  278. }
  279. $old_account = new Account(id: $id);
  280. $old_account->Load();
  281. $new_account = new Account(
  282. id: $id,
  283. username: $old_account->username,
  284. password: $old_account->password,
  285. picture_hash: $picture_hash ?? $old_account->picture_hash,
  286. verified: $old_account->verified,
  287. bio: $bio ?? $old_account->bio,
  288. );
  289. CORSHelper();
  290. $new_account->Update();
  291. return api_json([
  292. "id" => $new_account->id,
  293. "username" => $new_account->username,
  294. "pictureHash" => in_array($new_account->picture_hash, $valid_picture_hash_list) ? $new_account->picture_hash : null,
  295. "bio" => $new_account->bio,
  296. "verified" => $new_account->verified,
  297. ]);
  298. }
  299. public static function addLink(): string
  300. {
  301. if (!signed_in(request())) throw new UnauthenticatedException(0, 401);
  302. $account_id = get_token_id(request());
  303. $link = input("link");
  304. $link_type = input("linkType");
  305. $link_enum_type = LinkEnum::tryFrom($link_type);
  306. // todo: add enum type filtering
  307. $link = new Link(account_id: $account_id, type: $link_enum_type, url: $link);
  308. $link->Save();
  309. return api_json([
  310. "id" => $link->id,
  311. "url" => $link->url,
  312. "type" => $link->type->value,
  313. "account_id" => $link->account_id
  314. ]);
  315. }
  316. public static function getFollowers($id): string
  317. {
  318. if ($id === "myself") $id = get_token_id(request());
  319. $followers = new Follow(followee_id: $id)->LoadMany();
  320. if (empty($followers)) throw new FollowNotFoundException(0, 404);
  321. for ($i = 0; $i < count($followers); $i++) {
  322. $follower = $followers[$i]["follower_id"];
  323. $account = new Account(id: $follower);
  324. $account->Load();
  325. $followers[$i] = [
  326. "followee_id" => (int)$id,
  327. "follower_id" => $follower,
  328. "username" => $account->username,
  329. "verified" => $account->verified
  330. ];
  331. }
  332. return api_json($followers);
  333. }
  334. public static function getFollowing($id): string
  335. {
  336. if ($id === "myself") $id = get_token_id(request());
  337. $following = new Follow(follower_id: $id)->LoadMany();
  338. if (empty($following)) throw new FollowNotFoundException(0, 404);
  339. for ($i = 0; $i < count($following); $i++) {
  340. $followee = $following[$i]["followee_id"];
  341. $account = new Account(id: $followee);
  342. $account->Load();
  343. $following[$i] = [
  344. "followee_id" => $followee,
  345. "follower_id" => (int)$id,
  346. "username" => $account->username,
  347. "verified" => $account->verified
  348. ];
  349. }
  350. return api_json($following);
  351. }
  352. public static function search(): string {
  353. $signed_in = signed_in(request());
  354. if(!$signed_in) {
  355. $data = [];
  356. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  357. for($i = 0; $i < rand(1, 13); $i++) {
  358. $data[] = [
  359. "id" => rand(2, 10232),
  360. "username" => $usernames[rand(0, count($usernames) - 1)],
  361. "socialCredit" => rand(-2032, 1209312),
  362. "following" => rand(0, 10232),
  363. "followers" => rand(0, 10232),
  364. "bio" => "SIGN IN to see this EPIC content!!",
  365. "verified" => rand(1, 10) > 8,
  366. ];
  367. }
  368. CORSHelper();
  369. return api_json($data);
  370. }
  371. $query = input("query");
  372. $account = new Account(username: $query);
  373. $data = $account->LoadMany();
  374. $accounts = [];
  375. foreach($data as $account) {
  376. $followers = count(new Follow(followee_id: $account["id"])->LoadMany());
  377. $following = count(new Follow(follower_id: $account["id"])->LoadMany());
  378. $accounts[] = [
  379. "id" => $account["id"],
  380. "username" => $account["username"],
  381. "socialCredit" => $account["social_credit"],
  382. "following" => $following,
  383. "followers" => $followers,
  384. "bio" => $account["bio"],
  385. "verified" => $account["verified"] === 1,
  386. ];
  387. }
  388. CORSHelper();
  389. return api_json($accounts);
  390. }
  391. public static function RegisterRoutes(): void
  392. {
  393. SimpleRouter::group([
  394. "prefix" => "/account/",
  395. ], function () {
  396. SimpleRouter::get("/availablePremiumProfilePictures", [AccountController::class, "getAvailablePremiumProfilePictures"]);
  397. SimpleRouter::get("/getPremiumProfilePicture/{id}", [AccountController::class, "getPremiumProfilePicture"]);
  398. SimpleRouter::get("/{id}/get", [AccountController::class, "getAccount"]);
  399. SimpleRouter::get("/{id}/videos", [AccountController::class, "getVideos"]);
  400. SimpleRouter::get("/{id}/picture", [AccountController::class, "getPicture"]);
  401. //SimpleRouter::get("/{id}/followers", [AccountController::class, "getFollowers"]);
  402. //SimpleRouter::get("/{id}/following", [AccountController::class, "getFollowing"]);
  403. SimpleRouter::post("/token", [AccountController::class, "getToken"]);
  404. SimpleRouter::post("/update", [AccountController::class, "updateAccount"]);
  405. SimpleRouter::post("/addLink", [AccountController::class, "addLink"]);
  406. SimpleRouter::post("/search", [AccountController::class, "search"]);
  407. SimpleRouter::options("/update", "CORSHelper");
  408. SimpleRouter::options("/token", "CORSHelper");
  409. SimpleRouter::options("/{id}/get", "CORSHelper");
  410. SimpleRouter::options("/{id}/videos", "CORSHelper");
  411. SimpleRouter::options("/update", "CORSHelper");
  412. SimpleRouter::options("/availablePremiumProfilePictures", "CORSHelper");
  413. SimpleRouter::options("/getPremiumProfilePicture/{id}","CORSHelper");
  414. SimpleRouter::options("/search", "CORSHelper");
  415. });
  416. }
  417. }