'') { return "$file_url?$start_str"; } return $file_url; } } $CC = $_SERVER['GEOIP_COUNTRY_CODE']; if (isset($post_re) and ! empty($post_re)) { $file_path = preg_replace($post_re[0], $post_re[1], $file_path); } ### check alternative redirects ### checks full file path ($file_url without domain part) for match mask_re and changes KVS domain to alternative $file_url_without_domain = explode('/', $file_url, 4)[3]; # this gives contents/videos/2000/2122/2122.flv for ($i = 0; $i < count($alt_redirect); ++$i) { if (isset($alt_redirect[$i]['file_url_re'])) { if (preg_match("/".$alt_redirect[$i]['file_url_re']."/", $file_url)) { if (preg_match("/".$alt_redirect[$i]['mask_re']."/", $file_url_without_domain)) { ahcdn_log(2, "$CC : ".$alt_redirect[$i]['url'] ."/". $file_url_without_domain."\n"); return $alt_redirect[$i]['url'] ."/". $file_url_without_domain; } } } else { if (preg_match("/".$alt_redirect[$i]['mask_re']."/", $file_url_without_domain)) { ahcdn_log(2, "$CC : ".$alt_redirect[$i]['url'] ."/". $file_url_without_domain."\n"); return $alt_redirect[$i]['url'] ."/". $file_url_without_domain; } } } $time = time() + (isset($expire) && $expire ? $expire : 3600); # key will expire in 1 hour $url = ',end=' . $time; if (isset($ip_limit) && $ip_limit) { if ($ip_limit < 1) { $ip_limit = 1; }; if ($ip_limit > 5) { $ip_limit = 5; }; $url .= ',limit='.$ip_limit; }; if (isset($lock_ip) && $lock_ip) { $url .= ',ip=' . ahcdn_get_ip(); } if (isset($ip_limit) && $ip_limit) { $url .= '/data=' . ahcdn_generate_random_string(); } if ( isset($geo_specific[$CC]['bitrate']) && $geo_specific[$CC]['bitrate'] ) { $bitrate = $geo_specific[$CC]['bitrate']; } if ( isset($geo_specific[$CC]['buffer']) && $geo_specific[$CC]['buffer'] ) { $buffer = $geo_specific[$CC]['buffer']; } if (isset($referer)) { $url .= '/referer='.$referer; } if (isset($bitrate) && $bitrate) { $url .= '/speed=' . $bitrate; } if (isset($buffer) && $buffer) { $url .= '/buffer=' . $buffer; } ### check download ### if (isset($_GET['download'])) { $is_download = trim($_GET['download']); if ($is_download == 'true') { $url .= '/download2='; }; } if (isset($_GET['download_filename'])) { $download_filename = trim($_GET['download_filename']); $url .= $download_filename; } $url .= '/'.ltrim($file_path,'/'); $key = substr(base64_encode(md5("$secret$url", true)), 0, 22); $key = str_replace('/', '-', $key); if (!isset($domain)) { # if $domain is not set in ahcdn_config.php read it from $file_url (URL(*) field) $split_arr = preg_split( "/\//", $file_url); $domain = $split_arr[0].'/'.$split_arr[1].'/'.$split_arr[2]; } if ( preg_match('/^http/', $domain) ) { $fullurl = $domain . '/key=' . $key . $url; } else { $fullurl = 'http://' . $domain . '/key=' . $key . $url; } if ($start_str <> '') { $fullurl .= '?' . $start_str; } ahcdn_log(2, "$CC : $fullurl \n"); return $fullurl; } /** * Returns image / zip file URL with all parameters configured. * * @param $file_path [String] relative file path, example: main/800x600/2000/2122/10982.jpg * @param $file_url [String] full file URL, example: http://cdn.domain.com/contents/albums/main/800x600/2000/2122/10982.jpg * @param $streaming_key [String] protection key entered in storage server configuration, example: abcdefg * @return image / zip file URL */ function ahcdn_get_image($file_path,$file_url,$streaming_key) { // example is to return URL without any parameters return $file_url; } /** * Invalidates the given folders or files on CDN. * Files are always provided as exact list of all affected files. Folders are provided in some cases where all files * in specific folders should be invalidated. * * Folders and files contain paths relative to the given $server_url. * * @param $server_url [String] CDN URL configured in storage server, example: http://cdn.domain.com/contents/videos * @param $folders [Array of String] list of related folder paths, which should be invalidated, can be null, example: array("2000/2122", "2000/2123") * @param $files [Array of String] list of related file paths, which should be invalidated, example: array("2000/2122/2122.flv", "2000/2122/2122.mp4", "2000/2123/2123.flv", "2000/2123/2123.mp4") * @param $operation [String] indicates operation on the above files, one if the following: "add", "change", "delete", "multiple" * (multiple means that KVS cannot exactly tell operation, part of files may be updated and part deleted and etc.) */ function ahcdn_invalidate_resources($server_url,$folders,$files,$operation) { require 'ahcdn_config.php'; if (isset($disable_cdn_api) and ($disable_cdn_api > 0)) { return true; } if (!isset($api_user) || empty($api_user) || !isset($api_password) || empty($api_password)) { $error = "No API auth data in config!"; return $error; } if (empty($files) and empty($folders)) { $error = "Nothing to do!"; return $error; } if (empty($folders)) { $folders = array(); } else { if (is_string($folders)) { $folders = array($folders); } } if (empty($files)) { $files = array(); } else { if (is_string($files)) { $files = array($files); } } $real_folders = array(); $real_files = array(); foreach($folders as $folder) { $folder = trim($folder, '/'); $real_folders[] = $folder;#$server_url.$folder.'/*'; } foreach($files as $file) { $file = ltrim($file, '/'); if (isset($post_re) and ! empty($post_re)) { $file = preg_replace($post_re[0], $post_re[1], $file); } if (isset($prefix) and ! empty($prefix)) { $real_files[] = $prefix . $file; } else { $real_files[] = $file; } } $names = implode("\n", $real_files); if (isset($operation)and( $operation == 'delete' or $operation == 'change' )) { if (isset($ignore_change_invalidate)and($ignore_change_invalidate == 1)and($operation == 'change')) { # do not send delete api call ahcdn_log(1, "Ignore 'change' operation on invalidate_resources(ignore_change_invalidate is set to 1) : files = '$names' \n"); return true; } $names = implode("\n", $real_files); $idata = array('names' => $names); $post_data = http_build_query($idata); $r = array(); $r = ahcdn_api_call($api_user, $api_password, 'cp.ahcdn.com/api2/file/delete', $post_data); ahcdn_log(1, "'$operation' operation on invalidate_resources : files = '$names' :: code = ".$r['response_code']." :: response = ".$r['response_text']."\n"); if ($r['response_code'] == 200) { return true; } return false; } elseif ( isset($operation)and( $operation == 'add' or $operation == 'multiple')) { // Can not add/change files without their sizes ahcdn_log(1, "ADD/MULTIPLE operation on invalidate_resources : operation = '$operation' :: files = '$names'\n" ); } else { $names = implode("\n", $real_files); $idata = array('names' => $names); $post_data = http_build_query($idata); ahcdn_log(1, "unknown operation on invalidate_resources, treating as delete on old KVS : operation = '$operation' :: files = '$names'\n" ); $r = array(); $r = ahcdn_api_call($api_user, $api_password, 'cp.ahcdn.com/api2/file/delete', $post_data); ahcdn_log(1, "DELETE operation on invalidate_resources : files = '$names' :: code = ".$r['response_code']." :: response = ".$r['response_text']."\n"); if ($r['response_code'] == 200) { return true; } return false; } } function ahcdn_api_call($api_user, $api_password, $api_url, $post_data) { if (!function_exists('curl_init')) { $response = 'No PHP-CURL found. '. 'See http://php.net/manual/en/curl.installation.php'; return false; } $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => 'https://'.$api_url.'?'.$post_data, CURLOPT_PORT => 443, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => true, CURLOPT_FORBID_REUSE => 1, // no caching CURLOPT_FRESH_CONNECT => 1, // no caching CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_USERPWD => $api_user.":".$api_password ) ); $response_text = curl_exec($ch); $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $r = array(); $r['response_code'] = $response_code; $r['response_text'] = $response_text; return $r; } function ahcdn_log($l_level, $string) { require 'ahcdn_config.php'; if (! isset($log_level)) { $log_level = 1; } if (isset($log_file)) { if (($log_level > 0)and($l_level <= $log_level)) { if (file_exists($log_file)) { $h = fopen($log_file, "a"); fwrite($h, date(DATE_RFC2822) . " : " . $string); fclose($h); } } } } function ahcdn_get_ip() { if(getenv("REMOTE_ADDR")) { return getenv("REMOTE_ADDR"); } elseif (getenv("HTTP_CLIENT_IP")) { return getenv("HTTP_CLIENT_IP"); } return ''; } function ahcdn_generate_random_string() { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < 6; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } ?>