]> git.mxchange.org Git - friendica-addons.git/blob - fromgplus/fromgplus.php
e19a003bc8f7cc49cbc94c5ab52593ca8b3b1923
[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) {
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 != "")
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                         //default:
279                         //      die($attachment->objectType);
280                 }
281         }
282         return($post.$quote);
283 }
284
285 function fromgplus_fetch($a, $uid) {
286         $maxfetch = 20;
287
288         $account = get_pconfig($uid,'fromgplus','account');
289         $key = get_config('fromgplus','key');
290
291         $result = file_get_contents("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
292         //$result = file_get_contents("google.txt");
293         //file_put_contents("google.txt", $result);
294
295         $activities = json_decode($result);
296
297         $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
298
299         $lastdate = 0;
300
301         $reversed = array_reverse($activities->items);
302
303         foreach($reversed as $item) {
304                 if (strtotime($item->published) <= $initiallastdate)
305                         continue;
306
307                 if ($lastdate < strtotime($item->published))
308                         $lastdate = strtotime($item->published);
309
310                 if ($item->access->description == "Public")
311                         switch($item->object->objectType) {
312                                 case "note":
313                                         $post = fromgplus_html2bbcode($item->object->content);
314
315                                         if (is_array($item->object->attachments))
316                                                 $post .= fromgplus_handleattachments($item);
317
318                                         // geocode, placeName
319                                         if (isset($item->address))
320                                                 $location = $item->address;
321                                         else
322                                                 $location = "";
323
324                                         fromgplus_post($a, $uid, $item->provider->title, $post, $location);
325
326                                         break;
327
328                                 case "activity":
329                                         $post = fromgplus_html2bbcode($item->annotation)."\n";
330                                         $post .= fromgplus_html2bbcode("&#x2672;");
331                                         //$post .= html2bbcode("&#x267B;");
332                                         //$post .= fromgplus_html2bbcode("&#x25CC;");
333                                         $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
334                                         $post .= fromgplus_html2bbcode($item->object->content);
335
336                                         if (is_array($item->object->attachments))
337                                                 $post .= "\n".trim(fromgplus_handleattachments($item));
338
339                                         if (isset($item->address))
340                                                 $location = $item->address;
341                                         else
342                                                 $location = "";
343
344                                         fromgplus_post($a, $uid, $item->provider->title, $post, $location);
345                                         break;
346                         }
347         }
348         if ($lastdate != 0)
349                 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
350 }
351
352 /*
353 // Test
354 $test1 = array();
355 $test2 = array();
356 require_once("boot.php");
357 require_once("include/bbcode.php");
358 require_once("include/html2plain.php");
359
360 if(@is_null($a)) {
361         $a = new App;
362 }
363
364 if(@is_null($db)) {
365         @include(".htconfig.php");
366         require_once("include/dba.php");
367         $db = new dba($db_host, $db_user, $db_pass, $db_data);
368         unset($db_host, $db_user, $db_pass, $db_data);
369 };
370
371 fromgplus_cron($a, $test2);
372 */