2010-11-13 01:41:35 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2008-2010, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
if (!defined('GNUSOCIAL')) { exit(1); }
|
2010-11-13 01:41:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-15 19:25:38 +00:00
|
|
|
* Utility class to wrap basic oEmbed lookups.
|
2010-11-13 01:41:35 +00:00
|
|
|
*
|
|
|
|
* Blacklisted hosts will use an alternate lookup method:
|
|
|
|
* - Twitpic
|
|
|
|
*
|
|
|
|
* Whitelisted hosts will use known oEmbed API endpoints:
|
|
|
|
* - Flickr, YFrog
|
|
|
|
*
|
|
|
|
* Sites that provide discovery links will use them directly; a bug
|
|
|
|
* in use of discovery links with query strings is worked around.
|
|
|
|
*
|
|
|
|
* Others will fall back to oohembed (unless disabled).
|
|
|
|
* The API endpoint can be configured or disabled through config
|
|
|
|
* as 'oohembed'/'endpoint'.
|
|
|
|
*/
|
|
|
|
class oEmbedHelper
|
|
|
|
{
|
|
|
|
protected static $apiMap = array(
|
2015-01-17 11:06:08 +00:00
|
|
|
'flickr.com' => 'https://www.flickr.com/services/oembed/',
|
|
|
|
'youtube.com' => 'https://www.youtube.com/oembed',
|
2011-04-06 22:57:33 +01:00
|
|
|
'viddler.com' => 'http://lab.viddler.com/services/oembed/',
|
2015-01-17 11:06:08 +00:00
|
|
|
'revision3.com' => 'https://revision3.com/api/oembed/',
|
|
|
|
'vimeo.com' => 'https://vimeo.com/api/oembed.json',
|
2010-11-13 01:41:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform or fake an oEmbed lookup for the given resource.
|
|
|
|
*
|
|
|
|
* Some known hosts are whitelisted with API endpoints where we
|
|
|
|
* know they exist but autodiscovery data isn't available.
|
|
|
|
* If autodiscovery links are missing and we don't recognize the
|
2011-09-30 20:51:23 +01:00
|
|
|
* host, we'll pass it to noembed.com's public service which
|
2010-11-13 01:41:35 +00:00
|
|
|
* will either proxy or fake info on a lot of sites.
|
|
|
|
*
|
|
|
|
* A few hosts are blacklisted due to known problems with oohembed,
|
|
|
|
* in which case we'll look up the info another way and return
|
|
|
|
* equivalent data.
|
|
|
|
*
|
|
|
|
* Throws exceptions on failure.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param array $params
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public static function getObject($url, $params=array())
|
|
|
|
{
|
2015-11-30 01:06:04 +00:00
|
|
|
common_log(LOG_INFO, 'Checking for remote URL metadata for ' . $url);
|
2010-11-13 01:41:35 +00:00
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
// TODO: Make this class something like UrlMetadata, or use a dataobject?
|
|
|
|
$metadata = new stdClass();
|
2011-04-06 22:57:33 +01:00
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
if (Event::handle('GetRemoteUrlMetadata', array($url, &$metadata))) {
|
|
|
|
// If that event didn't return anything, try downloading the body and parse it
|
|
|
|
$body = HTTPClient::quickGet($url);
|
2011-04-06 22:57:33 +01:00
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
// DOMDocument::loadHTML may throw warnings on unrecognized elements,
|
|
|
|
// and notices on unrecognized namespaces.
|
|
|
|
$old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
|
|
|
|
$dom = new DOMDocument();
|
|
|
|
$ok = $dom->loadHTML($body);
|
|
|
|
unset($body); // storing the DOM in memory is enough...
|
|
|
|
error_reporting($old);
|
2010-11-13 01:41:35 +00:00
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
if (!$ok) {
|
|
|
|
throw new oEmbedHelper_BadHtmlException();
|
2010-11-15 19:25:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
Event::handle('GetRemoteUrlMetadataFromDom', array($url, $dom, &$metadata));
|
2010-11-15 19:25:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 01:06:04 +00:00
|
|
|
return self::normalize($metadata);
|
2015-11-30 00:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partially ripped from OStatus' FeedDiscovery class.
|
|
|
|
*
|
|
|
|
* @param string $url source URL, used to resolve relative links
|
|
|
|
* @param string $body HTML body text
|
|
|
|
* @return mixed string with URL or false if no target found
|
|
|
|
*/
|
2015-11-30 01:06:04 +00:00
|
|
|
static function oEmbedEndpointFromHTML(DOMDocument $dom)
|
2015-11-30 00:28:18 +00:00
|
|
|
{
|
2010-11-15 19:25:38 +00:00
|
|
|
// Ok... now on to the links!
|
|
|
|
$feeds = array(
|
|
|
|
'application/json+oembed' => false,
|
|
|
|
);
|
|
|
|
|
|
|
|
$nodes = $dom->getElementsByTagName('link');
|
|
|
|
for ($i = 0; $i < $nodes->length; $i++) {
|
|
|
|
$node = $nodes->item($i);
|
|
|
|
if ($node->hasAttributes()) {
|
|
|
|
$rel = $node->attributes->getNamedItem('rel');
|
|
|
|
$type = $node->attributes->getNamedItem('type');
|
|
|
|
$href = $node->attributes->getNamedItem('href');
|
|
|
|
if ($rel && $type && $href) {
|
|
|
|
$rel = array_filter(explode(" ", $rel->value));
|
|
|
|
$type = trim($type->value);
|
|
|
|
$href = trim($href->value);
|
|
|
|
|
|
|
|
if (in_array('alternate', $rel) && array_key_exists($type, $feeds) && empty($feeds[$type])) {
|
|
|
|
// Save the first feed found of each type...
|
|
|
|
$feeds[$type] = $href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the highest-priority feed found
|
|
|
|
foreach ($feeds as $type => $url) {
|
|
|
|
if ($url) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new oEmbedHelper_DiscoveryException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually do an oEmbed lookup to a particular API endpoint.
|
|
|
|
*
|
|
|
|
* @param string $api oEmbed API endpoint URL
|
2010-11-13 01:41:35 +00:00
|
|
|
* @param string $url target URL to look up info about
|
|
|
|
* @param array $params
|
|
|
|
* @return object
|
|
|
|
*/
|
2015-11-30 01:06:04 +00:00
|
|
|
static function getOembedFrom($api, $url, $params=array())
|
2010-11-13 01:41:35 +00:00
|
|
|
{
|
2015-11-30 01:06:04 +00:00
|
|
|
if (empty($api)) {
|
|
|
|
// TRANS: Server exception thrown in oEmbed action if no API endpoint is available.
|
|
|
|
throw new ServerException(_('No oEmbed API endpoint available.'));
|
|
|
|
}
|
2010-11-15 19:25:38 +00:00
|
|
|
$params['url'] = $url;
|
|
|
|
$params['format'] = 'json';
|
2012-08-29 20:36:55 +01:00
|
|
|
$key=common_config('oembed','apikey');
|
|
|
|
if(isset($key)) {
|
|
|
|
$params['key'] = common_config('oembed','apikey');
|
|
|
|
}
|
2016-01-13 16:03:38 +00:00
|
|
|
|
|
|
|
$oembed_data = HTTPClient::quickGetJson($api, $params);
|
|
|
|
|
|
|
|
// purify html
|
|
|
|
if(isset($oembed_data->html)) {
|
|
|
|
$oembed_data->html = common_purify($oembed_data->html);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $oembed_data;
|
2010-11-15 19:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize oEmbed format.
|
|
|
|
*
|
|
|
|
* @param object $orig
|
|
|
|
* @return object
|
|
|
|
*/
|
2015-11-30 01:06:04 +00:00
|
|
|
static function normalize(stdClass $data)
|
2010-11-15 19:25:38 +00:00
|
|
|
{
|
|
|
|
if (empty($data->type)) {
|
|
|
|
throw new Exception('Invalid oEmbed data: no type field.');
|
2010-11-13 01:41:35 +00:00
|
|
|
}
|
2010-11-15 19:25:38 +00:00
|
|
|
if ($data->type == 'image') {
|
|
|
|
// YFrog does this.
|
|
|
|
$data->type = 'photo';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data->thumbnail_url)) {
|
|
|
|
if (!isset($data->thumbnail_width)) {
|
|
|
|
// !?!?!
|
2014-04-21 19:39:28 +01:00
|
|
|
$data->thumbnail_width = common_config('thumbnail', 'width');
|
|
|
|
$data->thumbnail_height = common_config('thumbnail', 'height');
|
2010-11-13 01:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 19:25:38 +00:00
|
|
|
return $data;
|
2010-11-13 01:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 19:25:38 +00:00
|
|
|
class oEmbedHelper_Exception extends Exception
|
|
|
|
{
|
2010-12-20 23:28:21 +00:00
|
|
|
public function __construct($message = "", $code = 0, $previous = null)
|
|
|
|
{
|
2010-12-20 23:32:43 +00:00
|
|
|
parent::__construct($message, $code);
|
2010-12-20 23:28:21 +00:00
|
|
|
}
|
2010-11-15 19:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class oEmbedHelper_BadHtmlException extends oEmbedHelper_Exception
|
2010-11-13 01:41:35 +00:00
|
|
|
{
|
2010-11-15 19:25:38 +00:00
|
|
|
function __construct($previous=null)
|
2010-11-13 01:41:35 +00:00
|
|
|
{
|
2010-11-15 19:25:38 +00:00
|
|
|
return parent::__construct('Bad HTML in discovery data.', 0, $previous);
|
2010-11-13 01:41:35 +00:00
|
|
|
}
|
2010-11-15 19:25:38 +00:00
|
|
|
}
|
2010-11-13 01:41:35 +00:00
|
|
|
|
2010-11-15 19:25:38 +00:00
|
|
|
class oEmbedHelper_DiscoveryException extends oEmbedHelper_Exception
|
|
|
|
{
|
|
|
|
function __construct($previous=null)
|
2010-11-13 01:41:35 +00:00
|
|
|
{
|
2010-11-15 19:25:38 +00:00
|
|
|
return parent::__construct('No oEmbed discovery data.', 0, $previous);
|
2010-11-13 01:41:35 +00:00
|
|
|
}
|
2016-01-13 16:03:38 +00:00
|
|
|
}
|