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 require_once('mod/share.php');
13 require_once('mod/parse_url.php');
14 require_once('include/text.php');
16 function fromgplus_install() {
17 register_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
18 register_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
19 register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
22 function fromgplus_uninstall() {
23 unregister_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
24 unregister_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
25 unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
28 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
29 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
32 function fromgplus_addon_settings(&$a,&$s) {
37 // If "gpluspost" is installed as well, then the settings are displayed there
38 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
39 if (count($result) > 0)
42 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
43 $keywords_checked = (intval(get_pconfig(local_user(), 'fromgplus', 'keywords')) ? ' checked="checked"' : '');
44 $account = get_pconfig(local_user(),'fromgplus','account');
46 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
47 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
49 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
50 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
51 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
54 $s .= '<div id="fromgplus-wrapper">';
56 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
57 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
58 $s .= '<div class="clear"></div>';
59 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
60 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
61 $s .= '</div><div class="clear"></div>';
62 $s .= '<label id="fromgplus-keywords-label" for="fromgplus-keywords">'.t('Add keywords to post').'</label>';
63 $s .= '<input id="fromgplus-keywords" type="checkbox" name="fromgplus-keywords" value="1"'.$keywords_checked.' />';
64 $s .= '<div class="clear"></div>';
66 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
67 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
73 function fromgplus_addon_settings_post(&$a,&$b) {
78 if($_POST['fromgplus-submit']) {
79 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
80 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
81 set_pconfig(local_user(),'fromgplus','enable', $enable);
82 $keywords = ((x($_POST, 'fromgplus-keywords')) ? intval($_POST['fromgplus-keywords']) : 0);
83 set_pconfig(local_user(),'fromgplus', 'keywords', $keywords);
86 del_pconfig(local_user(),'fromgplus','lastdate');
88 info( t('Google+ Import Settings saved.') . EOL);
92 function fromgplus_plugin_admin(&$a, &$o){
93 $t = get_markup_template("admin.tpl", "addon/fromgplus/");
95 $o = replace_macros($t, array(
96 '$submit' => t('Save Settings'),
97 '$key' => array('key', t('Key'), trim(get_config('fromgplus', 'key')), t('')),
101 function fromgplus_plugin_admin_post(&$a){
102 $key = ((x($_POST,'key')) ? trim($_POST['key']) : '');
103 set_config('fromgplus','key',$key);
104 info( t('Settings updated.'). EOL );
107 function fromgplus_cron($a,$b) {
108 $last = get_config('fromgplus','last_poll');
110 $poll_interval = intval(get_config('fromgplus','poll_interval'));
112 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
115 $next = $last + ($poll_interval * 60);
117 logger('fromgplus: poll intervall not reached');
122 logger('fromgplus: cron_start');
124 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
127 $account = get_pconfig($rr['uid'],'fromgplus','account');
129 logger('fromgplus: fetching for user '.$rr['uid']);
130 fromgplus_fetch($a, $rr['uid']);
135 logger('fromgplus: cron_end');
137 set_config('fromgplus','last_poll', time());
140 function fromgplus_post($a, $uid, $source, $body, $location, $coord, $id) {
144 // Don't know what it is. Maybe some trash from the mobile client
145 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
146 $body = str_replace($trash, "", $body);
150 if (substr($body, 0, 3) == "[b]") {
151 $pos = strpos($body, "[/b]");
152 $title = substr($body, 3, $pos-3);
153 $body = trim(substr($body, $pos+4));
157 $_SESSION['authenticated'] = true;
158 $_SESSION['uid'] = $uid;
161 $_REQUEST['type'] = 'wall';
162 $_REQUEST['api_source'] = true;
164 $_REQUEST['profile_uid'] = $uid;
165 $_REQUEST['source'] = $source;
166 $_REQUEST['extid'] = NETWORK_GPLUS;
169 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_GPLUS.':'.$id);
173 // $_REQUEST['parent']
174 // $_REQUEST['parent_uri']
176 $_REQUEST['title'] = $title;
177 $_REQUEST['body'] = $body;
178 $_REQUEST['location'] = $location;
179 $_REQUEST['coord'] = $coord;
181 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
182 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
186 require_once('mod/item.php');
187 //print_r($_REQUEST);
188 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
190 logger('fromgplus: done for user '.$uid);
193 function fromgplus_html2bbcode($html) {
195 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
197 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
198 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
199 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
200 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
201 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
202 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
203 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
205 $bbcode = trim(strip_tags($bbcode));
209 function fromgplus_parse_query($var)
212 * Use this function to parse out the query array element from
213 * the output of parse_url().
215 $var = parse_url($var, PHP_URL_QUERY);
216 $var = html_entity_decode($var);
217 $var = explode('&', $var);
220 foreach($var as $val) {
221 $x = explode('=', $val);
224 unset($val, $x, $var);
228 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
229 //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
230 //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
231 //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
232 $fullImage = $fullImage->url;
234 //$preview = "/w".$image->width."-h".$image->height."/";
235 //$preview2 = "/w".$image->width."-h".$image->height."-p/";
236 //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
237 $image = $image->url;
241 $queryvar = fromgplus_parse_query($fullImage);
242 if ($queryvar['url'] != "")
243 $cleaned["full"] = urldecode($queryvar['url']);
245 $cleaned["full"] = $fullImage;
246 if (@exif_imagetype($cleaned["full"]) == 0)
247 $cleaned["full"] = "";
249 $queryvar = fromgplus_parse_query($image);
250 if ($queryvar['url'] != "")
251 $cleaned["preview"] = urldecode($queryvar['url']);
253 $cleaned["preview"] = $image;
254 if (@exif_imagetype($cleaned["preview"]) == 0)
255 $cleaned["preview"] = "";
257 if ($cleaned["full"] == "") {
258 $cleaned["full"] = $cleaned["preview"];
259 $cleaned["preview"] = "";
262 if ($cleaned["full"] != "")
263 $infoFull = get_photo_info($cleaned["full"]);
265 $infoFull = array("0" => 0, "1" => 0);
267 if ($cleaned["preview"] != "")
268 $infoPreview = get_photo_info($cleaned["preview"]);
270 $infoFull = array("0" => 0, "1" => 0);
272 if (($infoPreview[0] >= $infoFull[0]) AND ($infoPreview[1] >= $infoFull[1])) {
273 $temp = $cleaned["full"];
274 $cleaned["full"] = $cleaned["preview"];
275 $cleaned["preview"] = $temp;
278 if (($cleaned["full"] == $cleaned["preview"]) OR (($infoPreview[0] == $infoFull[0]) AND ($infoPreview[1] == $infoFull[1])))
279 $cleaned["preview"] = "";
281 if ($cleaned["full"] == "")
282 if (@exif_imagetype($fullImage) != 0)
283 $cleaned["full"] = $fullImage;
285 if ($cleaned["full"] == "")
286 if (@exif_imagetype($image) != 0)
287 $cleaned["full"] = $image;
289 // Could be changed in the future to a link to the album
290 $cleaned["page"] = $cleaned["full"];
295 function fromgplus_cleantext($text) {
297 // Don't know what it is. But it is added to the text.
298 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
300 $text = strip_tags($text);
301 $text = html_entity_decode($text, ENT_QUOTES);
303 $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
307 function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
308 require_once("include/Photo.php");
309 require_once("include/items.php");
310 require_once("include/network.php");
315 $pagedata["type"] = "";
317 foreach ($item->object->attachments as $attachment) {
318 switch($attachment->objectType) {
320 $pagedata["type"] = "video";
321 $pagedata["url"] = original_url($attachment->url);
322 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
326 $pagedata["type"] = "link";
327 $pagedata["url"] = original_url($attachment->url);
328 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
330 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
331 if ($images["full"] != "")
332 $pagedata["images"][0]["src"] = $images["full"];
334 $quote = trim(fromgplus_html2bbcode($attachment->content));
337 $pagedata["text"] = $quote;
339 // Add Keywords to page link
340 $data = parseurl_getsiteinfo_cached($pagedata["url"], true);
341 if (isset($data["keywords"]) AND get_pconfig($uid, 'fromgplus', 'keywords')) {
342 $pagedata["keywords"] = $data["keywords"];
347 // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
349 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
351 if ($attachment->fullImage->url != "")
352 $images = store_photo($a, $uid, "", $attachment->fullImage->url);
353 elseif ($attachment->image->url != "")
354 $images = store_photo($a, $uid, "", $attachment->image->url);
357 if ($images["preview"] != "") {
358 $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
359 $pagedata["images"][0]["src"] = $images["preview"];
360 $pagedata["url"] = $images["page"];
361 } elseif ($images["full"] != "") {
362 $post .= "\n[img]".$images["full"]."[/img]\n";
363 $pagedata["images"][0]["src"] = $images["full"];
365 if ($images["preview"] != "")
366 $pagedata["images"][1]["src"] = $images["preview"];
369 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext))) {
370 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
371 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
376 $pagedata["url"] = original_url($attachment->url);
377 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
378 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
380 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
382 if ($images["preview"] != "") {
383 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
384 $pagedata["images"][0]["src"] = $images["preview"];
385 $pagedata["url"] = $images["full"];
386 } elseif ($images["full"] != "") {
387 $post .= "\n[img]".$images["full"]."[/img]\n";
388 $pagedata["images"][0]["src"] = $images["full"];
390 if ($images["preview"] != "")
391 $pagedata["images"][1]["src"] = $images["preview"];
396 $pagedata["type"] = "link";
397 $pagedata["url"] = original_url($attachment->url);
398 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
400 $thumb = $attachment->thumbnails[0];
401 $pagedata["images"][0]["src"] = $thumb->image->url;
403 $quote = trim(fromgplus_html2bbcode($thumb->description));
405 $pagedata["text"] = $quote;
410 $pagedata["url"] = original_url($attachment->url);
411 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
412 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
416 // die($attachment->objectType);
420 if ($pagedata["type"] != "")
421 return(add_page_info_data($pagedata));
423 return($post.$quote);
426 function fromgplus_fetch($a, $uid) {
429 // Special blank to identify postings from the googleplus connector
430 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
432 $account = get_pconfig($uid,'fromgplus','account');
433 $key = get_config('fromgplus','key');
435 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
436 //$result = file_get_contents("google.txt");
437 //file_put_contents("google.txt", $result);
439 $activities = json_decode($result);
441 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
443 $first_time = ($initiallastdate == "");
447 if (!is_array($activities->items))
450 $reversed = array_reverse($activities->items);
452 foreach($reversed as $item) {
454 if (strtotime($item->published) <= $initiallastdate)
457 // Don't publish items that are too young
458 if (strtotime($item->published) > (time() - 3*60)) {
459 logger('fromgplus_fetch: item too new '.$item->published);
463 if ($lastdate < strtotime($item->published))
464 $lastdate = strtotime($item->published);
466 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
471 if ($item->access->description == "Public") {
473 // Loop prevention through the special blank from the googleplus connector
474 //if (strstr($item->object->content, $blank))
475 if (strrpos($item->object->content, $blank) >= strlen($item->object->content) - 5)
478 switch($item->object->objectType) {
480 $post = fromgplus_html2bbcode($item->object->content);
482 if (is_array($item->object->attachments))
483 $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
487 if (isset($item->location)) {
488 if (isset($item->location->address->formatted))
489 $location = $item->location->address->formatted;
491 if (isset($item->location->displayName))
492 $location = $item->location->displayName;
494 if (isset($item->location->position->latitude) AND
495 isset($item->location->position->longitude))
496 $coord = $item->location->position->latitude." ".$item->location->position->longitude;
498 } elseif (isset($item->address))
499 $location = $item->address;
501 fromgplus_post($a, $uid, $item->provider->title, $post, $location, $coord, $item->id);
506 $post = fromgplus_html2bbcode($item->annotation)."\n";
508 if (!intval(get_config('system','old_share'))) {
510 if (function_exists("share_header"))
511 $post .= share_header($item->object->actor->displayName, $item->object->actor->url,
512 $item->object->actor->image->url, "",
513 datetime_convert('UTC','UTC',$item->object->published),$item->object->url);
515 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
516 "' profile='".$item->object->actor->url.
517 "' avatar='".$item->object->actor->image->url.
518 "' posted='".datetime_convert('UTC','UTC',$item->object->published).
519 "' link='".$item->object->url."']";
521 $post .= fromgplus_html2bbcode($item->object->content);
523 if (is_array($item->object->attachments))
524 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
528 $post .= fromgplus_html2bbcode("♲");
529 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
530 $post .= fromgplus_html2bbcode($item->object->content);
532 if (is_array($item->object->attachments))
533 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
538 if (isset($item->location)) {
539 if (isset($item->location->address->formatted))
540 $location = $item->location->address->formatted;
542 if (isset($item->location->displayName))
543 $location = $item->location->displayName;
545 if (isset($item->location->position->latitude) AND
546 isset($item->location->position->longitude))
547 $coord = $item->location->position->latitude." ".$item->location->position->longitude;
549 } elseif (isset($item->address))
550 $location = $item->address;
552 fromgplus_post($a, $uid, $item->provider->title, $post, $location, $coord, $item->id);
558 set_pconfig($uid,'fromgplus','lastdate', $lastdate);