embedhelper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * OembedPlugin implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Mikael Nordfeldth
  21. * @author hannes
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Utility class to wrap basic embed lookups.
  29. *
  30. * Blacklisted hosts will use an alternate lookup method:
  31. * - Twitpic
  32. *
  33. * Whitelisted hosts will use known embed API endpoints:
  34. * - Flickr, YFrog
  35. *
  36. * Sites that provide discovery links will use them directly; a bug
  37. * in use of discovery links with query strings is worked around.
  38. *
  39. * Others will fall back to oohembed (unless disabled).
  40. * The API endpoint can be configured or disabled through config
  41. * as 'oohembed'/'endpoint'.
  42. *
  43. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  44. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  45. */
  46. class EmbedHelper
  47. {
  48. /**
  49. * Perform or fake an oEmbed lookup for the given resource.
  50. *
  51. * Some known hosts are whitelisted with API endpoints where we
  52. * know they exist but autodiscovery data isn't available.
  53. * If autodiscovery links are missing and we don't recognize the
  54. * host, we'll pass it to noembed.com's public service which
  55. * will either proxy or fake info on a lot of sites.
  56. *
  57. * A few hosts are blacklisted due to known problems with oohembed,
  58. * in which case we'll look up the info another way and return
  59. * equivalent data.
  60. *
  61. * Throws exceptions on failure.
  62. *
  63. * @param string $url
  64. * @return object
  65. * @throws EmbedHelper_BadHtmlException
  66. * @throws HTTP_Request2_Exception
  67. */
  68. public static function getObject($url)
  69. {
  70. common_log(LOG_INFO, 'Checking for remote URL metadata for ' . $url);
  71. // TODO: Make this class something like UrlMetadata, or use a dataobject?
  72. $metadata = new stdClass();
  73. if (Event::handle('GetRemoteUrlMetadata', array($url, &$metadata))) {
  74. // If that event didn't return anything, try downloading the body and parse it
  75. // don't use quickGet since we want to check Content-Type header for utf-8
  76. $client = new HTTPClient();
  77. $response = $client->get($url);
  78. if (!$response->isOk()) {
  79. // TRANS: Exception. %s is the URL we tried to GET.
  80. throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
  81. }
  82. $body = $response->getBody();
  83. // DOMDocument::loadHTML may throw warnings on unrecognized elements,
  84. // and notices on unrecognized namespaces.
  85. $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
  86. // DOMDocument assumes ISO-8859-1 per HTML spec
  87. // use UTF-8 if we find any evidence of that encoding
  88. $utf8_evidence = false;
  89. $unicode_check_dom = new DOMDocument();
  90. $ok = $unicode_check_dom->loadHTML($body);
  91. if (!$ok) {
  92. throw new EmbedHelper_BadHtmlException();
  93. }
  94. $metaNodes = $unicode_check_dom->getElementsByTagName('meta');
  95. foreach ($metaNodes as $metaNode) {
  96. // case in-sensitive since Content-type and utf-8 can be written in many ways
  97. if (stristr($metaNode->getAttribute('http-equiv'), 'content-type')
  98. && stristr($metaNode->getAttribute('content'), 'utf-8')) {
  99. $utf8_evidence = true;
  100. break;
  101. } elseif (stristr($metaNode->getAttribute('charset'), 'utf-8')) {
  102. $utf8_evidence = true;
  103. break;
  104. }
  105. }
  106. unset($unicode_check_dom);
  107. // The Content-Type HTTP response header overrides encoding metatags in DOM
  108. if (stristr($response->getHeader('Content-Type'), 'utf-8')) {
  109. $utf8_evidence = true;
  110. }
  111. // add utf-8 encoding prolog if we have reason to believe this is utf-8 content
  112. // DOMDocument('1.0', 'UTF-8') does not work!
  113. $utf8_tag = $utf8_evidence ? '<?xml encoding="utf-8" ?>' : '';
  114. $dom = new DOMDocument();
  115. $ok = $dom->loadHTML($utf8_tag.$body);
  116. unset($body); // storing the DOM in memory is enough...
  117. error_reporting($old);
  118. if (!$ok) {
  119. throw new EmbedHelper_BadHtmlException();
  120. }
  121. Event::handle('GetRemoteUrlMetadataFromDom', array($url, $dom, &$metadata));
  122. }
  123. return self::normalize($metadata);
  124. }
  125. /**
  126. * Normalize oEmbed format.
  127. *
  128. * @param stdClass $data
  129. * @return object
  130. * @throws Exception
  131. */
  132. public static function normalize(stdClass $data)
  133. {
  134. if (empty($data->type)) {
  135. throw new Exception('Invalid oEmbed data: no type field.');
  136. }
  137. if ($data->type == 'image') {
  138. // YFrog does this.
  139. $data->type = 'photo';
  140. }
  141. if (isset($data->thumbnail_url)) {
  142. if (!isset($data->thumbnail_width)) {
  143. // !?!?!
  144. $data->thumbnail_width = common_config('thumbnail', 'width');
  145. $data->thumbnail_height = common_config('thumbnail', 'height');
  146. }
  147. }
  148. return $data;
  149. }
  150. }
  151. class EmbedHelper_Exception extends Exception
  152. {
  153. public function __construct($message = "", $code = 0, $previous = null)
  154. {
  155. parent::__construct($message, $code, $previous);
  156. }
  157. }
  158. class EmbedHelper_BadHtmlException extends EmbedHelper_Exception
  159. {
  160. public function __construct($previous=null)
  161. {
  162. return parent::__construct('Bad HTML in discovery data.', 0, $previous);
  163. }
  164. }
  165. class EmbedHelper_DiscoveryException extends EmbedHelper_Exception
  166. {
  167. public function __construct($previous=null)
  168. {
  169. return parent::__construct('No oEmbed discovery data.', 0, $previous);
  170. }
  171. }