123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- defined('GNUSOCIAL') || die();
- class EmbedHelper
- {
-
- public static function getObject($url)
- {
- common_log(LOG_INFO, 'Checking for remote URL metadata for ' . $url);
-
- $metadata = new stdClass();
- if (Event::handle('GetRemoteUrlMetadata', array($url, &$metadata))) {
-
-
- $client = new HTTPClient();
- $response = $client->get($url);
- if (!$response->isOk()) {
-
- throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus());
- }
- $body = $response->getBody();
-
-
- $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
-
-
- $utf8_evidence = false;
- $unicode_check_dom = new DOMDocument();
- $ok = $unicode_check_dom->loadHTML($body);
- if (!$ok) {
- throw new EmbedHelper_BadHtmlException();
- }
- $metaNodes = $unicode_check_dom->getElementsByTagName('meta');
- foreach ($metaNodes as $metaNode) {
-
- if (stristr($metaNode->getAttribute('http-equiv'), 'content-type')
- && stristr($metaNode->getAttribute('content'), 'utf-8')) {
- $utf8_evidence = true;
- break;
- } elseif (stristr($metaNode->getAttribute('charset'), 'utf-8')) {
- $utf8_evidence = true;
- break;
- }
- }
- unset($unicode_check_dom);
-
- if (stristr($response->getHeader('Content-Type'), 'utf-8')) {
- $utf8_evidence = true;
- }
-
-
- $utf8_tag = $utf8_evidence ? '<?xml encoding="utf-8" ?>' : '';
- $dom = new DOMDocument();
- $ok = $dom->loadHTML($utf8_tag.$body);
- unset($body);
- error_reporting($old);
- if (!$ok) {
- throw new EmbedHelper_BadHtmlException();
- }
- Event::handle('GetRemoteUrlMetadataFromDom', array($url, $dom, &$metadata));
- }
- return self::normalize($metadata);
- }
-
- public static function normalize(stdClass $data)
- {
- if (empty($data->type)) {
- throw new Exception('Invalid oEmbed data: no type field.');
- }
- if ($data->type == 'image') {
-
- $data->type = 'photo';
- }
- if (isset($data->thumbnail_url)) {
- if (!isset($data->thumbnail_width)) {
-
- $data->thumbnail_width = common_config('thumbnail', 'width');
- $data->thumbnail_height = common_config('thumbnail', 'height');
- }
- }
- return $data;
- }
- }
- class EmbedHelper_Exception extends Exception
- {
- public function __construct($message = "", $code = 0, $previous = null)
- {
- parent::__construct($message, $code, $previous);
- }
- }
- class EmbedHelper_BadHtmlException extends EmbedHelper_Exception
- {
- public function __construct($previous=null)
- {
- return parent::__construct('Bad HTML in discovery data.', 0, $previous);
- }
- }
- class EmbedHelper_DiscoveryException extends EmbedHelper_Exception
- {
- public function __construct($previous=null)
- {
- return parent::__construct('No oEmbed discovery data.', 0, $previous);
- }
- }
|