pumpio: New plugin to post to pump.io servers
[friendica-addons.git/.git] / pumpio / pumpio.php
1 <?php
2 /**
3  * Name: pump.io Post Connector
4  * Description: Post to pump.io
5  * Version: 0.1
6  * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
7  */
8
9 //require_once('library/OAuth1.php');
10 //require_once('addon/pumpio/pumpiooauth/pumpiooauth.php');
11
12 require('addon/pumpio/oauth/http.php');
13 require('addon/pumpio/oauth/oauth_client.php');
14
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');
21
22 }
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');
29 }
30
31 function pumpio_module() {}
32
33 function pumpio_content(&$a) {
34
35         if(! local_user()) {
36                 notice( t('Permission denied.') . EOL);
37                 return '';
38         }
39
40         if (isset($a->argv[1]))
41                 switch ($a->argv[1]) {
42                         case "connect":
43                                 $o = pumpio_connect($a);
44                                 break;
45                         default:
46                                 $o = print_r($a->argv, true);
47                                 break;
48                 }
49         else
50                 $o = pumpio_connect($a);
51
52         return $o;
53 }
54
55 function pumpio_registerclient($host) {
56
57         $url = "https://".$host."/api/client/register";
58
59         $params = array();
60
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";
67
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");
74
75         $s = curl_exec($ch);
76         $curl_info = curl_getinfo($ch);
77
78         if ($curl_info["http_code"] == "200") {
79                 $values = json_decode($s);
80                 return($values);
81                 $pumpio = array();
82                 $pumpio["client_id"] = $values->client_id;
83                 $pumpio["client_secret"] = $values->client_secret;
84                 print_r($values);
85         }
86         return(false);
87 }
88
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
91         session_start();
92
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');
97
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);
102
103                 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
104                 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
105         }
106
107         if (($consumer_key == "") OR ($consumer_secret == ""))
108                 return;
109
110         // The callback URL is the script that gets called after the user authenticates with pumpio
111         $callback_url = $a->get_baseurl()."/pumpio/connect";
112
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.
115
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;
119         $client->debug = 1;
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;
130
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);
136                         }
137                 }
138                 $success = $client->Finalize($success);
139         }
140         if($client->exit)
141             $o = 'Could not connect to pumpio. Refresh the page or try again later.';
142
143         if($success) {
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>';
146         }
147
148         return($o);
149 }
150
151 function pumpio_jot_nets(&$a,&$b) {
152     if(! local_user())
153         return;
154
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>';
161     }
162 }
163
164
165 function pumpio_settings(&$a,&$s) {
166
167     if(! local_user())
168         return;
169
170     /* Add our stylesheet to the page so we can make our settings look nice */
171
172     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
173
174     /* Get the current state of our config variables */
175
176     $enabled = get_pconfig(local_user(),'pumpio','post');
177
178     $checked = (($enabled) ? ' checked="checked" ' : '');
179
180     $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
181
182     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
183
184     $servername = get_pconfig(local_user(), "pumpio", "host");
185     $username = get_pconfig(local_user(), "pumpio", "user");
186
187     /* Add some HTML to the existing form */
188
189     $s .= '<div class="settings-block">';
190     $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
191
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>';
196
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>';
201
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>';
206
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>';
211
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>';
216
217         $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
218         $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
219
220         $s .= '<div id="pumpio-password-wrapper">';
221         if (($oauth_token == "") OR ($oauth_token_secret == ""))
222                 $s .= t("You are not authenticated to pumpio");
223
224         $s .= '</div><div class="clear"></div>';
225     }
226
227     /* provide a submit button */
228
229     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
230
231 }
232
233
234 function pumpio_settings_post(&$a,&$b) {
235
236         if(x($_POST,'pumpio-submit')) {
237
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']));
242
243         }
244
245 }
246
247 function pumpio_post_local(&$a,&$b) {
248
249         // This can probably be changed to allow editing by pointing to a different API endpoint
250
251         if($b['edit'])
252                 return;
253
254         if((! local_user()) || (local_user() != $b['uid']))
255                 return;
256
257         if($b['private'] || $b['parent'])
258                 return;
259
260         $pumpio_post   = intval(get_pconfig(local_user(),'pumpio','post'));
261
262         $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
263
264         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
265                 $pumpio_enable = 1;
266
267         if(! $pumpio_enable)
268                 return;
269
270         if(strlen($b['postopts']))
271                 $b['postopts'] .= ',';
272
273         $b['postopts'] .= 'pumpio';
274 }
275
276
277
278
279 function pumpio_send(&$a,&$b) {
280
281         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
282                 return;
283
284         if(! strstr($b['postopts'],'pumpio'))
285                 return;
286
287         if($b['parent'] != $b['id'])
288                 return;
289
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");
294
295         $host = get_pconfig($b['uid'], "pumpio", "host");
296         $user = get_pconfig($b['uid'], "pumpio", "user");
297
298         if($oauth_token && $oauth_token_secret) {
299
300                 require_once('include/bbcode.php');
301
302                 $title = trim($b['title']);
303
304                 if ($title != '')
305                         $title = "<h4>".$title."</h4>";
306
307                 $params->verb = "post";
308
309                 $params->object = array(
310                                         'objectType' => "note",
311                                         'content' => $title.bbcode($b['body'], false, false));
312
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;
321
322                 $success = $client->CallAPI(
323                                         'https://'.$host.'/api/user/'.$user.'/feed',
324                                         'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
325
326                 if($success)
327                         logger('pumpio_send: success');
328                 else
329                         logger('pumpio_send: general error: ' . print_r($user,true));
330
331         }
332 }
333