Fix code style Media.php

This commit is contained in:
Michael 2025-01-05 08:29:10 +00:00
parent 503e33a683
commit 8d1971e0b2

View file

@ -90,6 +90,7 @@ class Media
} }
$media['url'] = Network::sanitizeUrl($media['url']); $media['url'] = Network::sanitizeUrl($media['url']);
$media = self::unsetEmptyFields($media); $media = self::unsetEmptyFields($media);
$media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media); $media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media);
@ -221,9 +222,9 @@ class Media
$imagedata = Images::getInfoFromURLCached($media['url'], empty($media['description'])); $imagedata = Images::getInfoFromURLCached($media['url'], empty($media['description']));
if ($imagedata) { if ($imagedata) {
$media['mimetype'] = $imagedata['mime']; $media['mimetype'] = $imagedata['mime'];
$media['size'] = $imagedata['size']; $media['size'] = $imagedata['size'];
$media['width'] = $imagedata[0]; $media['width'] = $imagedata[0];
$media['height'] = $imagedata[1]; $media['height'] = $imagedata[1];
$media['blurhash'] = $imagedata['blurhash'] ?? null; $media['blurhash'] = $imagedata['blurhash'] ?? null;
if (!empty($imagedata['description']) && empty($media['description'])) { if (!empty($imagedata['description']) && empty($media['description'])) {
$media['description'] = $imagedata['description']; $media['description'] = $imagedata['description'];
@ -279,13 +280,13 @@ class Media
// When the original picture is potentially animated but the preview isn't, we override the preview // When the original picture is potentially animated but the preview isn't, we override the preview
if (in_array($media['mimetype'] ?? '', ['image/gif', 'image/png']) && !in_array($imagedata['mime'], ['image/gif', 'image/png'])) { if (in_array($media['mimetype'] ?? '', ['image/gif', 'image/png']) && !in_array($imagedata['mime'], ['image/gif', 'image/png'])) {
$media['preview'] = $media['url']; $media['preview'] = $media['url'];
$media['preview-width'] = $media['width']; $media['preview-width'] = $media['width'];
$media['preview-height'] = $media['height']; $media['preview-height'] = $media['height'];
return $media; return $media;
} }
$media['preview-width'] = $imagedata[0]; $media['preview-width'] = $imagedata[0];
$media['preview-height'] = $imagedata[1]; $media['preview-height'] = $imagedata[1];
} }
@ -339,21 +340,21 @@ class Media
$gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]); $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
} }
$media['type'] = self::ACTIVITY; $media['type'] = self::ACTIVITY;
$media['media-uri-id'] = $item['uri-id']; $media['media-uri-id'] = $item['uri-id'];
$media['height'] = null; $media['height'] = null;
$media['width'] = null; $media['width'] = null;
$media['preview'] = null; $media['preview'] = null;
$media['preview-height'] = null; $media['preview-height'] = null;
$media['preview-width'] = null; $media['preview-width'] = null;
$media['blurhash'] = null; $media['blurhash'] = null;
$media['description'] = $item['body']; $media['description'] = $item['body'];
$media['name'] = $item['title']; $media['name'] = $item['title'];
$media['author-url'] = $item['author-link']; $media['author-url'] = $item['author-link'];
$media['author-name'] = $item['author-name']; $media['author-name'] = $item['author-name'];
$media['author-image'] = $contact['avatar'] ?? $item['author-avatar']; $media['author-image'] = $contact['avatar'] ?? $item['author-avatar'];
$media['publisher-url'] = $gserver['url'] ?? null; $media['publisher-url'] = $gserver['url'] ?? null;
$media['publisher-name'] = $gserver['site_name'] ?? null; $media['publisher-name'] = $gserver['site_name'] ?? null;
$media['publisher-image'] = null; $media['publisher-image'] = null;
Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]); Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
@ -381,21 +382,21 @@ class Media
$gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]); $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
} }
$media['type'] = self::ACCOUNT; $media['type'] = self::ACCOUNT;
$media['media-uri-id'] = $contact['uri-id']; $media['media-uri-id'] = $contact['uri-id'];
$media['height'] = null; $media['height'] = null;
$media['width'] = null; $media['width'] = null;
$media['preview'] = null; $media['preview'] = null;
$media['preview-height'] = null; $media['preview-height'] = null;
$media['preview-width'] = null; $media['preview-width'] = null;
$media['blurhash'] = null; $media['blurhash'] = null;
$media['description'] = $contact['about']; $media['description'] = $contact['about'];
$media['name'] = $contact['name']; $media['name'] = $contact['name'];
$media['author-url'] = $contact['url']; $media['author-url'] = $contact['url'];
$media['author-name'] = $contact['name']; $media['author-name'] = $contact['name'];
$media['author-image'] = $contact['avatar']; $media['author-image'] = $contact['avatar'];
$media['publisher-url'] = $gserver['url'] ?? null; $media['publisher-url'] = $gserver['url'] ?? null;
$media['publisher-name'] = $gserver['site_name'] ?? null; $media['publisher-name'] = $gserver['site_name'] ?? null;
$media['publisher-image'] = null; $media['publisher-image'] = null;
Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]); Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]);
@ -417,22 +418,22 @@ class Media
Logger::debug('Detected site data is empty, use suggested media data instead', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'type' => $data['type']]); Logger::debug('Detected site data is empty, use suggested media data instead', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'type' => $data['type']]);
} }
} else { } else {
$media['preview'] = $data['images'][0]['src'] ?? null; $media['preview'] = $data['images'][0]['src'] ?? null;
$media['preview-height'] = $data['images'][0]['height'] ?? null; $media['preview-height'] = $data['images'][0]['height'] ?? null;
$media['preview-width'] = $data['images'][0]['width'] ?? null; $media['preview-width'] = $data['images'][0]['width'] ?? null;
$media['blurhash'] = $data['images'][0]['blurhash'] ?? null; $media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
$media['description'] = $data['text'] ?? null; $media['description'] = $data['text'] ?? null;
$media['name'] = $data['title'] ?? null; $media['name'] = $data['title'] ?? null;
} }
$media['type'] = self::HTML; $media['type'] = self::HTML;
$media['size'] = $data['size'] ?? null; $media['size'] = $data['size'] ?? null;
$media['author-url'] = $data['author_url'] ?? null; $media['author-url'] = $data['author_url'] ?? null;
$media['author-name'] = $data['author_name'] ?? null; $media['author-name'] = $data['author_name'] ?? null;
$media['author-image'] = $data['author_img'] ?? null; $media['author-image'] = $data['author_img'] ?? null;
$media['publisher-url'] = $data['publisher_url'] ?? null; $media['publisher-url'] = $data['publisher_url'] ?? null;
$media['publisher-name'] = $data['publisher_name'] ?? null; $media['publisher-name'] = $data['publisher_name'] ?? null;
$media['publisher-image'] = $data['publisher_img'] ?? null; $media['publisher-image'] = $data['publisher_img'] ?? null;
return $media; return $media;
} }
@ -461,9 +462,9 @@ class Media
$photo = Photo::selectFirst(['type', 'datasize', 'width', 'height', 'blurhash'], ['resource-id' => $matches[1], 'scale' => $matches[2]]); $photo = Photo::selectFirst(['type', 'datasize', 'width', 'height', 'blurhash'], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
if (!empty($photo)) { if (!empty($photo)) {
$media['mimetype'] = $photo['type']; $media['mimetype'] = $photo['type'];
$media['size'] = $photo['datasize']; $media['size'] = $photo['datasize'];
$media['width'] = $photo['width']; $media['width'] = $photo['width'];
$media['height'] = $photo['height']; $media['height'] = $photo['height'];
$media['blurhash'] = $photo['blurhash']; $media['blurhash'] = $photo['blurhash'];
} }
@ -472,7 +473,7 @@ class Media
} }
$photo = Photo::selectFirst(['width', 'height'], ['resource-id' => $matches[1], 'scale' => $matches[2]]); $photo = Photo::selectFirst(['width', 'height'], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
if (!empty($photo)) { if (!empty($photo)) {
$media['preview-width'] = $photo['width']; $media['preview-width'] = $photo['width'];
$media['preview-height'] = $photo['height']; $media['preview-height'] = $photo['height'];
} }
@ -505,7 +506,7 @@ class Media
} }
$filetype = strtolower($type[0]); $filetype = strtolower($type[0]);
$subtype = strtolower($type[1]); $subtype = strtolower($type[1]);
if ($filetype == 'image') { if ($filetype == 'image') {
$type = self::IMAGE; $type = self::IMAGE;
@ -612,8 +613,9 @@ class Media
if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
if (self::isLinkToImagePage($picture[1], $picture[2])) { if (self::isLinkToImagePage($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$image = str_replace(['-1.', '-2.'], '-0.', $picture[2]); $image = str_replace(['-1.', '-2.'], '-0.', $picture[2]);
$attachments[$image] = [ $attachments[$image] = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::IMAGE, 'type' => self::IMAGE,
@ -623,6 +625,7 @@ class Media
]; ];
} elseif (self::isLinkToPhoto($picture[1], $picture[2])) { } elseif (self::isLinkToPhoto($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::IMAGE, 'type' => self::IMAGE,
@ -632,6 +635,7 @@ class Media
]; ];
} elseif ($removepicturelinks) { } elseif ($removepicturelinks) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::UNKNOWN, 'type' => self::UNKNOWN,
@ -646,6 +650,7 @@ class Media
if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]]; $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
} }
} }
@ -653,8 +658,9 @@ class Media
if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
if (self::isLinkToImagePage($picture[1], $picture[2])) { if (self::isLinkToImagePage($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$image = str_replace(['-1.', '-2.'], '-0.', $picture[2]); $image = str_replace(['-1.', '-2.'], '-0.', $picture[2]);
$attachments[$image] = [ $attachments[$image] = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::IMAGE, 'type' => self::IMAGE,
@ -664,6 +670,7 @@ class Media
]; ];
} elseif (self::isLinkToPhoto($picture[1], $picture[2])) { } elseif (self::isLinkToPhoto($picture[1], $picture[2])) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::IMAGE, 'type' => self::IMAGE,
@ -673,6 +680,7 @@ class Media
]; ];
} elseif ($removepicturelinks) { } elseif ($removepicturelinks) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = [ $attachments[$picture[1]] = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::UNKNOWN, 'type' => self::UNKNOWN,
@ -687,6 +695,7 @@ class Media
if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) { if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) { foreach ($pictures as $picture) {
$body = str_replace($picture[0], '', $body); $body = str_replace($picture[0], '', $body);
$attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]]; $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
} }
} }
@ -694,6 +703,7 @@ class Media
if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) { if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) {
foreach ($audios as $audio) { foreach ($audios as $audio) {
$body = str_replace($audio[0], '', $body); $body = str_replace($audio[0], '', $body);
$attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]]; $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
} }
} }
@ -701,6 +711,7 @@ class Media
if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) { if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) {
foreach ($videos as $video) { foreach ($videos as $video) {
$body = str_replace($video[0], '', $body); $body = str_replace($video[0], '', $body);
$attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]]; $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
} }
} }
@ -731,7 +742,7 @@ class Media
{ {
do { do {
$prebody = $body; $prebody = $body;
$body = self::insertFromBody(0, $body, true); $body = self::insertFromBody(0, $body, true);
} while ($prebody != $body); } while ($prebody != $body);
return $body; return $body;
} }
@ -746,7 +757,7 @@ class Media
{ {
do { do {
$prebody = $body; $prebody = $body;
$body = self::insertFromBody(0, $body, false, true); $body = self::insertFromBody(0, $body, false, true);
} while ($prebody != $body); } while ($prebody != $body);
return $body; return $body;
} }
@ -829,15 +840,15 @@ class Media
Logger::info('Adding attachment data', ['data' => $data]); Logger::info('Adding attachment data', ['data' => $data]);
$attachment = [ $attachment = [
'uri-id' => $uriid, 'uri-id' => $uriid,
'type' => self::HTML, 'type' => self::HTML,
'url' => $data['url'], 'url' => $data['url'],
'preview' => $data['preview'] ?? null, 'preview' => $data['preview'] ?? null,
'description' => $data['description'] ?? null, 'description' => $data['description'] ?? null,
'name' => $data['title'] ?? null, 'name' => $data['title'] ?? null,
'author-url' => $data['author_url'] ?? null, 'author-url' => $data['author_url'] ?? null,
'author-name' => $data['author_name'] ?? null, 'author-name' => $data['author_name'] ?? null,
'publisher-url' => $data['provider_url'] ?? null, 'publisher-url' => $data['provider_url'] ?? null,
'publisher-name' => $data['provider_name'] ?? null, 'publisher-name' => $data['provider_name'] ?? null,
]; ];
if (!empty($data['image'])) { if (!empty($data['image'])) {
@ -860,11 +871,11 @@ class Media
} }
foreach ($matches as $attachment) { foreach ($matches as $attachment) {
$media['type'] = self::DOCUMENT; $media['type'] = self::DOCUMENT;
$media['uri-id'] = $uriid; $media['uri-id'] = $uriid;
$media['url'] = $attachment[1]; $media['url'] = $attachment[1];
$media['size'] = $attachment[2]; $media['size'] = $attachment[2];
$media['mimetype'] = $attachment[3]; $media['mimetype'] = $attachment[3];
$media['description'] = $attachment[4] ?? ''; $media['description'] = $attachment[4] ?? '';
self::insert($media); self::insert($media);
@ -1057,20 +1068,20 @@ class Media
} }
$data = [ $data = [
'type' => 'link', 'type' => 'link',
'url' => $links[0]['url'], 'url' => $links[0]['url'],
'title' => $links[0]['name'], 'title' => $links[0]['name'],
'text' => $links[0]['description'], 'text' => $links[0]['description'],
'publisher_name' => $links[0]['publisher-name'], 'publisher_name' => $links[0]['publisher-name'],
'publisher_url' => $links[0]['publisher-url'], 'publisher_url' => $links[0]['publisher-url'],
'publisher_img' => $links[0]['publisher-image'], 'publisher_img' => $links[0]['publisher-image'],
'author_name' => $links[0]['author-name'], 'author_name' => $links[0]['author-name'],
'author_url' => $links[0]['author-url'], 'author_url' => $links[0]['author-url'],
'author_img' => $links[0]['author-image'], 'author_img' => $links[0]['author-image'],
'images' => [[ 'images' => [[
'src' => $links[0]['preview'], 'src' => $links[0]['preview'],
'height' => $links[0]['preview-height'], 'height' => $links[0]['preview-height'],
'width' => $links[0]['preview-width'], 'width' => $links[0]['preview-width'],
]] ]]
]; ];
$body .= "\n" . PageInfo::getFooterFromData($data); $body .= "\n" . PageInfo::getFooterFromData($data);