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');
15 function fromgplus_install() {
16 register_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
17 register_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
18 register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
21 function fromgplus_uninstall() {
22 unregister_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
23 unregister_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
24 unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron');
27 unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
28 unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post');
31 function fromgplus_addon_settings(&$a,&$s) {
36 // If "gpluspost" is installed as well, then the settings are displayed there
37 $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
38 if (count($result) > 0)
41 $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
42 $keywords_checked = (intval(get_pconfig(local_user(), 'fromgplus', 'keywords')) ? ' checked="checked"' : '');
43 $account = get_pconfig(local_user(),'fromgplus','account');
45 $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
46 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
48 $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
49 $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
50 $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
53 $s .= '<div id="fromgplus-wrapper">';
55 $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
56 $s .= '<input id="fromgplus-enable" type="checkbox" name="fromgplus-enable" value="1"'.$enable_checked.' />';
57 $s .= '<div class="clear"></div>';
58 $s .= '<label id="fromgplus-label" for="fromgplus-account">'.t('Google Account ID').' </label>';
59 $s .= '<input id="fromgplus-account" type="text" name="fromgplus-account" value="'.$account.'" />';
60 $s .= '</div><div class="clear"></div>';
61 $s .= '<label id="fromgplus-keywords-label" for="fromgplus-keywords">'.t('Add keywords to post').'</label>';
62 $s .= '<input id="fromgplus-keywords" type="checkbox" name="fromgplus-keywords" value="1"'.$keywords_checked.' />';
63 $s .= '<div class="clear"></div>';
65 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit"
66 class="settings-submit" value="' . t('Save Settings') . '" /></div>';
72 function fromgplus_addon_settings_post(&$a,&$b) {
77 if($_POST['fromgplus-submit']) {
78 set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
79 $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
80 set_pconfig(local_user(),'fromgplus','enable', $enable);
81 $keywords = ((x($_POST, 'fromgplus-keywords')) ? intval($_POST['fromgplus-keywords']) : 0);
82 set_pconfig(local_user(),'fromgplus', 'keywords', $keywords);
85 del_pconfig(local_user(),'fromgplus','lastdate');
87 info( t('Google+ Import Settings saved.') . EOL);
91 function fromgplus_plugin_admin(&$a, &$o){
92 $t = get_markup_template("admin.tpl", "addon/fromgplus/");
94 $o = replace_macros($t, array(
95 '$submit' => t('Save Settings'),
96 '$key' => array('key', t('Key'), trim(get_config('fromgplus', 'key')), t('')),
100 function fromgplus_plugin_admin_post(&$a){
101 $key = ((x($_POST,'key')) ? trim($_POST['key']) : '');
102 set_config('fromgplus','key',$key);
103 info( t('Settings updated.'). EOL );
106 function fromgplus_cron($a,$b) {
107 $last = get_config('fromgplus','last_poll');
109 $poll_interval = intval(get_config('fromgplus','poll_interval'));
111 $poll_interval = FROMGPLUS_DEFAULT_POLL_INTERVAL;
114 $next = $last + ($poll_interval * 60);
116 logger('fromgplus: poll intervall not reached');
121 logger('fromgplus: cron_start');
123 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fromgplus' AND `k` = 'enable' AND `v` = '1' ORDER BY RAND() ");
126 $account = get_pconfig($rr['uid'],'fromgplus','account');
128 logger('fromgplus: fetching for user '.$rr['uid']);
129 fromgplus_fetch($a, $rr['uid']);
134 logger('fromgplus: cron_end');
136 set_config('fromgplus','last_poll', time());
139 function fromgplus_post($a, $uid, $source, $body, $location, $coord, $id) {
143 // Don't know what it is. Maybe some trash from the mobile client
144 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
145 $body = str_replace($trash, "", $body);
149 if (substr($body, 0, 3) == "[b]") {
150 $pos = strpos($body, "[/b]");
151 $title = substr($body, 3, $pos-3);
152 $body = trim(substr($body, $pos+4));
156 $_SESSION['authenticated'] = true;
157 $_SESSION['uid'] = $uid;
160 $_REQUEST['type'] = 'wall';
161 $_REQUEST['api_source'] = true;
163 $_REQUEST['profile_uid'] = $uid;
164 $_REQUEST['source'] = $source;
165 $_REQUEST['extid'] = NETWORK_GPLUS;
168 $_REQUEST['message_id'] = NETWORK_GPLUS.":".$id;
171 // $_REQUEST['parent']
172 // $_REQUEST['parent_uri']
174 $_REQUEST['title'] = $title;
175 $_REQUEST['body'] = $body;
176 $_REQUEST['location'] = $location;
177 $_REQUEST['coord'] = $coord;
179 if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
180 logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
184 require_once('mod/item.php');
185 //print_r($_REQUEST);
186 logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
188 logger('fromgplus: done for user '.$uid);
191 function fromgplus_html2bbcode($html) {
193 $bbcode = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
195 $bbcode = str_ireplace(array("\n"), array(""), $bbcode);
196 $bbcode = str_ireplace(array("<b>", "</b>"), array("[b]", "[/b]"), $bbcode);
197 $bbcode = str_ireplace(array("<i>", "</i>"), array("[i]", "[/i]"), $bbcode);
198 $bbcode = str_ireplace(array("<s>", "</s>"), array("[s]", "[/s]"), $bbcode);
199 $bbcode = str_ireplace(array("<br />"), array("\n"), $bbcode);
200 $bbcode = str_ireplace(array("<br/>"), array("\n"), $bbcode);
201 $bbcode = str_ireplace(array("<br>"), array("\n"), $bbcode);
203 $bbcode = trim(strip_tags($bbcode));
207 function fromgplus_parse_query($var)
210 * Use this function to parse out the query array element from
211 * the output of parse_url().
213 $var = parse_url($var, PHP_URL_QUERY);
214 $var = html_entity_decode($var);
215 $var = explode('&', $var);
218 foreach($var as $val) {
219 $x = explode('=', $val);
222 unset($val, $x, $var);
226 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
227 //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
228 //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
229 //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
230 $fullImage = $fullImage->url;
232 //$preview = "/w".$image->width."-h".$image->height."/";
233 //$preview2 = "/w".$image->width."-h".$image->height."-p/";
234 //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
235 $image = $image->url;
239 $queryvar = fromgplus_parse_query($fullImage);
240 if ($queryvar['url'] != "")
241 $cleaned["full"] = urldecode($queryvar['url']);
243 $cleaned["full"] = $fullImage;
244 if (@exif_imagetype($cleaned["full"]) == 0)
245 $cleaned["full"] = "";
247 $queryvar = fromgplus_parse_query($image);
248 if ($queryvar['url'] != "")
249 $cleaned["preview"] = urldecode($queryvar['url']);
251 $cleaned["preview"] = $image;
252 if (@exif_imagetype($cleaned["preview"]) == 0)
253 $cleaned["preview"] = "";
255 if ($cleaned["full"] == "") {
256 $cleaned["full"] = $cleaned["preview"];
257 $cleaned["preview"] = "";
260 if ($cleaned["full"] != "")
261 $infoFull = get_photo_info($cleaned["full"]);
263 $infoFull = array("0" => 0, "1" => 0);
265 if ($cleaned["preview"] != "")
266 $infoPreview = get_photo_info($cleaned["preview"]);
268 $infoFull = array("0" => 0, "1" => 0);
270 if (($infoPreview[0] >= $infoFull[0]) AND ($infoPreview[1] >= $infoFull[1])) {
271 $temp = $cleaned["full"];
272 $cleaned["full"] = $cleaned["preview"];
273 $cleaned["preview"] = $temp;
276 if (($cleaned["full"] == $cleaned["preview"]) OR (($infoPreview[0] == $infoFull[0]) AND ($infoPreview[1] == $infoFull[1])))
277 $cleaned["preview"] = "";
279 if ($cleaned["full"] == "")
280 if (@exif_imagetype($fullImage) != 0)
281 $cleaned["full"] = $fullImage;
283 if ($cleaned["full"] == "")
284 if (@exif_imagetype($image) != 0)
285 $cleaned["full"] = $image;
287 // Could be changed in the future to a link to the album
288 $cleaned["page"] = $cleaned["full"];
293 function fromgplus_cleantext($text) {
295 // Don't know what it is. But it is added to the text.
296 $trash = html_entity_decode("", ENT_QUOTES, 'UTF-8');
298 $text = strip_tags($text);
299 $text = html_entity_decode($text, ENT_QUOTES);
301 $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
305 function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
306 require_once("include/Photo.php");
307 require_once("include/items.php");
308 require_once("include/network.php");
313 $pagedata["type"] = "";
315 foreach ($item->object->attachments as $attachment) {
316 switch($attachment->objectType) {
318 $pagedata["type"] = "video";
319 $pagedata["url"] = original_url($attachment->url);
320 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
324 $pagedata["type"] = "link";
325 $pagedata["url"] = original_url($attachment->url);
326 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
328 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
329 if ($images["full"] != "")
330 $pagedata["images"][0]["src"] = $images["full"];
332 $quote = trim(fromgplus_html2bbcode($attachment->content));
335 $pagedata["text"] = $quote;
337 // Add Keywords to page link
338 $data = parseurl_getsiteinfo_cached($pagedata["url"], true);
339 if (isset($data["keywords"]) AND get_pconfig($uid, 'fromgplus', 'keywords')) {
340 $pagedata["keywords"] = $data["keywords"];
345 // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
347 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
349 if ($attachment->fullImage->url != "")
350 $images = store_photo($a, $uid, "", $attachment->fullImage->url);
351 elseif ($attachment->image->url != "")
352 $images = store_photo($a, $uid, "", $attachment->image->url);
355 if ($images["preview"] != "") {
356 $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
357 $pagedata["images"][0]["src"] = $images["preview"];
358 $pagedata["url"] = $images["page"];
359 } elseif ($images["full"] != "") {
360 $post .= "\n[img]".$images["full"]."[/img]\n";
361 $pagedata["images"][0]["src"] = $images["full"];
363 if ($images["preview"] != "")
364 $pagedata["images"][1]["src"] = $images["preview"];
367 if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext))) {
368 $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
369 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
374 $pagedata["url"] = original_url($attachment->url);
375 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
376 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
378 $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
380 if ($images["preview"] != "") {
381 $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
382 $pagedata["images"][0]["src"] = $images["preview"];
383 $pagedata["url"] = $images["full"];
384 } elseif ($images["full"] != "") {
385 $post .= "\n[img]".$images["full"]."[/img]\n";
386 $pagedata["images"][0]["src"] = $images["full"];
388 if ($images["preview"] != "")
389 $pagedata["images"][1]["src"] = $images["preview"];
394 $pagedata["type"] = "link";
395 $pagedata["url"] = original_url($attachment->url);
396 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
398 $thumb = $attachment->thumbnails[0];
399 $pagedata["images"][0]["src"] = $thumb->image->url;
401 $quote = trim(fromgplus_html2bbcode($thumb->description));
403 $pagedata["text"] = $quote;
408 $pagedata["url"] = original_url($attachment->url);
409 $pagedata["title"] = fromgplus_html2bbcode($attachment->displayName);
410 $post .= "\n\n[bookmark=".$pagedata["url"]."]".$pagedata["title"]."[/bookmark]\n";
414 // die($attachment->objectType);
418 if ($pagedata["type"] != "")
419 return(add_page_info_data($pagedata));
421 return($post.$quote);
424 function fromgplus_fetch($a, $uid) {
427 // Special blank to identify postings from the googleplus connector
428 $blank = html_entity_decode(" ", ENT_QUOTES, 'UTF-8');
430 $account = get_pconfig($uid,'fromgplus','account');
431 $key = get_config('fromgplus','key');
433 $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
434 //$result = file_get_contents("google.txt");
435 //file_put_contents("google.txt", $result);
437 $activities = json_decode($result);
439 $initiallastdate = get_pconfig($uid,'fromgplus','lastdate');
441 $first_time = ($initiallastdate == "");
445 if (!is_array($activities->items))
448 $reversed = array_reverse($activities->items);
450 foreach($reversed as $item) {
452 if (strtotime($item->published) <= $initiallastdate)
455 // Don't publish items that are too young
456 if (strtotime($item->published) > (time() - 3*60)) {
457 logger('fromgplus_fetch: item too new '.$item->published);
461 if ($lastdate < strtotime($item->published))
462 $lastdate = strtotime($item->published);
464 set_pconfig($uid,'fromgplus','lastdate', $lastdate);
469 if ($item->access->description == "Public") {
471 // Loop prevention through the special blank from the googleplus connector
472 //if (strstr($item->object->content, $blank))
473 if (strrpos($item->object->content, $blank) >= strlen($item->object->content) - 5)
476 switch($item->object->objectType) {
478 $post = fromgplus_html2bbcode($item->object->content);
480 if (is_array($item->object->attachments))
481 $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
485 if (isset($item->location)) {
486 if (isset($item->location->address->formatted))
487 $location = $item->location->address->formatted;
489 if (isset($item->location->displayName))
490 $location = $item->location->displayName;
492 if (isset($item->location->position->latitude) AND
493 isset($item->location->position->longitude))
494 $coord = $item->location->position->latitude." ".$item->location->position->longitude;
496 } elseif (isset($item->address))
497 $location = $item->address;
499 fromgplus_post($a, $uid, $item->provider->title, $post, $location, $coord, $item->id);
504 $post = fromgplus_html2bbcode($item->annotation)."\n";
506 if (!intval(get_config('system','old_share'))) {
508 if (function_exists("share_header"))
509 $post .= share_header($item->object->actor->displayName, $item->object->actor->url,
510 $item->object->actor->image->url, "",
511 datetime_convert('UTC','UTC',$item->object->published),$item->object->url);
513 $post .= "[share author='".str_replace("'", "'",$item->object->actor->displayName).
514 "' profile='".$item->object->actor->url.
515 "' avatar='".$item->object->actor->image->url.
516 "' posted='".datetime_convert('UTC','UTC',$item->object->published).
517 "' link='".$item->object->url."']";
519 $post .= fromgplus_html2bbcode($item->object->content);
521 if (is_array($item->object->attachments))
522 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
526 $post .= fromgplus_html2bbcode("♲");
527 $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
528 $post .= fromgplus_html2bbcode($item->object->content);
530 if (is_array($item->object->attachments))
531 $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
536 if (isset($item->location)) {
537 if (isset($item->location->address->formatted))
538 $location = $item->location->address->formatted;
540 if (isset($item->location->displayName))
541 $location = $item->location->displayName;
543 if (isset($item->location->position->latitude) AND
544 isset($item->location->position->longitude))
545 $coord = $item->location->position->latitude." ".$item->location->position->longitude;
547 } elseif (isset($item->address))
548 $location = $item->address;
550 fromgplus_post($a, $uid, $item->provider->title, $post, $location, $coord, $item->id);
556 set_pconfig($uid,'fromgplus','lastdate', $lastdate);