/** * Génère une URL signée BunnyCDN (nouveau système SHA256) * Conforme à : https://support.bunny.net/hc/en-us/articles/360016055099 */ function bunny_sign_url($url, $token_key, $expiry_seconds = 3600, $token_path = null) { $expires = time() + intval($expiry_seconds); $parsed = parse_url($url); $path = $parsed['path']; // token_path folder-based $extra_params = ""; if ($token_path) { $extra_params .= "token_path=" . $token_path; } // Concaténation à hasher // IMPORTANT : security-key + path + expires + extra_params (sans ?) $to_hash = $token_key . $path . $expires; if ($extra_params !== "") { $to_hash .= $extra_params; } // SHA256 RAW, puis Base64 $hash = base64_encode(hash('sha256', $to_hash, true)); // Replacements obligatoires Bunny $token = str_replace( array('+', '/', '='), array('-', '_', ''), $hash ); // Reconstituer l’URL $query = "token={$token}&expires={$expires}"; if ($token_path) { $query .= "&token_path=" . urlencode($token_path); } $sep = (strpos($url, '?') !== false) ? '&' : '?'; return $url . $sep . $query; } Nicolas Goyette - Funambules Médias