You are here

function googtube_youtube in Googtube 6

Same name and namespace in other branches
  1. 5 googtube.module \googtube_youtube()
  2. 6.2 googtube.module \googtube_youtube()
  3. 7.2 googtube.module \googtube_youtube()
  4. 7 googtube.module \googtube_youtube()
1 call to googtube_youtube()
googtube_filter in ./googtube.module

File

./googtube.module, line 156

Code

function googtube_youtube($match, $format) {

  // extract id from complete url
  $parsed_url = parse_url(check_url($match));
  parse_str($parsed_url['query'], $parsed_query);
  $youtube_id = $parsed_query['v'];
  $headers = get_headers("http://gdata.youtube.com/feeds/api/videos/" . $youtube_id);

  // existing video?
  if (preg_match('/^HTTP\\/\\d\\.\\d\\s+(200|301|302)/', $headers[0])) {

    // existing video
    // load info about video in xml format
    $xmlData = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/" . $youtube_id);

    // parse video entry
    $video = new stdClass();

    // get nodes in media: namespace for media information
    $media = $xmlData
      ->children('http://search.yahoo.com/mrss/');

    // get video title
    $video->title = $media->group->title;

    // get video description
    $video->description = $media->group->description;

    // get video thumbnail
    $attrs = $media->group->thumbnail[2]
      ->attributes();
    $video->thumbnailURL = $attrs['url'];

    // these variables include the video information
    // replace incorrect double hyphens in description by single hyphens and new-line by space (found some video's with these)
    $youtube_title = str_replace('"', '\'', stripslashes($video->title));
    $youtube_description = str_replace('"', '\'', str_replace("\n", " ", stripslashes($video->description)));
    $youtube_thumbnail = stripslashes($video->thumbnailURL);
  }
  else {

    // non-existing video
    // show removed videos?
    if (variable_get('googtube_removed', 1)) {

      // fill variables with Not available message
      $youtube_title = t('Not available.');
      $youtube_description = t('This video has been removed.');
      $youtube_thumbnail = drupal_get_path('module', 'googtube') . '/default.jpg';
    }
    else {

      // don't show removed video
      return;
    }
  }

  // construct html for each method
  switch (variable_get('googtube_method', 'embedded')) {
    case 'embedded':
      return '<object width="' . variable_get('googtube_width', '425') . '" height="' . variable_get('googtube_height', '344') . '"><param name="movie" value="http://www.youtube.com/v/' . $youtube_id . '?version=3&fs=' . variable_get('googtube_fs', 0) . '&autoplay=' . variable_get('googtube_autoplay', 0) . '&rel=' . variable_get('googtube_rel', 1) . '"></param><param name="wmode" value="transparent"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $youtube_id . '?version=3&fs=' . variable_get('googtube_fs', 0) . '&autoplay=' . variable_get('googtube_autoplay', 0) . '&rel=' . variable_get('googtube_rel', 1) . '" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" width="' . variable_get('googtube_width') . '" height="' . variable_get('googtube_height') . '"></embed></object>';
      break;
    case 'iframe':
      return '<iframe class="youtube-video" width="' . variable_get('googtube_width', '425') . '" height="' . variable_get('googtube_height', '344') . '" src="http://www.youtube.com/embed/' . $youtube_id . '?fs=' . variable_get('googtube_fs', 0) . '&autoplay=' . variable_get('googtube_autoplay', 0) . '&rel=' . variable_get('googtube_rel', 1) . '" frameborder="0" allowfullscreen></iframe>';
      break;
    case 'floatbox':
      return '<a href="http://www.youtube.com/embed/' . $youtube_id . '?fs=' . variable_get('googtube_fs', 0) . '&rel=' . variable_get('googtube_rel', 1) . '&hd=1&autoplay=' . variable_get('googtube_autoplay', 0) . '" class="floatbox youtube-video" data-fb-options="width:' . variable_get('googtube_width', '425') . ' height:' . variable_get('googtube_height', '344') . ' autoFitMedia:true"><img title="' . $youtube_title . ': ' . $youtube_description . ' ' . t('[CLICK TO PLAY VIDEO]') . '" src="' . $youtube_thumbnail . '" /></a>';
      break;
    case 'colorbox':
      return '<a class="colorbox-load" href="http://www.youtube.com/embed/' . $youtube_id . '?fs=' . variable_get('googtube_fs', 0) . '&rel=' . variable_get('googtube_rel', 1) . '&hd=1&autoplay=' . variable_get('googtube_autoplay', 0) . '&amp;width=' . variable_get('googtube_width', '425') . '&amp;height=' . variable_get('googtube_height', '344') . '&amp;iframe=true&amp;wmode=transparent" title="' . $youtube_title . ': ' . $youtube_description . '"><img title="' . $youtube_title . ': ' . $youtube_description . ' ' . t('[CLICK TO PLAY VIDEO]') . '" src="' . $youtube_thumbnail . '" /></a>';
      break;
  }
}