|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types=1 ); |
| 4 | + |
| 5 | +final class KnownAgentsRobotsBuilder { |
| 6 | + private const API_URL = 'https://api.knownagents.com/robots-txts'; |
| 7 | + |
| 8 | + /** |
| 9 | + * Policy presets mapped to Known Agents agent_types. |
| 10 | + * |
| 11 | + * @var array<string, array<int, string>> |
| 12 | + */ |
| 13 | + private const PRESETS = [ |
| 14 | + 'training-only' => [ |
| 15 | + 'AI Data Scraper', |
| 16 | + ], |
| 17 | + 'training-plus-undocumented' => [ |
| 18 | + 'AI Data Scraper', |
| 19 | + 'Undocumented AI Agent', |
| 20 | + ], |
| 21 | + 'aggressive' => [ |
| 22 | + 'AI Data Scraper', |
| 23 | + 'Undocumented AI Agent', |
| 24 | + 'Scraper', |
| 25 | + 'Intelligence Gatherer', |
| 26 | + ], |
| 27 | + ]; |
| 28 | + |
| 29 | + /** |
| 30 | + * Fetch robots.txt text from Known Agents. |
| 31 | + * |
| 32 | + * @param string $token Bearer token for the project. |
| 33 | + * @param array<int, string> $agent_types Known Agents categories to request. |
| 34 | + * @param string $disallow Path to disallow. Defaults to '/'. |
| 35 | + * @return string |
| 36 | + */ |
| 37 | + public static function fetch_robots_txt( string $token, array $agent_types, string $disallow = '/' ): string { |
| 38 | + if ( '' === trim( $token ) ) { |
| 39 | + throw new InvalidArgumentException( 'Known Agents token is required.' ); |
| 40 | + } |
| 41 | + |
| 42 | + if ( empty( $agent_types ) ) { |
| 43 | + throw new InvalidArgumentException( 'At least one agent type is required.' ); |
| 44 | + } |
| 45 | + |
| 46 | + $payload = json_encode( |
| 47 | + [ |
| 48 | + 'agent_types' => array_values( $agent_types ), |
| 49 | + 'disallow' => $disallow, |
| 50 | + ], |
| 51 | + JSON_UNESCAPED_SLASHES |
| 52 | + ); |
| 53 | + |
| 54 | + if ( false === $payload ) { |
| 55 | + throw new RuntimeException( 'Failed to encode Known Agents request payload.' ); |
| 56 | + } |
| 57 | + |
| 58 | + $ch = curl_init( self::API_URL ); |
| 59 | + |
| 60 | + if ( false === $ch ) { |
| 61 | + throw new RuntimeException( 'Failed to initialize cURL.' ); |
| 62 | + } |
| 63 | + |
| 64 | + curl_setopt_array( |
| 65 | + $ch, |
| 66 | + [ |
| 67 | + CURLOPT_POST => true, |
| 68 | + CURLOPT_RETURNTRANSFER => true, |
| 69 | + CURLOPT_HTTPHEADER => [ |
| 70 | + 'Authorization: Bearer ' . $token, |
| 71 | + 'Content-Type: application/json', |
| 72 | + ], |
| 73 | + CURLOPT_POSTFIELDS => $payload, |
| 74 | + CURLOPT_TIMEOUT => 30, |
| 75 | + CURLOPT_CONNECTTIMEOUT => 10, |
| 76 | + ] |
| 77 | + ); |
| 78 | + |
| 79 | + $response = curl_exec( $ch ); |
| 80 | + $errno = curl_errno( $ch ); |
| 81 | + $error = curl_error( $ch ); |
| 82 | + $code = (int) curl_getinfo( $ch, CURLINFO_RESPONSE_CODE ); |
| 83 | + |
| 84 | + curl_close( $ch ); |
| 85 | + |
| 86 | + if ( 0 !== $errno ) { |
| 87 | + throw new RuntimeException( 'Known Agents request failed: ' . $error ); |
| 88 | + } |
| 89 | + |
| 90 | + if ( ! is_string( $response ) ) { |
| 91 | + throw new RuntimeException( 'Known Agents response was not a string.' ); |
| 92 | + } |
| 93 | + |
| 94 | + if ( 200 > $code || 300 <= $code ) { |
| 95 | + throw new RuntimeException( |
| 96 | + sprintf( |
| 97 | + 'Known Agents API returned HTTP %d. Response: %s', |
| 98 | + $code, |
| 99 | + $response |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return $response; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Extract user-agent tokens from robots.txt text. |
| 109 | + * |
| 110 | + * @param string $robots_txt |
| 111 | + * @return array<int, string> |
| 112 | + */ |
| 113 | + public static function extract_user_agents( string $robots_txt ): array { |
| 114 | + $lines = preg_split( '/\R/', $robots_txt ); |
| 115 | + |
| 116 | + if ( false === $lines ) { |
| 117 | + return []; |
| 118 | + } |
| 119 | + |
| 120 | + $seen = []; |
| 121 | + $out = []; |
| 122 | + |
| 123 | + foreach ( $lines as $line ) { |
| 124 | + $line = trim( $line ); |
| 125 | + |
| 126 | + if ( '' === $line || str_starts_with( $line, '#' ) ) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + if ( ! preg_match( '/^User-agent:\s*(.+)$/i', $line, $matches ) ) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + $token = trim( $matches[1] ); |
| 135 | + |
| 136 | + if ( '' === $token || '*' === $token ) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + $key = strtolower( $token ); |
| 141 | + |
| 142 | + if ( isset( $seen[ $key ] ) ) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + $seen[ $key ] = true; |
| 147 | + $out[] = $token; |
| 148 | + } |
| 149 | + |
| 150 | + natcasesort( $out ); |
| 151 | + |
| 152 | + return array_values( $out ); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Build a PHP file that returns metadata + user agent list. |
| 157 | + * |
| 158 | + * @param array<int, string> $user_agents |
| 159 | + * @param array<int, string> $agent_types |
| 160 | + * @return string |
| 161 | + */ |
| 162 | + public static function build_php_array_file( array $user_agents, array $agent_types ): string { |
| 163 | + $export = var_export( |
| 164 | + [ |
| 165 | + 'generated_at' => gmdate( 'c' ), |
| 166 | + 'agent_types' => array_values( $agent_types ), |
| 167 | + 'user_agents' => array_values( $user_agents ), |
| 168 | + ], |
| 169 | + true |
| 170 | + ); |
| 171 | + |
| 172 | + return <<<PHP |
| 173 | +<?php |
| 174 | +declare(strict_types=1); |
| 175 | +
|
| 176 | +/** |
| 177 | + * Generated file. Do not edit by hand. |
| 178 | + */ |
| 179 | +
|
| 180 | +return {$export}; |
| 181 | +
|
| 182 | +PHP; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Convenience helper for presets. |
| 187 | + * |
| 188 | + * @param string $preset |
| 189 | + * @return array<int, string> |
| 190 | + */ |
| 191 | + public static function get_agent_types_for_preset( string $preset ): array { |
| 192 | + if ( ! isset( self::PRESETS[ $preset ] ) ) { |
| 193 | + throw new InvalidArgumentException( 'Unknown preset: ' . $preset ); |
| 194 | + } |
| 195 | + |
| 196 | + return self::PRESETS[ $preset ]; |
| 197 | + } |
| 198 | +} |
0 commit comments