Hi everybody.

I had a case with a client, who copy pasted partial url's of vimeo videos, and because we were using fckeditor they would not see the broken video inside it. Although they could click "view source" and remove the code from there, they requested us a better solution.

The only thing i could came up with was to test if the id found using preg_match inside MediaInternetVimeoHandler's function "parse" was that of a valid video. For this I used the Vimeo Developers / Simple API, using the API call "Making a Video Request" - documentation here: http://vimeo.com/api/docs/simple-api#video . I then test if the response XML contains the video ID, or not (data is returned only for valid videos, even videos that were deleted make this call return nothing). If not I do not add the video to the library, and also throw a MediaInternetValidationException about the invalid ID.

I think this is a useful functionality, and could be included in the module, so I am attaching the patch here.
It is against 7.x-dev.

I also created a similar patch for media_youtube, the related issue is here: #1292344: Validate the youtube ID before adding to the library

Comments

mikeytown2’s picture

Title: Validate the vimeo ID before adding to the library » Validate the vimeo ID before adding to the library; improve regex.
StatusFileSize
new1.19 KB

I've been having issues with the regex as well.

noslokire’s picture

Patch works for me, thanks

drewish’s picture

Status: Needs review » Needs work

I guess my patch at #1497108: Support more Vimeo URL formats overlaps with this. I think having an idValid function makes sense but it should be static since it takes the id as a parameter and doesn't rely on the class's state.

drewish’s picture

Okay so #1497108: Support more Vimeo URL formats got committed. I think it might make sense to look at validating the ids too but this will need a re-roll.

mikeytown2’s picture

Status: Needs work » Needs review
StatusFileSize
new1 KB

Currently using the Simple API: http://vimeo.com/api/docs/simple-api
We could go for php or json instead of XML (so we don't have to use simplexml_load_file()). In any case here is the patch re-rolled currently using XML.

Format comparisons:
http://vimeo.com/api/v2/video/39078923.xml
http://vimeo.com/api/v2/video/39078923.php
http://vimeo.com/api/v2/video/39078923.json

drewish’s picture

Status: Needs review » Needs work

Yeah I'd prefer JSON since we could avoid this issue #1180386: Use of copy() and simplexml_load_file() rather than drupal_http_request() causes Media:YouTube thumbnails to be empty but the is_valid function almost exactly mirrors MediaVimeoStreamWrapper::getVideoProperties() I think it might make sense to just use that for checking ids.

drewish’s picture

Yeah the point being use drupal_http_request() to fetch the URLs rather than loading them directly in simplexml_load_file()... but that's really academic since we should be using the existing function that does the request in that manner.

RobW’s picture

We decided on a solution for Media: YouTube, and can refactor that code to work here:

  /**
   * Check if a YouTube video id is valid.
   *
   * Check against the oembed stream instead of the gdata api site to
   * avoid "yt:quota too_many_recent_calls" errors.
   *
   * @return
   *   Boolean.
   */
  static public function validId($id) {
    $url = 'http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3D'. $id;
    $response = drupal_http_request($url, array('method' => 'HEAD'));
    if ($response->code != 200) {
      throw new MediaInternetValidationException("The YouTube video ID is invalid or the video was deleted.");
    }
    return TRUE;
  }

  public function parse($embedCode) {
    $patterns = array(
      // Patterns.
    );
    foreach ($patterns as $pattern) {
      preg_match($pattern, $embedCode, $matches);
      // @TODO: Parse is called often. Refactor so that valid ID is checked
      // when a video is added, but not every time the embedCode is parsed.
      if (isset($matches[1]) && self::validId($matches[1])) {
        return file_stream_wrapper_uri_normalize('youtube://v/' . $matches[1]);
      }
    }
  }
RobW’s picture

Issue summary: View changes

Changed link into [#] d.o issue.

devin carlson’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Category: Feature request » Task
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.68 KB

A patch to add a validId() method similar to the one used by Media: YouTube.

devin carlson’s picture

Status: Needs review » Fixed

Tested #10 with a variety of deleted/invalid videos and verified that users were successfully warned.

Committed #10 to Media: Vimeo 7.x-2.x.

  • Commit 34a42e2 on 7.x-2.x authored by mikeytown2, committed by Devin Carlson:
    Issue #1292360 by mikeytown2, Devin Carlson: Added video ID validation.
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.