]> git.mxchange.org Git - friendica-addons.git/blob - fromgplus/fromgplus.php
Merge pull request #146 from annando/master
[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         if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
128                 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
129                 return;
130         }
131
132         require_once('mod/item.php');
133         //print_r($_REQUEST);
134         logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
135         item_post($a);
136         logger('fromgplus: done for user '.$uid);
137 }
138
139 function fromgplus_html2bbcode($html) {
140
141         $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
142
143         $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
144         $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
145         $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
146         $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
147         $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
148         $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
149         $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
150
151         $bbcode = trim(strip_tags($bbcode));
152         return($bbcode);
153 }
154
155 function fromgplus_parse_query($var)
156  {
157         /**
158         *  Use this function to parse out the query array element from
159         *  the output of parse_url().
160         */
161         $var  = parse_url($var, PHP_URL_QUERY);
162         $var  = html_entity_decode($var);
163         $var  = explode('&', $var);
164         $arr  = array();
165
166         foreach($var as $val) {
167                 $x          = explode('=', $val);
168                 $arr[$x[0]] = $x[1];
169         }
170         unset($val, $x, $var);
171         return $arr;
172 }
173
174 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
175
176         $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
177         $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
178         $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
179
180         $preview = "/w".$image->width."-h".$image->height."/";
181         $preview2 = "/w".$image->width."-h".$image->height."-p/";
182         $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
183
184         $cleaned = array();
185
186         $queryvar = fromgplus_parse_query($fullImage);
187         if ($queryvar['url'] != "")
188                 $cleaned["full"] = urldecode($queryvar['url']);
189         else
190                 $cleaned["full"] = $fullImage;
191         if (@exif_imagetype($cleaned["full"]) == 0)
192                 $cleaned["full"] = "";
193
194         $queryvar = fromgplus_parse_query($image);
195         if ($queryvar['url'] != "")
196                 $cleaned["preview"] = urldecode($queryvar['url']);
197         else
198                 $cleaned["preview"] = $image;
199         if (@exif_imagetype($cleaned["preview"]) == 0)
200                 $cleaned["preview"] = "";
201
202         if ($cleaned["full"] == "") {
203                 $cleaned["full"] = $cleaned["preview"];
204                 $cleaned["preview"] = "";
205         }
206
207         if ($cleaned["full"] == $cleaned["preview"])
208                 $cleaned["preview"] = "";
209
210         if ($cleaned["full"] == "")
211                 if (@exif_imagetype($fullImage) != 0)
212                         $cleaned["full"] = $fullImage;
213
214         if ($cleaned["full"] == "")
215                 if (@exif_imagetype($image) != 0)
216                         $cleaned["full"] = $fullImage;
217
218         return($cleaned);
219 }
220
221 function fromgplus_cleantext($text) {
222         $text = strip_tags($text);
223         $text = html_entity_decode($text);
224         $text = trim($text);
225         $text = str_replace(array("\n", "\r", " "), array("", "", ""), $text);
226         return($text);
227 }
228
229 function fromgplus_handleattachments($item, $displaytext) {
230         $post = "";
231         $quote = "";
232
233         foreach ($item->object->attachments as $attachment) {
234                 switch($attachment->objectType) {
235                         case "video":
236                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
237
238                                 /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
239                                 if ($images["preview"] != "")
240                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
241                                 elseif ($images["full"] != "")
242                                         $post .= "\n[img]".$images["full"]."[/img]\n";*/
243
244                                 break;
245
246                         case "article":
247                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
248
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                                 if ($images["full"] != "")
255                                         $post .= "\n[img]".$images["full"]."[/img]\n";
256
257                                 //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
258                                 $quote = trim(fromgplus_html2bbcode($attachment->content));
259                                 if ($quote != "")
260                                         $quote = "\n[quote]".$quote."[/quote]";
261                                 break;
262
263                         case "photo":
264                                 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
265                                 if ($images["preview"] != "")
266                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
267                                 elseif ($images["full"] != "")
268                                         $post .= "\n[img]".$images["full"]."[/img]\n";
269
270                                 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext)))
271                                         $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
272                                 break;
273
274                         case "photo-album":
275                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
276
277                                 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
278                                 if ($images["preview"] != "")
279                                         $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
280                                 elseif ($images["full"] != "")
281                                         $post .= "\n[img]".$images["full"]."[/img]\n";
282
283                                 break;
284
285                         case "album":
286                                 foreach($attachment->thumbnails as $thumb) {
287                                         $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
288                                         $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
289                                         $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
290
291                                         $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
292                                 }
293                                 break;
294                         case "audio":
295                                 $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
296                                 break;
297                         //default:
298                         //      die($attachment->objectType);
299                 }
300         }
301         return($post.$quote);
302 }
303
304 function fromgplus_fetch($a, $uid) {
305         $maxfetch = 20;
306
307         $account = get_pconfig($uid,'fromgplus','account');
308         $key = get_config('fromgplus','key');
309
310         $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
311         //$result = file_get_contents("google.txt");
312         //file_put_contents("google.txt", $result);
313
314         $activities = json_decode($result);
315
316         $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
317
318         $lastdate = 0;
319
320         if (!is_array($activities->items))
321                 return;
322
323         $reversed = array_reverse($activities->items);
324
325         foreach($reversed as $item) {
326                 if (strtotime($item->published) <= $initiallastdate)
327                         continue;
328
329                 if ($lastdate < strtotime($item->published))
330                         $lastdate = strtotime($item->published);
331
332                 if ($item->access->description == "Public")
333                         switch($item->object->objectType) {
334                                 case "note":
335                                         $post = fromgplus_html2bbcode($item->object->content);
336
337                                         if (is_array($item->object->attachments))
338                                                 $post .= fromgplus_handleattachments($item, $item->object->content);
339
340                                         // geocode, placeName
341                                         if (isset($item->address))
342                                                 $location = $item->address;
343                                         else
344                                                 $location = "";
345
346                                         // Loop prevention - should be made better
347                                         if ($item->provider->title != "HootSuite")
348                                                 fromgplus_post($a, $uid, "Google+", $post, $location);
349                                         //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
350
351                                         break;
352
353                                 case "activity":
354                                         $post = fromgplus_html2bbcode($item->annotation)."\n";
355
356                                         if (intval(get_config('system','new_share'))) {
357                                                 $post .= "[share author='".str_replace("'", "&#039;",$item->object->actor->displayName).
358                                                                 "' profile='".$item->object->actor->url.
359                                                                 "' avatar='".$item->object->actor->image->url.
360                                                                 "' link='".$item->object->url."']";
361
362                                                 $post .= fromgplus_html2bbcode($item->object->content);
363
364                                                 if (is_array($item->object->attachments))
365                                                         $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
366
367                                                 $post .= "[/share]";
368                                         } else {
369                                                 $post .= fromgplus_html2bbcode("&#x2672;");
370                                                 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
371                                                 $post .= fromgplus_html2bbcode($item->object->content);
372
373                                                 if (is_array($item->object->attachments))
374                                                         $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
375                                         }
376
377                                         if (isset($item->address))
378                                                 $location = $item->address;
379                                         else
380                                                 $location = "";
381
382                                         // Loop prevention - should be made better
383                                         if ($item->provider->title != "HootSuite")
384                                                 fromgplus_post($a, $uid, "Google+", $post, $location);
385                                         //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
386                                         break;
387                         }
388         }
389         if ($lastdate != 0)
390                 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
391 }
392
393 /*
394 // Test
395 require_once("boot.php");
396
397 if(@is_null($a)) {
398         $a = new App;
399 }
400
401 if(@is_null($db)) {
402         @include(".htconfig.php");
403         require_once("include/dba.php");
404         $db = new dba($db_host, $db_user, $db_pass, $db_data);
405         unset($db_host, $db_user, $db_pass, $db_data);
406 };
407
408 $test = array();
409 fromgplus_cron($a, $test);
410 */