'method_not_allowed'],405); } if (strtolower(trim(explode(';',$_SERVER['CONTENT_TYPE'] ?? '')[0])) !== 'application/json') { twidgetReply(['error'=>'unsupported_media_type'],415); } $raw = file_get_contents('php://input',false,null,0,8193); $input = strlen($raw)<=8192 ? json_decode($raw,true) : null; if (!is_array($input)) twidgetReply(['error'=>'invalid_request'],400); try { $configPath = getenv('TWIDGET_CONFIG') ?: dirname($_SERVER['DOCUMENT_ROOT']).'/private/twidget.php'; if (!is_file($configPath)) throw new RuntimeException('not_configured'); $config = require $configPath; if (!is_array($config) || !is_string($config['credential'] ?? null) || !preg_match('/^[a-f0-9]{64}$/D',$config['credential']) || !is_string($config['url'] ?? null) || parse_url($config['url'],PHP_URL_SCHEME)!=='https') { throw new RuntimeException('not_configured'); } $curl = curl_init($config['url']); curl_setopt_array($curl,[ CURLOPT_POST=>true, CURLOPT_RETURNTRANSFER=>true, CURLOPT_CONNECTTIMEOUT=>3, CURLOPT_TIMEOUT=>8, CURLOPT_FOLLOWLOCATION=>false, CURLOPT_SSL_VERIFYPEER=>true, CURLOPT_SSL_VERIFYHOST=>2, CURLOPT_HTTPHEADER=>['Content-Type: application/json','Authorization: Bearer '.$config['credential']], CURLOPT_POSTFIELDS=>json_encode($input,JSON_THROW_ON_ERROR), ]); $body = curl_exec($curl); $status = (int)curl_getinfo($curl,CURLINFO_HTTP_CODE); if (PHP_VERSION_ID<80000) curl_close($curl); if (!is_string($body) || $status!==200) throw new RuntimeException('verification_unavailable'); $result = json_decode($body,true,512,JSON_THROW_ON_ERROR); if (!is_array($result) || !in_array($result['status'] ?? null,['ok','refused'],true)) { throw new RuntimeException('invalid_response'); } // No widget-specific policy, MQFT, notification or device logic here. twidgetReply(['status'=>$result['status']]); } catch (Throwable $error) { twidgetReply(['error'=>'verification_unavailable'],503); }