1
0

Webfinger.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. class WebFinger implements Rule
  5. {
  6. /**
  7. * Determine if the validation rule passes.
  8. *
  9. * @param string $attribute
  10. * @param mixed $value
  11. * @return bool
  12. */
  13. public function passes($attribute, $value)
  14. {
  15. if (! is_string($value)) {
  16. return false;
  17. }
  18. $mention = $value;
  19. if (str_starts_with($mention, '@')) {
  20. $mention = substr($mention, 1);
  21. }
  22. $parts = explode('@', $mention);
  23. if (count($parts) !== 2) {
  24. return false;
  25. }
  26. [$username, $domain] = $parts;
  27. if (empty($username) ||
  28. ! preg_match('/^[a-zA-Z0-9_.-]+$/', $username) ||
  29. strlen($username) >= 80) {
  30. return false;
  31. }
  32. if (empty($domain) ||
  33. ! str_contains($domain, '.') ||
  34. ! preg_match('/^[a-zA-Z0-9.-]+$/', $domain) ||
  35. strlen($domain) >= 255) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. /**
  41. * Get the validation error message.
  42. *
  43. * @return string
  44. */
  45. public function message()
  46. {
  47. return 'The :attribute must be a valid WebFinger address (username@domain.tld or @username@domain.tld)';
  48. }
  49. }