|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gravity\Validations\String; |
| 4 | + |
| 5 | +use Gravity\Validations\ValidationStrategy; |
| 6 | +use Gravity\Attributes\{ValidationRule, Param}; |
| 7 | + |
| 8 | +#[ValidationRule( |
| 9 | + name: 'disposableUrlDomain', |
| 10 | + description: 'Validates that a URL is not from a disposable/temporary domain (URL shorteners, free hosting, etc.)', |
| 11 | + category: 'String', |
| 12 | + examples: [ |
| 13 | + '$verifier->field("website")->disposableUrlDomain', |
| 14 | + '$verifier->field("website")->disposableUrlDomain(["bit.ly", "tinyurl.com"])' |
| 15 | + ] |
| 16 | +)] |
| 17 | +class DisposableUrlDomainValidation extends ValidationStrategy |
| 18 | +{ |
| 19 | + /** |
| 20 | + * List of known disposable/temporary URL domain patterns |
| 21 | + * |
| 22 | + * Categories: |
| 23 | + * - URL Shorteners |
| 24 | + * - Temporary file hosting |
| 25 | + * - Free subdomain services |
| 26 | + * - Suspicious free hosting |
| 27 | + * |
| 28 | + * @var array<string> |
| 29 | + */ |
| 30 | + public const DEFAULTS = [ |
| 31 | + 'bit.ly','tinyurl.com','goo.gl','ow.ly','t.co','is.gd','buff.ly','adf.ly','bc.vc','soo.gd','clk.im','s2r.co','shrtco.de', |
| 32 | + 'rb.gy','cutt.ly','short.io','tiny.cc','file.io','transfer.sh','temp.sh','tmpfiles.org','0x0.st','uguu.se','catbox.moe', |
| 33 | + 'litterbox.catbox.moe','pixeldrain.com','gofile.io','anonfiles.com','bayfiles.com','000webhostapp.com','freehosting.com', |
| 34 | + 'freehostia.com','x10hosting.com','byethost.com','5gbfree.com','freewha.com','ngrok.io','ngrok-free.app','localtunnel.me', |
| 35 | + 'serveo.net','localhost.run', |
| 36 | + ]; |
| 37 | + |
| 38 | + public function getName(): string |
| 39 | + { |
| 40 | + return 'disposableUrlDomain'; |
| 41 | + } |
| 42 | + |
| 43 | + protected function handler( |
| 44 | + mixed $value, |
| 45 | + #[Param('Array of disposable domain patterns', example: [])] |
| 46 | + array $disposables = [] |
| 47 | + ): bool { |
| 48 | + if (!is_string($value) || $value === '') { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + $host = parse_url($value, PHP_URL_HOST); |
| 53 | + |
| 54 | + if ($host === null || $host === false || $host === '') { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + $list = !empty($disposables) ? $disposables : self::DEFAULTS; |
| 59 | + |
| 60 | + $host = strtolower($host); |
| 61 | + |
| 62 | + foreach ($list as $disposable) { |
| 63 | + $disposableDomain = strtolower(ltrim($disposable, '.')); |
| 64 | + |
| 65 | + if ($host === $disposableDomain) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + if (str_ends_with($host, '.' . $disposableDomain)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | + } |
| 76 | +} |
0 commit comments