+
+ /**
+ * Add media attachments to the body
+ *
+ * @param int $uriid
+ * @param string $body
+ * @return string body
+ */
+ public static function addAttachmentsToBody(int $uriid, string $body = '')
+ {
+ if (empty($body)) {
+ $item = Post::selectFirst(['body'], ['uri-id' => $uriid]);
+ if (!DBA::isResult($item)) {
+ return '';
+ }
+ $body = $item['body'];
+ }
+ $original_body = $body;
+
+ $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
+
+ foreach (self::getByURIId($uriid, [self::IMAGE, self::AUDIO, self::VIDEO]) as $media) {
+ if (Item::containsLink($body, $media['preview'] ?? $media['url'], $media['type'])) {
+ continue;
+ }
+
+ if ($media['type'] == self::IMAGE) {
+ if (!empty($media['preview'])) {
+ if (!empty($media['description'])) {
+ $body .= "\n[url=" . $media['url'] . "][img=" . $media['preview'] . ']' . $media['description'] .'[/img][/url]';
+ } else {
+ $body .= "\n[url=" . $media['url'] . "][img]" . $media['preview'] .'[/img][/url]';
+ }
+ } else {
+ if (!empty($media['description'])) {
+ $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
+ } else {
+ $body .= "\n[img]" . $media['url'] .'[/img]';
+ }
+ }
+ } elseif ($media['type'] == self::AUDIO) {
+ $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
+ } elseif ($media['type'] == self::VIDEO) {
+ $body .= "\n[video]" . $media['url'] . "[/video]\n";
+ }
+ }
+
+ if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $original_body, $match)) {
+ $body .= "\n" . $match[1];
+ }
+
+ return $body;
+ }
+
+ /**
+ * Get preview link for given media id
+ *
+ * @param integer $id media id
+ * @param string $size One of the ProxyUtils::SIZE_* constants
+ * @return string preview link
+ */
+ public static function getPreviewUrlForId(int $id, string $size = ''):string
+ {
+ $url = DI::baseUrl() . '/photo/preview/';
+ switch ($size) {
+ case Proxy::SIZE_MICRO:
+ $url .= Proxy::PIXEL_MICRO . '/';
+ break;
+ case Proxy::SIZE_THUMB:
+ $url .= Proxy::PIXEL_THUMB . '/';
+ break;
+ case Proxy::SIZE_SMALL:
+ $url .= Proxy::PIXEL_SMALL . '/';
+ break;
+ case Proxy::SIZE_MEDIUM:
+ $url .= Proxy::PIXEL_MEDIUM . '/';
+ break;
+ case Proxy::SIZE_LARGE:
+ $url .= Proxy::PIXEL_LARGE . '/';
+ break;
+ }
+ return $url . $id;
+ }
+
+ /**
+ * Get media link for given media id
+ *
+ * @param integer $id media id
+ * @param string $size One of the ProxyUtils::SIZE_* constants
+ * @return string media link
+ */
+ public static function getUrlForId(int $id, string $size = ''):string
+ {
+ $url = DI::baseUrl() . '/photo/media/';
+ switch ($size) {
+ case Proxy::SIZE_MICRO:
+ $url .= Proxy::PIXEL_MICRO . '/';
+ break;
+ case Proxy::SIZE_THUMB:
+ $url .= Proxy::PIXEL_THUMB . '/';
+ break;
+ case Proxy::SIZE_SMALL:
+ $url .= Proxy::PIXEL_SMALL . '/';
+ break;
+ case Proxy::SIZE_MEDIUM:
+ $url .= Proxy::PIXEL_MEDIUM . '/';
+ break;
+ case Proxy::SIZE_LARGE:
+ $url .= Proxy::PIXEL_LARGE . '/';
+ break;
+ }
+ return $url . $id;
+ }