diff --git a/classes/File.php b/classes/File.php index b834036405..029ff487a4 100644 --- a/classes/File.php +++ b/classes/File.php @@ -352,30 +352,21 @@ class File extends Managed_DataObject function getEnclosure(){ $enclosure = (object) array(); - $enclosure->title=$this->title; - $enclosure->url=$this->url; - $enclosure->title=$this->title; - $enclosure->date=$this->date; - $enclosure->modified=$this->modified; - $enclosure->size=$this->size; - $enclosure->mimetype=$this->mimetype; - - if (!isset($this->filename)) { - $notEnclosureMimeTypes = array(null,'text/html','application/xhtml+xml'); - $mimetype = $this->mimetype; - if($mimetype != null){ - $mimetype = strtolower($this->mimetype); - } - $semicolon = strpos($mimetype,';'); - if($semicolon){ - $mimetype = substr($mimetype,0,$semicolon); - } - if (in_array($mimetype, $notEnclosureMimeTypes)) { - Event::handle('FileEnclosureMetadata', array($this, &$enclosure)); - } + foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype') as $key) { + $enclosure->$key = $this->$key; } - if (empty($enclosure->mimetype)) { - // This means we don't know what it is, so it can't be an enclosure! + + $needMoreMetadataMimetypes = array(null, 'text/html', 'application/xhtml+xml'); + + if (!isset($this->filename) && in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) { + // This fetches enclosure metadata for non-local links with unset/HTML mimetypes, + // which may be enriched through oEmbed or similar (implemented as plugins) + Event::handle('FileEnclosureMetadata', array($this, &$enclosure)); + } + if (empty($enclosure->mimetype) || in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) { + // This means we either don't know what it is, so it can't + // be shown as an enclosure, or it is an HTML link which + // does not link to a resource with further metadata. throw new ServerException('Unknown enclosure mimetype, not enough metadata'); } return $enclosure; diff --git a/lib/util.php b/lib/util.php index fd89bb491c..08a0cdea2f 100644 --- a/lib/util.php +++ b/lib/util.php @@ -1820,6 +1820,15 @@ function common_get_mime_media($type) return strtolower($tmp[0]); } +function common_bare_mime($mimetype) +{ + $mimetype = mb_strtolower($mimetype); + if ($semicolon = mb_strpos($mimetype, ';')) { + $mimetype = mb_substr($mimetype, 0, $semicolon); + } + return $mimetype; +} + function common_mime_type_match($type, $avail) { if(array_key_exists($type, $avail)) {