You are here

function scald_youtube_parse_id in Scald YouTube 7

Parse a YouTube ID and check validity.

2 calls to scald_youtube_parse_id()
scald_youtube_scald_add_form_fill in ./scald_youtube.module
Implements hook_scald_add_form_fill().
scald_youtube_validate_id in ./scald_youtube.module
Form element validation handler for YouTube identifier.

File

./scald_youtube.module, line 728
Defines a YouTube provider for Scald.

Code

function scald_youtube_parse_id($string, $check) {
  $identifier = NULL;
  $string = trim($string);
  if (!preg_match(SCALD_YOUTUBE_ID_REGEXP, $string)) {

    // The string ID is not easy to parse, let's try to analyze it.
    if (preg_match(SCALD_YOUTUBE_WEB_REGEXP, $string, $m)) {

      // This string is a full YouTube URL.
      $identifier['id'] = $m[1];
      if (!empty($m[3])) {
        $identifier['list'] = $m[3];
      }
    }
    elseif (preg_match("/^http/", $string)) {

      // This string is a URL, most likely a shortened one.
      // For example (http://dai.ly, http://bit.ly, etc...).
      $response = drupal_http_request($string);
      if ($response->code == 200 && isset($response->redirect_code) && ($response->redirect_code == 301 || $response->redirect_code == 302)) {
        return scald_youtube_parse_id($response->redirect_url, $check);
      }
    }
  }
  else {
    $identifier['id'] = $string;
  }
  if (!empty($identifier) && $check) {

    // Last check to confirm this video really exists on YouTube.
    $info = scald_youtube_video($identifier['id']);
    if (empty($info)) {
      $identifier['id'] = FALSE;
    }
  }
  return $identifier;
}