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\Core\Session;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Strings;
33 use Friendica\Util\XML;
35 require 'addon/pumpio/oauth/http.php';
36 require 'addon/pumpio/oauth/oauth_client.php';
37 require_once 'mod/share.php';
39 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
41 function pumpio_install()
43 Hook::register('load_config', 'addon/pumpio/pumpio.php', 'pumpio_load_config');
44 Hook::register('hook_fork', 'addon/pumpio/pumpio.php', 'hook_fork');
45 Hook::register('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
46 Hook::register('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
47 Hook::register('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
48 Hook::register('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
49 Hook::register('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
50 Hook::register('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
51 Hook::register('check_item_notification', 'addon/pumpio/pumpio.php', 'pumpio_check_item_notification');
55 * This is a statement rather than an actual function definition. The simple
56 * existence of this method is checked to figure out if the addon offers a
59 function pumpio_module() {}
61 function pumpio_content(App $a)
63 if (!Session::getLocalUser()) {
64 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
68 require_once 'mod/settings.php';
71 if (isset(DI::args()->getArgv()[1])) {
72 switch (DI::args()->getArgv()[1]) {
74 $o = pumpio_connect($a);
78 $o = print_r(DI::args()->getArgv(), true);
82 $o = pumpio_connect($a);
87 function pumpio_check_item_notification(App $a, array &$notification_data)
89 $hostname = DI::pConfig()->get($notification_data['uid'], 'pumpio', 'host');
90 $username = DI::pConfig()->get($notification_data['uid'], 'pumpio', 'user');
92 $notification_data['profiles'][] = 'https://' . $hostname . '/' . $username;
95 function pumpio_registerclient(App $a, $host)
97 $url = 'https://' . $host . '/api/client/register';
101 $application_name = DI::config()->get('pumpio', 'application_name');
103 if ($application_name == '') {
104 $application_name = DI::baseUrl()->getHostname();
107 $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
109 $params['type'] = 'client_associate';
110 $params['contacts'] = $adminlist[0];
111 $params['application_type'] = 'native';
112 $params['application_name'] = $application_name;
113 $params['logo_url'] = DI::baseUrl()->get() . '/images/friendica-256.png';
114 $params['redirect_uris'] = DI::baseUrl()->get() . '/pumpio/connect';
116 Logger::info('pumpio_registerclient: ' . $url . ' parameters', $params);
118 // @TODO Rewrite this to our own HTTP client
119 $ch = curl_init($url);
120 curl_setopt($ch, CURLOPT_HEADER, false);
121 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
122 curl_setopt($ch, CURLOPT_POST,1);
123 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
124 curl_setopt($ch, CURLOPT_USERAGENT, 'Friendica');
127 $curl_info = curl_getinfo($ch);
129 if ($curl_info['http_code'] == '200') {
130 $values = json_decode($s);
131 Logger::info('pumpio_registerclient: success ', $values);
134 Logger::info('pumpio_registerclient: failed: ', $curl_info);
139 function pumpio_connect(App $a)
141 // Define the needed keys
142 $consumer_key = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'consumer_key');
143 $consumer_secret = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'consumer_secret');
144 $hostname = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'host');
146 if ((($consumer_key == '') || ($consumer_secret == '')) && ($hostname != '')) {
147 Logger::notice('pumpio_connect: register client');
148 $clientdata = pumpio_registerclient($a, $hostname);
149 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'consumer_key', $clientdata->client_id);
150 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'consumer_secret', $clientdata->client_secret);
152 $consumer_key = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'consumer_key');
153 $consumer_secret = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'consumer_secret');
155 Logger::info('pumpio_connect: ckey: ' . $consumer_key . ' csecrect: ' . $consumer_secret);
158 if (($consumer_key == '') || ($consumer_secret == '')) {
159 Logger::notice('pumpio_connect: '.sprintf('Unable to register the client at the pump.io server "%s".', $hostname));
161 return DI::l10n()->t("Unable to register the client at the pump.io server '%s'.", $hostname);
164 // The callback URL is the script that gets called after the user authenticates with pumpio
165 $callback_url = DI::baseUrl()->get() . '/pumpio/connect';
167 // Let's begin. First we need a Request Token. The request token is required to send the user
168 // to pumpio's login page.
170 // Create a new instance of the oauth_client_class library. For this step, all we need to give the library is our
171 // Consumer Key and Consumer Secret
172 $client = new oauth_client_class;
174 $client->server = '';
175 $client->oauth_version = '1.0a';
176 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
177 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
178 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
179 $client->url_parameters = false;
180 $client->authorization_header = true;
181 $client->redirect_uri = $callback_url;
182 $client->client_id = $consumer_key;
183 $client->client_secret = $consumer_secret;
185 if (($success = $client->Initialize())) {
186 if (($success = $client->Process())) {
187 if (strlen($client->access_token)) {
188 Logger::info('pumpio_connect: otoken: ' . $client->access_token . ', osecrect: ' . $client->access_token_secret);
189 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'oauth_token', $client->access_token);
190 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'oauth_token_secret', $client->access_token_secret);
193 $success = $client->Finalize($success);
196 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
200 Logger::notice('pumpio_connect: authenticated');
201 $o = DI::l10n()->t('You are now authenticated to pumpio.');
202 $o .= '<br /><a href="' . DI::baseUrl()->get() . '/settings/connectors">' . DI::l10n()->t('return to the connector page') . '</a>';
204 Logger::notice('pumpio_connect: could not connect');
205 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
211 function pumpio_jot_nets(App $a, array &$jotnets_fields)
213 if (!Session::getLocalUser()) {
217 if (DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'post')) {
218 $jotnets_fields[] = [
219 'type' => 'checkbox',
222 DI::l10n()->t('Post to pumpio'),
223 DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'post_by_default')
229 function pumpio_settings(App $a, array &$data)
231 if (!Session::getLocalUser()) {
235 $pumpio_host = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'host');
236 $pumpio_user = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'user');
237 $oauth_token = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'oauth_token');
238 $oauth_token_secret = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'oauth_token_secret');
240 $import_enabled = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'import', false);
241 $enabled = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'post', false);
242 $def_enabled = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'post_by_default', false);
243 $public_enabled = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'public', false);
244 $mirror_enabled = DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'mirror', false);
246 $submit = ['pumpio-submit' => DI::l10n()->t('Save Settings')];
247 if ($oauth_token && $oauth_token_secret) {
248 $submit['pumpio-delete'] = DI::l10n()->t('Delete this preset');
251 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/pumpio/');
252 $html = Renderer::replaceMacros($t, [
254 'authenticate' => DI::l10n()->t('Authenticate your pump.io connection'),
256 '$pumpio_host' => $pumpio_host,
257 '$pumpio_user' => $pumpio_user,
258 '$oauth_token' => $oauth_token,
259 '$oauth_token_secret' => $oauth_token_secret,
260 '$authenticate_url' => DI::baseUrl()->get() . '/pumpio/connect',
261 '$servername' => ['pumpio_host', DI::l10n()->t('Pump.io servername (without "http://" or "https://" )'), $pumpio_host],
262 '$username' => ['pumpio_user', DI::l10n()->t('Pump.io username (without the servername)'), $pumpio_user],
263 '$import' => ['pumpio_import', DI::l10n()->t('Import the remote timeline'), $import_enabled],
264 '$enabled' => ['pumpio', DI::l10n()->t('Enable Pump.io Post Addon'), $enabled],
265 '$bydefault' => ['pumpio_bydefault', DI::l10n()->t('Post to Pump.io by default'), $def_enabled],
266 '$public' => ['pumpio_public', DI::l10n()->t('Should posts be public?'), $public_enabled],
267 '$mirror' => ['pumpio_mirror', DI::l10n()->t('Mirror all public posts'), $mirror_enabled],
271 'connector' => 'pumpio',
272 'title' => DI::l10n()->t('Pump.io Import/Export/Mirror'),
273 'image' => 'images/pumpio.png',
274 'enabled' => $enabled,
280 function pumpio_settings_post(App $a, array &$b)
282 if (!empty($_POST['pumpio_delete'])) {
283 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'consumer_key' , '');
284 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'consumer_secret' , '');
285 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'oauth_token' , '');
286 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'oauth_token_secret', '');
287 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'post' , false);
288 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'import' , false);
289 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'host' , '');
290 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'user' , '');
291 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'public' , false);
292 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'mirror' , false);
293 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'post_by_default' , false);
294 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'lastdate' , 0);
295 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'last_id' , '');
296 } elseif (!empty($_POST['pumpio-submit'])) {
297 // filtering the username if it is filled wrong
298 $user = $_POST['pumpio_user'];
299 if (strstr($user, '@')) {
300 $pos = strpos($user, '@');
303 $user = substr($user, 0, $pos);
307 // Filtering the hostname if someone is entering it with "http"
308 $host = $_POST['pumpio_host'];
310 $host = str_replace(['https://', 'http://'], ['', ''], $host);
312 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'post' , $_POST['pumpio'] ?? false);
313 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'import' , $_POST['pumpio_import'] ?? false);
314 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'host' , $host);
315 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'user' , $user);
316 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'public' , $_POST['pumpio_public'] ?? false);
317 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'mirror' , $_POST['pumpio_mirror'] ?? false);
318 DI::pConfig()->set(Session::getLocalUser(), 'pumpio', 'post_by_default', $_POST['pumpio_bydefault'] ?? false);
320 if (!empty($_POST['pumpio_mirror'])) {
321 DI::pConfig()->delete(Session::getLocalUser(), 'pumpio', 'lastdate');
326 function pumpio_load_config(App $a, ConfigFileLoader $loader)
328 $a->getConfigCache()->load($loader->loadAddonConfig('pumpio'));
331 function pumpio_hook_fork(App $a, array &$b)
333 if ($b['name'] != 'notifier_normal') {
339 // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
340 if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
341 $b['execute'] = false;
345 // if post comes from pump.io don't send it back
346 if ($post['app'] == 'pump.io') {
347 $b['execute'] = false;
351 if (DI::pConfig()->get($post['uid'], 'pumpio', 'import')) {
352 // Don't fork if it isn't a reply to a pump.io post
353 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::PUMPIO])) {
354 Logger::notice('No pump.io parent found for item ' . $post['id']);
355 $b['execute'] = false;
359 // Comments are never exported when we don't import the pumpio timeline
360 if (!strstr($post['postopts'], 'pumpio') || ($post['parent'] != $post['id']) || $post['private']) {
361 $b['execute'] = false;
367 function pumpio_post_local(App $a, array &$b)
369 if (!Session::getLocalUser() || (Session::getLocalUser() != $b['uid'])) {
373 $pumpio_post = intval(DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'post'));
375 $pumpio_enable = (($pumpio_post && !empty($_REQUEST['pumpio_enable'])) ? intval($_REQUEST['pumpio_enable']) : 0);
377 if ($b['api_source'] && intval(DI::pConfig()->get(Session::getLocalUser(), 'pumpio', 'post_by_default'))) {
381 if (!$pumpio_enable) {
385 if (strlen($b['postopts'])) {
386 $b['postopts'] .= ',';
389 $b['postopts'] .= 'pumpio';
392 function pumpio_send(App $a, array &$b)
394 if (!DI::pConfig()->get($b['uid'], 'pumpio', 'import') && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
398 Logger::debug('pumpio_send: parameter ', $b);
400 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
402 if ($b['parent'] != $b['id']) {
403 // Looking if its a reply to a pumpio post
404 $condition = ['id' => $b['parent'], 'network' => Protocol::PUMPIO];
405 $orig_post = Post::selectFirst([], $condition);
407 if (!DBA::isResult($orig_post)) {
408 Logger::notice('pumpio_send: no pumpio post ' . $b['parent']);
416 $receiver = pumpio_getreceiver($a, $b);
418 Logger::notice('pumpio_send: receiver ', $receiver);
420 if (!count($receiver) && ($b['private'] || !strstr($b['postopts'], 'pumpio'))) {
424 // Dont't post if the post doesn't belong to us.
425 // This is a check for forum postings
426 $self = User::getOwnerDataById($b['uid']);
427 if ($b['contact-id'] != $self['id']) {
432 if ($b['verb'] == Activity::LIKE) {
434 pumpio_action($a, $b['uid'], $b['thr-parent'], 'unlike');
436 pumpio_action($a, $b['uid'], $b['thr-parent'], 'like');
441 if ($b['verb'] == Activity::DISLIKE) {
445 if (($b['verb'] == Activity::POST) && ($b['created'] !== $b['edited']) && !$b['deleted']) {
446 pumpio_action($a, $b['uid'], $b['uri'], 'update', $b['body']);
449 if (($b['verb'] == Activity::POST) && $b['deleted']) {
450 pumpio_action($a, $b['uid'], $b['uri'], 'delete');
453 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
457 // if post comes from pump.io don't send it back
458 if ($b['app'] == 'pump.io') {
463 // Support for native shares
464 // http://<hostname>/api/<type>/shares?id=<the-object-id>
466 $oauth_token = DI::pConfig()->get($b['uid'], 'pumpio', 'oauth_token');
467 $oauth_token_secret = DI::pConfig()->get($b['uid'], 'pumpio', 'oauth_token_secret');
468 $consumer_key = DI::pConfig()->get($b['uid'], 'pumpio', 'consumer_key');
469 $consumer_secret = DI::pConfig()->get($b['uid'], 'pumpio', 'consumer_secret');
471 $host = DI::pConfig()->get($b['uid'], 'pumpio', 'host');
472 $user = DI::pConfig()->get($b['uid'], 'pumpio', 'user');
473 $public = DI::pConfig()->get($b['uid'], 'pumpio', 'public');
475 if ($oauth_token && $oauth_token_secret) {
476 $title = trim($b['title']);
478 $content = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
482 $params['verb'] = 'post';
485 $params['object'] = [
486 'objectType' => 'note',
487 'content' => $content];
489 if (!empty($title)) {
490 $params['object']['displayName'] = $title;
493 if (!empty($receiver['to'])) {
494 $params['to'] = $receiver['to'];
497 if (!empty($receiver['bto'])) {
498 $params['bto'] = $receiver['bto'];
501 if (!empty($receiver['cc'])) {
502 $params['cc'] = $receiver['cc'];
505 if (!empty($receiver['bcc'])) {
506 $params['bcc'] = $receiver['bcc'];
510 'id' => $orig_post['uri'],
511 'objectType' => 'note',
514 if (($orig_post['object-type'] != '') && (strstr($orig_post['object-type'], ActivityNamespace::ACTIVITY_SCHEMA))) {
515 $inReplyTo['objectType'] = str_replace(ActivityNamespace::ACTIVITY_SCHEMA, '', $orig_post['object-type']);
518 $params['object'] = [
519 'objectType' => 'comment',
520 'content' => $content,
521 'inReplyTo' => $inReplyTo];
524 $params['object']['displayName'] = $title;
528 $client = new oauth_client_class;
529 $client->oauth_version = '1.0a';
530 $client->url_parameters = false;
531 $client->authorization_header = true;
532 $client->access_token = $oauth_token;
533 $client->access_token_secret = $oauth_token_secret;
534 $client->client_id = $consumer_key;
535 $client->client_secret = $consumer_secret;
537 $username = $user . '@' . $host;
538 $url = 'https://' . $host . '/api/user/' . $user . '/feed';
540 if (pumpio_reachable($url)) {
541 $success = $client->CallAPI($url, 'POST', $params, ['FailOnAccessError' => true, 'RequestContentType' => 'application/json'], $user);
547 if ($user->generator->displayName) {
548 DI::pConfig()->set($b['uid'], 'pumpio', 'application_name', $user->generator->displayName);
551 $post_id = $user->object->id;
552 Logger::notice('pumpio_send ' . $username . ': success ' . $post_id);
553 if ($post_id && $iscomment) {
554 Logger::notice('pumpio_send ' . $username . ': Update extid ' . $post_id . ' for post id ' . $b['id']);
555 Item::update(['extid' => $post_id], ['id' => $b['id']]);
558 Logger::notice('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user, true));
564 function pumpio_action(App $a, int $uid, string $uri, string $action, string $content = '')
566 // Don't do likes and other stuff if you don't import the timeline
567 if (!DI::pConfig()->get($uid, 'pumpio', 'import')) {
571 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
572 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
573 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
574 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
575 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
576 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
578 $orig_post = Post::selectFirst([], ['uri' => $uri, 'uid' => $uid]);
580 if (!DBA::isResult($orig_post)) {
584 if ($orig_post['extid'] && !strstr($orig_post['extid'], '/proxy/')) {
585 $uri = $orig_post['extid'];
587 $uri = $orig_post['uri'];
590 if (($orig_post['object-type'] != '') && (strstr($orig_post['object-type'], ActivityNamespace::ACTIVITY_SCHEMA))) {
591 $objectType = str_replace(ActivityNamespace::ACTIVITY_SCHEMA, '', $orig_post['object-type']);
592 } elseif (strstr($uri, '/api/comment/')) {
593 $objectType = 'comment';
594 } elseif (strstr($uri, '/api/note/')) {
595 $objectType = 'note';
596 } elseif (strstr($uri, '/api/image/')) {
597 $objectType = 'image';
600 $params['verb'] = $action;
601 $params['object'] = [
603 'objectType' => $objectType,
604 'content' => $content,
607 $client = new oauth_client_class;
608 $client->oauth_version = '1.0a';
609 $client->authorization_header = true;
610 $client->url_parameters = false;
612 $client->client_id = $ckey;
613 $client->client_secret = $csecret;
614 $client->access_token = $otoken;
615 $client->access_token_secret = $osecret;
617 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
619 if (pumpio_reachable($url)) {
620 $success = $client->CallAPI($url, 'POST', $params, ['FailOnAccessError' => true, 'RequestContentType' => 'application/json'], $user);
626 Logger::notice('pumpio_action '.$username.' '.$action.': success '.$uri);
628 Logger::notice('pumpio_action '.$username.' '.$action.': general error: '.$uri);
633 function pumpio_sync(App $a)
635 if (!Addon::isEnabled('pumpio')) {
639 $last = DI::config()->get('pumpio', 'last_poll');
641 $poll_interval = intval(DI::config()->get('pumpio', 'poll_interval', PUMPIO_DEFAULT_POLL_INTERVAL));
644 $next = $last + ($poll_interval * 60);
645 if ($next > time()) {
646 Logger::notice('pumpio: poll intervall not reached');
650 Logger::notice('pumpio: cron_start');
652 $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'pumpio', 'k' => 'mirror', 'v' => '1']);
653 foreach ($pconfigs as $rr) {
654 Logger::notice('pumpio: mirroring user '.$rr['uid']);
655 pumpio_fetchtimeline($a, $rr['uid']);
658 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
659 if ($abandon_days < 1) {
663 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
665 $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'pumpio', 'k' => 'import', 'v' => '1']);
666 foreach ($pconfigs as $rr) {
667 if ($abandon_days != 0) {
668 if (DBA::exists('user', ["uid = ? AND `login_date` >= ?", $rr['uid'], $abandon_limit])) {
669 Logger::notice('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
674 Logger::notice('pumpio: importing timeline from user '.$rr['uid']);
675 pumpio_fetchinbox($a, $rr['uid']);
677 // check for new contacts once a day
678 $last_contact_check = DI::pConfig()->get($rr['uid'], 'pumpio', 'contact_check');
679 if ($last_contact_check) {
680 $next_contact_check = $last_contact_check + 86400;
682 $next_contact_check = 0;
685 if ($next_contact_check <= time()) {
686 pumpio_getallusers($a, $rr['uid']);
687 DI::pConfig()->set($rr['uid'], 'pumpio', 'contact_check', time());
691 Logger::notice('pumpio: cron_end');
693 DI::config()->set('pumpio', 'last_poll', time());
696 function pumpio_cron(App $a, $b)
698 Worker::add(Worker::PRIORITY_MEDIUM, 'addon/pumpio/pumpio_sync.php');
701 function pumpio_fetchtimeline(App $a, int $uid)
703 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
704 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
705 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
706 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
707 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
708 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
709 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
711 // get the application name for the pump.io app
712 // 1st try personal config, then system config and fallback to the
713 // hostname of the node if neither one is set.
714 $application_name = DI::pConfig()->get($uid, 'pumpio', 'application_name');
715 if ($application_name == '') {
716 $application_name = DI::config()->get('pumpio', 'application_name');
718 if ($application_name == '') {
719 $application_name = DI::baseUrl()->getHostname();
722 $first_time = ($lastdate == '');
724 $client = new oauth_client_class;
725 $client->oauth_version = '1.0a';
726 $client->authorization_header = true;
727 $client->url_parameters = false;
729 $client->client_id = $ckey;
730 $client->client_secret = $csecret;
731 $client->access_token = $otoken;
732 $client->access_token_secret = $osecret;
734 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
736 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);
738 $useraddr = $username.'@'.$hostname;
740 if (pumpio_reachable($url)) {
741 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $user);
748 Logger::notice('pumpio: error fetching posts for user ' . $uid . ' ' . $useraddr . ' ', $user);
752 $posts = array_reverse($user->items);
754 $initiallastdate = $lastdate;
758 foreach ($posts as $post) {
759 if ($post->published <= $initiallastdate) {
763 if ($lastdate < $post->published) {
764 $lastdate = $post->published;
772 if (@is_array($post->cc)) {
773 $receiptians = array_merge($receiptians, $post->cc);
776 if (@is_array($post->to)) {
777 $receiptians = array_merge($receiptians, $post->to);
781 foreach ($receiptians as $receiver) {
782 if (is_string($receiver->objectType) && ($receiver->id == 'http://activityschema.org/collection/public')) {
787 if ($public && !stristr($post->generator->displayName, $application_name)) {
788 $_SESSION['authenticated'] = true;
789 $_SESSION['uid'] = $uid;
792 $_REQUEST['api_source'] = true;
793 $_REQUEST['profile_uid'] = $uid;
794 $_REQUEST['source'] = 'pump.io';
796 if (isset($post->object->id)) {
797 $_REQUEST['message_id'] = Protocol::PUMPIO . ':' . $post->object->id;
800 if ($post->object->displayName != '') {
801 $_REQUEST['title'] = HTML::toBBCode($post->object->displayName);
803 $_REQUEST['title'] = '';
806 $_REQUEST['body'] = HTML::toBBCode($post->object->content);
808 // To-Do: Picture has to be cached and stored locally
809 if ($post->object->fullImage->url != '') {
810 if ($post->object->fullImage->pump_io->proxyURL != '') {
811 $_REQUEST['body'] = '[url=' . $post->object->fullImage->pump_io->proxyURL . '][img]' . $post->object->image->pump_io->proxyURL . "[/img][/url]\n" . $_REQUEST['body'];
813 $_REQUEST['body'] = '[url=' . $post->object->fullImage->url . '][img]' . $post->object->image->url . "[/img][/url]\n" . $_REQUEST['body'];
817 Logger::notice('pumpio: posting for user ' . $uid);
819 require_once 'mod/item.php';
822 Logger::notice('pumpio: posting done - user ' . $uid);
827 if ($lastdate != 0) {
828 DI::pConfig()->set($uid, 'pumpio', 'lastdate', $lastdate);
832 function pumpio_dounlike(App $a, int $uid, array $self, $post, string $own_id)
834 // Searching for the unliked post
835 // Two queries for speed issues
836 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
837 if (!DBA::isResult($orig_post)) {
838 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
839 if (!DBA::isResult($orig_post)) {
846 if (Strings::compareLink($post->actor->url, $own_id)) {
847 $contactid = $self['id'];
849 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
850 if (DBA::isResult($contact)) {
851 $contactid = $contact['id'];
854 if ($contactid == 0) {
855 $contactid = $orig_post['contact-id'];
859 Item::markForDeletion(['verb' => Activity::LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']]);
861 if (DBA::isResult($contact)) {
862 Logger::notice('pumpio_dounlike: unliked existing like. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
864 Logger::notice('pumpio_dounlike: not found. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' Url ' . $orig_post['uri']);
868 function pumpio_dolike(App $a, int $uid, array $self, $post, string $own_id, $threadcompletion = true)
870 if (empty($post->object->id)) {
871 Logger::info('Got empty like: '.print_r($post, true));
875 // Searching for the liked post
876 // Two queries for speed issues
877 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
878 if (!DBA::isResult($orig_post)) {
879 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
880 if (!DBA::isResult($orig_post)) {
886 if ($threadcompletion) {
887 pumpio_fetchallcomments($a, $uid, $post->object->id);
892 if (Strings::compareLink($post->actor->url, $own_id)) {
893 $contactid = $self['id'];
894 $post->actor->displayName = $self['name'];
895 $post->actor->url = $self['url'];
896 $post->actor->image->url = $self['photo'];
898 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
899 if (DBA::isResult($contact)) {
900 $contactid = $contact['id'];
903 if ($contactid == 0) {
904 $contactid = $orig_post['contact-id'];
909 'verb' => Activity::LIKE,
911 'contact-id' => $contactid,
912 'thr-parent' => $orig_post['uri'],
915 if (Post::exists($condition)) {
916 Logger::notice('pumpio_dolike: found existing like. User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
921 $likedata['parent'] = $orig_post['id'];
922 $likedata['verb'] = Activity::LIKE;
923 $likedata['gravity'] = Item::GRAVITY_ACTIVITY;
924 $likedata['uid'] = $uid;
925 $likedata['wall'] = 0;
926 $likedata['network'] = Protocol::PUMPIO;
927 $likedata['uri'] = Item::newURI();
928 $likedata['thr-parent'] = $orig_post['uri'];
929 $likedata['contact-id'] = $contactid;
930 $likedata['app'] = $post->generator->displayName;
931 $likedata['author-name'] = $post->actor->displayName;
932 $likedata['author-link'] = $post->actor->url;
933 if (!empty($post->actor->image)) {
934 $likedata['author-avatar'] = $post->actor->image->url;
937 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
938 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
939 $post_type = DI::l10n()->t('status');
940 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
941 $likedata['object-type'] = Activity\ObjectType::NOTE;
943 $likedata['body'] = DI::l10n()->t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
945 $likedata['object'] = '<object><type>' . Activity\ObjectType::NOTE . '</type><local>1</local>' .
946 '<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>';
948 $ret = Item::insert($likedata);
950 Logger::notice('pumpio_dolike: ' . $ret . ' User ' . $own_id . ' ' . $uid . ' Contact: ' . $contactid . ' URI ' . $orig_post['uri']);
953 function pumpio_get_contact($uid, $contact, $no_insert = false)
955 $cid = Contact::getIdForURL($contact->url, $uid);
961 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
962 if (!DBA::isResult($r)) {
963 // create contact record
966 'created' => DateTimeFormat::utcNow(),
967 'url' => $contact->url,
968 'nurl' => Strings::normaliseLink($contact->url),
969 'addr' => str_replace('acct:', '', $contact->id),
971 'notify' => $contact->id,
972 'poll' => 'pump.io ' . $contact->id,
973 'name' => $contact->displayName,
974 'nick' => $contact->preferredUsername,
975 'photo' => $contact->image->url,
976 'network' => Protocol::PUMPIO,
977 'rel' => Contact::FRIEND,
979 'location' => $contact->location->displayName,
980 'about' => $contact->summary,
987 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
988 if (!DBA::isResult($r)) {
992 $contact_id = $r['id'];
994 Group::addMember(User::getDefaultGroup($uid), $contact_id);
996 $contact_id = $r['id'];
999 if (!empty($contact->image->url)) {
1000 Contact::updateAvatar($contact_id, $contact->image->url);
1006 function pumpio_dodelete(App $a, int $uid, array $self, $post, string $own_id)
1008 // Two queries for speed issues
1009 $condition = ['uri' => $post->object->id, 'uid' => $uid];
1010 if (Post::exists($condition)) {
1011 Item::markForDeletion($condition);
1015 $condition = ['extid' => $post->object->id, 'uid' => $uid];
1016 if (Post::exists($condition)) {
1017 Item::markForDeletion($condition);
1023 function pumpio_dopost(App $a, $client, int $uid, array $self, $post, string $own_id, bool $threadcompletion = true)
1025 if (($post->verb == 'like') || ($post->verb == 'favorite')) {
1026 return pumpio_dolike($a, $uid, $self, $post, $own_id);
1029 if (($post->verb == 'unlike') || ($post->verb == 'unfavorite')) {
1030 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1033 if ($post->verb == 'delete') {
1034 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1037 if ($post->verb != 'update') {
1038 // Two queries for speed issues
1039 if (Post::exists(['uri' => $post->object->id, 'uid' => $uid])) {
1042 if (Post::exists(['extid' => $post->object->id, 'uid' => $uid])) {
1047 // Only handle these three types
1048 if (!strstr('post|share|update', $post->verb)) {
1053 if (@is_array($post->cc)) {
1054 $receiptians = array_merge($receiptians, $post->cc);
1057 if (@is_array($post->to)) {
1058 $receiptians = array_merge($receiptians, $post->to);
1063 foreach ($receiptians as $receiver) {
1064 if (is_string($receiver->objectType) && ($receiver->id == 'http://activityschema.org/collection/public')) {
1070 $postarray['network'] = Protocol::PUMPIO;
1071 $postarray['uid'] = $uid;
1072 $postarray['wall'] = 0;
1073 $postarray['uri'] = $post->object->id;
1074 $postarray['object-type'] = ActivityNamespace::ACTIVITY_SCHEMA . strtolower($post->object->objectType);
1076 if ($post->object->objectType != 'comment') {
1077 $contact_id = pumpio_get_contact($uid, $post->actor);
1080 $contact_id = $self['id'];
1083 $postarray['thr-parent'] = $post->object->id;
1086 $postarray['private'] = 1;
1087 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1090 $contact_id = pumpio_get_contact($uid, $post->actor, true);
1092 if (Strings::compareLink($post->actor->url, $own_id)) {
1093 $contact_id = $self['id'];
1094 $post->actor->displayName = $self['name'];
1095 $post->actor->url = $self['url'];
1096 $post->actor->image->url = $self['photo'];
1097 } elseif ($contact_id == 0) {
1098 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1099 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
1100 if (DBA::isResult($contact)) {
1101 $contact_id = $contact['id'];
1103 $contact_id = $self['id'];
1107 $reply = new stdClass;
1108 $reply->verb = 'note';
1110 if (isset($post->cc)) {
1111 $reply->cc = $post->cc;
1114 if (isset($post->to)) {
1115 $reply->to = $post->to;
1118 $reply->object = new stdClass;
1119 $reply->object->objectType = $post->object->inReplyTo->objectType;
1120 $reply->object->content = $post->object->inReplyTo->content;
1121 $reply->object->id = $post->object->inReplyTo->id;
1122 $reply->actor = $post->object->inReplyTo->author;
1123 $reply->url = $post->object->inReplyTo->url;
1124 $reply->generator = new stdClass;
1125 $reply->generator->displayName = 'pumpio';
1126 $reply->published = $post->object->inReplyTo->published;
1127 $reply->received = $post->object->inReplyTo->updated;
1128 $reply->url = $post->object->inReplyTo->url;
1129 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id, false);
1131 $postarray['thr-parent'] = $post->object->inReplyTo->id;
1134 // When there is no content there is no need to continue
1135 if (empty($post->object->content)) {
1139 if (!empty($post->object->pump_io->proxyURL)) {
1140 $postarray['extid'] = $post->object->pump_io->proxyURL;
1143 $postarray['contact-id'] = $contact_id;
1144 $postarray['verb'] = Activity::POST;
1145 $postarray['owner-name'] = $post->actor->displayName;
1146 $postarray['owner-link'] = $post->actor->url;
1147 $postarray['author-name'] = $postarray['owner-name'];
1148 $postarray['author-link'] = $postarray['owner-link'];
1149 if (!empty($post->actor->image)) {
1150 $postarray['owner-avatar'] = $post->actor->image->url;
1151 $postarray['author-avatar'] = $postarray['owner-avatar'];
1153 $postarray['plink'] = $post->object->url;
1154 $postarray['app'] = $post->generator->displayName;
1155 $postarray['title'] = '';
1156 $postarray['body'] = HTML::toBBCode($post->object->content);
1157 $postarray['object'] = json_encode($post);
1159 if (!empty($post->object->fullImage->url)) {
1160 $postarray['body'] = '[url=' . $post->object->fullImage->url . '][img]' . $post->object->image->url . "[/img][/url]\n" . $postarray['body'];
1163 if (!empty($post->object->displayName)) {
1164 $postarray['title'] = $post->object->displayName;
1167 $postarray['created'] = DateTimeFormat::utc($post->published);
1168 if (isset($post->updated)) {
1169 $postarray['edited'] = DateTimeFormat::utc($post->updated);
1170 } elseif (isset($post->received)) {
1171 $postarray['edited'] = DateTimeFormat::utc($post->received);
1173 $postarray['edited'] = $postarray['created'];
1176 if ($post->verb == 'share') {
1177 if (isset($post->object->author->displayName) && ($post->object->author->displayName != '')) {
1178 $share_author = $post->object->author->displayName;
1179 } elseif (isset($post->object->author->preferredUsername) && ($post->object->author->preferredUsername != '')) {
1180 $share_author = $post->object->author->preferredUsername;
1182 $share_author = $post->object->author->url;
1185 if (isset($post->object->created)) {
1186 $created = DateTimeFormat::utc($post->object->created);
1191 $postarray['body'] = Friendica\Content\Text\BBCode::getShareOpeningTag($share_author, $post->object->author->url,
1192 $post->object->author->image->url, $post->links->self->href, $created) .
1193 $postarray['body'] . '[/share]';
1196 if (trim($postarray['body']) == '') {
1200 $top_item = Item::insert($postarray);
1201 $postarray['id'] = $top_item;
1203 if (($top_item == 0) && ($post->verb == 'update')) {
1205 'title' => $postarray['title'],
1206 'body' => $postarray['body'],
1207 'changed' => $postarray['edited'],
1209 $condition = ['uri' => $postarray['uri'], 'uid' => $uid];
1210 Item::update($fields, $condition);
1213 if (($post->object->objectType == 'comment') && $threadcompletion) {
1214 pumpio_fetchallcomments($a, $uid, $postarray['thr-parent']);
1220 function pumpio_fetchinbox(App $a, int $uid)
1222 $ckey= DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1223 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1224 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1225 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1226 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
1227 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1228 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1230 $own_id = 'https://' . $hostname . '/' . $username;
1232 $self = User::getOwnerDataById($uid);
1234 $lastitems = DBA::p("SELECT `uri` FROM `post-thread-user`
1235 INNER JOIN `post-view` ON `post-view`.`id` = `post-thread-user`.`id`
1236 WHERE `post-thread-user`.`network` = ? AND `post-thread-user`.`uid` = ? AND `post-view`.`extid` != ''
1237 ORDER BY `post-thread-user`.`commented` DESC LIMIT 10", Protocol::PUMPIO, $uid);
1239 $client = new oauth_client_class();
1240 $client->oauth_version = '1.0a';
1241 $client->authorization_header = true;
1242 $client->url_parameters = false;
1244 $client->client_id = $ckey;
1245 $client->client_secret = $csecret;
1246 $client->access_token = $otoken;
1247 $client->access_token_secret = $osecret;
1249 $last_id = DI::pConfig()->get($uid, 'pumpio', 'last_id');
1251 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1253 if ($last_id != '') {
1254 $url .= '?since=' . urlencode($last_id);
1257 if (pumpio_reachable($url)) {
1258 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $user);
1267 if (!empty($user->items)) {
1268 $posts = array_reverse($user->items);
1270 if (count($posts)) {
1271 foreach ($posts as $post) {
1272 $last_id = $post->id;
1273 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, true);
1278 while ($item = DBA::fetch($lastitems)) {
1279 pumpio_fetchallcomments($a, $uid, $item['uri']);
1281 DBA::close($lastitems);
1283 DI::pConfig()->set($uid, 'pumpio', 'last_id', $last_id);
1286 function pumpio_getallusers(App &$a, int $uid)
1288 $ckey= DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1289 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1290 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1291 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1292 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1293 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1295 $client = new oauth_client_class;
1296 $client->oauth_version = '1.0a';
1297 $client->authorization_header = true;
1298 $client->url_parameters = false;
1300 $client->client_id = $ckey;
1301 $client->client_secret = $csecret;
1302 $client->access_token = $otoken;
1303 $client->access_token_secret = $osecret;
1305 $url = 'https://' . $hostname . '/api/user/' . $username . '/following';
1307 if (pumpio_reachable($url)) {
1308 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1313 if (empty($users)) {
1317 if ($users->totalItems > count($users->items)) {
1318 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1320 if (pumpio_reachable($url)) {
1321 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1327 if (!empty($users->items)) {
1328 foreach ($users->items as $user) {
1329 pumpio_get_contact($uid, $user);
1334 function pumpio_getreceiver(App $a, array $b)
1338 if (!$b['private']) {
1339 if (!strstr($b['postopts'], 'pumpio')) {
1343 $public = DI::pConfig()->get($b['uid'], 'pumpio', 'public');
1346 $receiver['to'][] = [
1347 'objectType' => 'collection',
1348 'id' => 'http://activityschema.org/collection/public'
1352 $cids = explode('><', $b['allow_cid']);
1353 $gids = explode('><', $b['allow_gid']);
1355 foreach ($cids as $cid) {
1356 $cid = trim($cid, ' <>');
1358 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b['uid'], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1359 if (DBA::isResult($contact)) {
1360 $receiver['bcc'][] = [
1361 'displayName' => $contact['name'],
1362 'objectType' => 'person',
1363 'preferredUsername' => $contact['nick'],
1364 'url' => $contact['url'],
1368 foreach ($gids as $gid) {
1369 $gid = trim($gid, ' <>');
1371 $contacts = DBA::p("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network`
1372 FROM `group_member`, `contact` WHERE `group_member`.`gid` = ?
1373 AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = ?",
1374 $gid, Protocol::PUMPIO);
1376 while ($row = DBA::fetch($contacts)) {
1377 $receiver['bcc'][] = [
1378 'displayName' => $row['name'],
1379 'objectType' => 'person',
1380 'preferredUsername' => $row['nick'],
1381 'url' => $row['url'],
1384 DBA::close($contacts);
1388 if ($b['inform'] != '') {
1389 $inform = explode(',', $b['inform']);
1391 foreach ($inform as $cid) {
1392 if (substr($cid, 0, 4) != 'cid:') {
1396 $cid = str_replace('cid:', '', $cid);
1398 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b['uid'], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1399 if (DBA::isResult($contact)) {
1400 $receiver['to'][] = [
1401 'displayName' => $contact['name'],
1402 'objectType' => 'person',
1403 'preferredUsername' => $contact['nick'],
1404 'url' => $contact['url'],
1413 function pumpio_fetchallcomments(App $a, $uid, $id)
1415 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1416 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1417 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1418 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1419 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1420 $username = DI::pConfig()->get($uid, 'pumpio', 'user');
1422 Logger::notice('pumpio_fetchallcomments: completing comment for user ' . $uid . ' post id ' . $id);
1424 $own_id = 'https://' . $hostname . '/' . $username;
1426 $self = User::getOwnerDataById($uid);
1428 // Fetching the original post
1429 $condition = ["`uri` = ? AND `uid` = ? AND `extid` != ''", $id, $uid];
1430 $original = Post::selectFirst(['extid'], $condition);
1431 if (!DBA::isResult($original)) {
1435 $url = $original['extid'];
1437 $client = new oauth_client_class;
1438 $client->oauth_version = '1.0a';
1439 $client->authorization_header = true;
1440 $client->url_parameters = false;
1442 $client->client_id = $ckey;
1443 $client->client_secret = $csecret;
1444 $client->access_token = $otoken;
1445 $client->access_token_secret = $osecret;
1447 Logger::notice('pumpio_fetchallcomments: fetching comment for user ' . $uid . ', URL ' . $url);
1449 if (pumpio_reachable($url)) {
1450 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $item);
1459 if ($item->likes->totalItems != 0) {
1460 foreach ($item->likes->items as $post) {
1461 $like = new stdClass;
1462 $like->object = new stdClass;
1463 $like->object->id = $item->id;
1464 $like->actor = new stdClass;
1465 if (!empty($item->displayName)) {
1466 $like->actor->displayName = $item->displayName;
1468 //$like->actor->preferredUsername = $item->preferredUsername;
1469 //$like->actor->image = $item->image;
1470 $like->actor->url = $item->url;
1471 $like->generator = new stdClass;
1472 $like->generator->displayName = 'pumpio';
1473 pumpio_dolike($a, $uid, $self, $post, $own_id, false);
1477 if ($item->replies->totalItems == 0) {
1481 foreach ($item->replies->items as $item) {
1482 if ($item->id == $id) {
1486 // Checking if the comment already exists - Two queries for speed issues
1487 if (Post::exists(['uri' => $item->id, 'uid' => $uid])) {
1491 if (Post::exists(['extid' => $item->id, 'uid' => $uid])) {
1495 $post = new stdClass;
1496 $post->verb = 'post';
1497 $post->actor = $item->author;
1498 $post->published = $item->published;
1499 $post->received = $item->updated;
1500 $post->generator = new stdClass;
1501 $post->generator->displayName = 'pumpio';
1502 // To-Do: Check for public post
1504 unset($item->author);
1505 unset($item->published);
1506 unset($item->updated);
1508 $post->object = $item;
1510 Logger::notice('pumpio_fetchallcomments: posting comment ' . $post->object->id . ' ', json_decode(json_encode($post), true));
1511 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1515 function pumpio_reachable(string $url): bool
1517 return DI::httpClient()->get($url, HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => 10])->isSuccess();