You are here

function _oembedprovider_handle_request in oEmbed 6.0

Same name and namespace in other branches
  1. 8 modules/oembedprovider/oembedprovider.inc \_oembedprovider_handle_request()
  2. 7 modules/oembedprovider/oembedprovider.inc \_oembedprovider_handle_request()
  3. 7.0 oembedprovider/oembedprovider.inc \_oembedprovider_handle_request()

Callback handler for oembed requests.

Parameters

string $format: Optional. The response format to use. Defaults to $_GET['format'] or 'json', in that order.

1 string reference to '_oembedprovider_handle_request'
oembedprovider_menu in ./oembedprovider.module
Implementation of hook_menu().

File

./oembedprovider.inc, line 15
Functions for the oEmbed provider

Code

function _oembedprovider_handle_request($format = '') {
  if (empty($format)) {
    $format = empty($_GET['format']) ? 'json' : $_GET['format'];
  }

  // Get the supported formats, and abort if a unsupported format it requested
  $formats = oembedprovider_formats();
  if (!isset($formats[$format])) {
    header('HTTP/1.0 501 Not implemented. Unsupported response format "' . check_plain($format) . '"');
    die;
  }
  $f = $formats[$format];
  header('Content-type: ' . $f['mime']);

  // Check that we got a url
  if (empty($_GET['url'])) {
    header('HTTP/1.0 406 Not acceptable. The url parameter is required.');
    print call_user_func($f['callback'], _oembedprovider_result('link', array(
      'title' => t('The url parameter is required'),
      'error' => 1,
    )));
    die;
  }
  $url = $_GET['url'];
  $matches = array();
  $provider = oembedcore_get_provider($url, $matches);
  if ($provider) {
    $forward = array(
      'maxwidth',
      'maxheight',
    );
    $attributes = array();
    foreach ($forward as $key) {
      if (isset($_GET[$key])) {
        $attributes[$key] = $_GET[$key];
      }
    }
    $data = oembedcore_oembed_fetch($provider, $url, $matches, $attributes);
    if ($data) {
      print call_user_func($f['callback'], $data);
    }
    else {
      header('HTTP/1.0 404 Not found.');
      print call_user_func($f['callback'], _oembedprovider_result('link', array(
        'title' => t('Could not fetch an embed for this url'),
        'error' => 1,
      )));
    }
    die;
  }
  else {
    header('HTTP/1.0 404 Not found.');
    print call_user_func($f['callback'], _oembedprovider_result('rich', array(
      'title' => t('Could not find a provider that supports this url'),
      'error' => 1,
    )));
    die;
  }
}