+ if (!tumblr_send_npf($b)) {
+ tumblr_send_legacy($b);
+ }
+}
+
+function tumblr_send_legacy(array $b)
+{
+ $b['body'] = BBCode::removeAttachment($b['body']);
+
+ $title = trim($b['title']);
+
+ $media = Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML, Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]);
+
+ $photo = array_search(Post\Media::IMAGE, array_column($media, 'type'));
+ $link = array_search(Post\Media::HTML, array_column($media, 'type'));
+ $audio = array_search(Post\Media::AUDIO, array_column($media, 'type'));
+ $video = array_search(Post\Media::VIDEO, array_column($media, 'type'));
+
+ $params = [
+ 'state' => 'published',
+ 'tags' => implode(',', array_column(Tag::getByURIId($b['uri-id']), 'name')),
+ 'tweet' => 'off',
+ 'format' => 'html',
+ ];
+
+ $body = BBCode::removeShareInformation($b['body']);
+ $body = Post\Media::removeFromEndOfBody($body);
+
+ if ($photo !== false) {
+ $params['type'] = 'photo';
+ $params['caption'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
+ $params['data'] = [];
+ foreach ($media as $photo) {
+ if ($photo['type'] == Post\Media::IMAGE) {
+ if (Network::isLocalLink($photo['url']) && ($data = Photo::getResourceData($photo['url']))) {
+ $photo = Photo::selectFirst([], ["`resource-id` = ? AND `scale` > ?", $data['guid'], 0]);
+ if (!empty($photo)) {
+ $params['data'][] = Photo::getImageDataForPhoto($photo);
+ }
+ }
+ }
+ }
+ } elseif ($link !== false) {
+ $params['type'] = 'link';
+ $params['title'] = $media[$link]['name'];
+ $params['url'] = $media[$link]['url'];
+ $params['description'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
+
+ if (!empty($media[$link]['preview'])) {
+ $params['thumbnail'] = $media[$link]['preview'];
+ }
+ if (!empty($media[$link]['description'])) {
+ $params['excerpt'] = $media[$link]['description'];
+ }
+ if (!empty($media[$link]['author-name'])) {
+ $params['author'] = $media[$link]['author-name'];
+ }
+ } elseif ($audio !== false) {
+ $params['type'] = 'audio';
+ $params['external_url'] = $media[$audio]['url'];
+ $params['caption'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
+ } elseif ($video !== false) {
+ $params['type'] = 'video';
+ $params['embed'] = $media[$video]['url'];
+ $params['caption'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
+ } else {
+ $params['type'] = 'text';
+ $params['title'] = $title;
+ $params['body'] = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
+ }
+
+ if (isset($params['caption']) && (trim($title) != '')) {
+ $params['caption'] = '<h1>' . $title . '</h1>' .
+ '<p>' . $params['caption'] . '</p>';
+ }
+
+ $page = tumblr_get_page($b['uid']);
+
+ $result = tumblr_post($b['uid'], 'blog/' . $page . '/post', $params);
+
+ if ($result->meta->status < 400) {
+ Logger::info('Success (legacy)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response]);
+ } else {
+ Logger::notice('Error posting blog (legacy)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
+ }
+}
+
+function tumblr_send_npf(array $post): bool
+{
+ $page = tumblr_get_page($post['uid']);
+
+ if (empty($page)) {
+ Logger::notice('Missing page, post will not be send to Tumblr.', ['uid' => $post['uid'], 'page' => $page, 'id' => $post['id']]);
+ // "true" is returned, since the legacy function will fail as well.
+ return true;
+ }
+
+ $post['body'] = Post\Media::addAttachmentsToBody($post['uri-id'], $post['body']);
+ if (!empty($post['title'])) {
+ $post['body'] = '[h1]' . $post['title'] . "[/h1]\n" . $post['body'];
+ }
+
+ $params = [
+ 'content' => NPF::fromBBCode($post['body'], $post['uri-id']),
+ 'state' => 'published',
+ 'date' => DateTimeFormat::utc($post['created'], DateTimeFormat::ATOM),
+ 'tags' => implode(',', array_column(Tag::getByURIId($post['uri-id']), 'name')),
+ 'is_private' => false,
+ 'interactability_reblog' => 'everyone'
+ ];
+
+ $result = tumblr_post($post['uid'], 'blog/' . $page . '/posts', $params);
+
+ if ($result->meta->status < 400) {
+ Logger::info('Success (NPF)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response]);
+ return true;
+ } else {
+ Logger::notice('Error posting blog (NPF)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
+ return false;
+ }
+}
+
+function tumblr_get_post_from_uri(string $uri): array
+{
+ $parts = explode(':', $uri);
+ if (($parts[0] != 'tumblr') || empty($parts[2])) {
+ return [];