/**
  * 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();
         }
     });
 }
 private static function follow_url($url)
 {
     if (!function_exists('curl_exec')) {
         return false;
     }
     $curl = curl_init();
     $curl_version = curl_version();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     // make curl_exec() return the result
     curl_setopt($curl, CURLOPT_HEADER, true);
     // header only
     curl_setopt($curl, CURLOPT_NOBODY, true);
     // return no body; HTTP request method: HEAD
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, \Podlove\get_setting('website', 'ssl_verify_peer') == 'on');
     // Don't check SSL certificate in order to be able to use self signed certificates
     curl_setopt($curl, CURLOPT_FAILONERROR, true);
     curl_setopt($curl, CURLOPT_TIMEOUT, 3);
     // HEAD requests shouldn't take > 2 seconds
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
     // follow redirects
     curl_setopt($curl, CURLOPT_MAXREDIRS, 0);
     // maximum number of redirects
     curl_setopt($curl, CURLOPT_USERAGENT, \Podlove\Http\Curl::user_agent());
     $response = curl_exec($curl);
     $response_header = curl_getinfo($curl);
     $error = curl_error($curl);
     curl_close($curl);
     if (isset($response_header['redirect_url']) && $response_header['redirect_url']) {
         return $response_header['redirect_url'];
     } else {
         return false;
     }
     // return array(
     // 	'header'   => $response_header,
     // 	'response' => $response,
     // 	'error'    => $error
     // );
 }
 /**
  * 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));
     }
 }
 public function check_code()
 {
     if (isset($_GET["code"]) && $_GET["code"]) {
         if ($this->get_module_option('auphonic_api_key') == "") {
             $ch = curl_init('https://auth.podlove.org/auphonic.php');
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
             curl_setopt($ch, CURLOPT_USERAGENT, \Podlove\Http\Curl::user_agent());
             curl_setopt($ch, CURLOPT_POSTFIELDS, array("redirect_uri" => get_site_url() . '/wp-admin/admin.php?page=podlove_settings_modules_handle', "code" => $_GET["code"]));
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
             // verify against startssl crt
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
             curl_setopt($ch, CURLOPT_CAINFO, \Podlove\PLUGIN_DIR . '/cert/podlove.crt');
             $result = curl_exec($ch);
             $this->update_module_option('auphonic_api_key', $result);
             header('Location: ' . get_site_url() . '/wp-admin/admin.php?page=podlove_settings_modules_handle');
         }
     }
     if (isset($_GET["reset_auphonic_auth_code"]) && $_GET["reset_auphonic_auth_code"] == "1") {
         $this->update_module_option('auphonic_api_key', "");
         delete_transient('podlove_auphonic_user');
         delete_transient('podlove_auphonic_presets');
         header('Location: ' . get_site_url() . '/wp-admin/admin.php?page=podlove_settings_modules_handle');
     }
 }
 /**
  * @todo  use \Podlove\Http\Curl
  * 
  * @return array	
  */
 public static function curl_get_header_for_url($url, $etag = NULL)
 {
     if (!function_exists('curl_exec')) {
         return [];
     }
     $curl = curl_init();
     if (\Podlove\Http\Curl::curl_can_follow_redirects()) {
         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
         // follow redirects
         curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
         // maximum number of redirects
     } else {
         $url = \Podlove\Http\Curl::resolve_redirects($url, 5);
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     // make curl_exec() return the result
     curl_setopt($curl, CURLOPT_HEADER, true);
     // header only
     curl_setopt($curl, CURLOPT_NOBODY, true);
     // return no body; HTTP request method: HEAD
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, \Podlove\get_setting('website', 'ssl_verify_peer') == 'on');
     // Don't check SSL certificate in order to be able to use self signed certificates
     curl_setopt($curl, CURLOPT_FAILONERROR, true);
     curl_setopt($curl, CURLOPT_TIMEOUT, 3);
     // HEAD requests shouldn't take > 2 seconds
     if ($etag) {
         curl_setopt($curl, CURLOPT_HTTPHEADER, array('If-None-Match: "' . $etag . '"'));
     }
     curl_setopt($curl, CURLOPT_USERAGENT, \Podlove\Http\Curl::user_agent());
     $response = curl_exec($curl);
     $response_header = curl_getinfo($curl);
     $error = curl_error($curl);
     curl_close($curl);
     return array('header' => $response_header, 'response' => $response, 'error' => $error);
 }