]> git.mxchange.org Git - friendica-addons.git/blob - tumblr/tumblr.php
d8ab10fca111e10e2c88adc31770b13c8be0cb86
[friendica-addons.git] / tumblr / tumblr.php
1 <?php
2 /**
3  * Name: Tumblr Post Connector
4  * Description: Post to Tumblr
5  * Version: 2.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9
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;
22 use Friendica\DI;
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;
40
41 define('TUMBLR_DEFAULT_POLL_INTERVAL', 10); // given in minutes
42
43 function tumblr_install()
44 {
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');
62 }
63
64 function tumblr_load_config(ConfigFileManager $loader)
65 {
66         DI::app()->getConfigCache()->load($loader->loadAddonConfig('tumblr'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
67 }
68
69 function tumblr_check_item_notification(array &$notification_data)
70 {
71         if (!tumblr_enabled_for_user($notification_data['uid'])) { 
72                 return;
73         }
74
75         $page = tumblr_get_page($notification_data['uid']);
76         if (empty($page)) {
77                 return;
78         }
79
80         $own_user = Contact::selectFirst(['url', 'alias'], ['uid' => $notification_data['uid'], 'poll' => 'tumblr::'.$page]);
81         if ($own_user) {
82                 $notification_data['profiles'][] = $own_user['url'];
83                 $notification_data['profiles'][] = $own_user['alias'];
84         }
85 }
86
87 function tumblr_probe_detect(array &$hookData)
88 {
89         // Don't overwrite an existing result
90         if (isset($hookData['result'])) {
91                 return;
92         }
93
94         // Avoid a lookup for the wrong network
95         if (!in_array($hookData['network'], ['', Protocol::TUMBLR])) {
96                 return;
97         }
98
99         Logger::debug('Search for tumblr blog', ['url' => $hookData['uri']]);
100
101         $hookData['result'] = tumblr_get_contact_by_url($hookData['uri']);
102 }
103
104 function tumblr_item_by_link(array &$hookData)
105 {
106         // Don't overwrite an existing result
107         if (isset($hookData['item_id'])) {
108                 return;
109         }
110
111         if (!tumblr_enabled_for_user($hookData['uid'])) {
112                 return;
113         }
114
115         if (!preg_match('#^https?://www\.tumblr.com/blog/view/(.+)/(\d+).*#', $hookData['uri'], $matches) && !preg_match('#^https?://www\.tumblr.com/(.+)/(\d+).*#', $hookData['uri'], $matches)) {
116                 return;
117         }
118         
119         Logger::debug('Found tumblr post', ['url' => $hookData['uri'], 'blog' => $matches[1], 'id' => $matches[2]]);
120
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]]);
125                 return [];
126         }
127
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']);
131         }
132 }
133
134 function tumblr_support_follow(array &$data)
135 {
136         if ($data['protocol'] == Protocol::TUMBLR) {
137                 $data['result'] = true;
138         }
139 }
140
141 function tumblr_follow(array &$hook_data)
142 {
143         $uid = DI::userSession()->getLocalUserId();
144
145         if (!tumblr_enabled_for_user($uid)) {
146                 return;
147         }
148
149         Logger::debug('Check if contact is Tumblr', ['url' => $hook_data['url']]);
150
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']]);
154                 return;
155         }
156
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']]);
161         } else {
162                 Logger::notice('Following failed', ['meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'url' => $fields['url']]);
163         }
164 }
165
166 function tumblr_unfollow(array &$hook_data)
167 {
168         if (!tumblr_enabled_for_user($hook_data['uid'])) {
169                 return;
170         }
171
172         if (!tumblr_get_contact_uuid($hook_data['contact'])) {
173                 return;
174         }
175         $result = tumblr_post($hook_data['uid'], 'user/unfollow', ['url' => $hook_data['contact']['url']]);
176         $hook_data['result'] = ($result->meta->status <= 399);
177 }
178
179 function tumblr_block(array &$hook_data)
180 {
181         if (!tumblr_enabled_for_user($hook_data['uid'])) {
182                 return;
183         }
184
185         $uuid = tumblr_get_contact_uuid($hook_data['contact']);
186         if (!$uuid) {
187                 return;
188         }
189
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);
192
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']);
197                 }
198         }
199 }
200
201 function tumblr_unblock(array &$hook_data)
202 {
203         if (!tumblr_enabled_for_user($hook_data['uid'])) {
204                 return;
205         }
206
207         $uuid = tumblr_get_contact_uuid($hook_data['contact']);
208         if (!$uuid) {
209                 return;
210         }
211
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);
214 }
215
216 function tumblr_get_contact_uuid(array $contact): string
217 {
218         if (($contact['network'] != Protocol::TUMBLR) || (substr($contact['poll'], 0, 8) != 'tumblr::')) {
219                 return '';
220         }
221         return substr($contact['poll'], 8);
222 }
223
224 /**
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
227  * module.
228  */
229 function tumblr_module()
230 {
231 }
232
233 function tumblr_content()
234 {
235         if (!DI::userSession()->getLocalUserId()) {
236                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
237                 return;
238         }
239
240         switch (DI::args()->getArgv()[1] ?? '') {
241                 case 'connect':
242                         tumblr_connect();
243                         break;
244
245                 case 'redirect':
246                         tumblr_redirect();
247                         break;
248         }
249         DI::baseUrl()->redirect('settings/connectors/tumblr');
250 }
251
252 function tumblr_redirect()
253 {
254         if (($_REQUEST['state'] ?? '') != DI::session()->get('oauth_state')) {
255                 return;
256         }
257
258         tumblr_get_token(DI::userSession()->getLocalUserId(), $_REQUEST['code'] ?? '');
259 }
260
261 function tumblr_connect()
262 {
263         // Define the needed keys
264         $consumer_key    = DI::config()->get('tumblr', 'consumer_key');
265         $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
266
267         if (empty($consumer_key) || empty($consumer_secret)) {
268                 return;
269         }
270
271         $state = base64_encode(random_bytes(20));
272         DI::session()->set('oauth_state', $state);
273
274         $parameters = [
275                 'client_id'     => $consumer_key,
276                 'response_type' => 'code',
277                 'scope'         => 'basic write offline_access',
278                 'state'         => $state
279         ];
280
281         System::externalRedirect('https://www.tumblr.com/oauth2/authorize?' . http_build_query($parameters));
282 }
283
284 function tumblr_addon_admin(string &$o)
285 {
286         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/tumblr/');
287
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'), ''],
292         ]);
293 }
294
295 function tumblr_addon_admin_post()
296 {
297         DI::config()->set('tumblr', 'consumer_key', trim($_POST['consumer_key'] ?? ''));
298         DI::config()->set('tumblr', 'consumer_secret', trim($_POST['consumer_secret'] ?? ''));
299 }
300
301 function tumblr_settings(array &$data)
302 {
303         if (!DI::userSession()->getLocalUserId()) {
304                 return;
305         }
306
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);
310
311         $cachekey = 'tumblr-blogs-' . DI::userSession()->getLocalUserId();
312         $blogs = DI::cache()->get($cachekey);
313         if (empty($blogs)) {
314                 $blogs = tumblr_get_blogs(DI::userSession()->getLocalUserId());
315                 if (!empty($blogs)) {
316                         DI::cache()->set($cachekey, $blogs, Duration::HALF_HOUR);
317                 }
318         }
319
320         if (!empty($blogs)) {
321                 $page = tumblr_get_page(DI::userSession()->getLocalUserId(), $blogs);
322
323                 $page_select = ['tumblr_page', DI::l10n()->t('Post to page:'), $page, '', $blogs];
324         }
325
326         $t    = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/tumblr/');
327         $html = Renderer::replaceMacros($t, [
328                 '$l10n' => [
329                         'connect'   => DI::l10n()->t('(Re-)Authenticate your tumblr page'),
330                         'noconnect' => DI::l10n()->t('You are not authenticated to tumblr'),
331                 ],
332
333                 '$authenticate_url' => DI::baseUrl() . '/tumblr/connect',
334
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 ?? '',
339         ]);
340
341         $data = [
342                 'connector' => 'tumblr',
343                 'title'     => DI::l10n()->t('Tumblr Import/Export'),
344                 'image'     => 'images/tumblr.png',
345                 'enabled'   => $enabled,
346                 'html'      => $html,
347         ];
348 }
349
350 function tumblr_jot_nets(array &$jotnets_fields)
351 {
352         if (!DI::userSession()->getLocalUserId()) {
353                 return;
354         }
355
356         if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post')) {
357                 $jotnets_fields[] = [
358                         'type' => 'checkbox',
359                         'field' => [
360                                 'tumblr_enable',
361                                 DI::l10n()->t('Post to Tumblr'),
362                                 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default')
363                         ]
364                 ];
365         }
366 }
367
368 function tumblr_settings_post(array &$b)
369 {
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']));
375         }
376 }
377
378 function tumblr_cron()
379 {
380         $last = DI::keyValue()->get('tumblr_last_poll');
381
382         $poll_interval = intval(DI::config()->get('tumblr', 'poll_interval'));
383         if (!$poll_interval) {
384                 $poll_interval = TUMBLR_DEFAULT_POLL_INTERVAL;
385         }
386
387         if ($last) {
388                 $next = $last + ($poll_interval * 60);
389                 if ($next > time()) {
390                         Logger::notice('poll intervall not reached');
391                         return;
392                 }
393         }
394         Logger::notice('cron_start');
395
396         $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
397         if ($abandon_days < 1) {
398                 $abandon_days = 0;
399         }
400
401         $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
402
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']]);
408                                 continue;
409                         }
410                 }
411
412                 Logger::notice('importing timeline - start', ['user' => $pconfig['uid']]);
413                 tumblr_fetch_dashboard($pconfig['uid']);
414                 Logger::notice('importing timeline - done', ['user' => $pconfig['uid']]);
415         }
416
417         Logger::notice('cron_end');
418
419         DI::keyValue()->set('tumblr_last_poll', time());
420 }
421
422 function tumblr_hook_fork(array &$b)
423 {
424         if ($b['name'] != 'notifier_normal') {
425                 return;
426         }
427
428         $post = $b['data'];
429
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;
434                 return;
435         }
436
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;
442                         return;
443                 }
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;
447                 return;
448         }
449 }
450
451 function tumblr_post_local(array &$b)
452 {
453         if ($b['edit']) {
454                 return;
455         }
456
457         if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
458                 return;
459         }
460
461         if ($b['private'] || $b['parent']) {
462                 return;
463         }
464
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);
467
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'))) {
470                 $tmbl_enable = 1;
471         }
472
473         if (!$tmbl_enable) {
474                 return;
475         }
476
477         if (strlen($b['postopts'])) {
478                 $b['postopts'] .= ',';
479         }
480
481         $b['postopts'] .= 'tumblr';
482 }
483
484 function tumblr_send(array &$b)
485 {
486         if (($b['created'] !== $b['edited']) && !$b['deleted']) {
487                 return;
488         }
489
490         if ($b['gravity'] != Item::GRAVITY_PARENT) {
491                 Logger::debug('Got comment', ['item' => $b]);
492
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']]);
496                         return;
497                 }
498
499                 Logger::debug('Parent found', ['parent' => $parent]);
500
501                 $page = tumblr_get_page($b['uid']);
502
503                 if ($b['gravity'] == Item::GRAVITY_COMMENT) {
504                         Logger::notice('Commenting is not supported (yet)');
505                 } else {
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)) {
518                                         return;
519                                 }
520                                 $params = ['id' => $announce['id']];
521                                 $result = tumblr_post($b['uid'], 'blog/' . $page . '/post/delete', $params);
522                         } else {
523                                 // Unsupported activity
524                                 return;
525                         }
526
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']]);
531                                 }
532                         } else {
533                                 Logger::notice('Error while performing activity', ['verb' => $b['verb'], 'deleted' => $b['deleted'], 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
534                         }
535                 }
536                 return;
537         } elseif ($b['private'] || !strstr($b['postopts'], 'tumblr')) {
538                 return;
539         }
540
541         if (!tumblr_send_npf($b)) {
542                 tumblr_send_legacy($b);
543         }
544 }
545
546 function tumblr_send_legacy(array $b)
547 {
548         $b['body'] = BBCode::removeAttachment($b['body']);
549
550         $title = trim($b['title']);
551
552         $media = Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML, Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]);
553
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'));
558
559         $params = [
560                 'state'  => 'published',
561                 'tags'   => implode(',', array_column(Tag::getByURIId($b['uri-id']), 'name')),
562                 'tweet'  => 'off',
563                 'format' => 'html',
564         ];
565
566         $body = BBCode::removeShareInformation($b['body']);
567         $body = Post\Media::removeFromEndOfBody($body);
568
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);
579                                         }
580                                 }
581                         }
582                 }
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);
588
589                 if (!empty($media[$link]['preview'])) {
590                         $params['thumbnail'] = $media[$link]['preview'];
591                 }
592                 if (!empty($media[$link]['description'])) {
593                         $params['excerpt'] = $media[$link]['description'];
594                 }
595                 if (!empty($media[$link]['author-name'])) {
596                         $params['author'] = $media[$link]['author-name'];
597                 }
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);
606         } else {
607                 $params['type']  = 'text';
608                 $params['title'] = $title;
609                 $params['body']  = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
610         }
611
612         if (isset($params['caption']) && (trim($title) != '')) {
613                 $params['caption'] = '<h1>' . $title . '</h1>' .
614                         '<p>' . $params['caption'] . '</p>';
615         }
616
617         $page = tumblr_get_page($b['uid']);
618
619         $result = tumblr_post($b['uid'], 'blog/' . $page . '/post', $params);
620
621         if ($result->meta->status < 400) {
622                 Logger::info('Success (legacy)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response]);
623         } else {
624                 Logger::notice('Error posting blog (legacy)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
625         }
626 }
627
628 function tumblr_send_npf(array $post): bool
629 {
630         $page = tumblr_get_page($post['uid']);
631
632         if (empty($page)) {
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.
635                 return true;
636         }
637
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'];
641         }
642
643         $params = [
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'
650         ];
651
652         $result = tumblr_post($post['uid'], 'blog/' . $page . '/posts', $params);
653
654         if ($result->meta->status < 400) {
655                 Logger::info('Success (NPF)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response]);
656                 return true;
657         } else {
658                 Logger::notice('Error posting blog (NPF)', ['blog' => $page, 'meta' => $result->meta, 'response' => $result->response, 'errors' => $result->errors, 'params' => $params]);
659                 return false;
660         }
661 }
662
663 function tumblr_get_post_from_uri(string $uri): array
664 {
665         $parts = explode(':', $uri);
666         if (($parts[0] != 'tumblr') || empty($parts[2])) {
667                 return [];
668         }
669
670         $post['id']        = $parts[2];
671         $post['reblog_key'] = $parts[3] ?? '';
672
673         $post['reblog_key'] = str_replace('@t', '', $post['reblog_key']); // Temp
674         return $post;
675 }
676
677 /**
678  * Fetch the dashboard (timeline) for the given user
679  *
680  * @param integer $uid
681  * @return void
682  */
683 function tumblr_fetch_dashboard(int $uid)
684 {
685         $page = tumblr_get_page($uid);
686
687         $parameters = ['reblog_info' => false, 'notes_info' => false, 'npf' => false];
688
689         $last = DI::pConfig()->get($uid, 'tumblr', 'last_id');
690         if (!empty($last)) {
691                 $parameters['since_id'] = $last;
692         }
693
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]);
697                 return [];
698         }
699
700         if (empty($dashboard->response->posts)) {
701                 return;
702         }
703
704         foreach (array_reverse($dashboard->response->posts) as $post) {
705                 $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
706
707                 if ($post->id > $last) {
708                         $last = $post->id;
709                 }
710
711                 Logger::debug('Importing post', ['uid' => $uid, 'created' => date(DateTimeFormat::MYSQL, $post->timestamp), 'uri' => $uri]);
712
713                 if (Post::exists(['uri' => $uri, 'uid' => $uid]) || ($post->blog->uuid == $page)) {
714                         DI::pConfig()->set($uid, 'tumblr', 'last_id', $last);
715                         continue;
716                 }
717
718                 tumblr_process_post($post, $uid, $uri);
719
720
721                 DI::pConfig()->set($uid, 'tumblr', 'last_id', $last);
722         }
723 }
724
725 function tumblr_process_post(stdClass $post, int $uid, string $uri = ''): int
726 {
727         if (empty($uri)) {
728                 $uri = 'tumblr::' . $post->id_string . ':' . $post->reblog_key;
729         }
730
731         $item = tumblr_get_header($post, $uri, $uid);
732
733         $item = tumblr_get_content($item, $post);
734
735         $id = item::insert($item);
736
737         if ($id) {
738                 $stored = Post::selectFirst(['uri-id'], ['id' => $id]);
739
740                 if (!empty($post->tags)) {
741                         foreach ($post->tags as $tag) {
742                                 Tag::store($stored['uri-id'], Tag::HASHTAG, $tag);
743                         }
744                 }
745         }
746         return $id;
747 }
748
749 /**
750  * Sets the initial data for the item array
751  *
752  * @param stdClass $post
753  * @param string $uri
754  * @param integer $uid
755  * @return array
756  */
757 function tumblr_get_header(stdClass $post, string $uri, int $uid): array
758 {
759         $contact = tumblr_get_contact($post->blog, $uid);
760         $item = [
761                 'network'       => Protocol::TUMBLR,
762                 'uid'           => $uid,
763                 'wall'          => false,
764                 'uri'           => $uri,
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)
773         ];
774
775         $item['owner-name']   = $item['author-name'];
776         $item['owner-link']   = $item['author-link'];
777         $item['owner-avatar'] = $item['author-avatar'];
778
779         return $item;
780 }
781
782 /**
783  * Set the body according the given content type
784  *
785  * @param array $item
786  * @param stdClass $post
787  * @return array
788  */
789 function tumblr_get_content(array $item, stdClass $post): array
790 {
791         switch ($post->type) {
792                 case 'text':
793                         $item['title'] = $post->title;
794                         $item['body'] = HTML::toBBCode(tumblr_add_npf_data($post->body, $post->post_url));
795                         break;
796
797                 case 'quote':
798                         if (empty($post->text)) {
799                                 $body = HTML::toBBCode($post->text) . "\n";
800                         } else {
801                                 $body = '';
802                         }
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";
807                         }
808                         $body .= '[quote]' . HTML::toBBCode($post->source) . '[/quote]';
809                         $item['body'] = $body;
810                         break;
811
812                 case 'link':
813                         $item['body'] = HTML::toBBCode($post->description) . "\n" . PageInfo::getFooterFromUrl($post->url);
814                         break;
815
816                 case 'answer':
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";
821                         } else {
822                                 $body = '';
823                         }
824                         $body .= '[quote]' . HTML::toBBCode($post->question) . "[/quote]\n" . HTML::toBBCode($post->answer);
825                         $item['body'] = $body;
826                         break;
827
828                 case 'video':
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";
840                         }
841                         break;
842
843                 case 'audio':
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";
849                         }
850                         break;
851
852                 case 'photo':
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]";
859                                 }
860                         }
861                         break;
862
863                 case 'chat':
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]";
868                         }
869                         $item['body'] .= "[/ul]\n";
870                         break;
871         }
872         return $item;
873 }
874
875 function tumblr_add_npf_data(string $html, string $plink): string
876 {
877         $doc = new DOMDocument();
878
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);
885                 if (empty($data)) {
886                         continue;
887                 }
888
889                 tumblr_replace_with_npf($doc, $node, tumblr_get_type_replacement($data, $plink));
890         }
891
892         $list = $xpath->query('//div[@data-npf]');
893         foreach ($list as $node) {
894                 $data = tumblr_get_npf_data($node);
895                 if (empty($data)) {
896                         continue;
897                 }
898
899                 tumblr_replace_with_npf($doc, $node, tumblr_get_type_replacement($data, $plink));
900         }
901
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'])) {
906                         continue;
907                 }
908                 tumblr_replace_with_npf($doc, $node, '[youtube]' . $attributes['data-url'] . '[/youtube]');
909         }
910
911         $list = $xpath->query('//figure[@data-npf]');
912         foreach ($list as $node) {
913                 $data = tumblr_get_npf_data($node);
914                 if (empty($data)) {
915                         continue;
916                 }
917                 tumblr_replace_with_npf($doc, $node, tumblr_get_type_replacement($data, $plink));
918         }
919
920         return $doc->saveHTML();
921 }
922
923 function tumblr_replace_with_npf(DOMDocument $doc, DOMNode $node, string $replacement)
924 {
925         if (empty($replacement)) {
926                 return;
927         }
928         $replace = $doc->createTextNode($replacement);
929         $node->parentNode->insertBefore($replace, $node);
930         $node->parentNode->removeChild($node);
931 }
932
933 function tumblr_get_npf_data(DOMNode $node): array
934 {
935         $attributes = tumblr_get_attributes($node);
936         if (empty($attributes['data-npf'])) {
937                 return [];
938         }
939
940         return json_decode($attributes['data-npf'], true);
941 }
942
943 function tumblr_get_attributes($node): array
944 {
945         if (empty($node->attributes)) {
946                 return [];
947         }
948
949         $attributes = [];
950         foreach ($node->attributes as $key => $attribute) {
951                 $attributes[$key] = trim($attribute->value);
952         }
953         return $attributes;
954 }
955
956 function tumblr_get_type_replacement(array $data, string $plink): string
957 {
958         switch ($data['type']) {
959                 case 'poll':
960                         $body = '[p][url=' . $plink . ']' . $data['question'] . '[/url][/p][ul]';
961                         foreach ($data['answers'] as $answer) {
962                                 $body .= '[li]' . $answer['answer_text'] . '[/li]';
963                         }
964                         $body .= '[/ul]';
965                         break;
966
967                 case 'link':
968                         $body = PageInfo::getFooterFromUrl(str_replace('https://href.li/?', '', $data['url']));
969                         break;
970
971                 case 'video':
972                         if (!empty($data['url']) && ($data['provider'] == 'tumblr')) {
973                                 $body = '[video]' . $data['url'] . '[/video]';
974                                 break;
975                         }
976
977                 default:
978                         Logger::notice('Unknown type', ['type' => $data['type'], 'data' => $data, 'plink' => $plink]);
979                         $body = '';
980         }
981
982         return $body;
983 }
984
985 /**
986  * Get a contact array for the given blog
987  *
988  * @param stdClass $blog
989  * @param integer $uid
990  * @return array
991  */
992 function tumblr_get_contact(stdClass $blog, int $uid): array
993 {
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)) {
997                 return $contact;
998         }
999         if (empty($contact)) {
1000                 $cid = tumblr_insert_contact($blog, $uid);
1001         } else {
1002                 $cid = $contact['id'];
1003         }
1004
1005         $condition['uid'] = 0;
1006
1007         $contact = Contact::selectFirst([], $condition);
1008         if (empty($contact)) {
1009                 $pcid = tumblr_insert_contact($blog, 0);
1010         } else {
1011                 $pcid = $contact['id'];
1012         }
1013
1014         tumblr_update_contact($blog, $uid, $cid, $pcid);
1015
1016         return Contact::getById($cid);
1017 }
1018
1019 /**
1020  * Create a new contact
1021  *
1022  * @param stdClass $blog
1023  * @param integer $uid
1024  * @return void
1025  */
1026 function tumblr_insert_contact(stdClass $blog, int $uid)
1027 {
1028         $baseurl = 'https://tumblr.com';
1029         $url     = $baseurl . '/' . $blog->name;
1030
1031         $fields = [
1032                 'uid'      => $uid,
1033                 'network'  => Protocol::TUMBLR,
1034                 'poll'     => 'tumblr::' . $blog->uuid,
1035                 'baseurl'  => $baseurl,
1036                 'priority' => 1,
1037                 'writable' => true,
1038                 'blocked'  => false,
1039                 'readonly' => false,
1040                 'pending'  => false,
1041                 'url'      => $url,
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)
1049         ];
1050         return Contact::insert($fields);
1051 }
1052
1053 /**
1054  * Updates the given contact for the given user and proviced contact ids
1055  *
1056  * @param stdClass $blog
1057  * @param integer $uid
1058  * @param integer $cid
1059  * @param integer $pcid
1060  * @return void
1061  */
1062 function tumblr_update_contact(stdClass $blog, int $uid, int $cid, int $pcid)
1063 {
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]);
1067                 return;
1068         }
1069
1070         $avatar = $info->response->blog->avatar;
1071         if (!empty($avatar)) {
1072                 Contact::updateAvatar($cid, $avatar[0]->url);
1073         }
1074
1075         $baseurl = 'https://tumblr.com';
1076         $url     = $baseurl . '/' . $info->response->blog->name;
1077
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;
1084         } else {
1085                 $rel = Contact::NOTHING;
1086         }
1087
1088         $uri_id = ItemURI::getIdByURI($url);
1089         $fields = [
1090                 'url'     => $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,
1100                 'rel'     => $rel,
1101         ];
1102
1103         Contact::update($fields, ['id' => $cid]);
1104
1105         $fields['rel'] = Contact::NOTHING;
1106         Contact::update($fields, ['id' => $pcid]);
1107 }
1108
1109 /**
1110  * Get the default page for posting. Detects the value if not provided or has got a bad value.
1111  *
1112  * @param integer $uid
1113  * @param array $blogs
1114  * @return string
1115  */
1116 function tumblr_get_page(int $uid, array $blogs = []): string
1117 {
1118         $page = DI::pConfig()->get($uid, 'tumblr', 'page');
1119
1120         if (!empty($page) && (strpos($page, '/') === false)) {
1121                 return $page;
1122         }
1123
1124         if (empty($blogs)) {
1125                 $blogs = tumblr_get_blogs($uid);
1126         }
1127
1128         if (!empty($blogs)) {
1129                 $page = array_key_first($blogs);
1130                 DI::pConfig()->set($uid, 'tumblr', 'page', $page);
1131                 return $page;
1132         }
1133
1134         return '';
1135 }
1136
1137 /**
1138  * Get an array of blogs for the given user
1139  *
1140  * @param integer $uid
1141  * @return array
1142  */
1143 function tumblr_get_blogs(int $uid): array
1144 {
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]);
1148                 return [];
1149         }
1150
1151         $blogs = [];
1152         foreach ($userinfo->response->user->blogs as $blog) {
1153                 $blogs[$blog->uuid] = $blog->name;
1154         }
1155         return $blogs;
1156 }
1157
1158 function tumblr_enabled_for_user(int $uid) 
1159 {
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'));
1164 }
1165
1166 /**
1167  * Get a contact array from a Tumblr url
1168  *
1169  * @param string $url
1170  * @return array
1171  */
1172 function tumblr_get_contact_by_url(string $url): array
1173 {
1174         $consumer_key = DI::config()->get('tumblr', 'consumer_key');
1175         if (empty($consumer_key)) {
1176                 return [];
1177         }
1178
1179         if (!preg_match('#^https?://tumblr.com/(.+)#', $url, $matches) && !preg_match('#^https?://www\.tumblr.com/(.+)#', $url, $matches) && !preg_match('#^https?://(.+)\.tumblr.com#', $url, $matches)) {
1180                 try {
1181                         $curlResult = DI::httpClient()->get($url);
1182                 } catch (\Exception $e) {
1183                         return [];
1184                 }
1185                 $html = $curlResult->getBody();
1186                 if (empty($html)) {
1187                         return [];
1188                 }
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'] ?? '';
1195         } else {
1196                 $blogs = explode('/', $matches[1]);
1197                 $blog = $blogs[0] ?? '';
1198         }
1199
1200         if (empty($blog)) {
1201                 return [];
1202         }
1203
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);
1207         if (empty($data)) {
1208                 return [];
1209         }
1210
1211         $baseurl = 'https://tumblr.com';
1212         $url     = $baseurl . '/' . $data->response->blog->name;
1213
1214         return [
1215                 'url'      => $url,
1216                 'nurl'     => Strings::normaliseLink($url),
1217                 'addr'     => $data->response->blog->name . '@tumblr.com',
1218                 'alias'    => $data->response->blog->url,
1219                 'batch'    => '',
1220                 'notify'   => '',
1221                 'poll'     => 'tumblr::' . $data->response->blog->uuid,
1222                 'poco'     => '',
1223                 'name'     => $data->response->blog->title,
1224                 'nick'     => $data->response->blog->name,
1225                 'network'  => Protocol::TUMBLR,
1226                 'baseurl'  => $baseurl,
1227                 'pubkey'   => '',
1228                 'priority' => 0,
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,
1233         ];
1234 }
1235
1236 /**
1237  * Perform an OAuth2 GET request
1238  *
1239  * @param integer $uid
1240  * @param string $url
1241  * @param array $parameters
1242  * @return stdClass
1243  */
1244 function tumblr_get(int $uid, string $url, array $parameters = []): stdClass
1245 {
1246         $url = 'https://api.tumblr.com/v2/' . $url;
1247
1248         if (!empty($parameters)) {
1249                 $url .= '?' . http_build_query($parameters);
1250         }
1251
1252         $curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . tumblr_get_token($uid)]]]);
1253         return tumblr_format_result($curlResult);
1254 }
1255
1256 /**
1257  * Perform an OAuth2 POST request
1258  *
1259  * @param integer $uid
1260  * @param string $url
1261  * @param array $parameters
1262  * @return stdClass
1263  */
1264 function tumblr_post(int $uid, string $url, array $parameters): stdClass
1265 {
1266         $url = 'https://api.tumblr.com/v2/' . $url;
1267
1268         $curlResult = DI::httpClient()->post($url, $parameters, ['Authorization' => ['Bearer ' . tumblr_get_token($uid)]]);
1269         return tumblr_format_result($curlResult);
1270 }
1271
1272 /**
1273  * Perform an OAuth2 DELETE request
1274  *
1275  * @param integer $uid
1276  * @param string $url
1277  * @param array $parameters
1278  * @return stdClass
1279  */
1280 function tumblr_delete(int $uid, string $url, array $parameters): stdClass
1281 {
1282         $url = 'https://api.tumblr.com/v2/' . $url;
1283
1284         $opts = [
1285                 HttpClientOptions::HEADERS     => ['Authorization' => ['Bearer ' . tumblr_get_token($uid)]],
1286                 HttpClientOptions::FORM_PARAMS => $parameters
1287         ];
1288
1289         $curlResult = DI::httpClient()->request('delete', $url, $opts);
1290         return tumblr_format_result($curlResult);
1291 }
1292
1293 /**
1294  * Format the get/post result value
1295  *
1296  * @param ICanHandleHttpResponses $curlResult
1297  * @return stdClass
1298  */
1299 function tumblr_format_result(ICanHandleHttpResponses $curlResult): stdClass
1300 {
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       = [];
1309         }
1310         return $result;
1311 }
1312
1313 /**
1314  * Fetch the OAuth token, update it if needed
1315  *
1316  * @param integer $uid
1317  * @param string $code
1318  * @return string
1319  */
1320 function tumblr_get_token(int $uid, string $code = ''): string
1321 {
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');
1325
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;
1329         }
1330
1331         $consumer_key    = DI::config()->get('tumblr', 'consumer_key');
1332         $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
1333
1334         $parameters = ['client_id' => $consumer_key, 'client_secret' => $consumer_secret];
1335
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]);
1340                         return '';
1341                 }
1342                 $expires_at = time() + $result->expires_in;
1343                 Logger::debug('Updated token from OAuth1 to OAuth2', ['uid' => $uid, 'expires_at' => date('c', $expires_at)]);
1344         } else {
1345                 if (!empty($code)) {
1346                         $parameters['code']       = $code;
1347                         $parameters['grant_type'] = 'authorization_code';
1348                 } else {
1349                         $parameters['refresh_token'] = $refresh_token;
1350                         $parameters['grant_type']    = 'refresh_token';
1351                 }
1352
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]);
1356                         return '';
1357                 }
1358         
1359                 $result = json_decode($curlResult->getBody());
1360                 if (empty($result)) {
1361                         Logger::info('Invalid result when updating token', ['uid' => $uid]);
1362                         return '';
1363                 }
1364         
1365                 $expires_at = time() + $result->expires_in;
1366                 Logger::debug('Renewed token', ['uid' => $uid, 'expires_at' => date('c', $expires_at)]);
1367         }
1368
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);
1372
1373         return $result->access_token;
1374 }
1375
1376 /**
1377  * Create an OAuth2 token out of an OAuth1 token
1378  *
1379  * @param int $uid
1380  * @return stdClass
1381  */
1382 function tumblr_exchange_token(int $uid): stdClass
1383 {
1384         $oauth_token        = DI::pConfig()->get($uid, 'tumblr', 'oauth_token');
1385         $oauth_token_secret = DI::pConfig()->get($uid, 'tumblr', 'oauth_token_secret');
1386
1387         $consumer_key    = DI::config()->get('tumblr', 'consumer_key');
1388         $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
1389
1390         $stack = HandlerStack::create();
1391
1392         $middleware = new Oauth1([
1393                 'consumer_key'    => $consumer_key,
1394                 'consumer_secret' => $consumer_secret,
1395                 'token'           => $oauth_token,
1396                 'token_secret'    => $oauth_token_secret
1397         ]);
1398
1399         $stack->push($middleware);
1400
1401         try {
1402                 $client = new Client([
1403                         'base_uri' => 'https://api.tumblr.com/v2/',
1404                         'handler' => $stack
1405                 ]);
1406
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;
1412         }
1413 }