3 * Name: pump.io Post Connector
4 * Description: Post to pump.io
6 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
8 require('addon/pumpio/oauth/http.php');
9 require('addon/pumpio/oauth/oauth_client.php');
11 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
13 function pumpio_install() {
14 register_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
15 register_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
16 register_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
17 register_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
18 register_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
19 register_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
22 function pumpio_uninstall() {
23 unregister_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
24 unregister_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
25 unregister_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
26 unregister_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
27 unregister_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
28 unregister_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
31 function pumpio_module() {}
33 function pumpio_content(&$a) {
36 notice( t('Permission denied.') . EOL);
40 if (isset($a->argv[1]))
41 switch ($a->argv[1]) {
43 $o = pumpio_connect($a);
46 $o = print_r($a->argv, true);
50 $o = pumpio_connect($a);
55 function pumpio_registerclient($a, $host) {
57 $url = "https://".$host."/api/client/register";
61 $application_name = get_config('pumpio', 'application_name');
63 if ($application_name == "")
64 $application_name = $a->get_hostname();
66 $params["type"] = "client_associate";
67 $params["contacts"] = $a->config['admin_email'];
68 $params["application_type"] = "native";
69 $params["application_name"] = $application_name;
70 $params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
71 $params["redirect_uris"] = $a->get_baseurl()."/pumpio/connect";
73 $ch = curl_init($url);
74 curl_setopt($ch, CURLOPT_HEADER, false);
75 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
76 curl_setopt($ch, CURLOPT_POST,1);
77 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
78 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
81 $curl_info = curl_getinfo($ch);
83 if ($curl_info["http_code"] == "200") {
84 $values = json_decode($s);
90 function pumpio_connect($a) {
91 // Start a session. This is necessary to hold on to a few keys the callback script will also need
94 // Define the needed keys
95 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
96 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
97 $hostname = get_pconfig(local_user(), 'pumpio','host');
99 if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
100 $clientdata = pumpio_registerclient($a, $hostname);
101 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
102 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
104 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
105 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
108 if (($consumer_key == "") OR ($consumer_secret == ""))
111 // The callback URL is the script that gets called after the user authenticates with pumpio
112 $callback_url = $a->get_baseurl()."/pumpio/connect";
114 // Let's begin. First we need a Request Token. The request token is required to send the user
115 // to pumpio's login page.
117 // Create a new instance of the TumblrOAuth library. For this step, all we need to give the library is our
118 // Consumer Key and Consumer Secret
119 $client = new oauth_client_class;
121 $client->server = '';
122 $client->oauth_version = '1.0a';
123 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
124 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
125 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
126 $client->url_parameters = false;
127 $client->authorization_header = true;
128 $client->redirect_uri = $callback_url;
129 $client->client_id = $consumer_key;
130 $client->client_secret = $consumer_secret;
132 if (($success = $client->Initialize())) {
133 if (($success = $client->Process())) {
134 if (strlen($client->access_token)) {
135 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
136 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
139 $success = $client->Finalize($success);
142 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
145 $o .= t("You are now authenticated to pumpio.");
146 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
152 function pumpio_jot_nets(&$a,&$b) {
156 $pumpio_post = get_pconfig(local_user(),'pumpio','post');
157 if(intval($pumpio_post) == 1) {
158 $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
159 $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
160 $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
161 . t('Post to pumpio') . '</div>';
166 function pumpio_settings(&$a,&$s) {
171 /* Add our stylesheet to the page so we can make our settings look nice */
173 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
175 /* Get the current state of our config variables */
177 $import_enabled = get_pconfig(local_user(),'pumpio','import');
178 $import_checked = (($import_enabled) ? ' checked="checked" ' : '');
180 $enabled = get_pconfig(local_user(),'pumpio','post');
181 $checked = (($enabled) ? ' checked="checked" ' : '');
183 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
184 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
186 $public_enabled = get_pconfig(local_user(),'pumpio','public');
187 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
189 $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
190 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
192 $servername = get_pconfig(local_user(), "pumpio", "host");
193 $username = get_pconfig(local_user(), "pumpio", "user");
195 /* Add some HTML to the existing form */
197 $s .= '<div class="settings-block">';
198 $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
200 $s .= '<div id="pumpio-username-wrapper">';
201 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
202 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
203 $s .= '</div><div class="clear"></div>';
205 $s .= '<div id="pumpio-servername-wrapper">';
206 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
207 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
208 $s .= '</div><div class="clear"></div>';
210 if (($username != '') AND ($servername != '')) {
212 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
213 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
215 $s .= '<div id="pumpio-password-wrapper">';
216 if (($oauth_token == "") OR ($oauth_token_secret == "")) {
217 $s .= '<div id="pumpio-authenticate-wrapper">';
218 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Authenticate your pump.io connection").'</a>';
219 $s .= '</div><div class="clear"></div>';
221 //$s .= t("You are not authenticated to pumpio");
223 $s .= '<div id="pumpio-import-wrapper">';
224 $s .= '<label id="pumpio-import-label" for="pumpio-import">' . t('Import the remote timeline') . '</label>';
225 $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
226 $s .= '</div><div class="clear"></div>';
228 $s .= '<div id="pumpio-enable-wrapper">';
229 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
230 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
231 $s .= '</div><div class="clear"></div>';
233 $s .= '<div id="pumpio-bydefault-wrapper">';
234 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
235 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
236 $s .= '</div><div class="clear"></div>';
238 $s .= '<div id="pumpio-public-wrapper">';
239 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
240 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
241 $s .= '</div><div class="clear"></div>';
243 $s .= '<div id="pumpio-mirror-wrapper">';
244 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
245 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
246 $s .= '</div><div class="clear"></div>';
248 $s .= '<div id="pumpio-delete-wrapper">';
249 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
250 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
251 $s .= '</div><div class="clear"></div>';
253 //$s .= '<div id="pumpio-authenticate-wrapper">';
254 //$s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Reauthenticate your pump.io connection").'</a>';
255 //$s .= '</div><div class="clear"></div>';
259 $s .= '</div><div class="clear"></div>';
262 /* provide a submit button */
264 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
269 function pumpio_settings_post(&$a,&$b) {
271 if(x($_POST,'pumpio-submit')) {
272 if(x($_POST,'pumpio_delete')) {
273 set_pconfig(local_user(),'pumpio','consumer_key','');
274 set_pconfig(local_user(),'pumpio','consumer_secret','');
275 set_pconfig(local_user(),'pumpio','host','');
276 set_pconfig(local_user(),'pumpio','oauth_token','');
277 set_pconfig(local_user(),'pumpio','oauth_token_secret','');
278 set_pconfig(local_user(),'pumpio','post',false);
279 set_pconfig(local_user(),'pumpio','post_by_default',false);
280 set_pconfig(local_user(),'pumpio','user','');
282 // filtering the username if it is filled wrong
283 $user = $_POST['pumpio_user'];
284 if (strstr($user, "@")) {
285 $pos = strpos($user, "@");
287 $user = substr($user, 0, $pos);
290 // Filtering the hostname if someone is entering it with "http"
291 $host = $_POST['pumpio_host'];
293 $host = str_replace(array("https://", "http://"), array("", ""), $host);
295 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
296 set_pconfig(local_user(),'pumpio','import',$_POST['pumpio_import']);
297 set_pconfig(local_user(),'pumpio','host',$host);
298 set_pconfig(local_user(),'pumpio','user',$user);
299 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
300 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
301 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
306 function pumpio_post_local(&$a,&$b) {
308 // This can probably be changed to allow editing by pointing to a different API endpoint
313 if((! local_user()) || (local_user() != $b['uid']))
316 if($b['private'] || $b['parent'])
319 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
321 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
323 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
329 if(strlen($b['postopts']))
330 $b['postopts'] .= ',';
332 $b['postopts'] .= 'pumpio';
338 function pumpio_send(&$a,&$b) {
339 if($b['deleted'] || ($b['created'] !== $b['edited']))
342 if($b['parent'] != $b['id']) {
343 // Looking if its a reply to a pumpio post
344 $r = q("SELECT item.* FROM item, contact WHERE item.id = %d AND item.uid = %d AND contact.id = `contact-id` AND contact.network='%s'LIMIT 1",
345 intval($b["parent"]),
347 dbesc(NETWORK_PUMPIO));
358 if(! strstr($b['postopts'],'pumpio'))
365 // if post comes from pump.io don't send it back
366 if($b['app'] == "pump.io")
369 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
370 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
371 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
372 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
374 $host = get_pconfig($b['uid'], "pumpio", "host");
375 $user = get_pconfig($b['uid'], "pumpio", "user");
376 $public = get_pconfig($b['uid'], "pumpio", "public");
378 if($oauth_token && $oauth_token_secret) {
380 require_once('include/bbcode.php');
382 $title = trim($b['title']);
385 $title = "<h4>".$title."</h4>";
387 $content = bbcode($b['body'], false, false);
389 // Enhance the way, videos are displayed
390 $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
391 $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
392 $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
393 $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
395 $URLSearchString = "^\[\]";
396 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
400 $params["verb"] = "post";
403 $params["object"] = array(
404 'objectType' => "note",
405 'content' => $title.$content);
408 $params["to"] = array(Array(
409 "objectType" => "collection",
410 "id" => "http://activityschema.org/collection/public"));
412 $inReplyTo = array("id" => $orig_post["uri"],
413 "objectType" => "note");
415 $params["object"] = array(
416 'objectType' => "comment",
417 'content' => $title.$content,
418 'inReplyTo' => $inReplyTo);
421 $client = new oauth_client_class;
422 $client->oauth_version = '1.0a';
423 $client->url_parameters = false;
424 $client->authorization_header = true;
425 $client->access_token = $oauth_token;
426 $client->access_token_secret = $oauth_token_secret;
427 $client->client_id = $consumer_key;
428 $client->client_secret = $consumer_secret;
430 $username = $user.'@'.$host;
431 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
433 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
436 $post_id = $user->object->id;
437 logger('pumpio_send '.$username.': success '.$post_id);
438 if($post_id AND $iscomment) {
439 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
440 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
446 logger('pumpio_send '.$username.': general error: ' . print_r($user,true));
448 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
449 require_once('include/queue_fn.php');
450 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
451 notice(t('Pump.io post failed. Queued for retry.').EOL);
457 function pumpio_cron($a,$b) {
458 $last = get_config('pumpio','last_poll');
460 $poll_interval = intval(get_config('pumpio','poll_interval'));
462 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
465 $next = $last + ($poll_interval * 60);
467 logger('pumpio: poll intervall not reached');
471 logger('pumpio: cron_start');
473 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
476 logger('pumpio: mirroring user '.$rr['uid']);
477 pumpio_fetchtimeline($a, $rr['uid']);
481 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
484 logger('pumpio: importing timeline from user '.$rr['uid']);
485 pumpio_fetchinbox($a, $rr['uid']);
490 //pumpio_getallusers($a, $uid);
492 logger('pumpio: cron_end');
494 set_config('pumpio','last_poll', time());
497 function pumpio_fetchtimeline($a, $uid) {
498 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
499 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
500 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
501 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
502 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
503 $hostname = get_pconfig($uid, 'pumpio','host');
504 $username = get_pconfig($uid, "pumpio", "user");
506 $application_name = get_config('pumpio', 'application_name');
508 if ($application_name == "")
509 $application_name = $a->get_hostname();
511 $first_time = ($lastdate == "");
513 $client = new oauth_client_class;
514 $client->oauth_version = '1.0a';
515 $client->authorization_header = true;
516 $client->url_parameters = false;
518 $client->client_id = $ckey;
519 $client->client_secret = $csecret;
520 $client->access_token = $otoken;
521 $client->access_token_secret = $osecret;
523 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
525 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
527 $username = $user.'@'.$host;
529 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
532 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
536 $posts = array_reverse($user->items);
538 $initiallastdate = $lastdate;
542 foreach ($posts as $post) {
543 if ($post->generator->published <= $initiallastdate)
546 if ($lastdate < $post->generator->published)
547 $lastdate = $post->generator->published;
552 $receiptians = array();
553 if (@is_array($post->cc))
554 $receiptians = array_merge($receiptians, $post->cc);
556 if (@is_array($post->to))
557 $receiptians = array_merge($receiptians, $post->to);
560 foreach ($receiptians AS $receiver)
561 if (is_string($receiver->objectType))
562 if ($receiver->id == "http://activityschema.org/collection/public")
565 if ($public AND !strstr($post->generator->displayName, $application_name)) {
566 require_once('include/html2bbcode.php');
568 $_SESSION["authenticated"] = true;
569 $_SESSION["uid"] = $uid;
572 $_REQUEST["type"] = "wall";
573 $_REQUEST["api_source"] = true;
574 $_REQUEST["profile_uid"] = $uid;
575 $_REQUEST["source"] = "pump.io";
577 if ($post->object->displayName != "")
578 $_REQUEST["title"] = html2bbcode($post->object->displayName);
580 $_REQUEST["title"] = "";
582 $_REQUEST["body"] = html2bbcode($post->object->content);
584 if ($post->object->fullImage->url != "")
585 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
587 logger('pumpio: posting for user '.$uid);
589 require_once('mod/item.php');
590 //print_r($_REQUEST);
592 logger('pumpio: posting done - user '.$uid);
597 //$lastdate = '2013-05-16T20:22:12Z';
600 set_pconfig($uid,'pumpio','lastdate', $lastdate);
603 function pumpio_dolike(&$a, $uid, $self, $post) {
606 // If we posted the like locally, it will be found with our url, not the FB url.
608 $second_url = (($likes->id == $fb_id) ? $self[0]['url'] : 'http://facebook.com/profile.php?id=' . $likes->id);
610 $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `verb` = '%s'
611 AND ( `author-link` = '%s' OR `author-link` = '%s' ) LIMIT 1",
612 dbesc($orig_post['uri']),
614 dbesc(ACTIVITY_LIKE),
615 dbesc('http://facebook.com/profile.php?id=' . $likes->id),
624 $own_id = "123455678";
626 // Two queries for speed issues
627 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
628 dbesc($post->object->id),
635 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
636 dbesc($post->object->id),
646 $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `author-link` = '%s' AND parent = %d LIMIT 1",
647 dbesc(ACTIVITY_LIKE),
649 dbesc($post->actor->url),
650 intval($orig_post['id'])
657 $likedata['parent'] = $orig_post['id'];
658 $likedata['verb'] = ACTIVITY_LIKE;
659 $likedata['gravity'] = 3;
660 $likedata['uid'] = $uid;
661 $likedata['wall'] = 0;
662 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
663 $likedata['parent-uri'] = $orig_post["uri"];
665 if($likes->id == $own_id)
666 $likedata['contact-id'] = $self[0]['id'];
668 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
669 dbesc($post->actor->url),
674 $likedata['contact-id'] = $r[0]['id'];
676 if(! x($likedata,'contact-id'))
677 $likedata['contact-id'] = $orig_post['contact-id'];
679 $likedata['app'] = $post->generator->displayName;
680 $likedata['verb'] = ACTIVITY_LIKE;
681 $likedata['author-name'] = $post->actor->displayName;
682 $likedata['author-link'] = $post->actor->url;
683 $likedata['author-avatar'] = $post->actor->image->url;
685 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
686 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
687 $post_type = t('status');
688 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
689 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
691 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
693 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
694 '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
696 item_store($likedata);
699 function pumpio_get_contact($uid, $contact) {
701 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
702 intval($uid), dbesc($contact->url));
705 // create contact record
706 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
707 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
708 `writable`, `blocked`, `readonly`, `pending` )
709 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
711 dbesc(datetime_convert()),
712 dbesc($contact->url),
713 dbesc(normalise_link($contact->url)),
714 dbesc(str_replace("acct:", "", $contact->id)),
716 dbesc($contact->id), // To-Do?
717 dbesc('pump.io ' . $contact->id), // To-Do?
718 dbesc($contact->displayName),
719 dbesc($contact->preferredUsername),
720 dbesc($contact->image->url),
721 dbesc(NETWORK_PUMPIO),
722 intval(CONTACT_IS_FRIEND),
727 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
728 dbesc($contact->url),
735 $contact_id = $r[0]['id'];
737 $g = q("select def_gid from user where uid = %d limit 1",
741 if($g && intval($g[0]['def_gid'])) {
742 require_once('include/group.php');
743 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
746 require_once("Photo.php");
748 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
750 q("UPDATE `contact` SET `photo` = '%s',
756 WHERE `id` = %d LIMIT 1
761 dbesc(datetime_convert()),
762 dbesc(datetime_convert()),
763 dbesc(datetime_convert()),
767 // update profile photos once every two weeks as we have no notification of when they change.
769 $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
771 // check that we have all the photos, this has been known to fail on occasion
773 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
774 require_once("Photo.php");
776 $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
778 q("UPDATE `contact` SET `photo` = '%s',
784 WHERE `id` = %d LIMIT 1
789 dbesc(datetime_convert()),
790 dbesc(datetime_convert()),
791 dbesc(datetime_convert()),
801 function pumpio_dodelete(&$a, $client, $uid, $self, $post) {
803 // Two queries for speed issues
804 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
805 dbesc($post->object->id),
810 drop_item($r[0]["id"], $false);
814 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
815 dbesc($post->object->id),
820 drop_item($r[0]["id"], $false);
825 function pumpio_dopost(&$a, $client, $uid, $self, $post) {
826 require_once('include/items.php');
828 if ($post->verb == "like") {
829 pumpio_dolike(&$a, $uid, $self, $post);
833 if ($post->verb == "delete") {
834 pumpio_dodelete(&$a, $uid, $self, $post);
838 // Only handle these three types
839 if (!strstr("post|share|update", $post->verb))
842 if ($post->verb != "update") {
843 // Two queries for speed issues
844 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
845 dbesc($post->object->id),
852 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
853 dbesc($post->object->id),
861 $receiptians = array();
862 if (@is_array($post->cc))
863 $receiptians = array_merge($receiptians, $post->cc);
865 if (@is_array($post->to))
866 $receiptians = array_merge($receiptians, $post->to);
868 foreach ($receiptians AS $receiver)
869 if (is_string($receiver->objectType))
870 if ($receiver->id == "http://activityschema.org/collection/public")
873 $contact_id = pumpio_get_contact($uid, $post->actor);
876 $contact_id = $self[0]['id'];
878 $postarray = array();
879 $postarray['gravity'] = 0;
880 $postarray['uid'] = $uid;
881 $postarray['wall'] = 0;
882 $postarray['uri'] = $post->object->id;
884 if ($post->object->objectType != "comment") {
885 $postarray['parent-uri'] = $post->object->id;
887 //echo($post->object->inReplyTo->url."\n");
888 //print_r($post->object->inReplyTo);
889 //echo $post->object->inReplyTo->likes->url."\n";
890 //$replies = $post->object->inReplyTo->replies->url;
891 //$replies = $post->object->likes->pump_io->proxyURL;
892 //$replies = $post->object->replies->pump_io->proxyURL;
895 //$replies = $post->object->replies->pump_io->proxyURL;
897 if ($replies != "") {
898 $success = $client->CallAPI($replies, 'GET', array(), array('FailOnAccessError'=>true), $replydata);
904 $reply->verb = "note";
905 $reply->cc = $post->cc;
906 $reply->to = $post->to;
907 $reply->object->objectType = $post->object->inReplyTo->objectType;
908 $reply->object->content = $post->object->inReplyTo->content;
909 $reply->object->id = $post->object->inReplyTo->id;
910 $reply->actor = $post->object->inReplyTo->author;
911 $reply->url = $post->object->inReplyTo->url;
912 $reply->generator->displayName = "pumpio";
913 $reply->published = $post->object->inReplyTo->published;
914 $reply->received = $post->object->inReplyTo->updated;
915 $reply->url = $post->object->inReplyTo->url;
916 pumpio_dopost(&$a, $client, $uid, $self, $reply);
918 $postarray['parent-uri'] = $post->object->inReplyTo->id;
921 $postarray['contact-id'] = $contact_id;
922 $postarray['verb'] = ACTIVITY_POST;
923 $postarray['owner-name'] = $post->actor->displayName;
924 $postarray['owner-link'] = $post->actor->url;
925 $postarray['owner-avatar'] = $post->actor->image->url;
926 $postarray['author-name'] = $post->actor->displayName;
927 $postarray['author-link'] = $post->actor->url;
928 $postarray['author-avatar'] = $post->actor->image->url;
929 $postarray['plink'] = $post->object->url;
930 $postarray['app'] = $post->generator->displayName;
931 $postarray['body'] = html2bbcode($post->object->content);
933 if ($post->object->fullImage->url != "")
934 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
936 if ($post->object->displayName != "")
937 $postarray['title'] = $post->object->displayName;
939 //$postarray['location'] = ""
940 $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
941 $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
943 $postarray['private'] = 1;
944 $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
947 if ($post->verb == "share") {
948 $postarray['body'] = "[share author='".$post->object->author->displayName.
949 "' profile='".$post->object->author->url.
950 "' avatar='".$post->object->author->image->url.
951 "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
954 if (trim($postarray['body']) == "")
957 $top_item = item_store($postarray);
959 if (($top_item == 0) AND ($post->verb == "update")) {
960 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
961 dbesc($postarray["title"]),
962 dbesc($postarray["body"]),
963 dbesc($postarray["edited"]),
964 dbesc($postarray["uri"]),
969 if ($post->object->objectType == "comment") {
971 $hostname = get_pconfig($uid, 'pumpio','host');
972 $username = get_pconfig($uid, "pumpio", "user");
974 $foreign_url = "https://".$hostname."/".$username;
976 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
983 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
985 if (link_compare($foreign_url, $postarray['author-link']))
988 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
989 dbesc($postarray['parent-uri']),
995 foreach($myconv as $conv) {
996 // now if we find a match, it means we're in this conversation
998 if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$foreign_url))
1001 require_once('include/enotify.php');
1003 $conv_parent = $conv['parent'];
1006 'type' => NOTIFY_COMMENT,
1007 'notify_flags' => $user[0]['notify-flags'],
1008 'language' => $user[0]['language'],
1009 'to_name' => $user[0]['username'],
1010 'to_email' => $user[0]['email'],
1011 'uid' => $user[0]['uid'],
1012 'item' => $cmntdata,
1013 'link' => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $top_item,
1014 'source_name' => $postarray['author-name'],
1015 'source_link' => $postarray['author-link'],
1016 'source_photo' => $postarray['author-avatar'],
1017 'verb' => ACTIVITY_POST,
1019 'parent' => $conv_parent,
1022 // only send one notification
1029 function pumpio_fetchinbox($a, $uid) {
1031 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1032 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1033 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1034 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1035 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1036 $hostname = get_pconfig($uid, 'pumpio','host');
1037 $username = get_pconfig($uid, "pumpio", "user");
1039 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1042 $client = new oauth_client_class;
1043 $client->oauth_version = '1.0a';
1044 $client->authorization_header = true;
1045 $client->url_parameters = false;
1047 $client->client_id = $ckey;
1048 $client->client_secret = $csecret;
1049 $client->access_token = $otoken;
1050 $client->access_token_secret = $osecret;
1052 $last_id = get_pconfig($uid,'pumpio','last_id');
1054 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1057 $url .= '?since='.urlencode($last_id);
1059 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1060 $posts = array_reverse($user->items);
1063 foreach ($posts as $post) {
1064 $last_id = $post->id;
1065 pumpio_dopost(&$a, $client, $uid, $self, $post);
1068 set_pconfig($uid,'pumpio','last_id', $last_id);
1071 function pumpio_getallusers($a, $uid) {
1072 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1073 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1074 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1075 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1076 $hostname = get_pconfig($uid, 'pumpio','host');
1077 $username = get_pconfig($uid, "pumpio", "user");
1079 $client = new oauth_client_class;
1080 $client->oauth_version = '1.0a';
1081 $client->authorization_header = true;
1082 $client->url_parameters = false;
1084 $client->client_id = $ckey;
1085 $client->client_secret = $csecret;
1086 $client->access_token = $otoken;
1087 $client->access_token_secret = $osecret;
1089 $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1091 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1093 if ($users->totalItems > count($users->items)) {
1094 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1096 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1099 foreach ($users->items AS $user)
1100 echo pumpio_get_contact($uid, $user)."\n";
1110 - eigene Inhalte editieren
1111 - eigene Inhalte löschen
1114 - vervollständigen der Threads
1115 - Aktualisieren nach Antworten