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);
87 $pumpio["client_id"] = $values->client_id;
88 $pumpio["client_secret"] = $values->client_secret;
94 function pumpio_connect($a) {
95 // Start a session. This is necessary to hold on to a few keys the callback script will also need
98 // Define the needed keys
99 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
100 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
101 $hostname = get_pconfig(local_user(), 'pumpio','host');
103 if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
104 $clientdata = pumpio_registerclient($a, $hostname);
105 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
106 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
108 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
109 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
112 if (($consumer_key == "") OR ($consumer_secret == ""))
115 // The callback URL is the script that gets called after the user authenticates with pumpio
116 $callback_url = $a->get_baseurl()."/pumpio/connect";
118 // Let's begin. First we need a Request Token. The request token is required to send the user
119 // to pumpio's login page.
121 // Create a new instance of the TumblrOAuth library. For this step, all we need to give the library is our
122 // Consumer Key and Consumer Secret
123 $client = new oauth_client_class;
125 $client->server = '';
126 $client->oauth_version = '1.0a';
127 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
128 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
129 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
130 $client->url_parameters = false;
131 $client->authorization_header = true;
132 $client->redirect_uri = $callback_url;
133 $client->client_id = $consumer_key;
134 $client->client_secret = $consumer_secret;
136 if (($success = $client->Initialize())) {
137 if (($success = $client->Process())) {
138 if (strlen($client->access_token)) {
139 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
140 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
143 $success = $client->Finalize($success);
146 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
149 $o .= t("You are now authenticated to pumpio.");
150 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
156 function pumpio_jot_nets(&$a,&$b) {
160 $pumpio_post = get_pconfig(local_user(),'pumpio','post');
161 if(intval($pumpio_post) == 1) {
162 $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
163 $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
164 $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
165 . t('Post to pumpio') . '</div>';
170 function pumpio_settings(&$a,&$s) {
175 /* Add our stylesheet to the page so we can make our settings look nice */
177 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
179 /* Get the current state of our config variables */
181 $enabled = get_pconfig(local_user(),'pumpio','post');
182 $checked = (($enabled) ? ' checked="checked" ' : '');
184 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
185 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
187 $public_enabled = get_pconfig(local_user(),'pumpio','public');
188 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
190 $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
191 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
193 $servername = get_pconfig(local_user(), "pumpio", "host");
194 $username = get_pconfig(local_user(), "pumpio", "user");
196 /* Add some HTML to the existing form */
198 $s .= '<div class="settings-block">';
199 $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
201 $s .= '<div id="pumpio-username-wrapper">';
202 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
203 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
204 $s .= '</div><div class="clear"></div>';
206 $s .= '<div id="pumpio-servername-wrapper">';
207 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
208 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
209 $s .= '</div><div class="clear"></div>';
211 if (($username != '') AND ($servername != '')) {
213 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
214 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
216 $s .= '<div id="pumpio-password-wrapper">';
217 if (($oauth_token == "") OR ($oauth_token_secret == "")) {
218 $s .= '<div id="pumpio-authenticate-wrapper">';
219 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Authenticate your pump.io connection").'</a>';
220 $s .= '</div><div class="clear"></div>';
222 //$s .= t("You are not authenticated to pumpio");
224 $s .= '<div id="pumpio-enable-wrapper">';
225 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
226 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
227 $s .= '</div><div class="clear"></div>';
229 $s .= '<div id="pumpio-bydefault-wrapper">';
230 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
231 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
232 $s .= '</div><div class="clear"></div>';
234 $s .= '<div id="pumpio-public-wrapper">';
235 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
236 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
237 $s .= '</div><div class="clear"></div>';
239 $s .= '<div id="pumpio-mirror-wrapper">';
240 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
241 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
242 $s .= '</div><div class="clear"></div>';
244 $s .= '<div id="pumpio-delete-wrapper">';
245 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
246 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
247 $s .= '</div><div class="clear"></div>';
249 //$s .= '<div id="pumpio-authenticate-wrapper">';
250 //$s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Reauthenticate your pump.io connection").'</a>';
251 //$s .= '</div><div class="clear"></div>';
255 $s .= '</div><div class="clear"></div>';
258 /* provide a submit button */
260 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
265 function pumpio_settings_post(&$a,&$b) {
267 if(x($_POST,'pumpio-submit')) {
268 if(x($_POST,'pumpio_delete')) {
269 set_pconfig(local_user(),'pumpio','consumer_key','');
270 set_pconfig(local_user(),'pumpio','consumer_secret','');
271 set_pconfig(local_user(),'pumpio','host','');
272 set_pconfig(local_user(),'pumpio','oauth_token','');
273 set_pconfig(local_user(),'pumpio','oauth_token_secret','');
274 set_pconfig(local_user(),'pumpio','post',false);
275 set_pconfig(local_user(),'pumpio','post_by_default',false);
276 set_pconfig(local_user(),'pumpio','user','');
278 // filtering the username if it is filled wrong
279 $user = $_POST['pumpio_user'];
280 if (strstr($user, "@")) {
281 $pos = strpos($user, "@");
283 $user = substr($user, 0, $pos);
286 // Filtering the hostname if someone is entering it with "http"
287 $host = $_POST['pumpio_host'];
289 $host = str_replace(array("https://", "http://"), array("", ""), $host);
291 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
292 set_pconfig(local_user(),'pumpio','host',$host);
293 set_pconfig(local_user(),'pumpio','user',$user);
294 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
295 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
296 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
301 function pumpio_post_local(&$a,&$b) {
303 // This can probably be changed to allow editing by pointing to a different API endpoint
308 if((! local_user()) || (local_user() != $b['uid']))
311 if($b['private'] || $b['parent'])
314 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
316 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
318 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
324 if(strlen($b['postopts']))
325 $b['postopts'] .= ',';
327 $b['postopts'] .= 'pumpio';
333 function pumpio_send(&$a,&$b) {
335 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
338 if(! strstr($b['postopts'],'pumpio'))
341 if($b['parent'] != $b['id'])
344 // if post comes from pump.io don't send it back
345 if($b['app'] == "pump.io")
348 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
349 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
350 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
351 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
353 $host = get_pconfig($b['uid'], "pumpio", "host");
354 $user = get_pconfig($b['uid'], "pumpio", "user");
355 $public = get_pconfig($b['uid'], "pumpio", "public");
357 if($oauth_token && $oauth_token_secret) {
359 require_once('include/bbcode.php');
361 $title = trim($b['title']);
364 $title = "<h4>".$title."</h4>";
366 $content = bbcode($b['body'], false, false);
368 // Enhance the way, videos are displayed
369 $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
370 $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
371 $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
372 $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
374 $URLSearchString = "^\[\]";
375 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
379 $params["verb"] = "post";
381 $params["object"] = array(
382 'objectType' => "note",
383 'content' => $title.$content);
386 $params["to"] = array(Array(
387 "objectType" => "collection",
388 "id" => "http://activityschema.org/collection/public"));
390 $client = new oauth_client_class;
391 $client->oauth_version = '1.0a';
392 $client->url_parameters = false;
393 $client->authorization_header = true;
394 $client->access_token = $oauth_token;
395 $client->access_token_secret = $oauth_token_secret;
396 $client->client_id = $consumer_key;
397 $client->client_secret = $consumer_secret;
399 $username = $user.'@'.$host;
401 $success = $client->CallAPI(
402 'https://'.$host.'/api/user/'.$user.'/feed',
403 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
406 logger('pumpio_send '.$username.': success');
408 logger('pumpio_send '.$username.': general error: ' . print_r($user,true));
413 function pumpio_cron($a,$b) {
414 $last = get_config('pumpio','last_poll');
416 $poll_interval = intval(get_config('pumpio','poll_interval'));
418 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
421 $next = $last + ($poll_interval * 60);
423 logger('pumpio: poll intervall not reached');
427 logger('pumpio: cron_start');
429 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
432 logger('pumpio: fetching for user '.$rr['uid']);
433 pumpio_fetchtimeline($a, $rr['uid']);
437 logger('pumpio: cron_end');
439 set_config('pumpio','last_poll', time());
442 function pumpio_fetchtimeline($a, $uid) {
443 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
444 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
445 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
446 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
447 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
448 $hostname = get_pconfig($uid, 'pumpio','host');
449 $username = get_pconfig($uid, "pumpio", "user");
451 $application_name = get_config('pumpio', 'application_name');
453 if ($application_name == "")
454 $application_name = $a->get_hostname();
456 $first_time = ($lastdate == "");
458 $client = new oauth_client_class;
459 $client->oauth_version = '1.0a';
460 $client->authorization_header = true;
461 $client->url_parameters = false;
463 $client->client_id = $ckey;
464 $client->client_secret = $csecret;
465 $client->access_token = $otoken;
466 $client->access_token_secret = $osecret;
468 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
470 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
472 $username = $user.'@'.$host;
474 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
477 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
481 $posts = array_reverse($user->items);
483 $initiallastdate = $lastdate;
487 foreach ($posts as $post) {
488 if ($post->generator->published <= $initiallastdate)
491 if ($lastdate < $post->generator->published)
492 $lastdate = $post->generator->published;
497 $receiptians = array();
498 if (@is_array($post->cc))
499 $receiptians = array_merge($receiptians, $post->cc);
501 if (@is_array($post->to))
502 $receiptians = array_merge($receiptians, $post->to);
505 foreach ($receiptians AS $receiver)
506 if (is_string($receiver->objectType))
507 if ($receiver->id == "http://activityschema.org/collection/public")
510 if ($public AND !strstr($post->generator->displayName, $application_name)) {
511 require_once('include/html2bbcode.php');
513 $_SESSION["authenticated"] = true;
514 $_SESSION["uid"] = $uid;
517 $_REQUEST["type"] = "wall";
518 $_REQUEST["api_source"] = true;
519 $_REQUEST["profile_uid"] = $uid;
520 $_REQUEST["source"] = "pump.io";
522 if ($post->object->displayName != "")
523 $_REQUEST["title"] = html2bbcode($post->object->displayName);
525 $_REQUEST["title"] = "";
527 $_REQUEST["body"] = html2bbcode($post->object->content);
529 if ($post->object->fullImage->url != "")
530 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
532 logger('pumpio: posting for user '.$uid);
534 require_once('mod/item.php');
535 //print_r($_REQUEST);
537 logger('pumpio: posting done - user '.$uid);
542 //$lastdate = '2013-05-16T20:22:12Z';
545 set_pconfig($uid,'pumpio','lastdate', $lastdate);