3 * Name: Tumblr Post Connector
4 * Description: Post to Tumblr
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 use Friendica\Content\PageInfo;
11 use Friendica\Content\Text\BBCode;
12 use Friendica\Content\Text\HTML;
13 use Friendica\Content\Text\NPF;
14 use Friendica\Core\Cache\Enum\Duration;
15 use Friendica\Core\Config\Util\ConfigFileManager;
16 use Friendica\Core\Hook;
17 use Friendica\Core\Logger;
18 use Friendica\Core\Protocol;
19 use Friendica\Core\Renderer;
20 use Friendica\Core\System;
21 use Friendica\Database\DBA;
23 use Friendica\Model\Contact;
24 use Friendica\Model\Item;
25 use Friendica\Model\ItemURI;
26 use Friendica\Model\Photo;
27 use Friendica\Model\Post;
28 use Friendica\Model\Tag;
29 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
30 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
31 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
32 use Friendica\Protocol\Activity;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Network;
35 use Friendica\Util\Strings;
36 use GuzzleHttp\Client;
37 use GuzzleHttp\Exception\RequestException;
38 use GuzzleHttp\HandlerStack;
39 use GuzzleHttp\Subscriber\Oauth\Oauth1;
41 define('TUMBLR_DEFAULT_POLL_INTERVAL', 10); // given in minutes
43 function tumblr_install()
45 Hook::register('load_config', __FILE__, 'tumblr_load_config');
46 Hook::register('hook_fork', __FILE__, 'tumblr_hook_fork');
47 Hook::register('post_local', __FILE__, 'tumblr_post_local');
48 Hook::register('notifier_normal', __FILE__, 'tumblr_send');
49 Hook::register('jot_networks', __FILE__, 'tumblr_jot_nets');
50 Hook::register('connector_settings', __FILE__, 'tumblr_settings');
51 Hook::register('connector_settings_post', __FILE__, 'tumblr_settings_post');
52 Hook::register('cron', __FILE__, 'tumblr_cron');
53 Hook::register('support_follow', __FILE__, 'tumblr_support_follow');
54 Hook::register('follow', __FILE__, 'tumblr_follow');
55 Hook::register('unfollow', __FILE__, 'tumblr_unfollow');
56 Hook::register('block', __FILE__, 'tumblr_block');
57 Hook::register('unblock', __FILE__, 'tumblr_unblock');
58 Hook::register('check_item_notification', __FILE__, 'tumblr_check_item_notification');
59 Hook::register('probe_detect', __FILE__, 'tumblr_probe_detect');
60 Hook::register('item_by_link', __FILE__, 'tumblr_item_by_link');
61 Logger::info('installed tumblr');
64 function tumblr_load_config(ConfigFileManager $loader)
66 DI::app()->getConfigCache()->load($loader->loadAddonConfig('tumblr'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
69 function tumblr_check_item_notification(array &$notification_data)
71 if (!tumblr_enabled_for_user($notification_data['uid'])) {
75 $page = tumblr_get_page($notification_data['uid']);
80 $own_user = Contact::selectFirst(['url', 'alias'], ['uid' => $notification_data['uid'], 'poll' => 'tumblr::'.$page]);
82 $notification_data['profiles'][] = $own_user['url'];
83 $notification_data['profiles'][] = $own_user['alias'];
87 function tumblr_probe_detect(array &$hookData)
89 // Don't overwrite an existing result
90 if (isset($hookData['result'])) {
94 // Avoid a lookup for the wrong network
95 if (!in_array($hookData['network'], ['', Protocol::TUMBLR])) {
99 Logger::debug('Search for tumblr blog', ['url' => $hookData['uri']]);
101 $hookData['result'] = tumblr_get_contact_by_url($hookData['uri']);
104 function tumblr_item_by_link(array &$hookData)
106 // Don't overwrite an existing result
107 if (isset($hookData['item_id'])) {
111 if (!tumblr_enabled_for_user($hookData['uid'])) {
115 if (!preg_match('#^https?://www\.tumblr.com/blog/view/(.+)/(\d+).*#', $hookData['uri'], $matches) && !preg_match('#^https?://www\.tumblr.com/(.+)/(\d+).*#', $hookData['uri'], $matches)) {
119 Logger::debug('Found tumblr post', ['url' => $hookData['uri'], 'blog' => $matches[1], 'id' => $matches[2]]);
121 $parameters = ['id' => $matches[2], 'reblog_info' => false, 'notes_info' => false, 'npf' => false];
122 $result = tumblr_get($hookData['uid'], 'blog/' . $matches[1] . '/posts', $parameters);
123 if ($result->meta->status > 399) {
124 Logger::notice('Error fetching status', ['meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'blog' => $matches[1], 'id' => $matches[2]]);
128 Logger::debug('Got post', ['blog' => $matches[1], 'id' => $matches[2], 'result' => $result->response->posts]);
129 if (!empty($result->response->posts)) {
130 $hookData['item_id'] = tumblr_process_post($result->response->posts[0], $hookData['uid']);
134 function tumblr_support_follow(array &$data)
136 if ($data['protocol'] == Protocol::TUMBLR) {
137 $data['result'] = true;
141 function tumblr_follow(array &$hook_data)
143 $uid = DI::userSession()->getLocalUserId();
145 if (!tumblr_enabled_for_user($uid)) {
149 Logger::debug('Check if contact is Tumblr', ['url' => $hook_data['url']]);
151 $fields = tumblr_get_contact_by_url($hook_data['url']);
152 if (empty($fields)) {
153 Logger::debug('Contact is not a Tumblr contact', ['url' => $hook_data['url']]);
157 $result = tumblr_post($uid, 'user/follow', ['url' => $fields['url']]);
158 if ($result->meta->status <= 399) {
159 $hook_data['contact'] = $fields;
160 Logger::debug('Successfully start following', ['url' => $fields['url']]);
162 Logger::notice('Following failed', ['meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'url' => $fields['url']]);
166 function tumblr_unfollow(array &$hook_data)
168 if (!tumblr_enabled_for_user($hook_data['uid'])) {
172 if (!tumblr_get_contact_uuid($hook_data['contact'])) {
175 $result = tumblr_post($hook_data['uid'], 'user/unfollow', ['url' => $hook_data['contact']['url']]);
176 $hook_data['result'] = ($result->meta->status <= 399);
179 function tumblr_block(array &$hook_data)
181 if (!tumblr_enabled_for_user($hook_data['uid'])) {
185 $uuid = tumblr_get_contact_uuid($hook_data['contact']);
190 $result = tumblr_post($hook_data['uid'], 'blog/' . tumblr_get_page($hook_data['uid']) . '/blocks', ['blocked_tumblelog' => $uuid]);
191 $hook_data['result'] = ($result->meta->status <= 399);
193 if ($hook_data['result']) {
194 $cdata = Contact::getPublicAndUserContactID($hook_data['contact']['id'], $hook_data['uid']);
195 if (!empty($cdata['user'])) {
196 Contact::remove($cdata['user']);
201 function tumblr_unblock(array &$hook_data)
203 if (!tumblr_enabled_for_user($hook_data['uid'])) {
207 $uuid = tumblr_get_contact_uuid($hook_data['contact']);
212 $result = tumblr_delete($hook_data['uid'], 'blog/' . tumblr_get_page($hook_data['uid']) . '/blocks', ['blocked_tumblelog' => $uuid]);
213 $hook_data['result'] = ($result->meta->status <= 399);
216 function tumblr_get_contact_uuid(array $contact): string
218 if (($contact['network'] != Protocol::TUMBLR) || (substr($contact['poll'], 0, 8) != 'tumblr::')) {
221 return substr($contact['poll'], 8);
225 * This is a statement rather than an actual function definition. The simple
226 * existence of this method is checked to figure out if the addon offers a
229 function tumblr_module()
233 function tumblr_content()
235 if (!DI::userSession()->getLocalUserId()) {
236 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
240 switch (DI::args()->getArgv()[1] ?? '') {
249 DI::baseUrl()->redirect('settings/connectors/tumblr');
252 function tumblr_redirect()
254 if (($_REQUEST['state'] ?? '') != DI::session()->get('oauth_state')) {
258 tumblr_get_token(DI::userSession()->getLocalUserId(), $_REQUEST['code'] ?? '');
261 function tumblr_connect()
263 // Define the needed keys
264 $consumer_key = DI::config()->get('tumblr', 'consumer_key');
265 $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
267 if (empty($consumer_key) || empty($consumer_secret)) {
271 $state = base64_encode(random_bytes(20));
272 DI::session()->set('oauth_state', $state);
275 'client_id' => $consumer_key,
276 'response_type' => 'code',
277 'scope' => 'basic write offline_access',
281 System::externalRedirect('https://www.tumblr.com/oauth2/authorize?' . http_build_query($parameters));
284 function tumblr_addon_admin(string &$o)
286 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/tumblr/');
288 $o = Renderer::replaceMacros($t, [
289 '$submit' => DI::l10n()->t('Save Settings'),
290 '$consumer_key' => ['consumer_key', DI::l10n()->t('Consumer Key'), DI::config()->get('tumblr', 'consumer_key'), ''],
291 '$consumer_secret' => ['consumer_secret', DI::l10n()->t('Consumer Secret'), DI::config()->get('tumblr', 'consumer_secret'), ''],
295 function tumblr_addon_admin_post()
297 DI::config()->set('tumblr', 'consumer_key', trim($_POST['consumer_key'] ?? ''));
298 DI::config()->set('tumblr', 'consumer_secret', trim($_POST['consumer_secret'] ?? ''));
301 function tumblr_settings(array &$data)
303 if (!DI::userSession()->getLocalUserId()) {
307 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post', false);
308 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default', false);
309 $import = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'import', false);
311 $cachekey = 'tumblr-blogs-' . DI::userSession()->getLocalUserId();
312 $blogs = DI::cache()->get($cachekey);
314 $blogs = tumblr_get_blogs(DI::userSession()->getLocalUserId());
315 if (!empty($blogs)) {
316 DI::cache()->set($cachekey, $blogs, Duration::HALF_HOUR);
320 if (!empty($blogs)) {
321 $page = tumblr_get_page(DI::userSession()->getLocalUserId(), $blogs);
323 $page_select = ['tumblr_page', DI::l10n()->t('Post to page:'), $page, '', $blogs];
326 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/tumblr/');
327 $html = Renderer::replaceMacros($t, [
329 'connect' => DI::l10n()->t('(Re-)Authenticate your tumblr page'),
330 'noconnect' => DI::l10n()->t('You are not authenticated to tumblr'),
333 '$authenticate_url' => DI::baseUrl() . '/tumblr/connect',
335 '$enable' => ['tumblr', DI::l10n()->t('Enable Tumblr Post Addon'), $enabled],
336 '$bydefault' => ['tumblr_bydefault', DI::l10n()->t('Post to Tumblr by default'), $def_enabled],
337 '$import' => ['tumblr_import', DI::l10n()->t('Import the remote timeline'), $import],
338 '$page_select' => $page_select ?? '',
342 'connector' => 'tumblr',
343 'title' => DI::l10n()->t('Tumblr Import/Export'),
344 'image' => 'images/tumblr.png',
345 'enabled' => $enabled,
350 function tumblr_jot_nets(array &$jotnets_fields)
352 if (!DI::userSession()->getLocalUserId()) {
356 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post')) {
357 $jotnets_fields[] = [
358 'type' => 'checkbox',
361 DI::l10n()->t('Post to Tumblr'),
362 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default')
368 function tumblr_settings_post(array &$b)
370 if (!empty($_POST['tumblr-submit'])) {
371 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'post', intval($_POST['tumblr']));
372 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'page', $_POST['tumblr_page']);
373 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default', intval($_POST['tumblr_bydefault']));
374 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'import', intval($_POST['tumblr_import']));
378 function tumblr_cron()
380 $last = DI::keyValue()->get('tumblr_last_poll');
382 $poll_interval = intval(DI::config()->get('tumblr', 'poll_interval'));
383 if (!$poll_interval) {
384 $poll_interval = TUMBLR_DEFAULT_POLL_INTERVAL;
388 $next = $last + ($poll_interval * 60);
389 if ($next > time()) {
390 Logger::notice('poll intervall not reached');
394 Logger::notice('cron_start');
396 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
397 if ($abandon_days < 1) {
401 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
403 $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'tumblr', 'k' => 'import', 'v' => true]);
404 foreach ($pconfigs as $pconfig) {
405 if ($abandon_days != 0) {
406 if (!DBA::exists('user', ["`uid` = ? AND `login_date` >= ?", $pconfig['uid'], $abandon_limit])) {
407 Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $pconfig['uid']]);
412 Logger::notice('importing timeline - start', ['user' => $pconfig['uid']]);
413 tumblr_fetch_dashboard($pconfig['uid']);
414 Logger::notice('importing timeline - done', ['user' => $pconfig['uid']]);
417 Logger::notice('cron_end');
419 DI::keyValue()->set('tumblr_last_poll', time());
422 function tumblr_hook_fork(array &$b)
424 if ($b['name'] != 'notifier_normal') {
430 // Editing is not supported by the addon
431 if (($post['created'] !== $post['edited']) && !$post['deleted']) {
432 DI::logger()->info('Editing is not supported by the addon');
433 $b['execute'] = false;
437 if (DI::pConfig()->get($post['uid'], 'tumblr', 'import')) {
438 // Don't post if it isn't a reply to a tumblr post
439 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::TUMBLR])) {
440 Logger::notice('No tumblr parent found', ['item' => $post['id']]);
441 $b['execute'] = false;
444 } elseif (!strstr($post['postopts'] ?? '', 'tumblr') || ($post['parent'] != $post['id']) || $post['private']) {
445 DI::logger()->info('Activities are never exported when we don\'t import the tumblr timeline', ['uid' => $post['uid']]);
446 $b['execute'] = false;
451 function tumblr_post_local(array &$b)
457 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
461 if ($b['private'] || $b['parent']) {
465 $tmbl_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post'));
466 $tmbl_enable = (($tmbl_post && !empty($_REQUEST['tumblr_enable'])) ? intval($_REQUEST['tumblr_enable']) : 0);
468 // if API is used, default to the chosen settings
469 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default'))) {
477 if (strlen($b['postopts'])) {
478 $b['postopts'] .= ',';
481 $b['postopts'] .= 'tumblr';
484 function tumblr_send(array &$b)
486 if (($b['created'] !== $b['edited']) && !$b['deleted']) {
490 if ($b['gravity'] != Item::GRAVITY_PARENT) {
491 Logger::debug('Got comment', ['item' => $b]);
493 $parent = tumblr_get_post_from_uri($b['thr-parent']);
494 if (empty($parent)) {
495 Logger::notice('No tumblr post', ['thr-parent' => $b['thr-parent']]);
499 Logger::debug('Parent found', ['parent' => $parent]);
501 $page = tumblr_get_page($b['uid']);
503 if ($b['gravity'] == Item::GRAVITY_COMMENT) {
504 Logger::notice('Commenting is not supported (yet)');
506 if (($b['verb'] == Activity::LIKE) && !$b['deleted']) {
507 $params = ['id' => $parent['id'], 'reblog_key' => $parent['reblog_key']];
508 $result = tumblr_post($b['uid'], 'user/like', $params);
509 } elseif (($b['verb'] == Activity::LIKE) && $b['deleted']) {
510 $params = ['id' => $parent['id'], 'reblog_key' => $parent['reblog_key']];
511 $result = tumblr_post($b['uid'], 'user/unlike', $params);
512 } elseif (($b['verb'] == Activity::ANNOUNCE) && !$b['deleted']) {
513 $params = ['id' => $parent['id'], 'reblog_key' => $parent['reblog_key']];
514 $result = tumblr_post($b['uid'], 'blog/' . $page . '/post/reblog', $params);
515 } elseif (($b['verb'] == Activity::ANNOUNCE) && $b['deleted']) {
516 $announce = tumblr_get_post_from_uri($b['extid']);
517 if (empty($announce)) {
520 $params = ['id' => $announce['id']];
521 $result = tumblr_post($b['uid'], 'blog/' . $page . '/post/delete', $params);
523 // Unsupported activity
527 if ($result->meta->status < 400) {
528 Logger::info('Successfully performed activity', ['verb' => $b['verb'], 'deleted' => $b['deleted'], 'meta' => $result->meta, 'response' => $result->response]);
529 if (!$b['deleted'] && !empty($result->response->id_string)) {
530 Item::update(['extid' => 'tumblr::' . $result->response->id_string], ['id' => $b['id']]);
533 Logger::notice('Error while performing activity', ['verb' => $b['verb'], 'deleted' => $b['deleted'], 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
537 } elseif ($b['private'] || !strstr($b['postopts'], 'tumblr')) {
541 if (!tumblr_send_npf($b)) {
542 tumblr_send_legacy($b);
546 function tumblr_send_legacy(array $b)
548 $b['body'] = BBCode::removeAttachment($b['body']);
550 $title = trim($b['title']);
552 $media = Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML, Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]);
554 $photo = array_search(Post\Media::IMAGE, array_column($media, 'type'));
555 $link = array_search(Post\Media::HTML, array_column($media, 'type'));
556 $audio = array_search(Post\Media::AUDIO, array_column($media, 'type'));
557 $video = array_search(Post\Media::VIDEO, array_column($media, 'type'));
560 'state' => 'published',
561 'tags' => implode(',', array_column(Tag::getByURIId($b['uri-id']), 'name')),
566 $body = BBCode::removeShareInformation($b['body']);
567 $body = Post\Media::removeFromEndOfBody($body);
569 if ($photo !== false) {
570 $params['type'] = 'photo';
571 $params['caption'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
572 $params['data'] = [];
573 foreach ($media as $photo) {
574 if ($photo['type'] == Post\Media::IMAGE) {
575 if (Network::isLocalLink($photo['url']) && ($data = Photo::getResourceData($photo['url']))) {
576 $photo = Photo::selectFirst([], ["`resource-id` = ? AND `scale` > ?", $data['guid'], 0]);
577 if (!empty($photo)) {
578 $params['data'][] = Photo::getImageDataForPhoto($photo);
583 } elseif ($link !== false) {
584 $params['type'] = 'link';
585 $params['title'] = $media[$link]['name'];
586 $params['url'] = $media[$link]['url'];
587 $params['description'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
589 if (!empty($media[$link]['preview'])) {
590 $params['thumbnail'] = $media[$link]['preview'];
592 if (!empty($media[$link]['description'])) {
593 $params['excerpt'] = $media[$link]['description'];
595 if (!empty($media[$link]['author-name'])) {
596 $params['author'] = $media[$link]['author-name'];
598 } elseif ($audio !== false) {
599 $params['type'] = 'audio';
600 $params['external_url'] = $media[$audio]['url'];
601 $params['caption'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
602 } elseif ($video !== false) {
603 $params['type'] = 'video';
604 $params['embed'] = $media[$video]['url'];
605 $params['caption'] = BBCode::convertForUriId($b['uri-id'], $body, BBCode::CONNECTORS);
607 $params['type'] = 'text';
608 $params['title'] = $title;
609 $params['body'] = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
612 if (isset($params['caption']) && (trim($title) != '')) {
613 $params['caption'] = '<h1>' . $title . '</h1>' .
614 '<p>' . $params['caption'] . '</p>';
617 $page = tumblr_get_page($b['uid']);
619 $result = tumblr_post($b['uid'], 'blog/' . $page . '/post', $params);
621 if ($result->meta->status < 400) {
622 Logger::info('Success (legacy)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response]);
624 Logger::notice('Error posting blog (legacy)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
628 function tumblr_send_npf(array $post): bool
630 $page = tumblr_get_page($post['uid']);
633 Logger::notice('Missing page, post will not be send to Tumblr.', ['uid' => $post['uid'], 'page' => $page, 'id' => $post['id']]);
634 // "true" is returned, since the legacy function will fail as well.
638 $post['body'] = Post\Media::addAttachmentsToBody($post['uri-id'], $post['body']);
639 if (!empty($post['title'])) {
640 $post['body'] = '[h1]' . $post['title'] . "[/h1]\n" . $post['body'];
644 'content' => NPF::fromBBCode($post['body'], $post['uri-id']),
645 'state' => 'published',
646 'date' => DateTimeFormat::utc($post['created'], DateTimeFormat::ATOM),
647 'tags' => implode(',', array_column(Tag::getByURIId($post['uri-id']), 'name')),
648 'is_private' => false,
649 'interactability_reblog' => 'everyone'
652 $result = tumblr_post($post['uid'], 'blog/' . $page . '/posts', $params);
654 if ($result->meta->status < 400) {
655 Logger::info('Success (NPF)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response]);
658 Logger::notice('Error posting blog (NPF)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
663 function tumblr_get_post_from_uri(string $uri): array
665 $parts = explode(':', $uri);
666 if (($parts[0] != 'tumblr') || empty($parts[2])) {
670 $post['id'] = $parts[2];
671 $post['reblog_key'] = $parts[3] ?? '';
673 $post['reblog_key'] = str_replace('@t', '', $post['reblog_key']); // Temp
678 * Fetch the dashboard (timeline) for the given user
680 * @param integer $uid
683 function tumblr_fetch_dashboard(int $uid)
685 $page = tumblr_get_page($uid);
687 $parameters = ['reblog_info' => false, 'notes_info' => false, 'npf' => false];
689 $last = DI::pConfig()->get($uid, 'tumblr', 'last_id');
691 $parameters['since_id'] = $last;
694 $dashboard = tumblr_get($uid, 'user/dashboard', $parameters);
695 if ($dashboard->meta->status > 399) {
696 Logger::notice('Error fetching dashboard', ['meta' => $dashboard->meta, 'response' => $dashboard->response, 'errors' => $dashboard->errors]);
700 if (empty($dashboard->response->posts)) {
704 foreach (array_reverse($dashboard->response->posts) as $post) {
705 $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
707 if ($post->id > $last) {
711 Logger::debug('Importing post', ['uid' => $uid, 'created' => date(DateTimeFormat::MYSQL, $post->timestamp), 'uri' => $uri]);
713 if (Post::exists(['uri' => $uri, 'uid' => $uid]) || ($post->blog->uuid == $page)) {
714 DI::pConfig()->set($uid, 'tumblr', 'last_id', $last);
718 tumblr_process_post($post, $uid, $uri);
721 DI::pConfig()->set($uid, 'tumblr', 'last_id', $last);
725 function tumblr_process_post(stdClass $post, int $uid, string $uri = ''): int
728 $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
731 $item = tumblr_get_header($post, $uri, $uid);
733 $item = tumblr_get_content($item, $post);
735 $id = item::insert($item);
738 $stored = Post::selectFirst(['uri-id'], ['id' => $id]);
740 if (!empty($post->tags)) {
741 foreach ($post->tags as $tag) {
742 Tag::store($stored['uri-id'], Tag::HASHTAG, $tag);
750 * Sets the initial data for the item array
752 * @param stdClass $post
754 * @param integer $uid
757 function tumblr_get_header(stdClass $post, string $uri, int $uid): array
759 $contact = tumblr_get_contact($post->blog, $uid);
761 'network' => Protocol::TUMBLR,
765 'private' => Item::UNLISTED,
766 'verb' => Activity::POST,
767 'contact-id' => $contact['id'],
768 'author-name' => $contact['name'],
769 'author-link' => $contact['url'],
770 'author-avatar' => $contact['avatar'],
771 'plink' => $post->post_url,
772 'created' => date(DateTimeFormat::MYSQL, $post->timestamp)
775 $item['owner-name'] = $item['author-name'];
776 $item['owner-link'] = $item['author-link'];
777 $item['owner-avatar'] = $item['author-avatar'];
783 * Set the body according the given content type
786 * @param stdClass $post
789 function tumblr_get_content(array $item, stdClass $post): array
791 switch ($post->type) {
793 $item['title'] = $post->title;
794 $item['body'] = HTML::toBBCode(tumblr_add_npf_data($post->body, $post->post_url));
798 if (empty($post->text)) {
799 $body = HTML::toBBCode($post->text) . "\n";
803 if (!empty($post->source_title) && !empty($post->source_url)) {
804 $body .= '[url=' . $post->source_url . ']' . $post->source_title . "[/url]:\n";
805 } elseif (!empty($post->source_title)) {
806 $body .= $post->source_title . ":\n";
808 $body .= '[quote]' . HTML::toBBCode($post->source) . '[/quote]';
809 $item['body'] = $body;
813 $item['body'] = HTML::toBBCode($post->description) . "\n" . PageInfo::getFooterFromUrl($post->url);
817 if (!empty($post->asking_name) && !empty($post->asking_url)) {
818 $body = '[url=' . $post->asking_url . ']' . $post->asking_name . "[/url]:\n";
819 } elseif (!empty($post->asking_name)) {
820 $body = $post->asking_name . ":\n";
824 $body .= '[quote]' . HTML::toBBCode($post->question) . "[/quote]\n" . HTML::toBBCode($post->answer);
825 $item['body'] = $body;
829 $item['body'] = HTML::toBBCode($post->caption);
830 if (!empty($post->video_url)) {
831 $item['body'] .= "\n[video]" . $post->video_url . "[/video]\n";
832 } elseif (!empty($post->thumbnail_url)) {
833 $item['body'] .= "\n[url=" . $post->permalink_url . "][img]" . $post->thumbnail_url . "[/img][/url]\n";
834 } elseif (!empty($post->permalink_url)) {
835 $item['body'] .= "\n[url]" . $post->permalink_url . "[/url]\n";
836 } elseif (!empty($post->source_url) && !empty($post->source_title)) {
837 $item['body'] .= "\n[url=" . $post->source_url . "]" . $post->source_title . "[/url]\n";
838 } elseif (!empty($post->source_url)) {
839 $item['body'] .= "\n[url]" . $post->source_url . "[/url]\n";
844 $item['body'] = HTML::toBBCode($post->caption);
845 if (!empty($post->source_url) && !empty($post->source_title)) {
846 $item['body'] .= "\n[url=" . $post->source_url . "]" . $post->source_title . "[/url]\n";
847 } elseif (!empty($post->source_url)) {
848 $item['body'] .= "\n[url]" . $post->source_url . "[/url]\n";
853 $item['body'] = HTML::toBBCode($post->caption);
854 foreach ($post->photos as $photo) {
855 if (!empty($photo->original_size)) {
856 $item['body'] .= "\n[img]" . $photo->original_size->url . "[/img]";
857 } elseif (!empty($photo->alt_sizes)) {
858 $item['body'] .= "\n[img]" . $photo->alt_sizes[0]->url . "[/img]";
864 $item['title'] = $post->title;
865 $item['body'] = "\n[ul]";
866 foreach ($post->dialogue as $line) {
867 $item['body'] .= "\n[li]" . $line->label . " " . $line->phrase . "[/li]";
869 $item['body'] .= "[/ul]\n";
875 function tumblr_add_npf_data(string $html, string $plink): string
877 $doc = new DOMDocument();
879 $doc->formatOutput = true;
880 @$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
881 $xpath = new DomXPath($doc);
882 $list = $xpath->query('//p[@class="npf_link"]');
883 foreach ($list as $node) {
884 $data = tumblr_get_npf_data($node);
889 tumblr_replace_with_npf($doc, $node, tumblr_get_type_replacement($data, $plink));
892 $list = $xpath->query('//div[@data-npf]');
893 foreach ($list as $node) {
894 $data = tumblr_get_npf_data($node);
899 tumblr_replace_with_npf($doc, $node, tumblr_get_type_replacement($data, $plink));
902 $list = $xpath->query('//figure[@data-provider="youtube"]');
903 foreach ($list as $node) {
904 $attributes = tumblr_get_attributes($node);
905 if (empty($attributes['data-url'])) {
908 tumblr_replace_with_npf($doc, $node, '[youtube]' . $attributes['data-url'] . '[/youtube]');
911 $list = $xpath->query('//figure[@data-npf]');
912 foreach ($list as $node) {
913 $data = tumblr_get_npf_data($node);
917 tumblr_replace_with_npf($doc, $node, tumblr_get_type_replacement($data, $plink));
920 return $doc->saveHTML();
923 function tumblr_replace_with_npf(DOMDocument $doc, DOMNode $node, string $replacement)
925 if (empty($replacement)) {
928 $replace = $doc->createTextNode($replacement);
929 $node->parentNode->insertBefore($replace, $node);
930 $node->parentNode->removeChild($node);
933 function tumblr_get_npf_data(DOMNode $node): array
935 $attributes = tumblr_get_attributes($node);
936 if (empty($attributes['data-npf'])) {
940 return json_decode($attributes['data-npf'], true);
943 function tumblr_get_attributes($node): array
945 if (empty($node->attributes)) {
950 foreach ($node->attributes as $key => $attribute) {
951 $attributes[$key] = trim($attribute->value);
956 function tumblr_get_type_replacement(array $data, string $plink): string
958 switch ($data['type']) {
960 $body = '[p][url=' . $plink . ']' . $data['question'] . '[/url][/p][ul]';
961 foreach ($data['answers'] as $answer) {
962 $body .= '[li]' . $answer['answer_text'] . '[/li]';
968 $body = PageInfo::getFooterFromUrl(str_replace('https://href.li/?', '', $data['url']));
972 if (!empty($data['url']) && ($data['provider'] == 'tumblr')) {
973 $body = '[video]' . $data['url'] . '[/video]';
978 Logger::notice('Unknown type', ['type' => $data['type'], 'data' => $data, 'plink' => $plink]);
986 * Get a contact array for the given blog
988 * @param stdClass $blog
989 * @param integer $uid
992 function tumblr_get_contact(stdClass $blog, int $uid): array
994 $condition = ['network' => Protocol::TUMBLR, 'uid' => $uid, 'poll' => 'tumblr::' . $blog->uuid];
995 $contact = Contact::selectFirst([], $condition);
996 if (!empty($contact) && (strtotime($contact['updated']) >= $blog->updated)) {
999 if (empty($contact)) {
1000 $cid = tumblr_insert_contact($blog, $uid);
1002 $cid = $contact['id'];
1005 $condition['uid'] = 0;
1007 $contact = Contact::selectFirst([], $condition);
1008 if (empty($contact)) {
1009 $pcid = tumblr_insert_contact($blog, 0);
1011 $pcid = $contact['id'];
1014 tumblr_update_contact($blog, $uid, $cid, $pcid);
1016 return Contact::getById($cid);
1020 * Create a new contact
1022 * @param stdClass $blog
1023 * @param integer $uid
1026 function tumblr_insert_contact(stdClass $blog, int $uid)
1028 $baseurl = 'https://tumblr.com';
1029 $url = $baseurl . '/' . $blog->name;
1033 'network' => Protocol::TUMBLR,
1034 'poll' => 'tumblr::' . $blog->uuid,
1035 'baseurl' => $baseurl,
1039 'readonly' => false,
1042 'nurl' => Strings::normaliseLink($url),
1043 'alias' => $blog->url,
1044 'name' => $blog->title,
1045 'nick' => $blog->name,
1046 'addr' => $blog->name . '@tumblr.com',
1047 'about' => HTML::toBBCode($blog->description),
1048 'updated' => date(DateTimeFormat::MYSQL, $blog->updated)
1050 return Contact::insert($fields);
1054 * Updates the given contact for the given user and proviced contact ids
1056 * @param stdClass $blog
1057 * @param integer $uid
1058 * @param integer $cid
1059 * @param integer $pcid
1062 function tumblr_update_contact(stdClass $blog, int $uid, int $cid, int $pcid)
1064 $info = tumblr_get($uid, 'blog/' . $blog->uuid . '/info');
1065 if ($info->meta->status > 399) {
1066 Logger::notice('Error fetching dashboard', ['meta' => $info->meta, 'response' => $info->response, 'errors' => $info->errors]);
1070 $avatar = $info->response->blog->avatar;
1071 if (!empty($avatar)) {
1072 Contact::updateAvatar($cid, $avatar[0]->url);
1075 $baseurl = 'https://tumblr.com';
1076 $url = $baseurl . '/' . $info->response->blog->name;
1078 if ($info->response->blog->followed && $info->response->blog->subscribed) {
1079 $rel = Contact::FRIEND;
1080 } elseif ($info->response->blog->followed && !$info->response->blog->subscribed) {
1081 $rel = Contact::SHARING;
1082 } elseif (!$info->response->blog->followed && $info->response->blog->subscribed) {
1083 $rel = Contact::FOLLOWER;
1085 $rel = Contact::NOTHING;
1088 $uri_id = ItemURI::getIdByURI($url);
1091 'nurl' => Strings::normaliseLink($url),
1092 'uri-id' => $uri_id,
1093 'alias' => $info->response->blog->url,
1094 'name' => $info->response->blog->title,
1095 'nick' => $info->response->blog->name,
1096 'addr' => $info->response->blog->name . '@tumblr.com',
1097 'about' => HTML::toBBCode($info->response->blog->description),
1098 'updated' => date(DateTimeFormat::MYSQL, $info->response->blog->updated),
1099 'header' => $info->response->blog->theme->header_image_focused,
1103 Contact::update($fields, ['id' => $cid]);
1105 $fields['rel'] = Contact::NOTHING;
1106 Contact::update($fields, ['id' => $pcid]);
1110 * Get the default page for posting. Detects the value if not provided or has got a bad value.
1112 * @param integer $uid
1113 * @param array $blogs
1116 function tumblr_get_page(int $uid, array $blogs = []): string
1118 $page = DI::pConfig()->get($uid, 'tumblr', 'page');
1120 if (!empty($page) && (strpos($page, '/') === false)) {
1124 if (empty($blogs)) {
1125 $blogs = tumblr_get_blogs($uid);
1128 if (!empty($blogs)) {
1129 $page = array_key_first($blogs);
1130 DI::pConfig()->set($uid, 'tumblr', 'page', $page);
1138 * Get an array of blogs for the given user
1140 * @param integer $uid
1143 function tumblr_get_blogs(int $uid): array
1145 $userinfo = tumblr_get($uid, 'user/info');
1146 if ($userinfo->meta->status > 299) {
1147 Logger::notice('Error fetching blogs', ['meta' => $userinfo->meta, 'response' => $userinfo->response, 'errors' => $userinfo->errors]);
1152 foreach ($userinfo->response->user->blogs as $blog) {
1153 $blogs[$blog->uuid] = $blog->name;
1158 function tumblr_enabled_for_user(int $uid)
1160 return !empty($uid) && !empty(DI::pConfig()->get($uid, 'tumblr', 'access_token')) &&
1161 !empty(DI::pConfig()->get($uid, 'tumblr', 'refresh_token')) &&
1162 !empty(DI::config()->get('tumblr', 'consumer_key')) &&
1163 !empty(DI::config()->get('tumblr', 'consumer_secret'));
1167 * Get a contact array from a Tumblr url
1169 * @param string $url
1172 function tumblr_get_contact_by_url(string $url): array
1174 $consumer_key = DI::config()->get('tumblr', 'consumer_key');
1175 if (empty($consumer_key)) {
1179 if (!preg_match('#^https?://tumblr.com/(.+)#', $url, $matches) && !preg_match('#^https?://www\.tumblr.com/(.+)#', $url, $matches) && !preg_match('#^https?://(.+)\.tumblr.com#', $url, $matches)) {
1181 $curlResult = DI::httpClient()->get($url);
1182 } catch (\Exception $e) {
1185 $html = $curlResult->getBody();
1189 $doc = new DOMDocument();
1190 @$doc->loadHTML($html);
1191 $xpath = new DomXPath($doc);
1192 $body = $xpath->query('body');
1193 $attributes = tumblr_get_attributes($body->item(0));
1194 $blog = $attributes['data-urlencoded-name'] ?? '';
1196 $blogs = explode('/', $matches[1]);
1197 $blog = $blogs[0] ?? '';
1204 $curlResult = DI::httpClient()->get('https://api.tumblr.com/v2/blog/' . $blog . '/info?api_key=' . $consumer_key);
1205 $body = $curlResult->getBody();
1206 $data = json_decode($body);
1211 $baseurl = 'https://tumblr.com';
1212 $url = $baseurl . '/' . $data->response->blog->name;
1216 'nurl' => Strings::normaliseLink($url),
1217 'addr' => $data->response->blog->name . '@tumblr.com',
1218 'alias' => $data->response->blog->url,
1221 'poll' => 'tumblr::' . $data->response->blog->uuid,
1223 'name' => $data->response->blog->title,
1224 'nick' => $data->response->blog->name,
1225 'network' => Protocol::TUMBLR,
1226 'baseurl' => $baseurl,
1229 'guid' => $data->response->blog->uuid,
1230 'about' => HTML::toBBCode($data->response->blog->description),
1231 'photo' => $data->response->blog->avatar[0]->url,
1232 'header' => $data->response->blog->theme->header_image_focused,
1237 * Perform an OAuth2 GET request
1239 * @param integer $uid
1240 * @param string $url
1241 * @param array $parameters
1244 function tumblr_get(int $uid, string $url, array $parameters = []): stdClass
1246 $url = 'https://api.tumblr.com/v2/' . $url;
1248 if (!empty($parameters)) {
1249 $url .= '?' . http_build_query($parameters);
1252 $curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . tumblr_get_token($uid)]]]);
1253 return tumblr_format_result($curlResult);
1257 * Perform an OAuth2 POST request
1259 * @param integer $uid
1260 * @param string $url
1261 * @param array $parameters
1264 function tumblr_post(int $uid, string $url, array $parameters): stdClass
1266 $url = 'https://api.tumblr.com/v2/' . $url;
1268 $curlResult = DI::httpClient()->post($url, $parameters, ['Authorization' => ['Bearer ' . tumblr_get_token($uid)]]);
1269 return tumblr_format_result($curlResult);
1273 * Perform an OAuth2 DELETE request
1275 * @param integer $uid
1276 * @param string $url
1277 * @param array $parameters
1280 function tumblr_delete(int $uid, string $url, array $parameters): stdClass
1282 $url = 'https://api.tumblr.com/v2/' . $url;
1285 HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . tumblr_get_token($uid)]],
1286 HttpClientOptions::FORM_PARAMS => $parameters
1289 $curlResult = DI::httpClient()->request('delete', $url, $opts);
1290 return tumblr_format_result($curlResult);
1294 * Format the get/post result value
1296 * @param ICanHandleHttpResponses $curlResult
1299 function tumblr_format_result(ICanHandleHttpResponses $curlResult): stdClass
1301 $result = json_decode($curlResult->getBody());
1302 if (empty($result) || empty($result->meta)) {
1303 $result = new stdClass;
1304 $result->meta = new stdClass;
1305 $result->meta->status = 500;
1306 $result->meta->msg = '';
1307 $result->response = [];
1308 $result->errors = [];
1314 * Fetch the OAuth token, update it if needed
1316 * @param integer $uid
1317 * @param string $code
1320 function tumblr_get_token(int $uid, string $code = ''): string
1322 $access_token = DI::pConfig()->get($uid, 'tumblr', 'access_token');
1323 $expires_at = DI::pConfig()->get($uid, 'tumblr', 'expires_at');
1324 $refresh_token = DI::pConfig()->get($uid, 'tumblr', 'refresh_token');
1326 if (empty($code) && !empty($access_token) && ($expires_at > (time()))) {
1327 Logger::debug('Got token', ['uid' => $uid, 'expires_at' => date('c', $expires_at)]);
1328 return $access_token;
1331 $consumer_key = DI::config()->get('tumblr', 'consumer_key');
1332 $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
1334 $parameters = ['client_id' => $consumer_key, 'client_secret' => $consumer_secret];
1336 if (empty($refresh_token) && empty($code)) {
1337 $result = tumblr_exchange_token($uid);
1338 if (empty($result->refresh_token)) {
1339 Logger::info('Invalid result while exchanging token', ['uid' => $uid]);
1342 $expires_at = time() + $result->expires_in;
1343 Logger::debug('Updated token from OAuth1 to OAuth2', ['uid' => $uid, 'expires_at' => date('c', $expires_at)]);
1345 if (!empty($code)) {
1346 $parameters['code'] = $code;
1347 $parameters['grant_type'] = 'authorization_code';
1349 $parameters['refresh_token'] = $refresh_token;
1350 $parameters['grant_type'] = 'refresh_token';
1353 $curlResult = DI::httpClient()->post('https://api.tumblr.com/v2/oauth2/token', $parameters);
1354 if (!$curlResult->isSuccess()) {
1355 Logger::info('Error fetching token', ['uid' => $uid, 'code' => $code, 'result' => $curlResult->getBody(), 'parameters' => $parameters]);
1359 $result = json_decode($curlResult->getBody());
1360 if (empty($result)) {
1361 Logger::info('Invalid result when updating token', ['uid' => $uid]);
1365 $expires_at = time() + $result->expires_in;
1366 Logger::debug('Renewed token', ['uid' => $uid, 'expires_at' => date('c', $expires_at)]);
1369 DI::pConfig()->set($uid, 'tumblr', 'access_token', $result->access_token);
1370 DI::pConfig()->set($uid, 'tumblr', 'expires_at', $expires_at);
1371 DI::pConfig()->set($uid, 'tumblr', 'refresh_token', $result->refresh_token);
1373 return $result->access_token;
1377 * Create an OAuth2 token out of an OAuth1 token
1382 function tumblr_exchange_token(int $uid): stdClass
1384 $oauth_token = DI::pConfig()->get($uid, 'tumblr', 'oauth_token');
1385 $oauth_token_secret = DI::pConfig()->get($uid, 'tumblr', 'oauth_token_secret');
1387 $consumer_key = DI::config()->get('tumblr', 'consumer_key');
1388 $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
1390 $stack = HandlerStack::create();
1392 $middleware = new Oauth1([
1393 'consumer_key' => $consumer_key,
1394 'consumer_secret' => $consumer_secret,
1395 'token' => $oauth_token,
1396 'token_secret' => $oauth_token_secret
1399 $stack->push($middleware);
1402 $client = new Client([
1403 'base_uri' => 'https://api.tumblr.com/v2/',
1407 $response = $client->post('oauth2/exchange', ['auth' => 'oauth']);
1408 return json_decode($response->getBody()->getContents());
1409 } catch (RequestException $exception) {
1410 Logger::notice('Exchange failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
1411 return new stdClass;