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\Worker;
17 use Friendica\Database\DBA;
19 use Friendica\Model\Contact;
20 use Friendica\Model\Group;
21 use Friendica\Model\Item;
22 use Friendica\Model\Post;
23 use Friendica\Model\User;
24 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
25 use Friendica\Protocol\Activity;
26 use Friendica\Protocol\ActivityNamespace;
27 use Friendica\Core\Config\Util\ConfigFileLoader;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Strings;
30 use Friendica\Util\XML;
32 require 'addon/pumpio/oauth/http.php';
33 require 'addon/pumpio/oauth/oauth_client.php';
34 require_once "mod/share.php";
36 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
38 function pumpio_install()
40 Hook::register('load_config', 'addon/pumpio/pumpio.php', 'pumpio_load_config');
41 Hook::register('hook_fork', 'addon/pumpio/pumpio.php', 'hook_fork');
42 Hook::register('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
43 Hook::register('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
44 Hook::register('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
45 Hook::register('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
46 Hook::register('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
47 Hook::register('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
48 Hook::register('check_item_notification', 'addon/pumpio/pumpio.php', 'pumpio_check_item_notification');
51 function pumpio_module() {}
53 function pumpio_content(App $a)
56 notice(DI::l10n()->t('Permission denied.') . EOL);
60 require_once("mod/settings.php");
63 if (isset(DI::args()->getArgv()[1])) {
64 switch (DI::args()->getArgv()[1]) {
66 $o = pumpio_connect($a);
69 $o = print_r(DI::args()->getArgv(), true);
73 $o = pumpio_connect($a);
78 function pumpio_check_item_notification($a, &$notification_data)
80 $hostname = DI::pConfig()->get($notification_data["uid"], 'pumpio', 'host');
81 $username = DI::pConfig()->get($notification_data["uid"], "pumpio", "user");
83 $notification_data["profiles"][] = "https://".$hostname."/".$username;
86 function pumpio_registerclient(App $a, $host)
88 $url = "https://".$host."/api/client/register";
92 $application_name = DI::config()->get('pumpio', 'application_name');
94 if ($application_name == "") {
95 $application_name = DI::baseUrl()->getHostname();
98 $adminlist = explode(",", str_replace(" ", "", DI::config()->get('config', 'admin_email')));
100 $params["type"] = "client_associate";
101 $params["contacts"] = $adminlist[0];
102 $params["application_type"] = "native";
103 $params["application_name"] = $application_name;
104 $params["logo_url"] = DI::baseUrl()->get()."/images/friendica-256.png";
105 $params["redirect_uris"] = DI::baseUrl()->get()."/pumpio/connect";
107 Logger::info("pumpio_registerclient: ".$url." parameters ".print_r($params, true));
109 $ch = curl_init($url);
110 curl_setopt($ch, CURLOPT_HEADER, false);
111 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
112 curl_setopt($ch, CURLOPT_POST,1);
113 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
114 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
117 $curl_info = curl_getinfo($ch);
119 if ($curl_info["http_code"] == "200") {
120 $values = json_decode($s);
121 Logger::info("pumpio_registerclient: success ".print_r($values, true));
124 Logger::info("pumpio_registerclient: failed: ".print_r($curl_info, true));
129 function pumpio_connect(App $a)
131 // Define the needed keys
132 $consumer_key = DI::pConfig()->get(local_user(), 'pumpio', 'consumer_key');
133 $consumer_secret = DI::pConfig()->get(local_user(), 'pumpio', 'consumer_secret');
134 $hostname = DI::pConfig()->get(local_user(), 'pumpio', 'host');
136 if ((($consumer_key == "") || ($consumer_secret == "")) && ($hostname != "")) {
137 Logger::notice("pumpio_connect: register client");
138 $clientdata = pumpio_registerclient($a, $hostname);
139 DI::pConfig()->set(local_user(), 'pumpio', 'consumer_key', $clientdata->client_id);
140 DI::pConfig()->set(local_user(), 'pumpio', 'consumer_secret', $clientdata->client_secret);
142 $consumer_key = DI::pConfig()->get(local_user(), 'pumpio', 'consumer_key');
143 $consumer_secret = DI::pConfig()->get(local_user(), 'pumpio', 'consumer_secret');
145 Logger::info("pumpio_connect: ckey: ".$consumer_key." csecrect: ".$consumer_secret);
148 if (($consumer_key == "") || ($consumer_secret == "")) {
149 Logger::notice("pumpio_connect: ".sprintf("Unable to register the client at the pump.io server '%s'.", $hostname));
151 return DI::l10n()->t("Unable to register the client at the pump.io server '%s'.", $hostname);
154 // The callback URL is the script that gets called after the user authenticates with pumpio
155 $callback_url = DI::baseUrl()->get()."/pumpio/connect";
157 // Let's begin. First we need a Request Token. The request token is required to send the user
158 // to pumpio's login page.
160 // Create a new instance of the oauth_client_class library. For this step, all we need to give the library is our
161 // Consumer Key and Consumer Secret
162 $client = new oauth_client_class;
164 $client->server = '';
165 $client->oauth_version = '1.0a';
166 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
167 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
168 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
169 $client->url_parameters = false;
170 $client->authorization_header = true;
171 $client->redirect_uri = $callback_url;
172 $client->client_id = $consumer_key;
173 $client->client_secret = $consumer_secret;
175 if (($success = $client->Initialize())) {
176 if (($success = $client->Process())) {
177 if (strlen($client->access_token)) {
178 Logger::info("pumpio_connect: otoken: ".$client->access_token." osecrect: ".$client->access_token_secret);
179 DI::pConfig()->set(local_user(), "pumpio", "oauth_token", $client->access_token);
180 DI::pConfig()->set(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
183 $success = $client->Finalize($success);
186 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
190 Logger::notice("pumpio_connect: authenticated");
191 $o = DI::l10n()->t("You are now authenticated to pumpio.");
192 $o .= '<br /><a href="'.DI::baseUrl()->get().'/settings/connectors">'.DI::l10n()->t("return to the connector page").'</a>';
194 Logger::notice("pumpio_connect: could not connect");
195 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
201 function pumpio_jot_nets(App $a, array &$jotnets_fields)
203 if (! local_user()) {
207 if (DI::pConfig()->get(local_user(), 'pumpio', 'post')) {
208 $jotnets_fields[] = [
209 'type' => 'checkbox',
212 DI::l10n()->t('Post to pumpio'),
213 DI::pConfig()->get(local_user(), 'pumpio', 'post_by_default')
219 function pumpio_settings(App $a, &$s)
225 /* Add our stylesheet to the page so we can make our settings look nice */
227 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
229 /* Get the current state of our config variables */
231 $import_enabled = DI::pConfig()->get(local_user(), 'pumpio', 'import');
232 $import_checked = (($import_enabled) ? ' checked="checked" ' : '');
234 $enabled = DI::pConfig()->get(local_user(), 'pumpio', 'post');
235 $checked = (($enabled) ? ' checked="checked" ' : '');
236 $css = (($enabled) ? '' : '-disabled');
238 $def_enabled = DI::pConfig()->get(local_user(), 'pumpio', 'post_by_default');
239 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
241 $public_enabled = DI::pConfig()->get(local_user(), 'pumpio', 'public');
242 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
244 $mirror_enabled = DI::pConfig()->get(local_user(), 'pumpio', 'mirror');
245 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
247 $servername = DI::pConfig()->get(local_user(), "pumpio", "host");
248 $username = DI::pConfig()->get(local_user(), "pumpio", "user");
250 /* Add some HTML to the existing form */
252 $s .= '<span id="settings_pumpio_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_pumpio_expanded\'); openClose(\'settings_pumpio_inflated\');">';
253 $s .= '<img class="connector'.$css.'" src="images/pumpio.png" /><h3 class="connector">'. DI::l10n()->t('Pump.io Import/Export/Mirror').'</h3>';
255 $s .= '<div id="settings_pumpio_expanded" class="settings-block" style="display: none;">';
256 $s .= '<span class="fakelink" onclick="openClose(\'settings_pumpio_expanded\'); openClose(\'settings_pumpio_inflated\');">';
257 $s .= '<img class="connector'.$css.'" src="images/pumpio.png" /><h3 class="connector">'. DI::l10n()->t('Pump.io Import/Export/Mirror').'</h3>';
260 $s .= '<div id="pumpio-username-wrapper">';
261 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.DI::l10n()->t('pump.io username (without the servername)').'</label>';
262 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
263 $s .= '</div><div class="clear"></div>';
265 $s .= '<div id="pumpio-servername-wrapper">';
266 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.DI::l10n()->t('pump.io servername (without "http://" or "https://" )').'</label>';
267 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
268 $s .= '</div><div class="clear"></div>';
270 if (($username != '') && ($servername != '')) {
271 $oauth_token = DI::pConfig()->get(local_user(), "pumpio", "oauth_token");
272 $oauth_token_secret = DI::pConfig()->get(local_user(), "pumpio", "oauth_token_secret");
274 $s .= '<div id="pumpio-password-wrapper">';
275 if (($oauth_token == "") || ($oauth_token_secret == "")) {
276 $s .= '<div id="pumpio-authenticate-wrapper">';
277 $s .= '<a href="'.DI::baseUrl()->get().'/pumpio/connect">'.DI::l10n()->t("Authenticate your pump.io connection").'</a>';
278 $s .= '</div><div class="clear"></div>';
280 $s .= '<div id="pumpio-import-wrapper">';
281 $s .= '<label id="pumpio-import-label" for="pumpio-import">' . DI::l10n()->t('Import the remote timeline') . '</label>';
282 $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
283 $s .= '</div><div class="clear"></div>';
285 $s .= '<div id="pumpio-enable-wrapper">';
286 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . DI::l10n()->t('Enable pump.io Post Addon') . '</label>';
287 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
288 $s .= '</div><div class="clear"></div>';
290 $s .= '<div id="pumpio-bydefault-wrapper">';
291 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . DI::l10n()->t('Post to pump.io by default') . '</label>';
292 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
293 $s .= '</div><div class="clear"></div>';
295 $s .= '<div id="pumpio-public-wrapper">';
296 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . DI::l10n()->t('Should posts be public?') . '</label>';
297 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
298 $s .= '</div><div class="clear"></div>';
300 $s .= '<div id="pumpio-mirror-wrapper">';
301 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . DI::l10n()->t('Mirror all public posts') . '</label>';
302 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
303 $s .= '</div><div class="clear"></div>';
305 $s .= '<div id="pumpio-delete-wrapper">';
306 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . DI::l10n()->t('Check to delete this preset') . '</label>';
307 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
308 $s .= '</div><div class="clear"></div>';
311 $s .= '</div><div class="clear"></div>';
314 /* provide a submit button */
316 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
319 function pumpio_settings_post(App $a, array &$b)
321 if (!empty($_POST['pumpio-submit'])) {
322 if (!empty($_POST['pumpio_delete'])) {
323 DI::pConfig()->set(local_user(), 'pumpio', 'consumer_key' , '');
324 DI::pConfig()->set(local_user(), 'pumpio', 'consumer_secret' , '');
325 DI::pConfig()->set(local_user(), 'pumpio', 'oauth_token' , '');
326 DI::pConfig()->set(local_user(), 'pumpio', 'oauth_token_secret', '');
327 DI::pConfig()->set(local_user(), 'pumpio', 'post' , false);
328 DI::pConfig()->set(local_user(), 'pumpio', 'import' , false);
329 DI::pConfig()->set(local_user(), 'pumpio', 'host' , '');
330 DI::pConfig()->set(local_user(), 'pumpio', 'user' , '');
331 DI::pConfig()->set(local_user(), 'pumpio', 'public' , false);
332 DI::pConfig()->set(local_user(), 'pumpio', 'mirror' , false);
333 DI::pConfig()->set(local_user(), 'pumpio', 'post_by_default' , false);
334 DI::pConfig()->set(local_user(), 'pumpio', 'lastdate' , 0);
335 DI::pConfig()->set(local_user(), 'pumpio', 'last_id' , '');
337 // filtering the username if it is filled wrong
338 $user = $_POST['pumpio_user'];
339 if (strstr($user, "@")) {
340 $pos = strpos($user, "@");
343 $user = substr($user, 0, $pos);
347 // Filtering the hostname if someone is entering it with "http"
348 $host = $_POST['pumpio_host'];
350 $host = str_replace(["https://", "http://"], ["", ""], $host);
352 DI::pConfig()->set(local_user(), 'pumpio', 'post' , $_POST['pumpio'] ?? false);
353 DI::pConfig()->set(local_user(), 'pumpio', 'import' , $_POST['pumpio_import'] ?? false);
354 DI::pConfig()->set(local_user(), 'pumpio', 'host' , $host);
355 DI::pConfig()->set(local_user(), 'pumpio', 'user' , $user);
356 DI::pConfig()->set(local_user(), 'pumpio', 'public' , $_POST['pumpio_public'] ?? false);
357 DI::pConfig()->set(local_user(), 'pumpio', 'mirror' , $_POST['pumpio_mirror'] ?? false);
358 DI::pConfig()->set(local_user(), 'pumpio', 'post_by_default', $_POST['pumpio_bydefault'] ?? false);
360 if (!empty($_POST['pumpio_mirror'])) {
361 DI::pConfig()->delete(local_user(), 'pumpio', 'lastdate');
367 function pumpio_load_config(App $a, ConfigFileLoader $loader)
369 $a->getConfigCache()->load($loader->loadAddonConfig('pumpio'));
372 function pumpio_hook_fork(App $a, array &$b)
374 if ($b['name'] != 'notifier_normal') {
380 // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
381 if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
382 $b['execute'] = false;
386 // if post comes from pump.io don't send it back
387 if ($post['app'] == "pump.io") {
388 $b['execute'] = false;
392 if (DI::pConfig()->get($post['uid'], 'pumpio', 'import')) {
393 // Don't fork if it isn't a reply to a pump.io post
394 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::PUMPIO])) {
395 Logger::notice('No pump.io parent found for item ' . $post['id']);
396 $b['execute'] = false;
400 // Comments are never exported when we don't import the pumpio timeline
401 if (!strstr($post['postopts'], 'pumpio') || ($post['parent'] != $post['id']) || $post['private']) {
402 $b['execute'] = false;
408 function pumpio_post_local(App $a, array &$b)
410 if (!local_user() || (local_user() != $b['uid'])) {
414 $pumpio_post = intval(DI::pConfig()->get(local_user(), 'pumpio', 'post'));
416 $pumpio_enable = (($pumpio_post && !empty($_REQUEST['pumpio_enable'])) ? intval($_REQUEST['pumpio_enable']) : 0);
418 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'pumpio', 'post_by_default'))) {
422 if (!$pumpio_enable) {
426 if (strlen($b['postopts'])) {
427 $b['postopts'] .= ',';
430 $b['postopts'] .= 'pumpio';
433 function pumpio_send(App $a, array &$b)
435 if (!DI::pConfig()->get($b["uid"], 'pumpio', 'import') && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
439 Logger::debug("pumpio_send: parameter ".print_r($b, true));
441 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
443 if ($b['parent'] != $b['id']) {
444 // Looking if its a reply to a pumpio post
445 $condition = ['id' => $b['parent'], 'network' => Protocol::PUMPIO];
446 $orig_post = Post::selectFirst([], $condition);
448 if (!DBA::isResult($orig_post)) {
449 Logger::notice("pumpio_send: no pumpio post ".$b["parent"]);
457 $receiver = pumpio_getreceiver($a, $b);
459 Logger::notice("pumpio_send: receiver ".print_r($receiver, true));
461 if (!count($receiver) && ($b['private'] || !strstr($b['postopts'], 'pumpio'))) {
465 // Dont't post if the post doesn't belong to us.
466 // This is a check for forum postings
467 $self = User::getOwnerDataById($b['uid']);
468 if ($b['contact-id'] != $self['id']) {
473 if ($b['verb'] == Activity::LIKE) {
475 pumpio_action($a, $b["uid"], $b["thr-parent"], "unlike");
477 pumpio_action($a, $b["uid"], $b["thr-parent"], "like");
482 if ($b['verb'] == Activity::DISLIKE) {
486 if (($b['verb'] == Activity::POST) && ($b['created'] !== $b['edited']) && !$b['deleted']) {
487 pumpio_action($a, $b["uid"], $b["uri"], "update", $b["body"]);
490 if (($b['verb'] == Activity::POST) && $b['deleted']) {
491 pumpio_action($a, $b["uid"], $b["uri"], "delete");
494 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
498 // if post comes from pump.io don't send it back
499 if ($b['app'] == "pump.io") {
504 // Support for native shares
505 // http://<hostname>/api/<type>/shares?id=<the-object-id>
507 $oauth_token = DI::pConfig()->get($b['uid'], "pumpio", "oauth_token");
508 $oauth_token_secret = DI::pConfig()->get($b['uid'], "pumpio", "oauth_token_secret");
509 $consumer_key = DI::pConfig()->get($b['uid'], "pumpio","consumer_key");
510 $consumer_secret = DI::pConfig()->get($b['uid'], "pumpio","consumer_secret");
512 $host = DI::pConfig()->get($b['uid'], "pumpio", "host");
513 $user = DI::pConfig()->get($b['uid'], "pumpio", "user");
514 $public = DI::pConfig()->get($b['uid'], "pumpio", "public");
516 if ($oauth_token && $oauth_token_secret) {
517 $title = trim($b['title']);
519 $content = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
523 $params["verb"] = "post";
526 $params["object"] = [
527 'objectType' => "note",
528 'content' => $content];
530 if (!empty($title)) {
531 $params["object"]["displayName"] = $title;
534 if (!empty($receiver["to"])) {
535 $params["to"] = $receiver["to"];
538 if (!empty($receiver["bto"])) {
539 $params["bto"] = $receiver["bto"];
542 if (!empty($receiver["cc"])) {
543 $params["cc"] = $receiver["cc"];
546 if (!empty($receiver["bcc"])) {
547 $params["bcc"] = $receiver["bcc"];
550 $inReplyTo = ["id" => $orig_post["uri"],
551 "objectType" => "note"];
553 if (($orig_post["object-type"] != "") && (strstr($orig_post["object-type"], ActivityNamespace::ACTIVITY_SCHEMA))) {
554 $inReplyTo["objectType"] = str_replace(ActivityNamespace::ACTIVITY_SCHEMA, '', $orig_post["object-type"]);
557 $params["object"] = [
558 'objectType' => "comment",
559 'content' => $content,
560 'inReplyTo' => $inReplyTo];
563 $params["object"]["displayName"] = $title;
567 $client = new oauth_client_class;
568 $client->oauth_version = '1.0a';
569 $client->url_parameters = false;
570 $client->authorization_header = true;
571 $client->access_token = $oauth_token;
572 $client->access_token_secret = $oauth_token_secret;
573 $client->client_id = $consumer_key;
574 $client->client_secret = $consumer_secret;
576 $username = $user.'@'.$host;
577 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
579 if (pumpio_reachable($url)) {
580 $success = $client->CallAPI($url, 'POST', $params, ['FailOnAccessError'=>true, 'RequestContentType'=>'application/json'], $user);
586 if ($user->generator->displayName) {
587 DI::pConfig()->set($b["uid"], "pumpio", "application_name", $user->generator->displayName);
590 $post_id = $user->object->id;
591 Logger::notice('pumpio_send '.$username.': success '.$post_id);
592 if ($post_id && $iscomment) {
593 Logger::notice('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
594 Item::update(['extid' => $post_id], ['id' => $b['id']]);
597 Logger::notice('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user, true));
603 function pumpio_action(App $a, $uid, $uri, $action, $content = "")
605 // Don't do likes and other stuff if you don't import the timeline
606 if (!DI::pConfig()->get($uid, 'pumpio', 'import')) {
610 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
611 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
612 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
613 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
614 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
615 $username = DI::pConfig()->get($uid, "pumpio", "user");
617 $orig_post = Post::selectFirst([], ['uri' => $uri, 'uid' => $uid]);
619 if (!DBA::isResult($orig_post)) {
623 if ($orig_post["extid"] && !strstr($orig_post["extid"], "/proxy/")) {
624 $uri = $orig_post["extid"];
626 $uri = $orig_post["uri"];
629 if (($orig_post["object-type"] != "") && (strstr($orig_post["object-type"], ActivityNamespace::ACTIVITY_SCHEMA))) {
630 $objectType = str_replace(ActivityNamespace::ACTIVITY_SCHEMA, '', $orig_post["object-type"]);
631 } elseif (strstr($uri, "/api/comment/")) {
632 $objectType = "comment";
633 } elseif (strstr($uri, "/api/note/")) {
634 $objectType = "note";
635 } elseif (strstr($uri, "/api/image/")) {
636 $objectType = "image";
639 $params["verb"] = $action;
640 $params["object"] = ['id' => $uri,
641 "objectType" => $objectType,
642 "content" => $content];
644 $client = new oauth_client_class;
645 $client->oauth_version = '1.0a';
646 $client->authorization_header = true;
647 $client->url_parameters = false;
649 $client->client_id = $ckey;
650 $client->client_secret = $csecret;
651 $client->access_token = $otoken;
652 $client->access_token_secret = $osecret;
654 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
656 if (pumpio_reachable($url)) {
657 $success = $client->CallAPI($url, 'POST', $params, ['FailOnAccessError'=>true, 'RequestContentType'=>'application/json'], $user);
663 Logger::notice('pumpio_action '.$username.' '.$action.': success '.$uri);
665 Logger::notice('pumpio_action '.$username.' '.$action.': general error: '.$uri);
670 function pumpio_sync(App $a)
672 if (!Addon::isEnabled('pumpio')) {
676 $last = DI::config()->get('pumpio', 'last_poll');
678 $poll_interval = intval(DI::config()->get('pumpio', 'poll_interval', PUMPIO_DEFAULT_POLL_INTERVAL));
681 $next = $last + ($poll_interval * 60);
682 if ($next > time()) {
683 Logger::notice('pumpio: poll intervall not reached');
687 Logger::notice('pumpio: cron_start');
689 $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'pumpio', 'k' => 'mirror', 'v' => '1']);
690 foreach ($pconfigs as $rr) {
691 Logger::notice('pumpio: mirroring user '.$rr['uid']);
692 pumpio_fetchtimeline($a, $rr['uid']);
695 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
696 if ($abandon_days < 1) {
700 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
702 $pconfigs = DBA::selectToArray('pconfig', ['uid'], ['cat' => 'pumpio', 'k' => 'import', 'v' => '1']);
703 foreach ($pconfigs as $rr) {
704 if ($abandon_days != 0) {
705 if (DBA::exists('user', ["uid = ? AND `login_date` >= ?", $rr['uid'], $abandon_limit])) {
706 Logger::notice('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
711 Logger::notice('pumpio: importing timeline from user '.$rr['uid']);
712 pumpio_fetchinbox($a, $rr['uid']);
714 // check for new contacts once a day
715 $last_contact_check = DI::pConfig()->get($rr['uid'], 'pumpio', 'contact_check');
716 if ($last_contact_check) {
717 $next_contact_check = $last_contact_check + 86400;
719 $next_contact_check = 0;
722 if ($next_contact_check <= time()) {
723 pumpio_getallusers($a, $rr["uid"]);
724 DI::pConfig()->set($rr['uid'], 'pumpio', 'contact_check', time());
728 Logger::notice('pumpio: cron_end');
730 DI::config()->set('pumpio', 'last_poll', time());
733 function pumpio_cron(App $a, $b)
735 Worker::add(PRIORITY_MEDIUM,"addon/pumpio/pumpio_sync.php");
738 function pumpio_fetchtimeline(App $a, $uid)
740 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
741 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
742 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
743 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
744 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
745 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
746 $username = DI::pConfig()->get($uid, "pumpio", "user");
748 // get the application name for the pump.io app
749 // 1st try personal config, then system config and fallback to the
750 // hostname of the node if neither one is set.
751 $application_name = DI::pConfig()->get($uid, 'pumpio', 'application_name');
752 if ($application_name == "") {
753 $application_name = DI::config()->get('pumpio', 'application_name');
755 if ($application_name == "") {
756 $application_name = DI::baseUrl()->getHostname();
759 $first_time = ($lastdate == "");
761 $client = new oauth_client_class;
762 $client->oauth_version = '1.0a';
763 $client->authorization_header = true;
764 $client->url_parameters = false;
766 $client->client_id = $ckey;
767 $client->client_secret = $csecret;
768 $client->access_token = $otoken;
769 $client->access_token_secret = $osecret;
771 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
773 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);
775 $useraddr = $username.'@'.$hostname;
777 if (pumpio_reachable($url)) {
778 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $user);
785 Logger::notice('pumpio: error fetching posts for user '.$uid." ".$useraddr." ".print_r($user, true));
789 $posts = array_reverse($user->items);
791 $initiallastdate = $lastdate;
795 foreach ($posts as $post) {
796 if ($post->published <= $initiallastdate) {
800 if ($lastdate < $post->published) {
801 $lastdate = $post->published;
809 if (@is_array($post->cc)) {
810 $receiptians = array_merge($receiptians, $post->cc);
813 if (@is_array($post->to)) {
814 $receiptians = array_merge($receiptians, $post->to);
818 foreach ($receiptians AS $receiver) {
819 if (is_string($receiver->objectType) && ($receiver->id == "http://activityschema.org/collection/public")) {
824 if ($public && !stristr($post->generator->displayName, $application_name)) {
825 $_SESSION["authenticated"] = true;
826 $_SESSION["uid"] = $uid;
829 $_REQUEST["api_source"] = true;
830 $_REQUEST["profile_uid"] = $uid;
831 $_REQUEST["source"] = "pump.io";
833 if (isset($post->object->id)) {
834 $_REQUEST['message_id'] = Protocol::PUMPIO.":".$post->object->id;
837 if ($post->object->displayName != "") {
838 $_REQUEST["title"] = HTML::toBBCode($post->object->displayName);
840 $_REQUEST["title"] = "";
843 $_REQUEST["body"] = HTML::toBBCode($post->object->content);
845 // To-Do: Picture has to be cached and stored locally
846 if ($post->object->fullImage->url != "") {
847 if ($post->object->fullImage->pump_io->proxyURL != "") {
848 $_REQUEST["body"] = "[url=".$post->object->fullImage->pump_io->proxyURL."][img]".$post->object->image->pump_io->proxyURL."[/img][/url]\n".$_REQUEST["body"];
850 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
854 Logger::notice('pumpio: posting for user '.$uid);
856 require_once('mod/item.php');
859 Logger::notice('pumpio: posting done - user '.$uid);
864 if ($lastdate != 0) {
865 DI::pConfig()->set($uid, 'pumpio', 'lastdate', $lastdate);
869 function pumpio_dounlike(App $a, $uid, $self, $post, $own_id)
871 // Searching for the unliked post
872 // Two queries for speed issues
873 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
874 if (!DBA::isResult($orig_post)) {
875 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
876 if (!DBA::isResult($orig_post)) {
883 if (Strings::compareLink($post->actor->url, $own_id)) {
884 $contactid = $self['id'];
886 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
887 if (DBA::isResult($contact)) {
888 $contactid = $contact['id'];
891 if ($contactid == 0) {
892 $contactid = $orig_post['contact-id'];
896 Item::markForDeletion(['verb' => Activity::LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']]);
898 if (DBA::isResult($contact)) {
899 Logger::notice("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
901 Logger::notice("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
905 function pumpio_dolike(App $a, $uid, $self, $post, $own_id, $threadcompletion = true)
907 if (empty($post->object->id)) {
908 Logger::info('Got empty like: '.print_r($post, true));
912 // Searching for the liked post
913 // Two queries for speed issues
914 $orig_post = Post::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
915 if (!DBA::isResult($orig_post)) {
916 $orig_post = Post::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
917 if (!DBA::isResult($orig_post)) {
923 if ($threadcompletion) {
924 pumpio_fetchallcomments($a, $uid, $post->object->id);
929 if (Strings::compareLink($post->actor->url, $own_id)) {
930 $contactid = $self['id'];
931 $post->actor->displayName = $self['name'];
932 $post->actor->url = $self['url'];
933 $post->actor->image->url = $self['photo'];
935 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
936 if (DBA::isResult($contact)) {
937 $contactid = $contact['id'];
940 if ($contactid == 0) {
941 $contactid = $orig_post['contact-id'];
945 $condition = ['verb' => Activity::LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']];
946 if (Post::exists($condition)) {
947 Logger::notice("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
952 $likedata['parent'] = $orig_post['id'];
953 $likedata['verb'] = Activity::LIKE;
954 $likedata['gravity'] = GRAVITY_ACTIVITY;
955 $likedata['uid'] = $uid;
956 $likedata['wall'] = 0;
957 $likedata['network'] = Protocol::PUMPIO;
958 $likedata['uri'] = Item::newURI($uid);
959 $likedata['thr-parent'] = $orig_post['uri'];
960 $likedata['contact-id'] = $contactid;
961 $likedata['app'] = $post->generator->displayName;
962 $likedata['author-name'] = $post->actor->displayName;
963 $likedata['author-link'] = $post->actor->url;
964 if (!empty($post->actor->image)) {
965 $likedata['author-avatar'] = $post->actor->image->url;
968 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
969 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
970 $post_type = DI::l10n()->t('status');
971 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
972 $likedata['object-type'] = Activity\ObjectType::NOTE;
974 $likedata['body'] = DI::l10n()->t('%1$s likes %2$s\'s %3$s', $author, $objauthor, $plink);
976 $likedata['object'] = '<object><type>' . Activity\ObjectType::NOTE . '</type><local>1</local>' .
977 '<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>';
979 $ret = Item::insert($likedata);
981 Logger::notice("pumpio_dolike: ".$ret." User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
984 function pumpio_get_contact($uid, $contact, $no_insert = false)
986 $cid = Contact::getIdForURL($contact->url, $uid);
992 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
993 if (!DBA::isResult($r)) {
994 // create contact record
997 'created' => DateTimeFormat::utcNow(),
998 'url' => $contact->url,
999 'nurl' => Strings::normaliseLink($contact->url),
1000 'addr' => str_replace("acct:", "", $contact->id),
1002 'notify' => $contact->id,
1003 'poll' => 'pump.io ' . $contact->id,
1004 'name' => $contact->displayName,
1005 'nick' => $contact->preferredUsername,
1006 'photo' => $contact->image->url,
1007 'network' => Protocol::PUMPIO,
1008 'rel' => Contact::FRIEND,
1010 'location' => $contact->location->displayName,
1011 'about' => $contact->summary,
1018 $r = Contact::selectFirst([], ['uid' => $uid, 'nurl' => Strings::normaliseLink($contact->url)]);
1019 if (!DBA::isResult($r)) {
1023 $contact_id = $r['id'];
1025 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1027 $contact_id = $r["id"];
1030 if (!empty($contact->image->url)) {
1031 Contact::updateAvatar($contact_id, $contact->image->url);
1037 function pumpio_dodelete(App $a, $uid, $self, $post, $own_id)
1039 // Two queries for speed issues
1040 $condition = ['uri' => $post->object->id, 'uid' => $uid];
1041 if (Post::exists($condition)) {
1042 Item::markForDeletion($condition);
1046 $condition = ['extid' => $post->object->id, 'uid' => $uid];
1047 if (Post::exists($condition)) {
1048 Item::markForDeletion($condition);
1054 function pumpio_dopost(App $a, $client, $uid, $self, $post, $own_id, $threadcompletion = true)
1056 if (($post->verb == "like") || ($post->verb == "favorite")) {
1057 return pumpio_dolike($a, $uid, $self, $post, $own_id);
1060 if (($post->verb == "unlike") || ($post->verb == "unfavorite")) {
1061 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1064 if ($post->verb == "delete") {
1065 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1068 if ($post->verb != "update") {
1069 // Two queries for speed issues
1070 if (Post::exists(['uri' => $post->object->id, 'uid' => $uid])) {
1073 if (Post::exists(['extid' => $post->object->id, 'uid' => $uid])) {
1078 // Only handle these three types
1079 if (!strstr("post|share|update", $post->verb)) {
1084 if (@is_array($post->cc)) {
1085 $receiptians = array_merge($receiptians, $post->cc);
1088 if (@is_array($post->to)) {
1089 $receiptians = array_merge($receiptians, $post->to);
1094 foreach ($receiptians AS $receiver) {
1095 if (is_string($receiver->objectType) && ($receiver->id == "http://activityschema.org/collection/public")) {
1101 $postarray['network'] = Protocol::PUMPIO;
1102 $postarray['uid'] = $uid;
1103 $postarray['wall'] = 0;
1104 $postarray['uri'] = $post->object->id;
1105 $postarray['object-type'] = ActivityNamespace::ACTIVITY_SCHEMA . strtolower($post->object->objectType);
1107 if ($post->object->objectType != "comment") {
1108 $contact_id = pumpio_get_contact($uid, $post->actor);
1111 $contact_id = $self['id'];
1114 $postarray['thr-parent'] = $post->object->id;
1117 $postarray['private'] = 1;
1118 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1121 $contact_id = pumpio_get_contact($uid, $post->actor, true);
1123 if (Strings::compareLink($post->actor->url, $own_id)) {
1124 $contact_id = $self['id'];
1125 $post->actor->displayName = $self['name'];
1126 $post->actor->url = $self['url'];
1127 $post->actor->image->url = $self['photo'];
1128 } elseif ($contact_id == 0) {
1129 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1130 $contact = Contact::selectFirst([], ['nurl' => Strings::normaliseLink($post->actor->url), 'uid' => $uid, 'blocked' => false, 'readonly' => false]);
1131 if (DBA::isResult($contact)) {
1132 $contact_id = $contact['id'];
1134 $contact_id = $self['id'];
1138 $reply = new stdClass;
1139 $reply->verb = "note";
1141 if (isset($post->cc)) {
1142 $reply->cc = $post->cc;
1145 if (isset($post->to)) {
1146 $reply->to = $post->to;
1149 $reply->object = new stdClass;
1150 $reply->object->objectType = $post->object->inReplyTo->objectType;
1151 $reply->object->content = $post->object->inReplyTo->content;
1152 $reply->object->id = $post->object->inReplyTo->id;
1153 $reply->actor = $post->object->inReplyTo->author;
1154 $reply->url = $post->object->inReplyTo->url;
1155 $reply->generator = new stdClass;
1156 $reply->generator->displayName = "pumpio";
1157 $reply->published = $post->object->inReplyTo->published;
1158 $reply->received = $post->object->inReplyTo->updated;
1159 $reply->url = $post->object->inReplyTo->url;
1160 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id, false);
1162 $postarray['thr-parent'] = $post->object->inReplyTo->id;
1165 // When there is no content there is no need to continue
1166 if (empty($post->object->content)) {
1170 if (!empty($post->object->pump_io->proxyURL)) {
1171 $postarray['extid'] = $post->object->pump_io->proxyURL;
1174 $postarray['contact-id'] = $contact_id;
1175 $postarray['verb'] = Activity::POST;
1176 $postarray['owner-name'] = $post->actor->displayName;
1177 $postarray['owner-link'] = $post->actor->url;
1178 $postarray['author-name'] = $postarray['owner-name'];
1179 $postarray['author-link'] = $postarray['owner-link'];
1180 if (!empty($post->actor->image)) {
1181 $postarray['owner-avatar'] = $post->actor->image->url;
1182 $postarray['author-avatar'] = $postarray['owner-avatar'];
1184 $postarray['plink'] = $post->object->url;
1185 $postarray['app'] = $post->generator->displayName;
1186 $postarray['title'] = '';
1187 $postarray['body'] = HTML::toBBCode($post->object->content);
1188 $postarray['object'] = json_encode($post);
1190 if (!empty($post->object->fullImage->url)) {
1191 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
1194 if (!empty($post->object->displayName)) {
1195 $postarray['title'] = $post->object->displayName;
1198 $postarray['created'] = DateTimeFormat::utc($post->published);
1199 if (isset($post->updated)) {
1200 $postarray['edited'] = DateTimeFormat::utc($post->updated);
1201 } elseif (isset($post->received)) {
1202 $postarray['edited'] = DateTimeFormat::utc($post->received);
1204 $postarray['edited'] = $postarray['created'];
1207 if ($post->verb == "share") {
1208 if (isset($post->object->author->displayName) && ($post->object->author->displayName != "")) {
1209 $share_author = $post->object->author->displayName;
1210 } elseif (isset($post->object->author->preferredUsername) && ($post->object->author->preferredUsername != "")) {
1211 $share_author = $post->object->author->preferredUsername;
1213 $share_author = $post->object->author->url;
1216 if (isset($post->object->created)) {
1217 $created = DateTimeFormat::utc($post->object->created);
1222 $postarray['body'] = Friendica\Content\Text\BBCode::getShareOpeningTag($share_author, $post->object->author->url,
1223 $post->object->author->image->url, $post->links->self->href, $created) .
1224 $postarray['body']."[/share]";
1227 if (trim($postarray['body']) == "") {
1231 $top_item = Item::insert($postarray);
1232 $postarray["id"] = $top_item;
1234 if (($top_item == 0) && ($post->verb == "update")) {
1235 $fields = ['title' => $postarray["title"], 'body' => $postarray["body"], 'changed' => $postarray["edited"]];
1236 $condition = ['uri' => $postarray["uri"], 'uid' => $uid];
1237 Item::update($fields, $condition);
1240 if (($post->object->objectType == "comment") && $threadcompletion) {
1241 pumpio_fetchallcomments($a, $uid, $postarray['thr-parent']);
1247 function pumpio_fetchinbox(App $a, $uid)
1249 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1250 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1251 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1252 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1253 $lastdate = DI::pConfig()->get($uid, 'pumpio', 'lastdate');
1254 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1255 $username = DI::pConfig()->get($uid, "pumpio", "user");
1257 $own_id = "https://".$hostname."/".$username;
1259 $self = User::getOwnerDataById($uid);
1261 $lastitems = DBA::p("SELECT `uri` FROM `post-thread-user`
1262 INNER JOIN `post-view` ON `post-view`.`id` = `post-thread-user`.`id`
1263 WHERE `post-thread-user`.`network` = ? AND `post-thread-user`.`uid` = ? AND `post-view`.`extid` != ''
1264 ORDER BY `post-thread-user`.`commented` DESC LIMIT 10", Protocol::PUMPIO, $uid);
1266 $client = new oauth_client_class;
1267 $client->oauth_version = '1.0a';
1268 $client->authorization_header = true;
1269 $client->url_parameters = false;
1271 $client->client_id = $ckey;
1272 $client->client_secret = $csecret;
1273 $client->access_token = $otoken;
1274 $client->access_token_secret = $osecret;
1276 $last_id = DI::pConfig()->get($uid, 'pumpio', 'last_id');
1278 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1280 if ($last_id != "") {
1281 $url .= '?since='.urlencode($last_id);
1284 if (pumpio_reachable($url)) {
1285 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $user);
1294 if (!empty($user->items)) {
1295 $posts = array_reverse($user->items);
1297 if (count($posts)) {
1298 foreach ($posts as $post) {
1299 $last_id = $post->id;
1300 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, true);
1305 while ($item = DBA::fetch($lastitems)) {
1306 pumpio_fetchallcomments($a, $uid, $item["uri"]);
1308 DBA::close($lastitems);
1310 DI::pConfig()->set($uid, 'pumpio', 'last_id', $last_id);
1313 function pumpio_getallusers(App &$a, $uid)
1315 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1316 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1317 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1318 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1319 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1320 $username = DI::pConfig()->get($uid, "pumpio", "user");
1322 $client = new oauth_client_class;
1323 $client->oauth_version = '1.0a';
1324 $client->authorization_header = true;
1325 $client->url_parameters = false;
1327 $client->client_id = $ckey;
1328 $client->client_secret = $csecret;
1329 $client->access_token = $otoken;
1330 $client->access_token_secret = $osecret;
1332 $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1334 if (pumpio_reachable($url)) {
1335 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1340 if (empty($users)) {
1344 if ($users->totalItems > count($users->items)) {
1345 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1347 if (pumpio_reachable($url)) {
1348 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
1354 if (!empty($users->items)) {
1355 foreach ($users->items as $user) {
1356 pumpio_get_contact($uid, $user);
1361 function pumpio_getreceiver(App $a, array $b)
1365 if (!$b["private"]) {
1366 if (!strstr($b['postopts'], 'pumpio')) {
1370 $public = DI::pConfig()->get($b['uid'], "pumpio", "public");
1373 $receiver["to"][] = [
1374 "objectType" => "collection",
1375 "id" => "http://activityschema.org/collection/public"];
1378 $cids = explode("><", $b["allow_cid"]);
1379 $gids = explode("><", $b["allow_gid"]);
1381 foreach ($cids AS $cid) {
1382 $cid = trim($cid, " <>");
1384 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b["uid"], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1385 if (DBA::isResult($contact)) {
1386 $receiver["bcc"][] = [
1387 "displayName" => $contact["name"],
1388 "objectType" => "person",
1389 "preferredUsername" => $contact["nick"],
1390 "url" => $contact["url"]];
1393 foreach ($gids AS $gid) {
1394 $gid = trim($gid, " <>");
1396 $contacts = DBA::p("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network`
1397 FROM `group_member`, `contact` WHERE `group_member`.`gid` = ?
1398 AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = ?",
1399 $gid, Protocol::PUMPIO);
1401 while ($row = DBA::fetch($contacts)) {
1402 $receiver["bcc"][] = [
1403 "displayName" => $row["name"],
1404 "objectType" => "person",
1405 "preferredUsername" => $row["nick"],
1406 "url" => $row["url"]];
1408 DBA::close($contacts);
1412 if ($b["inform"] != "") {
1413 $inform = explode(",", $b["inform"]);
1415 foreach ($inform AS $cid) {
1416 if (substr($cid, 0, 4) != "cid:") {
1420 $cid = str_replace("cid:", "", $cid);
1422 $contact = Contact::selectFirst(['name', 'nick', 'url'], ['id' => $cid, 'uid' => $b["uid"], 'network' => Protocol::PUMPIO, 'blocked' => false, 'readonly' => false]);
1423 if (DBA::isResult($contact)) {
1424 $receiver["to"][] = [
1425 "displayName" => $contact["name"],
1426 "objectType" => "person",
1427 "preferredUsername" => $contact["nick"],
1428 "url" => $contact["url"]];
1436 function pumpio_fetchallcomments(App $a, $uid, $id)
1438 $ckey = DI::pConfig()->get($uid, 'pumpio', 'consumer_key');
1439 $csecret = DI::pConfig()->get($uid, 'pumpio', 'consumer_secret');
1440 $otoken = DI::pConfig()->get($uid, 'pumpio', 'oauth_token');
1441 $osecret = DI::pConfig()->get($uid, 'pumpio', 'oauth_token_secret');
1442 $hostname = DI::pConfig()->get($uid, 'pumpio', 'host');
1443 $username = DI::pConfig()->get($uid, "pumpio", "user");
1445 Logger::notice("pumpio_fetchallcomments: completing comment for user ".$uid." post id ".$id);
1447 $own_id = "https://".$hostname."/".$username;
1449 $self = User::getOwnerDataById($uid);
1451 // Fetching the original post
1452 $condition = ["`uri` = ? AND `uid` = ? AND `extid` != ''", $id, $uid];
1453 $original = Post::selectFirst(['extid'], $condition);
1454 if (!DBA::isResult($original)) {
1458 $url = $original["extid"];
1460 $client = new oauth_client_class;
1461 $client->oauth_version = '1.0a';
1462 $client->authorization_header = true;
1463 $client->url_parameters = false;
1465 $client->client_id = $ckey;
1466 $client->client_secret = $csecret;
1467 $client->access_token = $otoken;
1468 $client->access_token_secret = $osecret;
1470 Logger::notice("pumpio_fetchallcomments: fetching comment for user ".$uid." url ".$url);
1472 if (pumpio_reachable($url)) {
1473 $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $item);
1482 if ($item->likes->totalItems != 0) {
1483 foreach ($item->likes->items AS $post) {
1484 $like = new stdClass;
1485 $like->object = new stdClass;
1486 $like->object->id = $item->id;
1487 $like->actor = new stdClass;
1488 if (!empty($item->displayName)) {
1489 $like->actor->displayName = $item->displayName;
1491 //$like->actor->preferredUsername = $item->preferredUsername;
1492 //$like->actor->image = $item->image;
1493 $like->actor->url = $item->url;
1494 $like->generator = new stdClass;
1495 $like->generator->displayName = "pumpio";
1496 pumpio_dolike($a, $uid, $self, $post, $own_id, false);
1500 if ($item->replies->totalItems == 0) {
1504 foreach ($item->replies->items AS $item) {
1505 if ($item->id == $id) {
1509 // Checking if the comment already exists - Two queries for speed issues
1510 if (Post::exists(['uri' => $item->id, 'uid' => $uid])) {
1514 if (Post::exists(['extid' => $item->id, 'uid' => $uid])) {
1518 $post = new stdClass;
1519 $post->verb = "post";
1520 $post->actor = $item->author;
1521 $post->published = $item->published;
1522 $post->received = $item->updated;
1523 $post->generator = new stdClass;
1524 $post->generator->displayName = "pumpio";
1525 // To-Do: Check for public post
1527 unset($item->author);
1528 unset($item->published);
1529 unset($item->updated);
1531 $post->object = $item;
1533 Logger::notice("pumpio_fetchallcomments: posting comment ".$post->object->id." ".print_r($post, true));
1534 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1538 function pumpio_reachable($url)
1540 return DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => 10])->isSuccess();