Bearcap.php 936 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Util\Lexer;
  3. use Illuminate\Support\Str;
  4. use App\Util\ActivityPub\Helpers;
  5. class Bearcap
  6. {
  7. public static function encode($url, $token)
  8. {
  9. return "bear:?t={$token}&u={$url}";
  10. }
  11. public static function decode($str)
  12. {
  13. if(!Str::startsWith($str, 'bear:')) {
  14. return false;
  15. }
  16. $query = parse_url($str, PHP_URL_QUERY);
  17. if(!$query) {
  18. return false;
  19. }
  20. $res = [];
  21. $parts = Str::of($str)->substr(6)->explode('&')->toArray();
  22. foreach($parts as $part) {
  23. if(Str::startsWith($part, 't=')) {
  24. $res['token'] = substr($part, 2);
  25. }
  26. if(Str::startsWith($part, 'u=')) {
  27. $res['url'] = substr($part, 2);
  28. }
  29. }
  30. if( !isset($res['token']) ||
  31. !isset($res['url'])
  32. ) {
  33. return false;
  34. }
  35. $url = $res['url'];
  36. if(mb_substr($url, 0, 8) !== 'https://') {
  37. return false;
  38. }
  39. $valid = filter_var($url, FILTER_VALIDATE_URL);
  40. if(!$valid) {
  41. return false;
  42. }
  43. return $res;
  44. }
  45. }