/**
  * Manually resolve redirects.
  * 
  * Some server configurations can't deal with cURL CURLOPT_FOLLOWLOCATION
  * setting. This method resolves a URL without using that setting.
  * 
  * @param  string  $url               URL to resolve
  * @param  integer $maximum_redirects Maximum redirects. Default: 5.
  * @return string                     Final URL
  */
 public static function resolve_redirects($url, $maximum_redirects = 5)
 {
     $curl = new Curl();
     $curl->request($url, ['method' => 'HEAD', '_redirection' => 0]);
     $response = $curl->get_response();
     $http_code = $response['response']['code'];
     $location = isset($response['headers']['location']) ? $response['headers']['location'] : NULL;
     if ($http_code >= 300 && $http_code <= 400 && $location && $maximum_redirects > 0) {
         return self::resolve_redirects($location, $maximum_redirects - 1);
     }
     return $url;
 }
 public function fetch_presets()
 {
     return self::cache_for('podlove_auphonic_presets', function () {
         $curl = new Http\Curl();
         $curl->request('https://auphonic.com/api/presets.json', array('headers' => array('Content-type' => 'application/json', 'Authorization' => 'Bearer ' . API_Wrapper::$auth_key)));
         $response = $curl->get_response();
         if ($curl->isSuccessful()) {
             return json_decode($response['body']);
         } else {
             return array();
         }
     });
 }
 /**
  * POST $data to the given $url
  * 
  * @param  string $url  ADN API URL
  * @param  array  $data
  */
 public function post($url, $data)
 {
     $data_string = json_encode($data);
     $curl = new Http\Curl();
     $curl->request($url, array('method' => 'POST', 'timeout' => '5000', 'body' => $data_string, 'headers' => array('Content-type' => 'application/json', 'Content-Length' => \Podlove\PHP\strlen($data_string))));
     $response = $curl->get_response();
     $body = json_decode($response['body']);
     if ($body->meta->code !== 200) {
         \Podlove\Log::get()->addWarning(sprintf('Error: App.net Module failed to Post: %s (Code %s)', str_replace("'", "''", $body->meta->error_message), $body->meta->code));
     }
 }
 /**
  * Fetch list of presets via Auphonic APU.
  *
  * Cached in transient "podlove_auphonic_presets".
  * 
  * @return string
  */
 public function fetch_presets()
 {
     $cache_key = 'podlove_auphonic_presets';
     if (($presets = get_transient($cache_key)) !== FALSE) {
         return $presets;
     } else {
         if (!($token = $this->get_module_option('auphonic_api_key'))) {
             return "";
         }
         $curl = new Http\Curl();
         $curl->request('https://auphonic.com/api/presets.json', array('headers' => array('Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $this->get_module_option('auphonic_api_key'))));
         $response = $curl->get_response();
         if ($curl->isSuccessful()) {
             $presets = json_decode($response['body']);
             set_transient($cache_key, $presets, 60 * 60 * 24 * 365);
             // 1 year, we devalidate manually
             return $presets;
         } else {
             return array();
         }
     }
 }