]> git.mxchange.org Git - friendica-addons.git/blob - fromgplus/fromgplus.php
Mirroring: There was a problem that the title variable wasn't cleared so it could...
[friendica-addons.git] / fromgplus / fromgplus.php
1 <?php
2 /**
3  * Name: From GPlus
4  * Description: Imports posts from a Google+ account and repeats them
5  * Version: 0.1
6  * Author: Michael Vogel <ike@piratenpartei.de>
7  *
8  */
9
10 define('FROMGPLUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
11
12 function fromgplus_install() {
13         register_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
14         register_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
15         register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
16 }
17
18 function fromgplus_uninstall() {
19         unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
20         unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
21         unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
22 }
23
24 function fromgplus_addon_settings(&$a,&$s) {
25
26         if(! local_user())
27                 return;
28
29         $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
30         $account = get_pconfig(local_user(),'fromgplus','account');
31
32         $s .= '<div class="settings-block">';
33         $s .= '<h3>' . t('Google+ Import Settings').'</h3>';
34         $s .= '<div id="fromgplus-wrapper">';
35
36         $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
37         $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
38         $s .= '<div class="clear"></div>';
39         $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
40         $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
41         $s .= '</div><div class="clear"></div>';
42
43         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit" 
44 class="settings-submit" value="' . t('Submit') . '" /></div>';
45         $s .= '</div>';
46
47         return;
48 }
49
50 function fromgplus_addon_settings_post(&$a,&$b) {
51
52         if(! local_user())
53                 return;
54
55         if($_POST['fromgplus-submit']) {
56                 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
57                 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
58                 set_pconfig(local_user(),'fromgplus','enable', $enable);
59                 info( t('Google+ Import Settings saved.') . EOL);
60         }
61 }
62
63 function fromgplus_cron($a,$b) {
64         $last = get_config('fromgplus','last_poll');
65
66         $poll_interval = intval(get_config('fromgplus','poll_interval'));
67         if(! $poll_interval)
68                 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
69
70         if($last) {
71                 $next = $last + ($poll_interval * 60);
72                 if($next > time()) {
73                         logger('fromgplus: poll intervall not reached');
74                         return;
75                 }
76         }
77
78         logger('fromgplus: cron_start');
79
80         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
81         if(count($r)) {
82                 foreach($r as $rr) {
83                         $account = get_pconfig($rr['uid'],'fromgplus','account');
84                         if ($account) {
85                         logger('fromgplus: fetching for user '.$rr['uid']);
86                                 fromgplus_fetch($a, $rr['uid']);
87                         }
88                 }
89         }
90
91         logger('fromgplus: cron_end');
92
93         set_config('fromgplus','last_poll', time());
94 }
95
96 function fromgplus_post($a, $uid, $source, $body, $location) {
97
98         //$uid = 2;
99
100         $body = trim($body);
101
102         if (substr($body, 0, 3) == "[b]") {
103                 $pos = strpos($body, "[/b]");
104                 $title = substr($body, 3, $pos-3);
105                 $body = trim(substr($body, $pos+4));
106         } else
107                 $title = "";
108
109         $_SESSION['authenticated'] = true;
110         $_SESSION['uid'] = $uid;
111
112         unset($_REQUEST);
113         $_REQUEST['type'] = 'wall';
114         $_REQUEST['api_source'] = true;
115
116         $_REQUEST['profile_uid'] = $uid;
117         $_REQUEST['source'] = $source;
118
119         // $_REQUEST['verb']
120         // $_REQUEST['parent']
121         // $_REQUEST['parent_uri']
122
123         $_REQUEST['title'] = $title;
124         $_REQUEST['body'] = $body;
125         $_REQUEST['location'] = $location;
126
127         logger('fromgplus: posting for user '.$uid);
128
129         require_once('mod/item.php');
130         //print_r($_REQUEST);
131         item_post($a);
132 }
133
134 function fromgplus_html2bbcode($html) {
135
136         $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
137
138         $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
139         $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
140         $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
141         $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
142         $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
143         $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
144         $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
145
146         $bbcode = trim(strip_tags($bbcode));
147         return($bbcode);
148 }
149
150 function fromgplus_parse_query($var)
151  {
152         /**
153         *  Use this function to parse out the query array element from
154         *  the output of parse_url().
155         */
156         $var  = parse_url($var, PHP_URL_QUERY);
157         $var  = html_entity_decode($var);
158         $var  = explode('&', $var);
159         $arr  = array();
160
161         foreach($var as $val) {
162                 $x          = explode('=', $val);
163                 $arr[$x[0]] = $x[1];
164         }
165         unset($val, $x, $var);
166         return $arr;
167 }
168
169 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
170
171         $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
172         $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
173         $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
174
175         $preview = "/w".$image->width."-h".$image->height."/";
176         $preview2 = "/w".$image->width."-h".$image->height."-p/";
177         $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
178
179         $cleaned = array();
180
181         $queryvar = fromgplus_parse_query($fullImage);
182         if ($queryvar['url'] != "")
183                 $cleaned["full"] = urldecode($queryvar['url']);
184         else
185                 $cleaned["full"] = $fullImage;
186         if (@exif_imagetype($cleaned["full"]) == 0)
187                 $cleaned["full"] = "";
188
189         $queryvar = fromgplus_parse_query($image);
190         if ($queryvar['url'] != "")
191                 $cleaned["preview"] = urldecode($queryvar['url']);
192         else
193                 $cleaned["preview"] = $image;
194         if (@exif_imagetype($cleaned["preview"]) == 0)
195                 $cleaned["preview"] = "";
196
197         if ($cleaned["full"] == "") {
198                 $cleaned["full"] = $cleaned["preview"];
199                 $cleaned["preview"] = "";
200         }
201
202         if ($cleaned["full"] == $cleaned["preview"])
203                 $cleaned["preview"] = "";
204
205         if ($cleaned["full"] == "")
206                 if (@exif_imagetype($fullImage) != 0)
207                         $cleaned["full"] = $fullImage;
208
209         if ($cleaned["full"] == "")
210                 if (@exif_imagetype($image) != 0)
211                         $cleaned["full"] = $fullImage;
212
213         return($cleaned);
214 }
215
216 function fromgplus_handleattachments($item, $displaytext) {
217         $post = "";
218         $quote = "";
219
220         foreach ($item->object->attachments as $attachment) {
221                 switch($attachment->objectType) {
222                         case "video":
223                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
224
225                                 /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
226                                 if ($images["preview"] != "")
227                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
228                                 elseif ($images["full"] != "")
229                                         $post .= "\n[img]".$images["full"]."[/img]\n";*/
230
231                                 break;
232
233                         case "article":
234                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
235
236                                 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
237                                 if ($images["preview"] != "")
238                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
239                                 elseif ($images["full"] != "")
240                                         $post .= "\n[img]".$images["full"]."[/img]\n";
241
242                                 //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
243                                 $quote = trim(fromgplus_html2bbcode($attachment->content));
244                                 if ($quote != "")
245                                         $quote = "\n[quote]".$quote."[/quote]";
246                                 break;
247
248                         case "photo":
249                                 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
250                                 if ($images["preview"] != "")
251                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
252                                 elseif ($images["full"] != "")
253                                         $post .= "\n[img]".$images["full"]."[/img]\n";
254
255                                 if (($attachment->displayName != "") AND ($attachment->displayName != $displaytext))
256                                         $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
257                                 break;
258
259                         case "photo-album":
260                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
261
262                                 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
263                                 if ($images["preview"] != "")
264                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
265                                 elseif ($images["full"] != "")
266                                         $post .= "\n[img]".$images["full"]."[/img]\n";
267
268                                 break;
269
270                         case "album":
271                                 foreach($attachment->thumbnails as $thumb) {
272                                         $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
273                                         $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
274                                         $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
275
276                                         $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
277                                 }
278                                 break;
279                         case "audio":
280                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
281                                 break;
282                         //default:
283                         //      die($attachment->objectType);
284                 }
285         }
286         return($post.$quote);
287 }
288
289 function fromgplus_fetch($a, $uid) {
290         $maxfetch = 20;
291
292         $account = get_pconfig($uid,'fromgplus','account');
293         $key = get_config('fromgplus','key');
294
295         $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
296         //$result = file_get_contents("google.txt");
297         //file_put_contents("google.txt", $result);
298
299         $activities = json_decode($result);
300
301         $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
302
303         $lastdate = 0;
304
305         if (!is_array($activities->items))
306                 return;
307
308         $reversed = array_reverse($activities->items);
309
310         foreach($reversed as $item) {
311                 if (strtotime($item->published) <= $initiallastdate)
312                         continue;
313
314                 if ($lastdate < strtotime($item->published))
315                         $lastdate = strtotime($item->published);
316
317                 if ($item->access->description == "Public")
318                         switch($item->object->objectType) {
319                                 case "note":
320                                         $post = fromgplus_html2bbcode($item->object->content);
321
322                                         if (is_array($item->object->attachments))
323                                                 $post .= fromgplus_handleattachments($item, $item->object->content);
324
325                                         // geocode, placeName
326                                         if (isset($item->address))
327                                                 $location = $item->address;
328                                         else
329                                                 $location = "";
330
331                                         // Loop prevention - should be made better
332                                         if ($item->provider->title != "HootSuite")
333                                                 fromgplus_post($a, $uid, "Google+", $post, $location);
334                                         //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
335
336                                         break;
337
338                                 case "activity":
339                                         $post = fromgplus_html2bbcode($item->annotation)."\n";
340
341                                         if (intval(get_config('system','new_share'))) {
342                                                 $post .= "[share author='".str_replace("'", "&#039;",$item->object->actor->displayName).
343                                                                 "' profile='".$item->object->actor->url.
344                                                                 "' avatar='".$item->object->actor->image->url.
345                                                                 "' link='".$item->object->url."']";
346
347                                                 $post .= fromgplus_html2bbcode($item->object->content);
348
349                                                 if (is_array($item->object->attachments))
350                                                         $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
351
352                                                 $post .= "[/share]";
353                                         } else {
354                                                 $post .= fromgplus_html2bbcode("&#x2672;");
355                                                 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
356                                                 $post .= fromgplus_html2bbcode($item->object->content);
357
358                                                 if (is_array($item->object->attachments))
359                                                         $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
360                                         }
361
362                                         if (isset($item->address))
363                                                 $location = $item->address;
364                                         else
365                                                 $location = "";
366
367                                         // Loop prevention - should be made better
368                                         if ($item->provider->title != "HootSuite")
369                                                 fromgplus_post($a, $uid, "Google+", $post, $location);
370                                         //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
371                                         break;
372                         }
373         }
374         if ($lastdate != 0)
375                 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
376 }
377
378 /*
379 // Test
380 require_once("boot.php");
381
382 if(@is_null($a)) {
383         $a = new App;
384 }
385
386 if(@is_null($db)) {
387         @include(".htconfig.php");
388         require_once("include/dba.php");
389         $db = new dba($db_host, $db_user, $db_pass, $db_data);
390         unset($db_host, $db_user, $db_pass, $db_data);
391 };
392
393 $test = array();
394 fromgplus_cron($a, $test);
395 */