You are here

function shorten_fetch in Shorten URLs 8

Same name and namespace in other branches
  1. 8.2 shorten.module \shorten_fetch()
  2. 6 shorten.module \shorten_fetch()
  3. 7.2 shorten.module \shorten_fetch()
  4. 7 shorten.module \shorten_fetch()

Downloads the response of the URL abbreviation service.

Parameters

$url: The URL which will return an abbreviated URL from any service. Includes both the service and the URL to be shortened.

$tag: If the response is XML, the tag within which to look for the shortened URL.

$special: A special format the service will return. Currently only supports 'json.'

$options: An associative array of options to allow executing an advanced request. Valid keys include anything that would work with drupal_http_request() except that $options['context'] is only used with the PHP request method.

Return value

An abbreviated URL or FALSE if fetching the abbreviated URL fails.

2 calls to shorten_fetch()
_shorten_get_url in ./shorten.module
Shortens URLs. Times out after three (3) seconds.
_shorten_googl in ./shorten.module
Helps get a shortened URL from Goo.gl.

File

./shorten.module, line 284
Shortens URLs via external services.

Code

function shorten_fetch($url, $tag = '', $special = '', $options = array()) {
  $options += array(
    'headers' => array(),
    'method' => 'GET',
    'data' => NULL,
    'max_redirects' => 3,
    'timeout' => \Drupal::config('shorten.settings')
      ->get('shorten_timeout'),
    'context' => NULL,
  );
  if (\Drupal::config('shorten.settings')
    ->get('shorten_method') == 'php') {
    try {
      $response = \Drupal::httpClient()
        ->get($url, $options);
      $result = stripslashes((string) $response
        ->getBody());
    } catch (RequestException $e) {
      \Drupal::logger('shorten')
        ->error('@code error shortening the URL @url using the PHP request method. Error message: %error', array(
        '@code' => $result->code,
        '@url' => $url,
        '%error' => $e,
      ));
    }
    $contents = empty($result) ? NULL : $result;
  }
  elseif (\Drupal::config('shorten.settings')
    ->get('shorten_method') == 'curl') {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $options['timeout']);

    // https://drupal.org/node/1481634

    //if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {

    //  curl_setopt($c, CURLOPT_FOLLOWLOCATION, TRUE);

    //}
    curl_setopt($c, CURLOPT_MAXREDIRS, $options['max_redirects']);

    // defined() checks due to https://drupal.org/node/1469400
    // Specifying the protocol is necessary to avoid malicious redirects to POP
    // in libcurl 7.26.0 to 7.28.1
    if (defined('CURLOPT_REDIR_PROTOCOLS') && defined('CURLPROTO_HTTP') && defined('CURLPROTO_HTTPS')) {
      curl_setopt($c, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
    }
    curl_setopt($c, CURLOPT_URL, $url);
    if ($options['method'] != 'GET') {
      $uri = @parse_url($url);
      if ($uri['scheme'] == 'http') {
        curl_setopt($c, CURLOPT_PORT, isset($uri['port']) ? $uri['port'] : 80);
        if (defined('CURLOPT_PROTOCOLS') && defined('CURLPROTO_HTTP')) {
          curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
        }
      }
      elseif ($uri['scheme'] == 'https') {
        curl_setopt($c, CURLOPT_PORT, isset($uri['port']) ? $uri['port'] : 443);
        if (defined('CURLOPT_PROTOCOLS') && defined('CURLPROTO_HTTPS')) {
          curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
        }
      }
      if (!empty($options['headers'])) {
        $curl_headers = array();
        foreach ($options['headers'] as $key => $value) {
          $curl_headers[] = trim($key) . ': ' . trim($value);
        }
        curl_setopt($c, CURLOPT_HTTPHEADER, $curl_headers);
      }
      if ($options['method'] == 'POST') {
        curl_setopt($c, CURLOPT_POST, TRUE);
        curl_setopt($c, CURLOPT_POSTFIELDS, $options['data']);
      }
      else {
        curl_setopt($c, CURLOPT_CUSTOMREQUEST, $options['method']);
      }
      curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
    }
    $contents = curl_exec($c);
    curl_close($c);
  }
  else {
    return FALSE;
  }
  if (!$contents) {
    return FALSE;
  }
  if ($tag) {
    if (!$special) {
      $contents = _shorten_xml($contents, $tag);
    }
    elseif ($special == 'json') {
      $contents = json_decode($contents, TRUE);
      $contents = $contents[$tag];
    }
  }
  if (!$contents || $contents == $url) {
    return FALSE;
  }
  return $contents;
}