3 * Name: pump.io Post Connector
4 * Description: Post to pump.io
6 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
9 //require_once('library/OAuth1.php');
10 //require_once('addon/pumpio/pumpiooauth/pumpiooauth.php');
12 require('addon/pumpio/oauth/http.php');
13 require('addon/pumpio/oauth/oauth_client.php');
15 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
17 function pumpio_install() {
18 register_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
19 register_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
20 register_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
21 register_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
22 register_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
23 register_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
26 function pumpio_uninstall() {
27 unregister_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
28 unregister_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
29 unregister_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
30 unregister_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
31 unregister_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
32 unregister_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
35 function pumpio_module() {}
37 function pumpio_content(&$a) {
40 notice( t('Permission denied.') . EOL);
44 if (isset($a->argv[1]))
45 switch ($a->argv[1]) {
47 $o = pumpio_connect($a);
50 $o = print_r($a->argv, true);
54 $o = pumpio_connect($a);
59 function pumpio_registerclient($a, $host) {
61 $url = "https://".$host."/api/client/register";
65 $application_name = get_config('pumpio', 'application_name');
67 if ($application_name == "")
68 $application_name = $a->get_hostname();
71 $params["type"] = "client_associate";
72 //$params["contacts"] = "icarus@dabo.de";
73 $params["application_type"] = "native";
74 $params["application_name"] = $application_name;
75 //$params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
76 //$params["redirect_uris"] = $a->get_baseurl()."a/addon/pumpio/pumpio.php";
78 $ch = curl_init($url);
79 curl_setopt($ch, CURLOPT_HEADER, false);
80 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
81 curl_setopt($ch, CURLOPT_POST,1);
82 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
83 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
86 $curl_info = curl_getinfo($ch);
88 if ($curl_info["http_code"] == "200") {
89 $values = json_decode($s);
92 $pumpio["client_id"] = $values->client_id;
93 $pumpio["client_secret"] = $values->client_secret;
99 function pumpio_connect($a) {
100 // Start a session. This is necessary to hold on to a few keys the callback script will also need
103 // Define the needed keys
104 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
105 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
106 $hostname = get_pconfig(local_user(), 'pumpio','host');
108 if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
109 $clientdata = pumpio_registerclient($a, $hostname);
110 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
111 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
113 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
114 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
117 if (($consumer_key == "") OR ($consumer_secret == ""))
120 // The callback URL is the script that gets called after the user authenticates with pumpio
121 $callback_url = $a->get_baseurl()."/pumpio/connect";
123 // Let's begin. First we need a Request Token. The request token is required to send the user
124 // to pumpio's login page.
126 // Create a new instance of the TumblrOAuth library. For this step, all we need to give the library is our
127 // Consumer Key and Consumer Secret
128 $client = new oauth_client_class;
130 $client->server = '';
131 $client->oauth_version = '1.0a';
132 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
133 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
134 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
135 $client->url_parameters = false;
136 $client->authorization_header = true;
137 $client->redirect_uri = $callback_url;
138 $client->client_id = $consumer_key;
139 $client->client_secret = $consumer_secret;
141 if (($success = $client->Initialize())) {
142 if (($success = $client->Process())) {
143 if (strlen($client->access_token)) {
144 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
145 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
148 $success = $client->Finalize($success);
151 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
154 $o .= t("You are now authenticated to pumpio.");
155 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
161 function pumpio_jot_nets(&$a,&$b) {
165 $pumpio_post = get_pconfig(local_user(),'pumpio','post');
166 if(intval($pumpio_post) == 1) {
167 $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
168 $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
169 $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
170 . t('Post to pumpio') . '</div>';
175 function pumpio_settings(&$a,&$s) {
180 /* Add our stylesheet to the page so we can make our settings look nice */
182 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
184 /* Get the current state of our config variables */
186 $enabled = get_pconfig(local_user(),'pumpio','post');
187 $checked = (($enabled) ? ' checked="checked" ' : '');
189 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
190 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
192 $public_enabled = get_pconfig(local_user(),'pumpio','public');
193 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
195 $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
196 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
198 $servername = get_pconfig(local_user(), "pumpio", "host");
199 $username = get_pconfig(local_user(), "pumpio", "user");
201 /* Add some HTML to the existing form */
203 $s .= '<div class="settings-block">';
204 $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
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 $s .= '<div id="pumpio-username-wrapper">';
212 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username').'</label>';
213 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
214 $s .= '</div><div class="clear"></div>';
216 if (($username != '') AND ($servername != '')) {
217 $s .= '<div id="pumpio-authenticate-wrapper">';
218 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("(Re-)Authenticate your pump.io connection").'</a>';
219 $s .= '</div><div class="clear"></div>';
221 $s .= '<div id="pumpio-enable-wrapper">';
222 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
223 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
224 $s .= '</div><div class="clear"></div>';
226 $s .= '<div id="pumpio-bydefault-wrapper">';
227 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
228 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
229 $s .= '</div><div class="clear"></div>';
231 $s .= '<div id="pumpio-public-wrapper">';
232 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
233 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
234 $s .= '</div><div class="clear"></div>';
236 $s .= '<div id="pumpio-mirror-wrapper">';
237 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
238 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
239 $s .= '</div><div class="clear"></div>';
241 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
242 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
244 $s .= '<div id="pumpio-password-wrapper">';
245 if (($oauth_token == "") OR ($oauth_token_secret == ""))
246 $s .= t("You are not authenticated to pumpio");
248 $s .= '</div><div class="clear"></div>';
251 /* provide a submit button */
253 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
258 function pumpio_settings_post(&$a,&$b) {
260 if(x($_POST,'pumpio-submit')) {
262 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
263 set_pconfig(local_user(),'pumpio','host',$_POST['pumpio_host']);
264 set_pconfig(local_user(),'pumpio','user',$_POST['pumpio_user']);
265 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
266 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
267 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
273 function pumpio_post_local(&$a,&$b) {
275 // This can probably be changed to allow editing by pointing to a different API endpoint
280 if((! local_user()) || (local_user() != $b['uid']))
283 if($b['private'] || $b['parent'])
286 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
288 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
290 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
296 if(strlen($b['postopts']))
297 $b['postopts'] .= ',';
299 $b['postopts'] .= 'pumpio';
305 function pumpio_send(&$a,&$b) {
307 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
310 if(! strstr($b['postopts'],'pumpio'))
313 if($b['parent'] != $b['id'])
316 // if post comes from pump.io don't send it back
317 if($b['app'] == "pump.io")
320 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
321 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
322 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
323 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
325 $host = get_pconfig($b['uid'], "pumpio", "host");
326 $user = get_pconfig($b['uid'], "pumpio", "user");
327 $public = get_pconfig($b['uid'], "pumpio", "public");
329 if($oauth_token && $oauth_token_secret) {
331 require_once('include/bbcode.php');
333 $title = trim($b['title']);
336 $title = "<h4>".$title."</h4>";
338 $params->verb = "post";
340 $params->object = array(
341 'objectType' => "note",
342 'content' => $title.bbcode($b['body'], false, false));
345 $params->to = array(Array(
346 "objectType" => "collection",
347 "id" => "http://activityschema.org/collection/public"));
349 $client = new oauth_client_class;
350 $client->oauth_version = '1.0a';
351 $client->url_parameters = false;
352 $client->authorization_header = true;
353 $client->access_token = $oauth_token;
354 $client->access_token_secret = $oauth_token_secret;
355 $client->client_id = $consumer_key;
356 $client->client_secret = $consumer_secret;
358 $success = $client->CallAPI(
359 'https://'.$host.'/api/user/'.$user.'/feed',
360 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
363 logger('pumpio_send: success');
365 logger('pumpio_send: general error: ' . print_r($user,true));
370 function pumpio_cron($a,$b) {
371 $last = get_config('pumpio','last_poll');
373 $poll_interval = intval(get_config('pumpio','poll_interval'));
375 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
378 $next = $last + ($poll_interval * 60);
380 logger('pumpio: poll intervall not reached');
384 logger('pumpio: cron_start');
386 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
389 logger('pumpio: fetching for user '.$rr['uid']);
390 pumpio_fetchtimeline($a, $rr['uid']);
394 logger('pumpio: cron_end');
396 set_config('pumpio','last_poll', time());
399 function pumpio_fetchtimeline($a, $uid) {
400 $ckey = get_pconfig($uid, 'pumpio', 'consumerkey');
401 $csecret = get_pconfig($uid, 'pumpio', 'consumersecret');
402 $otoken = get_pconfig($uid, 'pumpio', 'oauthtoken');
403 $osecret = get_pconfig($uid, 'pumpio', 'oauthsecret');
404 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
405 $hostname = get_pconfig($uid, 'pumpio','host');
406 $username = get_pconfig($uid, "pumpio", "user");
408 $application_name = get_config('pumpio', 'application_name');
410 if ($application_name == "")
411 $application_name = $a->get_hostname();
413 $first_time = ($lastid == "");
415 require('oauth/http.php');
416 require('oauth/oauth_client.php');
418 $client = new oauth_client_class;
419 $client->oauth_version = '1.0a';
420 $client->authorization_header = true;
421 $client->url_parameters = false;
423 $client->client_id = $ckey;
424 $client->client_secret = $csekret;
425 $client->access_token = $otoken;
426 $client->access_token_secret = $osecret;
428 $success = $client->CallAPI(
429 'https://'.$hostname.'/api/user/'.$username.'/feed/major',
430 'GET', array(), array('FailOnAccessError'=>true), $user);
432 $posts = array_reverse($user->items);
434 $initiallastdate = $lastdate;
438 foreach ($posts as $post) {
439 if (strtotime($post->published) <= $initiallastdate)
442 if ($lastdate < strtotime($post->published))
443 $lastdate = strtotime($post->published);
448 if (!strpos($post->source, $application_name)) {
449 require_once('include/html2bbcode.php');
451 $_SESSION["authenticated"] = true;
452 $_SESSION["uid"] = $uid;
454 $_REQUEST["type"] = "wall";
455 $_REQUEST["api_source"] = true;
456 $_REQUEST["profile_uid"] = $uid;
457 $_REQUEST["source"] = "pump.io";
459 $_REQUEST["body"] = html2bbcode($post->object->content);
461 logger('pumpio: posting for user '.$uid);
463 require_once('mod/item.php');
470 set_pconfig($uid,'pumpio','lastdate', $lastdate);