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