Skip to content

Instantly share code, notes, and snippets.

@ajgagnon
Last active November 19, 2020 17:00
Show Gist options
  • Select an option

  • Save ajgagnon/18f1d29de288bfc308f157cd88ff9b53 to your computer and use it in GitHub Desktop.

Select an option

Save ajgagnon/18f1d29de288bfc308f157cd88ff9b53 to your computer and use it in GitHub Desktop.
<?php
class BunnyCDN {
public static function signUrl($url, $securityKey, $expiration_time = 3600, $user_ip = NULL, $is_directory_token = false, $path_allowed = NULL, $countries_allowed = NULL, $countries_blocked = NULL, $referers_allowed = NULL)
{
if (!$user_ip) {
$user_ip = self::getUserIP();
}
if (!is_null($countries_allowed)) {
$url .= (parse_url($url, PHP_URL_QUERY) == "") ? "?" : "&";
$url .= "token_countries={$countries_allowed}";
}
if (!is_null($countries_blocked)) {
$url .= (parse_url($url, PHP_URL_QUERY) == "") ? "?" : "&";
$url .= "token_countries_blocked={$countries_blocked}";
}
if (!is_null($referers_allowed)) {
$url .= (parse_url($url, PHP_URL_QUERY) == "") ? "?" : "&";
$url .= "token_referer={$referers_allowed}";
}
$url_scheme = parse_url($url, PHP_URL_SCHEME);
$url_host = parse_url($url, PHP_URL_HOST);
$url_path = parse_url($url, PHP_URL_PATH);
$url_query = parse_url($url, PHP_URL_QUERY);
$parameters = array();
parse_str($url_query, $parameters);
// Check if the path is specified and ovewrite the default
$signature_path = $url_path;
if (!is_null($path_allowed)) {
$signature_path = $path_allowed;
$parameters["token_path"] = $signature_path;
}
// Expiration time
$expires = time() + $expiration_time;
// Construct the parameter data
ksort($parameters); // Sort alphabetically, very important
$parameter_data = "";
$parameter_data_url = "";
if (sizeof($parameters) > 0) {
foreach ($parameters as $key => $value) {
if (strlen($parameter_data) > 0)
$parameter_data .= "&";
$parameter_data_url .= "&";
$parameter_data .= "{$key}=" . $value;
$parameter_data_url .= "{$key}=" . urlencode($value); // URL encode everything but slashes for the URL data
}
}
// Generate the toke
$hashableBase = $securityKey . $signature_path . $expires;
// If using IP validation
if (!is_null($user_ip)) {
$hashableBase .= $user_ip;
}
$hashableBase .= $parameter_data;
// Generate the token
$token = hash('sha256', $hashableBase, true);
$token = base64_encode($token);
$token = strtr($token, '+/', '-_');
$token = str_replace('=', '', $token);
if ($is_directory_token) {
return "{$url_scheme}://{$url_host}/bcdn_token={$token}&expires={$expires}{$parameter_data_url}{$url_path}";
} else {
return "{$url_scheme}://{$url_host}{$url_path}?token={$token}{$parameter_data_url}&expires={$expires}";
}
}
public static function getUserIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
<?php
$url = 'http://httpstestprestoplayercom-presto.b-cdn.net/wp-content/uploads/presto-player-private/ocean-flyby.mp4';
$zone_token = "6e277223-9bc7-4492-85cd-9881ac1091d4";
$signed = BunnyCDN::signUrl($url, $zone_token, 36000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment