From d6f52f5939c65e904740f4f8b27a18375e1b2964 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Mon, 2 Jun 2014 01:26:23 +0200 Subject: [PATCH] File::processNew can return -1 which was not true for empty() Also, File->getEnclosure() now throws exception if not enough metadata. --- classes/File.php | 11 ++++------- lib/apiaction.php | 12 ++++++++---- lib/rssaction.php | 6 ++++-- lib/util.php | 13 +++++++------ plugins/FacebookBridge/lib/facebookclient.php | 5 +++-- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/classes/File.php b/classes/File.php index 6db1c7b806..5acf6a7b50 100644 --- a/classes/File.php +++ b/classes/File.php @@ -364,16 +364,13 @@ class File extends Managed_DataObject Event::handle('FileEnclosureMetadata', array($this, &$enclosure)); } } + if (empty($enclosure->mimetype)) { + // This means we don't know what it is, so it can't be an enclosure! + throw new ServerException('Unknown enclosure mimetype, not enough metadata'); + } return $enclosure; } - // quick back-compat hack, since there's still code using this - function isEnclosure() - { - $enclosure = $this->getEnclosure(); - return !empty($enclosure); - } - /** * Get the attachment's thumbnail record, if any. * Make sure you supply proper 'int' typed variables (or null). diff --git a/lib/apiaction.php b/lib/apiaction.php index 9f26b3b304..55860efa55 100644 --- a/lib/apiaction.php +++ b/lib/apiaction.php @@ -384,13 +384,15 @@ class ApiAction extends Action $twitter_status['attachments'] = array(); foreach ($attachments as $attachment) { - $enclosure_o=$attachment->getEnclosure(); - if ($enclosure_o) { + try { + $enclosure_o = $attachment->getEnclosure(); $enclosure = array(); $enclosure['url'] = $enclosure_o->url; $enclosure['mimetype'] = $enclosure_o->mimetype; $enclosure['size'] = $enclosure_o->size; $twitter_status['attachments'][] = $enclosure; + } catch (ServerException $e) { + // There was not enough metadata available } } } @@ -510,13 +512,15 @@ class ApiAction extends Action $enclosures = array(); foreach ($attachments as $attachment) { - $enclosure_o=$attachment->getEnclosure(); - if ($enclosure_o) { + try { + $enclosure_o = $attachment->getEnclosure(); $enclosure = array(); $enclosure['url'] = $enclosure_o->url; $enclosure['mimetype'] = $enclosure_o->mimetype; $enclosure['size'] = $enclosure_o->size; $enclosures[] = $enclosure; + } catch (ServerException $e) { + // There was not enough metadata available } } diff --git a/lib/rssaction.php b/lib/rssaction.php index dac7fa4606..684ecd6d8c 100644 --- a/lib/rssaction.php +++ b/lib/rssaction.php @@ -278,8 +278,8 @@ class Rss10Action extends Action $attachments = $notice->attachments(); if($attachments){ foreach($attachments as $attachment){ - $enclosure=$attachment->getEnclosure(); - if ($enclosure) { + try { + $enclosure = $attachment->getEnclosure(); $attribs = array('rdf:resource' => $enclosure->url); if ($enclosure->title) { $attribs['dc:title'] = $enclosure->title; @@ -294,6 +294,8 @@ class Rss10Action extends Action $attribs['enc:type'] = $enclosure->mimetype; } $this->element('enc:enclosure', $attribs); + } catch (ServerException $e) { + // There was not enough metadata available } $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url)); } diff --git a/lib/util.php b/lib/util.php index c76f7b2762..97c0a5aa57 100644 --- a/lib/util.php +++ b/lib/util.php @@ -989,22 +989,23 @@ function common_linkify($url) { $f = File::getKV('url', $longurl); - if (empty($f)) { + if (!$f instanceof File) { if (common_config('attachments', 'process_links')) { // XXX: this writes to the database. :< $f = File::processNew($longurl); } } - if (!empty($f)) { - if ($f->getEnclosure()) { + if ($f instanceof File) { + try { + $enclosure = $f->getEnclosure(); $is_attachment = true; $attachment_id = $f->id; $thumb = File_thumbnail::getKV('file_id', $f->id); - if (!empty($thumb)) { - $has_thumb = true; - } + $has_thumb = ($thumb instanceof File_thumbnail); + } catch (ServerException $e) { + // There was not enough metadata available } } diff --git a/plugins/FacebookBridge/lib/facebookclient.php b/plugins/FacebookBridge/lib/facebookclient.php index deb92e9313..5b512dfdff 100644 --- a/plugins/FacebookBridge/lib/facebookclient.php +++ b/plugins/FacebookBridge/lib/facebookclient.php @@ -651,9 +651,10 @@ class Facebookclient foreach($attachments as $attachment) { - if($enclosure = $attachment->getEnclosure()){ + try { + $enclosure = $attachment->getEnclosure(); $fbmedia = $this->getFacebookMedia($enclosure); - }else{ + } catch (ServerException $e) { $fbmedia = $this->getFacebookMedia($attachment); } if($fbmedia){