oembedhelper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Utility class to wrap basic oEmbed lookups.
  24. *
  25. * Blacklisted hosts will use an alternate lookup method:
  26. * - Twitpic
  27. *
  28. * Whitelisted hosts will use known oEmbed API endpoints:
  29. * - Flickr, YFrog
  30. *
  31. * Sites that provide discovery links will use them directly; a bug
  32. * in use of discovery links with query strings is worked around.
  33. *
  34. * Others will fall back to oohembed (unless disabled).
  35. * The API endpoint can be configured or disabled through config
  36. * as 'oohembed'/'endpoint'.
  37. */
  38. class oEmbedHelper
  39. {
  40. protected static $apiMap = array(
  41. 'flickr.com' => 'https://www.flickr.com/services/oembed/',
  42. 'youtube.com' => 'https://www.youtube.com/oembed',
  43. 'viddler.com' => 'http://lab.viddler.com/services/oembed/',
  44. 'revision3.com' => 'https://revision3.com/api/oembed/',
  45. 'vimeo.com' => 'https://vimeo.com/api/oembed.json',
  46. );
  47. protected static $functionMap = array(
  48. );
  49. /**
  50. * Perform or fake an oEmbed lookup for the given resource.
  51. *
  52. * Some known hosts are whitelisted with API endpoints where we
  53. * know they exist but autodiscovery data isn't available.
  54. * If autodiscovery links are missing and we don't recognize the
  55. * host, we'll pass it to noembed.com's public service which
  56. * will either proxy or fake info on a lot of sites.
  57. *
  58. * A few hosts are blacklisted due to known problems with oohembed,
  59. * in which case we'll look up the info another way and return
  60. * equivalent data.
  61. *
  62. * Throws exceptions on failure.
  63. *
  64. * @param string $url
  65. * @param array $params
  66. * @return object
  67. */
  68. public static function getObject($url, $params=array())
  69. {
  70. $host = parse_url($url, PHP_URL_HOST);
  71. if (substr($host, 0, 4) == 'www.') {
  72. $host = substr($host, 4);
  73. }
  74. common_log(LOG_INFO, 'Checking for oEmbed data for ' . $url);
  75. // You can fiddle with the order of discovery -- either skipping
  76. // some types or re-ordering them.
  77. $order = common_config('oembed', 'order');
  78. foreach ($order as $method) {
  79. switch ($method) {
  80. case 'built-in':
  81. common_log(LOG_INFO, 'Considering built-in oEmbed methods...');
  82. // Blacklist: systems with no oEmbed API of their own, which are
  83. // either missing from or broken on noembed.com's proxy.
  84. // we know how to look data up in another way...
  85. if (array_key_exists($host, self::$functionMap)) {
  86. common_log(LOG_INFO, 'We have a built-in method for ' . $host);
  87. $func = self::$functionMap[$host];
  88. return call_user_func($func, $url, $params);
  89. }
  90. break;
  91. case 'well-known':
  92. common_log(LOG_INFO, 'Considering well-known oEmbed endpoints...');
  93. // Whitelist: known API endpoints for sites that don't provide discovery...
  94. if (array_key_exists($host, self::$apiMap)) {
  95. $api = self::$apiMap[$host];
  96. common_log(LOG_INFO, 'Using well-known endpoint "' . $api . '" for "' . $host . '"');
  97. break 2;
  98. }
  99. break;
  100. case 'discovery':
  101. try {
  102. common_log(LOG_INFO, 'Trying to discover an oEmbed endpoint using link headers.');
  103. $api = self::discover($url);
  104. common_log(LOG_INFO, 'Found API endpoint ' . $api . ' for URL ' . $url);
  105. break 2;
  106. } catch (Exception $e) {
  107. common_log(LOG_INFO, 'Could not find an oEmbed endpoint using link headers.');
  108. // Just ignore it!
  109. }
  110. break;
  111. case 'service':
  112. $api = common_config('oembed', 'endpoint');
  113. common_log(LOG_INFO, 'Using service API endpoint ' . $api);
  114. break 2;
  115. break;
  116. }
  117. }
  118. if (empty($api)) {
  119. // TRANS: Server exception thrown in oEmbed action if no API endpoint is available.
  120. throw new ServerException(_('No oEmbed API endpoint available.'));
  121. }
  122. return self::getObjectFrom($api, $url, $params);
  123. }
  124. /**
  125. * Perform basic discovery.
  126. * @return string
  127. */
  128. static function discover($url)
  129. {
  130. // @fixme ideally skip this for non-HTML stuff!
  131. $body = self::http($url);
  132. return self::discoverFromHTML($url, $body);
  133. }
  134. /**
  135. * Partially ripped from OStatus' FeedDiscovery class.
  136. *
  137. * @param string $url source URL, used to resolve relative links
  138. * @param string $body HTML body text
  139. * @return mixed string with URL or false if no target found
  140. */
  141. static function discoverFromHTML($url, $body)
  142. {
  143. // DOMDocument::loadHTML may throw warnings on unrecognized elements,
  144. // and notices on unrecognized namespaces.
  145. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
  146. $dom = new DOMDocument();
  147. $ok = $dom->loadHTML($body);
  148. error_reporting($old);
  149. if (!$ok) {
  150. throw new oEmbedHelper_BadHtmlException();
  151. }
  152. // Ok... now on to the links!
  153. $feeds = array(
  154. 'application/json+oembed' => false,
  155. );
  156. $nodes = $dom->getElementsByTagName('link');
  157. for ($i = 0; $i < $nodes->length; $i++) {
  158. $node = $nodes->item($i);
  159. if ($node->hasAttributes()) {
  160. $rel = $node->attributes->getNamedItem('rel');
  161. $type = $node->attributes->getNamedItem('type');
  162. $href = $node->attributes->getNamedItem('href');
  163. if ($rel && $type && $href) {
  164. $rel = array_filter(explode(" ", $rel->value));
  165. $type = trim($type->value);
  166. $href = trim($href->value);
  167. if (in_array('alternate', $rel) && array_key_exists($type, $feeds) && empty($feeds[$type])) {
  168. // Save the first feed found of each type...
  169. $feeds[$type] = $href;
  170. }
  171. }
  172. }
  173. }
  174. // Return the highest-priority feed found
  175. foreach ($feeds as $type => $url) {
  176. if ($url) {
  177. return $url;
  178. }
  179. }
  180. throw new oEmbedHelper_DiscoveryException();
  181. }
  182. /**
  183. * Actually do an oEmbed lookup to a particular API endpoint.
  184. *
  185. * @param string $api oEmbed API endpoint URL
  186. * @param string $url target URL to look up info about
  187. * @param array $params
  188. * @return object
  189. */
  190. static function getObjectFrom($api, $url, $params=array())
  191. {
  192. $params['url'] = $url;
  193. $params['format'] = 'json';
  194. $key=common_config('oembed','apikey');
  195. if(isset($key)) {
  196. $params['key'] = common_config('oembed','apikey');
  197. }
  198. $data = self::json($api, $params);
  199. return self::normalize($data);
  200. }
  201. /**
  202. * Normalize oEmbed format.
  203. *
  204. * @param object $orig
  205. * @return object
  206. */
  207. static function normalize($orig)
  208. {
  209. $data = clone($orig);
  210. if (empty($data->type)) {
  211. throw new Exception('Invalid oEmbed data: no type field.');
  212. }
  213. if ($data->type == 'image') {
  214. // YFrog does this.
  215. $data->type = 'photo';
  216. }
  217. if (isset($data->thumbnail_url)) {
  218. if (!isset($data->thumbnail_width)) {
  219. // !?!?!
  220. $data->thumbnail_width = common_config('thumbnail', 'width');
  221. $data->thumbnail_height = common_config('thumbnail', 'height');
  222. }
  223. }
  224. return $data;
  225. }
  226. /**
  227. * Fetch some URL and return JSON data.
  228. *
  229. * @param string $url
  230. * @param array $params query-string params
  231. * @return object
  232. */
  233. static protected function json($url, $params=array())
  234. {
  235. $data = self::http($url, $params);
  236. return json_decode($data);
  237. }
  238. /**
  239. * Hit some web API and return data on success.
  240. * @param string $url
  241. * @param array $params
  242. * @return string
  243. */
  244. static protected function http($url, $params=array())
  245. {
  246. $client = HTTPClient::start();
  247. if ($params) {
  248. $query = http_build_query($params, null, '&');
  249. if (strpos($url, '?') === false) {
  250. $url .= '?' . $query;
  251. } else {
  252. $url .= '&' . $query;
  253. }
  254. }
  255. $response = $client->get($url);
  256. if ($response->isOk()) {
  257. return $response->getBody();
  258. } else {
  259. throw new Exception('Bad HTTP response code: ' . $response->getStatus());
  260. }
  261. }
  262. }
  263. class oEmbedHelper_Exception extends Exception
  264. {
  265. public function __construct($message = "", $code = 0, $previous = null)
  266. {
  267. parent::__construct($message, $code);
  268. }
  269. }
  270. class oEmbedHelper_BadHtmlException extends oEmbedHelper_Exception
  271. {
  272. function __construct($previous=null)
  273. {
  274. return parent::__construct('Bad HTML in discovery data.', 0, $previous);
  275. }
  276. }
  277. class oEmbedHelper_DiscoveryException extends oEmbedHelper_Exception
  278. {
  279. function __construct($previous=null)
  280. {
  281. return parent::__construct('No oEmbed discovery data.', 0, $previous);
  282. }
  283. }