You are here

function _dsq_fsockopen_urlopen in Drupal Most Popular 7

1 call to _dsq_fsockopen_urlopen()
dsq_urlopen in modules/mostpopular_disqus/disqusapi/url.php
Wrapper to provide a single interface for making an HTTP request.

File

modules/mostpopular_disqus/disqusapi/url.php, line 88

Code

function _dsq_fsockopen_urlopen($url, $postdata, &$response, $file_name, $file_field) {
  $buf = '';
  $req = '';
  $length = 0;
  $boundary = '----------' . md5(time());
  $postdata_str = dsq_get_post_content($boundary, $postdata, $file_name, $file_field);
  $url_pieces = parse_url($url);

  // Set default port for supported schemes if none is provided.
  if (!isset($url_pieces['port'])) {
    switch ($url_pieces['scheme']) {
      case 'http':
        $url_pieces['port'] = 80;
        break;
      case 'https':
        $url_pieces['port'] = 443;
        $url_pieces['host'] = 'ssl://' . $url_pieces['host'];
        break;
    }
  }

  // Set default path if trailing slash is not provided.
  if (!isset($url_pieces['path'])) {
    $url_pieces['path'] = '/';
  }

  // Determine if we need to include the port in the Host header or not.
  if ($url_pieces['port'] == 80 && $url_pieces['scheme'] == 'http' || $url_pieces['port'] == 443 && $url_pieces['scheme'] == 'https') {
    $host = $url_pieces['host'];
  }
  else {
    $host = $url_pieces['host'] . ':' . $url_pieces['port'];
  }
  $fp = @fsockopen($url_pieces['host'], $url_pieces['port'], $errno, $errstr, SOCKET_TIMEOUT);
  if (!$fp) {
    return false;
  }
  $path = $url_pieces['path'];
  if ($url_pieces['query']) {
    $path .= '?' . $url_pieces['query'];
  }
  $req .= ($postdata_str ? 'POST' : 'GET') . ' ' . $path . " HTTP/1.1\r\n";
  $req .= 'Host: ' . $host . "\r\n";
  $req .= dsq_get_http_headers_for_request($boundary, $postdata_str, $file_name, $file_field);
  if ($postdata_str) {
    $req .= "\r\n\r\n" . $postdata_str;
  }
  $req .= "\r\n\r\n";
  fwrite($fp, $req);
  while (!feof($fp)) {
    $buf .= fgets($fp, 4096);
  }

  // Parse headers from the response buffers.
  list($headers, $response['data']) = explode("\r\n\r\n", $buf, 2);

  // Get status code from headers.
  $headers = explode("\r\n", $headers);
  list($unused, $response['code'], $unused) = explode(' ', $headers[0], 3);
  $headers = array_slice($headers, 1);

  // Convert headers into associative array.
  foreach ($headers as $unused => $header) {
    $header = explode(':', $header);
    $header[0] = trim($header[0]);
    $header[1] = trim($header[1]);
    $headers[strtolower($header[0])] = strtolower($header[1]);
  }

  // If transfer-coding is set to chunked, we need to join the message body
  // together.
  if (isset($headers['transfer-encoding']) && 'chunked' == $headers['transfer-encoding']) {
    $chunk_data = $response['data'];
    $joined_data = '';
    while (true) {

      // Strip length from body.
      list($chunk_length, $chunk_data) = explode("\r\n", $chunk_data, 2);
      $chunk_length = hexdec($chunk_length);
      if (!$chunk_length || !strlen($chunk_data)) {
        break;
      }
      $joined_data .= substr($chunk_data, 0, $chunk_length);
      $chunk_data = substr($chunk_data, $chunk_length + 1);
      $length += $chunk_length;
    }
    $response['data'] = $joined_data;
  }
  else {
    $length = $headers['content-length'];
  }
}