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\ConfigFileManager;
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()
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();
73 $o = print_r(DI::args()->getArgv(), true);
77 $o = pumpio_connect();
82 function pumpio_check_item_notification(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($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()->getHost();
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() . '/images/friendica-256.png';
109 $params['redirect_uris'] = DI::baseUrl() . '/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 ', (array)$values);
129 Logger::info('pumpio_registerclient: failed: ', $curl_info);
134 function pumpio_connect()
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($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() . '/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() . '/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(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(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() . '/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(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(ConfigFileManager $loader)
323 DI::app()->getConfigCache()->load($loader->loadAddonConfig('pumpio'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
326 function pumpio_hook_fork(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(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(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($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($b['uid'], $b['thr-parent'], 'unlike');
431 pumpio_action($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($b['uid'], $b['uri'], 'update', $b['body']);
444 if (($b['verb'] == Activity::POST) && $b['deleted']) {
445 pumpio_action($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(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()
630 if (!Addon::isEnabled('pumpio')) {
634 $last = DI::keyValue()->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($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($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($rr['uid']);
682 DI::pConfig()->set($rr['uid'], 'pumpio', 'contact_check', time());
686 Logger::notice('pumpio: cron_end');
688 DI::keyValue()->set('pumpio_last_poll', time());
691 function pumpio_cron($b)
693 Worker::add(Worker::PRIORITY_MEDIUM, 'addon/pumpio/pumpio_sync.php');
696 function pumpio_fetchtimeline(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()->getHost();
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 $postarray['uid'] = $uid;
784 $postarray['app'] = 'pump.io';
786 if ($post->object->displayName != '') {
787 $postarray['title'] = HTML::toBBCode($post->object->displayName);
789 $postarray['title'] = '';
792 $postarray['body'] = HTML::toBBCode($post->object->content);
794 // To-Do: Picture has to be cached and stored locally
795 if ($post->object->fullImage->url != '') {
796 if ($post->object->fullImage->pump_io->proxyURL != '') {
797 $postarray['body'] = '[url=' . $post->object->fullImage->pump_io->proxyURL . '][img]' . $post->object->image->pump_io->proxyURL . "[/img][/url]\n" . $postarray['body'];
799 $postarray['body'] = '[url=' . $post->object->fullImage->url . '][img]' . $post->object->image->url . "[/img][/url]\n" . $postarray['body'];
803 Logger::notice('pumpio: posting for user ' . $uid);
805 Item::insert($postarray, true);
807 Logger::notice('pumpio: posting done - user ' . $uid);
812 if ($lastdate != 0) {
813 DI::pConfig()->set($uid, 'pumpio', 'lastdate', $lastdate);
817 function pumpio_dounlike(int $uid, array $self, $post, string $own_id)
819 // Searching for the unliked post
820 // Two queries for speed issues
821 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
822 if (!DBA::isResult($orig_post)) {
823 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
824 if (!DBA::isResult($orig_post)) {
831 if (Strings::compareLink($post->actor->url, $own_id)) {
832 $contactid = $self['id'];
834 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
835 if (DBA::isResult($contact)) {
836 $contactid = $contact['id'];
839 if ($contactid == 0) {
840 $contactid = $orig_post['contact-id'];
844 Item::markForDeletion(['verb' => Activity::LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']]);
846 if (DBA::isResult($contact)) {
847 Logger::notice('pumpio_dounlike: unliked existing like. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
849 Logger::notice('pumpio_dounlike: not found. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' Url ' . $orig_post['uri']);
853 function pumpio_dolike(int $uid, array $self, $post, string $own_id, $threadcompletion = true)
855 if (empty($post->object->id)) {
856 Logger::info('Got empty like: '.print_r($post, true));
860 // Searching for the liked post
861 // Two queries for speed issues
862 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
863 if (!DBA::isResult($orig_post)) {
864 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
865 if (!DBA::isResult($orig_post)) {
871 if ($threadcompletion) {
872 pumpio_fetchallcomments($uid, $post->object->id);
877 if (Strings::compareLink($post->actor->url, $own_id)) {
878 $contactid = $self['id'];
879 $post->actor->displayName = $self['name'];
880 $post->actor->url = $self['url'];
881 $post->actor->image->url = $self['photo'];
883 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
884 if (DBA::isResult($contact)) {
885 $contactid = $contact['id'];
888 if ($contactid == 0) {
889 $contactid = $orig_post['contact-id'];
894 'verb' => Activity::LIKE,
896 'contact-id' => $contactid,
897 'thr-parent' => $orig_post['uri'],
900 if (Post::exists($condition)) {
901 Logger::notice('pumpio_dolike: found existing like. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
906 $likedata['parent'] = $orig_post['id'];
907 $likedata['verb'] = Activity::LIKE;
908 $likedata['gravity'] = Item::GRAVITY_ACTIVITY;
909 $likedata['uid'] = $uid;
910 $likedata['wall'] = 0;
911 $likedata['network'] = Protocol::PUMPIO;
912 $likedata['uri'] = Item::newURI();
913 $likedata['thr-parent'] = $orig_post['uri'];
914 $likedata['contact-id'] = $contactid;
915 $likedata['app'] = $post->generator->displayName;
916 $likedata['author-name'] = $post->actor->displayName;
917 $likedata['author-link'] = $post->actor->url;
918 if (!empty($post->actor->image)) {
919 $likedata['author-avatar'] = $post->actor->image->url;
922 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
923 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
924 $post_type = DI::l10n()->t('status');
925 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
926 $likedata['object-type'] = Activity\ObjectType::NOTE;
928 $likedata['body'] = DI::l10n()->t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
930 $likedata['object'] = '<object><type>' . Activity\ObjectType::NOTE . '</type><local>1</local>' .
931 '<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>';
933 $ret = Item::insert($likedata);
935 Logger::notice('pumpio_dolike: ' . $ret . ' User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
938 function pumpio_get_contact($uid, $contact, $no_insert = false)
940 $cid = Contact::getIdForURL($contact->url, $uid);
946 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
947 if (!DBA::isResult($r)) {
948 // create contact record
951 'created' => DateTimeFormat::utcNow(),
952 'url' => $contact->url,
953 'nurl' => Strings::normaliseLink($contact->url),
954 'addr' => str_replace('acct:', '', $contact->id),
956 'notify' => $contact->id,
957 'poll' => 'pump.io ' . $contact->id,
958 'name' => $contact->displayName,
959 'nick' => $contact->preferredUsername,
960 'photo' => $contact->image->url,
961 'network' => Protocol::PUMPIO,
962 'rel' => Contact::FRIEND,
964 'location' => $contact->location->displayName,
965 'about' => $contact->summary,
972 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
973 if (!DBA::isResult($r)) {
977 $contact_id = $r['id'];
979 Group::addMember(User::getDefaultGroup($uid), $contact_id);
981 $contact_id = $r['id'];
984 if (!empty($contact->image->url)) {
985 Contact::updateAvatar($contact_id, $contact->image->url);
991 function pumpio_dodelete(int $uid, array $self, $post, string $own_id)
993 // Two queries for speed issues
994 $condition = ['uri' => $post->object->id, 'uid' => $uid];
995 if (Post::exists($condition)) {
996 Item::markForDeletion($condition);
1000 $condition = ['extid' => $post->object->id, 'uid' => $uid];
1001 if (Post::exists($condition)) {
1002 Item::markForDeletion($condition);
1008 function pumpio_dopost($client, int $uid, array $self, $post, string $own_id, bool $threadcompletion = true)
1010 if (($post->verb == 'like') || ($post->verb == 'favorite')) {
1011 return pumpio_dolike($uid, $self, $post, $own_id);
1014 if (($post->verb == 'unlike') || ($post->verb == 'unfavorite')) {
1015 return pumpio_dounlike($uid, $self, $post, $own_id);
1018 if ($post->verb == 'delete') {
1019 return pumpio_dodelete($uid, $self, $post, $own_id);
1022 if ($post->verb != 'update') {
1023 // Two queries for speed issues
1024 if (Post::exists(['uri' => $post->object->id, 'uid' => $uid])) {
1027 if (Post::exists(['extid' => $post->object->id, 'uid' => $uid])) {
1032 // Only handle these three types
1033 if (!strstr('post|share|update', $post->verb)) {
1038 if (@is_array($post->cc)) {
1039 $receiptians = array_merge($receiptians, $post->cc);
1042 if (@is_array($post->to)) {
1043 $receiptians = array_merge($receiptians, $post->to);
1048 foreach ($receiptians as $receiver) {
1049 if (is_string($receiver->objectType) && ($receiver->id == 'http://activityschema.org/collection/public')) {
1055 $postarray['network'] = Protocol::PUMPIO;
1056 $postarray['uid'] = $uid;
1057 $postarray['wall'] = 0;
1058 $postarray['uri'] = $post->object->id;
1059 $postarray['object-type'] = ActivityNamespace::ACTIVITY_SCHEMA . strtolower($post->object->objectType);
1061 if ($post->object->objectType != 'comment') {
1062 $contact_id = pumpio_get_contact($uid, $post->actor);
1065 $contact_id = $self['id'];
1068 $postarray['thr-parent'] = $post->object->id;
1071 $postarray['private'] = 1;
1072 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1075 $contact_id = pumpio_get_contact($uid, $post->actor, true);
1077 if (Strings::compareLink($post->actor->url, $own_id)) {
1078 $contact_id = $self['id'];
1079 $post->actor->displayName = $self['name'];
1080 $post->actor->url = $self['url'];
1081 $post->actor->image->url = $self['photo'];
1082 } elseif ($contact_id == 0) {
1083 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1084 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
1085 if (DBA::isResult($contact)) {
1086 $contact_id = $contact['id'];
1088 $contact_id = $self['id'];
1092 $reply = new stdClass;
1093 $reply->verb = 'note';
1095 if (isset($post->cc)) {
1096 $reply->cc = $post->cc;
1099 if (isset($post->to)) {
1100 $reply->to = $post->to;
1103 $reply->object = new stdClass;
1104 $reply->object->objectType = $post->object->inReplyTo->objectType;
1105 $reply->object->content = $post->object->inReplyTo->content;
1106 $reply->object->id = $post->object->inReplyTo->id;
1107 $reply->actor = $post->object->inReplyTo->author;
1108 $reply->url = $post->object->inReplyTo->url;
1109 $reply->generator = new stdClass;
1110 $reply->generator->displayName = 'pumpio';
1111 $reply->published = $post->object->inReplyTo->published;
1112 $reply->received = $post->object->inReplyTo->updated;
1113 pumpio_dopost($client, $uid, $self, $reply, $own_id, false);
1115 $postarray['thr-parent'] = $post->object->inReplyTo->id;
1118 // When there is no content there is no need to continue
1119 if (empty($post->object->content)) {
1123 if (!empty($post->object->pump_io->proxyURL)) {
1124 $postarray['extid'] = $post->object->pump_io->proxyURL;
1127 $postarray['contact-id'] = $contact_id;
1128 $postarray['verb'] = Activity::POST;
1129 $postarray['owner-name'] = $post->actor->displayName;
1130 $postarray['owner-link'] = $post->actor->url;
1131 $postarray['author-name'] = $postarray['owner-name'];
1132 $postarray['author-link'] = $postarray['owner-link'];
1133 if (!empty($post->actor->image)) {
1134 $postarray['owner-avatar'] = $post->actor->image->url;
1135 $postarray['author-avatar'] = $postarray['owner-avatar'];
1137 $postarray['plink'] = $post->object->url;
1138 $postarray['app'] = $post->generator->displayName;
1139 $postarray['title'] = '';
1140 $postarray['body'] = HTML::toBBCode($post->object->content);
1141 $postarray['object'] = json_encode($post);
1143 if (!empty($post->object->fullImage->url)) {
1144 $postarray['body'] = '[url=' . $post->object->fullImage->url . '][img]' . $post->object->image->url . "[/img][/url]\n" . $postarray['body'];
1147 if (!empty($post->object->displayName)) {
1148 $postarray['title'] = $post->object->displayName;
1151 $postarray['created'] = DateTimeFormat::utc($post->published);
1152 if (isset($post->updated)) {
1153 $postarray['edited'] = DateTimeFormat::utc($post->updated);
1154 } elseif (isset($post->received)) {
1155 $postarray['edited'] = DateTimeFormat::utc($post->received);
1157 $postarray['edited'] = $postarray['created'];
1160 if ($post->verb == 'share') {
1161 if (isset($post->object->author->displayName) && ($post->object->author->displayName != '')) {
1162 $share_author = $post->object->author->displayName;
1163 } elseif (isset($post->object->author->preferredUsername) && ($post->object->author->preferredUsername != '')) {
1164 $share_author = $post->object->author->preferredUsername;
1166 $share_author = $post->object->author->url;
1169 if (isset($post->object->created)) {
1170 $created = DateTimeFormat::utc($post->object->created);
1175 $postarray['body'] = Friendica\Content\Text\BBCode::getShareOpeningTag($share_author, $post->object->author->url,
1176 $post->object->author->image->url, $post->links->self->href, $created) .
1177 $postarray['body'] . '[/share]';
1180 if (trim($postarray['body']) == '') {
1184 $top_item = Item::insert($postarray);
1185 $postarray['id'] = $top_item;
1187 if (($top_item == 0) && ($post->verb == 'update')) {
1189 'title' => $postarray['title'],
1190 'body' => $postarray['body'],
1191 'changed' => $postarray['edited'],
1193 $condition = ['uri' => $postarray['uri'], 'uid' => $uid];
1194 Item::update($fields, $condition);
1197 if (($post->object->objectType == 'comment') && $threadcompletion) {
1198 pumpio_fetchallcomments($uid, $postarray['thr-parent']);
1204 function pumpio_fetchinbox(int $uid)
1206 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1207 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1208 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1209 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1210 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
1211 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1212 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1214 $own_id = 'https://' . $hostname . '/' . $username;
1216 $self = User::getOwnerDataById($uid);
1218 $lastitems = DBA::p("SELECT `uri` FROM `post-thread-user`
1219 INNER JOIN `post-view` ON `post-view`.`uri-id` = `post-thread-user`.`uri-id`
1220 WHERE `post-thread-user`.`network` = ? AND `post-thread-user`.`uid` = ? AND `post-view`.`extid` != ''
1221 ORDER BY `post-thread-user`.`commented` DESC LIMIT 10", Protocol::PUMPIO, $uid);
1223 $client = new oauth_client_class();
1224 $client->oauth_version = '1.0a';
1225 $client->authorization_header = true;
1226 $client->url_parameters = false;
1228 $client->client_id = $ckey;
1229 $client->client_secret = $csecret;
1230 $client->access_token = $otoken;
1231 $client->access_token_secret = $osecret;
1233 $last_id = DI::pConfig()->get($uid, 'pumpio', 'last_id');
1235 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1237 if ($last_id != '') {
1238 $url .= '?since=' . urlencode($last_id);
1241 if (pumpio_reachable($url)) {
1242 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $user);
1251 if (!empty($user->items)) {
1252 $posts = array_reverse($user->items);
1254 if (count($posts)) {
1255 foreach ($posts as $post) {
1256 $last_id = $post->id;
1257 pumpio_dopost($client, $uid, $self, $post, $own_id, true);
1262 while ($item = DBA::fetch($lastitems)) {
1263 pumpio_fetchallcomments($uid, $item['uri']);
1265 DBA::close($lastitems);
1267 DI::pConfig()->set($uid, 'pumpio', 'last_id', $last_id);
1270 function pumpio_getallusers(int $uid)
1272 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1273 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1274 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1275 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1276 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1277 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1279 $client = new oauth_client_class;
1280 $client->oauth_version = '1.0a';
1281 $client->authorization_header = true;
1282 $client->url_parameters = false;
1284 $client->client_id = $ckey;
1285 $client->client_secret = $csecret;
1286 $client->access_token = $otoken;
1287 $client->access_token_secret = $osecret;
1289 $url = 'https://' . $hostname . '/api/user/' . $username . '/following';
1291 if (pumpio_reachable($url)) {
1292 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1297 if (empty($users)) {
1301 if ($users->totalItems > count($users->items)) {
1302 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1304 if (pumpio_reachable($url)) {
1305 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1311 if (!empty($users->items)) {
1312 foreach ($users->items as $user) {
1313 pumpio_get_contact($uid, $user);
1318 function pumpio_getreceiver(array $b)
1322 if (!$b['private']) {
1323 if (!strstr($b['postopts'], 'pumpio')) {
1327 $public = DI::pConfig()->get($b['uid'], 'pumpio', 'public');
1330 $receiver['to'][] = [
1331 'objectType' => 'collection',
1332 'id' => 'http://activityschema.org/collection/public'
1336 $cids = explode('><', $b['allow_cid']);
1337 $gids = explode('><', $b['allow_gid']);
1339 foreach ($cids as $cid) {
1340 $cid = trim($cid, ' <>');
1342 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b['uid'], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1343 if (DBA::isResult($contact)) {
1344 $receiver['bcc'][] = [
1345 'displayName' => $contact['name'],
1346 'objectType' => 'person',
1347 'preferredUsername' => $contact['nick'],
1348 'url' => $contact['url'],
1352 foreach ($gids as $gid) {
1353 $gid = trim($gid, ' <>');
1355 $contacts = DBA::p("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network`
1356 FROM `group_member`, `contact` WHERE `group_member`.`gid` = ?
1357 AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = ?",
1358 $gid, Protocol::PUMPIO);
1360 while ($row = DBA::fetch($contacts)) {
1361 $receiver['bcc'][] = [
1362 'displayName' => $row['name'],
1363 'objectType' => 'person',
1364 'preferredUsername' => $row['nick'],
1365 'url' => $row['url'],
1368 DBA::close($contacts);
1372 if ($b['inform'] != '') {
1373 $inform = explode(',', $b['inform']);
1375 foreach ($inform as $cid) {
1376 if (substr($cid, 0, 4) != 'cid:') {
1380 $cid = str_replace('cid:', '', $cid);
1382 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b['uid'], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1383 if (DBA::isResult($contact)) {
1384 $receiver['to'][] = [
1385 'displayName' => $contact['name'],
1386 'objectType' => 'person',
1387 'preferredUsername' => $contact['nick'],
1388 'url' => $contact['url'],
1397 function pumpio_fetchallcomments($uid, $id)
1399 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1400 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1401 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1402 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1403 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1404 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1406 Logger::notice('pumpio_fetchallcomments: completing comment for user ' . $uid . ' post id ' . $id);
1408 $own_id = 'https://' . $hostname . '/' . $username;
1410 $self = User::getOwnerDataById($uid);
1412 // Fetching the original post
1413 $condition = ["`uri` = ? AND `uid` = ? AND `extid` != ''", $id, $uid];
1414 $original = Post::selectFirst(['extid'], $condition);
1415 if (!DBA::isResult($original)) {
1419 $url = $original['extid'];
1421 $client = new oauth_client_class;
1422 $client->oauth_version = '1.0a';
1423 $client->authorization_header = true;
1424 $client->url_parameters = false;
1426 $client->client_id = $ckey;
1427 $client->client_secret = $csecret;
1428 $client->access_token = $otoken;
1429 $client->access_token_secret = $osecret;
1431 Logger::notice('pumpio_fetchallcomments: fetching comment for user ' . $uid . ', URL ' . $url);
1433 if (pumpio_reachable($url)) {
1434 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $item);
1443 if ($item->likes->totalItems != 0) {
1444 foreach ($item->likes->items as $post) {
1445 $like = new stdClass;
1446 $like->object = new stdClass;
1447 $like->object->id = $item->id;
1448 $like->actor = new stdClass;
1449 if (!empty($item->displayName)) {
1450 $like->actor->displayName = $item->displayName;
1452 //$like->actor->preferredUsername = $item->preferredUsername;
1453 //$like->actor->image = $item->image;
1454 $like->actor->url = $item->url;
1455 $like->generator = new stdClass;
1456 $like->generator->displayName = 'pumpio';
1457 pumpio_dolike($uid, $self, $post, $own_id, false);
1461 if ($item->replies->totalItems == 0) {
1465 foreach ($item->replies->items as $item) {
1466 if ($item->id == $id) {
1470 // Checking if the comment already exists - Two queries for speed issues
1471 if (Post::exists(['uri' => $item->id, 'uid' => $uid])) {
1475 if (Post::exists(['extid' => $item->id, 'uid' => $uid])) {
1479 $post = new stdClass;
1480 $post->verb = 'post';
1481 $post->actor = $item->author;
1482 $post->published = $item->published;
1483 $post->received = $item->updated;
1484 $post->generator = new stdClass;
1485 $post->generator->displayName = 'pumpio';
1486 // To-Do: Check for public post
1488 unset($item->author);
1489 unset($item->published);
1490 unset($item->updated);
1492 $post->object = $item;
1494 Logger::notice('pumpio_fetchallcomments: posting comment ' . $post->object->id . ' ', json_decode(json_encode($post), true));
1495 pumpio_dopost($client, $uid, $self, $post, $own_id, false);
1499 function pumpio_reachable(string $url): bool
1501 return DI::httpClient()->get($url, HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => 10])->isSuccess();