3 # This code was based on code originally created by the LiveJournal project
4 # owned and operated by Live Journal, Inc. The code has been modified and expanded
5 # by Dreamwidth Studios, LLC. These files were originally licensed under
6 # the terms of the license supplied by Live Journal, Inc, which can
7 # currently be found at:
9 # http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
11 # In accordance with the original license, this code and all its
12 # modifications are provided under the GNU General Public License.
13 # A copy of that license can be found in the LICENSE file included as
14 # part of this distribution.
17 # DW::Hooks::EmbedWhitelist
19 # Keep a whitelist of trusted sites which we trust for certain kinds of embeds
22 # Afuna <coder.dw@afunamatata.com>
24 # Copyright (c) 2011 by Dreamwidth Studios, LLC.
26 package DW::Hooks::EmbedWhitelist;
32 # for internal use only
33 # this is used when sites may offer embeds from multiple subdomain
34 # e.g., www, www1, etc
36 my $want_domain = $_[0];
37 my $domain_from_uri = $_[1];
39 return $domain_from_uri =~ /^(?:[\w.-]*\.)?\Q$want_domain\E$/;
43 my $want_path = $_[0];
44 my $path_from_uri = $_[1];
46 return $path_from_uri =~ /^$want_path$/;
49 my %host_path_match = (
50 "bandcamp.com" => qr!^/EmbeddedPlayer/!,
51 "blip.tv" => qr!^/play/!,
53 "www.dailymotion.com" => qr!^/embed/video/!,
54 "dotsub.com" => qr!^/media/!,
56 "maps.google.com" => qr!^/maps!,
57 "ext.nicovideo.jp" => qr!^/thumb/!,
59 "www.sbs.com.au" => qr!/player/embed/!, # best guess; language parameter before /player may vary
60 "www.scribd.com" => qr!^/embeds/!,
61 "www.slideshare.net" => qr!^/slideshow/embed_code/!,
63 "player.vimeo.com" => qr!^/video/\d+$!,
66 LJ::Hooks::register_hook( 'allow_iframe_embeds', sub {
67 my ( $embed_url, %opts ) = @_;
69 return 0 unless $embed_url;
71 my $parsed_uri = URI->new( $embed_url );
73 my $uri_scheme = $parsed_uri->scheme;
74 return 0 unless $uri_scheme eq "http" || $uri_scheme eq "https";
76 my $uri_host = $parsed_uri->host;
77 my $uri_path = $parsed_uri->path; # not including query
79 my $path_regex = $host_path_match{$uri_host};
80 return 1 if $path_regex && ( $uri_path =~ $path_regex );
82 ## YouTube (http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html)
83 if ( match_subdomain( "youtube.com", $uri_host ) || match_subdomain( "youtube-nocookie.com", $uri_host ) ) {
84 return 1 if match_full_path( qr!/embed/[-_a-zA-Z0-9]{11,}!, $uri_path );
87 if ( $uri_host eq "commons.wikimedia.org" ) {
88 return 1 if $uri_path =~ m!^/wiki/File:! && $parsed_uri->query =~ m/embedplayer=yes/;