3 * Name: pump.io Post Connector
4 * Description: Bidirectional (posting, relaying and reading) connector for pump.io.
6 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Content\Text\HTML;
12 use Friendica\Core\Addon;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Protocol;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Worker;
18 use Friendica\Database\DBA;
20 use Friendica\Model\Contact;
21 use Friendica\Model\Group;
22 use Friendica\Model\Item;
23 use Friendica\Model\Post;
24 use Friendica\Model\User;
25 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
26 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
27 use Friendica\Protocol\Activity;
28 use Friendica\Protocol\ActivityNamespace;
29 use Friendica\Core\Config\Util\ConfigFileLoader;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Strings;
32 use Friendica\Util\XML;
34 require 'addon/pumpio/oauth/http.php';
35 require 'addon/pumpio/oauth/oauth_client.php';
37 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
39 function pumpio_install()
41 Hook::register('load_config', 'addon/pumpio/pumpio.php', 'pumpio_load_config');
42 Hook::register('hook_fork', 'addon/pumpio/pumpio.php', 'hook_fork');
43 Hook::register('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
44 Hook::register('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
45 Hook::register('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
46 Hook::register('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
47 Hook::register('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
48 Hook::register('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
49 Hook::register('check_item_notification', 'addon/pumpio/pumpio.php', 'pumpio_check_item_notification');
53 * This is a statement rather than an actual function definition. The simple
54 * existence of this method is checked to figure out if the addon offers a
57 function pumpio_module() {}
59 function pumpio_content(App $a)
61 if (!DI::userSession()->getLocalUserId()) {
62 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
66 if (isset(DI::args()->getArgv()[1])) {
67 switch (DI::args()->getArgv()[1]) {
69 $o = pumpio_connect($a);
73 $o = print_r(DI::args()->getArgv(), true);
77 $o = pumpio_connect($a);
82 function pumpio_check_item_notification(App $a, array &$notification_data)
84 $hostname = DI::pConfig()->get($notification_data['uid'], 'pumpio', 'host');
85 $username = DI::pConfig()->get($notification_data['uid'], 'pumpio', 'user');
87 $notification_data['profiles'][] = 'https://' . $hostname . '/' . $username;
90 function pumpio_registerclient(App $a, $host)
92 $url = 'https://' . $host . '/api/client/register';
96 $application_name = DI::config()->get('pumpio', 'application_name');
98 if ($application_name == '') {
99 $application_name = DI::baseUrl()->getHostname();
102 $firstAdmin = User::getFirstAdmin(['email']);
104 $params['type'] = 'client_associate';
105 $params['contacts'] = $firstAdmin['email'];
106 $params['application_type'] = 'native';
107 $params['application_name'] = $application_name;
108 $params['logo_url'] = DI::baseUrl()->get() . '/images/friendica-256.png';
109 $params['redirect_uris'] = DI::baseUrl()->get() . '/pumpio/connect';
111 Logger::info('pumpio_registerclient: ' . $url . ' parameters', $params);
113 // @TODO Rewrite this to our own HTTP client
114 $ch = curl_init($url);
115 curl_setopt($ch, CURLOPT_HEADER, false);
116 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
117 curl_setopt($ch, CURLOPT_POST,1);
118 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
119 curl_setopt($ch, CURLOPT_USERAGENT, 'Friendica');
122 $curl_info = curl_getinfo($ch);
124 if ($curl_info['http_code'] == '200') {
125 $values = json_decode($s);
126 Logger::info('pumpio_registerclient: success ', $values);
129 Logger::info('pumpio_registerclient: failed: ', $curl_info);
134 function pumpio_connect(App $a)
136 // Define the needed keys
137 $consumer_key = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_key');
138 $consumer_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_secret');
139 $hostname = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'host');
141 if ((($consumer_key == '') || ($consumer_secret == '')) && ($hostname != '')) {
142 Logger::notice('pumpio_connect: register client');
143 $clientdata = pumpio_registerclient($a, $hostname);
144 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_key', $clientdata->client_id);
145 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_secret', $clientdata->client_secret);
147 $consumer_key = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_key');
148 $consumer_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_secret');
150 Logger::info('pumpio_connect: ckey: ' . $consumer_key . ' csecrect: ' . $consumer_secret);
153 if (($consumer_key == '') || ($consumer_secret == '')) {
154 Logger::notice('pumpio_connect: '.sprintf('Unable to register the client at the pump.io server "%s".', $hostname));
156 return DI::l10n()->t("Unable to register the client at the pump.io server '%s'.", $hostname);
159 // The callback URL is the script that gets called after the user authenticates with pumpio
160 $callback_url = DI::baseUrl()->get() . '/pumpio/connect';
162 // Let's begin. First we need a Request Token. The request token is required to send the user
163 // to pumpio's login page.
165 // Create a new instance of the oauth_client_class library. For this step, all we need to give the library is our
166 // Consumer Key and Consumer Secret
167 $client = new oauth_client_class;
169 $client->server = '';
170 $client->oauth_version = '1.0a';
171 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
172 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
173 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
174 $client->url_parameters = false;
175 $client->authorization_header = true;
176 $client->redirect_uri = $callback_url;
177 $client->client_id = $consumer_key;
178 $client->client_secret = $consumer_secret;
180 if (($success = $client->Initialize())) {
181 if (($success = $client->Process())) {
182 if (strlen($client->access_token)) {
183 Logger::info('pumpio_connect: otoken: ' . $client->access_token . ', osecrect: ' . $client->access_token_secret);
184 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'oauth_token', $client->access_token);
185 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'oauth_token_secret', $client->access_token_secret);
188 $success = $client->Finalize($success);
191 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
195 Logger::notice('pumpio_connect: authenticated');
196 $o = DI::l10n()->t('You are now authenticated to pumpio.');
197 $o .= '<br /><a href="' . DI::baseUrl()->get() . '/settings/connectors">' . DI::l10n()->t('return to the connector page') . '</a>';
199 Logger::notice('pumpio_connect: could not connect');
200 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
206 function pumpio_jot_nets(App $a, array &$jotnets_fields)
208 if (!DI::userSession()->getLocalUserId()) {
212 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'post')) {
213 $jotnets_fields[] = [
214 'type' => 'checkbox',
217 DI::l10n()->t('Post to pumpio'),
218 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'post_by_default')
224 function pumpio_settings(App $a, array &$data)
226 if (!DI::userSession()->getLocalUserId()) {
230 $pumpio_host = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'host');
231 $pumpio_user = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'user');
232 $oauth_token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'oauth_token');
233 $oauth_token_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'oauth_token_secret');
235 $import_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'import', false);
236 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'post', false);
237 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'post_by_default', false);
238 $public_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'public', false);
239 $mirror_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'mirror', false);
241 $submit = ['pumpio-submit' => DI::l10n()->t('Save Settings')];
242 if ($oauth_token && $oauth_token_secret) {
243 $submit['pumpio-delete'] = DI::l10n()->t('Delete this preset');
246 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/pumpio/');
247 $html = Renderer::replaceMacros($t, [
249 'authenticate' => DI::l10n()->t('Authenticate your pump.io connection'),
251 '$pumpio_host' => $pumpio_host,
252 '$pumpio_user' => $pumpio_user,
253 '$oauth_token' => $oauth_token,
254 '$oauth_token_secret' => $oauth_token_secret,
255 '$authenticate_url' => DI::baseUrl()->get() . '/pumpio/connect',
256 '$servername' => ['pumpio_host', DI::l10n()->t('Pump.io servername (without "http://" or "https://" )'), $pumpio_host],
257 '$username' => ['pumpio_user', DI::l10n()->t('Pump.io username (without the servername)'), $pumpio_user],
258 '$import' => ['pumpio_import', DI::l10n()->t('Import the remote timeline'), $import_enabled],
259 '$enabled' => ['pumpio', DI::l10n()->t('Enable Pump.io Post Addon'), $enabled],
260 '$bydefault' => ['pumpio_bydefault', DI::l10n()->t('Post to Pump.io by default'), $def_enabled],
261 '$public' => ['pumpio_public', DI::l10n()->t('Should posts be public?'), $public_enabled],
262 '$mirror' => ['pumpio_mirror', DI::l10n()->t('Mirror all public posts'), $mirror_enabled],
266 'connector' => 'pumpio',
267 'title' => DI::l10n()->t('Pump.io Import/Export/Mirror'),
268 'image' => 'images/pumpio.png',
269 'enabled' => $enabled,
275 function pumpio_settings_post(App $a, array &$b)
277 if (!empty($_POST['pumpio_delete'])) {
278 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_key' , '');
279 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'consumer_secret' , '');
280 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'oauth_token' , '');
281 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'oauth_token_secret', '');
282 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'post' , false);
283 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'import' , false);
284 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'host' , '');
285 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'user' , '');
286 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'public' , false);
287 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'mirror' , false);
288 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'post_by_default' , false);
289 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'lastdate' , 0);
290 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'last_id' , '');
291 } elseif (!empty($_POST['pumpio-submit'])) {
292 // filtering the username if it is filled wrong
293 $user = $_POST['pumpio_user'];
294 if (strstr($user, '@')) {
295 $pos = strpos($user, '@');
298 $user = substr($user, 0, $pos);
302 // Filtering the hostname if someone is entering it with "http"
303 $host = $_POST['pumpio_host'];
305 $host = str_replace(['https://', 'http://'], ['', ''], $host);
307 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'post' , $_POST['pumpio'] ?? false);
308 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'import' , $_POST['pumpio_import'] ?? false);
309 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'host' , $host);
310 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'user' , $user);
311 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'public' , $_POST['pumpio_public'] ?? false);
312 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'mirror' , $_POST['pumpio_mirror'] ?? false);
313 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pumpio', 'post_by_default', $_POST['pumpio_bydefault'] ?? false);
315 if (!empty($_POST['pumpio_mirror'])) {
316 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'pumpio', 'lastdate');
321 function pumpio_load_config(App $a, ConfigFileLoader $loader)
323 $a->getConfigCache()->load($loader->loadAddonConfig('pumpio'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
326 function pumpio_hook_fork(App $a, array &$b)
328 if ($b['name'] != 'notifier_normal') {
334 // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
335 if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
336 $b['execute'] = false;
340 // if post comes from pump.io don't send it back
341 if ($post['app'] == 'pump.io') {
342 $b['execute'] = false;
346 if (DI::pConfig()->get($post['uid'], 'pumpio', 'import')) {
347 // Don't fork if it isn't a reply to a pump.io post
348 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::PUMPIO])) {
349 Logger::notice('No pump.io parent found for item ' . $post['id']);
350 $b['execute'] = false;
354 // Comments are never exported when we don't import the pumpio timeline
355 if (!strstr($post['postopts'], 'pumpio') || ($post['parent'] != $post['id']) || $post['private']) {
356 $b['execute'] = false;
362 function pumpio_post_local(App $a, array &$b)
364 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
368 $pumpio_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'post'));
370 $pumpio_enable = (($pumpio_post && !empty($_REQUEST['pumpio_enable'])) ? intval($_REQUEST['pumpio_enable']) : 0);
372 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pumpio', 'post_by_default'))) {
376 if (!$pumpio_enable) {
380 if (strlen($b['postopts'])) {
381 $b['postopts'] .= ',';
384 $b['postopts'] .= 'pumpio';
387 function pumpio_send(App $a, array &$b)
389 if (!DI::pConfig()->get($b['uid'], 'pumpio', 'import') && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
393 Logger::debug('pumpio_send: parameter ', $b);
395 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
397 if ($b['parent'] != $b['id']) {
398 // Looking if its a reply to a pumpio post
399 $condition = ['id' => $b['parent'], 'network' => Protocol::PUMPIO];
400 $orig_post = Post::selectFirst([], $condition);
402 if (!DBA::isResult($orig_post)) {
403 Logger::notice('pumpio_send: no pumpio post ' . $b['parent']);
411 $receiver = pumpio_getreceiver($a, $b);
413 Logger::notice('pumpio_send: receiver ', $receiver);
415 if (!count($receiver) && ($b['private'] || !strstr($b['postopts'], 'pumpio'))) {
419 // Dont't post if the post doesn't belong to us.
420 // This is a check for forum postings
421 $self = User::getOwnerDataById($b['uid']);
422 if ($b['contact-id'] != $self['id']) {
427 if ($b['verb'] == Activity::LIKE) {
429 pumpio_action($a, $b['uid'], $b['thr-parent'], 'unlike');
431 pumpio_action($a, $b['uid'], $b['thr-parent'], 'like');
436 if ($b['verb'] == Activity::DISLIKE) {
440 if (($b['verb'] == Activity::POST) && ($b['created'] !== $b['edited']) && !$b['deleted']) {
441 pumpio_action($a, $b['uid'], $b['uri'], 'update', $b['body']);
444 if (($b['verb'] == Activity::POST) && $b['deleted']) {
445 pumpio_action($a, $b['uid'], $b['uri'], 'delete');
448 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
452 // if post comes from pump.io don't send it back
453 if ($b['app'] == 'pump.io') {
458 // Support for native shares
459 // http://<hostname>/api/<type>/shares?id=<the-object-id>
461 $oauth_token = DI::pConfig()->get($b['uid'], 'pumpio', 'oauth_token');
462 $oauth_token_secret = DI::pConfig()->get($b['uid'], 'pumpio', 'oauth_token_secret');
463 $consumer_key = DI::pConfig()->get($b['uid'], 'pumpio', 'consumer_key');
464 $consumer_secret = DI::pConfig()->get($b['uid'], 'pumpio', 'consumer_secret');
466 $host = DI::pConfig()->get($b['uid'], 'pumpio', 'host');
467 $user = DI::pConfig()->get($b['uid'], 'pumpio', 'user');
468 $public = DI::pConfig()->get($b['uid'], 'pumpio', 'public');
470 if ($oauth_token && $oauth_token_secret) {
471 $title = trim($b['title']);
473 $content = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
477 $params['verb'] = 'post';
480 $params['object'] = [
481 'objectType' => 'note',
482 'content' => $content];
484 if (!empty($title)) {
485 $params['object']['displayName'] = $title;
488 if (!empty($receiver['to'])) {
489 $params['to'] = $receiver['to'];
492 if (!empty($receiver['bto'])) {
493 $params['bto'] = $receiver['bto'];
496 if (!empty($receiver['cc'])) {
497 $params['cc'] = $receiver['cc'];
500 if (!empty($receiver['bcc'])) {
501 $params['bcc'] = $receiver['bcc'];
505 'id' => $orig_post['uri'],
506 'objectType' => 'note',
509 if (($orig_post['object-type'] != '') && (strstr($orig_post['object-type'], ActivityNamespace::ACTIVITY_SCHEMA))) {
510 $inReplyTo['objectType'] = str_replace(ActivityNamespace::ACTIVITY_SCHEMA, '', $orig_post['object-type']);
513 $params['object'] = [
514 'objectType' => 'comment',
515 'content' => $content,
516 'inReplyTo' => $inReplyTo];
519 $params['object']['displayName'] = $title;
523 $client = new oauth_client_class;
524 $client->oauth_version = '1.0a';
525 $client->url_parameters = false;
526 $client->authorization_header = true;
527 $client->access_token = $oauth_token;
528 $client->access_token_secret = $oauth_token_secret;
529 $client->client_id = $consumer_key;
530 $client->client_secret = $consumer_secret;
532 $username = $user . '@' . $host;
533 $url = 'https://' . $host . '/api/user/' . $user . '/feed';
535 if (pumpio_reachable($url)) {
536 $success = $client->CallAPI($url, 'POST', $params, ['FailOnAccessError' => true, 'RequestContentType' => 'application/json'], $user);
542 if ($user->generator->displayName) {
543 DI::pConfig()->set($b['uid'], 'pumpio', 'application_name', $user->generator->displayName);
546 $post_id = $user->object->id;
547 Logger::notice('pumpio_send ' . $username . ': success ' . $post_id);
548 if ($post_id && $iscomment) {
549 Logger::notice('pumpio_send ' . $username . ': Update extid ' . $post_id . ' for post id ' . $b['id']);
550 Item::update(['extid' => $post_id], ['id' => $b['id']]);
553 Logger::notice('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user, true));
559 function pumpio_action(App $a, int $uid, string $uri, string $action, string $content = '')
561 // Don't do likes and other stuff if you don't import the timeline
562 if (!DI::pConfig()->get($uid, 'pumpio', 'import')) {
566 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
567 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
568 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
569 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
570 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
571 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
573 $orig_post = Post::selectFirst([], ['uri' => $uri, 'uid' => $uid]);
575 if (!DBA::isResult($orig_post)) {
579 if ($orig_post['extid'] && !strstr($orig_post['extid'], '/proxy/')) {
580 $uri = $orig_post['extid'];
582 $uri = $orig_post['uri'];
585 if (($orig_post['object-type'] != '') && (strstr($orig_post['object-type'], ActivityNamespace::ACTIVITY_SCHEMA))) {
586 $objectType = str_replace(ActivityNamespace::ACTIVITY_SCHEMA, '', $orig_post['object-type']);
587 } elseif (strstr($uri, '/api/comment/')) {
588 $objectType = 'comment';
589 } elseif (strstr($uri, '/api/note/')) {
590 $objectType = 'note';
591 } elseif (strstr($uri, '/api/image/')) {
592 $objectType = 'image';
595 $params['verb'] = $action;
596 $params['object'] = [
598 'objectType' => $objectType,
599 'content' => $content,
602 $client = new oauth_client_class;
603 $client->oauth_version = '1.0a';
604 $client->authorization_header = true;
605 $client->url_parameters = false;
607 $client->client_id = $ckey;
608 $client->client_secret = $csecret;
609 $client->access_token = $otoken;
610 $client->access_token_secret = $osecret;
612 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
614 if (pumpio_reachable($url)) {
615 $success = $client->CallAPI($url, 'POST', $params, ['FailOnAccessError' => true, 'RequestContentType' => 'application/json'], $user);
621 Logger::notice('pumpio_action '.$username.' '.$action.': success '.$uri);
623 Logger::notice('pumpio_action '.$username.' '.$action.': general error: '.$uri);
628 function pumpio_sync(App $a)
630 if (!Addon::isEnabled('pumpio')) {
634 $last = DI::config()->get('pumpio', 'last_poll');
636 $poll_interval = intval(DI::config()->get('pumpio', 'poll_interval', PUMPIO_DEFAULT_POLL_INTERVAL));
639 $next = $last + ($poll_interval * 60);
640 if ($next > time()) {
641 Logger::notice('pumpio: poll intervall not reached');
645 Logger::notice('pumpio: cron_start');
647 $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'pumpio', 'k' => 'mirror', 'v' => '1']);
648 foreach ($pconfigs as $rr) {
649 Logger::notice('pumpio: mirroring user '.$rr['uid']);
650 pumpio_fetchtimeline($a, $rr['uid']);
653 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
654 if ($abandon_days < 1) {
658 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
660 $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'pumpio', 'k' => 'import', 'v' => '1']);
661 foreach ($pconfigs as $rr) {
662 if ($abandon_days != 0) {
663 if (DBA::exists('user', ["uid = ? AND `login_date` >= ?", $rr['uid'], $abandon_limit])) {
664 Logger::notice('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
669 Logger::notice('pumpio: importing timeline from user '.$rr['uid']);
670 pumpio_fetchinbox($a, $rr['uid']);
672 // check for new contacts once a day
673 $last_contact_check = DI::pConfig()->get($rr['uid'], 'pumpio', 'contact_check');
674 if ($last_contact_check) {
675 $next_contact_check = $last_contact_check + 86400;
677 $next_contact_check = 0;
680 if ($next_contact_check <= time()) {
681 pumpio_getallusers($a, $rr['uid']);
682 DI::pConfig()->set($rr['uid'], 'pumpio', 'contact_check', time());
686 Logger::notice('pumpio: cron_end');
688 DI::config()->set('pumpio', 'last_poll', time());
691 function pumpio_cron(App $a, $b)
693 Worker::add(Worker::PRIORITY_MEDIUM, 'addon/pumpio/pumpio_sync.php');
696 function pumpio_fetchtimeline(App $a, int $uid)
698 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
699 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
700 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
701 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
702 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
703 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
704 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
706 // get the application name for the pump.io app
707 // 1st try personal config, then system config and fallback to the
708 // hostname of the node if neither one is set.
709 $application_name = DI::pConfig()->get($uid, 'pumpio', 'application_name');
710 if ($application_name == '') {
711 $application_name = DI::config()->get('pumpio', 'application_name');
713 if ($application_name == '') {
714 $application_name = DI::baseUrl()->getHostname();
717 $first_time = ($lastdate == '');
719 $client = new oauth_client_class;
720 $client->oauth_version = '1.0a';
721 $client->authorization_header = true;
722 $client->url_parameters = false;
724 $client->client_id = $ckey;
725 $client->client_secret = $csecret;
726 $client->access_token = $otoken;
727 $client->access_token_secret = $osecret;
729 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
731 Logger::notice('pumpio: fetching for user ' . $uid . ' ' . $url . ' C:' . $client->client_id . ' CS:' . $client->client_secret . ' T:' . $client->access_token . ' TS:' . $client->access_token_secret);
733 $useraddr = $username.'@'.$hostname;
735 if (pumpio_reachable($url)) {
736 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $user);
743 Logger::notice('pumpio: error fetching posts for user ' . $uid . ' ' . $useraddr . ' ', $user);
747 $posts = array_reverse($user->items);
749 $initiallastdate = $lastdate;
753 foreach ($posts as $post) {
754 if ($post->published <= $initiallastdate) {
758 if ($lastdate < $post->published) {
759 $lastdate = $post->published;
767 if (@is_array($post->cc)) {
768 $receiptians = array_merge($receiptians, $post->cc);
771 if (@is_array($post->to)) {
772 $receiptians = array_merge($receiptians, $post->to);
776 foreach ($receiptians as $receiver) {
777 if (is_string($receiver->objectType) && ($receiver->id == 'http://activityschema.org/collection/public')) {
782 if ($public && !stristr($post->generator->displayName, $application_name)) {
783 $_SESSION['authenticated'] = true;
784 $_SESSION['uid'] = $uid;
787 $_REQUEST['api_source'] = true;
788 $_REQUEST['profile_uid'] = $uid;
789 $_REQUEST['source'] = 'pump.io';
791 if (isset($post->object->id)) {
792 $_REQUEST['message_id'] = Protocol::PUMPIO . ':' . $post->object->id;
795 if ($post->object->displayName != '') {
796 $_REQUEST['title'] = HTML::toBBCode($post->object->displayName);
798 $_REQUEST['title'] = '';
801 $_REQUEST['body'] = HTML::toBBCode($post->object->content);
803 // To-Do: Picture has to be cached and stored locally
804 if ($post->object->fullImage->url != '') {
805 if ($post->object->fullImage->pump_io->proxyURL != '') {
806 $_REQUEST['body'] = '[url=' . $post->object->fullImage->pump_io->proxyURL . '][img]' . $post->object->image->pump_io->proxyURL . "[/img][/url]\n" . $_REQUEST['body'];
808 $_REQUEST['body'] = '[url=' . $post->object->fullImage->url . '][img]' . $post->object->image->url . "[/img][/url]\n" . $_REQUEST['body'];
812 Logger::notice('pumpio: posting for user ' . $uid);
814 require_once 'mod/item.php';
817 Logger::notice('pumpio: posting done - user ' . $uid);
822 if ($lastdate != 0) {
823 DI::pConfig()->set($uid, 'pumpio', 'lastdate', $lastdate);
827 function pumpio_dounlike(App $a, int $uid, array $self, $post, string $own_id)
829 // Searching for the unliked post
830 // Two queries for speed issues
831 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
832 if (!DBA::isResult($orig_post)) {
833 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
834 if (!DBA::isResult($orig_post)) {
841 if (Strings::compareLink($post->actor->url, $own_id)) {
842 $contactid = $self['id'];
844 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
845 if (DBA::isResult($contact)) {
846 $contactid = $contact['id'];
849 if ($contactid == 0) {
850 $contactid = $orig_post['contact-id'];
854 Item::markForDeletion(['verb' => Activity::LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']]);
856 if (DBA::isResult($contact)) {
857 Logger::notice('pumpio_dounlike: unliked existing like. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
859 Logger::notice('pumpio_dounlike: not found. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' Url ' . $orig_post['uri']);
863 function pumpio_dolike(App $a, int $uid, array $self, $post, string $own_id, $threadcompletion = true)
865 if (empty($post->object->id)) {
866 Logger::info('Got empty like: '.print_r($post, true));
870 // Searching for the liked post
871 // Two queries for speed issues
872 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
873 if (!DBA::isResult($orig_post)) {
874 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
875 if (!DBA::isResult($orig_post)) {
881 if ($threadcompletion) {
882 pumpio_fetchallcomments($a, $uid, $post->object->id);
887 if (Strings::compareLink($post->actor->url, $own_id)) {
888 $contactid = $self['id'];
889 $post->actor->displayName = $self['name'];
890 $post->actor->url = $self['url'];
891 $post->actor->image->url = $self['photo'];
893 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
894 if (DBA::isResult($contact)) {
895 $contactid = $contact['id'];
898 if ($contactid == 0) {
899 $contactid = $orig_post['contact-id'];
904 'verb' => Activity::LIKE,
906 'contact-id' => $contactid,
907 'thr-parent' => $orig_post['uri'],
910 if (Post::exists($condition)) {
911 Logger::notice('pumpio_dolike: found existing like. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
916 $likedata['parent'] = $orig_post['id'];
917 $likedata['verb'] = Activity::LIKE;
918 $likedata['gravity'] = Item::GRAVITY_ACTIVITY;
919 $likedata['uid'] = $uid;
920 $likedata['wall'] = 0;
921 $likedata['network'] = Protocol::PUMPIO;
922 $likedata['uri'] = Item::newURI();
923 $likedata['thr-parent'] = $orig_post['uri'];
924 $likedata['contact-id'] = $contactid;
925 $likedata['app'] = $post->generator->displayName;
926 $likedata['author-name'] = $post->actor->displayName;
927 $likedata['author-link'] = $post->actor->url;
928 if (!empty($post->actor->image)) {
929 $likedata['author-avatar'] = $post->actor->image->url;
932 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
933 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
934 $post_type = DI::l10n()->t('status');
935 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
936 $likedata['object-type'] = Activity\ObjectType::NOTE;
938 $likedata['body'] = DI::l10n()->t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
940 $likedata['object'] = '<object><type>' . Activity\ObjectType::NOTE . '</type><local>1</local>' .
941 '<id>' . $orig_post['uri'] . '</id><link>' . XML::escape('<link rel="alternate" type="text/html" href="' . XML::escape($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
943 $ret = Item::insert($likedata);
945 Logger::notice('pumpio_dolike: ' . $ret . ' User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
948 function pumpio_get_contact($uid, $contact, $no_insert = false)
950 $cid = Contact::getIdForURL($contact->url, $uid);
956 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
957 if (!DBA::isResult($r)) {
958 // create contact record
961 'created' => DateTimeFormat::utcNow(),
962 'url' => $contact->url,
963 'nurl' => Strings::normaliseLink($contact->url),
964 'addr' => str_replace('acct:', '', $contact->id),
966 'notify' => $contact->id,
967 'poll' => 'pump.io ' . $contact->id,
968 'name' => $contact->displayName,
969 'nick' => $contact->preferredUsername,
970 'photo' => $contact->image->url,
971 'network' => Protocol::PUMPIO,
972 'rel' => Contact::FRIEND,
974 'location' => $contact->location->displayName,
975 'about' => $contact->summary,
982 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
983 if (!DBA::isResult($r)) {
987 $contact_id = $r['id'];
989 Group::addMember(User::getDefaultGroup($uid), $contact_id);
991 $contact_id = $r['id'];
994 if (!empty($contact->image->url)) {
995 Contact::updateAvatar($contact_id, $contact->image->url);
1001 function pumpio_dodelete(App $a, int $uid, array $self, $post, string $own_id)
1003 // Two queries for speed issues
1004 $condition = ['uri' => $post->object->id, 'uid' => $uid];
1005 if (Post::exists($condition)) {
1006 Item::markForDeletion($condition);
1010 $condition = ['extid' => $post->object->id, 'uid' => $uid];
1011 if (Post::exists($condition)) {
1012 Item::markForDeletion($condition);
1018 function pumpio_dopost(App $a, $client, int $uid, array $self, $post, string $own_id, bool $threadcompletion = true)
1020 if (($post->verb == 'like') || ($post->verb == 'favorite')) {
1021 return pumpio_dolike($a, $uid, $self, $post, $own_id);
1024 if (($post->verb == 'unlike') || ($post->verb == 'unfavorite')) {
1025 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1028 if ($post->verb == 'delete') {
1029 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1032 if ($post->verb != 'update') {
1033 // Two queries for speed issues
1034 if (Post::exists(['uri' => $post->object->id, 'uid' => $uid])) {
1037 if (Post::exists(['extid' => $post->object->id, 'uid' => $uid])) {
1042 // Only handle these three types
1043 if (!strstr('post|share|update', $post->verb)) {
1048 if (@is_array($post->cc)) {
1049 $receiptians = array_merge($receiptians, $post->cc);
1052 if (@is_array($post->to)) {
1053 $receiptians = array_merge($receiptians, $post->to);
1058 foreach ($receiptians as $receiver) {
1059 if (is_string($receiver->objectType) && ($receiver->id == 'http://activityschema.org/collection/public')) {
1065 $postarray['network'] = Protocol::PUMPIO;
1066 $postarray['uid'] = $uid;
1067 $postarray['wall'] = 0;
1068 $postarray['uri'] = $post->object->id;
1069 $postarray['object-type'] = ActivityNamespace::ACTIVITY_SCHEMA . strtolower($post->object->objectType);
1071 if ($post->object->objectType != 'comment') {
1072 $contact_id = pumpio_get_contact($uid, $post->actor);
1075 $contact_id = $self['id'];
1078 $postarray['thr-parent'] = $post->object->id;
1081 $postarray['private'] = 1;
1082 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1085 $contact_id = pumpio_get_contact($uid, $post->actor, true);
1087 if (Strings::compareLink($post->actor->url, $own_id)) {
1088 $contact_id = $self['id'];
1089 $post->actor->displayName = $self['name'];
1090 $post->actor->url = $self['url'];
1091 $post->actor->image->url = $self['photo'];
1092 } elseif ($contact_id == 0) {
1093 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1094 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
1095 if (DBA::isResult($contact)) {
1096 $contact_id = $contact['id'];
1098 $contact_id = $self['id'];
1102 $reply = new stdClass;
1103 $reply->verb = 'note';
1105 if (isset($post->cc)) {
1106 $reply->cc = $post->cc;
1109 if (isset($post->to)) {
1110 $reply->to = $post->to;
1113 $reply->object = new stdClass;
1114 $reply->object->objectType = $post->object->inReplyTo->objectType;
1115 $reply->object->content = $post->object->inReplyTo->content;
1116 $reply->object->id = $post->object->inReplyTo->id;
1117 $reply->actor = $post->object->inReplyTo->author;
1118 $reply->url = $post->object->inReplyTo->url;
1119 $reply->generator = new stdClass;
1120 $reply->generator->displayName = 'pumpio';
1121 $reply->published = $post->object->inReplyTo->published;
1122 $reply->received = $post->object->inReplyTo->updated;
1123 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id, false);
1125 $postarray['thr-parent'] = $post->object->inReplyTo->id;
1128 // When there is no content there is no need to continue
1129 if (empty($post->object->content)) {
1133 if (!empty($post->object->pump_io->proxyURL)) {
1134 $postarray['extid'] = $post->object->pump_io->proxyURL;
1137 $postarray['contact-id'] = $contact_id;
1138 $postarray['verb'] = Activity::POST;
1139 $postarray['owner-name'] = $post->actor->displayName;
1140 $postarray['owner-link'] = $post->actor->url;
1141 $postarray['author-name'] = $postarray['owner-name'];
1142 $postarray['author-link'] = $postarray['owner-link'];
1143 if (!empty($post->actor->image)) {
1144 $postarray['owner-avatar'] = $post->actor->image->url;
1145 $postarray['author-avatar'] = $postarray['owner-avatar'];
1147 $postarray['plink'] = $post->object->url;
1148 $postarray['app'] = $post->generator->displayName;
1149 $postarray['title'] = '';
1150 $postarray['body'] = HTML::toBBCode($post->object->content);
1151 $postarray['object'] = json_encode($post);
1153 if (!empty($post->object->fullImage->url)) {
1154 $postarray['body'] = '[url=' . $post->object->fullImage->url . '][img]' . $post->object->image->url . "[/img][/url]\n" . $postarray['body'];
1157 if (!empty($post->object->displayName)) {
1158 $postarray['title'] = $post->object->displayName;
1161 $postarray['created'] = DateTimeFormat::utc($post->published);
1162 if (isset($post->updated)) {
1163 $postarray['edited'] = DateTimeFormat::utc($post->updated);
1164 } elseif (isset($post->received)) {
1165 $postarray['edited'] = DateTimeFormat::utc($post->received);
1167 $postarray['edited'] = $postarray['created'];
1170 if ($post->verb == 'share') {
1171 if (isset($post->object->author->displayName) && ($post->object->author->displayName != '')) {
1172 $share_author = $post->object->author->displayName;
1173 } elseif (isset($post->object->author->preferredUsername) && ($post->object->author->preferredUsername != '')) {
1174 $share_author = $post->object->author->preferredUsername;
1176 $share_author = $post->object->author->url;
1179 if (isset($post->object->created)) {
1180 $created = DateTimeFormat::utc($post->object->created);
1185 $postarray['body'] = Friendica\Content\Text\BBCode::getShareOpeningTag($share_author, $post->object->author->url,
1186 $post->object->author->image->url, $post->links->self->href, $created) .
1187 $postarray['body'] . '[/share]';
1190 if (trim($postarray['body']) == '') {
1194 $top_item = Item::insert($postarray);
1195 $postarray['id'] = $top_item;
1197 if (($top_item == 0) && ($post->verb == 'update')) {
1199 'title' => $postarray['title'],
1200 'body' => $postarray['body'],
1201 'changed' => $postarray['edited'],
1203 $condition = ['uri' => $postarray['uri'], 'uid' => $uid];
1204 Item::update($fields, $condition);
1207 if (($post->object->objectType == 'comment') && $threadcompletion) {
1208 pumpio_fetchallcomments($a, $uid, $postarray['thr-parent']);
1214 function pumpio_fetchinbox(App $a, int $uid)
1216 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1217 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1218 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1219 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1220 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
1221 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1222 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1224 $own_id = 'https://' . $hostname . '/' . $username;
1226 $self = User::getOwnerDataById($uid);
1228 $lastitems = DBA::p("SELECT `uri` FROM `post-thread-user`
1229 INNER JOIN `post-view` ON `post-view`.`uri-id` = `post-thread-user`.`uri-id`
1230 WHERE `post-thread-user`.`network` = ? AND `post-thread-user`.`uid` = ? AND `post-view`.`extid` != ''
1231 ORDER BY `post-thread-user`.`commented` DESC LIMIT 10", Protocol::PUMPIO, $uid);
1233 $client = new oauth_client_class();
1234 $client->oauth_version = '1.0a';
1235 $client->authorization_header = true;
1236 $client->url_parameters = false;
1238 $client->client_id = $ckey;
1239 $client->client_secret = $csecret;
1240 $client->access_token = $otoken;
1241 $client->access_token_secret = $osecret;
1243 $last_id = DI::pConfig()->get($uid, 'pumpio', 'last_id');
1245 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1247 if ($last_id != '') {
1248 $url .= '?since=' . urlencode($last_id);
1251 if (pumpio_reachable($url)) {
1252 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $user);
1261 if (!empty($user->items)) {
1262 $posts = array_reverse($user->items);
1264 if (count($posts)) {
1265 foreach ($posts as $post) {
1266 $last_id = $post->id;
1267 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, true);
1272 while ($item = DBA::fetch($lastitems)) {
1273 pumpio_fetchallcomments($a, $uid, $item['uri']);
1275 DBA::close($lastitems);
1277 DI::pConfig()->set($uid, 'pumpio', 'last_id', $last_id);
1280 function pumpio_getallusers(App &$a, int $uid)
1282 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1283 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1284 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1285 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1286 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1287 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1289 $client = new oauth_client_class;
1290 $client->oauth_version = '1.0a';
1291 $client->authorization_header = true;
1292 $client->url_parameters = false;
1294 $client->client_id = $ckey;
1295 $client->client_secret = $csecret;
1296 $client->access_token = $otoken;
1297 $client->access_token_secret = $osecret;
1299 $url = 'https://' . $hostname . '/api/user/' . $username . '/following';
1301 if (pumpio_reachable($url)) {
1302 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1307 if (empty($users)) {
1311 if ($users->totalItems > count($users->items)) {
1312 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1314 if (pumpio_reachable($url)) {
1315 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1321 if (!empty($users->items)) {
1322 foreach ($users->items as $user) {
1323 pumpio_get_contact($uid, $user);
1328 function pumpio_getreceiver(App $a, array $b)
1332 if (!$b['private']) {
1333 if (!strstr($b['postopts'], 'pumpio')) {
1337 $public = DI::pConfig()->get($b['uid'], 'pumpio', 'public');
1340 $receiver['to'][] = [
1341 'objectType' => 'collection',
1342 'id' => 'http://activityschema.org/collection/public'
1346 $cids = explode('><', $b['allow_cid']);
1347 $gids = explode('><', $b['allow_gid']);
1349 foreach ($cids as $cid) {
1350 $cid = trim($cid, ' <>');
1352 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b['uid'], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1353 if (DBA::isResult($contact)) {
1354 $receiver['bcc'][] = [
1355 'displayName' => $contact['name'],
1356 'objectType' => 'person',
1357 'preferredUsername' => $contact['nick'],
1358 'url' => $contact['url'],
1362 foreach ($gids as $gid) {
1363 $gid = trim($gid, ' <>');
1365 $contacts = DBA::p("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network`
1366 FROM `group_member`, `contact` WHERE `group_member`.`gid` = ?
1367 AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = ?",
1368 $gid, Protocol::PUMPIO);
1370 while ($row = DBA::fetch($contacts)) {
1371 $receiver['bcc'][] = [
1372 'displayName' => $row['name'],
1373 'objectType' => 'person',
1374 'preferredUsername' => $row['nick'],
1375 'url' => $row['url'],
1378 DBA::close($contacts);
1382 if ($b['inform'] != '') {
1383 $inform = explode(',', $b['inform']);
1385 foreach ($inform as $cid) {
1386 if (substr($cid, 0, 4) != 'cid:') {
1390 $cid = str_replace('cid:', '', $cid);
1392 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b['uid'], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1393 if (DBA::isResult($contact)) {
1394 $receiver['to'][] = [
1395 'displayName' => $contact['name'],
1396 'objectType' => 'person',
1397 'preferredUsername' => $contact['nick'],
1398 'url' => $contact['url'],
1407 function pumpio_fetchallcomments(App $a, $uid, $id)
1409 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1410 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1411 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1412 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1413 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1414 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1416 Logger::notice('pumpio_fetchallcomments: completing comment for user ' . $uid . ' post id ' . $id);
1418 $own_id = 'https://' . $hostname . '/' . $username;
1420 $self = User::getOwnerDataById($uid);
1422 // Fetching the original post
1423 $condition = ["`uri` = ? AND `uid` = ? AND `extid` != ''", $id, $uid];
1424 $original = Post::selectFirst(['extid'], $condition);
1425 if (!DBA::isResult($original)) {
1429 $url = $original['extid'];
1431 $client = new oauth_client_class;
1432 $client->oauth_version = '1.0a';
1433 $client->authorization_header = true;
1434 $client->url_parameters = false;
1436 $client->client_id = $ckey;
1437 $client->client_secret = $csecret;
1438 $client->access_token = $otoken;
1439 $client->access_token_secret = $osecret;
1441 Logger::notice('pumpio_fetchallcomments: fetching comment for user ' . $uid . ', URL ' . $url);
1443 if (pumpio_reachable($url)) {
1444 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $item);
1453 if ($item->likes->totalItems != 0) {
1454 foreach ($item->likes->items as $post) {
1455 $like = new stdClass;
1456 $like->object = new stdClass;
1457 $like->object->id = $item->id;
1458 $like->actor = new stdClass;
1459 if (!empty($item->displayName)) {
1460 $like->actor->displayName = $item->displayName;
1462 //$like->actor->preferredUsername = $item->preferredUsername;
1463 //$like->actor->image = $item->image;
1464 $like->actor->url = $item->url;
1465 $like->generator = new stdClass;
1466 $like->generator->displayName = 'pumpio';
1467 pumpio_dolike($a, $uid, $self, $post, $own_id, false);
1471 if ($item->replies->totalItems == 0) {
1475 foreach ($item->replies->items as $item) {
1476 if ($item->id == $id) {
1480 // Checking if the comment already exists - Two queries for speed issues
1481 if (Post::exists(['uri' => $item->id, 'uid' => $uid])) {
1485 if (Post::exists(['extid' => $item->id, 'uid' => $uid])) {
1489 $post = new stdClass;
1490 $post->verb = 'post';
1491 $post->actor = $item->author;
1492 $post->published = $item->published;
1493 $post->received = $item->updated;
1494 $post->generator = new stdClass;
1495 $post->generator->displayName = 'pumpio';
1496 // To-Do: Check for public post
1498 unset($item->author);
1499 unset($item->published);
1500 unset($item->updated);
1502 $post->object = $item;
1504 Logger::notice('pumpio_fetchallcomments: posting comment ' . $post->object->id . ' ', json_decode(json_encode($post), true));
1505 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1509 function pumpio_reachable(string $url): bool
1511 return DI::httpClient()->get($url, HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => 10])->isSuccess();