index.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. require_once 'vendor/paragonie/sodium_compat/autoload.php';
  3. require_once 'vendor/mustangostang/spyc/Spyc.php';
  4. require_once 'vendor/autoload.php';
  5. use MessagePack\MessagePack;
  6. function generateId() {
  7. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  8. $charactersLength = strlen($characters);
  9. $randomString = '';
  10. for ($i = 0; $i < 5; $i++) {
  11. $randomString .= $characters[rand(0, $charactersLength - 1)];
  12. }
  13. return $randomString;
  14. }
  15. $p = sodium_hex2bin('544bb8c5a82c90747b09c1d584166978deaf0789ed1a9bb4487b1d8e8e2ce7e6');
  16. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  17. $req = file_get_contents('php://input');
  18. try{
  19. $value = MessagePack::unpack($req);
  20. } catch(Exception $e) {
  21. http_response_code(400);
  22. die();
  23. }
  24. $sig = $value['signature'];
  25. $msg = $value['url'];
  26. $v = sodium_crypto_sign_verify_detached($sig, $msg, $p);
  27. if ($v == True) {
  28. if (!file_exists('ids.yml'))
  29. $ids = [];
  30. else
  31. $ids = Spyc::YAMLLoad('ids.yml');
  32. $present = array_search($msg, $ids);
  33. if ($present !== false) {
  34. echo("https://apiote.tk/s/$present");
  35. die();
  36. }
  37. do {
  38. $id = generateId();
  39. } while (array_key_exists($id, $ids));
  40. $ids[$id] = $msg;
  41. $idsContent = Spyc::YAMLDump($ids);
  42. file_put_contents('ids.yml', $idsContent);
  43. http_response_code(200);
  44. echo("https://apiote.tk/s/$id");
  45. } else {
  46. http_response_code(403);
  47. die();
  48. }
  49. } else {
  50. $actual_link = "$_SERVER[REQUEST_URI]";
  51. $a = explode('/', $actual_link);
  52. $id = end($a);
  53. $ids = Spyc::YAMLLoad('ids.yml');
  54. $url = $ids[$id];
  55. if (strpos($url, 'http') !== 0) {
  56. $url = "http://$url";
  57. }
  58. header("Location: $url");
  59. }
  60. ?>