'OpenAI Search','GPTBot'=>'OpenAI Training','ChatGPT-User'=>'OpenAI User Fetch','Claude-SearchBot'=>'Anthropic Search','Claude-User'=>'Anthropic User Fetch','ClaudeBot'=>'Anthropic Training','PerplexityBot'=>'Perplexity Search','Perplexity-User'=>'Perplexity User Fetch','Google-Extended'=>'Google AI Control','Googlebot'=>'Google Search','Applebot-Extended'=>'Apple AI','Applebot'=>'Apple Search','CCBot'=>'Common Crawl','Amazonbot'=>'Amazon','Bytespider'=>'ByteDance','meta-externalagent'=>'Meta External Agent','Meta-ExternalAgent'=>'Meta External Agent','cohere-ai'=>'Cohere']; $label='Recognized automated client'; foreach($bots as $needle=>$name){ if(stripos($ua,$needle)!==false){$label=$name;break;} } $saltFile=$dir.'/.ai-log-salt'; if(!file_exists($saltFile)) @file_put_contents($saltFile,bin2hex(random_bytes(24)),LOCK_EX); $salt=@file_get_contents($saltFile)?:'useworthy'; $row=['ts'=>gmdate('c'),'channel'=>'crawler','bot'=>$label,'method'=>$_SERVER['REQUEST_METHOD']??'GET','path'=>'/'.$requested,'user_agent'=>$ua,'ip_hash'=>hash('sha256',$salt.'|'.($_SERVER['REMOTE_ADDR']??'')),'referer'=>substr($_SERVER['HTTP_REFERER']??'',0,500)]; $log=$dir.'/ai-crawler.ndjson'; if(file_exists($log)&&filesize($log)>10485760) @rename($log,$log.'.1'); @file_put_contents($log,json_encode($row,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND|LOCK_EX); $ext=strtolower(pathinfo($target,PATHINFO_EXTENSION)); $types=['html'=>'text/html; charset=utf-8','md'=>'text/markdown; charset=utf-8','txt'=>'text/plain; charset=utf-8','json'=>'application/json; charset=utf-8','xml'=>'application/xml; charset=utf-8']; header('Content-Type: '.($types[$ext]??'application/octet-stream')); header('X-Useworthy-Crawler-Logged: 1'); header('Vary: User-Agent'); $mtime=filemtime($target); $etag='"'.sha1_file($target).'"'; header('ETag: '.$etag); header('Last-Modified: '.gmdate('D, d M Y H:i:s',$mtime).' GMT'); if(($_SERVER['HTTP_IF_NONE_MATCH']??'')===$etag){http_response_code(304);exit;} if($ext==='html'){ $md=preg_replace('/\.html$/','.md','/'.$requested); if($requested==='index.html')$md='/index.md'; header('Link: <'.$md.'>; rel="alternate"; type="text/markdown", ; rel="describedby"'); } readfile($target); ?>