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 function pumpio_install() {
16 register_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
17 register_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
18 register_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
19 register_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
20 register_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
23 function pumpio_uninstall() {
24 unregister_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
25 unregister_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
26 unregister_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
27 unregister_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
28 unregister_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
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($host) {
57 $url = "https://".$host."/api/client/register";
61 $params["type"] = "client_associate";
62 $params["contacts"] = "icarus@dabo.de";
63 $params["application_type"] = "native";
64 $params["application_name"] = "pirati.ca";
65 $params["logo_url"] = "https://pirati.ca/images/friendica-256.png";
66 $params["redirect_uris"] = "http://pirati.ca/addon/pumpio/pumpio.php";
68 $ch = curl_init($url);
69 curl_setopt($ch, CURLOPT_HEADER, false);
70 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
71 curl_setopt($ch, CURLOPT_POST,1);
72 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
73 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
76 $curl_info = curl_getinfo($ch);
78 if ($curl_info["http_code"] == "200") {
79 $values = json_decode($s);
82 $pumpio["client_id"] = $values->client_id;
83 $pumpio["client_secret"] = $values->client_secret;
89 function pumpio_connect($a) {
90 // Start a session. This is necessary to hold on to a few keys the callback script will also need
93 // Define the needed keys
94 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
95 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
96 $hostname = get_pconfig(local_user(), 'pumpio','host');
98 if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
99 $clientdata = pumpio_registerclient($hostname);
100 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
101 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
103 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
104 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
107 if (($consumer_key == "") OR ($consumer_secret == ""))
110 // The callback URL is the script that gets called after the user authenticates with pumpio
111 $callback_url = $a->get_baseurl()."/pumpio/connect";
113 // Let's begin. First we need a Request Token. The request token is required to send the user
114 // to pumpio's login page.
116 // Create a new instance of the TumblrOAuth library. For this step, all we need to give the library is our
117 // Consumer Key and Consumer Secret
118 $client = new oauth_client_class;
120 $client->server = '';
121 $client->oauth_version = '1.0a';
122 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
123 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
124 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
125 $client->url_parameters = false;
126 $client->authorization_header = true;
127 $client->redirect_uri = $callback_url;
128 $client->client_id = $consumer_key;
129 $client->client_secret = $consumer_secret;
131 if (($success = $client->Initialize())) {
132 if (($success = $client->Process())) {
133 if (strlen($client->access_token)) {
134 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
135 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
138 $success = $client->Finalize($success);
141 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
144 $o .= t("You are now authenticated to pumpio.");
145 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
151 function pumpio_jot_nets(&$a,&$b) {
155 $pumpio_post = get_pconfig(local_user(),'pumpio','post');
156 if(intval($pumpio_post) == 1) {
157 $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
158 $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
159 $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
160 . t('Post to pumpio') . '</div>';
165 function pumpio_settings(&$a,&$s) {
170 /* Add our stylesheet to the page so we can make our settings look nice */
172 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
174 /* Get the current state of our config variables */
176 $enabled = get_pconfig(local_user(),'pumpio','post');
178 $checked = (($enabled) ? ' checked="checked" ' : '');
180 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
182 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
184 $servername = get_pconfig(local_user(), "pumpio", "host");
185 $username = get_pconfig(local_user(), "pumpio", "user");
187 /* Add some HTML to the existing form */
189 $s .= '<div class="settings-block">';
190 $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
192 $s .= '<div id="pumpio-servername-wrapper">';
193 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername').'</label>';
194 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
195 $s .= '</div><div class="clear"></div>';
197 $s .= '<div id="pumpio-username-wrapper">';
198 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username').'</label>';
199 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
200 $s .= '</div><div class="clear"></div>';
202 if (($username != '') AND ($servername != '')) {
203 $s .= '<div id="pumpio-authenticate-wrapper">';
204 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("(Re-)Authenticate your pump.io connection").'</a>';
205 $s .= '</div><div class="clear"></div>';
207 $s .= '<div id="pumpio-enable-wrapper">';
208 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
209 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
210 $s .= '</div><div class="clear"></div>';
212 $s .= '<div id="pumpio-bydefault-wrapper">';
213 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
214 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
215 $s .= '</div><div class="clear"></div>';
217 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
218 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
220 $s .= '<div id="pumpio-password-wrapper">';
221 if (($oauth_token == "") OR ($oauth_token_secret == ""))
222 $s .= t("You are not authenticated to pumpio");
224 $s .= '</div><div class="clear"></div>';
227 /* provide a submit button */
229 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
234 function pumpio_settings_post(&$a,&$b) {
236 if(x($_POST,'pumpio-submit')) {
238 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
239 set_pconfig(local_user(),'pumpio','host',$_POST['pumpio_host']);
240 set_pconfig(local_user(),'pumpio','user',$_POST['pumpio_user']);
241 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
247 function pumpio_post_local(&$a,&$b) {
249 // This can probably be changed to allow editing by pointing to a different API endpoint
254 if((! local_user()) || (local_user() != $b['uid']))
257 if($b['private'] || $b['parent'])
260 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
262 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
264 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
270 if(strlen($b['postopts']))
271 $b['postopts'] .= ',';
273 $b['postopts'] .= 'pumpio';
279 function pumpio_send(&$a,&$b) {
281 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
284 if(! strstr($b['postopts'],'pumpio'))
287 if($b['parent'] != $b['id'])
290 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
291 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
292 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
293 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
295 $host = get_pconfig($b['uid'], "pumpio", "host");
296 $user = get_pconfig($b['uid'], "pumpio", "user");
298 if($oauth_token && $oauth_token_secret) {
300 require_once('include/bbcode.php');
302 $title = trim($b['title']);
305 $title = "<h4>".$title."</h4>";
307 $params->verb = "post";
309 $params->object = array(
310 'objectType' => "note",
311 'content' => $title.bbcode($b['body'], false, false));
313 $client = new oauth_client_class;
314 $client->oauth_version = '1.0a';
315 $client->url_parameters = false;
316 $client->authorization_header = true;
317 $client->access_token = $oauth_token;
318 $client->access_token_secret = $oauth_token_secret;
319 $client->client_id = $consumer_key;
320 $client->client_secret = $consumer_secret;
322 $success = $client->CallAPI(
323 'https://'.$host.'/api/user/'.$user.'/feed',
324 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
327 logger('pumpio_send: success');
329 logger('pumpio_send: general error: ' . print_r($user,true));