const PREVIEW_LARGE = 2;
const PREVIEW_SMALL = 3;
- /**
- * Fetches attachment data that were generated the old way
- *
- * @param string $body Message body
- * @return array
- * 'type' -> Message type ('link', 'video', 'photo')
- * 'text' -> Text before the shared message
- * 'after' -> Text after the shared message
- * 'image' -> Preview image of the message
- * 'url' -> Url to the attached message
- * 'title' -> Title of the attachment
- * 'description' -> Description of the attachment
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- private static function getOldAttachmentData(string $body): array
- {
- $post = [];
-
- // Simplify image codes
- $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
-
- if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism", $body, $attached, PREG_SET_ORDER)) {
- foreach ($attached as $data) {
- if (!in_array($data[1], ['type-link', 'type-video', 'type-photo'])) {
- continue;
- }
-
- $post['type'] = substr($data[1], 5);
-
- $pos = strpos($body, $data[0]);
- if ($pos > 0) {
- $post['text'] = trim(substr($body, 0, $pos));
- $post['after'] = trim(substr($body, $pos + strlen($data[0])));
- } else {
- $post['text'] = trim(str_replace($data[0], '', $body));
- $post['after'] = '';
- }
-
- $attacheddata = $data[2];
-
- if (preg_match("/\[img\](.*?)\[\/img\]/ism", $attacheddata, $matches)) {
-
- $picturedata = Images::getInfoFromURLCached($matches[1]);
-
- if ($picturedata) {
- if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
- $post['image'] = $matches[1];
- } else {
- $post['preview'] = $matches[1];
- }
- }
- }
-
- if (preg_match("/\[bookmark\=(.*?)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
- $post['url'] = $matches[1];
- $post['title'] = $matches[2];
- }
- if (!empty($post['url']) && (in_array($post['type'], ['link', 'video']))
- && preg_match("/\[url\=(.*?)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
- $post['url'] = $matches[1];
- }
-
- // Search for description
- if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches)) {
- $post['description'] = $matches[1];
- }
- }
- }
- return $post;
- }
-
/**
* Fetches attachment data that were generated with the "attachment" element
*
if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
DI::profiler()->stopRecording();
- return self::getOldAttachmentData($body);
+ return [];
}
$attributes = $match[2];
return $data;
}
- public static function getAttachedData(string $body, array $item = []): array
- {
- /*
- - text:
- - type: link, video, photo
- - title:
- - url:
- - image:
- - description:
- - (thumbnail)
- */
-
- DI::profiler()->startRecording('rendering');
- $has_title = !empty($item['title']);
- $plink = $item['plink'] ?? '';
- $post = self::getAttachmentData($body);
-
- // Get all linked images with alternative image description
- if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- if ($id = Photo::getIdForName($picture[1])) {
- $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2], 'id' => $id];
- } else {
- $post['remote_images'][] = ['url' => $picture[1], 'description' => $picture[2]];
- }
- }
- if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
- $post['image_description'] = $post['images'][0]['description'];
- }
- }
-
- if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- if ($id = Photo::getIdForName($picture[1])) {
- $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => '', 'id' => $id];
- } else {
- $post['remote_images'][] = ['url' => $picture[1], 'description' => ''];
- }
- }
- }
-
- if (!isset($post['type'])) {
- $post['text'] = $body;
- }
-
- // Simplify image codes
- $post['text'] = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $post['text']);
- $post['text'] = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $post['text']);
-
- // if nothing is found, it maybe having an image.
- if (!isset($post['type'])) {
- if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $post['text'], $pictures, PREG_SET_ORDER)) {
- if ((count($pictures) == 1) && !$has_title && !Photo::isLocal($pictures[0][2])) {
- if (!empty($item['object-type']) && ($item['object-type'] == Activity\ObjectType::IMAGE)) {
- // Replace the preview picture with the real picture
- $url = str_replace('-1.', '-0.', $pictures[0][2]);
- $data = ['url' => $url, 'type' => 'photo'];
- } else {
- // Checking, if the link goes to a picture
- $data = ParseUrl::getSiteinfoCached($pictures[0][1]);
- }
-
- // Workaround:
- // Sometimes photo posts to the own album are not detected at the start.
- // So we seem to cannot use the cache for these cases. That's strange.
- if (($data['type'] != 'photo') && strstr($pictures[0][1], '/photos/')) {
- $data = ParseUrl::getSiteinfo($pictures[0][1]);
- }
-
- if ($data['type'] == 'photo') {
- $post['type'] = 'photo';
- if (isset($data['images'][0])) {
- $post['image'] = $data['images'][0]['src'];
- $post['url'] = $data['url'];
- } else {
- $post['image'] = $data['url'];
- }
-
- $post['preview'] = $pictures[0][2];
- $post['text'] = trim(str_replace($pictures[0][0], '', $post['text']));
- } else {
- $imgdata = Images::getInfoFromURLCached($pictures[0][1]);
- if (($imgdata) && substr($imgdata['mime'], 0, 6) == 'image/') {
- $post['type'] = 'photo';
- $post['image'] = $pictures[0][1];
- $post['preview'] = $pictures[0][2];
- $post['text'] = trim(str_replace($pictures[0][0], '', $post['text']));
- }
- }
- } elseif (count($pictures) > 0) {
- if (count($pictures) > 4) {
- $post['type'] = 'link';
- $post['url'] = $plink;
- } else {
- $post['type'] = 'photo';
- }
-
- $post['image'] = $pictures[0][2];
-
- foreach ($pictures as $picture) {
- $post['text'] = trim(str_replace($picture[0], '', $post['text']));
- }
- }
- } elseif (preg_match_all("(\[img\](.*?)\[\/img\])ism", $post['text'], $pictures, PREG_SET_ORDER)) {
- if ($has_title) {
- $post['type'] = 'link';
- $post['url'] = $plink;
- } else {
- $post['type'] = 'photo';
- }
-
- $post['image'] = $pictures[0][1];
- foreach ($pictures as $picture) {
- $post['text'] = trim(str_replace($picture[0], '', $post['text']));
- }
- }
-
- // Test for the external links
- preg_match_all("(\[url\](.*?)\[\/url\])ism", $post['text'], $links1, PREG_SET_ORDER);
- preg_match_all("(\[url\=(.*?)\].*?\[\/url\])ism", $post['text'], $links2, PREG_SET_ORDER);
-
- $links = array_merge($links1, $links2);
-
- // If there is only a single one, then use it.
- // This should cover link posts via API.
- if ((count($links) == 1) && !isset($post['preview']) && !$has_title) {
- $post['type'] = 'link';
- $post['url'] = $links[0][1];
- }
-
- // Simplify "video" element
- $post['text'] = preg_replace('(\[video.*?\ssrc\s?=\s?([^\s\]]+).*?\].*?\[/video\])ism', '[video]$1[/video]', $post['text']);
-
- // Now count the number of external media links
- preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $post['text'], $links1, PREG_SET_ORDER);
- preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $post['text'], $links2, PREG_SET_ORDER);
- preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $post['text'], $links3, PREG_SET_ORDER);
- preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $post['text'], $links4, PREG_SET_ORDER);
-
- // Add them to the other external links
- $links = array_merge($links, $links1, $links2, $links3, $links4);
-
- // Are there more than one?
- if (count($links) > 1) {
- // The post will be the type "text", which means a blog post
- unset($post['type']);
- $post['url'] = $plink;
- }
-
- if (!isset($post['type'])) {
- $post['type'] = 'text';
- }
-
- if (($post['type'] == 'photo') && empty($post['images']) && !empty($post['remote_images'])) {
- $post['images'] = $post['remote_images'];
- $post['image'] = $post['images'][0]['url'];
- if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
- $post['image_description'] = $post['images'][0]['description'];
- }
- }
- unset($post['remote_images']);
- } elseif (isset($post['url']) && ($post['type'] == 'video')) {
- $data = ParseUrl::getSiteinfoCached($post['url']);
-
- if (isset($data['images'][0])) {
- $post['image'] = $data['images'][0]['src'];
- }
- } elseif (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $post['text'], $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- $post['text'] = trim(str_replace($picture[0], '', $post['text']));
- }
- }
-
- DI::profiler()->stopRecording();
- return $post;
- }
-
/**
* Remove [attachment] BBCode and replaces it with a regular [url]
*
*/
public function createFromUriId(int $uriId, array $history = []): \Friendica\Object\Api\Mastodon\Card
{
- $item = Post::selectFirst(['body'], ['uri-id' => $uriId]);
- if (!empty($item['body'])) {
- $data = BBCode::getAttachmentData($item['body']);
- } else {
- $data = [];
+ $media = Post\Media::getByURIId($uriId, [Post\Media::HTML]);
+ if (empty($media) && empty($media[0]['description']) && !empty($media[0]['image']) && !empty($media[0]['preview'])) {
+ return new \Friendica\Object\Api\Mastodon\Card([], $history);
}
- foreach (Post\Media::getByURIId($uriId, [Post\Media::HTML]) as $attached) {
- if ((empty($data['url']) || Strings::compareLink($data['url'], $attached['url'])) &&
- (!empty($attached['description']) || !empty($attached['image']) || !empty($attached['preview']))) {
- $parts = parse_url($attached['url']);
- if (!empty($parts['scheme']) && !empty($parts['host'])) {
- if (empty($attached['publisher-name'])) {
- $attached['publisher-name'] = $parts['host'];
- }
- if (empty($attached['publisher-url']) || empty(parse_url($attached['publisher-url'], PHP_URL_SCHEME))) {
- $attached['publisher-url'] = $parts['scheme'] . '://' . $parts['host'];
+ $parts = parse_url($media[0]['url']);
+ if (!empty($parts['scheme']) && !empty($parts['host'])) {
+ if (empty($media[0]['publisher-name'])) {
+ $media[0]['publisher-name'] = $parts['host'];
+ }
+ if (empty($media[0]['publisher-url']) || empty(parse_url($media[0]['publisher-url'], PHP_URL_SCHEME))) {
+ $media[0]['publisher-url'] = $parts['scheme'] . '://' . $parts['host'];
- if (!empty($parts['port'])) {
- $attached['publisher-url'] .= ':' . $parts['port'];
- }
- }
+ if (!empty($parts['port'])) {
+ $media[0]['publisher-url'] .= ':' . $parts['port'];
}
-
- $data['url'] = $attached['url'];
- $data['title'] = $attached['name'];
- $data['description'] = $attached['description'];
- $data['type'] = 'link';
- $data['author_name'] = $attached['author-name'];
- $data['author_url'] = $attached['author-url'];
- $data['provider_name'] = $attached['publisher-name'];
- $data['provider_url'] = $attached['publisher-url'];
- $data['image'] = $attached['preview'];
- $data['width'] = $attached['preview-width'];
- $data['height'] = $attached['preview-height'];
- $data['blurhash'] = $attached['blurhash'];
}
}
+ $data = [];
+
+ $data['url'] = $media[0]['url'];
+ $data['title'] = $media[0]['name'];
+ $data['description'] = $media[0]['description'];
+ $data['type'] = 'link';
+ $data['author_name'] = $media[0]['author-name'];
+ $data['author_url'] = $media[0]['author-url'];
+ $data['provider_name'] = $media[0]['publisher-name'];
+ $data['provider_url'] = $media[0]['publisher-url'];
+ $data['image'] = $media[0]['preview'];
+ $data['width'] = $media[0]['preview-width'];
+ $data['height'] = $media[0]['preview-height'];
+ $data['blurhash'] = $media[0]['blurhash'];
+
return new \Friendica\Object\Api\Mastodon\Card($data, $history);
}
}