4 * Description: Imports posts from a Google+ account and repeats them
6 * Author: Michael Vogel <ike@piratenpartei.de>
10 define('FROMGPLUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
12 use Friendica\Core\Addon;
13 use Friendica\Core\Config;
14 use Friendica\Core\PConfig;
15 use Friendica\Object\Image;
17 require_once 'mod/share.php';
18 require_once 'mod/parse_url.php';
19 require_once 'include/text.php';
21 function fromgplus_install() {
22 Addon::registerHook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
23 Addon::registerHook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
24 Addon::registerHook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
27 function fromgplus_uninstall() {
28 Addon::unregisterHook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
29 Addon::unregisterHook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
30 Addon::unregisterHook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
33 Addon::unregisterHook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
34 Addon::unregisterHook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
37 function fromgplus_addon_settings(&$a,&$s) {
42 // If "gpluspost" is installed as well, then the settings are displayed there
43 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
44 if (count($result) > 0)
47 $enable_checked = (intval(PConfig::get(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
48 $keywords_checked = (intval(PConfig::get(local_user(), 'fromgplus', 'keywords')) ? ' checked="checked"' : '');
49 $account = PConfig::get(local_user(),'fromgplus','account');
51 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
52 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
54 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
55 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
56 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
59 $s .= '<div id="fromgplus-wrapper">';
61 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
62 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
63 $s .= '<div class="clear"></div>';
64 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
65 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
66 $s .= '</div><div class="clear"></div>';
67 $s .= '<label id="fromgplus-keywords-label" for="fromgplus-keywords">'.t('Add keywords to post').'</label>';
68 $s .= '<input id="fromgplus-keywords" type="checkbox" name="fromgplus-keywords" value="1"'.$keywords_checked.' />';
69 $s .= '<div class="clear"></div>';
71 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
72 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
78 function fromgplus_addon_settings_post(&$a,&$b) {
83 if($_POST['fromgplus-submit']) {
84 PConfig::set(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
85 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
86 PConfig::set(local_user(),'fromgplus','enable', $enable);
87 $keywords = ((x($_POST, 'fromgplus-keywords')) ? intval($_POST['fromgplus-keywords']) : 0);
88 PConfig::set(local_user(),'fromgplus', 'keywords', $keywords);
91 PConfig::delete(local_user(),'fromgplus','lastdate');
93 info( t('Google+ Import Settings saved.') . EOL);
97 function fromgplus_plugin_admin(&$a, &$o){
98 $t = get_markup_template("admin.tpl", "addon/fromgplus/");
100 $o = replace_macros($t, [
101 '$submit' => t('Save Settings'),
102 '$key' => ['key', t('Key'), trim(Config::get('fromgplus', 'key')), t('')],
106 function fromgplus_plugin_admin_post(&$a){
107 $key = ((x($_POST,'key')) ? trim($_POST['key']) : '');
108 Config::set('fromgplus','key',$key);
109 info( t('Settings updated.'). EOL );
112 function fromgplus_cron($a,$b) {
113 $last = Config::get('fromgplus','last_poll');
115 $poll_interval = intval(Config::get('fromgplus','poll_interval'));
117 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
120 $next = $last + ($poll_interval * 60);
122 logger('fromgplus: poll intervall not reached');
127 logger('fromgplus: cron_start');
129 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
132 $account = PConfig::get($rr['uid'],'fromgplus','account');
134 logger('fromgplus: fetching for user '.$rr['uid']);
135 fromgplus_fetch($a, $rr['uid']);
140 logger('fromgplus: cron_end');
142 Config::set('fromgplus','last_poll', time());
145 function fromgplus_post($a, $uid, $source, $body, $location, $coord, $id) {
149 // Don't know what it is. Maybe some trash from the mobile client
150 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
151 $body = str_replace($trash, "", $body);
155 if (substr($body, 0, 3) == "[b]") {
156 $pos = strpos($body, "[/b]");
157 $title = substr($body, 3, $pos-3);
158 $body = trim(substr($body, $pos+4));
162 $_SESSION['authenticated'] = true;
163 $_SESSION['uid'] = $uid;
166 $_REQUEST['type'] = 'wall';
167 $_REQUEST['api_source'] = true;
169 $_REQUEST['profile_uid'] = $uid;
170 $_REQUEST['source'] = $source;
171 $_REQUEST['extid'] = NETWORK_GPLUS;
174 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_GPLUS.':'.$id);
178 // $_REQUEST['parent']
179 // $_REQUEST['parent_uri']
181 $_REQUEST['title'] = $title;
182 $_REQUEST['body'] = $body;
183 $_REQUEST['location'] = $location;
184 $_REQUEST['coord'] = $coord;
186 if (($_REQUEST['title'] == "") && ($_REQUEST['body'] == "")) {
187 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
191 require_once('mod/item.php');
192 //print_r($_REQUEST);
193 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
195 logger('fromgplus: done for user '.$uid);
198 function fromgplus_html2bbcode($html) {
200 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
202 $bbcode = str_ireplace(["\n"], [""], $bbcode);
203 $bbcode = str_ireplace(["<b>", "</b>"], ["[b]", "[/b]"], $bbcode);
204 $bbcode = str_ireplace(["<i>", "</i>"], ["[i]", "[/i]"], $bbcode);
205 $bbcode = str_ireplace(["<s>", "</s>"], ["[s]", "[/s]"], $bbcode);
206 $bbcode = str_ireplace(["<br />"], ["\n"], $bbcode);
207 $bbcode = str_ireplace(["<br/>"], ["\n"], $bbcode);
208 $bbcode = str_ireplace(["<br>"], ["\n"], $bbcode);
210 $bbcode = trim(strip_tags($bbcode));
214 function fromgplus_parse_query($var)
217 * Use this function to parse out the query array element from
218 * the output of parse_url().
220 $var = parse_url($var, PHP_URL_QUERY);
221 $var = html_entity_decode($var);
222 $var = explode('&', $var);
225 foreach($var as $val) {
226 $x = explode('=', $val);
229 unset($val, $x, $var);
233 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
234 //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
235 //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
236 //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
237 $fullImage = $fullImage->url;
239 //$preview = "/w".$image->width."-h".$image->height."/";
240 //$preview2 = "/w".$image->width."-h".$image->height."-p/";
241 //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
242 $image = $image->url;
246 $queryvar = fromgplus_parse_query($fullImage);
247 if ($queryvar['url'] != "")
248 $cleaned["full"] = urldecode($queryvar['url']);
250 $cleaned["full"] = $fullImage;
251 if (@exif_imagetype($cleaned["full"]) == 0)
252 $cleaned["full"] = "";
254 $queryvar = fromgplus_parse_query($image);
255 if ($queryvar['url'] != "")
256 $cleaned["preview"] = urldecode($queryvar['url']);
258 $cleaned["preview"] = $image;
259 if (@exif_imagetype($cleaned["preview"]) == 0)
260 $cleaned["preview"] = "";
262 if ($cleaned["full"] == "") {
263 $cleaned["full"] = $cleaned["preview"];
264 $cleaned["preview"] = "";
267 if ($cleaned["full"] != "")
268 $infoFull = Image::getInfoFromURL($cleaned["full"]);
270 $infoFull = ["0" => 0, "1" => 0];
272 if ($cleaned["preview"] != "")
273 $infoPreview = Image::getInfoFromURL($cleaned["preview"]);
275 $infoFull = ["0" => 0, "1" => 0];
277 if (($infoPreview[0] >= $infoFull[0]) && ($infoPreview[1] >= $infoFull[1])) {
278 $temp = $cleaned["full"];
279 $cleaned["full"] = $cleaned["preview"];
280 $cleaned["preview"] = $temp;
283 if (($cleaned["full"] == $cleaned["preview"]) || (($infoPreview[0] == $infoFull[0]) && ($infoPreview[1] == $infoFull[1])))
284 $cleaned["preview"] = "";
286 if ($cleaned["full"] == "")
287 if (@exif_imagetype($fullImage) != 0)
288 $cleaned["full"] = $fullImage;
290 if ($cleaned["full"] == "")
291 if (@exif_imagetype($image) != 0)
292 $cleaned["full"] = $image;
294 // Could be changed in the future to a link to the album
295 $cleaned["page"] = $cleaned["full"];
300 function fromgplus_cleantext($text) {
302 // Don't know what it is. But it is added to the text.
303 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
305 $text = strip_tags($text);
306 $text = html_entity_decode($text, ENT_QUOTES);
308 $text = str_replace(["\n", "\r", " ", $trash], ["", "", "", ""], $text);
312 function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
313 require_once("include/items.php");
314 require_once("include/network.php");
319 $pagedata["type"] = "";
321 foreach ($item->object->attachments as $attachment) {
322 switch($attachment->objectType) {
324 $pagedata["type"] = "video";
325 $pagedata["url"] = original_url($attachment->url);
326 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
330 $pagedata["type"] = "link";
331 $pagedata["url"] = original_url($attachment->url);
332 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
334 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
335 if ($images["full"] != "")
336 $pagedata["images"][0]["src"] = $images["full"];
338 $quote = trim(fromgplus_html2bbcode($attachment->content));
341 $pagedata["text"] = $quote;
343 // Add Keywords to page link
344 $data = parseurl_getsiteinfo_cached($pagedata["url"], true);
345 if (isset($data["keywords"]) && PConfig::get($uid, 'fromgplus', 'keywords')) {
346 $pagedata["keywords"] = $data["keywords"];
351 // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
353 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
355 if ($attachment->fullImage->url != "") {
356 $images = Image::storePhoto($a, $uid, "", $attachment->fullImage->url);
357 } elseif ($attachment->image->url != "") {
358 $images = Image::storePhoto($a, $uid, "", $attachment->image->url);
362 if ($images["preview"] != "") {
363 $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
364 $pagedata["images"][0]["src"] = $images["preview"];
365 $pagedata["url"] = $images["page"];
366 } elseif ($images["full"] != "") {
367 $post .= "\n[img]".$images["full"]."[/img]\n";
368 $pagedata["images"][0]["src"] = $images["full"];
370 if ($images["preview"] != "") {
371 $pagedata["images"][1]["src"] = $images["preview"];
375 if (($attachment->displayName != "") && (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext))) {
376 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
377 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
382 $pagedata["url"] = original_url($attachment->url);
383 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
384 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
386 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
388 if ($images["preview"] != "") {
389 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
390 $pagedata["images"][0]["src"] = $images["preview"];
391 $pagedata["url"] = $images["full"];
392 } elseif ($images["full"] != "") {
393 $post .= "\n[img]".$images["full"]."[/img]\n";
394 $pagedata["images"][0]["src"] = $images["full"];
396 if ($images["preview"] != "")
397 $pagedata["images"][1]["src"] = $images["preview"];
402 $pagedata["type"] = "link";
403 $pagedata["url"] = original_url($attachment->url);
404 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
406 $thumb = $attachment->thumbnails[0];
407 $pagedata["images"][0]["src"] = $thumb->image->url;
409 $quote = trim(fromgplus_html2bbcode($thumb->description));
411 $pagedata["text"] = $quote;
416 $pagedata["url"] = original_url($attachment->url);
417 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
418 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
422 // die($attachment->objectType);
426 if ($pagedata["type"] != "")
427 return(add_page_info_data($pagedata));
429 return($post.$quote);
432 function fromgplus_fetch($a, $uid) {
435 // Special blank to identify postings from the googleplus connector
436 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
438 $account = PConfig::get($uid,'fromgplus','account');
439 $key = Config::get('fromgplus','key');
441 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
442 //$result = file_get_contents("google.txt");
443 //file_put_contents("google.txt", $result);
445 $activities = json_decode($result);
447 $initiallastdate = PConfig::get($uid,'fromgplus','lastdate');
449 $first_time = ($initiallastdate == "");
453 if (!is_array($activities->items))
456 $reversed = array_reverse($activities->items);
458 foreach($reversed as $item) {
460 if (strtotime($item->published) <= $initiallastdate)
463 // Don't publish items that are too young
464 if (strtotime($item->published) > (time() - 3*60)) {
465 logger('fromgplus_fetch: item too new '.$item->published);
469 if ($lastdate < strtotime($item->published))
470 $lastdate = strtotime($item->published);
472 PConfig::set($uid,'fromgplus','lastdate', $lastdate);
477 if ($item->access->description == "Public") {
479 // Loop prevention through the special blank from the googleplus connector
480 //if (strstr($item->object->content, $blank))
481 if (strrpos($item->object->content, $blank) >= strlen($item->object->content) - 5)
484 switch($item->object->objectType) {
486 $post = fromgplus_html2bbcode($item->object->content);
488 if (is_array($item->object->attachments))
489 $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
493 if (isset($item->location)) {
494 if (isset($item->location->address->formatted))
495 $location = $item->location->address->formatted;
497 if (isset($item->location->displayName))
498 $location = $item->location->displayName;
500 if (isset($item->location->position->latitude) &&
501 isset($item->location->position->longitude))
502 $coord = $item->location->position->latitude." ".$item->location->position->longitude;
504 } elseif (isset($item->address))
505 $location = $item->address;
507 fromgplus_post($a, $uid, $item->provider->title, $post, $location, $coord, $item->id);
512 $post = fromgplus_html2bbcode($item->annotation)."\n";
514 if (!intval(Config::get('system','old_share'))) {
516 if (function_exists("share_header"))
517 $post .= share_header($item->object->actor->displayName, $item->object->actor->url,
518 $item->object->actor->image->url, "",
519 datetime_convert('UTC','UTC',$item->object->published),$item->object->url);
521 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
522 "' profile='".$item->object->actor->url.
523 "' avatar='".$item->object->actor->image->url.
524 "' posted='".datetime_convert('UTC','UTC',$item->object->published).
525 "' link='".$item->object->url."']";
527 $post .= fromgplus_html2bbcode($item->object->content);
529 if (is_array($item->object->attachments))
530 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
534 $post .= fromgplus_html2bbcode("♲");
535 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
536 $post .= fromgplus_html2bbcode($item->object->content);
538 if (is_array($item->object->attachments))
539 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
544 if (isset($item->location)) {
545 if (isset($item->location->address->formatted))
546 $location = $item->location->address->formatted;
548 if (isset($item->location->displayName))
549 $location = $item->location->displayName;
551 if (isset($item->location->position->latitude) &&
552 isset($item->location->position->longitude))
553 $coord = $item->location->position->latitude." ".$item->location->position->longitude;
555 } elseif (isset($item->address))
556 $location = $item->address;
558 fromgplus_post($a, $uid, $item->provider->title, $post, $location, $coord, $item->id);
564 PConfig::set($uid,'fromgplus','lastdate', $lastdate);