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-servername-wrapper">';
202 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
203 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
204 $s .= '</div><div class="clear"></div>';
206 $s .= '<div id="pumpio-username-wrapper">';
207 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
208 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
209 $s .= '</div><div class="clear"></div>';
211 if (($username != '') AND ($servername != '')) {
212 $s .= '<div id="pumpio-authenticate-wrapper">';
213 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("(Re-)Authenticate your pump.io connection").'</a>';
214 $s .= '</div><div class="clear"></div>';
216 $s .= '<div id="pumpio-enable-wrapper">';
217 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
218 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
219 $s .= '</div><div class="clear"></div>';
221 $s .= '<div id="pumpio-bydefault-wrapper">';
222 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
223 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
224 $s .= '</div><div class="clear"></div>';
226 $s .= '<div id="pumpio-public-wrapper">';
227 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
228 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
229 $s .= '</div><div class="clear"></div>';
231 $s .= '<div id="pumpio-mirror-wrapper">';
232 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
233 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
234 $s .= '</div><div class="clear"></div>';
236 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
237 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
239 $s .= '<div id="pumpio-password-wrapper">';
240 if (($oauth_token == "") OR ($oauth_token_secret == ""))
241 $s .= t("You are not authenticated to pumpio");
243 $s .= '</div><div class="clear"></div>';
246 /* provide a submit button */
248 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
253 function pumpio_settings_post(&$a,&$b) {
255 if(x($_POST,'pumpio-submit')) {
256 // filtering the username if it is filled wrong
257 $user = $_POST['pumpio_user'];
258 if (strstr($user, "@")) {
259 $pos = strpos($user, "@");
261 $user = substr($user, 0, $pos);
264 // Filtering the hostname if someone is entering it with "http"
265 $host = $_POST['pumpio_host'];
267 $host = str_replace(array("https://", "http://"), array("", ""), $host);
269 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
270 set_pconfig(local_user(),'pumpio','host',$host);
271 set_pconfig(local_user(),'pumpio','user',$user);
272 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
273 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
274 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
280 function pumpio_post_local(&$a,&$b) {
282 // This can probably be changed to allow editing by pointing to a different API endpoint
287 if((! local_user()) || (local_user() != $b['uid']))
290 if($b['private'] || $b['parent'])
293 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
295 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
297 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
303 if(strlen($b['postopts']))
304 $b['postopts'] .= ',';
306 $b['postopts'] .= 'pumpio';
312 function pumpio_send(&$a,&$b) {
314 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
317 if(! strstr($b['postopts'],'pumpio'))
320 if($b['parent'] != $b['id'])
323 // if post comes from pump.io don't send it back
324 if($b['app'] == "pump.io")
327 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
328 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
329 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
330 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
332 $host = get_pconfig($b['uid'], "pumpio", "host");
333 $user = get_pconfig($b['uid'], "pumpio", "user");
334 $public = get_pconfig($b['uid'], "pumpio", "public");
336 if($oauth_token && $oauth_token_secret) {
338 require_once('include/bbcode.php');
340 $title = trim($b['title']);
343 $title = "<h4>".$title."</h4>";
347 $params["verb"] = "post";
349 $params["object"] = array(
350 'objectType' => "note",
351 'content' => $title.bbcode($b['body'], false, false));
354 $params["to"] = array(Array(
355 "objectType" => "collection",
356 "id" => "http://activityschema.org/collection/public"));
358 $client = new oauth_client_class;
359 $client->oauth_version = '1.0a';
360 $client->url_parameters = false;
361 $client->authorization_header = true;
362 $client->access_token = $oauth_token;
363 $client->access_token_secret = $oauth_token_secret;
364 $client->client_id = $consumer_key;
365 $client->client_secret = $consumer_secret;
367 $success = $client->CallAPI(
368 'https://'.$host.'/api/user/'.$user.'/feed',
369 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
372 logger('pumpio_send: success');
374 logger('pumpio_send: general error: ' . print_r($user,true));
379 function pumpio_cron($a,$b) {
380 $last = get_config('pumpio','last_poll');
382 $poll_interval = intval(get_config('pumpio','poll_interval'));
384 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
387 $next = $last + ($poll_interval * 60);
389 logger('pumpio: poll intervall not reached');
393 logger('pumpio: cron_start');
395 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
398 logger('pumpio: fetching for user '.$rr['uid']);
399 pumpio_fetchtimeline($a, $rr['uid']);
403 logger('pumpio: cron_end');
405 set_config('pumpio','last_poll', time());
408 function pumpio_fetchtimeline($a, $uid) {
409 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
410 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
411 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
412 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
413 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
414 $hostname = get_pconfig($uid, 'pumpio','host');
415 $username = get_pconfig($uid, "pumpio", "user");
417 $application_name = get_config('pumpio', 'application_name');
419 if ($application_name == "")
420 $application_name = $a->get_hostname();
422 $first_time = ($lastdate == "");
424 $client = new oauth_client_class;
425 $client->oauth_version = '1.0a';
426 $client->authorization_header = true;
427 $client->url_parameters = false;
429 $client->client_id = $ckey;
430 $client->client_secret = $csecret;
431 $client->access_token = $otoken;
432 $client->access_token_secret = $osecret;
434 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
436 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
438 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
441 logger('pumpio: error fetching posts for user '.$uid." ".print_r($user, true));
445 $posts = array_reverse($user->items);
447 $initiallastdate = $lastdate;
451 foreach ($posts as $post) {
452 if ($post->generator->published <= $initiallastdate)
455 if ($lastdate < $post->generator->published)
456 $lastdate = $post->generator->published;
461 $receiptians = array();
462 if (@is_array($post->cc))
463 $receiptians = array_merge($receiptians, $post->cc);
465 if (@is_array($post->to))
466 $receiptians = array_merge($receiptians, $post->to);
469 foreach ($receiptians AS $receiver)
470 if (is_string($receiver->objectType))
471 if ($receiver->id == "http://activityschema.org/collection/public")
474 if ($public AND !strstr($post->generator->displayName, $application_name)) {
475 require_once('include/html2bbcode.php');
477 $_SESSION["authenticated"] = true;
478 $_SESSION["uid"] = $uid;
480 $_REQUEST["type"] = "wall";
481 $_REQUEST["api_source"] = true;
482 $_REQUEST["profile_uid"] = $uid;
483 $_REQUEST["source"] = "pump.io";
485 if ($post->object->displayName != "")
486 $_REQUEST["title"] = html2bbcode($post->object->displayName);
488 $_REQUEST["body"] = html2bbcode($post->object->content);
490 if ($post->object->fullImage->url != "")
491 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
493 logger('pumpio: posting for user '.$uid);
495 require_once('mod/item.php');
496 //print_r($_REQUEST);
498 logger('pumpio: posting done - user '.$uid);
503 //$lastdate = '2013-05-16T20:22:12Z';
506 set_pconfig($uid,'pumpio','lastdate', $lastdate);