Files
annu-kute-ced/includes/lib/http.php
T

47 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* Client HTTP GET sécurisé, commun aux intégrations distantes
* (PeerTube, Castopod, Funkwhale).
*
* Extrait de includes/config.php (ARC-1) — chargé par ce dernier.
* Centralise les réglages cURL : timeouts bornés, vérification SSL
* systématique, User-Agent identifié.
*/
/**
* Effectue une requête HTTP GET avec des options de sécurité par défaut.
*
* @param string $url URL complète à appeler (validée au préalable contre les SSRF)
* @param array $options Options optionnelles :
* - timeout (int, défaut 10) : durée maximale totale en secondes
* - connectTimeout (int, défaut 10) : durée maximale de connexion en secondes
* - followLocation (bool, défaut true) : suivre les redirections
* - maxRedirects (int, défaut 3) : nombre maximal de redirections suivies
* - userAgent (string, défaut 'fediverse-oki/1.0') : en-tête User-Agent
* - headers (array, défaut []) : en-têtes HTTP additionnels
* @return array Tableau associatif : 'body' (string|false), 'code' (int), 'error' (string)
*/
function httpGet($url, $options = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout'] ?? 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['connectTimeout'] ?? 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $options['followLocation'] ?? true);
curl_setopt($ch, CURLOPT_MAXREDIRS, $options['maxRedirects'] ?? 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, $options['userAgent'] ?? 'fediverse-oki/1.0');
if (!empty($options['headers'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $options['headers']);
}
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
return ['body' => $body, 'code' => $code, 'error' => $error];
}